-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlomp.ts
404 lines (339 loc) · 7.37 KB
/
lomp.ts
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
export class Tokenizer {
_program: string = '';
constructor({ program }) {
this._program = program;
}
space_out(c) {
this._program = this._program.replaceAll(c, ` ${c} `);
return this;
}
tokenize() {
let toks = [];
for (let i = 0; i < this._program.length; i++) {
let c = this._program[i];
if ('(){}[]:'.includes(c)) {
toks.push(c);
} else if (c === ';') {
while (this._program[i] !== '\n') {
i++;
}
} else if (' \n\t'.includes(c)) {
} else if (c === '"') {
let str = '';
i++;
while (this._program[i] !== '"') {
str += this._program[i];
i++;
}
toks.push(str);
} else {
let sym = '';
while (this._program[i] && /[^ \n\t()\{\}\[\]\:;"]/.test(this._program[i])) {
sym += this._program[i];
i++;
}
i--;
let float = Number.parseFloat(sym);
if (!isNaN(float)) {
toks.push(float);
} else if (sym === 'true' || sym === 'false') {
toks.push(JSON.parse(sym))
} else {
toks.push(OpSymbol.from_string(sym));
}
}
}
return toks;
}
static tokenize(program: string) {
let tt = new Tokenizer({ program });
return tt.tokenize();
}
}
export enum SymbolKind {
Standard,
Vau,
}
export class OpSymbol implements Expr {
_name: string = '';
_kind: SymbolKind = SymbolKind.Standard;
constructor({ name, kind }) {
this._name = name;
this._kind = kind;
}
static standard(name: string) {
return new OpSymbol({ name, kind: SymbolKind.Standard });
}
static vau(name: string) {
return new OpSymbol({ name, kind: SymbolKind.Vau });
}
static from_string(val: string) {
if (val[0] === '$') {
return OpSymbol.vau(val.slice(1));
} else {
return OpSymbol.standard(val);
}
}
key() {
return this._name;
}
evl(env) { return this; }
is_vau() {
return this._kind === SymbolKind.Vau;
}
is_standard() {
return this._kind === SymbolKind.Standard;
}
json() {
return this.key();
}
}
interface Expr {
evl(env: Env);
json();
}
export class SExp implements Expr {
_op: OpSymbol;
_args: Expr[];
constructor(base: Expr[]) {
if (!(base[0] instanceof OpSymbol)) {
throw new Error('invalid car ' + base[0]);
}
this._op = base[0] as OpSymbol;
this._args = base.slice(1);
}
// combiner
evl(env: Env) {
// sexp reduce
let receiver = this._args[0].evl(env);
let passed = this._args.slice(1).map(a => this._op.is_standard() ? a.evl(env) : a);
// receiver._env = env;
const key = this._op.key();
if (!(key in Object.getPrototypeOf(receiver))) {
throw new Error(`invalid method ${key} on receiver ${objectName(receiver)}`)
}
return receiver[this._op.key()](...passed);
}
json() {
return [this._op, ...this._args];
}
}
export class SMap extends Object implements Expr {
constructor(map = {}) {
super();
for (let [key, val] of Object.entries(map)) {
this[key] = val;
}
}
evl(env) {
return this;
}
json() {
return this;
}
}
export class Parser {
_toks: [string];
constructor({ toks }) {
this._toks = toks;
}
peek(): string {
return this._toks.length > 0 ? this._toks[0] : null;
}
chomp() {
let tok = this.peek();
this._toks.splice(0, 1);
return tok;
}
key_sep() {
let c = this.chomp();
if (c !== ':') {
throw new Error('expected :, found ' + c);
}
}
next_form(): Expr {
const head = this.chomp();
if (head === null) {
return null;
}
const n = +head;
if (!isNaN(n)) {
return n;
} else if (head === '(') {
let cur = this.peek();
let form: Expr[] = [];
while (cur !== ')') {
if (cur === null) {
throw new Error('Unclosed (');
}
form.push(this.next_form());
cur = this.peek();
}
this.chomp();
return new SExp(form);
} else if (head === '{') {
let cur = this.peek();
let form = new SMap();
while (cur !== '}') {
if (cur === null) {
throw new Error('Unclosed {');
}
let sym = this.next_form() as OpSymbol;
this.key_sep();
let val = this.next_form();
form[sym.key()] = val;
cur = this.peek();
}
this.chomp();
return form;
} else {
return head;
}
}
program(): Expr {
const prog: Expr[] = [OpSymbol.vau('progn')];
while (true) {
const form = this.next_form();
if (form === null) {
break;
} else {
prog.push(form);
}
}
if (prog.length === 2) {
return prog[1];
} else {
return new SExp(prog);
}
}
static _from_program(program: string) {
return new Parser({
toks: Tokenizer.tokenize(program)
})
}
}
function objectName(o) {
if ('name' in Object.getPrototypeOf(o)) {
return o.name();
} else {
return 'anon';
}
}
declare global {
export interface String {
tokenize();
parse(): Expr;
evl(env);
json();
}
}
String.prototype.tokenize = function() {
return (new Tokenizer({ program: this })).tokenize();
}
String.prototype.parse = function() {
return (new Parser({ toks: this.tokenize() })).program();
}
String.prototype.evl = function(env) {
return this;
}
String.prototype.json = function() {
return JSON.stringify(this);
}
declare global {
export interface Number {
name();
evl(env);
json();
'+'(n: number): number;
'-'(n: number): number;
'*'(n: number): number;
'/'(n: number): number;
'='(n: number): boolean;
'>'(n: number): boolean;
}
}
Number.prototype.name = function() {
return `number/${this}`;
}
Number.prototype.evl = function(env) {
return this;
}
Number.prototype.json = function() {
return this;
}
Number.prototype['+'] = function(n) {
return this + n;
}
Number.prototype['-'] = function(n) {
return this - n;
}
Number.prototype['*'] = function(n) {
return this * n;
}
Number.prototype['/'] = function(n) {
return this / n;
}
Number.prototype['='] = function(n) {
return this === n;
}
Number.prototype['>'] = function(n) {
return this > n;
}
declare global {
export interface Array<T> {
evl(env);
}
}
Array.prototype.evl = function(env: Env) {
}
declare global {
export interface Boolean {
evl(env);
name(): string;
'if'(then, elss);
}
}
Boolean.prototype.name = function() {
return this.toString();
}
Boolean.prototype['if'] = function(then, elss) {
return this ? then.evl() : elss.evl();
}
export class Role {
}
export class Env {
_generic_functions: {};
_vaus: {};
constructor({ genericFunctions = {}, vaus = {} } = {}) {
this._generic_functions = genericFunctions;
this._vaus = vaus;
}
lookup(sym: OpSymbol, args = []) {
if (sym.is_standard()) {
let gf = this._generic_functions[sym.key()];
// gf.roles.map(role => )
} else if (sym.is_vau()) {
return this._vaus[sym.key()];
}
}
static base_env() {
return new Env({
vaus: {
progn: {}
}
});
}
}
function objectify(o, env: Env) {
return new Proxy(o, {
get(target, p: string) {
if (p[0] === '_' || p[0] === '$') {
return env.lookup(OpSymbol.from_string(p));
} else {
return target[p];
}
}
})
}
export class SClass {
}