|
12 | 12 | namespace microcode { |
13 | 13 | // resolveTooltip to go from Tid to string (replace space by -) |
14 | 14 | // 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 | + } |
15 | 65 | } |
0 commit comments