From 89d1b973394aeea4ceb94edc3d3c394c6cf59874 Mon Sep 17 00:00:00 2001 From: sebastian Date: Sun, 19 Mar 2017 11:41:22 +0100 Subject: [PATCH] =?UTF-8?q?code=20generator=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dhbwstuttgart/strucTypes/AS_Abstract.java | 40 ++++++++- .../dhbwstuttgart/strucTypes/AS_Argument.java | 10 ++- src/de/dhbwstuttgart/strucTypes/AS_Field.java | 11 +++ .../dhbwstuttgart/strucTypes/Algo_Static.java | 16 ++++ .../dhbwstuttgart/strucTypes/Algo_Type.java | 48 ++++++---- .../strucTypes/Algo_TypeExpr.java | 62 ++++++++++--- .../strucTypes/CodeGenerator.java | 90 +++++++++++++++++++ src/de/dhbwstuttgart/syntaxtree/Method.java | 8 ++ .../syntaxtree/statement/LocalVar.java | 7 ++ .../syntaxtree/statement/Return.java | 7 ++ test/strucTypes/GeneralParserTest.java | 6 +- test/strucTypes/StructuralTypesSimple2.jav | 2 +- 12 files changed, 269 insertions(+), 38 deletions(-) create mode 100644 src/de/dhbwstuttgart/strucTypes/Algo_Static.java create mode 100644 src/de/dhbwstuttgart/strucTypes/CodeGenerator.java diff --git a/src/de/dhbwstuttgart/strucTypes/AS_Abstract.java b/src/de/dhbwstuttgart/strucTypes/AS_Abstract.java index b7e0bd28..f847fc33 100644 --- a/src/de/dhbwstuttgart/strucTypes/AS_Abstract.java +++ b/src/de/dhbwstuttgart/strucTypes/AS_Abstract.java @@ -1,5 +1,43 @@ package de.dhbwstuttgart.strucTypes; -public class AS_Abstract { +import java.util.List; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.FormalParameter; + +public class AS_Abstract { + + + + public static AS_Field find_AS_Field(Field f, List ass) { + AS_Field res = null; + + for (AS_Abstract as : ass) { + if (as.getClass().equals(AS_Field.class)) { + AS_Field asf = (AS_Field) as; + if (f == asf.get_Field() ) { + return asf; + } + } + } + return res; + } + + + + + + public static AS_Argument find_AS_Argument(FormalParameter f , List ass ) { + AS_Argument res = null; + + for (AS_Abstract as : ass) { + if ( as.getClass().equals(AS_Argument.class) ) { + AS_Argument asa = (AS_Argument) as; + if (f == asa.fp) { + res = asa; + } + } + } + return res; + } } diff --git a/src/de/dhbwstuttgart/strucTypes/AS_Argument.java b/src/de/dhbwstuttgart/strucTypes/AS_Argument.java index 93019e26..db26ef95 100644 --- a/src/de/dhbwstuttgart/strucTypes/AS_Argument.java +++ b/src/de/dhbwstuttgart/strucTypes/AS_Argument.java @@ -7,8 +7,8 @@ import de.dhbwstuttgart.syntaxtree.Method; public class AS_Argument extends AS_Abstract { - FormalParameter fp; - TV_Alpha tv_alpha; + public FormalParameter fp; + public TV_Alpha tv_alpha; @@ -19,10 +19,16 @@ public class AS_Argument extends AS_Abstract { } + public String get_code() { + return String.format("%s : %s ", tv_alpha.toString() , fp.getName()); + } + + public String toString() { return String.format("Assumption: { %s : %s } )" , fp.getName() , tv_alpha.toString() ); } + } diff --git a/src/de/dhbwstuttgart/strucTypes/AS_Field.java b/src/de/dhbwstuttgart/strucTypes/AS_Field.java index 1a0c32b2..7d6528cd 100644 --- a/src/de/dhbwstuttgart/strucTypes/AS_Field.java +++ b/src/de/dhbwstuttgart/strucTypes/AS_Field.java @@ -17,6 +17,15 @@ public class AS_Field extends AS_Abstract { this.tv_field = new TV_Field(cl,field); } + + public Field get_Field() { + return field; + } + + + public String get_code() { + return String.format("%s : %s", tv_field.toString() , field.getName() ); + } public String toString() { @@ -27,5 +36,7 @@ public class AS_Field extends AS_Abstract { + + } diff --git a/src/de/dhbwstuttgart/strucTypes/Algo_Static.java b/src/de/dhbwstuttgart/strucTypes/Algo_Static.java new file mode 100644 index 00000000..f66c4880 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/Algo_Static.java @@ -0,0 +1,16 @@ +package de.dhbwstuttgart.strucTypes; + +public class Algo_Static { + + + public static String get_Assumption() { + + + + + return "Nothing"; + + } + + +} diff --git a/src/de/dhbwstuttgart/strucTypes/Algo_Type.java b/src/de/dhbwstuttgart/strucTypes/Algo_Type.java index 75ac00f0..f66e9e77 100644 --- a/src/de/dhbwstuttgart/strucTypes/Algo_Type.java +++ b/src/de/dhbwstuttgart/strucTypes/Algo_Type.java @@ -35,38 +35,51 @@ public class Algo_Type { - - - // für alle Methoden der Klasse - List ass_arguments = new ArrayList(); + + // -> For (m_strich) {return e; } Element Methoden + //List ass_arguments = new ArrayList(); for (Method m : cl.getMethods()) { + + // -> Ass = AssAll u { .... for (int i = 0; i < m.getParameterList().getFormalparalist().size() ; i++ ) { AS_Argument as_argument = new AS_Argument(m.getParameterList().getFormalparalist().get(i), i , m , cl ); - ass_arguments.add(as_argument); + assAll.add(as_argument); } + + + // -> (e_typed, C' = TypeExpre(Ass,e) ) + Algo_TypeExpr algo = new Algo_TypeExpr(assAll, m.get_Block().getStatements()); + System.out.println("getypte expression: "+ algo.res_expression_typed); } - // ASS = AssAll vereinigt mit ass_arguments - assAll.addAll(ass_arguments); + // ASS = AssAll vereinigt mit ass_arguments (wird oben erledigt) + //-> assAll.addAll(ass_arguments); - //Beispielhafte Ausgabe der Assumptions + // (Test) Beispielhafte Ausgabe der Assumptions for (AS_Abstract as : assAll ) { System.out.println(as.toString()); } + + + // TEST: Ausgabe des Code + System.out.println("Code: "); + System.out.println(CodeGenerator.generateClass(cl, assAll)); - - - // TypeExpression muss noch ausgeführt werden // TypeExpression (insert Types and collects corrospending constraints) - + + + + + + } - // fass + // =:fass private List make_field_assumptions(ClassOrInterface cl) { List fass = new ArrayList(); @@ -74,12 +87,11 @@ public class Algo_Type { for (Field f : cl.getFieldDecl() ) { AS_Field as_f = new AS_Field(cl, f) ; fass.add(as_f); - } - + } return fass; } - // mass + // =:mass private List make_method_assumptions(ClassOrInterface cl) { List mass = new ArrayList(); @@ -87,15 +99,13 @@ public class Algo_Type { for(Method m : cl.getMethods() ) { AS_Method as_m = new AS_Method(cl,m); mass.add(as_m); - } - + } return mass; } - // TypeExpression diff --git a/src/de/dhbwstuttgart/strucTypes/Algo_TypeExpr.java b/src/de/dhbwstuttgart/strucTypes/Algo_TypeExpr.java index 475d930a..c88f49de 100644 --- a/src/de/dhbwstuttgart/strucTypes/Algo_TypeExpr.java +++ b/src/de/dhbwstuttgart/strucTypes/Algo_TypeExpr.java @@ -3,36 +3,70 @@ package de.dhbwstuttgart.strucTypes; import java.util.ArrayList; import java.util.List; +import de.dhbwstuttgart.syntaxtree.statement.LocalVar; +import de.dhbwstuttgart.syntaxtree.statement.Return; import de.dhbwstuttgart.syntaxtree.statement.Statement; public class Algo_TypeExpr { - List res_constriant_set; + // Ergebnisse: Expression Typed und Constraint-Set + List res_constriant_set; //TODO Hier muss noch ein passendes Datenformat gefunden werden String res_expression_typed; + // übergabe der assumptions und der Statments der Methode - public Algo_TypeExpr(List ass , List expression ) { + public Algo_TypeExpr(List ass , List statements ) { this.res_constriant_set = new ArrayList(); + res_expression_typed = ""; + + + + // Methode enthält kein Statement + if (statements.size() == 0 ) { + System.out.println("Not Implemmented"); + } + + + + // Methode enthält nur ein Return statment das ein feld zurückgibt + if (statements.size() == 1) { + if (statements.get(0).getClass().equals(Return.class)) { + Return exp_rt = (Return) statements.get(0); + if (exp_rt.get_Expression().getClass().equals(LocalVar.class )) { + LocalVar lvar = (LocalVar) exp_rt.get_Expression(); + + String feldname = lvar.get_expression(); + + for (AS_Abstract as : ass) { + if (as.getClass().equals(AS_Argument.class)) { + AS_Argument as_arg = (AS_Argument) as; + if (as_arg.fp.getName().equals(feldname)) { + res_expression_typed = feldname + " : " + as_arg.tv_alpha.toString(); + // res_constraint_set + } + } + } + } + } + } + + + // Methode nur ein Return statment das ein feld zurückgibt und eine Methode aufruft + + + //-> Es muss so sein dass ich diese Return Statement beurteilen kann + + + - /* - * Wenn erstes element Return statement ist da - */ + - // Fall 1 (Ass , x ) Expression ist lediglich ein Argument - - // ContraintSet ist leer; - - // Expression Typed? - - // Suche in den Assumptions nach der passenden Assumption - - // this.res_expression_typed = x : alpha... diff --git a/src/de/dhbwstuttgart/strucTypes/CodeGenerator.java b/src/de/dhbwstuttgart/strucTypes/CodeGenerator.java new file mode 100644 index 00000000..61576c09 --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/CodeGenerator.java @@ -0,0 +1,90 @@ +package de.dhbwstuttgart.strucTypes; + +import java.util.List; + +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.FormalParameter; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.SourceFile; + +public class CodeGenerator { + + + public static String generate(SourceFile sf, List ass) { + String code = ""; + + // Generate Classes + for (ClassOrInterface cl : sf.getClasses()) { + String code_class = generateClass(cl,ass); + code = code + code_class; + } + return code; + } + + + + + public static String generateClass(ClassOrInterface cl, List ass ) { + String code = "class " + cl.getClassName() + " { \n \n"; + + // Felder + for (Field f : cl.getFieldDecl()) { + String code_field = AS_Abstract.find_AS_Field(f, ass).get_code(); + code = code + code_field + " \n"; + } + + code = code + "\n \n"; + + + // Methoden + for (Method m : cl.getMethods() ) { + String m_string = generateMethod(m, ass); + code = code + m_string + "\n \n"; + } + return code + "\n } "; + } + + + + + public static String generateField(SourceFile sf, List ass ) { + String code = ""; + + return code; + } + + + + public static String generateMethod(Method m, List ass ) { + String code = m.getName() + " ( " ; + + // Generate Formalparameter + List fp = m.getParameterList().getFormalparalist(); + for (FormalParameter f : fp ) { + AS_Argument arg = AS_Abstract.find_AS_Argument(f, ass); + code = code + arg.get_code() + ", "; + } + + if (fp.size() > 0) { + code = code.substring(0, code.length() -2); + } + + code = code + " ) { \n block return expression } " ; + + + + + + + return code; + } + + + + + + + + +} diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 7d18c634..636e4ee7 100755 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -43,4 +43,12 @@ public class Method extends Field implements IItemWithOffset public ParameterList getParameterList() { return parameterlist; } + + + //erweitert Sebastian Ritt + public Block get_Block() { + return block; + } + + } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java index 55072f00..a02a4e2f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java @@ -26,4 +26,11 @@ public class LocalVar extends Statement{ public ConstraintSet getConstraints(TypeInferenceInformation info) { return new ConstraintSet(); } + + // @ Sebastian + public String get_expression() { + return expression; + } + + } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java index dfe6661d..010e2c9a 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java @@ -22,4 +22,11 @@ public class Return extends Statement public ConstraintSet getConstraints(TypeInferenceInformation info) { return retexpr.getConstraints(info); } + + // @Sebastian + public Expression get_Expression() { + return retexpr; + } + + } diff --git a/test/strucTypes/GeneralParserTest.java b/test/strucTypes/GeneralParserTest.java index 1a2e6fc1..c9989214 100644 --- a/test/strucTypes/GeneralParserTest.java +++ b/test/strucTypes/GeneralParserTest.java @@ -41,7 +41,7 @@ public class GeneralParserTest{ //filenames.add("FieldVarTest.jav"); //filenames.add("StructuralTypesField.jav"); - filenames.add("StructuralTypesSimple.jav"); + filenames.add("StructuralTypesSimple2.jav"); JavaTXParser parser = new JavaTXParser(); try{ @@ -52,10 +52,14 @@ public class GeneralParserTest{ SourceFile sf = parser.parse(new File(rootDirectory + filename)); SourceFile sfdebug = sf; + List ass = new ArrayList(); Algo_Type type = new Algo_Type( ass , sf.getClasses().get(0)); + + + // Erstellen der TypeExpression diff --git a/test/strucTypes/StructuralTypesSimple2.jav b/test/strucTypes/StructuralTypesSimple2.jav index 6d641fd1..a618c63e 100644 --- a/test/strucTypes/StructuralTypesSimple2.jav +++ b/test/strucTypes/StructuralTypesSimple2.jav @@ -5,7 +5,7 @@ class A { A f2; - mt1(x) { return x; } + mt1(x) { return x; } mt2(x) { return x.feld ; }