raising events appears to work. nondeterminism currently ignored

This commit is contained in:
Joeri Exelmans 2025-10-09 13:17:01 +02:00
parent 3f2db4457f
commit 09a87d3025
8 changed files with 243 additions and 194 deletions

View file

@ -0,0 +1,46 @@
// Just a simple recursive interpreter for the action language
import { Expression } from "./label_ast";
import { Environment } from "./runtime_types";
const UNARY_OPERATOR_MAP: Map<string, (x: any) => any> = new Map([
["!", x => !x],
["-", x => -x as any],
]);
const BINARY_OPERATOR_MAP: Map<string, (a: any, b: any) => any> = new Map([
["+", (a, b) => a + b],
["-", (a, b) => a - b],
["*", (a, b) => a * b],
["/", (a, b) => a / b],
["&&", (a, b) => a && b],
["||", (a, b) => a || b],
["==", (a, b) => a == b],
["<=", (a, b) => a <= b],
[">=", (a, b) => a >= b],
["<", (a, b) => a < b],
[">", (a, b) => a > b],
]);
export function evalExpr(expr: Expression, environment: Environment): any {
if (expr.kind === "literal") {
return expr.value;
}
else if (expr.kind === "ref") {
const found = environment.get(expr.variable);
if (found === undefined) {
throw new Error(`variable '${expr.variable}' does not exist in environment`);
}
return found;
}
else if (expr.kind === "unaryExpr") {
const arg = evalExpr(expr.expr, environment);
return UNARY_OPERATOR_MAP.get(expr.operator)!(arg);
}
else if (expr.kind === "binaryExpr") {
const lhs = evalExpr(expr.lhs, environment);
const rhs = evalExpr(expr.rhs, environment);
return BINARY_OPERATOR_MAP.get(expr.operator)!(lhs, rhs);
}
throw new Error("should never reach here");
}