1 import { readline } from "./node_readline";
3 import { MalType, MalBoolean, MalNull, MalList, MalVector, MalHashMap, MalSymbol, MalFunction } from "./types";
4 import { Env } from "./env";
5 import * as core from "./core";
6 import { readStr } from "./reader";
7 import { prStr } from "./printer";
10 function read(str: string): MalType {
14 function evalAST(ast: MalType, env: Env): MalType {
17 const f = env.get(ast);
19 throw new Error(`unknown symbol: ${ast.v}`);
23 return new MalList(ast.list.map(ast => evalMal(ast, env)));
25 return new MalVector(ast.list.map(ast => evalMal(ast, env)));
27 const list: MalType[] = [];
28 for (const [key, value] of ast.entries()) {
30 list.push(evalMal(value, env));
32 return new MalHashMap(list);
39 function evalMal(ast: MalType, env: Env): MalType {
40 if (ast.type !== "list") {
41 return evalAST(ast, env);
43 if (ast.list.length === 0) {
46 const first = ast.list[0];
51 const [, key, value] = ast.list;
52 if (!MalSymbol.is(key)) {
53 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
56 throw new Error(`unexpected syntax`);
58 return env.set(key, evalMal(value, env))
61 let letEnv = new Env(env);
62 const pairs = ast.list[1];
63 if (!MalList.is(pairs) && !MalVector.is(pairs)) {
64 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
66 for (let i = 0; i < pairs.list.length; i += 2) {
67 const key = pairs.list[i];
68 const value = pairs.list[i + 1];
69 if (!MalSymbol.is(key)) {
70 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
73 throw new Error(`unexpected syntax`);
76 letEnv.set(key, evalMal(value, letEnv));
78 return evalMal(ast.list[2], letEnv);
81 const [, ...list] = ast.list;
82 const ret = evalAST(new MalList(list), env);
83 if (!MalList.is(ret) && !MalVector.is(ret)) {
84 throw new Error(`unexpected return type: ${ret.type}, expected: list or vector`);
86 return ret.list[ret.list.length - 1];
89 const [, cond, thenExpr, elseExrp] = ast.list;
90 const ret = evalMal(cond, env);
92 if (MalBoolean.is(ret) && !ret.v) {
94 } else if (MalNull.is(ret)) {
98 return evalMal(thenExpr, env);
99 } else if (elseExrp) {
100 return evalMal(elseExrp, env);
102 return MalNull.instance;
106 const [, args, binds] = ast.list;
107 if (!MalList.is(args) && !MalVector.is(args)) {
108 throw new Error(`unexpected return type: ${args.type}, expected: list or vector`);
110 const symbols = args.list.map(arg => {
111 if (!MalSymbol.is(arg)) {
112 throw new Error(`unexpected return type: ${arg.type}, expected: symbol`);
116 return MalFunction.fromBootstrap((...fnArgs: MalType[]) => {
117 return evalMal(binds, new Env(env, symbols, fnArgs));
122 const result = evalAST(ast, env);
123 if (!MalList.is(result) && !MalVector.is(result)) {
124 throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
126 const [f, ...args] = result.list;
127 if (!MalFunction.is(f)) {
128 throw new Error(`unexpected token: ${f.type}, expected: function`);
130 return f.func(...args);
134 function print(exp: MalType): string {
138 const replEnv = new Env();
139 function rep(str: string): string {
140 return print(evalMal(read(str), replEnv));
143 // core.EXT: defined using Racket
144 core.ns.forEach((value, key) => {
145 replEnv.set(key, value);
148 // core.mal: defined using the language itself
149 rep("(def! not (fn* (a) (if a false true)))");
152 const line = readline("user> ");
160 console.log(rep(line));
162 const err: Error = e;
163 console.error(err.message);