Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
dc4491f
start on parser
thomasjball Nov 22, 2025
b08179e
reverse map
thomasjball Nov 22, 2025
9de1c3a
todo
thomasjball Nov 22, 2025
5284421
refactoring for unique tokens
thomasjball Nov 23, 2025
589e382
old to new conversion
thomasjball Nov 23, 2025
d2483a0
remove old tids
thomasjball Nov 23, 2025
b7bd04c
fix up translation
thomasjball Nov 23, 2025
d0d8644
duplicates found
thomasjball Nov 23, 2025
4d49f4a
eliminate duplicates
thomasjball Nov 23, 2025
b38b80d
new functions for parse/unparse from text
thomasjball Nov 24, 2025
8c463b8
more text conversion
thomasjball Nov 24, 2025
85990be
melody as text
thomasjball Nov 24, 2025
cd0363c
getting closer!
thomasjball Nov 24, 2025
15ce977
finish off parse/unparse for fieldeditors
thomasjball Nov 24, 2025
e4c039e
start on toString
thomasjball Nov 24, 2025
280f7ce
unparser working
thomasjball Nov 24, 2025
94e1b16
done unparsing
thomasjball Nov 24, 2025
1c84f13
add EOP marker
thomasjball Nov 24, 2025
def49de
work on parser
thomasjball Nov 25, 2025
45f54ff
done with parser
thomasjball Nov 26, 2025
76078a8
ugh
thomasjball Nov 26, 2025
37db0a6
return magnet
thomasjball Nov 26, 2025
77682b7
start debugging parser
thomasjball Nov 26, 2025
cb10f94
progress?
thomasjball Nov 26, 2025
4869111
fix up reverse tooltips
thomasjball Nov 26, 2025
379cea4
getting further...
thomasjball Nov 26, 2025
e4cec15
more checking
thomasjball Nov 26, 2025
9b0c0b8
clean up unparser
thomasjball Nov 26, 2025
1893dff
redo syntax
thomasjball Nov 26, 2025
9a57ccb
more changes
thomasjball Nov 26, 2025
ea6dc45
fix corner case
thomasjball Nov 26, 2025
946999b
parser not asserting
thomasjball Nov 26, 2025
ce36ade
fix up corner case
thomasjball Nov 26, 2025
4ce1aac
move pretty printing out of language.ts
thomasjball Nov 26, 2025
599cc6d
syntax error
thomasjball Nov 26, 2025
3bc9504
move to token-based parsing
thomasjball Nov 26, 2025
3ae09bc
compiling, again
thomasjball Nov 26, 2025
579e797
fix up token processing
thomasjball Nov 27, 2025
b9a337c
fix parser
thomasjball Nov 27, 2025
ccd746b
oops
thomasjball Nov 27, 2025
886e745
more testing
thomasjball Nov 27, 2025
2bee7b5
fix up phases (don't rely on isFilter, ...)
thomasjball Nov 27, 2025
374db5b
fix up silliness
thomasjball Nov 27, 2025
4e1f629
Merge branch 'main' of https://github.com/microbit-apps/microcode-v2 …
thomasjball Nov 27, 2025
04ce472
remove files
thomasjball Nov 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace microcode {
function jacdacImages(name: string | number) {
if (name == Tid.TID_FILTER_KITA_KEY_1) return icondb.kita_key_1
if (name == Tid.TID_FILTER_KITA_KEY_2) return icondb.kita_key_2
if (name == Tid.TID_SENSOR_MAGNET) return icondb.magnet
if (name == Tid.TID_SENSOR_SLIDER) return icondb.kita_slider
if (name == Tid.TID_SENSOR_ROTARY) return icondb.kita_rotary
if (name == Tid.TID_FILTER_ROTARY_LEFT) return icondb.kita_rotary_left
Expand Down Expand Up @@ -103,10 +102,7 @@ namespace microcode {

// math
if (typeof name == "number") {
if (
microcode.isModifierConstant(name) ||
microcode.isFilterConstant(name)
) {
if (microcode.isConstant(name)) {
return icondb.numberToImage(getParam(name))
}
}
Expand All @@ -126,6 +122,7 @@ namespace microcode {

// variables

if (name == Tid.TID_SENSOR_MAGNET) return icondb.magnet
if (name == Tid.TID_SENSOR_CUP_X_WRITTEN) return icondb.cupXwritten
if (name == Tid.TID_SENSOR_CUP_Y_WRITTEN) return icondb.cupYwritten
if (name == Tid.TID_SENSOR_CUP_Z_WRITTEN) return icondb.cupZwritten
Expand Down
91,944 changes: 48,732 additions & 43,212 deletions assets/js/binary-en.js

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions assets/progs/happySad.mc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Page 1
When press button_A Do show_image LED_image `
1 1 . 1 1
1 1 . 1 1
. . . . .
1 . . . 1
. 1 1 1 .
`
LED_image `
1 1 . 1 1
. . . . .
1 . . . 1
. 1 1 1 .
. . . . .
`


When press button_A Do play_sound happy

When press button_B Do show_image LED_image `
1 1 . 1 1
1 1 . 1 1
. . . . .
. 1 1 1 .
1 . . . 1
`
LED_image `
1 1 . 1 1
1 1 . 1 1
. . . . .
. . . . .
1 1 1 1 1
`


When press button_B Do play_sound sad

Page 2

Page 3

Page 4

Page 5
30 changes: 11 additions & 19 deletions assets/strings/en/tooltips.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"T12": "release",
"T13": "move",
"T14": "timer",
"T15": "light",
"T15": "light (external)",
"T16": "temperature",
"T17": "radio receive",
"T18": "sound",
Expand All @@ -55,7 +55,7 @@
"T27": "light",
"T28": "moisture",
"T29": "distance",
"T30": "line",
"T30": "reflected light",
"T40": "switch page",
"T41": "play sound",
"T43": "music",
Expand Down Expand Up @@ -90,7 +90,7 @@
"T86": "5 seconds",
"T87": "loud",
"T88": "quiet",
"T89": "NA",
"T89": "NA1",
"T90": "shake",
"T91": "tilt up",
"T92": "tilt down",
Expand All @@ -110,21 +110,16 @@
"T107": "lost right",
"T108": "face up",
"T109": "face down",
"T110": "on",
"T111": "off",
"T110": "active",
"T111": "unactive",
"T150": "page 1",
"T151": "page 2",
"T152": "page 3",
"T153": "page 4",
"T154": "page 5",
"T155": "1",
"T156": "2",
"T157": "3",
"T158": "4",
"T159": "5",
"T160": "LED image",
"T161": "red",
"T162": "purple",
"T161": "NA5",
"T162": "NA6",
"T163": "giggle",
"T164": "happy",
"T165": "hello",
Expand All @@ -135,15 +130,12 @@
"T170": "spring",
"T171": "twinkle",
"T172": "yawn",
"T173": "variable X",
"T174": "variable Y",
"T175": "variable Z",
"T176": "radio value",
"T177": "random number",
"T178": "repeat",
"T179": "melody",
"T180": "temperature",
"T181": "NA",
"T180": "temp.",
"T181": "NA2",
"T182": "red",
"T183": "green",
"T184": "blue",
Expand All @@ -154,8 +146,8 @@
"T189": "sparkle",
"T190": "forward",
"T191": "reverse",
"T192": "turn left",
"T193": "turn right",
"T192": "NA3",
"T193": "NA4",
"T194": "stop",
"T195": "fast forward",
"T196": "spin left",
Expand Down
16 changes: 15 additions & 1 deletion editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ namespace microcode {
this.programChanged = false
this.app.save(SAVESLOT_AUTO, this.progdef.toBuffer())
}
// runProgram(this.progdef)
}

private pickDiskSLot() {
Expand Down Expand Up @@ -295,6 +294,21 @@ namespace microcode {
this.app.save(SAVESLOT_AUTO, this.progdef.toBuffer())
} else {
this.progdef = ProgramDefn.fromBuffer(new BufferReader(buf))

progToString(this.progdef)
const pas1 = progToStringRet
// console.log(`PAS1\n${pas1}`)
parseProg(pas1)
const progFromString = parseProgRet
progToString(progFromString)
const pas2 = progToStringRet
// console.log("PAS2")
// console.log(pas2)
// check the programs are the same
control.assert(
pas1 == pas2,
`progs not the same ${pas1} \n---\n ${pas2}`
)
}
this.configureP1Keys()
}
Expand Down
69 changes: 59 additions & 10 deletions fieldeditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ namespace microcode {
fromBuffer(buf: BufferReader): any {
return undefined
}
toString(field: any): string {
return ""
}
fromTokens(tokens: string[]): any {
return undefined
}
}

export class ModifierEditor {
Expand Down Expand Up @@ -93,6 +99,13 @@ namespace microcode {
const str = buf.readString()
return { num: str }
}
toString(field: BoxedNumAsStr) {
return field.num
}
fromTokens(tokens: string[]) {
// TODO: check that we have a number
return { num: tokens.length > 0 ? tokens[0] : "0" }
}
}

export class DigitEditor extends ModifierEditor {
Expand Down Expand Up @@ -170,6 +183,24 @@ namespace microcode {
}
return img
}
toString(img: Bitmap) {
let ret = ""
for (let row = 0; row < 5; row++) {
for (let col = 0; col < 5; col++) {
ret += img.getPixel(col, row) ? `1` : `.`
if (col < 4) ret += " "
}
ret += `\n`
}
return ret + ""
}
fromTokens(tokens: string[]): Bitmap {
let ret = bitmaps.create(5, 5)
for (let i = 0; i < tokens.length && i < 25; i++) {
ret.setPixel(i % 5, Math.idiv(i, 5), tokens[i] == "1" ? 1 : 0)
}
return ret
}
}

export class IconEditor extends ModifierEditor {
Expand Down Expand Up @@ -202,6 +233,28 @@ namespace microcode {
tempo: number
}

export function melodyToNotes(melody: Melody) {
const notes = melody.notes.split("")
let result = ""
for (const n of notes) {
if (n == ".") result += "- "
else result += noteNames[parseInt(n)] + " "
}
return result + ""
}

function notesToMelody(tokens: string[]) {
let res = ""
tokens.forEach((note, index) => {
if (note == "-") res += "."
else {
const index = noteNames.indexOf(note)
if (index >= 0) res += index.toString()
}
})
return { notes: res, tempo: 120 }
}

export const MELODY_LENGTH = 4
export const NUM_NOTES = 5

Expand Down Expand Up @@ -258,6 +311,12 @@ namespace microcode {
}
return { tempo, notes }
}
toString(melody: Melody): string {
return melodyToNotes(melody)
}
fromTokens(tokens: string[]): Melody {
return notesToMelody(tokens)
}
}

export class MelodyEditor extends ModifierEditor {
Expand Down Expand Up @@ -286,16 +345,6 @@ namespace microcode {
field ? field : this.fieldEditor.clone(this.field)
)
}
// music.play(music.stringPlayable("C - E - G F E D ", 120), music.PlaybackMode.UntilDone)
getNoteSequence() {
const notes = this.field.notes.split("")
let result = ""
for (const n of notes) {
if (n == ".") result += "- "
else result += noteNames[parseInt(n)] + " "
}
return result
}
}

let iconEditorTile: ModifierEditor = undefined
Expand Down
6 changes: 4 additions & 2 deletions interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,10 @@ namespace microcode {
}
case Tid.TID_ACTUATOR_MUSIC: {
// TODO: get the whole sequence and do as one shot, to avoid burps
const mod = this.rule.modifiers[this.modifierIndex]
param = (mod as MelodyEditor).getNoteSequence()
const mod = this.rule.modifiers[
this.modifierIndex
] as MelodyEditor
param = melodyToNotes(mod.field)
break
}
case Tid.TID_ACTUATOR_SPEAKER: {
Expand Down
23 changes: 22 additions & 1 deletion language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,14 @@ namespace microcode {
public static fromBuffer(br: BufferReader) {
const defn = new RuleDefn()
const handleFieldEditor = (which: string) => {
const by = br.readByte()
let by = br.readByte()
// convert from old to new
if (isOldModifierCoin(by))
by = Tid.TID_FILTER_COIN_1 + (by - Tid.TID_MODIFIER_COIN_1)
if (isOldModifierVar(by))
by =
Tid.TID_FILTER_CUP_X_READ +
(by - Tid.TID_MODIFIER_CUP_X_READ)
const tile = getEditor(by)
if (tile instanceof ModifierEditor) {
const field = tile.fieldEditor.fromBuffer(br)
Expand Down Expand Up @@ -264,6 +271,7 @@ namespace microcode {
defn.actuators.push(actuatorEnum)
assert(!br.eof())
while (isModifier(br.peekByte())) {
// TODO: convert old coin and old var
handleFieldEditor("modifiers")
assert(!br.eof())
}
Expand Down Expand Up @@ -417,6 +425,19 @@ namespace microcode {
all.push(getEditor(Tid.TID_DECIMAL_EDITOR))
all.push(getEditor(Tid.TID_POS_INT_EDITOR))
}
if (rangeName == "modifiers") {
// add constants and vars
all.concat([
Tid.TID_FILTER_COIN_1,
Tid.TID_FILTER_COIN_2,
Tid.TID_FILTER_COIN_3,
Tid.TID_FILTER_COIN_4,
Tid.TID_FILTER_COIN_5,
Tid.TID_FILTER_CUP_X_READ,
Tid.TID_FILTER_CUP_Y_READ,
Tid.TID_FILTER_CUP_Z_READ,
])
}

all = all
.filter((tile: Tile) => isVisible(tile))
Expand Down
Loading