protected com.yahoo.bullet.parsing.Expression visitBinaryExpression(InfixExpression node, Void context) throws ParsingException { BinaryExpression binaryExpression = new BinaryExpression(); binaryExpression.setLeft(process(node.getLeft())); binaryExpression.setRight(process(node.getRight())); switch (node.getOp()) { case ADD: binaryExpression.setOperation(Operation.ADD); break; case SUB: binaryExpression.setOperation(Operation.SUB); break; case MUL: binaryExpression.setOperation(Operation.MUL); break; case DIV: binaryExpression.setOperation(Operation.DIV); break; } return binaryExpression; }
private TypedObject calculateBinaryExpression(BinaryExpression expression, BulletRecord record) { TypedObject left = calculate(expression.getLeft(), record); TypedObject right = calculate(expression.getRight(), record); Type resultType = getResultType(left.getType(), right.getType()); TypedObject castedLeft = left.forceCast(resultType); TypedObject castedRight = right.forceCast(resultType); TypedObject result = null; switch (expression.getOperation()) { case ADD: result = add(resultType, castedLeft, castedRight); break; case SUB: result = sub(resultType, castedLeft, castedRight); break; case MUL: result = mul(resultType, castedLeft, castedRight); break; case DIV: result = div(resultType, castedLeft, castedRight); break; } Type newType = expression.getType(); if (newType != null) { result = result.forceCast(newType); } return result; }
protected com.yahoo.bullet.parsing.Expression visitCastExpression(CastExpression node, Void context) throws ParsingException { if (containsCastExpression(node.getExpression())) { throw new ParsingException("Casting of cast expressions is not supported"); } com.yahoo.bullet.parsing.Expression expression = process(node.getExpression()); if (expression instanceof BinaryExpression) { ((BinaryExpression) expression).setType(Type.valueOf(node.getCastType().toUpperCase())); } else { Value value = ((LeafExpression) expression).getValue(); ((LeafExpression) expression).setValue(new Value(value.getKind(), value.getValue(), Type.valueOf(node.getCastType().toUpperCase()))); } return expression; }