1 import { readline } from "./node_readline";
3 import { Node, MalType, MalString, MalNil, MalList, MalVector, MalHashMap, MalSymbol, 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 quasiquote(ast: MalType): MalType {
16 return new MalList([MalSymbol.get("quote"), ast]);
19 throw new Error(`unexpected token type: ${ast.type}, expected: list or vector`);
21 const [arg1, arg2] = ast.list;
22 if (arg1.type === Node.Symbol && arg1.v === "unquote") {
27 throw new Error(`unexpected token type: ${arg1.type}, expected: list or vector`);
29 const [arg11, arg12] = arg1.list;
30 if (arg11.type === Node.Symbol && arg11.v === "splice-unquote") {
32 MalSymbol.get("concat"),
34 quasiquote(new MalList(ast.list.slice(1))),
40 MalSymbol.get("cons"),
42 quasiquote(new MalList(ast.list.slice(1))),
45 function isPair(ast: MalType) {
50 return 0 < ast.list.length;
54 function isMacro(ast: MalType, env: Env): boolean {
58 const s = ast.list[0];
59 if (s.type !== Node.Symbol) {
62 const foundEnv = env.find(s);
67 const f = foundEnv.get(s);
68 if (f.type !== Node.Function) {
75 function macroexpand(ast: MalType, env: Env): MalType {
76 while (isMacro(ast, env)) {
78 throw new Error(`unexpected token type: ${ast.type}, expected: list or vector`);
80 const s = ast.list[0];
81 if (s.type !== Node.Symbol) {
82 throw new Error(`unexpected token type: ${s.type}, expected: symbol`);
85 if (f.type !== Node.Function) {
86 throw new Error(`unexpected token type: ${f.type}, expected: function`);
88 ast = f.func(...ast.list.slice(1));
94 function evalAST(ast: MalType, env: Env): MalType {
97 const f = env.get(ast);
99 throw new Error(`unknown symbol: ${ast.v}`);
103 return new MalList(ast.list.map(ast => evalMal(ast, env)));
105 return new MalVector(ast.list.map(ast => evalMal(ast, env)));
107 const list: MalType[] = [];
108 for (const [key, value] of ast.entries()) {
110 list.push(evalMal(value, env));
112 return new MalHashMap(list);
119 function evalMal(ast: MalType, env: Env): MalType {
121 if (ast.type !== Node.List) {
122 return evalAST(ast, env);
125 ast = macroexpand(ast, env);
127 return evalAST(ast, env);
130 if (ast.list.length === 0) {
133 const first = ast.list[0];
134 switch (first.type) {
138 const [, key, value] = ast.list;
139 if (key.type !== Node.Symbol) {
140 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
143 throw new Error(`unexpected syntax`);
145 return env.set(key, evalMal(value, env));
149 const pairs = ast.list[1];
151 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
153 for (let i = 0; i < pairs.list.length; i += 2) {
154 const key = pairs.list[i];
155 const value = pairs.list[i + 1];
156 if (key.type !== Node.Symbol) {
157 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
159 if (!key || !value) {
160 throw new Error(`unexpected syntax`);
163 env.set(key, evalMal(value, env));
172 ast = quasiquote(ast.list[1]);
176 const [, key, value] = ast.list;
177 if (key.type !== Node.Symbol) {
178 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
181 throw new Error(`unexpected syntax`);
183 const f = evalMal(value, env);
184 if (f.type !== Node.Function) {
185 throw new Error(`unexpected token type: ${f.type}, expected: function`);
188 return env.set(key, f);
190 case "macroexpand": {
191 return macroexpand(ast.list[1], env);
194 const list = ast.list.slice(1, -1);
195 evalAST(new MalList(list), env);
196 ast = ast.list[ast.list.length - 1];
200 const [, cond, thenExpr, elseExrp] = ast.list;
201 const ret = evalMal(cond, env);
203 if (ret.type === Node.Boolean && !ret.v) {
205 } else if (ret.type === Node.Nil) {
210 } else if (elseExrp) {
213 ast = MalNil.instance;
218 const [, params, bodyAst] = ast.list;
219 if (!isSeq(params)) {
220 throw new Error(`unexpected return type: ${params.type}, expected: list or vector`);
222 const symbols = params.list.map(param => {
223 if (param.type !== Node.Symbol) {
224 throw new Error(`unexpected return type: ${param.type}, expected: symbol`);
228 return MalFunction.fromLisp(evalMal, env, symbols, bodyAst);
232 const result = evalAST(ast, env);
233 if (!isSeq(result)) {
234 throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
236 const [f, ...args] = result.list;
237 if (f.type !== Node.Function) {
238 throw new Error(`unexpected token: ${f.type}, expected: function`);
242 env = f.newEnv(args);
246 return f.func(...args);
251 function print(exp: MalType): string {
255 const replEnv = new Env();
256 function rep(str: string): string {
257 return print(evalMal(read(str), replEnv));
260 // core.EXT: defined using Racket
261 core.ns.forEach((value, key) => {
262 replEnv.set(key, value);
264 replEnv.set(MalSymbol.get("eval"), MalFunction.fromBootstrap(ast => {
266 throw new Error(`undefined argument`);
268 return evalMal(ast, replEnv);
270 replEnv.set(MalSymbol.get("*ARGV*"), new MalList([]));
272 // core.mal: defined using the language itself
273 rep("(def! not (fn* (a) (if a false true)))");
274 rep(`(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))`);
275 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)))))))`);
276 rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))");
278 if (typeof process !== "undefined" && 2 < process.argv.length) {
279 replEnv.set(MalSymbol.get("*ARGV*"), new MalList(process.argv.slice(3).map(s => new MalString(s))));
280 rep(`(load-file "${process.argv[2]}")`);
285 const line = readline("user> ");
293 console.log(rep(line));
295 const err: Error = e;
296 console.error(err.message);