mondo: generic bracket names + simplify call handler + refactor mondough
This commit is contained in:
parent
e16295623d
commit
c691916cbf
3 changed files with 90 additions and 94 deletions
|
|
@ -1,52 +1,56 @@
|
|||
import { strudelScope, reify, fast, slow, seq, stepcat } from '@strudel/core';
|
||||
import { strudelScope, reify, fast, slow, seq, stepcat, extend, expand, pace } from '@strudel/core';
|
||||
import { registerLanguage } from '@strudel/transpiler';
|
||||
import { MondoRunner } from '../mondo/mondo.mjs';
|
||||
|
||||
let runner = new MondoRunner(strudelScope);
|
||||
const tail = (friend, pat) => pat.fmap((a) => (b) => (Array.isArray(a) ? [...a, b] : [a, b])).appLeft(friend);
|
||||
|
||||
strudelScope.leaf = (token, scope) => {
|
||||
let { value } = token;
|
||||
// local scope
|
||||
if (token.type === 'plain' && scope[value]) {
|
||||
return reify(scope[value]); // -> local scope has no location
|
||||
}
|
||||
const [from, to] = token.loc;
|
||||
if (token.type === 'plain' && strudelScope[value]) {
|
||||
// what if 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);
|
||||
}
|
||||
return reify(value).withLoc(from, to);
|
||||
};
|
||||
strudelScope.curly = stepcat;
|
||||
strudelScope.seq = (...args) => stepcat(...args).setSteps(1);
|
||||
strudelScope.cat = (...args) => stepcat(...args).pace(1);
|
||||
|
||||
strudelScope.call = (fn, args, name) => {
|
||||
const [pat, ...rest] = args;
|
||||
if (!['seq', 'cat', 'stack', 'curly', ':', '..', '!', '@', '%'].includes(name)) {
|
||||
args = [...rest, pat];
|
||||
}
|
||||
return fn(...args);
|
||||
};
|
||||
|
||||
strudelScope['*'] = fast;
|
||||
strudelScope['/'] = slow;
|
||||
strudelScope['!'] = (pat, n) => pat.extend(n);
|
||||
strudelScope['@'] = (pat, n) => pat.expand(n);
|
||||
strudelScope['%'] = (pat, n) => pat.pace(n);
|
||||
|
||||
// : operator
|
||||
const tail = (pat, friend) => pat.fmap((a) => (b) => (Array.isArray(a) ? [...a, b] : [a, b])).appLeft(friend);
|
||||
strudelScope[':'] = tail;
|
||||
|
||||
// .. operator
|
||||
const arrayRange = (start, stop, step = 1) =>
|
||||
Array.from({ length: Math.abs(stop - start) / step + 1 }, (_, index) =>
|
||||
start < stop ? start + index * step : start - index * step,
|
||||
);
|
||||
const range = (min, max) => min.squeezeBind((a) => max.bind((b) => seq(...arrayRange(a, b))));
|
||||
strudelScope['..'] = range;
|
||||
|
||||
let lib = {};
|
||||
lib.curly = stepcat;
|
||||
lib.square = (...args) => stepcat(...args).setSteps(1);
|
||||
lib.angle = (...args) => stepcat(...args).pace(1);
|
||||
lib['*'] = fast;
|
||||
lib['/'] = slow;
|
||||
lib['!'] = extend;
|
||||
lib['@'] = expand;
|
||||
lib['%'] = pace;
|
||||
lib[':'] = tail;
|
||||
lib['..'] = range;
|
||||
|
||||
let runner = new MondoRunner({
|
||||
call(name, args, scope) {
|
||||
console.log('call', name, args, scope);
|
||||
const fn = lib[name] || strudelScope[name];
|
||||
if (!fn) {
|
||||
throw new Error(`[moundough]: unknown function "${name}"`);
|
||||
}
|
||||
if (!['square', 'angle', 'stack', 'curly'].includes(name)) {
|
||||
// flip args (pat to end)
|
||||
const [pat, ...rest] = args;
|
||||
args = [...rest, pat];
|
||||
}
|
||||
return fn(...args);
|
||||
},
|
||||
leaf(token, scope) {
|
||||
let { value } = token;
|
||||
// local scope
|
||||
if (token.type === 'plain' && scope[value]) {
|
||||
return reify(scope[value]); // -> local scope has no location
|
||||
}
|
||||
const [from, to] = token.loc;
|
||||
if (token.type === 'plain' && strudelScope[value]) {
|
||||
// what if 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);
|
||||
}
|
||||
return reify(value).withLoc(from, to);
|
||||
},
|
||||
});
|
||||
|
||||
export function mondo(code, offset = 0) {
|
||||
if (Array.isArray(code)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue