diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index c5480ac0..b13a1fb6 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -167,6 +167,7 @@ public class BytecodeGenMethod implements StatementVisitor{ public void visit(BinaryExpr binary) { System.out.println("\t++ In Binary: "); + System.out.println(binary.operation.toString()); } @@ -318,7 +319,7 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(IfStmt ifStmt) { - + System.out.println("If"); } @Override @@ -389,7 +390,7 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(UnaryExpr unaryExpr) { - throw new NotImplementedException(); + System.out.println(unaryExpr.operation.toString()); } @Override diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 679b23df..2b0b1a4f 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -7,6 +7,7 @@ import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.*; +import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr.Operation; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -278,13 +279,14 @@ public class StatementGenerator { } private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + Expression argument = convert(stmt.unaryExpression()); + Token offset = stmt.getStart(); + return new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset); } - private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { + return new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), + TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); } private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { @@ -293,8 +295,8 @@ public class StatementGenerator { } private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + return new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), + TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); } private Statement convert(Java8Parser.AssignmentContext stmt) { @@ -600,7 +602,30 @@ public class StatementGenerator { private BinaryExpr.Operator convertBinaryOperator(String operator) { //return BinaryExpr.Operator.ADD; - throw new NotImplementedException(); + if(operator.equals("+")) { + return BinaryExpr.Operator.ADD; + }else if(operator.equals("-")) { + return BinaryExpr.Operator.SUB; + }else if(operator.equals("*")) { + return BinaryExpr.Operator.MUL; + }else if(operator.equals("&")) { + return BinaryExpr.Operator.AND; + }else if(operator.equals("|")) { + return BinaryExpr.Operator.OR; + }else if(operator.equals("/")) { + return BinaryExpr.Operator.DIV; + }else if(operator.equals("<")) { + return BinaryExpr.Operator.LESSTHAN; + }else if(operator.equals(">")) { + return BinaryExpr.Operator.BIGGERTHAN; + }else if(operator.equals(">=")) { + return BinaryExpr.Operator.BIGGEREQUAL; + } else if(operator.equals("<=")) { + return BinaryExpr.Operator.LESSEQUAL; + } else { + throw new NotImplementedException(); + } +// throw new NotImplementedException(); } private Expression convert(Java8Parser.ShiftExpressionContext expression) { @@ -628,7 +653,11 @@ public class StatementGenerator { if(expression.multiplicativeExpression() == null){ return convert(expression.unaryExpression()); }else{ - throw new NotImplementedException(); + Expression leftSide = convert(expression.multiplicativeExpression()); + Expression rightSide = convert(expression.unaryExpression()); + BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); + Token offset = expression.getStart(); + return new BinaryExpr(op, TypePlaceholder.fresh(offset), leftSide, rightSide, offset); } } diff --git a/test/bytecode/For.jav b/test/bytecode/For.jav index c93b34c4..04cb6a53 100644 --- a/test/bytecode/For.jav +++ b/test/bytecode/For.jav @@ -1,6 +1,15 @@ +import java.lang.Integer; +import java.lang.Boolean; + class For{ m(Integer x){ + Boolean b = true; + c = 5; + c++; + ++c; + c--; + --c; while(x<2){ x = x +1; b = false; @@ -10,4 +19,12 @@ class For{ // x = x + 5; // } } + +// m2(Integer x){ +// if(x<2) { +// return 1; +// }else { +// return 2; +// } +// } } \ No newline at end of file diff --git a/test/parser/GeneralParserTest.java b/test/parser/GeneralParserTest.java index c48e1320..b218347f 100644 --- a/test/parser/GeneralParserTest.java +++ b/test/parser/GeneralParserTest.java @@ -41,7 +41,8 @@ public class GeneralParserTest{ filenames.add("FieldVarTest.jav"); filenames.add("StructuralTypes.jav"); */ - filenames.add("ExtendsTest.jav"); +// filenames.add("ExtendsTest.jav"); + filenames.add("OpratorTest.jav"); try{ new JavaTXCompiler(filenames.stream().map(s -> new File(rootDirectory + s)).collect(Collectors.toList())); }catch(Exception exc){ diff --git a/test/parser/OpratorTest.jav b/test/parser/OpratorTest.jav new file mode 100644 index 00000000..f99734f6 --- /dev/null +++ b/test/parser/OpratorTest.jav @@ -0,0 +1,12 @@ +import java.lang.Integer; + +class OpratorTest { + m(Integer a, Integer b) { + c = a+b; + d = a-b; + e = a*b; + f = a/b; + + return c; + } +} \ No newline at end of file