From 282b0685525bdaa8153c3ce8a8b69b2fe1c306e9 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 19 Feb 2014 23:04:48 +0100 Subject: [PATCH] Begeonnen mit Implementierung von getChildren() --- src/mycompiler/MyCompiler.java | 1 - src/mycompiler/SourceFile.java | 5 +- src/mycompiler/SyntaxTreeNode.java | 4 +- src/mycompiler/myclass/FieldDecl.java | 95 ------------------- .../myclass/FieldInitialization.java | 87 ----------------- src/mycompiler/myclass/Method.java | 2 +- src/mycompiler/mystatement/Assign.java | 14 +++ src/mycompiler/mystatement/Binary.java | 9 ++ src/mycompiler/mystatement/Block.java | 13 +++ src/mycompiler/mystatement/IfStmt.java | 13 +++ .../mystatement/LocalOrFieldVar.java | 2 +- src/mycompiler/mystatement/MethodCall.java | 2 +- src/mycompiler/mytest/LambdaTest.java | 2 +- .../assumptions/TypeAssumptions.java | 11 ++- .../test/lambda/ConstructorTest.java | 2 +- .../test/lambda/ParseMultipleFilesTest.java | 4 +- .../test/lambda/testResults/LambdaTest.log | 8 +- 17 files changed, 71 insertions(+), 203 deletions(-) delete mode 100755 src/mycompiler/myclass/FieldDecl.java delete mode 100755 src/mycompiler/myclass/FieldInitialization.java diff --git a/src/mycompiler/MyCompiler.java b/src/mycompiler/MyCompiler.java index 602ae2ee..2b5ca372 100755 --- a/src/mycompiler/MyCompiler.java +++ b/src/mycompiler/MyCompiler.java @@ -17,7 +17,6 @@ import mycompiler.myclass.Class; import mycompiler.myclass.ClassBody; import mycompiler.myclass.Constructor_Backup; import mycompiler.myclass.DeclId; -import mycompiler.myclass.FieldDecl; import mycompiler.myclass.FormalParameter; import mycompiler.myclass.ImportDeclarations; import mycompiler.myclass.Method; diff --git a/src/mycompiler/SourceFile.java b/src/mycompiler/SourceFile.java index 2db1dda7..5df43c11 100755 --- a/src/mycompiler/SourceFile.java +++ b/src/mycompiler/SourceFile.java @@ -1508,7 +1508,8 @@ public class SourceFile @Override public void parserPostProcessing(SyntaxTreeNode parent) { if(parent!=null)throw new TypinferenzException("Eine SourceFile hat keine Elternelement im Syntaxbaum"); - for(SyntaxTreeNode node : this.getChildren())node.parserPostProcessing(this); + super.parserPostProcessing(parent); + //for(SyntaxTreeNode node : this.getChildren())node.parserPostProcessing(this); } @@ -1521,7 +1522,7 @@ public class SourceFile @Override public Vector getChildren() { - Vector ret = super.getChildren(); + Vector ret = new Vector(); for(Class cl : this.KlassenVektor){ ret.add(cl); } diff --git a/src/mycompiler/SyntaxTreeNode.java b/src/mycompiler/SyntaxTreeNode.java index 5252ab87..eeb0bd7a 100644 --- a/src/mycompiler/SyntaxTreeNode.java +++ b/src/mycompiler/SyntaxTreeNode.java @@ -25,9 +25,7 @@ public abstract class SyntaxTreeNode { return this.parent; } - public Vector getChildren(){ - return new Vector(); - } + public abstract Vector getChildren(); public Class getParentClass(){ SyntaxTreeNode parent = this.getParent(); diff --git a/src/mycompiler/myclass/FieldDecl.java b/src/mycompiler/myclass/FieldDecl.java deleted file mode 100755 index d994b567..00000000 --- a/src/mycompiler/myclass/FieldDecl.java +++ /dev/null @@ -1,95 +0,0 @@ -// ino.module.FieldDecl.8560.package -package mycompiler.myclass; -// ino.end -// ino.module.FieldDecl.8560.import -import java.util.Vector; - -import typinferenz.JavaCodeResult; -import typinferenz.ResultSet; -import typinferenz.Typeable; -import typinferenz.TypeInsertable; -import typinferenz.assumptions.TypeAssumptions; -import mycompiler.mybytecode.ClassFile; -import mycompiler.myexception.JVMCodeException; -// ino.end -import mycompiler.mystatement.Expr; -import mycompiler.mytypereconstruction.set.CTypeAssumptionSet; -import mycompiler.mytypereconstruction.typeassumption.CTypeAssumption; - -@Deprecated -// ino.class.FieldDecl.23367.declaration -public abstract class FieldDecl implements TypeInsertable -// ino.end -// ino.class.FieldDecl.23367.body -{ - // ino.attribute.declid.23370.declaration - protected Vector declid = new Vector(); // Vector, da 'int a, b, c, ...' auch eingeparst werden muss - - // ino.end - // ino.method.getTypeName.23373.declaration - public abstract String getTypeName(); - // ino.end - - - - // ino.method.codegen.23376.declaration - public abstract void codegen(ClassFile classfile, Vector paralist) - throws JVMCodeException; - // ino.end - - - - // ino.method.set_DeclId.23379.definition - public void set_DeclId(DeclId did) - // ino.end - // ino.method.set_DeclId.23379.body - { - this.declid.addElement(did); - } - // ino.end - - - - // ino.method.get_Name.23382.definition - public Vector get_Name() - // ino.end - // ino.method.get_Name.23382.body - { - return declid; - } - // ino.end - - // ino.method.getDeclIdVector.23385.definition - public Vector getDeclIdVector() - // ino.end - // ino.method.getDeclIdVector.23385.body - { - // otth: ganzer Vektor zur�ckgeben, um ihn zu kopieren (vgl. MyCompiler - Konstruktor in Methode umwandeln) - return declid; - } - // ino.end - - // ino.method.setDeclIdVector.23388.definition - public void setDeclIdVector( Vector vDeclId ) - // ino.end - // ino.method.setDeclIdVector.23388.body - { - // otth: kompletter Vektor setzen, um ihn zu kopieren (vgl. MyCompiler - Konstruktor in Methode umwandeln) - declid = vDeclId; - } - // ino.end - - - - - public abstract JavaCodeResult printJavaCode(ResultSet resultSet); - - /** - * Diese Methode generiert die Assumptions für dieses Feld der Klasse classmember - * @param classmember - * @return - */ - public abstract TypeAssumptions createTypeAssumptions(Class classmember); - -} -// ino.end diff --git a/src/mycompiler/myclass/FieldInitialization.java b/src/mycompiler/myclass/FieldInitialization.java deleted file mode 100755 index 39779a2c..00000000 --- a/src/mycompiler/myclass/FieldInitialization.java +++ /dev/null @@ -1,87 +0,0 @@ -package mycompiler.myclass; - -import java.util.Vector; - -import typinferenz.JavaCodeResult; -import typinferenz.ResultSet; -import typinferenz.assumptions.TypeAssumptions; -import mycompiler.mybytecode.ClassFile; -import mycompiler.myexception.JVMCodeException; -import mycompiler.mystatement.Expr; -import mycompiler.mytype.Type; -import mycompiler.mytype.TypePlaceholder; -import mycompiler.mytypereconstruction.typeassumption.CInstVarTypeAssumption; - -@Deprecated -public class FieldInitialization extends InstVarDecl { - - - private Expr wert; - //private Type type; - - public void setWert(Expr initialExpression){ - this.wert = initialExpression; - } - public Expr getWert(){ - return this.wert; - } - - public String getIdentifier(){ - return this.getIdentifier().elementAt(0).name; - } - - - @Override - public String getTypeName() { - // TODO Auto-generated method stub - return null; - } - - - @Override - public void codegen(ClassFile classfile, Vector paralist) - throws JVMCodeException { - // TODO Auto-generated method stub - - } - /* - public Type getType() { - return this.type; - } - public void setType(Type type) { - this.type = type; - } - */ - - @Override - public String toString() - { - return super.toString() + "=" + getWert().toString(); - } - - - public JavaCodeResult printJavaCode(ResultSet resultSet) { - JavaCodeResult ret = new JavaCodeResult(); - ret.attach(this.getType().printJavaCode(resultSet)).attach( " ").attach( this.getName()+" = ").attach(this.getWert().printJavaCode(resultSet) ).attach( ";"); - - return ret; - } - - @Override - public TypeAssumptions createTypeAssumptions(Class classmember) { - ////////////////////////////// - //Felder: - ////////////////////////////// - - TypeAssumptions assumptions = new TypeAssumptions(); - /* - * TODO: Der Feld-Assumption muss ein TPH als Typ hinzugefügt werden, falls er Typlos initialisiert wurde. Dies kann auch der Type-Algorithmus der Inst/FieldVar - Klasse machen. - * Wird das Feld mit einem Typ initialisiert so muss dieser auch in die Assumptions. - */ - if(this.getType() == null)this.setType(TypePlaceholder.fresh(this)); - assumptions.add(TypeAssumptions.createFieldVarAssumption(classmember.getName(), this.getName(), this.getType())); - classmember.get_ClassBody().addField(this); - return assumptions; - } - -} diff --git a/src/mycompiler/myclass/Method.java b/src/mycompiler/myclass/Method.java index 702402b4..58041076 100755 --- a/src/mycompiler/myclass/Method.java +++ b/src/mycompiler/myclass/Method.java @@ -662,7 +662,7 @@ public class Method extends Field implements ITypeReplacementListener, IItemWith @Override public Vector getChildren() { - Vector ret = super.getChildren(); + Vector ret = new Vector(); ret.add(this.block); return ret; } diff --git a/src/mycompiler/mystatement/Assign.java b/src/mycompiler/mystatement/Assign.java index 57d484cb..bc81c745 100755 --- a/src/mycompiler/mystatement/Assign.java +++ b/src/mycompiler/mystatement/Assign.java @@ -7,6 +7,7 @@ import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; +import mycompiler.SyntaxTreeNode; import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.CodeAttribute; import mycompiler.mybytecode.JVMCode; @@ -32,6 +33,7 @@ import mycompiler.mytypereconstruction.unify.Unify; import org.apache.log4j.Logger; + // ino.end import typinferenz.JavaCodeResult; import typinferenz.SingleConstraint; @@ -259,5 +261,17 @@ public class Assign extends Expr return ret; } + + + + + @Override + public Vector getChildren() { + Vector ret = new Vector(); + ret.add(this.expr1); + ret.add(this.expr2); + return ret; + } + } // ino.end diff --git a/src/mycompiler/mystatement/Binary.java b/src/mycompiler/mystatement/Binary.java index d8b9edc9..3bd3c40a 100755 --- a/src/mycompiler/mystatement/Binary.java +++ b/src/mycompiler/mystatement/Binary.java @@ -6,6 +6,7 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; +import mycompiler.SyntaxTreeNode; import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.CodeAttribute; import mycompiler.mybytecode.JVMCode; @@ -31,6 +32,7 @@ import org.apache.log4j.Logger; // ino.end + import sun.reflect.generics.reflectiveObjects.NotImplementedException; import typinferenz.ConstraintsSet; import typinferenz.JavaCodeResult; @@ -263,6 +265,13 @@ public class Binary extends BinaryExpr public JavaCodeResult printJavaCode(ResultSet resultSet) { throw new NotImplementedException(); } + + + + @Override + public Vector getChildren() { + throw new NotImplementedException(); + } } diff --git a/src/mycompiler/mystatement/Block.java b/src/mycompiler/mystatement/Block.java index 074d3410..ea072094 100755 --- a/src/mycompiler/mystatement/Block.java +++ b/src/mycompiler/mystatement/Block.java @@ -7,6 +7,7 @@ import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; +import mycompiler.SyntaxTreeNode; import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.CodeAttribute; import mycompiler.myclass.Class; @@ -30,6 +31,7 @@ import org.apache.log4j.Logger; // ino.end + import sun.reflect.generics.reflectiveObjects.NotImplementedException; import typinferenz.JavaCodeResult; import typinferenz.SingleConstraint; @@ -304,5 +306,16 @@ public class Block extends Statement return ret.attach("}"); } + + + + @Override + public Vector getChildren() { + Vector ret = new Vector(); + for(Statement st : this.get_Statement()){ + ret.add(st); + } + return ret; + } } // ino.end diff --git a/src/mycompiler/mystatement/IfStmt.java b/src/mycompiler/mystatement/IfStmt.java index b869eb90..c79d1529 100755 --- a/src/mycompiler/mystatement/IfStmt.java +++ b/src/mycompiler/mystatement/IfStmt.java @@ -7,6 +7,7 @@ import java.util.Hashtable; import java.util.Iterator; import java.util.Vector; +import mycompiler.SyntaxTreeNode; import mycompiler.mybytecode.ClassFile; import mycompiler.mybytecode.CodeAttribute; import mycompiler.mybytecode.JVMCode; @@ -40,6 +41,7 @@ import org.apache.log4j.Logger; // ino.end + import sun.reflect.generics.reflectiveObjects.NotImplementedException; import typinferenz.JavaCodeResult; import typinferenz.SingleConstraint; @@ -414,5 +416,16 @@ public class IfStmt extends Statement ret.attach("\n}"); return ret; } + + + + @Override + public Vector getChildren() { + Vector ret = new Vector(); + if(this.expr!=null)ret.add(this.expr); + if(this.else_block!=null)ret.add(this.else_block); + if(this.then_block!=null)ret.add(this.then_block); + return ret; + } } // ino.end diff --git a/src/mycompiler/mystatement/LocalOrFieldVar.java b/src/mycompiler/mystatement/LocalOrFieldVar.java index db6b5973..ebaf14a2 100755 --- a/src/mycompiler/mystatement/LocalOrFieldVar.java +++ b/src/mycompiler/mystatement/LocalOrFieldVar.java @@ -204,7 +204,7 @@ public class LocalOrFieldVar extends Expr public ConstraintsSet TYPEExpr(TypeAssumptions assumptions) { ConstraintsSet ret = new ConstraintsSet(); //gibt es eine Assumption für den die LocalOrFieldVar-Variablen, dann folgendes ausführen: - Type thisTypeAssumption = assumptions.getVarType(this.get_Name()); + Type thisTypeAssumption = assumptions.getVarType(this.get_Name(), this.getParentClass()); this.setType(thisTypeAssumption); //ret.add(new Constraint(thisTypeAssumption, this.getTypeVariable())); return ret; diff --git a/src/mycompiler/mystatement/MethodCall.java b/src/mycompiler/mystatement/MethodCall.java index 4296171a..c8d59e3b 100755 --- a/src/mycompiler/mystatement/MethodCall.java +++ b/src/mycompiler/mystatement/MethodCall.java @@ -241,7 +241,7 @@ public class MethodCall extends Expr parameterVector.addElement(fp); } pl.formalparameter=parameterVector; - meth.setReturnType(returnType); + meth.setType(returnType); meth.setParameterList(pl); meth.set_DeclId(new DeclId(assumption.getIdentifier())); return(meth); diff --git a/src/mycompiler/mytest/LambdaTest.java b/src/mycompiler/mytest/LambdaTest.java index dee04725..519886f7 100755 --- a/src/mycompiler/mytest/LambdaTest.java +++ b/src/mycompiler/mytest/LambdaTest.java @@ -98,7 +98,7 @@ public class LambdaTest { TypeAssumptions assumptions = result1.getInterferedClass().getPublicFieldAssumptions();// TypeAssumptions.getAssumptionsFor(classname); assertTrue("Fehler in Methode TypeAssumptions.getAssumptionsFor", assumptions!=null); for(String variable : variableTypeAssumptions.keySet()){ - Type assumedType = assumptions.getVarType(variable); + Type assumedType = assumptions.getVarType(variable, result1.getInterferedClass()); assertTrue("Der Variable muss ein TPH zugewiesen sein.", assumedType!=null && assumedType instanceof TypePlaceholder); //AssumedType auflösen: diff --git a/src/typinferenz/assumptions/TypeAssumptions.java b/src/typinferenz/assumptions/TypeAssumptions.java index 8dc9562a..3c1ce434 100755 --- a/src/typinferenz/assumptions/TypeAssumptions.java +++ b/src/typinferenz/assumptions/TypeAssumptions.java @@ -1,6 +1,7 @@ package typinferenz.assumptions; import java.util.Iterator; +import mycompiler.myclass.Class; import java.util.Vector; import sun.reflect.generics.reflectiveObjects.NotImplementedException; @@ -145,9 +146,10 @@ public class TypeAssumptions { * Durchsucht die Assumptions des aktuellen Gültigkeitsbereichs (lokale Variablen und Felder der aktuellen Klasse) nach einem Eintrag für den übergebenen Variablennamen. * In den Assumptions wird dann in der Reihenfolge LocalVarAssumptions, FieldAssumption nach dem übergebenen Variablennamen gesucht. * @param variableName - der Identifier der gesuchten Variablen - * @return - Der Typ für diesen Identifier. + * @param inScope - Sucht auch die Felder der übergebenen Klasse ab. Hier kann auch null übergeben werden, dann werden nur die Lokalen Variablen dieser TypeAssumption durchsucht. + * @return - Der Typ für diesen Identifier */ - public Type getVarType(String variableName){ + public Type getVarType(String variableName, Class inScope){ //Zuerst die Parameter durchsuchen for(ParameterAssumption ass : this.parameterAssumptions){//this.getParameterAssumptions(null)){ if(ass.getIdentifier().equals(variableName))return ass.getAssumedType(); @@ -158,8 +160,8 @@ public class TypeAssumptions { } //und zuletzt die Felder der Klasse in dessen Namensraum sich dieses AssumptionSet befindet. - if(this.thisClassName!=null){ - for(FieldAssumption ass : this.getAssumptionsFor(this.thisClassName)){ + if(inScope!=null){ + for(FieldAssumption ass : this.getAssumptionsFor(inScope.getName())){ if(ass.getIdentifier().equals(variableName) && ass instanceof FieldAssumption)return ass.getAssumedType(); } } @@ -246,3 +248,4 @@ public class TypeAssumptions { } } + diff --git a/test/mycompiler/test/lambda/ConstructorTest.java b/test/mycompiler/test/lambda/ConstructorTest.java index 2da4e867..b1ab6b91 100755 --- a/test/mycompiler/test/lambda/ConstructorTest.java +++ b/test/mycompiler/test/lambda/ConstructorTest.java @@ -37,7 +37,7 @@ public class ConstructorTest { anzahlGeparsterKlassen++; }else if(cl.getName().equals("ConstructorTest2")){ anzahlGeparsterKlassen++; - assertTrue("ConstructorTest2.var muss vom Typ ConstructorTest1 sein.", ass.getVarType("var").printJavaCode( + assertTrue("ConstructorTest2.var muss vom Typ ConstructorTest1 sein.", ass.getVarType("var",cl).printJavaCode( res.getUnifiedConstraints()).toString().equals("ConstructorTest1")); }else fail("Die geparsten Klassen müssen ConstructorTest1 und ConstructorTest2 heißen"); } diff --git a/test/mycompiler/test/lambda/ParseMultipleFilesTest.java b/test/mycompiler/test/lambda/ParseMultipleFilesTest.java index b912e06f..cebc2264 100755 --- a/test/mycompiler/test/lambda/ParseMultipleFilesTest.java +++ b/test/mycompiler/test/lambda/ParseMultipleFilesTest.java @@ -33,10 +33,10 @@ public class ParseMultipleFilesTest { TypeAssumptions ass = cl.getPublicFieldAssumptions(); if(cl.getName().equals("Klasse1")){ anzahlGeparsterKlassen++; - assertTrue("Klasse1.var1 muss vom Typ int sein.", ass.getVarType("var1").printJavaCode(res.getUnifiedConstraints()).toString().equals("int")); + assertTrue("Klasse1.var1 muss vom Typ int sein.", ass.getVarType("var1",cl).printJavaCode(res.getUnifiedConstraints()).toString().equals("int")); }else if(cl.getName().equals("Klasse2")){ anzahlGeparsterKlassen++; - assertTrue("Klasse1.var1 muss vom Typ Klasse1 sein.", ass.getVarType("var1").printJavaCode(res.getUnifiedConstraints()).toString().equals("Klasse1")); + assertTrue("Klasse1.var1 muss vom Typ Klasse1 sein.", ass.getVarType("var1",cl).printJavaCode(res.getUnifiedConstraints()).toString().equals("Klasse1")); }else fail("Die geparsten Klassen müssen Klasse1 und Klasse2 heißen"); } assertTrue("Es müssen insgesamt 2 Klassen inferierd worden sein", anzahlGeparsterKlassen == 2); diff --git a/test/mycompiler/test/lambda/testResults/LambdaTest.log b/test/mycompiler/test/lambda/testResults/LambdaTest.log index 57d84f71..7421bb94 100644 --- a/test/mycompiler/test/lambda/testResults/LambdaTest.log +++ b/test/mycompiler/test/lambda/testResults/LambdaTest.log @@ -1,11 +1,11 @@ Class DEBUG [Typeinference] Erstellte Assumptions: this: WhileTestMethod Assumptions: -[typinferenz.assumptions.MethodAssumption@52f37721, typinferenz.assumptions.MethodAssumption@6e124212] +[typinferenz.assumptions.MethodAssumption@53be6b71, typinferenz.assumptions.MethodAssumption@7a7ff2aa] FieldVar Assumptions: -[typinferenz.assumptions.FieldAssumption@70afe432] +[typinferenz.assumptions.FieldAssumption@6614b5ba] LocalVar Assumptions: [] Parameter Assumptions: [] -Block DEBUG [Typeinference] Prozessing statement: WHILE null { [(var = mycompiler.mystatement.StringLiteral@2a2d0d78)] -Block DEBUG [Typeinference] Prozessing statement: (var = mycompiler.mystatement.StringLiteral@2a2d0d78) +Block DEBUG [Typeinference] Prozessing statement: WHILE null { [(var = mycompiler.mystatement.StringLiteral@49940c14)] +Block DEBUG [Typeinference] Prozessing statement: (var = mycompiler.mystatement.StringLiteral@49940c14)