@psdo/compiler 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,44 @@
1
+ /**
2
+ * The lexer program is the first step in the lanugage interpreting pipeline.
3
+ * It is responsible for taking the source text file and converting that into a
4
+ * stream of tokens. These tokens are any single blocks of information within the
5
+ * source code file, such as a single variable identifier or any operation symbols.
6
+ *
7
+ * The lexer does not determine any program correctness and is only responsible for
8
+ * converting the source into tokens, as such the only errors it throws are fundamental
9
+ * formating errors where the input block of text cannot be matched to any know tokens.
10
+ *
11
+ * This stream of tokens is then handed over to the parser later which program
12
+ * correctness is verified then.
13
+ */
14
+ /** Every kind of token `tokenise()` can produce. see `grammar.md` for more details */
15
+ export type TokenType = "NUMBER" | "IDENTIFIER" | "IF" | "THEN" | "ELSE" | "END" | "ENDIF" | "FOR" | "WHILE" | "DO" | "TO" | "DOWNTO" | "ENDFOR" | "ENDWHILE" | "FUNCTION" | "RETURN" | "REF" | "ENDFUNCTION" | "PLUS" | "MINUS" | "STAR" | "SLASH" | "PERCENT" | "EQ" | "LT" | "GT" | "LE" | "GE" | "EQEQ" | "NE" | "AND" | "OR" | "NOT" | "TRUE" | "FALSE" | "CARET" | "LPAREN" | "RPAREN" | "LBRACK" | "RBRACK" | "COMMA" | "DOT" | "CONSTRUCTOR" | "CLASS" | "PUBLIC" | "PRIVATE" | "NEW" | "ENDCONSTRUCTOR" | "ENDCLASS" | "THIS" | "NEWLINE" | "EOF";
16
+ /**
17
+ * A single token produced by `tokenise()`
18
+ *
19
+ * `value` is always the exact source text which was scanned to produce the
20
+ * token (so a `NUMBER` token's value is still a string, e.g. `"42"` not `42`).
21
+ * The parser is responsible for converting it to the type it actually needs.
22
+ *
23
+ * `line` is 1-indexed and is what every `"(line N)"` error message comes from.
24
+ *
25
+ */
26
+ export interface Token {
27
+ type: TokenType;
28
+ value: string;
29
+ line: number;
30
+ }
31
+ export declare const KEYWORDS: Map<string, TokenType>;
32
+ /**
33
+ * Scans source text into an array of tokens, ending with a `EOF` token so
34
+ * the parser knows when a programs token stream has finished.
35
+ *
36
+ * This only validates that every character can be turned into *some* token --
37
+ * it has no idea on the resulting sequences correctness. `parseProgram()` is
38
+ * what will reject incorrect program syntax.
39
+ *
40
+ * @param source input raw source code text file as a string
41
+ * @returns a stream of tokens
42
+ * @throws {Error} an any character that cannot start a valid token
43
+ */
44
+ export declare function tokenise(source: string): Token[];
package/dist/lexer.js ADDED
@@ -0,0 +1,161 @@
1
+ /**
2
+ * The lexer program is the first step in the lanugage interpreting pipeline.
3
+ * It is responsible for taking the source text file and converting that into a
4
+ * stream of tokens. These tokens are any single blocks of information within the
5
+ * source code file, such as a single variable identifier or any operation symbols.
6
+ *
7
+ * The lexer does not determine any program correctness and is only responsible for
8
+ * converting the source into tokens, as such the only errors it throws are fundamental
9
+ * formating errors where the input block of text cannot be matched to any know tokens.
10
+ *
11
+ * This stream of tokens is then handed over to the parser later which program
12
+ * correctness is verified then.
13
+ */
14
+ import { CompileError } from "./errors.js";
15
+ function scanNumber(source, start, line) {
16
+ let end = start;
17
+ while (end < source.length && /[0-9]/.test(source[end])) {
18
+ end++;
19
+ }
20
+ if (source[end] === ".") {
21
+ end++;
22
+ while (end < source.length && /[0-9]/.test(source[end])) {
23
+ end++;
24
+ }
25
+ }
26
+ const text = source.slice(start, end);
27
+ return { token: { type: "NUMBER", value: text, line }, nextPos: end };
28
+ }
29
+ // reserved words -- anything scanned by scanWord() that isn't here is an IDENTIFIER
30
+ export const KEYWORDS = new Map([
31
+ ["if", "IF"], ["then", "THEN"], ["else", "ELSE"], ["endif", "ENDIF"],
32
+ ["while", "WHILE"], ["for", "FOR"], ["do", "DO"], ["to", "TO"], ["downto", "DOWNTO"],
33
+ ["endwhile", "ENDWHILE"], ["endfor", "ENDFOR"], ["endfunction", "ENDFUNCTION"],
34
+ ["function", "FUNCTION"], ["return", "RETURN"], ["ref", "REF"],
35
+ ["and", "AND"], ["or", "OR"], ["this", "THIS"], ["not", "NOT"],
36
+ ["public", "PUBLIC"], ["private", "PRIVATE"], ["new", "NEW"],
37
+ ["class", "CLASS"], ["constructor", "CONSTRUCTOR"],
38
+ ["endclass", "ENDCLASS"], ["endconstructor", "ENDCONSTRUCTOR"],
39
+ ["true", "TRUE"], ["false", "FALSE"]
40
+ ]);
41
+ function scanWord(source, start, line) {
42
+ let end = start;
43
+ while (end < source.length && /[a-zA-Z0-9]/.test(source[end])) {
44
+ end++;
45
+ }
46
+ const value = source.slice(start, end);
47
+ const type = KEYWORDS.get(value) ?? "IDENTIFIER";
48
+ return { token: { type, value, line }, nextPos: end };
49
+ }
50
+ const COMPARISON = {
51
+ "=": "EQ", "<": "LT", ">": "GT",
52
+ "<=": "LE", ">=": "GE", "==": "EQEQ", "!=": "NE"
53
+ };
54
+ function scanComp(source, start, line) {
55
+ let end = start + 1;
56
+ if (source[end] === "=") {
57
+ end++;
58
+ }
59
+ const value = source.slice(start, end);
60
+ if (value === "!") {
61
+ return { error: `Unexpected char "!" - did you mean "!="?`, nextPos: end };
62
+ }
63
+ const type = COMPARISON[value];
64
+ return { token: { type, value, line }, nextPos: end };
65
+ }
66
+ const SYMBOLS = {
67
+ "+": "PLUS", "-": "MINUS", "*": "STAR", "/": "SLASH", "%": "PERCENT",
68
+ "(": "LPAREN", ")": "RPAREN", "[": "LBRACK", "]": "RBRACK",
69
+ ".": "DOT", ",": "COMMA", "^": "CARET"
70
+ };
71
+ /**
72
+ * Scans source text into an array of tokens, ending with a `EOF` token so
73
+ * the parser knows when a programs token stream has finished.
74
+ *
75
+ * This only validates that every character can be turned into *some* token --
76
+ * it has no idea on the resulting sequences correctness. `parseProgram()` is
77
+ * what will reject incorrect program syntax.
78
+ *
79
+ * @param source input raw source code text file as a string
80
+ * @returns a stream of tokens
81
+ * @throws {Error} an any character that cannot start a valid token
82
+ */
83
+ export function tokenise(source) {
84
+ source = source.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
85
+ let line = 1;
86
+ let pos = 0;
87
+ const tokens = [];
88
+ const errors = [];
89
+ let badRunEnd = -1;
90
+ while (pos < source.length) {
91
+ const char = source[pos];
92
+ if (char === " " || char === "\t") {
93
+ pos++;
94
+ continue;
95
+ }
96
+ if (char === "\n") {
97
+ tokens.push({ type: "NEWLINE", value: "\n", line });
98
+ line++;
99
+ pos++;
100
+ continue;
101
+ }
102
+ if (char === '/') {
103
+ const next = source[pos + 1];
104
+ if (next === '/') {
105
+ pos += 2;
106
+ while (source[pos] !== undefined && source[pos] !== '\n') {
107
+ pos++;
108
+ }
109
+ continue;
110
+ }
111
+ if (next === '*') {
112
+ pos += 2;
113
+ while (source[pos] !== undefined) {
114
+ if (source[pos] === '*' && source[pos + 1] === '/') {
115
+ pos += 2;
116
+ break;
117
+ }
118
+ if (source[pos] === '\n')
119
+ line++;
120
+ pos++;
121
+ }
122
+ continue;
123
+ }
124
+ }
125
+ if (/[0-9]/.test(char)) {
126
+ const { token, nextPos } = scanNumber(source, pos, line);
127
+ tokens.push(token);
128
+ pos = nextPos;
129
+ continue;
130
+ }
131
+ if (/[a-zA-Z]/.test(char)) {
132
+ const { token, nextPos } = scanWord(source, pos, line);
133
+ tokens.push(token);
134
+ pos = nextPos;
135
+ continue;
136
+ }
137
+ if (/[<>=!]/.test(char)) {
138
+ const scanned = scanComp(source, pos, line);
139
+ if ("error" in scanned)
140
+ errors.push({ message: scanned.error, line });
141
+ else
142
+ tokens.push(scanned.token);
143
+ pos = scanned.nextPos;
144
+ continue;
145
+ }
146
+ if (/[-+*/()\[\],.%^]/.test(char)) {
147
+ const type = SYMBOLS[char];
148
+ tokens.push({ type, value: char, line });
149
+ pos++;
150
+ continue;
151
+ }
152
+ if (pos !== badRunEnd)
153
+ errors.push({ message: `Unexpected character "${char}"`, line });
154
+ badRunEnd = pos + 1;
155
+ pos++;
156
+ }
157
+ if (errors.length > 0)
158
+ throw new CompileError(errors);
159
+ tokens.push({ type: "EOF", value: "", line });
160
+ return tokens;
161
+ }
@@ -0,0 +1,85 @@
1
+ import { RuntimeError } from "./errors.js";
2
+ export declare const RUNTIME: {
3
+ expectedNumber: (p: {
4
+ got: string;
5
+ }) => string;
6
+ expectedArray: (p: {
7
+ got: string;
8
+ }) => string;
9
+ indexOutOfBounds: (p: {
10
+ idx: number;
11
+ }) => string;
12
+ undefinedVariable: (p: {
13
+ name: string;
14
+ }) => string;
15
+ undefinedFunction: (p: {
16
+ name: string;
17
+ }) => string;
18
+ reservedName: (p: {
19
+ name: string;
20
+ }) => string;
21
+ wrongArgCount: (p: {
22
+ kind: "Function" | "Method" | "Constructor";
23
+ name: string;
24
+ expected: number;
25
+ got: number;
26
+ }) => string;
27
+ noReturnValue: (p: {
28
+ name: string;
29
+ }) => string;
30
+ missingReturn: (p: {
31
+ name: string;
32
+ }) => string;
33
+ byRefNeedsVariable: () => string;
34
+ notCallable: () => string;
35
+ stackDepth: (p: {
36
+ limit: number;
37
+ }) => string;
38
+ executionLimit: (p: {
39
+ limit: number;
40
+ }) => string;
41
+ unknownClass: (p: {
42
+ name: string;
43
+ }) => string;
44
+ thisOutsideClass: () => string;
45
+ cannotAssignThis: () => string;
46
+ fieldOfNonObject: (p: {
47
+ field: string;
48
+ got: string;
49
+ }) => string;
50
+ noSuchField: (p: {
51
+ cls: string;
52
+ name: string;
53
+ }) => string;
54
+ fieldIsMethod: (p: {
55
+ cls: string;
56
+ name: string;
57
+ }) => string;
58
+ privateField: (p: {
59
+ cls: string;
60
+ name: string;
61
+ }) => string;
62
+ unassignedField: (p: {
63
+ name: string;
64
+ }) => string;
65
+ methodOnNonObject: (p: {
66
+ name: string;
67
+ got: string;
68
+ }) => string;
69
+ noSuchMethod: (p: {
70
+ cls: string;
71
+ name: string;
72
+ }) => string;
73
+ methodIsField: (p: {
74
+ cls: string;
75
+ name: string;
76
+ }) => string;
77
+ privateMethod: (p: {
78
+ cls: string;
79
+ name: string;
80
+ }) => string;
81
+ };
82
+ export type RuntimeCode = keyof typeof RUNTIME;
83
+ export type RuntimeParams<C extends RuntimeCode> = Parameters<(typeof RUNTIME)[C]> extends [infer P] ? [params: P] : [];
84
+ export declare function makeError<C extends RuntimeCode>(code: C, ...params: RuntimeParams<C>): RuntimeError;
85
+ export declare function fail<C extends RuntimeCode>(code: C, ...params: RuntimeParams<C>): never;
@@ -0,0 +1,39 @@
1
+ import { RuntimeError } from "./errors.js";
2
+ export const RUNTIME = {
3
+ // === values === //
4
+ expectedNumber: (p) => `Expected a number but got ${p.got}`,
5
+ expectedArray: (p) => `Expected an array but got ${p.got}`,
6
+ indexOutOfBounds: (p) => `Index ${p.idx} out of bounds`,
7
+ // === variables, functions and calls === //
8
+ undefinedVariable: (p) => `Undefined variable "${p.name}"`,
9
+ undefinedFunction: (p) => `Function "${p.name}" is undefined`,
10
+ reservedName: (p) => `"${p.name}" is a reserved built-in and cannot be redefined`,
11
+ wrongArgCount: (p) => `${p.kind} ${p.name} expected ${p.expected} but got ${p.got}`,
12
+ noReturnValue: (p) => `Function "${p.name}" does not have a return value`,
13
+ missingReturn: (p) => `No return statement specified for function "${p.name}"`,
14
+ byRefNeedsVariable: () => `Only variables and array elements can be passed by reference`,
15
+ notCallable: () => `Only functions and methods can be called, not array elements`,
16
+ // === limits === //
17
+ stackDepth: (p) => `Stack depth limit of ${p.limit} reached, ensure there are no infinite loops`,
18
+ executionLimit: (p) => `Execution limit of ${p.limit} reached, ensure there are no infinite loops`,
19
+ // === classes and objects === //
20
+ unknownClass: (p) => `Class definition for ${p.name} does not exist`,
21
+ thisOutsideClass: () => `"this" can only be used inside a class`,
22
+ cannotAssignThis: () => `Can't assign to "this"`,
23
+ fieldOfNonObject: (p) => `Can't read field "${p.field}" of ${p.got}`,
24
+ noSuchField: (p) => `Class ${p.cls} has no field "${p.name}"`,
25
+ fieldIsMethod: (p) => `"${p.name}" is a method of ${p.cls}, not a field`,
26
+ privateField: (p) => `Field "${p.name}" of ${p.cls} is private, declare it as "public ${p.name}" to use it outside the class`,
27
+ unassignedField: (p) => `Field "${p.name}" has not been assigned`,
28
+ methodOnNonObject: (p) => `Can't call method "${p.name}" on ${p.got}`,
29
+ noSuchMethod: (p) => `Class ${p.cls} has no method "${p.name}"`,
30
+ methodIsField: (p) => `"${p.name}" is a field of ${p.cls}, not a method`,
31
+ privateMethod: (p) => `Method "${p.name}" of ${p.cls} is private, declare it as "public function ${p.name}()" to use it outside the class`,
32
+ };
33
+ export function makeError(code, ...params) {
34
+ const build = RUNTIME[code];
35
+ return new RuntimeError(code, build(...params));
36
+ }
37
+ export function fail(code, ...params) {
38
+ throw makeError(code, ...params);
39
+ }
@@ -0,0 +1 @@
1
+ export declare const PRELUDE: string;
@@ -0,0 +1,125 @@
1
+ // AUTO-GENERATED from src/prelude.psdo by scripts/build-prelude.ts. Do not edit.
2
+ const SWAP = `
3
+ function swap(ref a, ref b)
4
+ temp = a
5
+ a = b
6
+ b = temp
7
+ return
8
+ endfunction
9
+ `;
10
+ const LEN = `
11
+ function len(A)
12
+ return 0
13
+ endfunction
14
+ `;
15
+ const ABS = `
16
+ function abs(x)
17
+ if x >= 0 then
18
+ return x
19
+ else
20
+ return -x
21
+ endif
22
+ endfunction
23
+ `;
24
+ const FLOOR = `
25
+ function floor(x)
26
+ r = x % 1
27
+ if r < 0 then
28
+ r = r + 1
29
+ endif
30
+ return x - r
31
+ endfunction
32
+ `;
33
+ const CEIL = `
34
+ function ceil(x)
35
+ return -floor(-x)
36
+ endfunction
37
+ `;
38
+ const GCD = `
39
+ function gcd(a, b)
40
+ a = abs(a)
41
+ b = abs(b)
42
+ while b != 0 do
43
+ tmp = b
44
+ b = a % b
45
+ a = tmp
46
+ endwhile
47
+ return a
48
+ endfunction
49
+ `;
50
+ const LN = `
51
+ function ln(x)
52
+ LN2 = 0.6931471805599453
53
+ k = 0
54
+ while x > 1.5 do
55
+ x = x / 2
56
+ k = k + 1
57
+ endwhile
58
+
59
+ while x < 0.75 do
60
+ x = x * 2
61
+ k = k - 1
62
+ endwhile
63
+
64
+ z = (x - 1) / (x + 1)
65
+ term = z
66
+ total = 0
67
+
68
+ n = 1
69
+ while n <= 40 do
70
+ total = total + (term / n)
71
+ term = term * z * z
72
+ n = n + 2
73
+ endwhile
74
+
75
+ return 2 * total + k * LN2
76
+ endfunction
77
+ `;
78
+ const LOG = `
79
+ function log(x, n)
80
+ return ln(x) / ln(n)
81
+ endfunction
82
+ `;
83
+ const SIN = `
84
+ function sin(x)
85
+ PI = 3.141592653589793
86
+ x = x - 2 * PI * floor(x / (2 * PI))
87
+ if x > PI then
88
+ x = x - 2 * PI
89
+ endif
90
+
91
+ term = x
92
+ total = x
93
+
94
+ for n = 1 to 15 do
95
+ term = term * (-x * x / ((2 * n) * (2 * n + 1)))
96
+ total = total + term
97
+ endfor
98
+
99
+ return total
100
+ endfunction
101
+ `;
102
+ const COS = `
103
+ function cos(x)
104
+ PI = 3.141592653589793
105
+ return sin(x + PI / 2)
106
+ endfunction
107
+ `;
108
+ const TAN = `
109
+ function tan(x)
110
+ return sin(x) / cos(x)
111
+ endfunction
112
+ `;
113
+ export const PRELUDE = [
114
+ SWAP,
115
+ LEN,
116
+ ABS,
117
+ FLOOR,
118
+ CEIL,
119
+ GCD,
120
+ LN,
121
+ LOG,
122
+ SIN,
123
+ COS,
124
+ TAN,
125
+ ].join("\n\n");
@@ -0,0 +1,37 @@
1
+ import type { Diagnostic } from "./errors.js";
2
+ import type { RuntimeCode } from "./messages.js";
3
+ import type { Frame, SessionStats, Value } from "./types.js";
4
+ export type SessionError = {
5
+ phase: "compile";
6
+ diagnostics: Diagnostic[];
7
+ } | {
8
+ phase: "runtime";
9
+ code: RuntimeCode;
10
+ message: string;
11
+ line: number | undefined;
12
+ };
13
+ export type Outcome = {
14
+ kind: "ok";
15
+ frames: Frame[];
16
+ } | {
17
+ kind: "value";
18
+ value: Value;
19
+ frames: Frame[];
20
+ } | {
21
+ kind: "error";
22
+ error: SessionError;
23
+ };
24
+ export declare function isComplete(source: string): boolean;
25
+ export declare function formatValue(value: Value): string;
26
+ export declare class Session {
27
+ private interp;
28
+ private defs;
29
+ private known;
30
+ feed(source: string): Outcome;
31
+ private feedExpression;
32
+ private compileError;
33
+ private runtimeError;
34
+ variables(): Record<string, Value>;
35
+ stats(): SessionStats;
36
+ reset(): void;
37
+ }
@@ -0,0 +1,136 @@
1
+ import { tokenise } from "./lexer.js";
2
+ import { parseExpressionOnly, parseProgram } from "./ast.js";
3
+ import { checkChunk, checkExpression } from "./checker.js";
4
+ import { Interpreter, PRELUDE_DEFS } from "./interpreter.js";
5
+ import { CompileError, RuntimeError } from "./errors.js";
6
+ const OPENERS = new Set(["IF", "WHILE", "FOR", "FUNCTION", "CLASS", "CONSTRUCTOR"]);
7
+ const CLOSERS = new Set(["ENDIF", "ENDWHILE", "ENDFOR", "ENDFUNCTION", "ENDCLASS", "ENDCONSTRUCTOR"]);
8
+ export function isComplete(source) {
9
+ let tokens;
10
+ try {
11
+ tokens = tokenise(source);
12
+ }
13
+ catch {
14
+ return true;
15
+ }
16
+ let depth = 0;
17
+ for (const t of tokens) {
18
+ OPENERS.has(t.type) && depth++;
19
+ CLOSERS.has(t.type) && depth--;
20
+ }
21
+ return depth <= 0;
22
+ }
23
+ export function formatValue(value) {
24
+ const inside = new Set();
25
+ const show = (v) => {
26
+ if (typeof v === "number")
27
+ return String(Number(v.toPrecision(12)));
28
+ if (inside.has(v))
29
+ return "...";
30
+ inside.add(v);
31
+ try {
32
+ if (Array.isArray(v))
33
+ return "[" + v.map(e => show(e)).join(", ") + "]";
34
+ const fields = Object.entries(v.fields).map(f => f[0] + ": " + show(f[1]));
35
+ return `${v.cls} {${fields.length ? ` ${fields.join(", ")} ` : ""}}`;
36
+ }
37
+ finally {
38
+ inside.delete(v);
39
+ }
40
+ };
41
+ return show(value);
42
+ }
43
+ const isBlank = (tokens) => tokens.every(t => t.type === "EOF" || t.type === "NEWLINE");
44
+ const getNewDefs = (chunk) => chunk.filter(stmt => stmt.kind === "Class" || stmt.kind === "Function");
45
+ const STATEMENT_STARTS = new Set(["IDENTIFIER", "THIS", "IF", "WHILE", "FOR", "FUNCTION", "CLASS", "RETURN"]);
46
+ export class Session {
47
+ interp = new Interpreter();
48
+ defs = [];
49
+ known() {
50
+ return { defs: [...PRELUDE_DEFS, ...this.defs], vars: this.interp.variableNames() };
51
+ }
52
+ feed(source) {
53
+ let tokens;
54
+ try {
55
+ tokens = tokenise(source);
56
+ }
57
+ catch (e) {
58
+ return this.compileError(e);
59
+ }
60
+ // blank input
61
+ if (isBlank(tokens))
62
+ return { kind: "ok", frames: [] };
63
+ let expr;
64
+ let asExpression;
65
+ try {
66
+ expr = parseExpressionOnly(tokens);
67
+ }
68
+ catch (e) {
69
+ asExpression = e;
70
+ }
71
+ if (expr)
72
+ return this.feedExpression(expr);
73
+ let program;
74
+ try {
75
+ program = parseProgram(tokens);
76
+ }
77
+ catch (e) {
78
+ if (!(e instanceof CompileError))
79
+ throw e;
80
+ const first = tokens.filter(t => t.type !== "NEWLINE")[0].type;
81
+ return this.compileError(STATEMENT_STARTS.has(first) ? e : asExpression);
82
+ }
83
+ const problems = checkChunk(program, this.known());
84
+ if (problems.length > 0)
85
+ return { kind: "error", error: { phase: "compile", diagnostics: problems } };
86
+ const checkpoint = this.interp.checkpoint();
87
+ try {
88
+ this.interp.define(program);
89
+ const frames = this.interp.exec(program);
90
+ this.defs.push(...getNewDefs(program));
91
+ return { kind: "ok", frames };
92
+ }
93
+ catch (e) {
94
+ this.interp.restore(checkpoint);
95
+ return this.runtimeError(e);
96
+ }
97
+ }
98
+ feedExpression(expr) {
99
+ const problems = checkExpression(expr, this.known());
100
+ if (problems.length > 0)
101
+ return { kind: "error", error: { phase: "compile", diagnostics: problems } };
102
+ const checkpoint = this.interp.checkpoint();
103
+ try {
104
+ const { value, frames } = this.interp.evalTop(expr);
105
+ if (value === undefined)
106
+ return { kind: "ok", frames };
107
+ return { kind: "value", value, frames };
108
+ }
109
+ catch (e) {
110
+ this.interp.restore(checkpoint);
111
+ return this.runtimeError(e);
112
+ }
113
+ }
114
+ compileError(e) {
115
+ if (e instanceof CompileError) {
116
+ return { kind: "error", error: { phase: "compile", diagnostics: e.diagnostics } };
117
+ }
118
+ throw e;
119
+ }
120
+ runtimeError(e) {
121
+ if (e instanceof RuntimeError) {
122
+ return { kind: "error", error: { phase: "runtime", code: e.code, message: e.detail, line: e.line } };
123
+ }
124
+ throw e;
125
+ }
126
+ variables() {
127
+ return this.interp.variables();
128
+ }
129
+ stats() {
130
+ return this.interp.stats();
131
+ }
132
+ reset() {
133
+ this.interp = new Interpreter();
134
+ this.defs = [];
135
+ }
136
+ }