-
Notifications
You must be signed in to change notification settings - Fork 0
/
evalSyntax.js
669 lines (592 loc) · 17.7 KB
/
evalSyntax.js
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// Evaluator Class
// https://medium.com/dailyjs/compiler-in-javascript-using-antlr-9ec53fd2780f
import ScillaType, {
MapType,
ppType,
resolveTMapKey,
resolveTMapValue,
substTypeinType,
} from "./types.js";
import { ScillaExpr as SE, Pattern, Error } from "./syntax.js";
import SyntaxVisitor from "./syntaxVisitor.js";
import * as SL from "./literals.js";
import Builtins from "./builtins.js";
import _ from "lodash";
import * as DT from "./datatypes.js";
import {
addLineToLogOutput,
isError,
logOutput,
printLog,
setError,
} from "./general.js";
import util from "util";
const ST = new ScillaType();
const SV = new SyntaxVisitor();
const BI = new Builtins();
export default class Evaluator {
constructor(env) {
this.globalEnv = env;
this.ADTDict = new DT.DataTypeDict();
}
lookup(x, env) {
// console.log("lookup", env, x, env[x]);
return _.has(env, x)
? env[x]
: setError(new Error(`Error: environment lookup, didn't find ${x}`));
}
setEnv(k, v) {
// console.log("Binding " + k + " with " + v);
this.globalEnv[k] = v;
}
getEnv() {
return this.globalEnv;
}
wrap(value, env) {
// console.log("Wrapping value " + value);
return { value: value, env: env };
}
printError(funcname, msg) {
console.log("[ERROR]" + funcname + ": " + msg);
}
//Returns match context
matchClause(v, p) {
if (p instanceof Pattern.WildCard) {
return "Wildcard";
}
if (p instanceof Pattern.Binder) {
// const x = p.x;
// return { x: v };
return "Binder";
}
if (p instanceof Pattern.ConstructorPat) {
if (v.name === p.c) {
return "ConstructorPat";
}
}
// this.printError("matchClause", "Didn't match match clause.");
return undefined;
}
//Takes in a context that can become a type
//or if already turned into ST, then just return
antlrTypetoScillaType(ctx) {
if (ctx instanceof ST) {
return ctx;
} else {
}
}
substTypeInLit(tvar, type, lit) {
// Handles only Map and ADT literals
// Update the context - global
if (lit instanceof SL.Map) {
const kts = substTypeinType(tvar, type, lit.mtyp.t1);
const vts = substTypeinType(tvar, type, lit.mtyp.t2);
const newMap = new Map(
new MapType(resolveTMapKey(kts), resolveTMapValue(vts))
);
const ltsKeys = Object.keys(lit.kv);
ltsKeys.forEach((lKey) => {
const keySubst = this.substTypeInLit(tvar, type, lKey);
const valSubst = this.substTypeInLit(tvar, type, lit.kv[lKey]);
newMap[keySubst] = valSubst;
});
return newMap;
} else if (lit instanceof SL.ADTValue) {
const cloneLit = new SL.ADTValue(lit.name, null, null);
cloneLit.typl = lit.typl.map((typ) => {
return substTypeinType(tvar, type, typ);
});
cloneLit.ll = lit.ll.map((l) => this.substTypeInLit(tvar, type, l));
return cloneLit;
} else {
return lit;
}
}
//Since we will be returning updated context in this case,
//we must only use constructor fields instead of methods like identifier()
//in the rest of the code
//Return updated Expr
substTypeInExpr(tvar, tp, expr) {
if (expr instanceof SE.Var) {
//If Var, change nothing
return expr;
}
//If Lit, update the lit
if (expr instanceof SL.ScillaLiterals) {
expr = this.substTypeInLit(tvar, tp, expr);
return expr;
}
//If Lit, update the lit
// if (expr.a instanceof SL.ScillaLiterals) {
// this.substTypeInLit(tvar, tp, expr.a.l);
// return expr;
// }
// this.printError("substTypeInExpr", "Couldn't match Atomic");
// return expr;
if (expr instanceof SE.Fun) {
//Update type in fun type - Note: doesn't do anything yet
// if (printLog) {
// addLineToLogOutput(
// `before type substitution ${util.inspect(expr.ty, false, null, true)}`
// );
// }
expr.ty = substTypeinType(tvar, tp, expr.ty);
expr.e = this.substTypeInExpr(tvar, tp, expr.e);
// if (printLog) {
// addLineToLogOutput(
// `after type substitution ${util.inspect(expr.ty, false, null, true)}`
// );
// }
return expr;
}
if (expr instanceof SE.Match) {
//Update type in fun type - Note: doesn't do anything yet
expr.clauses = expr.clauses.map((expPm) => {
expPm.exp = this.substTypeInExpr(tvar, tp, expPm.exp);
return expPm;
});
return expr;
}
if (expr instanceof SE.TFun) {
if (expr.i === tvar) {
return expr;
} else {
expr.e = this.substTypeInExpr(tvar, tp, expr.e);
return expr;
}
}
if (expr instanceof SE.DataConstructor) {
expr.ts = expr.ts.map((ty) => substTypeinType(tvar, tp, ty));
return expr;
}
if (expr instanceof SE.App) {
return expr;
}
if (expr instanceof SE.Builtin) {
return expr;
}
if (expr instanceof SE.Message) {
return expr;
}
if (expr instanceof SE.Let) {
if (expr.ty !== undefined) {
substTypeinType(tvar, tp, expr.ty);
}
expr.lhs = this.substTypeInExpr(tvar, tp, expr.lhs); //lhs
expr.rhs = this.substTypeInExpr(tvar, tp, expr.rhs); //rhs
return expr;
}
if (expr instanceof SE.TApp) {
expr.targs = expr.targs.map((ty) => substTypeinType(tvar, tp, ty));
return expr;
}
}
evalCid(ctx, env) {
return ctx;
}
evalSid(ctx, env) {
return ctx; // does not account for SidCid
}
evalScid(ctx, env) {
return ctx;
}
evalCIDBystr(ctx, env) {
return ctx.BYSTR().getText()
? ctx.BYSTR().getText()
: this.printError("evalCIDBystr", "Couldn't match CIDBystr");
}
evalCID(ctx, env) {
return ctx.CID().getText()
? ctx.CID().getText()
: this.printError("evalCID", "Couldn't match CID");
}
evalID(ctx, env) {
return ctx;
}
evalTArg(ctx, env) {
return ctx;
}
evalWildcard(ctx, env) {
return ctx.getText();
}
evalBuiltinArgs(ctx, env) {
return ctx.map((arg) => this.evalSid(arg));
}
// evalExpPmClause(ctx, env) {
// const p = this.evalPattern(ctx.p, env);
// const e = this.evalExp(ctx.e, env);
// return new ExpPmClause(p, e);
// }
evalLet(ctx, env) {
const x = ctx.x;
const value = this.evalSimpleExp(ctx.lhs, env);
// return if evaluation of lhs produced an error
if (isError()) {
return;
}
env[x] = value;
return this.evalExp(ctx.rhs, env);
}
//Returns a closure
evalFun(ctx, env) {
const param = ctx.id;
const clo = (x) => {
const env_ = _.cloneDeep(env);
// if (printLog) {
// addLineToLogOutput(
// `Binding ${param} to ${x.constructor.name}: ${JSON.stringify(x)}`
// );
// }
env_[param] = x;
return this.evalExp(ctx.e, env_);
};
return new SL.Clo(clo);
}
//Note, this is written in a slightly convoluted way because the keyword
//`this` doesn't operate properly inside `reduce` since reduce is also
//a closure.
evalApp(ctx, env) {
const func_id = this.evalSid(ctx.f_var, env); // gets the identifier
const func = this.lookup(func_id, env);
const argsLit = ctx.args.map((arg) =>
this.lookup(this.evalSid(arg, env), env)
);
// check for an error at lookup
if (isError()) {
return;
}
let fullyAppliedRes = argsLit.reduce(function (res, arg) {
//Apply closure to argument
const partialRes = res.clo(arg);
// env = partialRes.env;
return partialRes;
}, func);
return fullyAppliedRes;
}
evalMessage(ctx, env) {
const messageKVPairs = ctx.es.map((pair) => {
if (pair?.i !== undefined && pair?.l !== undefined) {
return new SL.MsgEntry(
pair.i,
SL.literalType(pair.l),
this.evalLiteral(pair.l, env)
);
} else if (pair?.i !== undefined && pair?.v !== undefined) {
pair.v = this.lookup(pair.v, env);
return new SL.MsgEntry(pair.i, SL.literalType(pair.v), pair.v);
} else {
setError(new Error(`Error: ${ctx.constructor.name} ${pair}`));
return;
}
});
return new SL.Msg(messageKVPairs);
}
evalBuiltin(ctx, env) {
const id = this.evalID(ctx.b);
// console.log(this.lookup(ctx.xs[0], env));
const builtinFunc = BI.parseBuiltinIdentifier(id);
if (builtinFunc === undefined) {
setError(
new Error(`Error: ${id} is not recognised as a builtin function`)
);
return;
}
// const typeArgs =
// ctx.targs !== undefined
// ? ctx.targs.map((targ) =>
// targ.ts !== null ? ST.resolveTArg(targ) : targ.getText()
// )
// : undefined;
const builtinArgs = this.evalBuiltinArgs(ctx.xs).map((arg) => {
return this.lookup(arg, env);
});
// check if an error occurred during lookup
if (isError()) {
return;
}
const builtinFuncResult = builtinArgs.reduce(function (res, arg) {
//Apply closure to argument
const partialRes = res(arg);
return partialRes;
}, builtinFunc);
return builtinFuncResult;
}
evalDataConstructor(ctx, env) {
const c = this.evalScid(ctx.c, env);
const constr = this.ADTDict.lookUpConstr(c);
if (constr === undefined) {
setError(
new Error(`${ctx.constructor.name}: ADT constructor does not exist`)
);
return;
}
if (constr.arity !== ctx.args.length) {
setError(
new Error(`${ctx.constructor.name}: Constructor arity mismatch`)
);
return;
}
const targs = ctx.ts;
const args = ctx.args.map((arg) =>
this.lookup(this.evalSid(arg, env), env)
);
return new SL.ADTValue(c, targs, args);
}
evalPattern(value, ctx, env) {
if (ctx instanceof Pattern.WildCard) {
// no binding required
return env;
} else if (ctx instanceof Pattern.Binder) {
// bind value to variable to environment
env[ctx.x] = value;
return env;
} else if (ctx instanceof Pattern.ConstructorPat) {
// ConstructorPat case
const valueADTConstr = this.ADTDict.lookUpConstr(value.name);
const ctxADTConstr = this.ADTDict.lookUpConstr(ctx.c);
if (valueADTConstr.cname !== ctxADTConstr.cname) {
return undefined;
} else {
if (value.ll.length !== ctxADTConstr.arity) {
setError(
new Error(
`Error: ${ctx.constructor.name} pattern matching arity mismatch for ADT.`
)
);
return undefined;
}
for (let a = 0; a < ctxADTConstr.arity; a++) {
if (isError()) {
return undefined;
}
const nextEnv = this.evalArgPattern(value.ll[a], ctx.ps[a], env);
if (nextEnv === undefined) {
return undefined;
} else {
_.merge(env, nextEnv);
}
}
return env;
}
} else {
return undefined;
}
}
evalArgPattern(value, ctx, env) {
if (ctx instanceof Pattern.WildCard) {
// no binding required
return env;
} else if (ctx instanceof Pattern.Binder) {
env[ctx.x] = value;
return env;
} else if (ctx instanceof Pattern.ConstructorPat) {
if (ctx.ps !== []) {
return this.evalPattern(value, ctx, env);
}
if (value.name === ctx.c) {
return env;
} else {
return undefined;
}
} else {
// currently this case will not occur due to the syntax translation
// layer
return this.evalPattern(value, ctx, env);
}
}
checkMatchExp(value, ctx, env) {
/**
* A pattern-match must be exhaustive,
* i.e., every legal (type-safe) value of x must be matched by a pattern.
* Additionally, every pattern must be reachable,
* i.e., for each pattern there must be a legal (type-safe) value of x
* that matches that pattern, and which does not match
* any pattern preceding it.
*
* Current implementation does an under estimate of a well
* formed match expression by only checking the value provided during run
* time and not all possible values (which would require enumeration
* of the types of the literals of the ADTValue)
*/
const checkPattern = (value, ctx) => {
if (ctx instanceof Pattern.WildCard) {
// no binding required
return true;
} else if (ctx instanceof Pattern.Binder) {
// bind value to variable to environment
return true;
} else if (ctx instanceof Pattern.ConstructorPat) {
// ConstructorPat case
const valueADTConstr = this.ADTDict.lookUpConstr(value.name);
const ctxADTConstr = this.ADTDict.lookUpConstr(ctx.c);
if (valueADTConstr.cname !== ctxADTConstr.cname) {
return false;
} else {
if (value.ll.length !== ctxADTConstr.arity) {
setError(
new Error(
`Error: ${ctx.constructor.name} pattern matching arity mismatch for ADT.`
)
);
return false;
}
for (let a = 0; a < ctxADTConstr.arity; a++) {
if (isError()) {
return false;
}
const nextEnv = checkArgPattern(value.ll[a], ctx.ps[a], env);
if (nextEnv === false) {
return false;
} else {
}
}
return true;
}
} else {
return false;
}
};
const checkArgPattern = (value, ctx) => {
if (ctx instanceof Pattern.WildCard) {
// no binding required
return true;
} else if (ctx instanceof Pattern.Binder) {
return true;
} else if (ctx instanceof Pattern.ConstructorPat) {
if (ctx.ps !== []) {
return checkPattern(value, ctx);
}
if (value.name === ctx.c) {
return true;
} else {
return false;
}
} else {
// currently this case will not occur due to the syntax translation
// layer
return checkPattern(value, ctx);
}
};
const clauseReachability = ctx.clauses.map(() => 0); // initialize empty array
for (const [index, clause] of ctx.clauses.entries()) {
const isReachable = checkPattern(value, clause.pat);
if (isReachable) {
clauseReachability[index] = clauseReachability[index] + 1;
}
}
if (!clauseReachability.every((reached) => reached <= 1)) {
setError(new Error("Duplicate pattern found in pattern match"));
return undefined;
}
if (clauseReachability.every((reached) => reached === 0)) {
setError(new Error("Non-exhaustive pattern matching found"));
return undefined;
}
return true;
}
enumerateExpectedPatterns(value) {
/**
* This function should enumerate all expected patterns
* using the type of the value to enumerate
* the kind of types and hence constructors that must appear
* in the match expression for it to be well-formed.
*/
}
evalMatchExp(ctx, env) {
const value = this.lookup(ctx.x, env);
const wellFormedMatch = this.checkMatchExp(value, ctx, env);
if (!wellFormedMatch) {
return;
}
let nextEnv = undefined;
for (const clause of ctx.clauses) {
nextEnv = this.evalPattern(value, clause.pat, env);
if (nextEnv !== undefined) {
return this.evalExp(clause.exp, nextEnv);
}
}
// reaching this point would be due to an error
// no clauses matched
setError(new Error("Couldn't find a matching clause"));
if (isError()) {
return;
}
}
evalTFun(ctx, env) {
const tvar = ctx.i;
const clo = (tp) => {
const env_ = _.cloneDeep(env);
// if (printLog) {
// addLineToLogOutput(`Instantiating ${tvar} as ${ppType(tp)}`);
// }
const exp = this.substTypeInExpr(tvar, tp, ctx.e);
return this.evalSimpleExp(exp, env_);
};
return new SL.Clo(clo);
}
evalTApp(ctx, env) {
// console.log("At Tapp for " + this.evalSid(ctx.f, env));
const tfunc_id = this.evalSid(ctx.f, env);
const tfunc = this.lookup(tfunc_id, env);
// check if an error occurred during lookup
if (isError()) {
return;
}
const argsLit = ctx.targs.map((targ) => this.evalTArg(targ, env));
const fullyAppliedTRes = argsLit.reduce(function (tres, arg) {
//Apply closure to arg
const partialRes = tres.clo(arg);
return partialRes;
}, tfunc);
return fullyAppliedTRes;
}
evalLiteral(ctx, env) {
return ctx;
}
evalVar(ctx, env) {
const variable = this.lookup(this.evalSid(ctx.s), env);
// check if an error occurred during lookup
if (isError()) {
return;
}
return variable;
}
evalSimpleExp(ctx, env) {
if (!ctx) {
return;
}
return ctx instanceof SE.Let
? this.evalLet(ctx, env)
: ctx instanceof SL.ScillaLiterals
? this.evalLiteral(ctx, env)
: ctx instanceof SE.Var
? this.evalVar(ctx, env)
: ctx instanceof SE.Fun
? this.evalFun(ctx, env)
: ctx instanceof SE.App
? this.evalApp(ctx, env)
: ctx instanceof SE.Message
? this.evalMessage(ctx, env)
: ctx instanceof SE.Builtin
? this.evalBuiltin(ctx, env) //All builtins will be saved as closures
: ctx instanceof SE.DataConstructor
? this.evalDataConstructor(ctx, env)
: ctx instanceof SE.Match
? this.evalMatchExp(ctx, env)
: ctx instanceof SE.TFun
? this.evalTFun(ctx, env)
: ctx instanceof SE.TApp
? this.evalTApp(ctx, env)
: undefined;
}
evalExp(ctx, env) {
return this.evalSimpleExp(ctx, env);
}
evalChildren(ctx) {
if (!ctx) {
return;
}
const env = this.getEnv();
return ctx instanceof SE ? this.evalSimpleExp(ctx, env) : undefined;
}
}