UNPKG

ifc-expressions

Version:

Parsing and evaluation of IFC expressions

34 lines (33 loc) 1.52 kB
import { Expr2 } from "../Expr2.js"; import { NumericValue } from "../../value/NumericValue.js"; import { ExprKind } from "../ExprKind.js"; import { StringValue } from "../../value/StringValue.js"; import { ExprEvalTypeErrorObj } from "../ExprEvalResult.js"; import { Type, Types } from "../../type/Types.js"; export class PlusOrConcatExpr extends Expr2 { constructor(left, right) { super(ExprKind.NUM_PLUS, left, right); } calculateResult(ctx, localCtx, leftResult, rightResult) { if (leftResult instanceof NumericValue && rightResult instanceof NumericValue) { return NumericValue.of(leftResult.getValue().plus(rightResult.getValue())); } else if (leftResult instanceof StringValue && rightResult instanceof StringValue) { return StringValue.of(leftResult.getValue() + rightResult.getValue()); } return new ExprEvalTypeErrorObj(ExprKind.FUNCTION, `Operator '+' requires the operands to be either both of type numeric or both of type string, but they are '${leftResult .getType() .getName()}' (left operand) and '${rightResult .getType() .getName()}' (right operand)`, [leftResult, rightResult], this.getTextSpan()); } buildExprString(builder) { builder.appendExpr(this.left).appendString(" + ").appendExpr(this.right); } getType() { return Types.or(Type.NUMERIC, Type.STRING); } } //# sourceMappingURL=PlusOrConcatExpr.js.map