-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdsl_scratch.txt
583 lines (387 loc) · 11.6 KB
/
dsl_scratch.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
for EqPredicate: add this to IdentResolveTypeAssertMap
"*any": func(arg string, resolver IdentifierResolver) (any, error) {
return AssertPointer(resolver.Resolve(arg), "*any")
},
this allows us to match on arbitrary pointers
where AssertPointer is probably going to need reflect?
this way when the signature receives its args, if it has IdentResolve<*any> it'll know, ah, this is a pointer to something somewhere, with no further info, just a pointer. This is useful for object comparison (is this entity the big bad goblin? pointer==pointer)
add val types to lexer/parser:
- numeric
- bool
- string (with quotes **** strings must have quotes or else "mind.something" is an invalid string)
(this implies "valueOrEntityAccess" will look different, in addition to other parts probably)
x. Notation (but we don't have x in the same scope as e... hmmmm)
(sub-problem: . notation generally, like instead of HasTag we should have either e.HasTag or x.HasTag)
overloaded(?) int,int bool,bool float,float string,string pointer,pointer Eq
Numeric (+attempt dereference) comparator predicates (Lt(x,y))
*Vec2D component access (access self[position].X for example)
Spatial hash if WithinDistance (change withindistance to a literal part of the syntax. like <Distance>;<FilterExpr>;<SortExpr>;
the "problem" of the reflection/switch from https://chat.openai.com/chat/715d54db-81e1-46ee-b4b0-137c305df659
is a problem in search of a solution
even for users who want to extend to their own entity filter DSL (EFDSL = Extremely Fucking Delicious Spaghetti Lasagna)
with their own predicates, they can use the existing predicates to implement their logic assuming:
they put relevant data they want to predicate over into a component/blackboard/tag/state
for example if the game was chess, you don't need to implement CanQueen for a pawn, you can just
look at
Eq(self<rank>, 7)
actually that's not a good example. rather anyway abstrawctly put, you'd compute down some complex query into a flag every so often, so u can just match on it
code generation:
engine:
list of types we want supported -> EntityFilterDSLPredicate()
(EntityFilterDSLPredicate() checks for wrapper overrides from user)
user:
list of types we want supported -> predicateTypeSignatureWrapperOverrides
"CanBe": EntityFilterDSLPredicate(
"string, int",
func(args []any) func(*Entity) bool {
t, err := DSLAssertArgTypes("string,int", args, resolver)
if err != nil {
logDSLError("%s", err)
}
k, v := t[0].([]string), t[0].([]string)
return func(x *Entity) bool {
return x.HasComponent(STATE) && x.GetIntMap(STATE).ValCanBeSetTo(k, v)
}
},
),
"CanBe": EntityFilterDSLPredicate(
"string, int",
func(k string, v int) func(*Entity) bool {
return func(x *Entity) bool {
return x.HasComponent(STATE) && x.GetIntMap(STATE).ValCanBeSetTo(k, v)
}
},
),
Here are some expressions in a DSL for entity selection/filtering i want to use in my game engine. I've written a lexer so far.
(ignore the wrapping call to lex(), it just echoes the input stirng and then lexes it token by token)
lex(`HasTag(ox) && CanBe(yoked, 1); Closest(mind.field)`)
lex(`First(HasTag(ox) && CanBe(yoked, 1); Closest(mind.field))`)
lex(`VillagerOf(self.village)`)
lex(`!VillagerOf(self.village)`)
lex(`Is(bb.village1.strongest)`)
lex(`HasTag(deer); Closest(self)`)
lex(`HasTags(ox,legendary)`)
lex(`Closest(self)`)
with the corresponding test output:
```
HasTag(ox) && CanBe(yoked, 1); Closest(mind.field)
Function: HasTag
OpenParen:
Identifier: ox
CloseParen:
And:
Function: CanBe
OpenParen:
Identifier: yoked
Comma:
Identifier: 1
CloseParen:
Semicolon:
Function: Closest
OpenParen:
Identifier: mind.field
CloseParen:
First(HasTag(ox) && CanBe(yoked, 1); Closest(mind.field))
Function: First
OpenParen:
Function: HasTag
OpenParen:
Identifier: ox
CloseParen:
And:
Function: CanBe
OpenParen:
Identifier: yoked
Comma:
Identifier: 1
CloseParen:
Semicolon:
Function: Closest
OpenParen:
Identifier: mind.field
CloseParen:
CloseParen:
VillagerOf(self.village)
Function: VillagerOf
OpenParen:
Identifier: self.village
CloseParen:
!VillagerOf(self.village)
Not:
Function: VillagerOf
OpenParen:
Identifier: self.village
CloseParen:
Is(bb.village1.strongest)
Function: Is
OpenParen:
Identifier: bb.village1.strongest
CloseParen:
HasTag(deer); Closest(self)
Function: HasTag
OpenParen:
Identifier: deer
CloseParen:
Semicolon:
Function: Closest
OpenParen:
Identifier: self
CloseParen:
HasTags(ox,legendary)
Function: HasTags
OpenParen:
Identifier: ox
Comma:
Identifier: legendary
CloseParen:
Closest(self)
Function: Closest
OpenParen:
Identifier: self
CloseParen:
```
and here is the lexer code:
```
package sameriver
import (
"strings"
"text/scanner"
"unicode"
)
type EntityFilterDSLToken int
const (
EOF EntityFilterDSLToken = iota
Not
And
Or
Function
Identifier
OpenParen
CloseParen
Comma
Semicolon
)
func (t EntityFilterDSLToken) String() string {
switch t {
case EOF:
return "EOF"
case Not:
return "Not"
case And:
return "And"
case Or:
return "Or"
case Function:
return "Function"
case Identifier:
return "Identifier"
case OpenParen:
return "OpenParen"
case CloseParen:
return "CloseParen"
case Comma:
return "Comma"
case Semicolon:
return "Semicolon"
default:
return "Unknown"
}
}
type EntityFilterDSLLexer struct {
scanner.Scanner
token EntityFilterDSLToken
stringValue string
}
func (l *EntityFilterDSLLexer) IsEOF() bool {
return l.Peek() == scanner.EOF
}
func (l *EntityFilterDSLLexer) TokenText() string {
return l.stringValue
}
func (l *EntityFilterDSLLexer) Lex() EntityFilterDSLToken {
l.stringValue = ""
l.token = EOF
for !l.IsEOF() {
r := l.Peek()
if unicode.IsSpace(r) {
l.Next()
continue
}
switch {
case r == '!':
l.Next()
l.token = Not
case r == '&':
l.Next()
if l.Peek() == '&' {
l.Next()
l.token = And
} else {
l.token = EOF
}
case r == '|':
l.Next()
if l.Peek() == '|' {
l.Next()
l.token = Or
} else {
l.token = EOF
}
case r == '(':
l.Next()
l.token = OpenParen
case r == ')':
l.Next()
l.token = CloseParen
case r == ',':
l.Next()
l.token = Comma
case r == ';':
l.Next()
l.token = Semicolon
case unicode.IsUpper(r):
str := l.scanString(func(r rune) bool {
return unicode.IsLetter(r)
})
if str != "" {
l.stringValue = str
l.token = Function
} else {
l.token = EOF
}
case unicode.IsLower(r) || unicode.IsDigit(r) || r == '.':
str := l.scanString(func(r rune) bool {
return unicode.IsLetter(r) || unicode.IsDigit(r) || r == '.'
})
if str != "" {
l.stringValue = str
l.token = Identifier
} else {
l.token = EOF
}
default:
l.token = EOF
l.Next()
}
if l.token != EOF {
break
}
}
return l.token
}
func (l *EntityFilterDSLLexer) scanString(isValid func(rune) bool) string {
var buf strings.Builder
for !l.IsEOF() && isValid(l.Peek()) {
buf.WriteRune(l.Next())
}
return buf.String()
}
```
you'll notice i took a design decision to have it very simple so that i get output like this:
```
for: HasTags(bb.village1.enemyTags)
tokens:
Function: HasTags
OpenParen:
Identifier: bb.village1.enemyTags
CloseParen:
```
the Function names are not tokenized to specific types of functions, and the Identifiers are not split on dots and are just plain strings.
So, we have this lexer.
How can we read the stream of tokens and make an AST object? One where we would be able to basically assign our own functions to evaluate the nodes in the tree?
Operators would be implemented with a func signature like
```
func Or(pa func(*Entity) bool, pb func(*Entity) bool) func(*Entity) bool {
return func (e *Entity) bool { return pa(e) || pb(e)
}
```
or
```
func Not(p func(*Entity) bool) func(*Entity) bool {
return func(e *Entity) bool { return !p(e) }
}
```
while a function might be either of type Predicate or Sort.
```
Predicates: [CanBe, Is, HasTag, HasTags, VillagerOf]
Sorts: [Closest]
```
The functions that we would implement to resolve the value of a Predicate in the tree would be of type `func(args []string) func(*Entity)bool`
For example:
```
// e is in scope as the *Entity that this DSL expression is being evaluated from
// the perspective *of*. We would do something like
// e.EntityFilterDSL("CanBe(yoked, 1)")
var EntityFilterDSLPredicates := map[string]any {
"CanBe": func(args []string) func(*Entity) bool {
k, v := args[0], args[1]
return func (x *Entity) bool {
return e.GetIntMap(STATE).ValCanBeSetTo(k, v)
}
},
"HasTags": func(args []string) func(*Entity) bool {
return func (x *Entity) bool {
return e.HasTags(args...)
}
},
"Is": func(args[]string) func(*Entity) bool {
return func(x *Entity) bool {
lookup := resolveIdentifier(args[0])
if ent, ok := lookup.(*Entity); ok {
return true
}
return false
}
},
}
```
the resolveIdentifier() function will be left for me to work out.
We would basically write one handler for Function call operations, with a switch statement inside on the string, the lexer.TokenText(), that would invoke different methods in the above map, passing them the comma-separated tokens as a spread array of strings for the variadic signature.
Closest, as a sort, would yield a comparator we can use to sort a list of entities or find the min/max element.
```
var EntityFilterDSLSorts := map[string]any {
"Closest": func(args ...string) func(a, b *Entity) int {
pole := resolveIdentifier(args[0]).(*Entity)
return func(a, b *Entity) int {
return a.DistanceFrom(pole) - b.DistanceFrom(pole)
}
}
```
So, given these maps, I would like to define our basic correct syntax for an expression in our DSL as:
`<Predicate Expr>;<Sort Expr>`
How can I verify the expected structure as output by the lexer? Does this happen during parsing? Maybe we need to generate a grammar map with regexes?
Finally, consider the tree induced by this expression should always be with the Semicolon token has highest precedence (or i dunno if i'm using precedence right here). The tree's root should be the semicolon. Because it's there that we do something different on the left and right.
For the predicate expressions, we evaluated the function calls - which produced func(*Entity)bool's, and we passed these up through their operators which just wrap such predicate functions, and we came to the top with one big predicate function with nested babies inside.
But for the sort expression, there's just one function call - in this case to Closest (we can define as many as we want later).
func (w *World) ClosestEntityFilter(pos Vec2D, box Vec2D, filter func(*Entity) bool) *Entity {
closest := (*Entity)(nil)
closestDistance := math.MaxFloat64
for _, e := range w.FilterAllEntities(filter) {
entityPos := e.GetVec2D(POSITION)
entityBox := e.GetVec2D(BOX)
distance := RectDistance(pos, box, *entityPos, *entityBox)
if distance < closestDistance {
closestDistance = distance
closest = e
}
}
return closest
}
// e is in scope as the *Entity that this DSL expression is being evaluated from
// the perspective *of*.
var EntityFilterDSLFuncs := map[string]any {
}
func (pos Vec2D, box Vec2D, filter func(*Entity) bool) *Entity {
closest := (*Entity)(nil)
closestDistance := math.MaxFloat64
for _, e := range w.FilterAllEntities(filter) {
entityPos := e.GetVec2D(POSITION)
entityBox := e.GetVec2D(BOX)
distance := RectDistance(pos, box, *entityPos, *entityBox)
if distance < closestDistance {
closestDistance = distance
closest = e
}
}
return closest
}