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.map) {
29 list.push(evalSexp(value, env));
31 return new MalHashMap(list);
37 function evalSexp(ast: MalType, env: Env): MalType {
39 if (ast.type !== "list") {
40 return evalAST(ast, env);
42 if (ast.list.length === 0) {
45 const first = ast.list[0];
50 const [, key, value] = ast.list;
51 if (!MalSymbol.is(key)) {
52 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
55 throw new Error(`unexpected syntax`);
57 return env.set(key, evalSexp(value, env))
61 const pairs = ast.list[1];
62 if (!MalList.is(pairs) && !MalVector.is(pairs)) {
63 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
65 for (let i = 0; i < pairs.list.length; i += 2) {
66 const key = pairs.list[i];
67 const value = pairs.list[i + 1];
68 if (!MalSymbol.is(key)) {
69 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
72 throw new Error(`unexpected syntax`);
75 env.set(key, evalSexp(value, env));
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 ast = ret.list[ret.list.length - 1];
90 const [, cond, thenExpr, elseExrp] = ast.list;
91 const ret = evalSexp(cond, env);
93 if (MalBoolean.is(ret) && !ret.v) {
95 } else if (MalNull.is(ret)) {
100 } else if (elseExrp) {
103 ast = MalNull.instance;
108 const [, params, bodyAst] = ast.list;
109 if (!MalList.is(params) && !MalVector.is(params)) {
110 throw new Error(`unexpected return type: ${params.type}, expected: list or vector`);
112 const symbols = params.list.map(param => {
113 if (!MalSymbol.is(param)) {
114 throw new Error(`unexpected return type: ${param.type}, expected: symbol`);
118 return MalFunction.fromLisp(evalSexp, env, symbols, bodyAst);
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`);
132 env = f.newEnv(args);
136 return f.func(...args);
140 function print(exp: MalType): string {
144 const replEnv = new Env();
145 for (const [key, value] of core.ns) {
146 replEnv.set(key, value);
149 // core.mal: defined using the language itself
150 rep("(def! not (fn* (a) (if a false true)))");
152 function rep(str: string): string {
153 return print(evalSexp(read(str), replEnv));
157 const line = readline("user> ");
165 console.log(rep(line));
167 const err: Error = e;
168 console.error(err.message);