small fix
[jackhill/mal.git] / ts / step5_tco.ts
1 import { readline } from "./node_readline";
2
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";
8
9 // READ
10 function read(str: string): MalType {
11 return readStr(str);
12 }
13
14 function evalAST(ast: MalType, env: Env): MalType {
15 switch (ast.type) {
16 case Node.Symbol:
17 const f = env.get(ast);
18 if (!f) {
19 throw new Error(`unknown symbol: ${ast.v}`);
20 }
21 return f;
22 case Node.List:
23 return new MalList(ast.list.map(ast => evalMal(ast, env)));
24 case Node.Vector:
25 return new MalVector(ast.list.map(ast => evalMal(ast, env)));
26 case Node.HashMap:
27 const list: MalType[] = [];
28 for (const [key, value] of ast.entries()) {
29 list.push(key);
30 list.push(evalMal(value, env));
31 }
32 return new MalHashMap(list);
33 default:
34 return ast;
35 }
36 }
37
38 // EVAL
39 function evalMal(ast: MalType, env: Env): MalType {
40 loop: while (true) {
41 if (ast.type !== Node.List) {
42 return evalAST(ast, env);
43 }
44 if (ast.list.length === 0) {
45 return ast;
46 }
47 const first = ast.list[0];
48 switch (first.type) {
49 case Node.Symbol:
50 switch (first.v) {
51 case "def!": {
52 const [, key, value] = ast.list;
53 if (key.type !== Node.Symbol) {
54 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
55 }
56 if (!value) {
57 throw new Error(`unexpected syntax`);
58 }
59 return env.set(key, evalMal(value, env));
60 }
61 case "let*": {
62 env = new Env(env);
63 const pairs = ast.list[1];
64 if (!isSeq(pairs)) {
65 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
66 }
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`);
72 }
73 if (!key || !value) {
74 throw new Error(`unexpected syntax`);
75 }
76
77 env.set(key, evalMal(value, env));
78 }
79 ast = ast.list[2];
80 continue loop;
81 }
82 case "do": {
83 const list = ast.list.slice(1, -1);
84 evalAST(new MalList(list), env);
85 ast = ast.list[ast.list.length - 1];
86 continue loop;
87 }
88 case "if": {
89 const [, cond, thenExpr, elseExrp] = ast.list;
90 const ret = evalMal(cond, env);
91 let b = true;
92 if (ret.type === Node.Boolean && !ret.v) {
93 b = false;
94 } else if (ret.type === Node.Nil) {
95 b = false;
96 }
97 if (b) {
98 ast = thenExpr;
99 } else if (elseExrp) {
100 ast = elseExrp;
101 } else {
102 ast = MalNil.instance;
103 }
104 continue loop;
105 }
106 case "fn*": {
107 const [, params, bodyAst] = ast.list;
108 if (!isSeq(params)) {
109 throw new Error(`unexpected return type: ${params.type}, expected: list or vector`);
110 }
111 const symbols = params.list.map(param => {
112 if (param.type !== Node.Symbol) {
113 throw new Error(`unexpected return type: ${param.type}, expected: symbol`);
114 }
115 return param;
116 });
117 return MalFunction.fromLisp(evalMal, env, symbols, bodyAst);
118 }
119 }
120 }
121 const result = evalAST(ast, env);
122 if (!isSeq(result)) {
123 throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
124 }
125 const [f, ...args] = result.list;
126 if (f.type !== Node.Function) {
127 throw new Error(`unexpected token: ${f.type}, expected: function`);
128 }
129 if (f.ast) {
130 ast = f.ast;
131 env = f.newEnv(args);
132 continue loop;
133 }
134
135 return f.func(...args);
136 }
137 }
138
139 // PRINT
140 function print(exp: MalType): string {
141 return prStr(exp);
142 }
143
144 const replEnv = new Env();
145 function rep(str: string): string {
146 return print(evalMal(read(str), replEnv));
147 }
148
149 // core.EXT: defined using Racket
150 core.ns.forEach((value, key) => {
151 replEnv.set(key, value);
152 });
153
154 // core.mal: defined using the language itself
155 rep("(def! not (fn* (a) (if a false true)))");
156
157 while (true) {
158 const line = readline("user> ");
159 if (line == null) {
160 break;
161 }
162 if (line === "") {
163 continue;
164 }
165 try {
166 console.log(rep(line));
167 } catch (e) {
168 const err: Error = e;
169 console.error(err.message);
170 }
171 }