Skip to content

Commit 4ce1aac

Browse files
committed
move pretty printing out of language.ts
1 parent ce36ade commit 4ce1aac

File tree

3 files changed

+51
-55
lines changed

3 files changed

+51
-55
lines changed

editor.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,12 @@ namespace microcode {
294294
this.app.save(SAVESLOT_AUTO, this.progdef.toBuffer())
295295
} else {
296296
this.progdef = ProgramDefn.fromBuffer(new BufferReader(buf))
297-
// to string
298-
const progAsString = this.progdef.toString()
299-
const progFromString = parse(progAsString)
300-
console.log(progFromString.toString())
301-
// const progAsBuf = progFromString.toBuffer()
302-
// // compare buffers
303-
// if (buf.length == progAsBuf.length) {
304-
// } else {
305-
// control.assert(
306-
// false,
307-
// `buf.length = ${buf.length} progAsBuf.length = ${progAsBuf.length}`
308-
// )
309-
// }
297+
298+
progToString(this.progdef)
299+
const progAsString = progToStringRet
300+
parseProg(progAsString)
301+
const progFromString = parseProgRet
302+
// check the programs are the same
310303
}
311304
this.configureP1Keys()
312305
}

language.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -278,35 +278,6 @@ namespace microcode {
278278
defn.fixup()
279279
return defn
280280
}
281-
282-
public toString() {
283-
const toToken = (tile: Tile) =>
284-
resolveTooltip("T" + getTid(tile)).replaceAll(" ", "_")
285-
const tileToString = (tile: Tile) => {
286-
const tok = toToken(tile)
287-
if (tile instanceof ModifierEditor) {
288-
const mod = tile as ModifierEditor
289-
const field = mod.fieldEditor.toString(mod.getField())
290-
if (mod instanceof IconEditor) return `${tok}\n${field}`
291-
if (mod instanceof MelodyEditor) return `${tok} ${field}\n`
292-
else return `${tok} ${field}`
293-
}
294-
return tok
295-
}
296-
return (
297-
"When " +
298-
toToken(this.sensor) +
299-
" " +
300-
this.filters.map(tileToString).join(" ") +
301-
" Do " +
302-
(this.actuators.length
303-
? toToken(this.actuators[0]) +
304-
" " +
305-
this.modifiers.map(tileToString).join(" ")
306-
: "") +
307-
"\n"
308-
)
309-
}
310281
}
311282

312283
export class PageDefn {
@@ -358,11 +329,6 @@ namespace microcode {
358329
br.readByte()
359330
return defn
360331
}
361-
362-
public toString() {
363-
const res = this.rules.map(rule => rule.toString())
364-
return res.join("\n")
365-
}
366332
}
367333

368334
export function PAGE_IDS() {
@@ -413,11 +379,6 @@ namespace microcode {
413379
br.readByte()
414380
return defn
415381
}
416-
417-
public toString() {
418-
const res = this.pages.map(page => page.toString())
419-
return res.map((ps, i) => `Page ${i + 1}\n${ps}`).join("\n")
420-
}
421382
}
422383

423384
function mkConstraints(): Constraints {

mcparser.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
1-
// a parser for MC programs
1+
// a pretty printer and parser for MC programs
2+
// web only
23

34
namespace microcode {
4-
export function parse(str: string): ProgramDefn {
5+
export let progToStringRet: string = undefined
6+
//% shim=TD_NOOP
7+
export function progToString(prog: ProgramDefn): string {
8+
const ruleToString = (rule: RuleDefn) => {
9+
const toToken = (tile: Tile) =>
10+
resolveTooltip("T" + getTid(tile)).replaceAll(" ", "_")
11+
const tileToString = (tile: Tile) => {
12+
const tok = toToken(tile)
13+
if (tile instanceof ModifierEditor) {
14+
const mod = tile as ModifierEditor
15+
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`
18+
else return `${tok} ${field}`
19+
}
20+
return tok
21+
}
22+
return (
23+
"When " +
24+
toToken(rule.sensor) +
25+
" " +
26+
rule.filters.map(tileToString).join(" ") +
27+
" Do " +
28+
(rule.actuators.length
29+
? toToken(rule.actuators[0]) +
30+
" " +
31+
rule.modifiers.map(tileToString).join(" ")
32+
: "") +
33+
"\n"
34+
)
35+
}
36+
const pageToString = (page: PageDefn) => {
37+
const res = page.rules.map(ruleToString)
38+
return res.join("\n")
39+
}
40+
const res = prog.pages.map(pageToString)
41+
progToStringRet = res.map((ps, i) => `Page ${i + 1}\n${ps}`).join("\n")
42+
}
43+
44+
export let parseProgRet: ProgramDefn = undefined
45+
//% shim=TD_NOOP
46+
export function parseProg(str: string): void {
547
const token2tile = (tok: string) => {
648
const tid = tooltip2tid(tok.replaceAll("_", " "))
749
control.assert(tid != undefined, `tok ${tok} does not have mapping`)
@@ -83,6 +125,6 @@ namespace microcode {
83125
}
84126
if (currRule) currPage.rules.push(currRule)
85127
prog.pages.push(currPage)
86-
return prog
128+
parseProgRet = prog
87129
}
88130
}

0 commit comments

Comments
 (0)