fix nested environment scopes + highlight runtime errors in editor
This commit is contained in:
parent
56467e5ea5
commit
a81fe1e884
9 changed files with 77 additions and 43 deletions
|
|
@ -1,5 +1,6 @@
|
|||
// Just a simple recursive interpreter for the action language
|
||||
|
||||
import { RuntimeError } from "./interpreter";
|
||||
import { Expression } from "./label_ast";
|
||||
import { Environment } from "./runtime_types";
|
||||
|
||||
|
|
@ -22,7 +23,8 @@ const BINARY_OPERATOR_MAP: Map<string, (a: any, b: any) => any> = new Map([
|
|||
["%", (a, b) => a % b],
|
||||
]);
|
||||
|
||||
export function evalExpr(expr: Expression, environment: Environment): any {
|
||||
// parameter uids: list of UIDs to append to any raised errors
|
||||
export function evalExpr(expr: Expression, environment: Environment, uids: string[] = []): any {
|
||||
if (expr.kind === "literal") {
|
||||
return expr.value;
|
||||
}
|
||||
|
|
@ -30,22 +32,22 @@ export function evalExpr(expr: Expression, environment: Environment): any {
|
|||
const found = environment.get(expr.variable);
|
||||
if (found === undefined) {
|
||||
console.log({environment});
|
||||
throw new Error(`variable '${expr.variable}' does not exist in environment`);
|
||||
throw new RuntimeError(`variable '${expr.variable}' does not exist in environment`, uids);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
else if (expr.kind === "unaryExpr") {
|
||||
const arg = evalExpr(expr.expr, environment);
|
||||
const arg = evalExpr(expr.expr, environment, uids);
|
||||
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);
|
||||
const lhs = evalExpr(expr.lhs, environment, uids);
|
||||
const rhs = evalExpr(expr.rhs, environment, uids);
|
||||
return BINARY_OPERATOR_MAP.get(expr.operator)!(lhs, rhs);
|
||||
}
|
||||
else if (expr.kind === "call") {
|
||||
const fn = evalExpr(expr.fn, environment);
|
||||
const param = evalExpr(expr.param, environment);
|
||||
const fn = evalExpr(expr.fn, environment, uids);
|
||||
const param = evalExpr(expr.param, environment, uids);
|
||||
return fn(param);
|
||||
}
|
||||
throw new Error("should never reach here");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue