UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

48 lines (47 loc) 2.03 kB
import { Func } from "../Func.js"; import { FuncArgAny } from "../arg/FuncArgAny.js"; import { ExprEvalFunctionEvaluationErrorObj, ExprEvalStatus, ExprEvalSuccessObj, } from "../../ExprEvalResult.js"; import { StringValue } from "../../../value/StringValue.js"; import { Type } from "../../../type/Types.js"; import { NumericValue } from "../../../value/NumericValue.js"; import { Decimal } from "decimal.js"; import { BooleanValue } from "../../../value/BooleanValue.js"; import { LogicalValue } from "../../../value/LogicalValue.js"; import { ReferenceValue } from "../../../value/ReferenceValue.js"; export class TONUMERIC extends Func { constructor() { super("TONUMERIC", [new FuncArgAny(true, "object")]); } calculateResult(callingExpr, evaluatedArguments) { const val = evaluatedArguments.get("object"); try { return new ExprEvalSuccessObj(NumericValue.of(this.convert(val))); } catch (e) { return new ExprEvalFunctionEvaluationErrorObj(callingExpr.getKind(), ExprEvalStatus.MATH_ERROR, `Cannot convert ${val.getValue()} to numeric: ${e.message}`, this.name, callingExpr.getTextSpan()); } } convert(value) { if (value instanceof BooleanValue) { return value.getValue() ? new Decimal(1) : new Decimal(0); } if (value instanceof StringValue || value instanceof ReferenceValue) { return new Decimal(value.getValue()); } if (value instanceof NumericValue) { return value.getValue(); } if (value instanceof LogicalValue) { return value.getValue() === "UNKNOWN" ? new Decimal(-1) : value.getValue() ? new Decimal(1) : new Decimal(0); } throw new Error(`numeric conversion not implemented for type ${value.constructor.name}`); } getReturnType(argumentTypes) { return Type.NUMERIC; } } //# sourceMappingURL=TONUMERIC.js.map