ifc-expressions
Version:
Parsing and evaluation of IFC expressions
69 lines (68 loc) • 3.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAP = 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 FuncArgMappings_js_1 = require("../arg/FuncArgMappings.js");
const Types_js_1 = require("../../../type/Types.js");
const WrongFunctionArgumentTypeException_js_1 = require("../../../error/WrongFunctionArgumentTypeException.js");
class MAP extends Func_js_1.Func {
constructor() {
super("MAP", [
new FuncArg_js_1.FuncArg(true, MAP.ARG_NAME_MAPPING_INPUT),
new FuncArgMappings_js_1.FuncArgMappings(true, MAP.ARG_NAME_MAPPINGS),
new FuncArg_js_1.FuncArg(false, MAP.ARG_NAME_DEFAULT_VALUE),
]);
}
getReturnType(argumentTypes) {
const returnTypes = argumentTypes[1]
.getTypes()
.map((t) => t.getTypes()[1]);
return Types_js_1.Types.or(...returnTypes);
}
checkArgumentsAndGetReturnType(argumentTypes, ctx) {
const returnType = super.checkArgumentsAndGetReturnType(argumentTypes, ctx);
const tupleTypes = argumentTypes[1][1]
.toArrayType()
.getElementType().getTypes()[0].getTypes();
const inType = tupleTypes[0];
const outType = tupleTypes[1];
if (!argumentTypes[0][1].overlapsWith(inType)) {
throw new WrongFunctionArgumentTypeException_js_1.WrongFunctionArgumentTypeException(this.name, this.formalArguments[0].name, inType, argumentTypes[0][1], 0, argumentTypes[0][0]);
}
if (argumentTypes.length > 2 &&
!argumentTypes[2][1].overlapsWith(outType)) {
throw new WrongFunctionArgumentTypeException_js_1.WrongFunctionArgumentTypeException(this.name, this.formalArguments[2].name, inType, argumentTypes[2][1], 2, argumentTypes[2][0]);
}
return returnType;
}
calculateResult(callingExpr, evaluatedArguments) {
const mappings = evaluatedArguments.get(MAP.ARG_NAME_MAPPINGS);
const input = evaluatedArguments.get(MAP.ARG_NAME_MAPPING_INPUT);
const defaultValue = evaluatedArguments.get(MAP.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 (pair[0].equals(input)) {
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.MAP = MAP;
MAP.ARG_NAME_MAPPINGS = "mappings";
MAP.ARG_NAME_MAPPING_INPUT = "input";
MAP.ARG_NAME_DEFAULT_VALUE = "defaultValue";
//# sourceMappingURL=MAP.js.map