public void testLinenoMultilineEq() { AstRoot root = parse( "\nif\n" + " (((a == \n" + " 3) && \n" + " (b == 2)) || \n" + " (c == 1)) {\n" + "}\n"); IfStatement ifStmt = (IfStatement) root.getFirstChild(); InfixExpression orTest = (InfixExpression) ifStmt.getCondition(); ParenthesizedExpression cTestParen = (ParenthesizedExpression) orTest.getRight(); InfixExpression cTest = (InfixExpression) cTestParen.getExpression(); ParenthesizedExpression andTestParen = (ParenthesizedExpression) orTest.getLeft(); InfixExpression andTest = (InfixExpression) andTestParen.getExpression(); AstNode aTest = andTest.getLeft(); AstNode bTest = andTest.getRight(); assertEquals(1, ifStmt.getLineno()); assertEquals(2, orTest.getLineno()); assertEquals(2, andTest.getLineno()); assertEquals(2, aTest.getLineno()); assertEquals(4, bTest.getLineno()); assertEquals(5, cTest.getLineno()); assertEquals(5, cTestParen.getLineno()); assertEquals(2, andTestParen.getLineno()); }
public void testLinenoEq() { AstRoot root = parse("\na\n" + "==\n" + "b\n"); ExpressionStatement expr = (ExpressionStatement) root.getFirstChild(); InfixExpression compare = (InfixExpression) expr.getExpression(); AstNode lhs = compare.getLeft(); AstNode rhs = compare.getRight(); assertEquals(1, lhs.getLineno()); assertEquals(1, compare.getLineno()); assertEquals(3, rhs.getLineno()); }
public static void main(String[] args) { String[] exp = {"(", "3", "+", "2", ")", "*", "value1", "/", "value2"}; String[] postfix = InfixExpression.convertToPostfix(exp); String[] variables = {"value1", "value2", "value3"}; String[] values = {"1.09", "2", "3.5"}; System.out.print("Expression: "); for (int i = 0; i < exp.length; i++) System.out.print(exp[i]); System.out.println("\nPostfix evaluation: " + evalPostfix(postfix, variables, values)); String exp1[] = {"A", "+", "(", "B", "/", "C", ")", "*", "(", "D", "/", "E", ")"}; String[] postfixEXP = InfixExpression.convertToPostfix(exp1); String[] prefix = InfixExpression.convertToPrefix(exp1); for (int i = 0; i < postfixEXP.length; i++) System.out.print(postfixEXP[i]); System.out.print("\n"); for (int i = 0; i < prefix.length; i++) System.out.print(prefix[i]); }
/* * @see ASTVisitor#visit(InfixExpression) */ @Override public boolean visit(InfixExpression node) { node.getLeftOperand().accept(this); this.fBuffer.append(' '); // for cases like x= i - -1; or x= i++ + ++i; this.fBuffer.append(node.getOperator().toString()); this.fBuffer.append(' '); node.getRightOperand().accept(this); final List<Expression> extendedOperands = node.extendedOperands(); if (extendedOperands.size() != 0) { this.fBuffer.append(' '); for (Iterator<Expression> it = extendedOperands.iterator(); it.hasNext(); ) { this.fBuffer.append(node.getOperator().toString()).append(' '); Expression e = it.next(); e.accept(this); } } return false; }
/* (non-Javadoc) * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.InfixExpression) */ public boolean visit(InfixExpression node) { PTInfixExpression inf = InstantiationFactory.eINSTANCE.createPTInfixExpression(); inf.setLeftOperand(perform(node.getLeftOperand())); PTInfixOperator inoper = getParseInfix(node.getOperator()); if (inoper == null) { // It is not one we can handle. throw new InvalidExpressionException( MessageFormat.format( WorkbenchUtilityMessages.ParseTreeCreationFromAST_OperatorTooComplicatedToHandle_EXC_, new Object[] {node.getOperator().toString()})); } inf.setOperator(inoper); inf.setRightOperand(perform(node.getRightOperand())); List eops = inf.getExtendedOperands(); List neops = node.extendedOperands(); int nsize = neops.size(); for (int i = 0; i < nsize; i++) { eops.add(perform((Expression) neops.get(i))); } expression = inf; return false; }
public void testLinenoComma() { AstRoot root = parse("\na,\n" + " b,\n" + " c;\n"); ExpressionStatement stmt = (ExpressionStatement) root.getFirstChild(); InfixExpression comma1 = (InfixExpression) stmt.getExpression(); InfixExpression comma2 = (InfixExpression) comma1.getLeft(); AstNode cRef = comma1.getRight(); AstNode aRef = comma2.getLeft(); AstNode bRef = comma2.getRight(); assertEquals(1, comma1.getLineno()); assertEquals(1, comma2.getLineno()); assertEquals(1, aRef.getLineno()); assertEquals(2, bRef.getLineno()); assertEquals(3, cRef.getLineno()); }
public void testNestedOr() { AstNode root = parse("\nif (a && \n" + " b() || \n" + " /* comment */\n" + " c) {\n" + "}\n"); IfStatement ifStmt = (IfStatement) root.getFirstChild(); InfixExpression orClause = (InfixExpression) ifStmt.getCondition(); InfixExpression andClause = (InfixExpression) orClause.getLeft(); AstNode cName = orClause.getRight(); assertEquals(1, ifStmt.getLineno()); assertEquals(1, orClause.getLineno()); assertEquals(1, andClause.getLineno()); assertEquals(4, cName.getLineno()); }
public boolean visit(InfixExpression s) throws Exception { Map<String, String> parameters = createInitialParameters(s); parameters.put("operator", s.getOperator()); xmlWriter.startTag("InfixExpression", parameters); return true; }
public void testLinenoMultilineBitTest() { AstRoot root = parse( "\nif (\n" + " ((a \n" + " | 3 \n" + " ) == \n" + " (b \n" + " & 2)) && \n" + " ((a \n" + " ^ 0xffff) \n" + " != \n" + " (c \n" + " << 1))) {\n" + "}\n"); IfStatement ifStmt = (IfStatement) root.getFirstChild(); InfixExpression andTest = (InfixExpression) ifStmt.getCondition(); ParenthesizedExpression bigLHSExpr = (ParenthesizedExpression) andTest.getLeft(); ParenthesizedExpression bigRHSExpr = (ParenthesizedExpression) andTest.getRight(); InfixExpression eqTest = (InfixExpression) bigLHSExpr.getExpression(); InfixExpression notEqTest = (InfixExpression) bigRHSExpr.getExpression(); ParenthesizedExpression test1Expr = (ParenthesizedExpression) eqTest.getLeft(); ParenthesizedExpression test2Expr = (ParenthesizedExpression) eqTest.getRight(); ParenthesizedExpression test3Expr = (ParenthesizedExpression) notEqTest.getLeft(); ParenthesizedExpression test4Expr = (ParenthesizedExpression) notEqTest.getRight(); InfixExpression bitOrTest = (InfixExpression) test1Expr.getExpression(); InfixExpression bitAndTest = (InfixExpression) test2Expr.getExpression(); InfixExpression bitXorTest = (InfixExpression) test3Expr.getExpression(); InfixExpression bitShiftTest = (InfixExpression) test4Expr.getExpression(); assertEquals(1, ifStmt.getLineno()); assertEquals(2, bigLHSExpr.getLineno()); assertEquals(7, bigRHSExpr.getLineno()); assertEquals(2, eqTest.getLineno()); assertEquals(7, notEqTest.getLineno()); assertEquals(2, test1Expr.getLineno()); assertEquals(5, test2Expr.getLineno()); assertEquals(7, test3Expr.getLineno()); assertEquals(10, test4Expr.getLineno()); assertEquals(2, bitOrTest.getLineno()); assertEquals(5, bitAndTest.getLineno()); assertEquals(7, bitXorTest.getLineno()); assertEquals(10, bitShiftTest.getLineno()); }
public void testLinenoInfix() { AstRoot root = parse( "\nvar d = a\n" + " + \n" + " b;\n" + "var\n" + " e =\n" + " a +\n" + " c;\n" + "var f = b\n" + " / c;\n"); VariableDeclaration stmt1 = (VariableDeclaration) root.getFirstChild(); List<VariableInitializer> vars1 = stmt1.getVariables(); VariableInitializer var1 = vars1.get(0); Name firstVarName = (Name) var1.getTarget(); InfixExpression var1Add = (InfixExpression) var1.getInitializer(); VariableDeclaration stmt2 = (VariableDeclaration) stmt1.getNext(); List<VariableInitializer> vars2 = stmt2.getVariables(); VariableInitializer var2 = vars2.get(0); Name secondVarName = (Name) var2.getTarget(); InfixExpression var2Add = (InfixExpression) var2.getInitializer(); VariableDeclaration stmt3 = (VariableDeclaration) stmt2.getNext(); List<VariableInitializer> vars3 = stmt3.getVariables(); VariableInitializer var3 = vars3.get(0); Name thirdVarName = (Name) var3.getTarget(); InfixExpression thirdVarDiv = (InfixExpression) var3.getInitializer(); ReturnStatement returnStmt = (ReturnStatement) stmt3.getNext(); assertEquals(1, var1.getLineno()); assertEquals(1, firstVarName.getLineno()); assertEquals(1, var1Add.getLineno()); assertEquals(1, var1Add.getLeft().getLineno()); assertEquals(3, var1Add.getRight().getLineno()); // var directive with name on next line wrong -- // should be 6. assertEquals(5, var2.getLineno()); assertEquals(5, secondVarName.getLineno()); assertEquals(6, var2Add.getLineno()); assertEquals(6, var2Add.getLeft().getLineno()); assertEquals(7, var2Add.getRight().getLineno()); assertEquals(8, var3.getLineno()); assertEquals(8, thirdVarName.getLineno()); assertEquals(8, thirdVarDiv.getLineno()); assertEquals(8, thirdVarDiv.getLeft().getLineno()); assertEquals(9, thirdVarDiv.getRight().getLineno()); }
public void setRightHandSide(Expression RightHandSide) { if (!(RightHandSide instanceof Name)) throw new IllegalArgumentException(); super.setRightHandSide(RightHandSide); }
public void setOperator(Operator operand) { if (!(operand.equals(Operator.UMOVE) || operand.equals(Operator.BMOVE))) throw new IllegalArgumentException(); super.setOperator(operand); }