about summary refs log tree commit diff stats
path: root/js/blotbotboot/lisp.js
blob: 72ecf56b39e1cf05aae256183c49cf0671618176 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// lifted from https://github.com/maryrosecook/littlelisp
// extended by eli

(function (exports) {
  const library = {
    print: (x) => {
      console.log(x);
      return x;
    },

    display: (x) => {
      console.log(x);
      return x;
    },

    concat: (...items) => {
      return items.reduce((acc, item) => {
        return `${acc}${item}`;
      }, "");
    },

    // math
    add: (...args) => {
      return args.reduce((sum, val) => sum + val);
    },

    sub: (...args) => {
      // Subtracts values.
      return args.reduce((sum, val) => sum - val);
    },

    mul: (...args) => {
      // Multiplies values.
      return args.reduce((sum, val) => sum * val);
    },

    div: (...args) => {
      // Divides values.
      return args.reduce((sum, val) => sum / val);
    },

    mod: (a, b) => {
      // Returns the modulo of a and b.
      return a % b;
    },

    clamp: (val, min, max) => {
      // Clamps a value between min and max.
      return Math.min(max, Math.max(min, val));
    },

    step: (val, step) => {
      return Math.round(val / step) * step;
    },

    min: Math.min,
    max: Math.max,
    ceil: Math.ceil,
    floor: Math.floor, // round down to the nearest integer.
    sin: Math.sin,
    cos: Math.cos,
    log: Math.log, // calculates on the base of e.

    pow: (a, b) => {
      // calculates a^b.
      return Math.pow(a, b);
    },

    sqrt: Math.sqrt, // calculate the square root.

    sq: (a) => {
      // calculate the square.
      return a * a;
    },

    PI: Math.PI,
    TWO_PI: Math.PI * 2,
    TAU: this.TWO_PI,

    random: (...args) => {
      if (args.length >= 2) {
        // (random start end)
        return args[0] + Math.random() * (args[1] - args[0]);
      } else if (args.length === 1) {
        // (random max)
        return Math.random() * args[0];
      }
      return Math.random();
    },

    // logic
    gt: (a, b) => {
      // Returns true if a is greater than b, else false.
      return a > b;
    },

    lt: (a, b) => {
      // Returns true if a is less than b, else false.
      return a < b;
    },

    eq: (a, b) => {
      // Returns true if a is equal to b, else false.
      return a === b;
    },

    and: (a, b, ...rest) => {
      // Returns true if all conditions are true.
      const args = [a, b].concat(rest);
      for (let i = 0; i < args.length; i++) {
        if (!args[i]) {
          return args[i];
        }
      }
      return args[args.length - 1];
    },

    or: (a, b, ...rest) => {
      // Returns true if at least one condition is true.
      const args = [a, b].concat(rest);
      for (let i = 0; i < args.length; i++) {
        if (args[i]) {
          return args[i];
        }
      }
      return args[args.length - 1];
    },

    // arrays
    map: async (fn, arr) => {
      let res = [];
      for (let i = 0; i < arr.length; i++) {
        const arg = arr[i];
        res.push(await fn(arr[i], i));
      }
      return res;
    },

    filter: (fn, arr) => {
      const list = Array.from(arr);
      return Promise.all(
        list.map((element, index) => fn(element, index, list))
      ).then((result) => {
        return list.filter((_, index) => {
          return result[index];
        });
      });
    },

    reduce: async (fn, arr, acc) => {
      const length = arr.length;
      let result = acc === undefined ? subject[0] : acc;
      for (let i = acc === undefined ? 1 : 0; i < length; i++) {
        result = await fn(result, arr[i], i, arr);
      }
      return result;
    },

    len: (item) => {
      // returns the length of a list.
      return item.length;
    },

    first: (arr) => {
      // returns the first item of a list.
      return arr[0];
    },

    car: (arr) => {
      // returns the first item of a list.
      return arr[0];
    },

    last: (arr) => {
      // returns the last
      return arr[arr.length - 1];
    },

    rest: ([_, ...arr]) => {
      return arr;
    },

    cdr: ([_, ...arr]) => {
      return arr;
    },

    range: (start, end, step = 1) => {
      const arr = [];
      if (step > 0) {
        for (let i = start; i <= end; i += step) {
          arr.push(i);
        }
      } else {
        for (let i = start; i >= end; i += step) {
          arr.push(i);
        }
      }
      return arr;
    },

    // objects
    get: (item, key) => {
      // gets an object's parameter with name.
      return item[key];
    },

    set: (item, ...args) => {
      // sets an object's parameter with name as value.
      for (let i = 0; i < args.length; i += 2) {
        const key = args[i];
        const val = args[i + 1];
        item[key] = val;
      }
      return item;
    },

    of: (h, ...keys) => {
      // gets object parameters with names.
      return keys.reduce((acc, key) => {
        return acc[key];
      }, h);
    },

    keys: (item) => {
      // returns a list of the object's keys
      return Object.keys(item);
    },

    values: (item) => {
      // returns a list of the object's values
      return Object.values(item);
    },

    time: (rate = 1) => {
      // returns timestamp in milliseconds.
      return Date.now() * rate;
    },

    js: () => {
      // Javascript interop.
      return window; // note, this only works in the browser
    },

    test: (name, a, b) => {
      if (`${a}` !== `${b}`) {
        console.warn("failed " + name, a, b);
      } else {
        console.log("passed " + name, a);
      }
      return a === b;
    },

    benchmark: async (fn) => {
      // logs time taken to execute a function.
      const start = Date.now();
      const result = await fn();
      console.log(`time taken: ${Date.now() - start}ms`);
      return result;
    },
  };

  const TYPES = {
    identifier: 0,
    number: 1,
    string: 2,
    bool: 3,
  };

  const Context = function (scope, parent) {
    this.scope = scope;
    this.parent = parent;
    this.get = function (identifier) {
      if (identifier in this.scope) {
        return this.scope[identifier];
      } else if (this.parent !== undefined) {
        return this.parent.get(identifier);
      }
    };
  };

  const special = {
    let: function (input, context) {
      const letContext = input[1].reduce(function (acc, x) {
        acc.scope[x[0].value] = interpret(x[1], context);
        return acc;
      }, new Context({}, context));
      return interpret(input[2], letContext);
    },
    def: function (input, context) {
      const identifier = input[1].value;
      const value =
        input[2].type === TYPES.string && input[3] ? input[3] : input[2];
      context.scope[identifier] = interpret(value, context);
      return value;
    },
    defn: function (input, context) {
      const fnName = input[1].value;
      const fnParams =
        input[2].type === TYPES.string && input[3] ? input[3] : input[2];
      const fnBody =
        input[2].type === TYPES.string && input[4] ? input[4] : input[3];
      context.scope[fnName] = async function () {
        const lambdaArguments = arguments;
        const lambdaScope = fnParams.reduce(function (acc, x, i) {
          acc[x.value] = lambdaArguments[i];
          return acc;
        }, {});
        return interpret(fnBody, new Context(lambdaScope, context));
      };
    },
    lambda: function (input, context) {
      return async function () {
        const lambdaArguments = arguments;
        const lambdaScope = input[1].reduce(function (acc, x, i) {
          acc[x.value] = lambdaArguments[i];
          return acc;
        }, {});
        return interpret(input[2], new Context(lambdaScope, context));
      };
    },
    if: async function (input, context) {
      if (await interpret(input[1], context)) {
        return interpret(input[2], context);
      }
      return input[3] ? interpret(input[3], context) : [];
    },
    __fn: function (input, context) {
      return async function () {
        const lambdaArguments = arguments;
        const keys = [
          ...new Set(
            input
              .slice(2)
              .flat(100)
              .filter((i) => i.type === TYPES.identifier && i.value[0] === "%")
              .map((x) => x.value)
              .sort()
          ),
        ];
        const lambdaScope = keys.reduce(function (acc, x, i) {
          acc[x] = lambdaArguments[i];
          return acc;
        }, {});
        return interpret(input.slice(1), new Context(lambdaScope, context));
      };
    },
    __obj: async function (input, context) {
      const obj = {};
      for (let i = 1; i < input.length; i += 2) {
        obj[await interpret(input[i], context)] = await interpret(
          input[i + 1],
          context
        );
      }
      return obj;
    },
  };

  const interpretList = function (input, context) {
    if (input.length > 0 && input[0].value in special) {
      return special[input[0].value](input, context);
    } else {
      var list = input.map(function (x) {
        return interpret(x, context);
      });
      if (list[0] instanceof Function) {
        return list[0].apply(undefined, list.slice(1));
      } else {
        return list;
      }
    }
  };

  const interpret = function (input, context) {
    if (context === undefined) {
      return interpret(input, new Context(library));
    } else if (input instanceof Array) {
      return interpretList(input, context);
    } else if (input.type === "identifier") {
      return context.get(input.value);
    } else if (input.type === "number" || input.type === "string") {
      return input.value;
    }
  };

  let categorize = function (input) {
    if (!isNaN(parseFloat(input))) {
      return {
        type: "number",
        value: parseFloat(input),
      };
    } else if (input[0] === '"' && input.slice(-1) === '"') {
      return {
        type: "string",
        value: input.slice(1, -1),
      };
    } else {
      return {
        type: "identifier",
        value: input,
      };
    }
  };

  let parenthesize = function (input, list) {
    if (list === undefined) {
      return parenthesize(input, []);
    } else {
      let token = input.shift();
      if (token === undefined) {
        return list.pop();
      } else if (token === "(") {
        list.push(parenthesize(input, []));
        return parenthesize(input, list);
      } else if (token === ")") {
        return list;
      } else {
        return parenthesize(input, list.concat(categorize(token)));
      }
    }
  };

  let tokenize = function (input) {
    return input
      .split('"')
      .map(function (x, i) {
        if (i % 2 === 0) {
          // not in string
          return x.replace(/\(/g, " ( ").replace(/\)/g, " ) ");
        } else {
          // in string
          return x.replace(/ /g, "!whitespace!");
        }
      })
      .join('"')
      .trim()
      .split(/\s+/)
      .map(function (x) {
        return x.replace(/!whitespace!/g, " ");
      });
  };

  let parse = function (input) {
    return parenthesize(tokenize(input));
  };

  exports.lisp = {
    parse: parse,
    interpret: interpret,
  };
})(typeof exports === "undefined" ? this : exports);