mondo: use stepcat for curly braces

This commit is contained in:
Felix Roos 2025-03-20 14:09:32 +01:00
parent 9626f3cc9a
commit e16295623d
No known key found for this signature in database
2 changed files with 18 additions and 10 deletions

View file

@ -12,10 +12,12 @@ export class MondoParser {
quotes_single: /^'(.*?)'/,
open_list: /^\(/,
close_list: /^\)/,
open_cat: /^</,
open_cat: /^</, // todo: rename angle
close_cat: /^>/,
open_seq: /^\[/,
open_seq: /^\[/, // todo: rename square
close_seq: /^\]/,
open_curly: /^\{/,
close_curly: /^\}/,
number: /^-?[0-9]*\.?[0-9]+/, // before pipe!
op: /^[*/:!@%]|^\.{2}/, // * / : ! @ % ..
pipe: /^\./,
@ -93,6 +95,9 @@ export class MondoParser {
if (next === 'open_seq') {
return this.parse_seq();
}
if (next === 'open_curly') {
return this.parse_curly();
}
return this.consume(next);
}
desugar_children(children) {
@ -256,6 +261,12 @@ export class MondoParser {
children = this.desugar(children, 'seq');
return { type: 'list', children };
}
parse_curly() {
let children = this.parse_pair('open_curly', 'close_curly');
children = [{ type: 'plain', value: 'curly' }, ...children];
children = this.desugar(children, 'curly');
return { type: 'list', children };
}
consume(type) {
// shift removes first element and returns it
const token = this.tokens.shift();

View file

@ -1,4 +1,4 @@
import { strudelScope, reify, fast, slow, seq } from '@strudel/core';
import { strudelScope, reify, fast, slow, seq, stepcat } from '@strudel/core';
import { registerLanguage } from '@strudel/transpiler';
import { MondoRunner } from '../mondo/mondo.mjs';
@ -18,18 +18,15 @@ strudelScope.leaf = (token, scope) => {
}
return reify(value).withLoc(from, to);
};
strudelScope.curly = stepcat;
strudelScope.seq = (...args) => stepcat(...args).setSteps(1);
strudelScope.cat = (...args) => stepcat(...args).pace(1);
strudelScope.call = (fn, args, name) => {
const [pat, ...rest] = args;
if (!['seq', 'cat', 'stack', ':', '..', '!', '@', '%'].includes(name)) {
if (!['seq', 'cat', 'stack', 'curly', ':', '..', '!', '@', '%'].includes(name)) {
args = [...rest, pat];
}
if (name === 'seq') {
return stepcat(...args).setSteps(1);
}
if (name === 'cat') {
return stepcat(...args).pace(1);
}
return fn(...args);
};