Additive/multiplicativeExpression und UnaryExpression implementiert

This commit is contained in:
Fayez Abu Alia 2018-02-21 15:43:28 +01:00
parent c747ab0885
commit ac4a79f0e7
5 changed files with 72 additions and 12 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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;
// }
// }
}

View File

@ -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){

View File

@ -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;
}
}