2024-02-05 17:00:06 +00:00
|
|
|
#programming-language #compiler-design #lisp #tips
|
2023-11-24 15:30:28 +00:00
|
|
|
|
2023-11-24 17:50:09 +00:00
|
|
|
[Make A Lisp](https://github.com/kanaka/mal)よりさらに最小限の実装ってどのくらいかなー、というのを考えた。Cons、Cdr、Carは無い。演算は四則演算とifのみ。その代わりクロージャは作れるのでこのくらいのことはできる。
|
2023-11-24 15:30:28 +00:00
|
|
|
|
|
|
|
```lisp
|
|
|
|
(let double (lambda (x) (* x x)) (double 6))
|
|
|
|
```
|
|
|
|
|
2023-11-24 17:30:30 +00:00
|
|
|
ただ再帰は今のところ難しい。多分ListとAtomの区別がきちんとできてないせいで不動点コンビネータがきちんと動かない。
|
|
|
|
|
2023-11-24 17:50:09 +00:00
|
|
|
あくまで雰囲気重視で、ということなのだが、結果的にコードが短いため初学者にわかりやすいかと言われると、かなりコメントを足していかないと読めなさそうで矛盾してる気がする。
|
2023-11-24 15:30:28 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
const reader = require("readline").createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
});
|
|
|
|
|
|
|
|
const tokenize = (str) =>
|
|
|
|
str.match(/[\(]|[\)]|[\+\-\*\/]|[a-zA-Z_]+|(-?\d+(\.\d+)?)/g);
|
|
|
|
const parse_token = (token, stack, res) => {
|
|
|
|
switch (token) {
|
|
|
|
case '(': {
|
|
|
|
let child = [];
|
|
|
|
stack.push(child);
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
case ')': {
|
|
|
|
let c = stack.pop();
|
|
|
|
if (stack.length == 0) {
|
|
|
|
return c
|
|
|
|
} else {
|
|
|
|
stack[stack.length - 1].push(c)
|
|
|
|
return stack[stack.length - 1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
res.push(token);
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const parse = (tokens) => {
|
|
|
|
let stack = [];
|
|
|
|
let next_res = [];
|
|
|
|
for (token of tokens) {
|
|
|
|
next_res = parse_token(token, stack, next_res)
|
|
|
|
}
|
|
|
|
return next_res
|
|
|
|
}
|
2023-11-24 17:30:30 +00:00
|
|
|
const read = (str) => parse(tokenize(str));
|
2023-11-24 15:30:28 +00:00
|
|
|
|
|
|
|
const binop = (args, fn) => fn(args[0], args[1]);
|
|
|
|
const env = {
|
|
|
|
"parent": null,
|
|
|
|
"+": args => binop(args, (a, b) => a + b),
|
|
|
|
"-": args => binop(args, (a, b) => a - b),
|
|
|
|
"*": args => binop(args, (a, b) => a * b),
|
2023-11-24 17:30:30 +00:00
|
|
|
"/": args => binop(args, (a, b) => a / b),
|
|
|
|
"if": args => args[0] ? args[1] : args[2]
|
2023-11-24 15:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const try_find = (name, env) => {
|
|
|
|
if (env)
|
|
|
|
if (env[name]) {
|
|
|
|
return env[name]
|
|
|
|
} else {
|
|
|
|
return try_find(name, env["parent"])
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const eval = (expr, env) => {
|
2023-11-24 17:30:30 +00:00
|
|
|
if (expr ==null){
|
|
|
|
return null;
|
|
|
|
}
|
2023-11-24 15:30:28 +00:00
|
|
|
if (!Array.isArray(expr)) {
|
|
|
|
return !isNaN(parseFloat(expr)) ? parseFloat(expr) : try_find(expr, env)
|
|
|
|
} else {
|
|
|
|
const op = expr.shift();
|
|
|
|
switch (op) {
|
|
|
|
case 'let': {
|
2023-11-24 17:30:30 +00:00
|
|
|
let res = eval(expr[1], env);
|
2023-11-24 15:30:28 +00:00
|
|
|
let newenv = { "parent": env };
|
2023-11-24 17:30:30 +00:00
|
|
|
newenv[expr[0]] = res;
|
|
|
|
return eval(expr[2], newenv)
|
2023-11-24 15:30:28 +00:00
|
|
|
}
|
|
|
|
case 'lambda': return ['closure', expr[0], expr[1], env]//ids,body,env
|
|
|
|
default: {
|
2023-11-24 17:30:30 +00:00
|
|
|
let fn = eval(op, env);
|
2023-11-24 15:30:28 +00:00
|
|
|
if (fn) {
|
2023-11-24 17:30:30 +00:00
|
|
|
if (Array.isArray(fn) && fn[0] === "closure") {//closure
|
2023-11-24 15:30:28 +00:00
|
|
|
for (let i = 0; i < fn[1].length; i++) {
|
2023-11-24 17:30:30 +00:00
|
|
|
fn[3][fn[1][i]] = eval(expr[i],env)
|
2023-11-24 15:30:28 +00:00
|
|
|
}
|
|
|
|
return eval(fn[2], fn[3])
|
2023-11-24 17:30:30 +00:00
|
|
|
} else {//extern function
|
2023-11-24 15:30:28 +00:00
|
|
|
return fn(expr.map(e => eval(e, env)))
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-24 17:58:50 +00:00
|
|
|
return null
|
2023-11-24 15:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const rep = (line) => {
|
|
|
|
console.log(eval(read(line), env))
|
|
|
|
}
|
|
|
|
reader.on("line", (line) => {
|
|
|
|
if (line) { rep(line); }
|
|
|
|
});
|
|
|
|
```
|