mondo: bring back $ for stacking

This commit is contained in:
Felix Roos 2025-03-23 22:49:23 +01:00
parent 51e3aa257c
commit dd5743ab8c
No known key found for this signature in database
2 changed files with 24 additions and 17 deletions

View file

@ -20,9 +20,9 @@ export class MondoParser {
close_curly: /^\}/, close_curly: /^\}/,
number: /^-?[0-9]*\.?[0-9]+/, // before pipe! number: /^-?[0-9]*\.?[0-9]+/, // before pipe!
op: /^[*/:!@%?]|^\.{2}/, // * / : ! @ % ? .. op: /^[*/:!@%?]|^\.{2}/, // * / : ! @ % ? ..
dollar: /^\$/, // dollar: /^\$/,
pipe: /^\./, pipe: /^\./,
stack: /^[,]/, stack: /^[,$]/,
or: /^[|]/, or: /^[|]/,
plain: /^[a-zA-Z0-9-~_^#]+/, plain: /^[a-zA-Z0-9-~_^#]+/,
}; };
@ -121,16 +121,20 @@ export class MondoParser {
return next(children); return next(children);
} }
// collect args of stack function // collect args of stack function
const args = chunks.map((chunk) => { const args = chunks
if (chunk.length === 1) { .map((chunk) => {
// chunks of one element can be added to the stack as is if (!chunk.length) {
return chunk[0]; return; // useful for things like "$ s bd $ s hh*8" (first chunk is empty)
} else { }
if (chunk.length === 1) {
// chunks of one element can be added to the stack as is
return chunk[0];
}
// chunks of multiple args // chunks of multiple args
chunk = next(chunk); chunk = next(chunk);
return { type: 'list', children: chunk }; return { type: 'list', children: chunk };
} })
}); .filter(Boolean); // ignore empty chunks
return [{ type: 'plain', value: split_type }, ...args]; return [{ type: 'plain', value: split_type }, ...args];
} }
// prevents to get a list, e.g. ((x y)) => (x y) // prevents to get a list, e.g. ((x y)) => (x y)
@ -169,7 +173,7 @@ export class MondoParser {
} }
return children; return children;
} }
desugar_pipes(children, next) { desugar_pipes(children) {
let chunks = this.split_children(children, 'pipe'); let chunks = this.split_children(children, 'pipe');
while (chunks.length > 1) { while (chunks.length > 1) {
let [left, right, ...rest] = chunks; let [left, right, ...rest] = chunks;
@ -190,13 +194,15 @@ export class MondoParser {
chunks = [[...left.slice(0, -1), call, ...right.slice(1)], ...rest]; // jazz (fast 2 hh) chunks = [[...left.slice(0, -1), call, ...right.slice(1)], ...rest]; // jazz (fast 2 hh)
} else { } else {
//s jazz hh.fast 2 => (fast 2 (s jazz hh)) //s jazz hh.fast 2 => (fast 2 (s jazz hh))
const call = left.length > 1 ? { type: 'list', children: next(left) } : left[0]; // const call = left.length > 1 ? { type: 'list', children: next(left) } : left[0];
const call = left.length > 1 ? { type: 'list', children: left } : left[0];
chunks = [[...right, call], ...rest]; chunks = [[...right, call], ...rest];
} }
} }
return next(chunks[0]); // return next(chunks[0]);
return chunks[0];
} }
desugar_dollars(children) { /* desugar_dollars(children) {
let chunks = this.split_children(children, 'dollar'); let chunks = this.split_children(children, 'dollar');
while (chunks.length > 1) { while (chunks.length > 1) {
let [left, right, ...rest] = chunks; let [left, right, ...rest] = chunks;
@ -205,7 +211,7 @@ export class MondoParser {
chunks = [[...left, call], ...rest]; chunks = [[...left, call], ...rest];
} }
return chunks[0]; return chunks[0];
} } */
parse_pair(open_type, close_type) { parse_pair(open_type, close_type) {
this.consume(open_type); this.consume(open_type);
const children = []; const children = [];
@ -227,7 +233,8 @@ export class MondoParser {
children = [{ type: 'plain', value: type }, ...children]; children = [{ type: 'plain', value: type }, ...children];
} }
children = this.desugar_ops(children); children = this.desugar_ops(children);
children = this.desugar_pipes(children, (children) => this.desugar_dollars(children)); // children = this.desugar_pipes(children, (children) => this.desugar_dollars(children));
children = this.desugar_pipes(children);
return children; return children;
}), }),
); );

View file

@ -109,9 +109,9 @@ describe('mondo sugar', () => {
it('should desugar x:y:z', () => expect(desguar('x:y:z')).toEqual('(: z (: y x))')); it('should desugar x:y:z', () => expect(desguar('x:y:z')).toEqual('(: z (: y x))'));
it('should desugar x:y*x', () => expect(desguar('bd:0*2')).toEqual('(* 2 (: 0 bd))')); it('should desugar x:y*x', () => expect(desguar('bd:0*2')).toEqual('(* 2 (: 0 bd))'));
it('should desugar a..b', () => expect(desguar('0..2')).toEqual('(.. 2 0)')); it('should desugar a..b', () => expect(desguar('0..2')).toEqual('(.. 2 0)'));
it('should desugar x $ y', () => expect(desguar('x $ y')).toEqual('(x y)')); /* it('should desugar x $ y', () => expect(desguar('x $ y')).toEqual('(x y)'));
it('should desugar x $ y z', () => expect(desguar('x $ y z')).toEqual('(x (y z))')); it('should desugar x $ y z', () => expect(desguar('x $ y z')).toEqual('(x (y z))'));
it('should desugar x $ y . z', () => expect(desguar('x $ y . z')).toEqual('(z (x y))')); it('should desugar x $ y . z', () => expect(desguar('x $ y . z')).toEqual('(z (x y))')); */
it('should desugar README example', () => it('should desugar README example', () =>
expect(desguar('s [bd hh*2 cp.(crush 4) <mt ht lt>] . speed .8')).toEqual( expect(desguar('s [bd hh*2 cp.(crush 4) <mt ht lt>] . speed .8')).toEqual(