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";
9 function read(str: string): MalType {
13 function evalAST(ast: MalType, env: Env): MalType {
16 const f = env.get(ast);
18 throw new Error(`unknown symbol: ${ast.v}`);
22 return new MalList(ast.list.map(ast => evalSexp(ast, env)));
24 return new MalVector(ast.list.map(ast => evalSexp(ast, env)));
26 const list: MalType[] = [];
27 for (const [key, value] of ast.entries()) {
29 list.push(evalSexp(value, env));
31 return new MalHashMap(list);
37 function evalSexp(ast: MalType, env: Env): MalType {
38 if (ast.type !== "list") {
39 return evalAST(ast, env);
41 if (ast.list.length === 0) {
44 const first = ast.list[0];
49 const [, key, value] = ast.list;
50 if (!MalSymbol.is(key)) {
51 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
54 throw new Error(`unexpected syntax`);
56 return env.set(key, evalSexp(value, env))
59 let letEnv = new Env(env);
60 const pairs = ast.list[1];
61 if (!MalList.is(pairs) && !MalVector.is(pairs)) {
62 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
64 for (let i = 0; i < pairs.list.length; i += 2) {
65 const key = pairs.list[i];
66 const value = pairs.list[i + 1];
67 if (!MalSymbol.is(key)) {
68 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
71 throw new Error(`unexpected syntax`);
74 letEnv.set(key, evalSexp(value, letEnv));
76 return evalSexp(ast.list[2], letEnv);
79 const [, ...list] = ast.list;
80 const ret = evalAST(new MalList(list), env);
81 if (!MalList.is(ret) && !MalVector.is(ret)) {
82 throw new Error(`unexpected return type: ${ret.type}, expected: list or vector`);
84 return ret.list[ret.list.length - 1];
87 const [, cond, thenExpr, elseExrp] = ast.list;
88 const ret = evalSexp(cond, env);
90 if (MalBoolean.is(ret) && !ret.v) {
92 } else if (MalNull.is(ret)) {
96 return evalSexp(thenExpr, env);
97 } else if (elseExrp) {
98 return evalSexp(elseExrp, env);
100 return MalNull.instance;
104 const [, args, binds] = ast.list;
105 if (!MalList.is(args) && !MalVector.is(args)) {
106 throw new Error(`unexpected return type: ${args.type}, expected: list or vector`);
108 const symbols = args.list.map(arg => {
109 if (!MalSymbol.is(arg)) {
110 throw new Error(`unexpected return type: ${arg.type}, expected: symbol`);
114 return MalFunction.fromBootstrap((...fnArgs: MalType[]) => {
115 return evalSexp(binds, new Env(env, symbols, fnArgs));
120 const result = evalAST(ast, env);
121 if (!MalList.is(result) && !MalVector.is(result)) {
122 throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
124 const [f, ...args] = result.list;
125 if (!MalFunction.is(f)) {
126 throw new Error(`unexpected token: ${f.type}, expected: function`);
128 return f.func(...args);
131 function print(exp: MalType): string {
135 const replEnv = new Env();
136 core.ns.forEach((value, key) => {
137 replEnv.set(key, value);
140 // core.mal: defined using the language itself
141 rep("(def! not (fn* (a) (if a false true)))");
143 function rep(str: string): string {
144 return print(evalSexp(read(str), replEnv));
148 const line = readline("user> ");
156 console.log(rep(line));
158 const err: Error = e;
159 console.error(err.message);