From dd5743ab8c3e0ae057a0859e57badfaf98a00e1e Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Sun, 23 Mar 2025 22:49:23 +0100 Subject: [PATCH] mondo: bring back $ for stacking --- packages/mondo/mondo.mjs | 37 ++++++++++++++++++------------ packages/mondo/test/mondo.test.mjs | 4 ++-- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/packages/mondo/mondo.mjs b/packages/mondo/mondo.mjs index cc05e131e..3f0eb0385 100644 --- a/packages/mondo/mondo.mjs +++ b/packages/mondo/mondo.mjs @@ -20,9 +20,9 @@ export class MondoParser { close_curly: /^\}/, number: /^-?[0-9]*\.?[0-9]+/, // before pipe! op: /^[*/:!@%?]|^\.{2}/, // * / : ! @ % ? .. - dollar: /^\$/, + // dollar: /^\$/, pipe: /^\./, - stack: /^[,]/, + stack: /^[,$]/, or: /^[|]/, plain: /^[a-zA-Z0-9-~_^#]+/, }; @@ -121,16 +121,20 @@ export class MondoParser { return next(children); } // collect args of stack function - const args = chunks.map((chunk) => { - if (chunk.length === 1) { - // chunks of one element can be added to the stack as is - return chunk[0]; - } else { + const args = chunks + .map((chunk) => { + if (!chunk.length) { + return; // useful for things like "$ s bd $ s hh*8" (first chunk is empty) + } + if (chunk.length === 1) { + // chunks of one element can be added to the stack as is + return chunk[0]; + } // chunks of multiple args chunk = next(chunk); return { type: 'list', children: chunk }; - } - }); + }) + .filter(Boolean); // ignore empty chunks return [{ type: 'plain', value: split_type }, ...args]; } // prevents to get a list, e.g. ((x y)) => (x y) @@ -169,7 +173,7 @@ export class MondoParser { } return children; } - desugar_pipes(children, next) { + desugar_pipes(children) { let chunks = this.split_children(children, 'pipe'); while (chunks.length > 1) { 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) } else { //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]; } } - return next(chunks[0]); + // return next(chunks[0]); + return chunks[0]; } - desugar_dollars(children) { + /* desugar_dollars(children) { let chunks = this.split_children(children, 'dollar'); while (chunks.length > 1) { let [left, right, ...rest] = chunks; @@ -205,7 +211,7 @@ export class MondoParser { chunks = [[...left, call], ...rest]; } return chunks[0]; - } + } */ parse_pair(open_type, close_type) { this.consume(open_type); const children = []; @@ -227,7 +233,8 @@ export class MondoParser { children = [{ type: 'plain', value: type }, ...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; }), ); diff --git a/packages/mondo/test/mondo.test.mjs b/packages/mondo/test/mondo.test.mjs index f302174e8..a35eb8aa8 100644 --- a/packages/mondo/test/mondo.test.mjs +++ b/packages/mondo/test/mondo.test.mjs @@ -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*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 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('(z (x y))')); + it('should desugar x $ y . z', () => expect(desguar('x $ y . z')).toEqual('(z (x y))')); */ it('should desugar README example', () => expect(desguar('s [bd hh*2 cp.(crush 4) ] . speed .8')).toEqual(