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;
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<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 {
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() );
}
}

View File

@ -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 {
}

View File

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

View File

@ -35,38 +35,51 @@ public class Algo_Type {
// für alle Methoden der Klasse
List<AS_Argument> ass_arguments = new ArrayList<AS_Argument>();
// -> For (m_strich) {return e; } Element Methoden
//List<AS_Argument> ass_arguments = new ArrayList<AS_Argument>();
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<AS_Field> make_field_assumptions(ClassOrInterface cl) {
List<AS_Field> fass = new ArrayList<AS_Field>();
@ -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<AS_Method> make_method_assumptions(ClassOrInterface cl) {
List<AS_Method> mass = new ArrayList<AS_Method>();
@ -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

View File

@ -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<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
String res_expression_typed;
// ü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>();
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...

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() {
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) {
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) {
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("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<AS_Abstract> ass = new ArrayList<AS_Abstract>();
Algo_Type type = new Algo_Type( ass , sf.getClasses().get(0));
// Erstellen der TypeExpression

View File

@ -5,7 +5,7 @@ class A {
A f2;
mt1(x) { return x; }
mt1(x) { return x; }
mt2(x) { return x.feld ; }