mondo: improve mondo package api + update readme

This commit is contained in:
Felix Roos 2025-03-29 21:33:51 +01:00
parent 2e017e46f9
commit c9cafa37fd
No known key found for this signature in database
4 changed files with 89 additions and 60 deletions

View file

@ -5,23 +5,32 @@ an experimental parser for an *uzulang*, a custom dsl for patterns that can stan
- [uzulang I](https://garten.salat.dev/uzu/uzulang1.html) - [uzulang I](https://garten.salat.dev/uzu/uzulang1.html)
- [uzulang II](https://garten.salat.dev/uzu/uzulang2.html) - [uzulang II](https://garten.salat.dev/uzu/uzulang2.html)
## Example Usage
```js ```js
import { MondoRunner } from 'uzu' import { MondoRunner } from 'mondo'
// define our library of functions and variables
const runner = MondoRunner({ seq, cat, s, crush, speed, '*': fast }); let lib = {
const pat = runner.run('s [bd hh*2 (cp.crush 4) <mt ht lt>] . speed .8') add: (a, b) => a + b,
``` mul: (a, b) => a * b,
PI: Math.PI,
the above code will create the following call structure: };
// this function will evaluate nodes in the syntax tree
```lisp function evaluator(node) {
(speed // check if node is a leaf node (!= list)
(s if (node.type !== 'list') {
(seq bd // check lib if we find a match in the lib, otherwise return value
(* hh 2) return lib[node.value] ?? node.value;
(crush cp 4) }
(cat mt ht lt) // now it can only be a list..
) const [fn, ...args] = node.children;
) .8 // children in a list will already be evaluated
) // the first child is expected to be a function
if (typeof fn !== 'function') {
throw new Error(`"${fn}" is not a function ${typeof fn}`);
}
return fn(...args);
}
const runner = new MondoRunner(evaluator);
const pat = runner.run('add 1 (mul 2 PI)') // 7.283185307179586
``` ```

View file

@ -306,11 +306,10 @@ export function printAst(ast, compact = false, lvl = 0) {
// lisp runner // lisp runner
export class MondoRunner { export class MondoRunner {
constructor(lib) { constructor(evaluator) {
this.parser = new MondoParser(); this.parser = new MondoParser();
this.lib = lib; this.evaluator = evaluator;
this.assert(!!this.lib.leaf, `no handler for leaft nodes! add "leaf" to your lib`); this.assert(typeof evaluator === 'function', `expected an evaluator function to be passed to new MondoRunner`);
this.assert(!!this.lib.call, `no handler for call nodes! add "call" to your lib`);
} }
// a helper to check conditions and throw if they are not met // a helper to check conditions and throw if they are not met
assert(condition, error) { assert(condition, error) {
@ -331,15 +330,10 @@ export class MondoRunner {
} else if (['quotes_double', 'quotes_single'].includes(ast.type)) { } else if (['quotes_double', 'quotes_single'].includes(ast.type)) {
ast.value = ast.value.slice(1, -1); ast.value = ast.value.slice(1, -1);
} }
return this.lib.leaf(ast, scope); return this.evaluator(ast, scope);
} }
// is list if (ast.children[0]?.value === 'lambda') {
if (!ast.children.length) {
throw new Error(`empty list`);
}
if (ast.children[0].value === 'lambda') {
const [_, args, body] = ast.children; const [_, args, body] = ast.children;
const argNames = args.children.map((child) => child.value); const argNames = args.children.map((child) => child.value);
return (x) => { return (x) => {
@ -349,10 +343,8 @@ export class MondoRunner {
return this.evaluate(body, scope); return this.evaluate(body, scope);
}; };
} }
// evaluate all children before evaluating list
const args = ast.children.map((arg) => this.evaluate(arg, scope)); ast.children = ast.children.map((arg) => this.evaluate(arg, scope));
// we could short circuit arg[0] if its plain... return this.evaluator(ast, scope);
// evaluate args
return this.lib.call(args[0], args.slice(1), scope);
} }
} }

View file

@ -5,7 +5,7 @@ This program is free software: you can redistribute it and/or modify it under th
*/ */
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { MondoParser, printAst } from '../mondo.mjs'; import { MondoParser, printAst, MondoRunner } from '../mondo.mjs';
const parser = new MondoParser(); const parser = new MondoParser();
const p = (code) => parser.parse(code, -1); const p = (code) => parser.parse(code, -1);
@ -128,3 +128,28 @@ describe('mondo sugar', () => {
it('should desugar_lambda', () => it('should desugar_lambda', () =>
expect(printAst(parser.desugar_lambda(lambda.children, target))).toEqual('(fast 2 xyz)')); */ expect(printAst(parser.desugar_lambda(lambda.children, target))).toEqual('(fast 2 xyz)')); */
}); });
describe('mondo arithmetic', () => {
let lib = {
add: (a, b) => a + b,
mul: (a, b) => a * b,
PI: Math.PI,
};
function evaluator(node) {
// check if node is a leaf node (!= list)
if (node.type !== 'list') {
// check lib if we find a match in the lib, otherwise return value
return lib[node.value] ?? node.value;
}
// now it can only be a list..
const [fn, ...args] = node.children;
// children in a list will already be evaluated
// the first child is expected to be a function
if (typeof fn !== 'function') {
throw new Error(`"${fn}" is not a function ${typeof fn}`);
}
return fn(...args);
}
const runner = new MondoRunner(evaluator);
it('should desugar (.)', () => expect(runner.run('add 1 (mul 2 PI)').toFixed(2)).toEqual('7.28'));
});

View file

@ -43,8 +43,12 @@ lib['..'] = range;
lib['or'] = (...children) => chooseIn(...children); // always has structure but is cyclewise.. e.g. "s oh*8.dec[.04 | .5]" lib['or'] = (...children) => chooseIn(...children); // always has structure but is cyclewise.. e.g. "s oh*8.dec[.04 | .5]"
//lib['or'] = (...children) => chooseOut(...children); // "s oh*8.dec[.04 | .5]" is better but "dec[.04 | .5].s oh*8" has no struct //lib['or'] = (...children) => chooseOut(...children); // "s oh*8.dec[.04 | .5]" is better but "dec[.04 | .5].s oh*8" has no struct
let runner = new MondoRunner({ function evaluator(node, scope) {
call(name, args, scope) { const { type } = node;
// node is list
if (type === 'list') {
const { children } = node;
const [name, ...args] = children;
// name is expected to be a pattern of functions! // name is expected to be a pattern of functions!
const first = name.firstCycle(true)[0]; const first = name.firstCycle(true)[0];
if (typeof first?.value !== 'function') { if (typeof first?.value !== 'function') {
@ -58,30 +62,29 @@ let runner = new MondoRunner({
return fn(...args); return fn(...args);
}) })
.innerJoin(); .innerJoin();
}, }
leaf(token, scope) { // node is leaf
let { value, type } = token; let { value } = node;
// local scope if (type === 'plain' && scope[value]) {
if (type === 'plain' && scope[value]) { return reify(scope[value]); // -> local scope has no location
return reify(scope[value]); // -> local scope has no location }
} const variable = lib[value] ?? strudelScope[value];
const variable = lib[value] ?? strudelScope[value]; let pat;
let pat; if (type === 'plain' && typeof variable !== 'undefined') {
if (type === 'plain' && typeof variable !== 'undefined') { // problem: collisions when we want a string that happens to also be a variable name
// problem: collisions when we want a string that happens to also be a variable name // example: "s sine" -> sine is also a variable
// example: "s sine" -> sine is also a variable pat = reify(variable);
pat = reify(variable); } else {
} else { pat = reify(value);
pat = reify(value); }
} if (node.loc) {
pat = pat.withLoc(node.loc[0], node.loc[1]);
}
pat.foo = true;
return pat;
}
if (token.loc) { let runner = new MondoRunner(evaluator);
pat = pat.withLoc(token.loc[0], token.loc[1]);
}
pat.foo = true;
return pat;
},
});
export function mondo(code, offset = 0) { export function mondo(code, offset = 0) {
if (Array.isArray(code)) { if (Array.isArray(code)) {