Compare commits

...

9 Commits

Author SHA1 Message Date
FelixKrespach
998c146f4b Final 2017-10-02 17:11:55 +02:00
FelixKrespach
88c78342d5 Final 2017-10-01 09:58:56 +02:00
FelixKrespach
86b2118770 Refactoring 2017-09-25 08:24:08 +02:00
FelixKrespach
03080c43cb Refactoring 2017-09-25 08:17:36 +02:00
FelixKrespach
df1c19e60d Tests added 2017-09-23 18:33:48 +02:00
FelixKrespach
07303ffa71 Implemented Continue, Catches, Break visit ... 2017-09-22 18:19:34 +02:00
FelixKrespach
881830235a Break Class + Test 2017-09-22 14:49:43 +02:00
FelixKrespach
b88b9e2534 methods in statementgenerator implemented
Tests + visit
2017-09-21 22:36:13 +02:00
FelixKrespach
8e1c1765ff Many methods in statementgenerator implemented 2017-09-20 17:36:16 +02:00
26 changed files with 344 additions and 68 deletions

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import com.sun.javafx.fxml.expression.UnaryExpression;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
@ -266,25 +267,21 @@ public class StatementGenerator {
//TODO
throw new NotImplementedException();
}
//TODO Felix_K
private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) {
//TODO
throw new NotImplementedException();
return new PreIncExpr(convert(stmt.unaryExpression()),stmt.getStart());
}
//TODO Felix_K
private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) {
//TODO
throw new NotImplementedException();
return new PreDecExpr(convert(stmt.unaryExpression()),stmt.getStart());
}
//TODO Felix_K
private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) {
//TODO
throw new NotImplementedException();
return new PostIncExpr(convert(stmt.postfixExpression()));
}
//TODO Felix_K
private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) {
//TODO
throw new NotImplementedException();
return new PostDecExpr(convert(stmt.postfixExpression()));
}
private Statement convert(Java8Parser.AssignmentContext stmt) {
@ -298,30 +295,45 @@ public class StatementGenerator {
else if (leftSide instanceof LocalVar)return new AssignToLocal((LocalVar) leftSide);
else throw new NotImplementedException();
}
//TODO Felix_K
private Statement convert(Java8Parser.IfThenStatementContext stmt){
//TODO
throw new NotImplementedException();
Expression expr = convert(stmt.expression());
Statement thenBlock = convert(stmt.statement());
Statement elseBlock = new EmptyStmt(stmt.getStart());
return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart());
}
//TODO Felix_K
private Statement convert(Java8Parser.IfThenElseStatementContext stmt){
//TODO
throw new NotImplementedException();
Expression expr = convert(stmt.expression());
Statement thenBlock = convert(stmt.statementNoShortIf());
Statement elseBlock = convert(stmt.statement());
return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart());
}
//TODO Felix_K
private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt){
//TODO
throw new NotImplementedException();
Expression expr = convert(stmt.expression());
Statement thenBlock = convert(stmt.statementNoShortIf().get(0));
Statement elseBlock = convert(stmt.statementNoShortIf().get(1));
return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart());
}
private Statement convert(Java8Parser.AssertStatementContext stmt){
//TODO
throw new NotImplementedException();
//TODO Felix_K
private Statement convert(Java8Parser.AssertStatementContext stmt)
{
List<Expression> exprList= new ArrayList<Expression>();
for (int i =0; i<stmt.expression().size();i++){
exprList.add(convert(stmt.expression(i)));
}
return new AssertStmt(TypePlaceholder.fresh(stmt.getStart()),exprList,stmt.getStart());
}
//TODO Felix_K
private Statement convert(Java8Parser.SwitchStatementContext stmt){
//TODO
throw new NotImplementedException();
Expression expr = convert(stmt.expression());
List<Statement> statementList = new ArrayList<Statement>();
statementList.add( convert(stmt.switchBlock()));
statementList.add(new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),"",stmt.getStart()));
Block block = new Block(statementList, stmt.getStart());
return new SwitchStmt(TypePlaceholder.fresh(stmt.getStart()),expr,block,stmt.getStart());
}
private Statement convert(Java8Parser.SwitchBlockContext stmt){
@ -339,12 +351,13 @@ public class StatementGenerator {
Statement block = convert(stmt.statement());
return new WhileStmt(expr, block,stmt.getStart());
}
//TODO Felix_K
private Statement convert(Java8Parser.WhileStatementNoShortIfContext stmt){
//TODO
throw new NotImplementedException();
Expression expr = convert(stmt.expression());
Statement block = convert(stmt.statementNoShortIf());
return new WhileStmt(expr, block, stmt.getStart());
}
//TODO Felix_K
private Statement convert(Java8Parser.DoStatementContext stmt){
Statement block = convert(stmt.statement());
Expression expr = convert(stmt.expression());
@ -366,15 +379,43 @@ public class StatementGenerator {
return convert(stmt.enhancedForStatementNoShortIf());
}else throw new NotImplementedException();
}
//TODO Felix_K
private Statement convert(Java8Parser.BasicForStatementContext stmt){
//TODO
throw new NotImplementedException();
List<Statement> stateList = new ArrayList<>();
List<Statement> whileList = new ArrayList<>();
if (stmt.forInit().localVariableDeclaration()==null)
{
whileList.addAll(convert(stmt.forInit().localVariableDeclaration()));
}
else
{
whileList.add(convert(stmt.forInit().statementExpressionList()));
}
Expression expr = convert(stmt.expression());
stateList.add(convert(stmt.statement()));
stateList.add(convert(stmt.forUpdate()));
Block forBlock = new Block(stateList,stmt.getStart());
whileList.add(new WhileStmt(expr,forBlock,stmt.getStart()));
return new Block(whileList,stmt.getStart());
}
//TODO Felix_K
private Statement convert(Java8Parser.BasicForStatementNoShortIfContext stmt){
//TODO
throw new NotImplementedException();
List<Statement> stateList = new ArrayList<>();
List<Statement> whileList = new ArrayList<>();
if (stmt.forInit().localVariableDeclaration()==null)
{
whileList.addAll(convert(stmt.forInit().localVariableDeclaration()));
}
else
{
whileList.add(convert(stmt.forInit().statementExpressionList()));
}
Expression expr = convert(stmt.expression());
stateList.add(convert(stmt.statementNoShortIf()));
stateList.add(convert(stmt.forUpdate()));
Block forBlock = new Block(stateList,stmt.getStart());
whileList.add(new WhileStmt(expr,forBlock,stmt.getStart()));
return new Block(whileList,stmt.getStart());
}
private List<Statement> convert(Java8Parser.ForInitContext stmt){
@ -384,7 +425,7 @@ public class StatementGenerator {
return convert(stmt.localVariableDeclaration());
}else throw new NotImplementedException();
}
//TODO Felix_K
private List<Statement> convert(Java8Parser.LocalVariableDeclarationContext declaration) {
List<Statement> ret = new ArrayList<>();
if(declaration.variableModifier() != null && declaration.variableModifier().size() > 0){
@ -454,15 +495,22 @@ public class StatementGenerator {
//TODO
throw new NotImplementedException();
}
//TODO Felix_K
private Statement convert(Java8Parser.BreakStatementContext stmt){
//TODO
throw new NotImplementedException();
if (stmt.Identifier()==null)
{
String indentifier = "";
return new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),indentifier,stmt.getStart());
}
return new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),stmt.Identifier().getText(),stmt.getStart());
}
//TODO Felix_K
private Statement convert(Java8Parser.ContinueStatementContext stmt){
//TODO
throw new NotImplementedException();
if (stmt.Identifier()==null){
String indentifier = "";
return new ContinueStmt(TypePlaceholder.fresh(stmt.getStart()),indentifier, stmt.getStart());
}
return new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),stmt.Identifier().getText(),stmt.getStart());
}
private Statement convert(Java8Parser.ReturnStatementContext stmt){
@ -488,7 +536,8 @@ public class StatementGenerator {
throw new NotImplementedException();
}
private Statement convert(Java8Parser.CatchesContext stmt){
private Statement convert(Java8Parser.CatchesContext stmt)
{
//TODO
throw new NotImplementedException();
}

View File

@ -15,8 +15,8 @@ public class SyntacticSugar {
//TODO
//if (hasReturn(((WhileStmt) lastStmt).loopBlock)) return statements;
} else if (lastStmt instanceof IfStmt) {
if (hasReturn(((IfStmt) lastStmt).then_block)
&& hasReturn(((IfStmt) lastStmt).else_block)) return statements;
//if (hasReturn(((IfStmt) lastStmt).then_block)
//&& hasReturn(((IfStmt) lastStmt).else_block)) return statements;
} else if (lastStmt instanceof ForStmt) {
if (hasReturn(((ForStmt) lastStmt).body_Loop_block)) return statements;
} else {

View File

@ -35,4 +35,6 @@ public interface ASTVisitor extends StatementVisitor{
void visit(ExtendsWildcardType extendsWildcardType);
void visit(GenericRefType genericRefType);
void visit(BreakStmt breakStmt);
}

View File

@ -268,4 +268,8 @@ public abstract class AbstractASTWalker implements ASTVisitor{
public void visit(SuperCall superCall) {
this.visit((MethodCall)superCall);
}
@Override
public void visit(BreakStmt breakStmt) {
}
}

View File

@ -18,6 +18,8 @@ public interface StatementVisitor {
void visit(Block block);
void visit(BreakStmt breakStmt);
void visit(CastExpr castExpr);
void visit(EmptyStmt emptyStmt);

View File

@ -0,0 +1,24 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
import java.util.List;
/**
* Created by Felix_K on 21.09.2017.
*/
public class AssertStmt extends Statement
{
public final List<Expression> expr;
public AssertStmt(RefTypeOrTPHOrWildcardOrGeneric type, List<Expression> expr, Token offset){
super(type, offset);
this.expr=expr;
}
@Override
public void accept(StatementVisitor visitor) {
}
}

View File

@ -0,0 +1,25 @@
package de.dhbwstuttgart.syntaxtree.statement;
import com.sun.scenario.effect.Offset;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
/**
* Created by Felix_K on 20.09.2017.
*/
public class BreakStmt extends Statement
{
public final String indentifier;
public BreakStmt(RefTypeOrTPHOrWildcardOrGeneric type, String Indentifier, Token offset)
{
super(type, offset);
this.indentifier=Indentifier;
}
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -0,0 +1,15 @@
package de.dhbwstuttgart.syntaxtree.statement;
import java.util.List;
/**
* Created by Felix_K on 21.09.2017.
*/
public class CatchesStatement
{
final List<Statement> catchClauses;
public CatchesStatement(List<Statement> catches)
{
catchClauses = catches;
}
}

View File

@ -0,0 +1,14 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
/**
* Created by Felix_K on 22.09.2017.
*/
public class ContinueStmt extends BreakStmt {
public ContinueStmt(RefTypeOrTPHOrWildcardOrGeneric type, String indent, Token offset){
super(type, indent, offset);
}
}

View File

@ -11,11 +11,11 @@ import org.antlr.v4.runtime.Token;
public class IfStmt extends Statement
{
public final Expression expr;
public final Block then_block;
public final Block else_block;
public final Statement then_block;
public final Statement else_block;
public IfStmt(RefTypeOrTPHOrWildcardOrGeneric type,
Expression expr, Block thenBlock, Block elseBlock, Token offset)
Expression expr, Statement thenBlock, Statement elseBlock, Token offset)
{
super(type,offset);
this.expr = expr;
@ -23,6 +23,7 @@ public class IfStmt extends Statement
this.else_block = elseBlock;
}
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);

View File

@ -3,6 +3,7 @@ import java.util.Hashtable;
import java.util.Iterator;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import org.antlr.v4.runtime.Token;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.InstructionList;

View File

@ -4,12 +4,15 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import org.antlr.v4.runtime.Token;
public class PostIncExpr extends UnaryExpr
{
final Expression expr;
public PostIncExpr(Expression expr) {
super(null);
this.expr=expr;
}
}

View File

@ -12,8 +12,10 @@ import org.apache.bcel.generic.InstructionList;
public class PreDecExpr extends UnaryExpr
{
public PreDecExpr(Token offset)
Expression expr;
public PreDecExpr(Expression expr, Token offset)
{
super(offset);
this.expr=expr;
}
}

View File

@ -5,8 +5,10 @@ import org.antlr.v4.runtime.Token;
public class PreIncExpr extends UnaryExpr
{
public PreIncExpr(Token offset)
final Expression expr;
public PreIncExpr(Expression expr,Token offset)
{
super(offset);
this.expr=expr;
}
}

View File

@ -0,0 +1,24 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token;
/**
* Created by Felix_K on 23.09.2017.
*/
public class SwitchStmt extends Statement
{
public final Expression expr;
public final Block switchBlock;
public SwitchStmt(RefTypeOrTPHOrWildcardOrGeneric type,Expression expr, Block block, Token offset){
super(type,offset);
this.expr=expr;
this.switchBlock=block;
}
@Override
public void accept(StatementVisitor visitor) {
}
}

View File

@ -209,6 +209,12 @@ public class OutputGenerator implements ASTVisitor {
out.append("}");
}
@Override
public void visit(BreakStmt breakStmt)
{
out.append("break"+ " " + breakStmt.indentifier);
}
@Override
public void visit(CastExpr castExpr) {
@ -232,7 +238,15 @@ public class OutputGenerator implements ASTVisitor {
@Override
public void visit(IfStmt ifStmt) {
out.append("if (");
ifStmt.expr.accept(this);
out.append(")");
//TODO implement to set ; if then block is empty
ifStmt.then_block.accept(this);
out.append("else");
ifStmt.else_block.accept(this);
//TODO Felix_K
}
@Override

View File

@ -1,6 +1,8 @@
package de.dhbwstuttgart.typedeployment;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.BreakStmt;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.result.ResultSet;
@ -70,4 +72,5 @@ class TypeInsertPlacerClass extends AbstractASTWalker{
public void visit(LambdaExpression lambdaExpression) {
//Lambda-Ausdrücke brauchen keine Typeinsetzungen
}
}

View File

@ -87,6 +87,8 @@ public class TYPEStmt implements StatementVisitor{
stmt.accept(this);
}
}
@Override
public void visit(BreakStmt breakStmt){throw new NotImplementedException();}
@Override
public void visit(CastExpr castExpr) {

View File

@ -0,0 +1,7 @@
class AssertTest
{
void assertMethod()
{
assert true;
}
}

View File

@ -0,0 +1,7 @@
class BreakTest
{
void methode()
{
break BREAKTEST;
}
}

24
test/parser/IfTest.jav Normal file
View File

@ -0,0 +1,24 @@
class IfTest
{
public void IfStatementTest();
public void ifThenNoShortIfTest()
{
if(y)
{
}
}
public void ifThenElseTest()
{
if(z)
{
}
else
{
}
}
}

View File

@ -0,0 +1,10 @@
class TestClass
{
void testMethod()
{
//++i;
//i++;
//--i;
//i--;
}
}

View File

@ -0,0 +1,11 @@
public class SwitchTest
{
void testMethod()
{
switch(i)
{
case i:
break;
}
}
}

View File

@ -0,0 +1,14 @@
class TryCatchTest
{
public void TryTest()
{
try
{
}
catch()
{
}
}
}

View File

@ -1,14 +1,16 @@
class WhileTest{
void methode(){
Boolean test;
do{
test=test;
}while(test);
class WhileTest
{
void methode()
{
Boolean boolVar=true;
do
{
}while(boolVar);
while(test){
while(boolVar)
{
test = test;
}
return;
}
}

View File

@ -2,7 +2,9 @@ package typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
import de.dhbwstuttgart.syntaxtree.visual.OutputGenerator;
import de.dhbwstuttgart.typedeployment.TypeInsert;
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
import de.dhbwstuttgart.typeinference.result.ResultSet;
@ -20,14 +22,14 @@ import java.util.Set;
public class JavaTXCompilerTest extends JavaTXCompiler {
private static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/";
private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/";
private static final List<File> filesToTest = new ArrayList<>();
@Test
public void test() throws IOException, java.lang.ClassNotFoundException {
//filesToTest.add(new File(rootDirectory+"Faculty.jav"));
//filesToTest.add(new File(rootDirectory+"mathStruc.jav"));
filesToTest.add(new File(rootDirectory+"test.jav"));
//filesToTest.add(new File(rootDirectory+"test.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
@ -36,9 +38,20 @@ public class JavaTXCompilerTest extends JavaTXCompiler {
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
//filesToTest.add(new File(rootDirectory+"Import.jav"));
//filesToTest.add(new File(rootDirectory+"BreakTest.jav"));
//filesToTest.add(new File(rootDirectory+"WhileTest.jav"));
//filesToTest.add(new File(rootDirectory+"IfTest.jav"));
filesToTest.add(new File(rootDirectory+"AssertTest.jav"));
//filesToTest.add(new File(rootDirectory+"PrePostOperationTest.jav"));
//filesToTest.add(new File(rootDirectory+"SwitchTest.jav"));
//filesToTest.add(new File(rootDirectory+"WhileTest.jav"));
for(File f : filesToTest){
SourceFile sf = this.parse(f);
System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
System.out.println("-------------------------------------|"+f.getName()+"|----------------------------------------------");
System.out.println(ASTPrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
/*
for(ResultSet resultSet : this.typeInference()){
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet);
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
@ -46,6 +59,7 @@ public class JavaTXCompilerTest extends JavaTXCompiler {
System.out.println(tip.insert(content));
}
}
*/
}
}