UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

54 lines (53 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FunctionExpr = void 0; const ExprEvalResult_js_1 = require("../ExprEvalResult.js"); const ExprKind_js_1 = require("../ExprKind.js"); const Expr0_js_1 = require("../Expr0.js"); const IfcExpressionFunctions_js_1 = require("./IfcExpressionFunctions.js"); const IfcExpressionUtils_js_1 = require("../../util/IfcExpressionUtils.js"); class FunctionExpr extends Expr0_js_1.Expr0 { constructor(name, functionArguments) { super(ExprKind_js_1.ExprKind.FUNCTION); this.name = name; this.arguments = functionArguments; this.functionImplementation = IfcExpressionFunctions_js_1.IfcExpressionFunctions.getFunction(this.name); if ((0, IfcExpressionUtils_js_1.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 ((0, ExprEvalResult_js_1.isExprEvalSuccess)(evaluationResult)) { return evaluationResult.result; } return evaluationResult; } applyFunction(name, functionArgs) { const func = IfcExpressionFunctions_js_1.IfcExpressionFunctions.getFunction(name); if ((0, IfcExpressionUtils_js_1.isNullish)(func)) { return new ExprEvalResult_js_1.ExprEvalFunctionEvaluationErrorObj(ExprKind_js_1.ExprKind.FUNCTION, ExprEvalResult_js_1.ExprEvalStatus.UNKNOWN_FUNCTION, `No such function ${IfcExpressionFunctions_js_1.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())); } } exports.FunctionExpr = FunctionExpr; //# sourceMappingURL=FunctionExpr.js.map