more fitting to process's txt
[jackhill/mal.git] / ts / step4_if_fn_do.ts
1 import { readline } from "./node_readline";
2
3 import { MalType, MalBoolean, MalNull, MalList, MalVector, MalHashMap, MalSymbol, MalFunction } 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 "symbol":
17 const f = env.get(ast);
18 if (!f) {
19 throw new Error(`unknown symbol: ${ast.v}`);
20 }
21 return f;
22 case "list":
23 return new MalList(ast.list.map(ast => evalMal(ast, env)));
24 case "vector":
25 return new MalVector(ast.list.map(ast => evalMal(ast, env)));
26 case "hash-map":
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 if (ast.type !== "list") {
41 return evalAST(ast, env);
42 }
43 if (ast.list.length === 0) {
44 return ast;
45 }
46 const first = ast.list[0];
47 switch (first.type) {
48 case "symbol":
49 switch (first.v) {
50 case "def!": {
51 const [, key, value] = ast.list;
52 if (!MalSymbol.is(key)) {
53 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
54 }
55 if (!value) {
56 throw new Error(`unexpected syntax`);
57 }
58 return env.set(key, evalMal(value, env))
59 }
60 case "let*": {
61 let letEnv = new Env(env);
62 const pairs = ast.list[1];
63 if (!MalList.is(pairs) && !MalVector.is(pairs)) {
64 throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
65 }
66 for (let i = 0; i < pairs.list.length; i += 2) {
67 const key = pairs.list[i];
68 const value = pairs.list[i + 1];
69 if (!MalSymbol.is(key)) {
70 throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
71 }
72 if (!key || !value) {
73 throw new Error(`unexpected syntax`);
74 }
75
76 letEnv.set(key, evalMal(value, letEnv));
77 }
78 return evalMal(ast.list[2], letEnv);
79 }
80 case "do": {
81 const [, ...list] = ast.list;
82 const ret = evalAST(new MalList(list), env);
83 if (!MalList.is(ret) && !MalVector.is(ret)) {
84 throw new Error(`unexpected return type: ${ret.type}, expected: list or vector`);
85 }
86 return ret.list[ret.list.length - 1];
87 }
88 case "if": {
89 const [, cond, thenExpr, elseExrp] = ast.list;
90 const ret = evalMal(cond, env);
91 let b = true;
92 if (MalBoolean.is(ret) && !ret.v) {
93 b = false;
94 } else if (MalNull.is(ret)) {
95 b = false;
96 }
97 if (b) {
98 return evalMal(thenExpr, env);
99 } else if (elseExrp) {
100 return evalMal(elseExrp, env);
101 } else {
102 return MalNull.instance;
103 }
104 }
105 case "fn*": {
106 const [, args, binds] = ast.list;
107 if (!MalList.is(args) && !MalVector.is(args)) {
108 throw new Error(`unexpected return type: ${args.type}, expected: list or vector`);
109 }
110 const symbols = args.list.map(arg => {
111 if (!MalSymbol.is(arg)) {
112 throw new Error(`unexpected return type: ${arg.type}, expected: symbol`);
113 }
114 return arg;
115 });
116 return MalFunction.fromBootstrap((...fnArgs: MalType[]) => {
117 return evalMal(binds, new Env(env, symbols, fnArgs));
118 });
119 }
120 }
121 }
122 const result = evalAST(ast, env);
123 if (!MalList.is(result) && !MalVector.is(result)) {
124 throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
125 }
126 const [f, ...args] = result.list;
127 if (!MalFunction.is(f)) {
128 throw new Error(`unexpected token: ${f.type}, expected: function`);
129 }
130 return f.func(...args);
131 }
132
133 // PRINT
134 function print(exp: MalType): string {
135 return prStr(exp);
136 }
137
138 const replEnv = new Env();
139 function rep(str: string): string {
140 return print(evalMal(read(str), replEnv));
141 }
142
143 // core.EXT: defined using Racket
144 core.ns.forEach((value, key) => {
145 replEnv.set(key, value);
146 });
147
148 // core.mal: defined using the language itself
149 rep("(def! not (fn* (a) (if a false true)))");
150
151 while (true) {
152 const line = readline("user> ");
153 if (line == null) {
154 break;
155 }
156 if (line === "") {
157 continue;
158 }
159 try {
160 console.log(rep(line));
161 } catch (e) {
162 const err: Error = e;
163 console.error(err.message);
164 }
165 }