Map<String, BinaryOperation> ops = new HashMap<String, BinaryOperation>(); ops.put("ADD", new BinaryOperation() { public int apply(int a, int b) { return a + b; } }); ops.put("MUL", new BinaryOperation() { public int apply(int a, int b) { return a * b; } }); // etc.
public boolean isGreaterPrecedence(BinaryOperation o) { return o.getPrecedence() > PTABLE[operation]; }
@Override public void setRight(ASTNode node) { super.setRight(node); }
bo = new BinaryOperation(op, tk, tk2, ctx); break; Object val = new BinaryOperation(oper.getOperator(), inv ? new LiteralNode(signNumber(tk2.getLiteralValue())) : tk2, rightNode) .getReducedValueAccelerated(null, null, null); bo = new BinaryOperation(tkOp.getOperator(), tk,new LiteralNode(p_inv ? signNumber(val) : val)); bo = new BinaryOperation(op, tk, tk2, ctx); bo.setRightMost(new BinaryOperation(op2, bo.getRightMost(), astLinkedList.nextNode(), ctx)); else if (bo.getOperation() != op2 && PTABLE[op] == PTABLE[op2]) { if (PTABLE[bo.getOperation()] == PTABLE[op2]) { bo = new BinaryOperation(op2, bo, astLinkedList.nextNode(), ctx); bo = new BinaryOperation(bo.getOperation(), bo.getLeft(), bo.getRight(), ctx); bo.setRight(new BinaryOperation(op2, bo.getRight(), tk2, ctx)); else if (PTABLE[bo.getOperation()] >= PTABLE[op2]) { bo = new BinaryOperation(op2, bo, astLinkedList.nextNode(), ctx); bo = new BinaryOperation(bo.getOperation(), bo.getLeft(), bo.getRight(), ctx); bo.setRight(new BinaryOperation(op2, bo.getRight(), tk2, ctx));
sbuf.append("OPERATION [" + getOperatorName(bo.getOperation()) + "] {").append(bo.getLeft().getName()) .append("} {").append(bo.getRight().getName()).append("}");
Class targetType = isAritmeticOperation(operation) ? egressType : left.getEgressType(); this.right = new LiteralNode(convert(right.getReducedValueAccelerated(null, null, null), targetType), pCtx); } else if ( !(areCompatible(left.getEgressType(), right.getEgressType()) || (( operation == Operator.EQUAL || operation == Operator.NEQUAL) && CompatibilityStrategy.areEqualityCompatible(left.getEgressType(), right.getEgressType()))) ) {
Object val = new BinaryOperation(oper.getOperator(), inv ? new LiteralNode(signNumber(tk2.getLiteralValue()), pCtx) : tk2, rightNode, pCtx) .getReducedValueAccelerated(null, null, null); bo = new BinaryOperation(tkOp.getOperator(), tk, new LiteralNode(p_inv ? signNumber(val) : val, pCtx), pCtx); bo = new BinaryOperation(op, tk, tk2, pCtx); bo.setRightMost(boOptimize(op2, bo.getRightMost(), astLinkedList.nextNode(), pCtx)); else if (bo.getOperation() != op2 && PTABLE[op] == PTABLE[op2]) { if (PTABLE[bo.getOperation()] == PTABLE[op2]) { bo = new BinaryOperation(bo.getOperation(), bo.getLeft(), bo.getRight(), pCtx); bo.setRight(new BinaryOperation(op2, bo.getRight(), tk2, pCtx)); else if (PTABLE[bo.getOperation()] >= PTABLE[op2]) { bo = new BinaryOperation(op2, bo, astLinkedList.nextNode(), pCtx); bo = new BinaryOperation(bo.getOperation(), bo.getLeft(), bo.getRight(), pCtx); bo.setRight(new BinaryOperation(op2, bo.getRight(), tk2, pCtx));
sbuf.append("OPERATION [" + getOperatorName(bo.getOperation()) + "] {").append(bo.getLeft().getName()) .append("} {").append(bo.getRight().getName()).append("}");
Class targetType = isAritmeticOperation(operation) ? egressType : left.getEgressType(); this.right = new LiteralNode(convert(right.getReducedValueAccelerated(null, null, null), targetType), pCtx); } else if ( !(areCompatible(left.getEgressType(), right.getEgressType()) || (( operation == Operator.EQUAL || operation == Operator.NEQUAL) && CompatibilityStrategy.areEqualityCompatible(left.getEgressType(), right.getEgressType()))) ) {
Object val = new BinaryOperation(oper.getOperator(), inv ? new LiteralNode(signNumber(tk2.getLiteralValue()), pCtx) : tk2, rightNode, pCtx) .getReducedValueAccelerated(null, null, null); bo = new BinaryOperation(tkOp.getOperator(), tk, new LiteralNode(p_inv ? signNumber(val) : val, pCtx), pCtx); bo = new BinaryOperation(op, tk, tk2, pCtx); bo.setRightMost(boOptimize(op2, bo.getRightMost(), astLinkedList.nextNode(), pCtx)); else if (bo.getOperation() != op2 && PTABLE[op] == PTABLE[op2]) { if (PTABLE[bo.getOperation()] == PTABLE[op2]) { bo = new BinaryOperation(bo.getOperation(), bo.getLeft(), bo.getRight(), pCtx); bo.setRight(new BinaryOperation(op2, bo.getRight(), tk2, pCtx)); else if (PTABLE[bo.getOperation()] >= PTABLE[op2]) { bo = new BinaryOperation(op2, bo, astLinkedList.nextNode(), pCtx); bo = new BinaryOperation(bo.getOperation(), bo.getLeft(), bo.getRight(), pCtx); bo.setRight(new BinaryOperation(op2, bo.getRight(), tk2, pCtx));
sbuf.append("OPERATION [" + getOperatorName(bo.getOperation()) + "] {").append(bo.getLeft().getName()) .append("} {").append(bo.getRight().getName()).append("}");
private static BinaryOperation boOptimize(int op, ASTNode tk, ASTNode tk2, ParserContext pCtx) { if (tk.getEgressType() == Integer.class && tk2.getEgressType() == Integer.class) { switch (op) { case Operator.ADD: return new IntAdd(tk, tk2, pCtx); case Operator.SUB: return new IntSub(tk, tk2, pCtx); case Operator.MULT: return new IntMult(tk, tk2, pCtx); case Operator.DIV: return new IntDiv(tk, tk2, pCtx); default: return new BinaryOperation(op, tk, tk2, pCtx); } } else { return new BinaryOperation(op, tk, tk2, pCtx); } }
@Override public void setRight(ASTNode node) { super.setRight(node); }
public boolean isGreaterPrecedence(BinaryOperation o) { return o.getPrecedence() > PTABLE[operation]; }
if (node instanceof BinaryOperation) { BinaryOperation binaryOperation = (BinaryOperation)node; condition.left = analyzeNode(binaryOperation.getLeft()); condition.operation = BooleanOperator.fromMvelOpCode(binaryOperation.getOperation()); condition.right = analyzeNode(binaryOperation.getRight()); } else if (node instanceof RegExMatch) { condition.left = analyzeNode(node);
private static BinaryOperation boOptimize(int op, ASTNode tk, ASTNode tk2, ParserContext pCtx) { if (tk.getEgressType() == Integer.class && tk2.getEgressType() == Integer.class) { switch (op) { case Operator.ADD: return new IntAdd(tk, tk2, pCtx); case Operator.SUB: return new IntSub(tk, tk2, pCtx); case Operator.MULT: return new IntMult(tk, tk2, pCtx); case Operator.DIV: return new IntDiv(tk, tk2, pCtx); default: return new BinaryOperation(op, tk, tk2, pCtx); } } else { return new BinaryOperation(op, tk, tk2, pCtx); } }
@Override public void setRight(ASTNode node) { super.setRight(node); }
public boolean isGreaterPrecedence(BinaryOperation o) { return o.getPrecedence() > PTABLE[operation]; }
return new AritmeticExpression(analyzeNode(op.getLeft()), AritmeticOperator.fromMvelOpCode(op.getOperation()), analyzeNode(op.getRight()));