ifc-expressions
Version:
Parsing and evaluation of IFC expressions
52 lines (51 loc) • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TONUMERIC = void 0;
const Func_js_1 = require("../Func.js");
const FuncArgAny_js_1 = require("../arg/FuncArgAny.js");
const ExprEvalResult_js_1 = require("../../ExprEvalResult.js");
const StringValue_js_1 = require("../../../value/StringValue.js");
const Types_js_1 = require("../../../type/Types.js");
const NumericValue_js_1 = require("../../../value/NumericValue.js");
const decimal_js_1 = require("decimal.js");
const BooleanValue_js_1 = require("../../../value/BooleanValue.js");
const LogicalValue_js_1 = require("../../../value/LogicalValue.js");
const ReferenceValue_js_1 = require("../../../value/ReferenceValue.js");
class TONUMERIC extends Func_js_1.Func {
constructor() {
super("TONUMERIC", [new FuncArgAny_js_1.FuncArgAny(true, "object")]);
}
calculateResult(callingExpr, evaluatedArguments) {
const val = evaluatedArguments.get("object");
try {
return new ExprEvalResult_js_1.ExprEvalSuccessObj(NumericValue_js_1.NumericValue.of(this.convert(val)));
}
catch (e) {
return new ExprEvalResult_js_1.ExprEvalFunctionEvaluationErrorObj(callingExpr.getKind(), ExprEvalResult_js_1.ExprEvalStatus.MATH_ERROR, `Cannot convert ${val.getValue()} to numeric: ${e.message}`, this.name, callingExpr.getTextSpan());
}
}
convert(value) {
if (value instanceof BooleanValue_js_1.BooleanValue) {
return value.getValue() ? new decimal_js_1.Decimal(1) : new decimal_js_1.Decimal(0);
}
if (value instanceof StringValue_js_1.StringValue || value instanceof ReferenceValue_js_1.ReferenceValue) {
return new decimal_js_1.Decimal(value.getValue());
}
if (value instanceof NumericValue_js_1.NumericValue) {
return value.getValue();
}
if (value instanceof LogicalValue_js_1.LogicalValue) {
return value.getValue() === "UNKNOWN"
? new decimal_js_1.Decimal(-1)
: value.getValue()
? new decimal_js_1.Decimal(1)
: new decimal_js_1.Decimal(0);
}
throw new Error(`numeric conversion not implemented for type ${value.constructor.name}`);
}
getReturnType(argumentTypes) {
return Types_js_1.Type.NUMERIC;
}
}
exports.TONUMERIC = TONUMERIC;
//# sourceMappingURL=TONUMERIC.js.map