From cd477d605690500e2e00bd5b3805240ffd7644bb Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 20 Mar 2023 12:01:09 +0100 Subject: Clean up code generation script and opcodes. --- tools/gencode.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/gencode.py | 77 ------------------------------------------- 2 files changed, 99 insertions(+), 77 deletions(-) create mode 100644 tools/gencode.js delete mode 100644 tools/gencode.py (limited to 'tools') diff --git a/tools/gencode.js b/tools/gencode.js new file mode 100644 index 0000000..c2071bb --- /dev/null +++ b/tools/gencode.js @@ -0,0 +1,99 @@ +"use strict" + +let fs = require("fs") + +let pc = 0 +let UCODE = Array(72).fill(0) +let SCODE = Array(72).fill(0) + +function emit(line) { + ++pc + line[0] = "vm_" + line[0] + for (let i = 1; i < line.length; ++i) { + if (typeof line[i] === "string" && line[i][0] === "(" && !line[i].match(/\)=>/)) + line[i] = "()=>" + line[i] + } + console.log("\t[ " + line.join(", ") + " ],") +} + +console.log("const CODE = [") + +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 "EVENT": + emit(["return"]) + UCODE[line[1]] = pc + console.log("// EVENT " + line[1]) + break + case "SHADED": + emit(["return"]) + SCODE[line[1]] = pc + console.log("// SHADED " + line[1]) + break + + case "space_opt": + emit([ "space", 0, 1, line[1], "(s)=>" + line.slice(2).join(" ") ]) + break + case "space": + emit([ "space", 0, 0, line[1], "(s)=>" + line.slice(2).join(" ") ]) + break + case "space_undo_opt": + emit([ "space", 1, 1, line[1], "(s)=>" + line.slice(2).join(" ") ]) + break + case "space_undo": + emit([ "space", 1, 0, line[1], "(s)=>" + line.slice(2).join(" ") ]) + break + + case "piece_opt": + emit([ "piece", 0, 1, line[1], "(s,p)=>" + line.slice(2).join(" ") ]) + break + case "piece": + emit([ "piece", 0, 0, line[1], "(s,p)=>" + line.slice(2).join(" ") ]) + break + case "piece_undo_opt": + emit([ "piece", 1, 1, line[1], "(s,p)=>" + line.slice(2).join(" ") ]) + break + case "piece_undo": + emit([ "piece", 1, 0, line[1], "(s,p)=>" + line.slice(2).join(" ") ]) + break + + case "place_opt": + emit([ "place", 0, 1, line[1], line[2] ]) + break + case "place": + emit([ "place", 0, 0, line[1], line[2] ]) + break + case "place_undo_opt": + emit([ "place", 1, 1, line[1], line[2] ]) + break + case "place_undo": + emit([ "place", 1, 0, line[1], line[2] ]) + 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("const UCODE = " + JSON.stringify(UCODE)) +console.log("const SCODE = " + JSON.stringify(SCODE)) diff --git a/tools/gencode.py b/tools/gencode.py deleted file mode 100644 index c02f24d..0000000 --- a/tools/gencode.py +++ /dev/null @@ -1,77 +0,0 @@ -#!/bin/env python3 - -code = {} -buf = [] -event = 0 - -def flush_code(): - global buf - if event > 0: - code[event] = buf - buf = [] - -event = 0 -for line in open("events.txt").readlines(): - line = line.strip() - if line == "EOF": - break - elif line == "": - continue - elif line.startswith("#"): - continue - elif line.startswith("EVENT"): - flush_code() - event = (int(line.split()[1]) << 1) - elif line.startswith("SHADED"): - flush_code() - event = (int(line.split()[1]) << 1) + 1 - else: - buf.append(line) - -flush_code() - -code_index = ["-1"] * (72 * 2) - -pc = 0 -print("const CODE = [") -for event in range(2,146): - if event & 1 == 1: - print("\t// SHADED", event >> 1) - else: - print("\t// EVENT", event >> 1) - if not event in code: - print("\t// TODO") - continue - code_index[event-2] = str(pc) - for line in code[event]: - if line.startswith('space'): - line = line.split(' ', 2) - print('\t[ vm_'+line[0]+', ' + line[1] + ', (s)=>' + line[2] + ' ],') - elif line.startswith('piece'): - line = line.split(' ', 2) - print('\t[ vm_'+line[0]+', ' + line[1] + ', (p,s)=>' + line[2] + ' ],') - elif line.startswith('while'): - line = line.split(' ', 1) - print('\t[ vm_while, ()=>' + line[1] + ' ],') - elif line.startswith('if'): - line = line.split(' ', 1) - print('\t[ vm_if, ()=>' + line[1] + ' ],') - elif line.startswith('prompt'): - line = line.split(' ', 1) - print('\t[ vm_prompt, "' + line[1] + '" ],') - elif line.startswith('log'): - line = line.split(' ', 1) - print('\t[ vm_log, "' + line[1] + '" ],') - else: - line = line.split(' ') - cmd = line[0] - args = [ ("()=>" + x if x[0] == '(' else x) for x in line[1:] ] - if len(args) > 0: - print('\t[ vm_' + cmd + ', ' + ', '.join(args) + ' ],') - else: - print('\t[ vm_' + cmd + ' ],') - pc = pc + 1 - print('\t[ vm_return ],') - pc = pc + 1 -print("]") -print("const CODE_INDEX = [ " + ", ".join(code_index) + " ]") -- cgit v1.2.3