mondo: patternable function names

This commit is contained in:
Felix Roos
2025-03-26 09:27:02 +01:00
parent 1738e4d538
commit 64a6dacc1e
3 changed files with 34 additions and 26 deletions
+7 -12
View File
@@ -280,7 +280,7 @@ export class MondoParser {
get_locations(code, offset = 0) {
let walk = (ast, locations = []) => {
if (ast.type === 'list') {
return ast.children.slice(1).forEach((child) => walk(child, locations));
return ast.children.forEach((child) => walk(child, locations));
}
if (ast.loc) {
locations.push(ast.loc);
@@ -335,17 +335,11 @@ export class MondoRunner {
}
// is list
// the first element is expected to be the function name
const first = ast.children[0];
let name;
if (first?.type !== 'list') {
name = first.value; // regular function call e.g. (fast 2 (s bd))
} else {
// dynamic function name e.g. "(<speed fast> 2 (s bd))"
name = this.evaluate(first);
if (!ast.children.length) {
throw new Error(`empty list`);
}
if (name === 'lambda') {
if (ast.children[0].value === 'lambda') {
const [_, args, body] = ast.children;
const argNames = args.children.map((child) => child.value);
return (x) => {
@@ -356,8 +350,9 @@ export class MondoRunner {
};
}
const args = ast.children.map((arg) => this.evaluate(arg, scope));
// we could short circuit arg[0] if its plain...
// evaluate args
const args = ast.children.slice(1).map((arg) => this.evaluate(arg, scope));
return this.lib.call(name, args, scope);
return this.lib.call(args[0], args.slice(1), scope);
}
}
+25 -12
View File
@@ -11,7 +11,6 @@ import {
chooseIn,
degradeBy,
silence,
isPattern,
} from '@strudel/core';
import { registerLanguage } from '@strudel/transpiler';
import { MondoRunner } from '../mondo/mondo.mjs';
@@ -24,7 +23,10 @@ const arrayRange = (start, stop, step = 1) =>
);
const range = (max, min) => min.squeezeBind((a) => max.bind((b) => seq(...arrayRange(a, b))));
let nope = (...args) => args[args.length - 1];
let lib = {};
lib['nope'] = nope;
lib['-'] = silence;
lib['~'] = silence;
lib.curly = stepcat;
@@ -43,15 +45,19 @@ lib['or'] = (...children) => chooseIn(...children); // always has structure but
let runner = new MondoRunner({
call(name, args, scope) {
if (isPattern(name)) {
// patterned function name, e.g. "s bd . <speed fast> 2"
return name.fmap((fn) => fn(...args)).innerJoin();
// name is expected to be a pattern of functions!
const first = name.firstCycle(true)[0];
if (typeof first?.value !== 'function') {
throw new Error(`[mondough] "${first}" is not a function`);
}
const fn = lib[name] || strudelScope[name];
if (!fn) {
throw new Error(`[moundough]: unknown function "${name}"`);
}
return fn(...args);
return name
.fmap((fn) => {
if (typeof fn !== 'function') {
throw new Error(`[mondough] "${fn}" is not a function`);
}
return fn(...args);
})
.innerJoin();
},
leaf(token, scope) {
let { value, type } = token;
@@ -59,14 +65,21 @@ let runner = new MondoRunner({
if (type === 'plain' && scope[value]) {
return reify(scope[value]); // -> local scope has no location
}
const [from, to] = token.loc;
const variable = lib[value] ?? strudelScope[value];
let pat;
if (type === 'plain' && typeof variable !== 'undefined') {
// problem: collisions when we want a string that happens to also be a variable name
// example: "s sine" -> sine is also a variable
return reify(strudelScope[value]).withLoc(from, to);
pat = reify(variable);
} else {
pat = reify(value);
}
return reify(value).withLoc(from, to);
if (token.loc) {
pat = pat.withLoc(token.loc[0], token.loc[1]);
}
pat.foo = true;
return pat;
},
});
+2 -2
View File
@@ -14,7 +14,7 @@ Here's an example:
<MiniRepl
mondo
client:idle
tune={`$ note c2 .(euclid <3 6 3> <8 16>) . *2
tune={`$ note (c2 .euclid <3 6 3> <8 16>) . *2
.s "sine" .add (note [0 <12 24>]*2)
.dec(sine .range .2 2) .room .5
.lpf(sine/3.range 120 400)
@@ -27,7 +27,7 @@ $ s [bd bd bd bd] .bank tr909.clip.5
.ply<1 [1 [2 4]]>
$ s oh*4 .press .bank tr909 .speed.8
.dec <.02 .05>*2 .(add (saw/8.range 0 1))`}
.dec (<.02 .05>*2 .add (saw/8.range 0 1))`}
/>
## Mondo in the REPL