-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
165 lines (154 loc) · 4.33 KB
/
utils.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
import { typeCheck } from "./typechecker.ts";
import { evaluate } from "./interpreter.ts";
import { MyPLError } from "./exceptions.ts";
export type DiscriminateUnion<T, K extends keyof T, V extends T[K]> = T extends
Record<K, V> ? T : never;
export function assertNever(x: never): never {
throw new Error();
}
export function prettyPrint(obj: any) {
function removeInfo(arg: any): any {
if (!arg) {
return arg;
}
if (Array.isArray(arg)) {
return arg.map((el) => removeInfo(el));
}
if ("info" in arg) {
const { info, ...result } = arg;
if (Object.keys(result).length === 1) {
return removeInfo(result[Object.keys(result)[0]]);
}
return removeInfo(result);
}
const result: any = {};
for (const [k, v] of Object.entries(arg)) {
if (typeof v === "object") {
result[k] = removeInfo(v);
} else {
result[k] = v;
}
}
return result;
}
return JSON.stringify(removeInfo(obj), null, 2);
}
export function omit<T>(obj: Record<string, T>, keys: string[]) {
const result: Record<string, T> = {};
for (const [k, v] of Object.entries(obj)) {
if (!keys.includes(k)) {
result[k] = v;
}
}
return result;
}
export const genUniqTypeVar = Symbol;
export const genUniqRowVar = Symbol;
//// For debugging...easier to console log than symbols
// let i = 0;
// export const genUniqTypeVar = () => {
// i++;
// return `?X_${i}` as any;
// };
// export const genUniqRowVar = () => {
// i++;
// return `?p_${i}` as any;
// };
export function printValue(v: ReturnType<typeof evaluate>): string {
switch (v.tag) {
case "TmBool":
return v.val ? `#t` : `#f`;
case "TmInt":
return `${v.val}`;
case "TmStr":
return `"${v.val}"`;
case "TmVoid":
return `void`;
case "TmEmpty":
return `empty`;
case "TmCons":
return `(cons ${printValue(v.car)} ${printValue(v.cdr)})`;
case "TmRecord":
return `{${
Object.keys(v.fields).sort()
.map((k) => `${k}:${printValue(v.fields[k])}`)
.join(" ")
}}`;
case "TmLocation":
return `(ref ${printValue(v.val)})`;
case "TmClosure":
return `[CLOSURE]`; // TODO ?
case "TmStdlibFun":
return `[STD_LIB]`; // TODO ?
default:
return assertNever(v);
}
}
export function printType(t: ReturnType<typeof typeCheck>) {
// produces stream of identifiers like
// 'a 'b 'c ... 'z 'aa 'ab 'ac ... 'az 'ba 'bb 'bc ... 'bz 'ca 'cb 'cc ...
const nextFreeGenerator = () => {
let i = 0;
return () => {
let n = i;
let result = "'";
while (n >= 26) {
const multiple = Math.floor(n / 26);
result += String.fromCharCode(97 + multiple - 1);
n -= (26 * multiple);
}
result += String.fromCharCode(97 + (n % 26));
i += 1;
return result;
};
};
const nextFree = nextFreeGenerator();
const symbolToPrettyType: Map<symbol, string> = new Map();
function helper(t: ReturnType<typeof typeCheck>): string {
switch (t.tag) {
case "TyBool":
return "bool";
case "TyInt":
return "int";
case "TyStr":
return "str";
case "TyVoid":
return "void";
case "TyList":
return `(Listof ${helper(t.elementType.type)})`;
case "TyRecord":
return `{${
Object.keys(t.rowExp.fieldTypes).sort().map((k) =>
`${k}:${helper(t.rowExp.fieldTypes[k].type)}`
).join(" ")
}}`;
case "TyArrow":
return `(-> ${(t.paramTypes.map((p) => helper(p.type))).join(" ")} ${
helper(t.returnType.type)
})`;
case "TyRef":
return `(ref ${helper(t.valType.type)})`;
case "TyId": {
if (!(symbolToPrettyType.has(t.name))) {
symbolToPrettyType.set(t.name, nextFree());
}
return symbolToPrettyType.get(t.name)!;
}
default:
return assertNever(t);
}
}
return helper(t);
}
export function printError(program: string, e: MyPLError) {
let errMsg = e.name + "\n" + e.message;
if (e.sourceInfo) {
errMsg += "\n" +
program.substring(e.sourceInfo.startIdx - 3, e.sourceInfo.endIdx + 3) +
"\n" +
" ".repeat(Math.min(3, e.sourceInfo.startIdx)) +
("^".repeat(e.sourceInfo.endIdx - e.sourceInfo.startIdx)) +
" ".repeat(3);
}
return errMsg;
}