/
yaks.ts
169 lines (139 loc) · 3.61 KB
/
yaks.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
import { Context, Error, Expr, Node, Token, Type, Value } from './header.ts';
import { lex } from './lex.ts';
import { parse } from './parse.ts';
import { transform } from './transform.ts';
import { check } from './check.ts';
import { evaluate } from './eval.ts';
function max(x: number, y: number): number {
return x > y ? x : y;
}
function repeatString(ctx: Context, s: string, n: number) {
let out = Array(n + 1).join(s);
ctx.write(out);
}
// Returns the position of the newline, or -1
function findStartOfLine(s: string, blame_pos: number): [number, number] {
let pos = -1;
let line_num = 1;
while (true) {
let new_pos = s.indexOf('\n', pos + 1);
if (new_pos == -1) {
return [pos + 1, line_num];
}
if (new_pos > blame_pos) {
return [pos + 1, line_num];
}
pos = new_pos;
line_num += 1;
}
}
function printError(
ctx: Context,
prog: string,
tokens: Token[],
kind: string,
e: Error,
) {
let blame_tok = tokens[e.loc];
let blame_start = blame_tok.start;
// Extract the right line, and find The LAST newline before the start
let [line_start, line_num] = findStartOfLine(prog, blame_start);
let pos = prog.indexOf('\n', line_start);
let line_end = (pos === -1) ? prog.length : pos;
//log(`line_num ${line_num} begin ${line_start} end ${line_end}`);
// Show snippet
ctx.write(prog.slice(line_start, line_end));
ctx.write('\n');
// Point to token
let col = blame_start - line_start;
repeatString(ctx, ' ', col);
let n = max(blame_tok.len, 1); // EOF is zero in length
repeatString(ctx, '^', n);
ctx.write('\n');
// Columns are 1-based like lines
ctx.write(
`yaks:${line_num}:${col + 1}: ${kind} error: ${e.message}`,
);
ctx.write('\n\n');
}
// Control printing
export const TRACE_LEX = 1 << 1;
export const TRACE_PARSE = 1 << 2;
export const TRACE_TRANSFORM = 1 << 3;
export const TRACE_TYPE = 1 << 4;
export const TRACE_EVAL = 1 << 5;
export const DYNAMIC = 1 << 6;
export function interpret(
ctx: Context,
prog: string,
flags: number,
): Value | undefined {
let log = ctx.log;
let write = ctx.write;
log('');
log(` PROGRAM ${prog}`);
let tokens = lex(prog); // failures deferred to parsing
if (flags & TRACE_LEX) {
log(' LEX');
// TODO: print these more nicely -- they point to the whole program
log(tokens);
}
let tree: Node | null = null;
try {
tree = parse(tokens);
if (flags & TRACE_PARSE) {
log(' PARSE');
log(tree);
}
} catch (e) {
log(' PARSE ERROR');
printError(ctx, prog, tokens, 'Parse', e);
return;
}
if (tree === null) {
return;
}
let tr_errors: Error[] = [];
let expr = transform(tree, tr_errors);
if (tr_errors.length) {
log(' TRANSFORM ERRORS');
for (let e of tr_errors) {
printError(ctx, prog, tokens, 'Transform', e);
}
return;
}
if (flags & TRACE_TRANSFORM) {
log(' TRANSFORM');
log(expr);
}
if (!(flags & DYNAMIC)) {
let types: Map<Expr, Type> = new Map();
let type_errors: Error[] = [];
let typ = check(expr, types, type_errors);
if (type_errors.length) {
log(' TYPE ERRORS');
for (let e of type_errors) {
printError(ctx, prog, tokens, 'Type', e);
}
return;
}
if (flags & TRACE_TYPE) {
log(' TOP LEVEL TYPE');
log(` --> ${typ}`);
}
}
let val: Value | null = null;
try {
val = evaluate(expr);
} catch (e) {
log(' EVAL ERROR');
printError(ctx, prog, tokens, 'Runtime', e);
return;
}
if (flags & TRACE_EVAL) {
log(' EVAL');
write(' --> ');
log(val);
}
return val;
}