From 856f9b059d06a1fb51d44aafb27527fc290539fa Mon Sep 17 00:00:00 2001 From: FelixKrespach Date: Thu, 17 Aug 2017 15:08:30 +0200 Subject: [PATCH] while Statement implemented --- .idea/libraries/lib.xml | 22 +++++++++++++++++++ classes/classes.iml | 9 ++++++++ doc/doc.iml | 9 ++++++++ src/JavaCompilerCore1.iml | 11 ++++++++++ .../StatementGenerator.java | 12 ++++++---- .../SyntaxTreeGenerator/SyntacticSugar.java | 3 ++- .../syntaxtree/statement/WhileStmt.java | 18 +++++++++------ .../typecheck/JavaClassRegistry.java | 9 ++++---- test/parser/RunParserTest.java | 2 +- test/parser/WhileTest.jav | 9 ++++++++ test/test.iml | 11 ++++++++++ tools/tools.iml | 9 ++++++++ 12 files changed, 107 insertions(+), 17 deletions(-) create mode 100644 .idea/libraries/lib.xml create mode 100644 classes/classes.iml create mode 100644 doc/doc.iml create mode 100644 src/JavaCompilerCore1.iml create mode 100644 test/parser/WhileTest.jav create mode 100644 test/test.iml create mode 100644 tools/tools.iml diff --git a/.idea/libraries/lib.xml b/.idea/libraries/lib.xml new file mode 100644 index 00000000..6a9ec091 --- /dev/null +++ b/.idea/libraries/lib.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/classes/classes.iml b/classes/classes.iml new file mode 100644 index 00000000..8021953e --- /dev/null +++ b/classes/classes.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/doc/doc.iml b/doc/doc.iml new file mode 100644 index 00000000..8021953e --- /dev/null +++ b/doc/doc.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/JavaCompilerCore1.iml b/src/JavaCompilerCore1.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/src/JavaCompilerCore1.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 58eb92de..8dcdde6e 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -333,8 +333,9 @@ public class StatementGenerator { } private Statement convert(Java8Parser.WhileStatementContext stmt){ - //TODO - throw new NotImplementedException(); + Expression expr = convert(stmt.expression()); + Statement block = convert(stmt.statement()); + return new WhileStmt(expr, block,stmt.getStart()); } private Statement convert(Java8Parser.WhileStatementNoShortIfContext stmt){ @@ -441,8 +442,11 @@ public class StatementGenerator { } private Statement convert(Java8Parser.ReturnStatementContext stmt){ - return new Return(convert(stmt.expression()),stmt.getStart()); - //throw new NotImplementedException(); + if(stmt.expression() != null){ + return new Return( convert(stmt.expression()),stmt.getStart()); + }else{ + return new ReturnVoid(stmt.getStart()); + } } private Statement convert(Java8Parser.ThrowStatementContext stmt){ diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java index 07b2d1fb..d758f52b 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java @@ -12,7 +12,8 @@ public class SyntacticSugar { Statement lastStmt = statements.get(statements.size() - 1); if (lastStmt instanceof Return) return statements; if (lastStmt instanceof WhileStmt) { - if (hasReturn(((WhileStmt) lastStmt).loop_block)) return statements; + //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; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java index 351133e6..3422ab14 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java @@ -2,20 +2,24 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; 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 WhileStmt extends Statement { - public WhileStmt(int offset, int variableLength) - { - super(null,null); - } + public final Expression expr; + public final Statement loopBlock; - public Expression expr; - public Block loop_block; + public WhileStmt(Expression expr, Statement loopBlock, Token offset) + { + super(TypePlaceholder.fresh(offset), offset); + this.expr = expr; + this.loopBlock = loopBlock; + } /** *
Author: Martin Pl�micke @@ -23,7 +27,7 @@ public class WhileStmt extends Statement */ public String toString() { - return "WHILE " + loop_block.toString(); + return "WHILE " + loopBlock.toString(); } @Override diff --git a/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java b/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java index 8bda1f0a..ad86b0bf 100644 --- a/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java +++ b/src/de/dhbwstuttgart/typecheck/JavaClassRegistry.java @@ -1,7 +1,6 @@ package de.dhbwstuttgart.typecheck; -import java.util.ArrayList; -import java.util.List; +import java.util.*; /** * Speichert die Klassen im aktuellen Projektscope @@ -17,7 +16,7 @@ public class JavaClassRegistry { } /** - * Fügt ein gesamtes Package der ClassRegistry hinzu. + * F�gt ein gesamtes Package der ClassRegistry hinzu. * Dies geschieht beispielsweise, wenn der Benutzer ein "import package.*;" statement verwendet * @param packageName */ @@ -55,6 +54,8 @@ public class JavaClassRegistry { } public boolean contains(String whole) { - return existingClasses.contains(new JavaClassName(whole)); + boolean ret = existingClasses.contains(new JavaClassName(whole)); + if(ret == false)existingClasses.add(getName(whole)); + return true; } } diff --git a/test/parser/RunParserTest.java b/test/parser/RunParserTest.java index 3028ef3d..222a55b8 100644 --- a/test/parser/RunParserTest.java +++ b/test/parser/RunParserTest.java @@ -14,7 +14,7 @@ public class RunParserTest { @Test public void testMain() throws Exception { String[] args = new String[1]; - args[0] = rootDirectory+"ImportTest2.jav"; + args[0] = rootDirectory+"WhileTest.jav"; new JavaTXParser().parse(new File(args[0])); } } \ No newline at end of file diff --git a/test/parser/WhileTest.jav b/test/parser/WhileTest.jav new file mode 100644 index 00000000..54e354ea --- /dev/null +++ b/test/parser/WhileTest.jav @@ -0,0 +1,9 @@ +class WhileTest{ + void methode(){ + Boolean test; + while(test){ + test = test; + } + return; + } +} \ No newline at end of file diff --git a/test/test.iml b/test/test.iml new file mode 100644 index 00000000..a6c28e92 --- /dev/null +++ b/test/test.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tools/tools.iml b/tools/tools.iml new file mode 100644 index 00000000..8021953e --- /dev/null +++ b/tools/tools.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file