IfStmt Constraints erstellen

This commit is contained in:
JanUlrich 2018-08-30 00:02:17 +02:00
parent 0ee5a6791d
commit 8759a9cc5d
6 changed files with 52 additions and 7 deletions

View File

@ -349,10 +349,8 @@ public class SyntaxTreeGenerator{
if(varCtx.variableInitializer() != null){
initializeField(varCtx, fieldType, generics);
}
else{
ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart()));
}
}
return ret;
}

View File

@ -231,7 +231,20 @@ public class OutputGenerator implements ASTVisitor{
@Override
public void visit(IfStmt ifStmt) {
out.append("if(");
ifStmt.expr.accept(this);
out.append(")\n");
tab();
out.append(tabs);
ifStmt.then_block.accept(this);
untab();
if(ifStmt.else_block != null){
out.append("\n" + tabs + "else\n");
tab();
out.append(tabs);
ifStmt.else_block.accept(this);
untab();
}
}
@Override

View File

@ -122,7 +122,20 @@ public class TYPEStmt implements StatementVisitor{
@Override
public void visit(IfStmt ifStmt) {
throw new NotImplementedException();
RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken());
//Expression inferieren:
ifStmt.expr.accept(this);
//Expression muss boolean sein:
constraintsSet.addUndConstraint(new Pair(ifStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT));
//Blöcke inferieren:
ifStmt.then_block.accept(this);
//Beide Blöcke müssen den gleichen Supertyp haben, welcher den Rückgabetyp des If-Stmts darstellt
constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT));
if(ifStmt.else_block != null){
ifStmt.else_block.accept(this);
constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT));
}
}
@Override

View File

@ -1,13 +1,15 @@
import java.lang.Integer;
import java.lang.Boolean;
import java.lang.String;
public class IfTest{
Integer m1(Boolean b) {
Integer i;
String b;
if(b) {
return i;
}else{
return b;
}
return i;
}
}

15
test/javFiles/IfTest.jav Normal file
View File

@ -0,0 +1,15 @@
import java.lang.Integer;
import java.lang.Boolean;
import java.lang.Object;
public class IfTest{
Object m1(Boolean b) {
Integer i;
Boolean b;
if(b) {
return i;
}else{
return b;
}
}
}

View File

@ -92,6 +92,10 @@ public class JavaTXCompilerTest {
public void fields() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Fields.jav"));
}
@Test
public void ifStatement() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"IfTest.jav"));
}
private static class TestResultSet{