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 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 evalAST(ast: MalType, env: Env): MalType {
57 const f = env.get(ast);
59 throw new Error(`unknown symbol: ${ast.v}`);
63 return new MalList(ast.list.map(ast => evalMal(ast, env)));
65 return new MalVector(ast.list.map(ast => evalMal(ast, env)));
67 const list: MalType[] = [];
68 for (const [key, value] of ast.entries()) {
70 list.push(evalMal(value, env));
72 return new MalHashMap(list);
79 function evalMal(ast: MalType, env: Env): MalType {
81 if (ast.type !== Node.List) {
82 return evalAST(ast, env);
84 if (ast.list.length === 0) {
87 const first = ast.list[0];
92 const [, key, value] = ast.list;
93 if (key.type !== Node.Symbol) {
94 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
97 throw new Error(`unexpected syntax`);
99 return env.set(key, evalMal(value, env));
103 const pairs = ast.list[1];
105 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
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`);
113 if (!key || !value) {
114 throw new Error(`unexpected syntax`);
117 env.set(key, evalMal(value, env));
126 ast = quasiquote(ast.list[1]);
130 const list = ast.list.slice(1, -1);
131 evalAST(new MalList(list), env);
132 ast = ast.list[ast.list.length - 1];
136 const [, cond, thenExpr, elseExrp] = ast.list;
137 const ret = evalMal(cond, env);
139 if (ret.type === Node.Boolean && !ret.v) {
141 } else if (ret.type === Node.Nil) {
146 } else if (elseExrp) {
149 ast = MalNil.instance;
154 const [, params, bodyAst] = ast.list;
155 if (!isSeq(params)) {
156 throw new Error(`unexpected return type: ${params.type}, expected: list or vector`);
158 const symbols = params.list.map(param => {
159 if (param.type !== Node.Symbol) {
160 throw new Error(`unexpected return type: ${param.type}, expected: symbol`);
164 return MalFunction.fromLisp(evalMal, env, symbols, bodyAst);
168 const result = evalAST(ast, env);
169 if (!isSeq(result)) {
170 throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
172 const [f, ...args] = result.list;
173 if (f.type !== Node.Function) {
174 throw new Error(`unexpected token: ${f.type}, expected: function`);
178 env = f.newEnv(args);
182 return f.func(...args);
187 function print(exp: MalType): string {
191 const replEnv = new Env();
192 function rep(str: string): string {
193 return print(evalMal(read(str), replEnv));
196 // core.EXT: defined using Racket
197 core.ns.forEach((value, key) => {
198 replEnv.set(key, value);
200 replEnv.set(MalSymbol.get("eval"), MalFunction.fromBootstrap(ast => {
202 throw new Error(`undefined argument`);
204 return evalMal(ast, replEnv);
206 replEnv.set(MalSymbol.get("*ARGV*"), new MalList([]));
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)")))))`);
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]}")`);
219 const line = readline("user> ");
227 console.log(rep(line));
230 console.error("Error:", prStr(e));
232 const err: Error = e;
233 console.error("Error:", err.message);