-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlanguage.ts
More file actions
509 lines (458 loc) · 16.6 KB
/
language.ts
File metadata and controls
509 lines (458 loc) · 16.6 KB
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
namespace microcode {
export type TilePredicate = (tile: Tile) => boolean
export interface Constraints {
[index: string]: any[]
provides?: number[]
requires?: number[]
only?: (string | number)[]
allow?: (string | number)[]
disallow?: (string | number | TilePredicate)[]
}
function mergeConstraints(src: Constraints, dst: Constraints) {
if (!src) return
for (const key of Object.keys(src)) {
if (key == "only") dst[key] = src[key]
else dst[key] = dst[key].concat(src[key])
}
}
function isCompatibleWith(src: Constraints, c: Constraints): boolean {
if (!src) return true
if (src.requires) {
let compat = false
src.requires.forEach(
req => (compat = compat || c.provides.some(pro => pro === req)),
)
if (!compat) return false
}
return true
}
function filterModifierCompat(
tile: Tile,
category: string | number,
c: Constraints,
): boolean {
const tid = getTid(tile)
const only = c.only.some(cat => cat === category || cat === tid)
if (only) return true
if (c.only.length) return false
const allows = c.allow.some(cat => cat === category || cat === tid)
if (!allows) return false
const disallows = !c.disallow.some(
cat =>
cat === category ||
cat === tid ||
(typeof cat == "function" && cat(tid)),
)
if (!disallows) return false
return true
}
export type Tile = number | ModifierEditor
export function getTid(tile: Tile): number {
if (tile instanceof ModifierEditor) return tile.tid
return tile
}
export function getIcon(tile: Tile) {
if (tile instanceof ModifierEditor) return tile.getIcon()
return tile
}
export type RuleRep = { [name: string]: Tile[] }
export class RuleDefn {
sensors: number[]
filters: number[]
actuators: number[]
modifiers: Tile[]
constructor() {
this.sensors = []
this.filters = []
this.actuators = []
this.modifiers = []
}
get sensor() {
if (this.sensors.length == 0) return Tid.TID_SENSOR_START_PAGE
return this.sensors[0]
}
public getRuleRep(): RuleRep {
return {
sensors: this.sensors,
filters: this.filters,
actuators: this.actuators,
modifiers: this.modifiers,
}
}
public isEmpty(): boolean {
return this.sensors.length === 0 && this.actuators.length === 0
}
private supportsMath(tile: Tile) {
return (
getKind(tile) == TileKind.Literal ||
getKind(tile) == TileKind.Variable ||
getKind(tile) == TileKind.Sensor
)
}
private fixupMath(name: string) {
const tiles = this.getRuleRep()[name]
for (let i = 0; i < tiles.length - 1; i++) {
const tile1 = tiles[i]
const tile2 = tiles[i + 1]
if (
this.supportsMath(tile1) &&
(this.supportsMath(tile2) ||
getTid(tile2) == Tid.TID_MODIFIER_RANDOM_TOSS)
) {
tiles.insertAt(i + 1, Tid.TID_OPERATOR_PLUS)
} else if (isMathOperator(getTid(tile1)) && i == 0) {
tiles.splice(i, 1)
} else if (
!this.supportsMath(tile1) &&
isMathOperator(getTid(tile2))
) {
tiles.splice(i + 1, 1)
} else if (
isMathOperator(getTid(tile2)) &&
i == tiles.length - 2
) {
tiles.splice(i + 1, 1)
} else if (
isMathOperator(getTid(tile1)) &&
!this.supportsMath(tile2)
) {
// TODO: can this ever occur?
}
}
}
public fixup() {
// filter and comparison operators
if (this.filters.length == 1) {
const tile = this.filters[0]
if (
!isComparisonOperator(getTid(tile)) &&
this.supportsMath(tile)
)
this.filters.insertAt(0, Tid.TID_COMPARE_EQ)
else if (isComparisonOperator(tile)) {
this.filters = []
}
}
// math and math operators (both over filter and modifiers)
this.fixupMath("filters")
this.fixupMath("modifiers")
}
public push(tile: Tile, name: string, fix = true): number {
const tiles = this.getRuleRep()[name]
tiles.push(tile)
const len = tiles.length
if (fix) this.fixup()
return tiles.length > len ? 2 : 1
}
public deleteAt(name: string, index: number) {
const ruleTiles = this.getRuleRep()[name]
const tile = ruleTiles[index]
ruleTiles.splice(index, 1)
this.fixup()
this.deleteIncompatibleTiles()
return index < ruleTiles.length ? 0 : index - ruleTiles.length
}
private getSuggestions(name: string, index: number) {
return Language.getTileSuggestions(this, name, index)
}
private deleteIncompatibleTiles() {
const doit = (name: string, i: number) => {
const ruleTiles = this.getRuleRep()[name]
while (i < ruleTiles.length) {
const suggestions = this.getSuggestions(name, i)
const compatible = suggestions.find(
t => getTid(t) == getTid(ruleTiles[i]),
)
if (compatible) i++
else {
ruleTiles.splice(i, ruleTiles.length - i)
return false
}
}
return true
}
doit("filters", 0)
doit("modifiers", 0)
}
public updateAt(name: string, index: number, tile: Tile) {
const tiles = this.getRuleRep()[name]
const oldTile = tiles[index]
tiles[index] = tile
if (oldTile != tile) {
// TODO: can this be subsumed by fixup/deleteIncompatible?
if (
oldTile == Tid.TID_MODIFIER_RANDOM_TOSS ||
tile == Tid.TID_MODIFIER_RANDOM_TOSS
)
tiles.splice(index + 1, tiles.length - (index + 1))
}
this.fixup()
this.deleteIncompatibleTiles()
}
public toBuffer(bw: BufferWriter) {
const handleFieldEditors = (tile: Tile) => {
bw.writeByte(getTid(tile))
const fieldEditor = getFieldEditor(tile)
if (fieldEditor) {
bw.writeBuffer(
fieldEditor.toBuffer(
(tile as ModifierEditor).getField(),
),
)
}
}
if (this.isEmpty()) return
bw.writeByte(this.sensor)
this.filters.forEach(handleFieldEditors)
this.actuators.forEach(act => bw.writeByte(act))
this.modifiers.forEach(handleFieldEditors)
}
public static fromBuffer(br: BufferReader) {
const defn = new RuleDefn()
const handleFieldEditor = (which: string) => {
let by = br.readByte()
// convert from old to new
if (isOldModifierCoin(by))
by = Tid.TID_FILTER_COIN_1 + (by - Tid.TID_MODIFIER_COIN_1)
if (isOldModifierVar(by))
by =
Tid.TID_FILTER_CUP_X_READ +
(by - Tid.TID_MODIFIER_CUP_X_READ)
const tile = getEditor(by)
if (tile instanceof ModifierEditor) {
const field = tile.fieldEditor.fromBuffer(br)
const newOne = tile.getNewInstance(field)
defn.push(<any>newOne, which, false)
} else {
defn.push(by, which, false)
}
}
assert(!br.eof())
const sensorEnum = br.readByte()
assert(isSensor(sensorEnum))
defn.sensors.push(sensorEnum)
assert(!br.eof())
while (isFilter(br.peekByte())) {
handleFieldEditor("filters")
assert(!br.eof())
}
assert(!br.eof())
if (!isActuator(br.peekByte())) {
return defn
}
assert(!br.eof())
const actuatorEnum = br.readByte()
defn.actuators.push(actuatorEnum)
assert(!br.eof())
while (isModifier(br.peekByte())) {
// TODO: convert old coin and old var
handleFieldEditor("modifiers")
assert(!br.eof())
}
defn.fixup()
return defn
}
}
export class PageDefn {
rules: RuleDefn[]
constructor() {
this.rules = []
}
public trim() {
while (
this.rules.length &&
this.rules[this.rules.length - 1].isEmpty()
) {
this.rules.pop()
}
}
public deleteRuleAt(index: number) {
if (index >= 0 && index < this.rules.length) {
const deleted = this.rules[index]
this.rules.splice(index, 1)
return deleted
}
return undefined
}
public insertRuleAt(index: number, newRule: RuleDefn) {
if (index >= 0 && index < this.rules.length) {
const insertRule = newRule ? newRule : new RuleDefn()
this.rules.insertAt(index, insertRule)
return insertRule
}
return undefined
}
public toBuffer(bw: BufferWriter) {
this.rules.forEach(rule => rule.toBuffer(bw))
bw.writeByte(Tid.END_OF_PAGE)
}
public static fromBuffer(br: BufferReader) {
const defn = new PageDefn()
assert(!br.eof())
while (br.peekByte() != Tid.END_OF_PAGE) {
defn.rules.push(RuleDefn.fromBuffer(br))
assert(!br.eof())
}
br.readByte()
return defn
}
}
export function PAGE_IDS() {
return [
Tid.TID_MODIFIER_PAGE_1,
Tid.TID_MODIFIER_PAGE_2,
Tid.TID_MODIFIER_PAGE_3,
Tid.TID_MODIFIER_PAGE_4,
Tid.TID_MODIFIER_PAGE_5,
]
}
export class ProgramDefn {
pages: PageDefn[]
constructor() {
this.pages = PAGE_IDS().map(id => new PageDefn())
}
public trim() {
this.pages.map(page => page.trim())
}
public toBuffer() {
const bw = new BufferWriter()
const magic = Buffer.create(4)
magic.setNumber(NumberFormat.UInt32LE, 0, 0x3e92f825)
bw.writeBuffer(magic)
this.pages.forEach(page => page.toBuffer(bw))
bw.writeByte(Tid.END_OF_PROG)
return bw.buffer
}
public static fromBuffer(br: BufferReader) {
const defn = new ProgramDefn()
assert(!br.eof())
const magic = br.readBuffer(4)
if (magic.getNumber(NumberFormat.UInt32LE, 0) != 0x3e92f825) {
console.log("bad magic")
return defn
}
defn.pages = []
assert(!br.eof())
while (br.peekByte() != Tid.END_OF_PROG) {
defn.pages.push(PageDefn.fromBuffer(br))
assert(!br.eof())
}
br.readByte()
return defn
}
}
function mkConstraints(): Constraints {
const c: Constraints = {
provides: [],
only: [],
requires: [],
allow: [],
disallow: [],
}
return c
}
export class Language {
public static getTileSuggestions(
rule: RuleDefn,
name: string,
index: number,
): Tile[] {
const tile = rule.getRuleRep()[name][index]
let rangeName = name
if (isComparisonOperator(getTid(tile))) {
if (microcodeClassic) return []
rangeName = "comparisonOperators"
} else if (isMathOperator(getTid(tile))) {
if (microcodeClassic) return []
rangeName = "mathOperators"
}
// based on the name, we have a range of tiles to choose from
const [lower, upper] = ranges[rangeName]
let all: Tile[] = []
for (let i = lower; i <= upper; ++i) {
const ed = getEditor(i)
if (ed) all.push(ed)
else all.push(i)
}
// special case for decimal editor
if (
!microcodeClassic &&
(rangeName == "filters" || rangeName == "modifiers")
) {
all.push(getEditor(Tid.TID_DECIMAL_EDITOR))
all.push(getEditor(Tid.TID_POS_INT_EDITOR))
}
if (rangeName == "modifiers") {
// add constants and vars
all = all.concat([
Tid.TID_FILTER_COIN_1,
Tid.TID_FILTER_COIN_2,
Tid.TID_FILTER_COIN_3,
Tid.TID_FILTER_COIN_4,
Tid.TID_FILTER_COIN_5,
Tid.TID_FILTER_CUP_X_READ,
Tid.TID_FILTER_CUP_Y_READ,
Tid.TID_FILTER_CUP_Z_READ,
])
}
all = all
.filter((tile: Tile) => isVisible(tile))
.sort((t1, t2) => priority(t1) - priority(t2))
if (name === "sensors" || name === "actuators") return all
// Collect existing tiles up to index.
let existing: Tile[] = []
const ruleRep = rule.getRuleRep()
for (let i = 0; i < index; ++i) {
const tile = ruleRep[name][i]
existing.push(tile)
}
// Return empty set if the last existing tile is a "terminal".
if (existing.length) {
const last = existing[existing.length - 1]
if (
isTerminal(last) ||
(name === "filters" && isTerminal(rule.sensors[0])) ||
(name === "modifiers" && isTerminal(rule.actuators[0]))
) {
return []
}
}
// Collect the built-up constraints.
const collect = mkConstraints()
if (name === "modifiers" && rule.actuators.length) {
const src = getConstraints(rule.actuators[0])
mergeConstraints(src, collect)
}
if (name == "filters" && rule.sensors.length) {
const src = getConstraints(rule.sensors[0])
mergeConstraints(src, collect)
} else if (name == "modifiers" && rule.actuators.length) {
const src = getConstraints(rule.actuators[0])
mergeConstraints(src, collect)
}
existing.forEach(tile => {
const src = getConstraints(tile)
mergeConstraints(src, collect)
})
return all.filter(tile => {
const src = getConstraints(tile)
const cat = getCategory(tile)
return (
isCompatibleWith(src, collect) &&
filterModifierCompat(tile, cat, collect)
)
})
}
public static ensureValid(rule: RuleDefn) {
// TODO: Handle more cases. ex:
// - filters not valid for new sensor
// - modifiers not valid for new sensor or actuator
if (!rule.sensors.length) {
rule.filters = []
}
if (!rule.actuators.length) {
rule.modifiers = []
}
}
}
}