waforth/src/tools/preprocess.js

30 lines
693 B
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");
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
const file = process.argv[2];
const lines = fs
.readFileSync(file)
.toString()
.split("\n");
const definitions = {};
lines.forEach(line => {
Object.keys(definitions).forEach(k => {
line = line.replace(
2019-11-08 22:22:52 +01:00
new RegExp("(\\s)" + escapeRegExp(k) + "(\\s|\\))", "g"),
"$1" + definitions[k] + " (; = " + k + " ;)$2"
2019-11-08 16:55:34 +01:00
);
});
2019-11-08 21:53:56 +01:00
const m = line.match(/^;;\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-08 21:53:56 +01:00
console.log(line);
2019-11-08 16:55:34 +01:00
});