prototype-3x5/notcl.js

89 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-07-27 05:54:06 +00:00
/**
2023-07-29 04:37:25 +00:00
* @typedef {Notcl.Command[]} Notcl.Script
* @typedef {Notcl.Word[]} Notcl.Command
* @typedef {object} Notcl.Word
2023-07-27 05:54:06 +00:00
* @property {string} text
*/
2023-07-29 04:37:25 +00:00
var Notcl = (() => {
const InterCommandWhitespace = Peg.Regex(/[^\S\n;]*/y);
2023-07-29 04:37:25 +00:00
const Comment = Peg.Regex(/#.*\n/y);
2023-07-29 04:37:25 +00:00
const PreWordWhitespace = Peg.Regex(/[^\S\n;]*/y);
2023-07-29 04:37:25 +00:00
const BasicWord = Peg.Map(Peg.Regex(/[^\s;]+/y), ([word]) => ({
text: word,
}));
2023-07-29 04:37:25 +00:00
const Word = Peg.Map(
Peg.Sequence(PreWordWhitespace, BasicWord),
([_, word]) => word
);
2023-07-29 04:37:25 +00:00
return {
/**
* Parse out a Notcl script into an easier-to-interpret representation.
* No script is actually executed yet.
*
* @param {string} code
* @returns Script
*/
parse(code) {
/* Preprocess */
// fold line endings
code = code.replace(/(?<!\\)((\\\\)*)\\\n/g, "$1");
2023-07-27 05:54:06 +00:00
2023-07-29 04:37:25 +00:00
/* Parse */
function nextWord(/* TODO: null/]/" terminator */) {
// TODO: handle all kinds of brace/substitution stuff
const [word, nextIndex] = Word(code, 0) ?? [null, 0];
if (word) {
code = code.substring(nextIndex);
return word;
} else {
return null;
}
2023-07-27 05:54:06 +00:00
}
2023-07-29 04:37:25 +00:00
function nextCommand(/* TODO: null/]/" terminator */) {
const command = /** @type {Notcl.Word[]} */ ([]);
while (true) {
// Strip whitespace
code = code.replace(/^\s*/, "");
// Strip comments
if (code[0] == "#") {
code = code.replace(/^.*\n/, "");
continue;
}
// Strip semicolons
if (code[0] == ";") {
code = code.substring(1);
continue;
}
break;
}
while (true) {
const word = nextWord();
if (word) {
command.push(word);
continue;
}
break;
}
2023-07-27 05:54:06 +00:00
2023-07-29 04:37:25 +00:00
return command;
}
2023-07-27 05:54:06 +00:00
2023-07-29 04:37:25 +00:00
/* Loop through commands, with safety check */
const script = /** @type {Notcl.Command[]} */ ([]);
for (let i = 0; i < 1000 && code != ""; i++) {
script.push(nextCommand());
}
2023-07-27 05:54:06 +00:00
2023-07-29 04:37:25 +00:00
return script;
},
};
})();