-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
236 lines (214 loc) · 6.35 KB
/
Copy pathmain.ts
File metadata and controls
236 lines (214 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const DEBUG = false;
const TIME = true;
import * as readline from "node:readline";
import { stdin as input, stdout as output } from "node:process";
type Sym = string;
type Num = number;
type Atom = Sym | Num;
type List = Array<Exp>;
type Exp = Atom | List;
class Env extends Map<string, Exp | Proc | Function> {
outer: Env | null;
constructor(params: Sym[] = [], args = [], outer = null) {
super();
params.forEach((p, i) => {
this.set(p, args[i]);
});
this.outer = outer;
}
find(variable: string): Env | null {
if (this.has(variable)) return this;
else if (this.outer) return this.outer.find(variable);
else return null;
}
}
class Proc extends Function {
params: List;
body: Exp;
env: Env;
constructor(params: List, body: Exp, env: Env) {
super();
this.params = params;
this.body = body;
this.env = env;
return new Proxy(this, {
// @ts-ignore
apply: (target, thisArg, argArray) => target.call(...argArray),
});
}
call(args: List) {
/* TODO: Handle invalid (non-string) params */
// @ts-ignore
return evaluate(this.body, new Env(this.params, args, this.env));
}
}
function tokenize(chars: string): Array<string> {
/* Convert a string of characters into a list of tokens */
return chars
.replaceAll("(", " ( ")
.replaceAll(")", " ) ")
.split(" ")
.filter((r) => r != "");
}
function parse(program: string): Exp {
/* Read a Scheme expression from a string. */
return readFromTokens(tokenize(program));
}
function readFromTokens(tokens: Array<string>): Exp {
/* Read an expression from a sequence of tokens */
if (tokens.length == 0) {
throw new SyntaxError("Unexpected EOF.");
}
const token = tokens.shift();
if (token == "(") {
let L: List = [];
while (tokens[0] != ")") {
L.push(readFromTokens(tokens));
}
tokens.shift();
return L;
} else if (token == ")") {
throw new SyntaxError("Unexpected )");
} else {
return atom(token!);
}
}
function atom(token: string): Atom {
/* Numbers become numbers, every other token is a symbol. */
const i = parseInt(token);
if (isNaN(i)) {
const f = parseFloat(token);
if (isNaN(f)) {
return token;
}
return f;
}
return i;
}
function standardEnv(): Env {
/* An environment with some Scheme standard procedures */
const env = new Env();
/* TODO */
// @ts-ignore
const globals: Array<[string, Exp | Function]> = [
["+", (x: Array<Num>) => x.reduce((p, c) => p + c)],
["-", (x: Array<Num>) => x.reduce((p, c) => p - c)],
["*", (x: Array<Num>) => x.reduce((p, c) => p * c)],
["/", (x: Array<Num>) => x.reduce((p, c) => p / c)],
[">", ([a, b]: Array<Num>) => a > b],
[">=", ([a, b]: Array<Num>) => a >= b],
["<", ([a, b]: Array<Num>) => a < b],
["<=", ([a, b]: Array<Num>) => a <= b],
["=", ([a, b]: Array<Num>) => a == b],
["abs", ([x]: Array<Num>) => Math.abs(x)],
["apply", (proc: Function, args: List) => proc(args)],
["car", ([x]: [Array<Num>]) => x[0]],
["cdr", ([x]: [Array<Num>]) => x.slice(1)],
["cons", ([x, y]: [Num, Array<Num>]) => [x, ...y]],
["equal?", ([x, y]: [Atom, Atom]) => x == y],
["list", (x: Array<Num>) => x],
["pi", Math.PI],
["pow", ([a, b]: Array<Num>) => Math.pow(a, b)],
["begin", (x: List) => x.at(-1)],
];
globals.forEach((t) => {
env.set(t[0], t[1]);
});
return env;
}
const globalEnv = standardEnv();
function evaluate(x: Exp, env: Env = globalEnv): Exp | Proc {
/* Evaluate an expression in an environment */
DEBUG && console.log({ x });
DEBUG && console.log({ env });
while (true) {
if (typeof x === "string") {
DEBUG && console.log(env.find(x));
const closestEnv = env.find(x);
/* Safe because closestEnv exists iff x belongs to closestEnv */
// @ts-ignore
if (closestEnv) return env.find(x)?.get(x);
else throw new Error(`Undefined symbol '${x}'`);
} else if (!Array.isArray(x)) {
return x;
}
const [op, ...args] = x;
DEBUG && console.log({ op }, { args });
if (op === "quote") {
return args[0];
} else if (op === "if") {
// @ts-ignore
const [test, conseq, alt] = args;
DEBUG && console.log({ test }, { conseq }, { alt });
const pred = evaluate(test, env);
DEBUG && console.log({ pred });
if (Array.isArray(pred)) {
/* [] is falsey in Lisp but truthy in JS */
x = pred.length ? conseq : alt;
} else {
x = pred ? conseq : alt;
}
} else if (op === "define") {
const [symbol, exp] = args;
if (typeof symbol === "string") {
env.set(symbol, evaluate(exp, env));
} else {
throw new SyntaxError(`Invalid identifier ${symbol}`);
}
return ""; // Ignored
} else if (op === "set!") {
const [symbol, exp] = args;
if (typeof symbol === "string") {
env.find(symbol)?.set(symbol, evaluate(exp, env));
} else {
throw new Error(`Undefined symbol '${symbol}'`);
}
return ""; // Ignored
} else if (op === "lambda") {
const [params, body] = args;
if (Array.isArray(params)) {
return new Proc(params, body, env);
} else {
throw new Error(`Invalid params '${params}'`);
}
} else {
const proc = evaluate(op, env);
DEBUG && console.log({ args });
const vals = args.map((arg) => evaluate(arg, env));
DEBUG && console.log({ vals });
DEBUG && console.log({ proc });
if (proc instanceof Proc) {
DEBUG && console.log(proc.toString());
DEBUG && console.log(proc(vals));
x = proc.body;
// TODO
// @ts-ignore
env = new Env(proc.params, vals, proc.env);
} else if (proc instanceof Function) {
// TODO
// @ts-ignore
return proc(vals);
} else {
throw new Error(`Undefined symbol '${proc}'`);
}
}
}
}
const rl = readline.createInterface({ input, output });
function repl(rl: readline.Interface): void {
/* A prompt-read-eval-print loop */
rl.question("> ", (answer) => {
TIME && console.time("evaluate");
try {
const val = evaluate(parse(answer));
if (val !== "") console.log(val);
} catch (e) {
if (e instanceof Error) {
console.error(`Error: ${e.message}`);
}
}
TIME && console.timeEnd("evaluate");
repl(rl);
});
}
repl(rl);