Skip to content

Commit def49de

Browse files
committed
work on parser
1 parent 1c84f13 commit def49de

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

mcparser.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,54 @@
1212
namespace microcode {
1313
// resolveTooltip to go from Tid to string (replace space by -)
1414
// reverseTooltip to go from string to tid
15+
16+
export function parse(str: string) {
17+
const token2tile = (tok: string) => {
18+
const tid = tooltip2tid(tok)
19+
// check to see if field editor needed
20+
const tile = getEditor(tid)
21+
if (tile instanceof ModifierEditor) {
22+
return tile.getNewInstance()
23+
} else {
24+
return tid
25+
}
26+
}
27+
const placeTile = (tile: Tile, rule: RuleDefn) => {
28+
const tid = getTid(tile)
29+
if (isFilter(tid)) rule.push(tile, "filters", false)
30+
else if (isModifier(tid)) rule.push(tile, "modifiers", false)
31+
else rule.push(tile, "actuators", false)
32+
}
33+
const prog = new ProgramDefn()
34+
prog.pages = []
35+
const lines = str.split("\n")
36+
let currPage: PageDefn = undefined
37+
let currRule: RuleDefn = undefined
38+
for (let i = 0; i < lines.length; i++) {
39+
const line = lines[0].split(" ")
40+
if (line[0] == "Page") {
41+
currPage = new PageDefn()
42+
} else if (line[0] == "EOP") {
43+
prog.pages.push(currPage)
44+
currPage = undefined
45+
} else if (line[0] == "EOR") {
46+
currPage.rules.push(currRule)
47+
currRule = undefined
48+
} else {
49+
if (!currRule) {
50+
currRule = new RuleDefn()
51+
currRule.sensors.push(token2tile(line[0]) as number)
52+
line.shift()
53+
}
54+
let tok = line.shift()
55+
while (tok) {
56+
const tile = token2tile(tok)
57+
placeTile(tile, currRule)
58+
if (tile instanceof ModifierEditor) {
59+
// TODO: process the field, based on context
60+
} else tok = line.shift()
61+
}
62+
}
63+
}
64+
}
1565
}

0 commit comments

Comments
 (0)