UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

66 lines (65 loc) 3.35 kB
import { Func } from "../Func.js"; import { ExprEvalFunctionEvaluationErrorObj, ExprEvalStatus, ExprEvalSuccessObj, ExprEvalTypeErrorObj, } from "../../ExprEvalResult.js"; import { FuncArg } from "../FuncArg.js"; import { ExprKind } from "../../ExprKind.js"; import { isNullish } from "../../../util/IfcExpressionUtils.js"; import { BooleanValue } from "../../../value/BooleanValue.js"; import { FuncArgMappings } from "../arg/FuncArgMappings.js"; import { WrongFunctionArgumentTypeException } from "../../../error/WrongFunctionArgumentTypeException.js"; import { Type, Types } from "../../../type/Types.js"; export class CHOOSE extends Func { constructor() { super("CHOOSE", [ new FuncArgMappings(true, CHOOSE.ARG_NAME_CASES), new FuncArg(false, CHOOSE.ARG_NAME_DEFAULT_VALUE), ]); } checkArgumentsAndGetReturnType(argumentTypes, ctx) { const returnType = super.checkArgumentsAndGetReturnType(argumentTypes, ctx); const tupleTypes = argumentTypes[0][1] .toArrayType() .getElementType().getTypes()[0].getTypes(); const inType = tupleTypes[0]; const outType = tupleTypes[1]; if (!Type.BOOLEAN.overlapsWith(inType)) { throw new WrongFunctionArgumentTypeException(this.name, this.formalArguments[0].name, Type.BOOLEAN, inType, 0, argumentTypes[0][0]); } if (argumentTypes.length > 1 && !argumentTypes[1][1].overlapsWith(outType)) { throw new WrongFunctionArgumentTypeException(this.name, this.formalArguments[1].name, inType, argumentTypes[1][1], 1, argumentTypes[1][0]); } return returnType; } getReturnType(argumentTypes) { const returnTypes = argumentTypes[0] .getTypes() .map((t) => t.getTypes()[1]); return Types.or(...returnTypes); } calculateResult(callingExpr, evaluatedArguments) { const mappings = evaluatedArguments.get(CHOOSE.ARG_NAME_CASES); const defaultValue = evaluatedArguments.get(CHOOSE.ARG_NAME_DEFAULT_VALUE); const outerArray = mappings.getValue(); const numMappings = outerArray.length; for (let i = 0; i < numMappings; i++) { const error = FuncArgMappings.checkSingleMapping(callingExpr, outerArray, i); if (!isNullish(error)) { return error; } const pair = outerArray[i].getValue(); if (!BooleanValue.isBooleanValueType(pair[0])) { return new ExprEvalTypeErrorObj(ExprKind.FUNCTION, `First array element at 0-based position ${i} in the mappings does not evaluate to a boolean`, pair[0], callingExpr.getTextSpan()); } if (pair[0].getValue() === true) { return new ExprEvalSuccessObj(pair[1]); } } if (isNullish(defaultValue)) { return new ExprEvalFunctionEvaluationErrorObj(ExprKind.FUNCTION, ExprEvalStatus.UNDEFINED_RESULT, "Input value not found in left column. No default return value specified, therefore the result of MAP() is undefined ", this.name, callingExpr.getTextSpan()); } return new ExprEvalSuccessObj(defaultValue); } } CHOOSE.ARG_NAME_CASES = "cases"; CHOOSE.ARG_NAME_DEFAULT_VALUE = "defaultValue"; //# sourceMappingURL=CHOOSE.js.map