1 import { readline } from "./node_readline";
3 import { Node, MalType, MalNil, MalList, MalVector, MalHashMap, MalFunction, isSeq } 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 {
41 if (ast.type !== Node.List) {
42 return evalAST(ast, env);
44 if (ast.list.length === 0) {
47 const first = ast.list[0];
52 const [, key, value] = ast.list;
53 if (key.type !== Node.Symbol) {
54 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
57 throw new Error(`unexpected syntax`);
59 return env.set(key, evalMal(value, env));
63 const pairs = ast.list[1];
65 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
67 for (let i = 0; i < pairs.list.length; i += 2) {
68 const key = pairs.list[i];
69 const value = pairs.list[i + 1];
70 if (key.type !== Node.Symbol) {
71 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
74 throw new Error(`unexpected syntax`);
77 env.set(key, evalMal(value, env));
83 const list = ast.list.slice(1, -1);
84 evalAST(new MalList(list), env);
85 ast = ast.list[ast.list.length - 1];
89 const [, cond, thenExpr, elseExrp] = ast.list;
90 const ret = evalMal(cond, env);
92 if (ret.type === Node.Boolean && !ret.v) {
94 } else if (ret.type === Node.Nil) {
99 } else if (elseExrp) {
102 ast = MalNil.instance;
107 const [, params, bodyAst] = ast.list;
108 if (!isSeq(params)) {
109 throw new Error(`unexpected return type: ${params.type}, expected: list or vector`);
111 const symbols = params.list.map(param => {
112 if (param.type !== Node.Symbol) {
113 throw new Error(`unexpected return type: ${param.type}, expected: symbol`);
117 return MalFunction.fromLisp(evalMal, env, symbols, bodyAst);
121 const result = evalAST(ast, env);
122 if (!isSeq(result)) {
123 throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
125 const [f, ...args] = result.list;
126 if (f.type !== Node.Function) {
127 throw new Error(`unexpected token: ${f.type}, expected: function`);
131 env = f.newEnv(args);
135 return f.func(...args);
140 function print(exp: MalType): string {
144 const replEnv = new Env();
145 function rep(str: string): string {
146 return print(evalMal(read(str), replEnv));
149 // core.EXT: defined using Racket
150 core.ns.forEach((value, key) => {
151 replEnv.set(key, value);
154 // core.mal: defined using the language itself
155 rep("(def! not (fn* (a) (if a false true)))");
158 const line = readline("user> ");
166 console.log(rep(line));
168 const err: Error = e;
169 console.error(err.message);