ifc-expressions
Version:
Parsing and evaluation of IFC expressions
50 lines (49 loc) • 2.07 kB
JavaScript
import { ExprEvalFunctionEvaluationErrorObj, ExprEvalStatus, isExprEvalSuccess, } from "../ExprEvalResult.js";
import { ExprKind } from "../ExprKind.js";
import { Expr0 } from "../Expr0.js";
import { IfcExpressionFunctions } from "./IfcExpressionFunctions.js";
import { isNullish } from "../../util/IfcExpressionUtils.js";
export class FunctionExpr extends Expr0 {
constructor(name, functionArguments) {
super(ExprKind.FUNCTION);
this.name = name;
this.arguments = functionArguments;
this.functionImplementation = IfcExpressionFunctions.getFunction(this.name);
if (isNullish(this.functionImplementation)) {
throw new Error(`No such function: ${this.name}`);
}
}
getChildren() {
return [...this.arguments];
}
doEvaluate(ctx, localCtx) {
const functionArguments = [];
for (let i = 0; i < this.arguments.length; i++) {
const evalResult = this.arguments[i].evaluate(ctx, localCtx);
functionArguments.push(evalResult);
}
const evaluationResult = this.applyFunction(this.name, functionArguments);
if (isExprEvalSuccess(evaluationResult)) {
return evaluationResult.result;
}
return evaluationResult;
}
applyFunction(name, functionArgs) {
const func = IfcExpressionFunctions.getFunction(name);
if (isNullish(func)) {
return new ExprEvalFunctionEvaluationErrorObj(ExprKind.FUNCTION, ExprEvalStatus.UNKNOWN_FUNCTION, `No such function ${IfcExpressionFunctions.normalizeName(name)}`, name, this.getTextSpan());
}
return func.evaluate(this, functionArgs);
}
buildExprString(builder) {
builder
.appendString(this.functionImplementation.getName())
.appendString("(")
.appendExprArray(this.arguments)
.appendString(")");
}
getType() {
return this.functionImplementation.getReturnType(this.arguments.map((f) => f.getType()));
}
}
//# sourceMappingURL=FunctionExpr.js.map