ifc-expressions
Version:
Parsing and evaluation of IFC expressions
57 lines (56 loc) • 2.38 kB
JavaScript
import { ExprBase } from "./ExprBase.js";
import { ExprEvalError2Obj, ExprEvalErrorUndefinedResult, ExprEvalStatus, isExprEvalError, isExprEvalSuccess, } from "./ExprEvalResult.js";
import { isNullish } from "../util/IfcExpressionUtils.js";
export class Expr2 extends ExprBase {
constructor(exprKind, left, right) {
super(exprKind);
this.left = left;
this.right = right;
}
getChildren() {
return [this.left, this.right];
}
onBeforeRecursion(ctx, localCtx) {
// override if subclass needs to do something before recursing
}
onAfterLeftRecursion(ctx, localCtx, leftResult) {
// override if subclass needs to do something with the left result, such as put it in the local context
}
evaluate(ctx, localCtx) {
this.onBeforeRecursion(ctx, localCtx);
const leftResult = this.left.evaluate(ctx, localCtx);
if (isNullish(leftResult)) {
return new ExprEvalErrorUndefinedResult(this.left.getKind(), this.left.getTextSpan());
}
if (isExprEvalError(leftResult)) {
return this.makeErrorForLeftSubExprError(ctx, localCtx, leftResult);
}
this.onAfterLeftRecursion(ctx, localCtx, leftResult.result);
const rightResult = this.right.evaluate(ctx, localCtx);
if (isNullish(rightResult)) {
return new ExprEvalErrorUndefinedResult(this.right.getKind(), this.getTextSpan());
}
if (isExprEvalSuccess(rightResult)) {
try {
const result = this.calculateResult(ctx, localCtx, leftResult.result, rightResult.result);
return this.wrapInResultIfNecessary(result);
}
catch (error) {
return this.handleError(error, leftResult, rightResult);
}
}
else {
return this.makeResultForRightSubExprError(ctx, localCtx, leftResult, rightResult);
}
}
makeResultForRightSubExprError(ctx, localCtx, leftResult, rightResult) {
return rightResult;
}
makeErrorForLeftSubExprError(ctx, localCtx, leftResult) {
return leftResult;
}
handleError(error, leftResult, rightResult) {
return new ExprEvalError2Obj(this.getKind(), leftResult, rightResult, ExprEvalStatus.ERROR, error, this.getTextSpan());
}
}
//# sourceMappingURL=Expr2.js.map