add REPL.
(it's not done yet, but the entry point already imports this since like 2 commits back.)
This commit is contained in:
parent
27b019595d
commit
047c2a4a42
|
@ -0,0 +1,45 @@
|
|||
import Line from "./Line.mjs";
|
||||
import { parse_line } from "./parse.mjs";
|
||||
import { compile_line } from "./compile.mjs";
|
||||
import { execute } from "./execute.mjs";
|
||||
|
||||
|
||||
|
||||
let env = new Map();
|
||||
|
||||
function run (line) {
|
||||
const parsed = parse_line(line);
|
||||
const type = parsed.type;
|
||||
|
||||
const compiled = compile_line(env, parsed);
|
||||
|
||||
if (!compiled.valid) {
|
||||
console.log("invalid line!");
|
||||
return;
|
||||
}
|
||||
|
||||
env = compiled.env;
|
||||
|
||||
if (type === Line.Expression) {
|
||||
console.log(prettify(evaluate(compiled.value)));
|
||||
} else {
|
||||
console.log(env);
|
||||
}
|
||||
}
|
||||
|
||||
function evaluate (fn) {
|
||||
return execute(fn[0])(fn[1]);
|
||||
}
|
||||
|
||||
function prettify (code) {
|
||||
if (Array.isArray(code)) {
|
||||
return prettify(code[0]).concat(" ", prettify(code[1]));
|
||||
}
|
||||
if (code === 0) {
|
||||
return "λ";
|
||||
}
|
||||
return code.toString();
|
||||
}
|
||||
|
||||
run("id = a → a");
|
||||
run("id id");
|
Loading…
Reference in New Issue