summaryrefslogtreecommitdiff
path: root/tools/gencode.js
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2024-10-25 11:58:58 +0200
committerTor Andersson <tor@ccxvii.net>2024-10-26 00:00:01 +0200
commit5d81b4294bd8f9b20ac8a396a185f6cf9550c00f (patch)
tree1fd4e5fd1bbab268f3421cef825d6504b39d5370 /tools/gencode.js
parent7122c8101e49300b7e29266a939f355e9eb8ffd3 (diff)
download1989-dawn-of-freedom-5d81b4294bd8f9b20ac8a396a185f6cf9550c00f.tar.gz
Update client.
Fixed some spelling errors in space data table. Create tools directory and add Makefile. Add layout.svg with boxes drawn on top of locations. Add genlayout.js to create list of box locations. Add gencolors.js to create beveled marker border colors. Major rewrite of play.js and play.css with official assets.
Diffstat (limited to 'tools/gencode.js')
-rw-r--r--tools/gencode.js106
1 files changed, 106 insertions, 0 deletions
diff --git a/tools/gencode.js b/tools/gencode.js
new file mode 100644
index 0000000..f2e3a26
--- /dev/null
+++ b/tools/gencode.js
@@ -0,0 +1,106 @@
+"use strict"
+
+let fs = require("fs")
+
+let pc = 0
+
+function tokenize(s) {
+ let list = []
+ let p = 0,
+ n = s.length
+ while (p < n) {
+ while (p < n && s[p] === " ")
+ ++p
+ if (p < n) {
+ let m = p
+ while (p < n && s[p] !== " ") {
+ let q = s[p++]
+ switch (q) {
+ case "(":
+ case "[":
+ case "{":
+ for (let x = 1; p < n && x > 0; ++p) {
+ switch (s[p]) {
+ case "(":
+ case "[":
+ case "{":
+ ++x
+ break
+ case ")":
+ case "]":
+ case "}":
+ --x
+ break
+ }
+ }
+ break
+ case '"':
+ case "'":
+ case "`":
+ while (p < n && s[p] !== q)
+ ++p
+ break
+ }
+ }
+ list.push(s.substring(m, p))
+ }
+ }
+ return list
+}
+
+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 = tokenize(line)
+ 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("]")
+console.log("// #endregion")