jevstrudel.git / packages / mondo / README.md
1# mondo
2
3a lisp-based language intended to be used as a custom dsl for patterns that can stand on its own feet. 
4see the `test` folder for usage examples
5
6more info:
7
8- [uzulang I](https://garten.salat.dev/uzu/uzulang1.html)
9- [uzulang II](https://garten.salat.dev/uzu/uzulang2.html)
10
11## Example Usage
12
13```js
14import { MondoRunner } from 'mondolang'
15// define our library of functions and variables
16let lib = {
17  add: (a, b) => a + b,
18  mul: (a, b) => a * b,
19  PI: Math.PI,
20};
21// this function will evaluate nodes in the syntax tree
22function evaluator(node) {
23  // check if node is a leaf node (!= list)
24  if (node.type !== 'list') {
25    // check lib if we find a match in the lib, otherwise return value
26    return lib[node.value] ?? node.value;
27  }
28  // now it can only be a list..
29  const [fn, ...args] = node.children;
30  // children in a list will already be evaluated
31  // the first child is expected to be a function
32  if (typeof fn !== 'function') {
33    throw new Error(`"${fn}" is not a function`);
34  }
35  return fn(...args);
36}
37const runner = new MondoRunner({ evaluator });
38const pat = runner.run('add 1 (mul 2 PI)') // 7.283185307179586
39```