Move implementations into impls/ dir
[jackhill/mal.git] / impls / ts / step7_quote.ts
1 import { readline } from "./node_readline";
2
3 import { Node, MalType, MalString, MalNil, MalList, MalVector, MalHashMap, MalSymbol, MalFunction, isAST, 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 quasiquote(ast: MalType): MalType {
15 if (!isPair(ast)) {
16 return new MalList([MalSymbol.get("quote"), ast]);
17 }
18 if (!isSeq(ast)) {
19 throw new Error(`unexpected token type: ${ast.type}, expected: list or vector`);
20 }
21 const [arg1, arg2] = ast.list;
22 if (arg1.type === Node.Symbol && arg1.v === "unquote") {
23 return arg2;
24 }
25 if (isPair(arg1)) {
26 if (!isSeq(arg1)) {
27 throw new Error(`unexpected token type: ${arg1.type}, expected: list or vector`);
28 }
29 const [arg11, arg12] = arg1.list;
30 if (arg11.type === Node.Symbol && arg11.v === "splice-unquote") {
31 return new MalList([
32 MalSymbol.get("concat"),
33 arg12,
34 quasiquote(new MalList(ast.list.slice(1))),
35 ]);
36 }
37 }
38
39 return new MalList([
40 MalSymbol.get("cons"),
41 quasiquote(arg1),
42 quasiquote(new MalList(ast.list.slice(1))),
43 ]);
44
45 function isPair(ast: MalType) {
46 if (!isSeq(ast)) {
47 return false;
48 }
49
50 return 0 < ast.list.length;
51 }
52 }
53
54 function evalAST(ast: MalType, env: Env): MalType {
55 switch (ast.type) {
56 case Node.Symbol:
57 const f = env.get(ast);
58 if (!f) {
59 throw new Error(`unknown symbol: ${ast.v}`);
60 }
61 return f;
62 case Node.List:
63 return new MalList(ast.list.map(ast => evalMal(ast, env)));
64 case Node.Vector:
65 return new MalVector(ast.list.map(ast => evalMal(ast, env)));
66 case Node.HashMap:
67 const list: MalType[] = [];
68 for (const [key, value] of ast.entries()) {
69 list.push(key);
70 list.push(evalMal(value, env));
71 }
72 return new MalHashMap(list);
73 default:
74 return ast;
75 }
76 }
77
78 // EVAL
79 function evalMal(ast: MalType, env: Env): MalType {
80 loop: while (true) {
81 if (ast.type !== Node.List) {
82 return evalAST(ast, env);
83 }
84 if (ast.list.length === 0) {
85 return ast;
86 }
87 const first = ast.list[0];
88 switch (first.type) {
89 case Node.Symbol:
90 switch (first.v) {
91 case "def!": {
92 const [, key, value] = ast.list;
93 if (key.type !== Node.Symbol) {
94 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
95 }
96 if (!value) {
97 throw new Error(`unexpected syntax`);
98 }
99 return env.set(key, evalMal(value, env));
100 }
101 case "let*": {
102 env = new Env(env);
103 const pairs = ast.list[1];
104 if (!isSeq(pairs)) {
105 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
106 }
107 for (let i = 0; i < pairs.list.length; i += 2) {
108 const key = pairs.list[i];
109 const value = pairs.list[i + 1];
110 if (key.type !== Node.Symbol) {
111 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
112 }
113 if (!key || !value) {
114 throw new Error(`unexpected syntax`);
115 }
116
117 env.set(key, evalMal(value, env));
118 }
119 ast = ast.list[2];
120 continue loop;
121 }
122 case "quote": {
123 return ast.list[1];
124 }
125 case "quasiquote": {
126 ast = quasiquote(ast.list[1]);
127 continue loop;
128 }
129 case "do": {
130 const list = ast.list.slice(1, -1);
131 evalAST(new MalList(list), env);
132 ast = ast.list[ast.list.length - 1];
133 continue loop;
134 }
135 case "if": {
136 const [, cond, thenExpr, elseExrp] = ast.list;
137 const ret = evalMal(cond, env);
138 let b = true;
139 if (ret.type === Node.Boolean && !ret.v) {
140 b = false;
141 } else if (ret.type === Node.Nil) {
142 b = false;
143 }
144 if (b) {
145 ast = thenExpr;
146 } else if (elseExrp) {
147 ast = elseExrp;
148 } else {
149 ast = MalNil.instance;
150 }
151 continue loop;
152 }
153 case "fn*": {
154 const [, params, bodyAst] = ast.list;
155 if (!isSeq(params)) {
156 throw new Error(`unexpected return type: ${params.type}, expected: list or vector`);
157 }
158 const symbols = params.list.map(param => {
159 if (param.type !== Node.Symbol) {
160 throw new Error(`unexpected return type: ${param.type}, expected: symbol`);
161 }
162 return param;
163 });
164 return MalFunction.fromLisp(evalMal, env, symbols, bodyAst);
165 }
166 }
167 }
168 const result = evalAST(ast, env);
169 if (!isSeq(result)) {
170 throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
171 }
172 const [f, ...args] = result.list;
173 if (f.type !== Node.Function) {
174 throw new Error(`unexpected token: ${f.type}, expected: function`);
175 }
176 if (f.ast) {
177 ast = f.ast;
178 env = f.newEnv(args);
179 continue loop;
180 }
181
182 return f.func(...args);
183 }
184 }
185
186 // PRINT
187 function print(exp: MalType): string {
188 return prStr(exp);
189 }
190
191 const replEnv = new Env();
192 function rep(str: string): string {
193 return print(evalMal(read(str), replEnv));
194 }
195
196 // core.EXT: defined using Racket
197 core.ns.forEach((value, key) => {
198 replEnv.set(key, value);
199 });
200 replEnv.set(MalSymbol.get("eval"), MalFunction.fromBootstrap(ast => {
201 if (!ast) {
202 throw new Error(`undefined argument`);
203 }
204 return evalMal(ast, replEnv);
205 }));
206 replEnv.set(MalSymbol.get("*ARGV*"), new MalList([]));
207
208 // core.mal: defined using the language itself
209 rep("(def! not (fn* (a) (if a false true)))");
210 rep(`(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) "\nnil)")))))`);
211
212 if (typeof process !== "undefined" && 2 < process.argv.length) {
213 replEnv.set(MalSymbol.get("*ARGV*"), new MalList(process.argv.slice(3).map(s => new MalString(s))));
214 rep(`(load-file "${process.argv[2]}")`);
215 process.exit(0);
216 }
217
218 while (true) {
219 const line = readline("user> ");
220 if (line == null) {
221 break;
222 }
223 if (line === "") {
224 continue;
225 }
226 try {
227 console.log(rep(line));
228 } catch (e) {
229 if (isAST(e)) {
230 console.error("Error:", prStr(e));
231 } else {
232 const err: Error = e;
233 console.error("Error:", err.message);
234 }
235 }
236 }