1 import { readline } from "./node_readline";
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";
10 function read(str: string): MalType {
14 function starts_with(lst: MalType[], sym: string): boolean {
15 if (lst.length == 2) {
25 function qq_loop(elt: MalType, acc: MalList): MalList {
26 if (elt.type == Node.List && starts_with(elt.list, "splice-unquote")) {
27 return new MalList([MalSymbol.get("concat"), elt.list[1], acc]);
29 return new MalList([MalSymbol.get("cons"), quasiquote(elt), acc]);
33 function qq_foldr(xs : MalType[]): MalList {
34 let acc = new MalList([])
35 for (let i=xs.length-1; 0<=i; i-=1) {
36 acc = qq_loop(xs[i], acc)
41 function quasiquote(ast: MalType): MalType {
44 return new MalList([MalSymbol.get("quote"), ast]);
46 return new MalList([MalSymbol.get("quote"), ast]);
48 if (starts_with(ast.list, "unquote")) {
51 return qq_foldr(ast.list);
54 return new MalList([MalSymbol.get("vec"), qq_foldr(ast.list)]);
60 function isMacro(ast: MalType, env: Env): boolean {
64 const s = ast.list[0];
65 if (s.type !== Node.Symbol) {
68 const foundEnv = env.find(s);
73 const f = foundEnv.get(s);
74 if (f.type !== Node.Function) {
81 function macroexpand(ast: MalType, env: Env): MalType {
82 while (isMacro(ast, env)) {
84 throw new Error(`unexpected token type: ${ast.type}, expected: list or vector`);
86 const s = ast.list[0];
87 if (s.type !== Node.Symbol) {
88 throw new Error(`unexpected token type: ${s.type}, expected: symbol`);
91 if (f.type !== Node.Function) {
92 throw new Error(`unexpected token type: ${f.type}, expected: function`);
94 ast = f.func(...ast.list.slice(1));
100 function evalAST(ast: MalType, env: Env): MalType {
103 const f = env.get(ast);
105 throw new Error(`unknown symbol: ${ast.v}`);
109 return new MalList(ast.list.map(ast => evalMal(ast, env)));
111 return new MalVector(ast.list.map(ast => evalMal(ast, env)));
113 const list: MalType[] = [];
114 for (const [key, value] of ast.entries()) {
116 list.push(evalMal(value, env));
118 return new MalHashMap(list);
125 function evalMal(ast: MalType, env: Env): MalType {
127 if (ast.type !== Node.List) {
128 return evalAST(ast, env);
130 if (ast.list.length === 0) {
134 ast = macroexpand(ast, env);
136 return evalAST(ast, env);
139 if (ast.list.length === 0) {
142 const first = ast.list[0];
143 switch (first.type) {
147 const [, key, value] = ast.list;
148 if (key.type !== Node.Symbol) {
149 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
152 throw new Error(`unexpected syntax`);
154 return env.set(key, evalMal(value, env));
158 const pairs = ast.list[1];
160 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
162 for (let i = 0; i < pairs.list.length; i += 2) {
163 const key = pairs.list[i];
164 const value = pairs.list[i + 1];
165 if (key.type !== Node.Symbol) {
166 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
168 if (!key || !value) {
169 throw new Error(`unexpected syntax`);
172 env.set(key, evalMal(value, env));
180 case "quasiquoteexpand": {
181 return quasiquote(ast.list[1]);
184 ast = quasiquote(ast.list[1]);
188 const [, key, value] = ast.list;
189 if (key.type !== Node.Symbol) {
190 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
193 throw new Error(`unexpected syntax`);
195 const f = evalMal(value, env);
196 if (f.type !== Node.Function) {
197 throw new Error(`unexpected token type: ${f.type}, expected: function`);
199 return env.set(key, f.toMacro());
201 case "macroexpand": {
202 return macroexpand(ast.list[1], env);
205 const list = ast.list.slice(1, -1);
206 evalAST(new MalList(list), env);
207 ast = ast.list[ast.list.length - 1];
211 const [, cond, thenExpr, elseExrp] = ast.list;
212 const ret = evalMal(cond, env);
214 if (ret.type === Node.Boolean && !ret.v) {
216 } else if (ret.type === Node.Nil) {
221 } else if (elseExrp) {
224 ast = MalNil.instance;
229 const [, params, bodyAst] = ast.list;
230 if (!isSeq(params)) {
231 throw new Error(`unexpected return type: ${params.type}, expected: list or vector`);
233 const symbols = params.list.map(param => {
234 if (param.type !== Node.Symbol) {
235 throw new Error(`unexpected return type: ${param.type}, expected: symbol`);
239 return MalFunction.fromLisp(evalMal, env, symbols, bodyAst);
243 const result = evalAST(ast, env);
244 if (!isSeq(result)) {
245 throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
247 const [f, ...args] = result.list;
248 if (f.type !== Node.Function) {
249 throw new Error(`unexpected token: ${f.type}, expected: function`);
253 env = f.newEnv(args);
257 return f.func(...args);
262 function print(exp: MalType): string {
266 const replEnv = new Env();
267 function rep(str: string): string {
268 return print(evalMal(read(str), replEnv));
271 // core.EXT: defined using Racket
272 core.ns.forEach((value, key) => {
273 replEnv.set(key, value);
275 replEnv.set(MalSymbol.get("eval"), MalFunction.fromBootstrap(ast => {
277 throw new Error(`undefined argument`);
279 return evalMal(ast, replEnv);
281 replEnv.set(MalSymbol.get("*ARGV*"), new MalList([]));
283 // core.mal: defined using the language itself
284 rep("(def! not (fn* (a) (if a false true)))");
285 rep(`(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) "\nnil)")))))`);
286 rep(`(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw "odd number of forms to cond")) (cons 'cond (rest (rest xs)))))))`);
288 if (typeof process !== "undefined" && 2 < process.argv.length) {
289 replEnv.set(MalSymbol.get("*ARGV*"), new MalList(process.argv.slice(3).map(s => new MalString(s))));
290 rep(`(load-file "${process.argv[2]}")`);
295 const line = readline("user> ");
303 console.log(rep(line));
306 console.error("Error:", prStr(e));
308 const err: Error = e;
309 console.error("Error:", err.message);