From 208706fb52436b99341c384e5a5ee5068c6c3547 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 20 Mar 2025 09:09:29 +0100 Subject: [PATCH] simplify mondo: ":" is just another operator --- packages/mondo/mondo.mjs | 42 +++++------------------------- packages/mondo/test/mondo.test.mjs | 1 + 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/packages/mondo/mondo.mjs b/packages/mondo/mondo.mjs index fc4069434..5997feb40 100644 --- a/packages/mondo/mondo.mjs +++ b/packages/mondo/mondo.mjs @@ -19,8 +19,7 @@ export class MondoParser { number: /^-?[0-9]*\.?[0-9]+/, // before pipe! pipe: /^\./, stack: /^[,$]/, - op: /^[*/]/, - tail: /^:/, + op: /^[*/:]/, plain: /^[a-zA-Z0-9-~_^]+/, }; // matches next token @@ -97,7 +96,6 @@ export class MondoParser { return this.consume(next); } desugar_children(children) { - children = this.resolve_tails(children); children = this.resolve_ops(children); children = this.resolve_pipes(children); return children; @@ -146,32 +144,10 @@ export class MondoParser { }); return [{ type: 'plain', value: 'stack' }, ...args]; } - resolve_tails(children) { - while (true) { - let opIndex = children.findIndex((child) => child.type === 'tail'); - if (opIndex === -1) break; - const op = { type: 'plain', value: children[opIndex].value }; - if (opIndex === children.length - 1) { - throw new Error(`cannot use operator as last child.`); - } - if (opIndex === 0) { - // regular function call (assuming each operator exists as function) - children[opIndex] = op; - continue; - } - const left = children[opIndex - 1]; - const right = children[opIndex + 1]; - - // convert infix to prefix notation - const call = { type: 'list', children: [op, left, right] }; - - // insert call while keeping other siblings - children = [...children.slice(0, opIndex - 1), call, ...children.slice(opIndex + 2)]; - // unwrap double list.. e.g. (s jazz) * 2 - if (children.length === 1) { - // there might be a cleaner solution - children = children[0].children; - } + // prevents to get a list, e.g. ((x y)) => (x y) + unwrap_children(children) { + if (children.length === 1) { + return children[0].children; } return children; } @@ -188,6 +164,7 @@ export class MondoParser { children[opIndex] = op; continue; } + // convert infix to prefix notation const left = children[opIndex - 1]; const right = children[opIndex + 1]; if (left.type === 'pipe') { @@ -195,15 +172,10 @@ export class MondoParser { children[opIndex] = op; continue; } - // convert infix to prefix notation const call = { type: 'list', children: [op, left, right] }; // insert call while keeping other siblings children = [...children.slice(0, opIndex - 1), call, ...children.slice(opIndex + 2)]; - // unwrap double list.. e.g. (s jazz) * 2 - if (children.length === 1) { - // there might be a cleaner solution - children = children[0].children; - } + children = this.unwrap_children(children); } return children; } diff --git a/packages/mondo/test/mondo.test.mjs b/packages/mondo/test/mondo.test.mjs index c78712e9d..e21ec75b0 100644 --- a/packages/mondo/test/mondo.test.mjs +++ b/packages/mondo/test/mondo.test.mjs @@ -101,6 +101,7 @@ describe('mondo sugar', () => { it('should desugar []*x', () => expect(desguar('[a [b c]*3]')).toEqual('(seq a (* (seq b c) 3))')); 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*x', () => expect(desguar('bd:0*2')).toEqual('(* (: bd 0) 2)')); it('should desugar README example', () => expect(desguar('s [bd hh*2 cp.(crush 4) ] . speed .8')).toEqual(