From 77c59de0f0623cbc9b4512cca4f68010c159b734 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 23 Aug 2017 18:28:26 +0200 Subject: [PATCH] =?UTF-8?q?Assign=20=C3=A4ndern.=20Es=20gibt=20jetzt=20ein?= =?UTF-8?q?e=20AssignLeftSide?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SyntaxTreeGenerator/AssignToLocal.java | 20 +++++++++++++ .../StatementGenerator.java | 16 +++++++---- .../syntaxtree/AbstractASTWalker.java | 12 ++++++++ .../syntaxtree/StatementVisitor.java | 5 ++++ .../syntaxtree/statement/Assign.java | 9 ++++-- .../syntaxtree/statement/AssignLeftSide.java | 12 ++++++++ .../syntaxtree/statement/AssignToField.java | 16 +++++++++++ .../syntaxtree/statement/Expression.java | 19 ++----------- .../statement/TypableStatement.java | 28 +++++++++++++++++++ .../syntaxtree/visual/OutputGenerator.java | 11 ++++++++ .../typedeployment/TypeInsertPlacer.java | 2 ++ .../typeinference/typeAlgo/TYPE.java | 11 ++++++++ 12 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/AssignToLocal.java create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/AssignLeftSide.java create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/TypableStatement.java diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/AssignToLocal.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/AssignToLocal.java new file mode 100644 index 000000000..c505bfb69 --- /dev/null +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/AssignToLocal.java @@ -0,0 +1,20 @@ +package de.dhbwstuttgart.parser.SyntaxTreeGenerator; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide; +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.statement.LocalVar; + +public class AssignToLocal extends AssignLeftSide { + public final LocalVar localVar; + + public AssignToLocal(LocalVar leftSide) { + super(leftSide.getType(), leftSide.getOffset()); + localVar = leftSide; + } + + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 03c727380..89bc43742 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -288,12 +288,15 @@ public class StatementGenerator { } private Statement convert(Java8Parser.AssignmentContext stmt) { - Expression leftHandSide = convert(stmt.leftHandSide()); + AssignLeftSide leftHandSide = convert(stmt.leftHandSide()); return new Assign(leftHandSide, convert(stmt.expression()), stmt.getStart()); } - private Expression convert(Java8Parser.LeftHandSideContext leftHandSide) { - return generateLocalOrFieldVarOrClassName(leftHandSide.getText(), leftHandSide.getStart()); + private AssignLeftSide convert(Java8Parser.LeftHandSideContext leftHandSide) { + Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide.getText(), leftHandSide.getStart()); + if(leftSide instanceof FieldVar)return new AssignToField((FieldVar) leftSide); + else if (leftSide instanceof LocalVar)return new AssignToLocal((LocalVar) leftSide); + else throw new NotImplementedException(); } private Statement convert(Java8Parser.IfThenStatementContext stmt){ @@ -412,7 +415,8 @@ public class StatementGenerator { }else{ initValue = convert(varDecl.variableInitializer().expression()); } - ret.add(new Assign(new LocalVar(name.getText(), type, name.getSymbol()), initValue, name.getSymbol())); + ret.add(new Assign(new AssignToLocal(new LocalVar(name.getText(), type, name.getSymbol())) + , initValue, name.getSymbol())); } } return ret; @@ -426,9 +430,9 @@ public class StatementGenerator { }else{ initValue = convert(varDecl.variableInitializer().expression()); } - return (new Assign( + return (new Assign(new AssignToField( new FieldVar(new This(varDecl.getStart()), name.getText(), - new Void(varDecl.getStart()), varDecl.getStart()), + new Void(varDecl.getStart()), varDecl.getStart())), initValue, name.getSymbol())); } diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 420b82575..58ba3fae8 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -1,5 +1,7 @@ package de.dhbwstuttgart.syntaxtree; +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; @@ -250,4 +252,14 @@ public abstract class AbstractASTWalker implements ASTVisitor{ public void visit(Literal literal) { } + + @Override + public void visit(AssignToField assignLeftSide) { + assignLeftSide.field.accept(this); + } + + @Override + public void visit(AssignToLocal assignLeftSide) { + assignLeftSide.localVar.accept(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index cb4a5c7b7..dce80777a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.syntaxtree; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; @@ -58,4 +59,8 @@ public interface StatementVisitor { void visit(Null aNull); void visit(Literal literal); + + void visit(AssignToField assignLeftSide); + + void visit(AssignToLocal assignLeftSide); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index 471093fb5..ed385c255 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -8,13 +8,16 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintsFactory; import de.dhbwstuttgart.typeinference.unify.model.PairOperator; import org.antlr.v4.runtime.Token; - +/* +Aufbau: +rightSide = leftSide + */ public class Assign extends Statement { public final Expression rightSide; - public final Expression lefSide; + public final AssignLeftSide lefSide; - public Assign(Expression leftHandSide, Expression value, Token offset) { + public Assign(AssignLeftSide leftHandSide, Expression value, Token offset) { super(leftHandSide.getType(), offset); this.rightSide = value; this.lefSide = leftHandSide; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/AssignLeftSide.java b/src/de/dhbwstuttgart/syntaxtree/statement/AssignLeftSide.java new file mode 100644 index 000000000..13fec3f8a --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/AssignLeftSide.java @@ -0,0 +1,12 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; + +public abstract class AssignLeftSide extends TypableStatement{ + + public AssignLeftSide(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { + super(type, offset); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java b/src/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java new file mode 100644 index 000000000..931443d18 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java @@ -0,0 +1,16 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; + +public class AssignToField extends AssignLeftSide{ + public final FieldVar field; + public AssignToField(FieldVar fieldVar) { + super(fieldVar.getType(), fieldVar.getOffset()); + field = fieldVar; + } + + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Expression.java b/src/de/dhbwstuttgart/syntaxtree/statement/Expression.java index 902a832de..0413e1247 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Expression.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Expression.java @@ -9,24 +9,9 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import org.antlr.v4.runtime.Token; -public abstract class Expression extends SyntaxTreeNode +public abstract class Expression extends TypableStatement { - private RefTypeOrTPHOrWildcardOrGeneric type; - public Expression(RefTypeOrTPHOrWildcardOrGeneric type, Token offset){ - super(offset); - if(type == null)throw new NullPointerException(); - this.type = type; - } - - public RefTypeOrTPHOrWildcardOrGeneric getType(){ - return type; - } - - public abstract void accept(StatementVisitor visitor); - - @Override - public void accept(ASTVisitor visitor) { - this.accept((StatementVisitor)visitor); + super(type, offset); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/TypableStatement.java b/src/de/dhbwstuttgart/syntaxtree/statement/TypableStatement.java new file mode 100644 index 000000000..a2166d2d6 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/TypableStatement.java @@ -0,0 +1,28 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.ASTVisitor; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; + +public abstract class TypableStatement extends SyntaxTreeNode{ + private RefTypeOrTPHOrWildcardOrGeneric type; + + public TypableStatement(RefTypeOrTPHOrWildcardOrGeneric type, Token offset){ + super(offset); + if(type == null)throw new NullPointerException(); + this.type = type; + } + + public RefTypeOrTPHOrWildcardOrGeneric getType(){ + return type; + } + + public abstract void accept(StatementVisitor visitor); + + @Override + public void accept(ASTVisitor visitor) { + this.accept((StatementVisitor)visitor); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 0adac8a83..e5040d6c1 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.syntaxtree.visual; import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; @@ -330,4 +331,14 @@ public class OutputGenerator implements ASTVisitor { public void visit(Literal literal) { } + + @Override + public void visit(AssignToField assignLeftSide) { + assignLeftSide.field.accept(this); + } + + @Override + public void visit(AssignToLocal assignLeftSide) { + assignLeftSide.localVar.accept(this); + } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index 88e7ad68f..c9fa39a63 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -1,6 +1,7 @@ package de.dhbwstuttgart.typedeployment; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.ResultSet; @@ -27,6 +28,7 @@ public class TypeInsertPlacer extends AbstractASTWalker{ this.inserts.addAll(cl.inserts); } + /* List ret = new ArrayList<>(); diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 646222ebf..eaa57cddc 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -4,6 +4,7 @@ package de.dhbwstuttgart.typeinference.typeAlgo; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; @@ -219,6 +220,16 @@ public class TYPE implements StatementVisitor{ throw new NotImplementedException(); } + @Override + public void visit(AssignToField assignLeftSide) { + //Hier ist kein Code nötig. Es werden keine extra Constraints generiert + } + + @Override + public void visit(AssignToLocal assignLeftSide) { + //Hier ist kein Code nötig. Es werden keine extra Constraints generiert + } + /* METHOD CALL Section: */