code generator hinzugefügt

This commit is contained in:
sebastian 2017-03-19 11:41:22 +01:00
parent 5d4ea4bd69
commit 89d1b97339
12 changed files with 269 additions and 38 deletions

View File

@ -1,5 +1,43 @@
package de.dhbwstuttgart.strucTypes; package de.dhbwstuttgart.strucTypes;
import java.util.List;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
public class AS_Abstract { public class AS_Abstract {
public static AS_Field find_AS_Field(Field f, List<AS_Abstract> 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<AS_Abstract> 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;
}
} }

View File

@ -7,8 +7,8 @@ import de.dhbwstuttgart.syntaxtree.Method;
public class AS_Argument extends AS_Abstract { public class AS_Argument extends AS_Abstract {
FormalParameter fp; public FormalParameter fp;
TV_Alpha tv_alpha; 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() { public String toString() {
return String.format("Assumption: { %s : %s } )" , fp.getName() , tv_alpha.toString() ); return String.format("Assumption: { %s : %s } )" , fp.getName() , tv_alpha.toString() );
} }
} }

View File

@ -18,6 +18,15 @@ public class AS_Field extends AS_Abstract {
} }
public Field get_Field() {
return field;
}
public String get_code() {
return String.format("%s : %s", tv_field.toString() , field.getName() );
}
public String toString() { public String toString() {
String klname = cl.getClassName().toString(); String klname = cl.getClassName().toString();
@ -28,4 +37,6 @@ public class AS_Field extends AS_Abstract {
} }

View File

@ -0,0 +1,16 @@
package de.dhbwstuttgart.strucTypes;
public class Algo_Static {
public static String get_Assumption() {
return "Nothing";
}
}

View File

@ -36,37 +36,50 @@ public class Algo_Type {
// -> For (m_strich) {return e; } Element Methoden
// für alle Methoden der Klasse //List<AS_Argument> ass_arguments = new ArrayList<AS_Argument>();
List<AS_Argument> ass_arguments = new ArrayList<AS_Argument>();
for (Method m : cl.getMethods()) { for (Method m : cl.getMethods()) {
// -> Ass = AssAll u { ....
for (int i = 0; i < m.getParameterList().getFormalparalist().size() ; i++ ) { 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 ); AS_Argument as_argument = new AS_Argument(m.getParameterList().getFormalparalist().get(i), i , m , cl );
ass_arguments.add(as_argument); assAll.add(as_argument);
}
} }
// ASS = AssAll vereinigt mit ass_arguments
assAll.addAll(ass_arguments); // -> (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 (wird oben erledigt)
//-> assAll.addAll(ass_arguments);
//Beispielhafte Ausgabe der Assumptions // (Test) Beispielhafte Ausgabe der Assumptions
for (AS_Abstract as : assAll ) { for (AS_Abstract as : assAll ) {
System.out.println(as.toString()); 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 muss noch ausgeführt werden
// TypeExpression (insert Types and collects corrospending constraints) // TypeExpression (insert Types and collects corrospending constraints)
} }
// fass // =:fass
private List<AS_Field> make_field_assumptions(ClassOrInterface cl) { private List<AS_Field> make_field_assumptions(ClassOrInterface cl) {
List<AS_Field> fass = new ArrayList<AS_Field>(); List<AS_Field> fass = new ArrayList<AS_Field>();
@ -75,11 +88,10 @@ public class Algo_Type {
AS_Field as_f = new AS_Field(cl, f) ; AS_Field as_f = new AS_Field(cl, f) ;
fass.add(as_f); fass.add(as_f);
} }
return fass; return fass;
} }
// mass // =:mass
private List<AS_Method> make_method_assumptions(ClassOrInterface cl) { private List<AS_Method> make_method_assumptions(ClassOrInterface cl) {
List<AS_Method> mass = new ArrayList<AS_Method>(); List<AS_Method> mass = new ArrayList<AS_Method>();
@ -88,14 +100,12 @@ public class Algo_Type {
AS_Method as_m = new AS_Method(cl,m); AS_Method as_m = new AS_Method(cl,m);
mass.add(as_m); mass.add(as_m);
} }
return mass; return mass;
} }
// TypeExpression

View File

@ -3,36 +3,70 @@ package de.dhbwstuttgart.strucTypes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement; import de.dhbwstuttgart.syntaxtree.statement.Statement;
public class Algo_TypeExpr { public class Algo_TypeExpr {
List<TC_Abstract> res_constriant_set; // Ergebnisse: Expression Typed und Constraint-Set
List<TC_Abstract> res_constriant_set;
//TODO Hier muss noch ein passendes Datenformat gefunden werden //TODO Hier muss noch ein passendes Datenformat gefunden werden
String res_expression_typed; String res_expression_typed;
// übergabe der assumptions und der Statments der Methode // übergabe der assumptions und der Statments der Methode
public Algo_TypeExpr(List<AS_Abstract> ass , List<Statement> expression ) { public Algo_TypeExpr(List<AS_Abstract> ass , List<Statement> statements ) {
this.res_constriant_set = new ArrayList<TC_Abstract>(); this.res_constriant_set = new ArrayList<TC_Abstract>();
res_expression_typed = "";
/*
* Wenn erstes element Return statement ist da
*/ // 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
// 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...

View File

@ -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<AS_Abstract> 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<AS_Abstract> 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<AS_Abstract> ass ) {
String code = "";
return code;
}
public static String generateMethod(Method m, List<AS_Abstract> ass ) {
String code = m.getName() + " ( " ;
// Generate Formalparameter
List<FormalParameter> 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;
}
}

View File

@ -43,4 +43,12 @@ public class Method extends Field implements IItemWithOffset
public ParameterList getParameterList() { public ParameterList getParameterList() {
return parameterlist; return parameterlist;
} }
//erweitert Sebastian Ritt
public Block get_Block() {
return block;
}
} }

View File

@ -26,4 +26,11 @@ public class LocalVar extends Statement{
public ConstraintSet getConstraints(TypeInferenceInformation info) { public ConstraintSet getConstraints(TypeInferenceInformation info) {
return new ConstraintSet(); return new ConstraintSet();
} }
// @ Sebastian
public String get_expression() {
return expression;
}
} }

View File

@ -22,4 +22,11 @@ public class Return extends Statement
public ConstraintSet getConstraints(TypeInferenceInformation info) { public ConstraintSet getConstraints(TypeInferenceInformation info) {
return retexpr.getConstraints(info); return retexpr.getConstraints(info);
} }
// @Sebastian
public Expression get_Expression() {
return retexpr;
}
} }

View File

@ -41,7 +41,7 @@ public class GeneralParserTest{
//filenames.add("FieldVarTest.jav"); //filenames.add("FieldVarTest.jav");
//filenames.add("StructuralTypesField.jav"); //filenames.add("StructuralTypesField.jav");
filenames.add("StructuralTypesSimple.jav"); filenames.add("StructuralTypesSimple2.jav");
JavaTXParser parser = new JavaTXParser(); JavaTXParser parser = new JavaTXParser();
try{ try{
@ -53,9 +53,13 @@ public class GeneralParserTest{
SourceFile sfdebug = sf; SourceFile sfdebug = sf;
List<AS_Abstract> ass = new ArrayList<AS_Abstract>(); List<AS_Abstract> ass = new ArrayList<AS_Abstract>();
Algo_Type type = new Algo_Type( ass , sf.getClasses().get(0)); Algo_Type type = new Algo_Type( ass , sf.getClasses().get(0));
// Erstellen der TypeExpression // Erstellen der TypeExpression