ifc-expressions
Version:
Parsing and evaluation of IFC expressions
70 lines (69 loc) • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CHOOSE = void 0;
const Func_js_1 = require("../Func.js");
const ExprEvalResult_js_1 = require("../../ExprEvalResult.js");
const FuncArg_js_1 = require("../FuncArg.js");
const ExprKind_js_1 = require("../../ExprKind.js");
const IfcExpressionUtils_js_1 = require("../../../util/IfcExpressionUtils.js");
const BooleanValue_js_1 = require("../../../value/BooleanValue.js");
const FuncArgMappings_js_1 = require("../arg/FuncArgMappings.js");
const WrongFunctionArgumentTypeException_js_1 = require("../../../error/WrongFunctionArgumentTypeException.js");
const Types_js_1 = require("../../../type/Types.js");
class CHOOSE extends Func_js_1.Func {
constructor() {
super("CHOOSE", [
new FuncArgMappings_js_1.FuncArgMappings(true, CHOOSE.ARG_NAME_CASES),
new FuncArg_js_1.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 (!Types_js_1.Type.BOOLEAN.overlapsWith(inType)) {
throw new WrongFunctionArgumentTypeException_js_1.WrongFunctionArgumentTypeException(this.name, this.formalArguments[0].name, Types_js_1.Type.BOOLEAN, inType, 0, argumentTypes[0][0]);
}
if (argumentTypes.length > 1 &&
!argumentTypes[1][1].overlapsWith(outType)) {
throw new WrongFunctionArgumentTypeException_js_1.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_js_1.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_js_1.FuncArgMappings.checkSingleMapping(callingExpr, outerArray, i);
if (!(0, IfcExpressionUtils_js_1.isNullish)(error)) {
return error;
}
const pair = outerArray[i].getValue();
if (!BooleanValue_js_1.BooleanValue.isBooleanValueType(pair[0])) {
return new ExprEvalResult_js_1.ExprEvalTypeErrorObj(ExprKind_js_1.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 ExprEvalResult_js_1.ExprEvalSuccessObj(pair[1]);
}
}
if ((0, IfcExpressionUtils_js_1.isNullish)(defaultValue)) {
return new ExprEvalResult_js_1.ExprEvalFunctionEvaluationErrorObj(ExprKind_js_1.ExprKind.FUNCTION, ExprEvalResult_js_1.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 ExprEvalResult_js_1.ExprEvalSuccessObj(defaultValue);
}
}
exports.CHOOSE = CHOOSE;
CHOOSE.ARG_NAME_CASES = "cases";
CHOOSE.ARG_NAME_DEFAULT_VALUE = "defaultValue";
//# sourceMappingURL=CHOOSE.js.map