From 642ddcdb59aa2df3202915936fab1f562734e9ce Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 19 Mar 2025 17:03:17 +0100 Subject: [PATCH] support : in mondo --- packages/mondo/mondo.mjs | 31 ++++++++++++++++++++++++++++++ packages/mondo/test/mondo.test.mjs | 2 ++ packages/mondough/mondough.mjs | 5 ++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/mondo/mondo.mjs b/packages/mondo/mondo.mjs index 68ce390d..4402aa52 100644 --- a/packages/mondo/mondo.mjs +++ b/packages/mondo/mondo.mjs @@ -20,6 +20,7 @@ export class MondoParser { pipe: /^\./, stack: /^[,$]/, op: /^[*/]/, + tail: /^:/, plain: /^[a-zA-Z0-9-~_^]+/, }; // matches next token @@ -96,6 +97,7 @@ 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; @@ -144,6 +146,35 @@ 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; + } + } + return children; + } resolve_ops(children) { while (true) { let opIndex = children.findIndex((child) => child.type === 'op'); diff --git a/packages/mondo/test/mondo.test.mjs b/packages/mondo/test/mondo.test.mjs index a8804182..c78712e9 100644 --- a/packages/mondo/test/mondo.test.mjs +++ b/packages/mondo/test/mondo.test.mjs @@ -99,6 +99,8 @@ describe('mondo sugar', () => { it('should desugar , ()', () => expect(desguar('(s bd, s cp)')).toEqual('(stack (s bd) (s cp))')); it('should desugar * /', () => expect(desguar('[a b*2 c d/3 e]')).toEqual('(seq a (* b 2) c (/ d 3) e)')); 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 README example', () => expect(desguar('s [bd hh*2 cp.(crush 4) ] . speed .8')).toEqual( diff --git a/packages/mondough/mondough.mjs b/packages/mondough/mondough.mjs index 52b169ca..c7fa4bce 100644 --- a/packages/mondough/mondough.mjs +++ b/packages/mondough/mondough.mjs @@ -17,7 +17,7 @@ strudelScope.leaf = (token) => { strudelScope.call = (fn, args, name) => { const [pat, ...rest] = args; - if (!['seq', 'cat', 'stack'].includes(name)) { + if (!['seq', 'cat', 'stack', ':'].includes(name)) { args = [...rest, pat]; } return fn(...args); @@ -26,6 +26,9 @@ strudelScope.call = (fn, args, name) => { strudelScope['*'] = fast; strudelScope['/'] = slow; +const tail = (pat, friend) => pat.fmap((a) => (b) => (Array.isArray(a) ? [...a, b] : [a, b])).appLeft(friend); +strudelScope[':'] = tail; + export function mondo(code, offset = 0) { if (Array.isArray(code)) { code = code.join('');