waforth/scripts/process.js

63 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-11-08 16:55:34 +01:00
#!/usr/bin/env node
const process = require("process");
const fs = require("fs");
2019-11-09 19:49:34 +01:00
const _ = require("lodash");
2019-11-09 20:04:11 +01:00
2022-04-13 22:31:41 +02:00
const args = process.argv.slice(2);
let enableBulkMemory = false;
if (args[0] === "--enable-bulk-memory") {
enableBulkMemory = true;
args.shift();
}
const [file, outfile] = args;
2019-11-08 16:55:34 +01:00
2022-04-13 17:02:46 +02:00
const lines = fs.readFileSync(file).toString().split("\n");
2019-11-08 16:55:34 +01:00
const definitions = {};
2019-11-09 20:04:11 +01:00
let skipLevel = 0;
let skippingDefinition = false;
2022-04-13 22:31:41 +02:00
let out = [];
2022-04-13 17:02:46 +02:00
lines.forEach((line) => {
2019-11-09 19:49:34 +01:00
// Constants
2022-04-13 17:02:46 +02:00
Object.keys(definitions).forEach((k) => {
2019-11-08 16:55:34 +01:00
line = line.replace(
2019-11-11 11:29:20 +01:00
new RegExp(
"(\\s)([^\\s])+(\\s)+\\(; = " + _.escapeRegExp(k) + " ;\\)",
"g"
),
"$1" + definitions[k] + " (; = " + k + " ;)"
2019-11-08 16:55:34 +01:00
);
});
2019-11-11 10:19:34 +01:00
const m = line.match(/^\s*;;\s+([!a-zA-Z0-9_]+)\s*:=\s*([^\s]+)/);
2019-11-08 16:55:34 +01:00
if (m) {
definitions[m[1]] = m[2];
}
2019-11-09 19:49:34 +01:00
2019-11-09 20:04:11 +01:00
// Bulk memory operations
2022-04-13 22:31:41 +02:00
if (enableBulkMemory) {
2019-11-09 20:04:11 +01:00
line = line
.replace(/\(call \$memcopy/g, "(memory.copy")
.replace(/\(call \$memset/g, "(memory.fill");
if (line.match(/\(func (\$memset|\$memcopy)/)) {
skippingDefinition = true;
skipLevel = 0;
}
}
if (skippingDefinition) {
skipLevel += (line.match(/\(/g) || []).length;
skipLevel -= (line.match(/\)/g) || []).length;
}
// Output line
if (!skippingDefinition) {
2022-04-13 22:31:41 +02:00
out.push(line);
2019-11-09 20:04:11 +01:00
}
if (skippingDefinition && skipLevel <= 0) {
skippingDefinition = false;
}
2019-11-08 16:55:34 +01:00
});
2022-04-13 22:31:41 +02:00
fs.writeFileSync(outfile, out.join("\n"));