1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 10:37:58 +03:00
mal/ts/step2_eval.ts

86 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-02-22 23:18:41 +03:00
import { readline } from "./node_readline";
2017-02-25 08:12:15 +03:00
import { Node, MalType, MalNumber, MalList, MalVector, MalHashMap, MalFunction } from "./types";
2017-02-22 23:18:41 +03:00
import { readStr } from "./reader";
import { prStr } from "./printer";
2017-02-25 05:32:06 +03:00
// READ
2017-02-22 23:18:41 +03:00
function read(str: string): MalType {
return readStr(str);
}
interface MalEnvironment {
[key: string]: MalFunction;
}
function evalAST(ast: MalType, env: MalEnvironment): MalType {
switch (ast.type) {
2017-02-25 08:12:15 +03:00
case Node.Symbol:
2017-02-22 23:18:41 +03:00
const f = env[ast.v];
if (!f) {
throw new Error(`unknown symbol: ${ast.v}`);
}
return f;
2017-02-25 08:12:15 +03:00
case Node.List:
2017-02-25 05:32:06 +03:00
return new MalList(ast.list.map(ast => evalMal(ast, env)));
2017-02-25 08:12:15 +03:00
case Node.Vector:
2017-02-25 05:32:06 +03:00
return new MalVector(ast.list.map(ast => evalMal(ast, env)));
2017-02-25 08:12:15 +03:00
case Node.HashMap:
2017-02-22 23:18:41 +03:00
const list: MalType[] = [];
2017-02-24 18:21:30 +03:00
for (const [key, value] of ast.entries()) {
2017-02-22 23:18:41 +03:00
list.push(key);
2017-02-25 05:32:06 +03:00
list.push(evalMal(value, env));
2017-02-22 23:18:41 +03:00
}
return new MalHashMap(list);
default:
return ast;
}
}
2017-02-25 05:32:06 +03:00
// EVAL
function evalMal(ast: MalType, env: MalEnvironment): MalType {
2017-02-25 08:12:15 +03:00
if (ast.type !== Node.List) {
2017-02-22 23:18:41 +03:00
return evalAST(ast, env);
}
if (ast.list.length === 0) {
return ast;
}
const result = evalAST(ast, env) as MalList;
2017-02-24 10:30:25 +03:00
const [f, ...args] = result.list;
2017-02-25 08:12:15 +03:00
if (f.type !== Node.Function) {
2017-02-22 23:18:41 +03:00
throw new Error(`unexpected token: ${f.type}, expected: function`);
}
2017-02-24 10:30:25 +03:00
return f.func(...args);
2017-02-22 23:18:41 +03:00
}
2017-02-25 05:32:06 +03:00
// PRINT
2017-02-22 23:18:41 +03:00
function print(exp: MalType): string {
return prStr(exp);
}
const replEnv: MalEnvironment = {
2017-02-24 10:30:25 +03:00
"+": MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v + b!.v)),
"-": MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v - b!.v)),
"*": MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v * b!.v)),
"/": MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v / b!.v)),
2017-02-22 23:18:41 +03:00
};
function rep(str: string): string {
2017-02-25 05:32:06 +03:00
return print(evalMal(read(str), replEnv));
2017-02-22 23:18:41 +03:00
}
while (true) {
const line = readline("user> ");
if (line == null) {
break;
}
if (line === "") {
continue;
}
try {
console.log(rep(line));
} catch (e) {
const err: Error = e;
console.error(err.message);
}
}