Merge branch 'parser' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into bytecode2

This commit is contained in:
Fayez Abu Alia 2018-02-21 15:45:22 +01:00
commit 6808535f67
5 changed files with 78 additions and 18 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) {
@ -599,8 +601,31 @@ public class StatementGenerator {
}
private BinaryExpr.Operator convertBinaryOperator(String operator) {
return null;
//return BinaryExpr.Operator.ADD;
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) {
@ -612,12 +637,15 @@ public class StatementGenerator {
}
private Expression convert(Java8Parser.AdditiveExpressionContext expression) {
if(expression.additiveExpression() != null){
return convert(expression.additiveExpression());
}else if(expression.multiplicativeExpression() != null){
if(expression.additiveExpression() == null){
return convert(expression.multiplicativeExpression());
}else{
throw new NotImplementedException();
}else {
Expression leftSide = convert(expression.additiveExpression());
Expression rightSide = convert(expression.multiplicativeExpression());
BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText());
Token offset = expression.getStart();
return new BinaryExpr(op, TypePlaceholder.fresh(expression.getStart()), leftSide, rightSide, offset);
}
}
@ -625,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

@ -3,7 +3,13 @@ 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;
@ -13,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;
}
}