Nodejs

A little LISP written in JavaScript.

fork
loading
Files
  • index.js

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8072293443515364","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"LyoqCiAqIEEgbGl0dGxlIExJU1Agd3JpdHRlbiBpbiBKYXZhU2NyaXB0LCB3aXRoIG1hbnkKICogdGhhbmtzIHRvIEFudG9uIERhdnlkb3YgYW5kIE1hcnkgUm9zZSBDb29rLgogKgogKiBAc2VlIGh0dHBzOi8vZ2l0aHViLmNvbS9kYXZ5ZG92YW50b24vcmxpc3AKICogQHNlZSBodHRwczovL2dpdGh1Yi5jb20vbWFyeXJvc2Vjb29rL2xpdHRsZWxpc3AKICoKICogQGF1dGhvciBFcmljIFdlaW5zdGVpbgogKi8KY29uc3QgcmVhZGxpbmVTeW5jID0gcmVxdWlyZSgicmVhZGxpbmUtc3luYyIpOwoKY2xhc3MgTGlzcGxldCB7CiAgY29uc3RydWN0b3IoKSB7CiAgICB0aGlzLmVudiA9IHsKICAgICAgIj09IjogZnVuY3Rpb24oKSB7CiAgICAgICAgbGV0IFthLCBiXSA9IEFycmF5LmZyb20oYXJndW1lbnRzKVswXTsKICAgICAgICByZXR1cm4gYSA9PT0gYjsKICAgICAgfSwKICAgICAgIiE9IjogZnVuY3Rpb24oKSB7CiAgICAgICAgbGV0IFthLCBiXSA9IEFycmF5LmZyb20oYXJndW1lbnRzKVswXTsKICAgICAgICByZXR1cm4gYSAhPT0gYjsKICAgICAgfSwKICAgICAgIjwiOiBmdW5jdGlvbigpIHsKICAgICAgICBsZXQgW2EsIGJdID0gQXJyYXkuZnJvbShhcmd1bWVudHMpWzBdOwogICAgICAgIHJldHVybiBhIDwgYjsKICAgICAgfSwKICAgICAgIjw9IjogZnVuY3Rpb24oKSB7CiAgICAgICAgbGV0IFthLCBiXSA9IEFycmF5LmZyb20oYXJndW1lbnRzKVswXTsKICAgICAgICByZXR1cm4gYSA8PSBiOwogICAgICB9LAogICAgICAiPiI6IGZ1bmN0aW9uKCkgewogICAgICAgIGxldCBbYSwgYl0gPSBBcnJheS5mcm9tKGFyZ3VtZW50cylbMF07CiAgICAgICAgcmV0dXJuIGEgPiBiOwogICAgICB9LAogICAgICAiPj0iOiBmdW5jdGlvbigpIHsKICAgICAgICBsZXQgW2EsIGJdID0gQXJyYXkuZnJvbShhcmd1bWVudHMpWzBdOwogICAgICAgIHJldHVybiBhID49IGI7CiAgICAgIH0sCiAgICAgICIrIjogZnVuY3Rpb24oKSB7IHJldHVybiBBcnJheS5mcm9tKGFyZ3VtZW50cylbMF0ucmVkdWNlKChhLCBlKSA9PiB7IHJldHVybiBhICsgZTsgfSk7IH0sCiAgICAgICItIjogZnVuY3Rpb24oKSB7IHJldHVybiBBcnJheS5mcm9tKGFyZ3VtZW50cylbMF0ucmVkdWNlKChhLCBlKSA9PiB7IHJldHVybiBhIC0gZTsgfSk7IH0sCiAgICAgICIqIjogZnVuY3Rpb24oKSB7IHJldHVybiBBcnJheS5mcm9tKGFyZ3VtZW50cylbMF0ucmVkdWNlKChhLCBlKSA9PiB7IHJldHVybiBhICogZTsgfSk7IH0sCiAgICAgICIvIjogZnVuY3Rpb24oKSB7IHJldHVybiBBcnJheS5mcm9tKGFyZ3VtZW50cylbMF0ucmVkdWNlKChhLCBlKSA9PiB7IHJldHVybiBhIC8gZTsgfSk7IH0sCiAgICAgICJ0cnVlIjogdHJ1ZSwKICAgICAgImZhbHNlIjogZmFsc2UsCiAgICB9OwogIH0KCiAgcnVuKGNvZGUpIHsKICAgIHJldHVybiB0aGlzLmV2YWwodGhpcy5wYXJzZShjb2RlKSk7CiAgfQoKICBwYXJzZShwcm9ncmFtKSB7CiAgICByZXR1cm4gdGhpcy5yZWFkKHRoaXMudG9rZW5pemUocHJvZ3JhbSkpOwogIH0KCiAgdG9rZW5pemUoY2hhcmFjdGVycykgewogICAgcmV0dXJuIGNoYXJhY3RlcnMuCiAgICAgIHJlcGxhY2UoL1xzXHMrL2csICI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SB7CiAgICAgIHJldHVybiB0aGlzLmVudlt0b2tlbl07CiAgICB9IGVsc2UgewogICAgICByZXR1cm4gdG9rZW4udG9TdHJpbmcoKTsKICAgIH0KICB9CgogIGV2YWwoZXhwcmVzc2lvbikgewogICAgaWYgKCFleHByZXNzaW9uKSB7CiAgICAgIHJldHVybjsKICAgIH0gZWxzZSBpZiAodHlwZW9mIGV4cHJlc3Npb24gPT09ICJudW1iZXIiIHx8IHR5cGVvZiBleHByZXNzaW9uID09PSAiYm9vbGVhbiIpIHsKICAgICAgcmV0dXJuIGV4cHJlc3Npb247CiAgICB9IGVsc2UgaWYgKE9iamVjdC5rZXlzKHRoaXMuZW52KS5pbmRleE9mKGV4cHJlc3Npb25bMF0pICE9PSAtMSkgewogICAgICBsZXQgZm4gPSBleHByZXNzaW9uWzBdOwogICAgICBsZXQgYXJncyA9IGV4cHJlc3Npb24uc2xpY2UoMSk7CgogICAgICBmdW5jdGlvbiBoYW5kbGVBcmdzKGFyZ3MpIHsKICAgICAgICBhcmdzID0gW10uc2xpY2UuY2FsbChhcmdzKTsKCiAgICAgICAgZm9yIChsZXQgaSA9IDA7IGkgPCBhcmdzLmxlbmd0aDsgaSsrKSB7CiAgICAgICAgICBpZiAoQXJyYXkuaXNBcnJheShhcmdzW2ldKSkgewogICAgICAgICAgICBhcmdzW2ldID0gdGhpcy5ldmFsKGFyZ3NbaV0pOwogICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gdGhpcy5ldmFsKGFyZ3MpOwogICAgICB9CgogICAgICByZXR1cm4gdGhpcy5lbnZbZm5dKGhhbmRsZUFyZ3MuY2FsbCh0aGlzLCBhcmdzKSk7CiAgICB9IGVsc2UgaWYgKGV4cHJlc3Npb25bMF0gPT09ICJkZWZpbmUiKSB7CiAgICAgIGxldCBhcmdzID0gZXhwcmVzc2lvbi5zbGljZSgxKTsKICAgICAgdGhpcy5lbnZbYXJnc1swXV0gPSBhcmdzWzFdOwogICAgICByZXR1cm4gZ2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Ym9keVtpXSA9IHRoaXMuZXZhbChyZXBsYWNlLmNhbGwodGhpcywgYm91bmQsIGJvZHlbaV0pKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoYm91bmRbYm9keVtpXV0pIHsKICAgICAgICAgICAgICBib2R5W2ldID0gYm91bmRbYm9keVtpXV07CiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICAgIHJldHVybiB0aGlzLmV2YWwoYm9keSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXBsYWNlLmNhbGwodGhpcywgYm91bmQsIGJvZHkpOwogICAgICB9CgogICAgICByZXR1cm4gZXZhbChleHByZXNzaW9uKTsKICAgIH0KICB9Cn0KCmxldCBsaXNwbGV0ID0gbmV3IExpc3BsZXQoKTsKCmNvbnNvbGUubG9nKGBMaXNwbGV0IGlzIHJlYWR5ISBUeXBlIGEgY29tbWFuZCBsaWtlIHNvOgoKICBydW4gKGRlZmluZSBwaSAzLjE0MTU5KQogIHJ1biAoZGVmaW5lIGNpcmNsZS1hcmVhIChsYW1iZGEgKHIpICgqIHBpICgqIHIgcikpKSkKICBydW4gKGNpcmNsZS1hcmVhIDEwKQpgKTsKCnJlYWRsaW5lU3luYy5wcm9tcHRDTExvb3AoewogIHJ1bjogKC4uLmNvZGUpID0+IGNvbnNvbGUubG9nKCc9PicsIGxpc3BsZXQucnVuKGNvZGUuam9pbignICcpKSksCn0pOw=="},"asBuffer":null},"loaded":true}}
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
/**
 * A little LISP written in JavaScript, with many
 * thanks to Anton Davydov and Mary Rose Cook.
 *
 * @see https://github.com/davydovanton/rlisp
 * @see https://github.com/maryrosecook/littlelisp
 *
 * @author Eric Weinstein
 */
const readlineSync = require("readline-sync");

class Lisplet {
  constructor() {
    this.env = {
      "==": function() {
        let [a, b] = Array.from(arguments)[0];
        return a === b;
      },
      "!=": function() {
        let [a, b] = Array.from(arguments)[0];
        return a !== b;
      },
      "<": function() {
        let [a, b] = Array.from(arguments)[0];
        return a < b;
      },
      "<=": function() {
        let [a, b] = Array.from(arguments)[0];
        return a <= b;
      },
      ">": function() {
        let [a, b] = Array.from(arguments)[0];
        return a > b;
      },
      ">=": function() {
        let [a, b] = Array.from(arguments)[0];
        return a >= b;
      },
      "+": function() { return Array.from(arguments)[0].reduce((a, e) => { return a + e; }); },
      "-": function() { return Array.from(arguments)[0].reduce((a, e) => { return a - e; }); },
      "*": function() { return Array.from(arguments)[0].reduce((a, e) => { return a * e; }); },
      "/": function() { return Array.from(arguments)[0].reduce((a, e) => { return a / e; }); },
      "true": true,
      "false": false,
    };
  }

  run(code) {
    return this.eval(this.parse(code));
  }

  parse(program) {
    return this.read(this.tokenize(program));
  }

  tokenize(characters) {
    return characters.
      replace(/\s\s+/g, " ").
      replace(/\(/g, " ( ").
      replace(/\)/g, " ) ").
      split(" ").
      filter(t => " \t\n".indexOf(t) === -1);
  }

  read(tokens) {
    if (tokens.length === 0) {
      return;
    }

    // Grab the first token.
    let token = tokens.shift();

    if (token === "(") {
      let list = [];

      while (tokens[0] !== ")") {
        list.push(this.read(tokens));
      }

      // Keep going (since we may have nested lists).
      tokens.shift();

      return list;
    } else if (token === ")") {
      throw new Error("Unexpected token ')'");
    } else {
      return this.atom(token);
    }
  }

  atom(token) {
    if (/\.\d+/.test(token)) {
      return parseFloat(token);
    } else if (/\d+/.test(token)) {
      return parseInt(token, 10);
    } else if (this.env[token] && typeof this.env[token] !== "function") {
      return this.env[token];
    } else {
      return token.toString();
    }
  }

  eval(expression) {
    if (!expression) {
      return;
    } else if (typeof expression === "number" || typeof expression === "boolean") {
      return expression;
    } else if (Object.keys(this.env).indexOf(expression[0]) !== -1) {
      let fn = expression[0];
      let args = expression.slice(1);

      function handleArgs(args) {
        args = [].slice.call(args);

        for (let i = 0; i < args.length; i++) {
          if (Array.isArray(args[i])) {
            args[i] = this.eval(args[i]);
          }
        }
        return this.eval(args);
      }

      return this.env[fn](handleArgs.call(this, args));
    } else if (expression[0] === "define") {
      let args = expression.slice(1);
      this.env[args[0]] = args[1];
      return global[args[0]] = this.eval(args[1]);
    } else if (expression[0] === "if") {
      let args = expression.slice(1);
      let [test, conseq, alt] = args;
      return this.eval(test) ? conseq : alt;
    } else if (expression[0] === "lambda") {
      let args = expression.slice(1);
      let [params, body] = args;
      return new Function(params, this.eval(body));
    } else {
      let args = expression.slice(1);
      let params = expression[0][1];

      if (params) {
        let body = [].slice.call(expression[0][2]);
        let bound = params.reduce((obj, k, i) => ({...obj, [k]: args[i] }), {});

        function replace(bound, body) {
          body = [].slice.call(body);
          bound = Object.assign({}, bound);

          for (let i = 0; i < body.length; i++) {
            if (Array.isArray(body[i])) {
              body[i] = this.eval(replace.call(this, bound, body[i]));
            }
            if (bound[body[i]]) {
              body[i] = bound[body[i]];
            }
          }
          return this.eval(body);
        }
        return replace.call(this, bound, body);
      }

      return eval(expression);
    }
  }
}

let lisplet = new Lisplet();

console.log(`Lisplet is ready! Type a command like so:

  run (define pi 3.14159)
  run (define circle-area (lambda (r) (* pi (* r r))))
  run (circle-area 10)
`);

readlineSync.promptCLLoop({
  run: (...code) => console.log('=>', lisplet.run(code.join(' '))),
});
node v10.16.0