forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.ts
559 lines (496 loc) · 17.2 KB
/
language.ts
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
namespace microcode {
export enum TileType {
SENSOR = 1,
FILTER = 2,
ACTUATOR = 3,
MODIFIER = 4,
}
// TODO: make into class
export interface Constraints {
provides?: string[]
requires?: string[]
only?: string[]
allow?: {
tiles?: string[]
categories?: string[]
}
disallow?: {
tiles?: string[]
categories?: string[]
}
handling?: { [id: string]: any }
}
export interface FieldEditor {
init: any
clone: (field: any) => any
editor: (
field: any,
picker: Picker,
onHide: () => void,
onDelete?: () => void
) => void // use picker to update field
toImage: (field: any) => Image // produce an image for the field for tile
buttonStyle: () => ButtonStyle
serialize: (field: any) => string
deserialize: (s: string) => any
}
// let P be jdParam
export enum JdKind {
Literal = 1, // value is P
Variable, // value is variables[P]
Page, // value is page[P]
EventCode,
ServiceInstanceIndex,
ServiceCommandArg, // argument of command sent will be set to P; P2 is duration in ms for Sequance
ExtLibFn, // call external function P(P2)
Timespan,
RadioValue,
Rotary,
Temperature,
Loop, // repeat modifier
// Filter/actuator kinds
Radio, // radio send/recv
RandomToss, // random number
NumFmt, // on actuator - P is numfmt
// for each modifier (defaults to [defaultModifier]), do ...
// P is a shortcut external function
// P2 is service arg size
Sequence,
}
export class TileDefn {
constructor(public type: TileType, public tid: string) {
this.priority = 0
}
priority: number
constraints: Constraints
fieldEditor: FieldEditor
jdKind: JdKind
jdParam: any
jdParam2: number
jdExternalClass: number
isVisible() {
if (this.jdExternalClass && !jacs.debugOut) {
const count = jdc.numServiceInstances(this.jdExternalClass)
// special case for buttons, which already exist on micro:bit (6 of them)
// we also have light sensor on board micro:bit (1 of them), as well as in Kit A
return this.jdExternalClass == 0x1473a263
? count > 6
: count > 0
}
return true
}
getField(): any {
return undefined
}
getIcon(): string | Image {
return this.tid
}
getNewInstance(field: any = null): TileDefn {
return this
}
buttonStyle(): ButtonStyle {
return this.fieldEditor
? this.fieldEditor.buttonStyle()
: ButtonStyles.FlatWhite
}
// the following is just set union - can be simplified
// TODO: probably really want a Gen/Kill framework instead
mergeConstraints(dst: Constraints) {
const src = this.constraints
if (!src) {
return
}
if (src.provides) {
src.provides.forEach(item => dst.provides.push(item))
}
if (src.requires) {
src.requires.forEach(item => dst.requires.push(item))
}
if (src.only) {
src.only.forEach(item => dst.only.push(item))
}
if (src.allow) {
;(src.allow.tiles || []).forEach(item =>
dst.allow.tiles.push(item)
)
;(src.allow.categories || []).forEach(item =>
dst.allow.categories.push(item)
)
}
if (src.disallow) {
;(src.disallow.tiles || []).forEach(item =>
dst.disallow.tiles.push(item)
)
;(src.disallow.categories || []).forEach(item =>
dst.disallow.categories.push(item)
)
}
if (src.handling) {
const keys = Object.keys(src.handling)
for (const key of keys) {
// TODO: deep copy
dst.handling[key] = src.handling[key]
}
}
}
isCompatibleWith(c: Constraints): boolean {
if (!this.constraints) return true
if (this.constraints.requires) {
let compat = false
this.constraints.requires.forEach(
req =>
(compat = compat || c.provides.some(pro => pro === req))
)
if (!compat) return false
}
// Check c.handling against this tile
if (c.handling) {
let compat = true
Object.keys(c.handling).forEach((name: any) => {
if (!compat) return
const rule = c.handling[name]
// MAX_COUNT rule: Allow at most N tiles of this type
if (name === "maxCount") {
// Count the instances of rule.category in c.provides
const count = c.provides.filter(
pro => pro === rule.category
).length
compat = count < rule.count
}
})
if (!compat) return false
}
return true
}
}
export enum Phase {
Pre,
Post,
}
export class StmtTileDefn extends TileDefn {
constructor(type: TileType, tid: string) {
super(type, tid)
}
public serviceClassName: string
public serviceInstanceIndex: number = 0
}
export class SensorDefn extends StmtTileDefn {
public eventCode: number
constructor(tid: string, public phase: Phase) {
super(TileType.SENSOR, tid)
}
}
export class FilterModifierBase extends TileDefn {
constructor(type: TileType, tid: string, public category: string) {
super(type, tid)
}
serviceCommandArg(): string | Buffer {
if (
typeof this.jdParam == "string" ||
typeof this.jdParam == "object"
)
return this.jdParam
return null
// throw "bad jdParam: " + this.name + " / " + this.jdParam
}
isCompatibleWith(c: Constraints): boolean {
if (!super.isCompatibleWith(c)) return false
const only = c.only.some(cat => cat === this.category)
if (only) return true
if (c.only.length) return false
const allows =
c.allow.categories.some(cat => cat === this.category) ||
c.allow.tiles.some(tid => tid === this.tid)
if (!allows) return false
const disallows =
!c.disallow.categories.some(cat => cat === this.category) &&
!c.disallow.tiles.some(tid => tid === this.tid)
if (!disallows) return false
return true
}
}
export class FilterDefn extends FilterModifierBase {
constructor(tid: string, category: string, priority: number) {
super(TileType.FILTER, tid, category)
this.priority = priority
}
}
export class ActuatorDefn extends StmtTileDefn {
public serviceCommand: number
public defaultModifier: ModifierDefn
constructor(tid: string) {
super(TileType.ACTUATOR, tid)
}
}
export class ModifierDefn extends FilterModifierBase {
constructor(tid: string, category: string, priority: number) {
super(TileType.MODIFIER, tid, category)
this.priority = priority
}
}
type RuleRep = { [name: string]: TileDefn[] }
export class RuleDefn {
sensors: SensorDefn[]
filters: FilterDefn[]
actuators: ActuatorDefn[]
modifiers: ModifierDefn[]
constructor() {
this.sensors = []
this.filters = []
this.actuators = []
this.modifiers = []
}
get sensor() {
if (this.sensors.length == 0)
return tilesDB.sensors[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 clone(): RuleDefn {
const rule = new RuleDefn()
rule.sensors = this.sensors.slice(0)
rule.actuators = this.actuators.slice(0)
rule.filters = this.filters.slice(0)
rule.modifiers = this.modifiers.slice(0)
return rule
}
public isEmpty(): boolean {
return this.sensors.length === 0 && this.actuators.length === 0
}
public toObj(): any {
const addField = (t: TileDefn) => {
if (t.fieldEditor) {
const ret = `${t.tid}(${t.fieldEditor.serialize(
t.getField()
)})`
return ret
} else {
return t.tid
}
}
const obj = {
S: this.sensors.map(t => addField(t)),
A: this.actuators.map(t => addField(t)),
F: this.filters.map(t => addField(t)),
M: this.modifiers.map(t => addField(t)),
}
if (!obj.S.length) delete obj.S
if (!obj.A.length) delete obj.A
if (!obj.F.length) delete obj.F
if (!obj.M.length) delete obj.M
return obj
}
public static FromObj(obj: any): RuleDefn {
const extractField = (t: string) => (s: string) => {
let hasField = s.indexOf("(")
if (hasField >= 0) {
const elem = s.substr(0, hasField)
if (Object.keys(tilesDB[t]).indexOf(elem) >= 0) {
const tile = tilesDB[t][elem]
const field = tile.fieldEditor.deserialize(
s.substr(hasField + 1, s.length - 2 - hasField)
)
const newOne = tile.getNewInstance(field)
return newOne
} else {
return undefined
}
} else {
return Object.keys(tilesDB[t]).indexOf(s) >= 0
? tilesDB[t][s]
: undefined
}
}
const defn = new RuleDefn()
const parseTile = (single: string, name: string) => {
if (Array.isArray(obj[single])) {
const tiles: any[] = obj[single]
return <any>tiles.map(extractField(name)).filter(t => !!t)
}
return []
}
if (typeof obj === "string") {
obj = JSON.parse(obj)
}
defn.sensors = parseTile("S", "sensors")
defn.actuators = parseTile("A", "actuators")
defn.filters = parseTile("F", "filters")
defn.modifiers = parseTile("M", "modifiers")
return defn
}
}
export class PageDefn {
rules: RuleDefn[]
constructor() {
this.rules = []
}
public clone(): PageDefn {
const page = new PageDefn()
page.rules = this.rules.map(rule => rule.clone())
return page
}
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) {
this.rules.splice(index, 1)
}
}
public insertRuleAt(index: number) {
if (index >= 0 && index < this.rules.length) {
const newRule = new RuleDefn()
// STS Array.splice doesn't support insert :(
// this.rules.splice(index, 0, new RuleDefn());
const newRules: RuleDefn[] = []
for (let i = 0; i < index; ++i) {
newRules.push(this.rules[i])
}
newRules.push(newRule)
for (let i = index; i < this.rules.length; ++i) {
newRules.push(this.rules[i])
}
this.rules = newRules
return newRule
}
return undefined
}
public toObj(): any {
const obj = {
R: this.rules.map(elem => elem.toObj()),
}
if (!obj.R.length) {
delete obj.R
}
return obj
}
public static FromObj(obj: any): PageDefn {
if (typeof obj === "string") {
obj = JSON.parse(obj)
}
const defn = new PageDefn()
if (Array.isArray(obj["R"])) {
const rules: any[] = obj["R"]
defn.rules = rules.map((elem: any) => RuleDefn.FromObj(elem))
}
return defn
}
}
export class ProgramDefn {
pages: PageDefn[]
constructor() {
this.pages = PAGE_IDS.map(id => new PageDefn())
}
public clone(): ProgramDefn {
const brain = new ProgramDefn()
brain.pages = this.pages.map(page => page.clone())
return brain
}
public trim() {
this.pages.map(page => page.trim())
}
public toObj(): any {
return {
P: this.pages.map(elem => elem.toObj()),
}
}
public static FromObj(obj: any): ProgramDefn {
if (typeof obj === "string") {
obj = JSON.parse(obj)
}
const defn = new ProgramDefn()
if (obj && obj["P"] && Array.isArray(obj["P"])) {
const pages: any[] = obj["P"]
defn.pages = pages.map((elem: any) => PageDefn.FromObj(elem))
}
return defn
}
}
function isTerminal(tile: TileDefn) {
return (
tile.constraints &&
tile.constraints.handling &&
tile.constraints.handling["terminal"]
)
}
export class Language {
public static getTileSuggestions(
rule: RuleDefn,
name: string,
index: number
): TileDefn[] {
const all = Object.keys(tilesDB[name])
.map(id => tilesDB[name][id])
.filter((tile: TileDefn) => tile.isVisible())
.sort((t1, t2) => t1.priority - t2.priority)
if (name === "sensors" || name === "actuators") return all
// Collect existing tiles up to index.
let existing: TileDefn[] = []
const ruleRep = rule.getRuleRep()
for (let i = 0; i < index; ++i) {
existing.push(ruleRep[name][i])
}
// 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 constraints = mkConstraints()
if (name === "modifiers" && rule.actuators.length) {
rule.actuators[0].mergeConstraints(constraints)
}
if (rule.sensors.length) {
rule.sensors[0].mergeConstraints(constraints)
}
existing.forEach(tile => tile.mergeConstraints(constraints))
return all.filter(tile => tile.isCompatibleWith(constraints))
}
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 = []
}
}
}
function mkConstraints(): Constraints {
const c: Constraints = {
provides: [],
only: [],
requires: [],
allow: {
tiles: [],
categories: [],
},
disallow: {
tiles: [],
categories: [],
},
handling: {},
}
return c
}
}