jevstrudel.git / packages / mondough / mondough.mjs
1import {
2  strudelScope,
3  reify,
4  fast,
5  slow,
6  seq,
7  stepcat,
8  replicate,
9  expand,
10  pace,
11  chooseIn,
12  degradeBy,
13  silence,
14  bjork,
15} from '@strudel/core';
16import { registerLanguage } from '@strudel/transpiler';
17import { MondoRunner } from 'mondolang';
18
19const tail = (friend, pat) => pat.fmap((a) => (b) => (Array.isArray(a) ? [...a, b] : [a, b])).appLeft(friend);
20
21const arrayRange = (start, stop, step = 1) =>
22  Array.from({ length: Math.abs(stop - start) / step + 1 }, (_, index) =>
23    start < stop ? start + index * step : start - index * step,
24  );
25const range = (max, min) => min.squeezeBind((a) => max.bind((b) => seq(...arrayRange(a, b))));
26
27let nope = (...args) => args[args.length - 1];
28
29let lib = {};
30lib['nope'] = nope;
31lib['-'] = (a, b) => b.early(a);
32lib['+'] = (a, b) => b.late(a);
33lib['_'] = silence;
34lib['~'] = silence;
35lib.curly = stepcat;
36lib.square = (...args) => stepcat(...args).setSteps(1);
37lib.angle = (...args) => stepcat(...args).pace(1);
38lib['*'] = fast;
39lib['/'] = slow;
40lib['!'] = replicate;
41lib['@'] = expand;
42lib['%'] = pace;
43lib['?'] = degradeBy; // todo: default 0.5 not working..
44lib['&'] = bjork;
45lib[':'] = tail;
46lib['..'] = range;
47lib['def'] = () => silence;
48lib['or'] = (...children) => chooseIn(...children); // always has structure but is cyclewise.. e.g. "s oh*8.dec[.04 | .5]"

lib['or'] = (...children) => chooseOut(...children); // "s oh8.dec[.04 | .5]" is better but "dec[.04 | .5].s oh8" has no struct

51function evaluator(node, scope) {
52  const { type } = node;
53  // node is list
54  if (type === 'list') {
55    const { children } = node;
56    const [name, ...args] = children;
57    // some functions wont be reified to make sure they work (e.g. see extend below)
58    if (typeof name === 'function') {
59      return name(...args);
60    }
61    if (name.value === 'def') {
62      return silence;
63    }
64    // name is expected to be a pattern of functions!
65    const first = name.firstCycle(true)[0];
66    const type = typeof first?.value;
67    if (type !== 'function') {
68      throw new Error(`[mondough] expected function, got "${first?.value}"`);
69    }
70    return name
71      .fmap((fn) => {
72        if (typeof fn !== 'function') {
73          throw new Error(`[mondough] "${fn}" is not a function b`);
74        }
75        return fn(...args);
76      })
77      .innerJoin();
78  }
79  // node is leaf
80  let { value } = node;
81  if (type === 'plain' && scope[value]) {
82    return reify(scope[value]); // -> local scope has no location
83  }
84  const variable = lib[value] ?? strudelScope[value];
85  // problem: collisions when we want a string that happens to also be a variable name
86  // example: "s sine" -> sine is also a variable
87  let pat;
88  if (type === 'plain' && typeof variable !== 'undefined') {
89    // some function names are not patternable, so we skip reification here
90    if (['!', 'extend', '@', 'expand', 'square', 'angle', 'all', 'setcpm', 'setcps'].includes(value)) {
91      return variable;
92    }
93    pat = reify(variable);
94  } else {
95    pat = reify(value);
96  }
97  if (node.loc) {
98    pat = pat.withLoc(node.loc[0], node.loc[1]);
99  }
100  return pat;
101}
103let runner = new MondoRunner({ evaluator });
104
105export function mondo(code, offset = 0) {
106  if (Array.isArray(code)) {
107    code = code.join('');
108  }
109  const pat = runner.run(code, undefined, offset);
110  return pat.markcss('color: var(--caret,--foreground);text-decoration:underline');
111}
112
113export let getLocations = (code, offset) => runner.parser.get_locations(code, offset);
114
115export const mondi = (str, offset) => {
116  const code = `[${str}]`;
117  return mondo(code, offset);
118};

tell transpiler how to get locations for mondo`` calls

121registerLanguage('mondo', {
122  getLocations,
123});

this is like mondo, but with a zero offset

126export const mondolang = (code) => mondo(code, 0);
127registerLanguage('mondolang', {
128  getLocations: (code) => getLocations(code, 0),
129});

uncomment the following to use mondo as mini notation language registerLanguage('minilang', { name: 'mondi', getLocations, });