Skip to content

Commit b9a337c

Browse files
committed
fix parser
1 parent 579e797 commit b9a337c

File tree

4 files changed

+27
-105
lines changed

4 files changed

+27
-105
lines changed

assets/progs/happySad.mc

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +0,0 @@
1-
Page 1
2-
When press button_A Do show_image LED_image
3-
1 1 . 1 1
4-
1 1 . 1 1
5-
. . . . .
6-
1 . . . 1
7-
. 1 1 1 .
8-
LED_image
9-
1 1 . 1 1
10-
. . . . .
11-
1 . . . 1
12-
. 1 1 1 .
13-
. . . . .
14-
15-
16-
When press button_A Do play_sound happy
17-
18-
When press button_B Do show_image LED_image
19-
1 1 . 1 1
20-
1 1 . 1 1
21-
. . . . .
22-
. 1 1 1 .
23-
1 . . . 1
24-
LED_image
25-
1 1 . 1 1
26-
1 1 . 1 1
27-
. . . . .
28-
. . . . .
29-
1 1 1 1 1
30-
31-
32-
When press button_B Do play_sound sad
33-
34-
Page 2
35-
36-
Page 3
37-
38-
Page 4
39-
40-
Page 5
41-
42-
43-
Page 1
44-
When press button_A Do show_image LED_image
45-
1 1 . 1 1
46-
1 1 . 1 1
47-
. . . . .
48-
1 . . . 1
49-
. 1 1 1 .
50-
LED_image
51-
1 1 . 1 1
52-
. . . . .
53-
1 . . . 1
54-
. 1 1 1 .
55-
. . . . .
56-
57-
58-
When press button_A Do play_sound happy
59-
60-
When press button_B Do show_image LED_image
61-
1 1 . 1 1
62-
1 1 . 1 1
63-
. . . . .
64-
. 1 1 1 .
65-
1 . . . 1
66-
LED_image
67-
1 1 . 1 1
68-
1 1 . 1 1
69-
. . . . .
70-
. . . . .
71-
1 1 1 1 1
72-
73-
74-
When press button_B Do play_sound sad
75-
76-
Page 2
77-
78-
Page 3
79-
80-
Page 4
81-
82-
Page 5
83-
84-

editor.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,14 @@ namespace microcode {
296296
this.progdef = ProgramDefn.fromBuffer(new BufferReader(buf))
297297

298298
progToString(this.progdef)
299-
const progAsString = progToStringRet
300-
parseProg(progAsString)
299+
const pas1 = progToStringRet
300+
console.log(`PAS1\n${pas1}`)
301+
parseProg(pas1)
301302
const progFromString = parseProgRet
303+
progToString(progFromString)
304+
const pas2 = progToStringRet
305+
console.log("PAS2")
306+
console.log(pas2)
302307
// check the programs are the same
303308
}
304309
this.configureP1Keys()

fieldeditors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ namespace microcode {
184184
return img
185185
}
186186
toString(img: Bitmap) {
187-
let ret = "`"
187+
let ret = ""
188188
for (let row = 0; row < 5; row++) {
189189
for (let col = 0; col < 5; col++) {
190190
ret += img.getPixel(col, row) ? `1` : `.`
191191
if (col < 4) ret += " "
192192
}
193193
ret += `\n`
194194
}
195-
return ret + "`"
195+
return ret + ""
196196
}
197197
fromTokens(tokens: string[]): Bitmap {
198198
let ret = bitmaps.create(5, 5)
@@ -235,12 +235,12 @@ namespace microcode {
235235

236236
export function melodyToNotes(melody: Melody) {
237237
const notes = melody.notes.split("")
238-
let result = "`"
238+
let result = ""
239239
for (const n of notes) {
240240
if (n == ".") result += "- "
241241
else result += noteNames[parseInt(n)] + " "
242242
}
243-
return result + "`"
243+
return result + ""
244244
}
245245

246246
function notesToMelody(tokens: string[]) {

mcparser.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ namespace microcode {
1313
if (tile instanceof ModifierEditor) {
1414
const mod = tile as ModifierEditor
1515
const field = mod.fieldEditor.toString(mod.getField())
16-
if (mod instanceof IconEditor) return `${tok}\n${field}`
17-
if (mod instanceof MelodyEditor) return `${tok} ${field}\n`
16+
if (mod instanceof IconEditor)
17+
return `${tok} \`\n${field}\`\n`
18+
if (mod instanceof MelodyEditor)
19+
return `${tok} \`${field}\`\n`
1820
else return `${tok} ${field}`
1921
}
2022
return tok
@@ -55,19 +57,20 @@ namespace microcode {
5557
return tid
5658
}
5759
}
58-
const addTile = (tile: Tile, rule: RuleDefn) => {
60+
const addTile = (rule: RuleDefn, tile: Tile) => {
5961
control.assert(rule != undefined, `No Rule definition`)
6062
const tid = getTid(tile)
6163
if (isSensor(tid)) rule.push(tile, "sensors", false)
6264
if (isFilter(tid)) rule.push(tile, "filters", false)
6365
else if (isModifier(tid)) rule.push(tile, "modifiers", false)
6466
else rule.push(tile, "actuators", false)
6567
}
68+
// tokenizer
6669
let cursor = 0
67-
const whiteSpace = (s: string) => {
68-
return s == " " || s == "\n" || s == "\t"
69-
}
7070
const getToken = () => {
71+
const whiteSpace = (s: string) => {
72+
return s == " " || s == "\n" || s == "\t"
73+
}
7174
let prev = cursor
7275
let gotToken = false
7376
while (cursor < str.length) {
@@ -95,26 +98,25 @@ namespace microcode {
9598
let currTile: Tile = undefined
9699
let tok: string = undefined
97100
while ((tok = getToken())) {
98-
// console.log(`tok1 = ${tok}`)
99-
if (currTile) {
101+
// console.log(`tok = ${tok}`)
102+
if (currTile && currTile instanceof ModifierEditor) {
100103
if (
101104
currTile instanceof IconEditor ||
102105
currTile instanceof MelodyEditor
103106
) {
104107
const thisTile = currTile as ModifierEditor
105108
control.assert(tok == "`", `expected \`, got ${tok}`)
106109
let tokens = []
107-
while ((tok = getToken()) == "." || tok == "1") {
110+
while ((tok = getToken()) != "`") {
108111
tokens.push(tok)
109112
}
110-
control.assert(
111-
tok == "`",
112-
`expected {0, 1, \`}, got ${tok}`
113-
)
113+
//console.log(`got tokens = ${tokens.join(":")}`)
114+
control.assert(tok == "`", `expected \`, got ${tok}`)
114115
currTile.field = thisTile.fieldEditor.fromTokens(tokens)
115116
} else if (currTile instanceof DigitEditor) {
116117
currTile.field = currTile.fieldEditor.fromTokens([tok])
117118
}
119+
currTile = undefined
118120
continue
119121
}
120122
currTile = undefined
@@ -126,7 +128,6 @@ namespace microcode {
126128
}
127129
currPage = new PageDefn()
128130
getToken() // consume page #
129-
continue
130131
} else if (tok == "When") {
131132
control.assert(currPage != undefined, `No Page defined`)
132133
if (currRule) currPage.rules.push(currRule)
@@ -135,7 +136,7 @@ namespace microcode {
135136
control.assert(currRule != undefined, `No When defined`)
136137
} else {
137138
currTile = token2tile(tok)
138-
addTile(currTile, currRule)
139+
addTile(currRule, currTile)
139140
}
140141
}
141142
if (currRule) currPage.rules.push(currRule)

0 commit comments

Comments
 (0)