use + and - as late / early

+ add some extra error handling
This commit is contained in:
Felix Roos
2025-06-18 22:07:16 +02:00
parent 9412bf60bf
commit 10ac7c353c
2 changed files with 16 additions and 3 deletions
+14 -2
View File
@@ -20,8 +20,8 @@ export class MondoParser {
open_curly: /^\{/,
close_curly: /^\}/,
number: /^-?[0-9]*\.?[0-9]+/, // before pipe!
// "+" and "-" might be added here, but then "-" won't work as silence anymore..
op: /^[*/:!@%?]|^\.{2}/, // * / : ! @ % ? ..
// TODO: better error handling when "-" is used as rest, e.g "s [- bd]"
op: /^[*/:!@%?+-]|^\.{2}/, // * / : ! @ % ? ..
// dollar: /^\$/,
pipe: /^#/,
stack: /^[,$]/,
@@ -173,6 +173,18 @@ export class MondoParser {
children[opIndex] = op;
continue;
}
// some careful error handling
if (left.type === 'op') {
throw new Error(`got 2 ops in a row: "${left.value}${op.value}"`);
}
if (right.type === 'op') {
let err = `got 2 ops in a row: "${op.value}${right.value}"`;
if (op.value === '-') {
// yes i know this file is not supposed to know about rests x.X
err += '. you probably want a rest, which is "_" in mondo!';
}
throw new Error(err);
}
const call = { type: 'list', children: [op, right, left] };
// insert call while keeping other siblings
children = [...children.slice(0, opIndex - 1), call, ...children.slice(opIndex + 2)];
+2 -1
View File
@@ -27,7 +27,8 @@ let nope = (...args) => args[args.length - 1];
let lib = {};
lib['nope'] = nope;
lib['-'] = silence;
lib['-'] = (a, b) => b.early(a);
lib['+'] = (a, b) => b.late(a);
lib['_'] = silence;
lib['~'] = silence;
lib.curly = stepcat;