-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.ts
48 lines (42 loc) · 1.43 KB
/
eval.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
import type { RA } from '../utils/types.js';
import { handleInput } from '../frontEnd.js';
import { Expression } from './definitions/expression/index.js';
import { FunctionDeclaration } from './definitions/FunctionDeclaration.js';
import { AstNode } from './definitions/AstNode.js';
import { VariableDeclaration } from './definitions/statement/VariableDeclaration.js';
import { StatementList } from './definitions/statement/StatementList.js';
export type EvalContext = {
readonly output: (message: string) => void;
readonly input: ReturnType<typeof handleInput>;
readonly onReturnCalled: (value: EvalValue) => void;
};
export type EvalValue =
| FunctionDeclaration
| boolean
| number
| string
| undefined;
export class ReturnValue {
public constructor(public readonly value: EvalValue) {}
}
export type EvalReturnValue = EvalValue | ReturnValue;
export async function evalList(
context: EvalContext,
list: RA<Expression | StatementList>
): Promise<EvalReturnValue> {
let value: EvalReturnValue = undefined;
for (const child of list) {
value = await child.evaluate(context);
if (value instanceof ReturnValue) return value;
}
return value;
}
export function resetValues(statement: AstNode) {
if (statement instanceof VariableDeclaration)
statement.typeCheck({
reportError: () => {
throw new Error('reportError is not implemented');
},
});
else statement.children.forEach(resetValues);
}