summaryrefslogtreecommitdiff
path: root/tools/gencode.js
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2023-05-07 17:51:58 +0200
committerTor Andersson <tor@ccxvii.net>2023-05-24 21:06:18 +0200
commit23576f10dea881fe483287760af97b20666c6d16 (patch)
tree1a31062a1f0e32e629b220500f13c16b8cf4be40 /tools/gencode.js
parent2381a17e3af7ea6d2f899568dfae11b7cdf805c7 (diff)
downloadred-flag-over-paris-23576f10dea881fe483287760af97b20666c6d16.tar.gz
Event script source and compiler.
Diffstat (limited to 'tools/gencode.js')
-rw-r--r--tools/gencode.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/tools/gencode.js b/tools/gencode.js
new file mode 100644
index 0000000..90215d5
--- /dev/null
+++ b/tools/gencode.js
@@ -0,0 +1,62 @@
+"use strict"
+
+let fs = require("fs")
+
+let pc = 0
+let CODE = Array(54).fill(0)
+
+function emit(line) {
+ ++pc
+ line[0] = "vm_" + line[0]
+ for (let i = 1; i < line.length; ++i) {
+ if (typeof line[i] === "string") {
+ if (line[i] === "all")
+ line[i] = 999
+ if (line[i][0] === "(" && !line[i].match(/\)=>/))
+ line[i] = "()=>" + line[i]
+ if (line[i][0] === "`")
+ line[i] = "()=>" + line[i]
+ }
+ }
+ console.log("\t[ " + line.join(", ") + " ],")
+}
+
+console.log("const CODE = []")
+let first = false
+
+for (let line of fs.readFileSync("events.txt", "utf-8").split("\n")) {
+ line = line.trim()
+ if (line.length === 0 || line[0] === "#")
+ continue
+ if (line === "EOF")
+ break
+ line = line.split(" ")
+ switch (line[0]) {
+ case "CARD":
+ if (first++) {
+ emit(["return"])
+ console.log("]")
+ }
+ console.log("")
+ console.log("CODE[" + line[1] + "] = [ // " + line.slice(3).join(" "))
+ break
+
+ case "log":
+ case "prompt":
+ emit([ line[0], line.slice(1).join(" ") ])
+ break
+
+ case "asm":
+ case "if":
+ case "while":
+ emit([ line[0], "()=>" + line.slice(1).join(" ") ])
+ break
+
+ default:
+ emit(line)
+ break
+ }
+}
+
+emit(["return"])
+console.log("]")