Dump
This commit is contained in:
parent
4645aa0d69
commit
86ee358528
src/de/dhbwstuttgart
strucTypes
AS_Abstract.javaAS_Field.javaAlgo_Type.javaAlgo_TypeExpr.javaCodeGenerator.javaTC_Field.javaTC_Method.javaTV_ReturnTyp.javaTV_ReturnType2.java
syntaxtree/statement
test/strucTypes
@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.Field;
|
||||
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
||||
import de.dhbwstuttgart.syntaxtree.Method;
|
||||
|
||||
public class AS_Abstract {
|
||||
|
||||
@ -39,5 +40,33 @@ public class AS_Abstract {
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static AS_Argument find_AS_Argument_by_Name(String name, List<AS_Abstract> ass ) {
|
||||
|
||||
// if
|
||||
AS_Argument result = null;
|
||||
|
||||
// else Find Assumption
|
||||
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(name)) {
|
||||
result = as_arg;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static AS_Argument find_AS_Argument_by_Name(String name, Method m, List<AS_Abstract> ass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -12,8 +12,7 @@ public class AS_Field extends AS_Abstract {
|
||||
public AS_Field (ClassOrInterface cl, Field field) {
|
||||
super();
|
||||
this.cl = cl;
|
||||
this.field = field;
|
||||
|
||||
this.field = field;
|
||||
this.tv_field = new TV_Field(cl,field);
|
||||
|
||||
}
|
||||
@ -26,6 +25,10 @@ public class AS_Field extends AS_Abstract {
|
||||
public String get_code() {
|
||||
return String.format("%s : %s", tv_field.toString() , field.getName() );
|
||||
}
|
||||
|
||||
public TV_Field get_TV_Field() {
|
||||
return this.tv_field;
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
|
@ -35,20 +35,28 @@ public class Algo_Type {
|
||||
|
||||
|
||||
|
||||
// -> AssGlobal = fass + mass + Assumption der Methode
|
||||
List<AS_Abstract> assGlobal = new ArrayList<>();
|
||||
assGlobal.addAll(assAll);
|
||||
|
||||
|
||||
|
||||
// -> 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++ ) {
|
||||
// Neue Liste sodass die Argumente der Methode nur für diese gelten
|
||||
List<AS_Abstract> ass2 = new ArrayList<AS_Abstract>();
|
||||
ass2.addAll(assAll);
|
||||
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 );
|
||||
assAll.add(as_argument);
|
||||
}
|
||||
|
||||
ass2.add(as_argument);
|
||||
}
|
||||
assGlobal.addAll(ass2);
|
||||
|
||||
// -> (e_typed, C' = TypeExpre(Ass,e) )
|
||||
Algo_TypeExpr algo = new Algo_TypeExpr(assAll, m.get_Block().getStatements());
|
||||
Algo_TypeExpr algo = new Algo_TypeExpr(ass2, m.get_Block().getStatements());
|
||||
System.out.println("getypte expression: "+ algo.res_expression_typed);
|
||||
}
|
||||
|
||||
@ -57,18 +65,25 @@ public class Algo_Type {
|
||||
|
||||
|
||||
// (Test) Beispielhafte Ausgabe der Assumptions
|
||||
for (AS_Abstract as : assAll ) {
|
||||
for (AS_Abstract as : assGlobal ) {
|
||||
System.out.println(as.toString());
|
||||
}
|
||||
|
||||
|
||||
// TEST: Ausgabe des Code
|
||||
System.out.println("Code: ");
|
||||
System.out.println(CodeGenerator.generateClass(cl, assAll));
|
||||
System.out.println(CodeGenerator.generateClass(cl, assGlobal));
|
||||
|
||||
|
||||
// TypeExpression muss noch ausgeführt werden
|
||||
// TypeExpression (insert Types and collects corrospending constraints)
|
||||
List<TC_Abstract> constraints = Algo_TypeExpr.generateConstraint(cl, assGlobal);
|
||||
System.out.println("Constraints generiert");
|
||||
for (TC_Abstract tc : constraints ) {
|
||||
System.out.println(tc.toString());
|
||||
// Hier ist ein Fehler -> der Contraint bezieht sich auf die Expression
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -3,6 +3,12 @@ package de.dhbwstuttgart.strucTypes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver;
|
||||
|
||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||
import de.dhbwstuttgart.syntaxtree.Field;
|
||||
import de.dhbwstuttgart.syntaxtree.Method;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
|
||||
@ -12,13 +18,29 @@ import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
||||
|
||||
public class Algo_TypeExpr {
|
||||
|
||||
// Ergebnisse: Expression Typed und Constraint-Set
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* Hier sind die Ergebnisse
|
||||
*
|
||||
*
|
||||
*/
|
||||
// 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;
|
||||
|
||||
|
||||
|
||||
|
||||
public Algo_TypeExpr() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// übergabe der assumptions und der Statments der Methode
|
||||
public Algo_TypeExpr(List<AS_Abstract> ass , List<Statement> statements ) {
|
||||
|
||||
@ -68,6 +90,11 @@ public class Algo_TypeExpr {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Methode generiert Quellcode aus einer Methode
|
||||
* Letztendlich wird in der Methode der gesamte Datentyp durchwandert und erstellt
|
||||
* Nach dem selben Schema muss auch die generierung der Constriants erfolgen:
|
||||
*/
|
||||
public static String expression2code(List<AS_Abstract> ass , List<Statement> statements ) {
|
||||
String res = "";
|
||||
|
||||
@ -88,8 +115,7 @@ public class Algo_TypeExpr {
|
||||
}
|
||||
|
||||
|
||||
public static String statement2code(List<AS_Abstract> ass , Statement statement) {
|
||||
|
||||
public static String statement2code(List<AS_Abstract> ass , Statement statement) {
|
||||
|
||||
// TODO: Else if
|
||||
|
||||
@ -109,15 +135,210 @@ public class Algo_TypeExpr {
|
||||
MethodCall mtcall = (MethodCall) statement;
|
||||
Statement receiver = (Statement) mtcall.get_Receiver().get_Expression();
|
||||
String str = statement2code(ass,receiver);
|
||||
return str + "."+ mtcall.get_Name() + "()";
|
||||
}
|
||||
|
||||
ArgumentList list = mtcall.get_ArgList();
|
||||
String arg_str = "";
|
||||
for (Expression exp : list.getArguments()) {
|
||||
// TODO Cast evtl ist falsch
|
||||
Statement st = (Statement) exp;
|
||||
arg_str = arg_str + statement2code(ass,st);
|
||||
}
|
||||
// Todo Implementing Types:
|
||||
return str + "."+ mtcall.get_Name() + "( " + arg_str + " )";
|
||||
}
|
||||
return "Fehler";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public List<TC_Abstract> generate(List<AS_Abstract> ass, List<Statement> exp) {
|
||||
|
||||
// Methoden enhält keine Statements
|
||||
if (exp.size() == 0) {
|
||||
this.res_constriant_set = null;
|
||||
this.res_expression_typed = null;
|
||||
}
|
||||
|
||||
|
||||
// Methoden enthält ein Return Statement
|
||||
if (exp.size() == 1) {
|
||||
Statement std = (Statement) exp.get(0);
|
||||
if (std.getClass() == Return.class) {
|
||||
Return ret = (Return) std;
|
||||
generate_return(ass, ret);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public List<TC_Abstract> generate_return(List<AS_Abstract> ass, Return ret) {
|
||||
|
||||
Expression exp = ret.get_Expression();
|
||||
|
||||
|
||||
if (exp.getClass() == LocalVar.class) {
|
||||
LocalVar lvar = (LocalVar) exp;
|
||||
// TODO wie soll ich hier mit der Language umgehen
|
||||
return new ArrayList<TC_Abstract>();
|
||||
}
|
||||
|
||||
|
||||
if (exp.getClass() == FieldVar.class) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (exp.getClass() == MethodCall.class) {
|
||||
MethodCall mt_call = (MethodCall) exp;
|
||||
|
||||
// Constraint
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public List<TC_Abstract> getRes_constriant_set() {
|
||||
return res_constriant_set;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getRes_expression_typed() {
|
||||
return res_expression_typed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Neuer Versuch mit neuer herangehensweise um den Typen zu berechnen
|
||||
/*
|
||||
* Es werden die Typen der Methoden berechnet und als String ausgegeben
|
||||
*/
|
||||
public static TV_Abstract get_rt_Type_of(Expression o, List<AS_Abstract> ass) {
|
||||
|
||||
if (o.getClass().equals(LocalVar.class)) {
|
||||
LocalVar v = (LocalVar) o;
|
||||
return get_rt_Type_of_LocalVar(v, ass);
|
||||
}
|
||||
else if (o.getClass().equals(MethodCall.class)) {
|
||||
MethodCall m_call = (MethodCall) o;
|
||||
return get_rt_Type_of_MethodCall(m_call, ass);
|
||||
}
|
||||
else if (o.getClass().equals(FieldVar.class)){
|
||||
// FieldVar ist noch nicht implementiert
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static TV_Alpha get_rt_Type_of_LocalVar(LocalVar v, List<AS_Abstract> ass) {
|
||||
AS_Argument arg = AS_Abstract.find_AS_Argument_by_Name(v.get_expression(), ass);
|
||||
return arg.tv_alpha;
|
||||
}
|
||||
|
||||
|
||||
public static TV_ReturnType2 get_rt_Type_of_MethodCall(MethodCall mt_call, List<AS_Abstract> ass) {
|
||||
TV_Abstract tv_receiver = get_rt_Type_of(mt_call.get_Receiver().get_Expression(), ass);
|
||||
TV_ReturnType2 res = new TV_ReturnType2(mt_call.get_Name(), tv_receiver.toString() );
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Neuer Versuch eine Methode berechneten Typen auszugeben
|
||||
public static String get_Return_typed(Method m, List<AS_Abstract> ass) {
|
||||
Return r = (Return) m.get_Block().statements.get(0);
|
||||
Expression ex = r.get_Expression();
|
||||
return get_rt_Type_of(ex,ass).toString();
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Erstellen der Constraints.
|
||||
// Auch dieses erstellen muss Rekursiv erfolgen
|
||||
// -------------------------------
|
||||
|
||||
public static List<TC_Abstract> generateConstraint(ClassOrInterface cl, List<AS_Abstract> ass ) {
|
||||
List<TC_Abstract> result = new ArrayList<>();
|
||||
|
||||
|
||||
// 1. Generate Field Constraints
|
||||
result.addAll(generateFieldConstraints(cl,ass));
|
||||
// 2. Generate Method Constraints
|
||||
result.addAll(generateMethodConstraints(cl,ass));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static List<TC_Field> generateFieldConstraints(ClassOrInterface cl, List<AS_Abstract> ass ) {
|
||||
List<TC_Field> result = new ArrayList<>();
|
||||
|
||||
for (Field f : cl.getFieldDecl()) {
|
||||
AS_Field as = (AS_Field) AS_Abstract.find_AS_Field(f, ass);
|
||||
TC_Field constraint = new TC_Field(cl,f, as.get_TV_Field());
|
||||
result.add(constraint);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Leider hat das erstellen der Constraints für die Methoden nicht mehr funktioniert
|
||||
* Aus dies muss Rekursiv erfolgen
|
||||
*/
|
||||
|
||||
public static List<TC_Method> generateMethodConstraints(ClassOrInterface cl, List<AS_Abstract> ass) {
|
||||
List<TC_Method> result = new ArrayList<>();
|
||||
|
||||
for(Method m : cl.getMethods()) {
|
||||
|
||||
Return r = (Return) m.get_Block().getStatements().get(0);
|
||||
Expression ex = r.get_Expression();
|
||||
if (ex.getClass().equals(LocalVar.class)) {
|
||||
// Kein Constraint
|
||||
}
|
||||
else if (ex.getClass().equals(FieldVar.class)){
|
||||
// Noch nicht implementiert
|
||||
|
||||
|
||||
}
|
||||
else if (ex.getClass().equals(MethodCall.class)) {
|
||||
MethodCall m_call = (MethodCall) ex;
|
||||
|
||||
|
||||
TC_Method tc = new TC_Method(cl,m);
|
||||
|
||||
result.add(tc);
|
||||
|
||||
|
||||
}
|
||||
else {
|
||||
// Darf nicht vorkommen
|
||||
System.out.println("Fehler - Type Expression");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -57,7 +57,12 @@ public class CodeGenerator {
|
||||
|
||||
|
||||
public static String generateMethod(Method m, List<AS_Abstract> ass ) {
|
||||
String code = m.getName() + " ( " ;
|
||||
|
||||
String rt_type = Algo_TypeExpr.get_Return_typed(m, ass);
|
||||
|
||||
|
||||
|
||||
String code = rt_type +" " + m.getName() + " ( " ;
|
||||
|
||||
// Generate Formalparameter
|
||||
List<FormalParameter> fp = m.getParameterList().getFormalparalist();
|
||||
@ -70,13 +75,9 @@ public class CodeGenerator {
|
||||
code = code.substring(0, code.length() -2);
|
||||
}
|
||||
|
||||
code = code + " ) { \n block return expression } " ;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
String expression_str = Algo_TypeExpr.expression2code(ass ,m.get_Block().statements);
|
||||
code = code + String.format(" ) { \n return %s } " , expression_str);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
@ -86,5 +87,9 @@ public class CodeGenerator {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -28,20 +28,22 @@ public class TC_Field extends TC_Abstract {
|
||||
// Not completely implemented
|
||||
private ClassOrInterface cl;
|
||||
private Field field;
|
||||
private TV_Field tv;
|
||||
|
||||
|
||||
/*
|
||||
* here the type(variable) has to be supplemented
|
||||
*/
|
||||
public TC_Field (ClassOrInterface cl , Field field ) {
|
||||
public TC_Field (ClassOrInterface cl , Field field , TV_Field tv ) {
|
||||
this.cl = cl;
|
||||
this.field = field;
|
||||
this.tv = tv;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String toString () {
|
||||
return "Phi( " + cl.getClassName().toString() + "," + field.getName().toString() + "," + "Typvariable?" + ")";
|
||||
return "Phi( " + cl.getClassName().toString() + "," + field.getName().toString() + "," + tv.toString() + ")";
|
||||
}
|
||||
|
||||
|
||||
|
@ -26,10 +26,7 @@ public class TC_Method extends TC_Abstract {
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
|
||||
|
||||
|
||||
public String toString() {
|
||||
return "Mue( " + cl.getClassName().toString() + "," + method.getName() + "...)" ;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,38 @@
|
||||
package de.dhbwstuttgart.strucTypes;
|
||||
|
||||
public class TV_Typ {
|
||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||
import de.dhbwstuttgart.syntaxtree.Method;
|
||||
|
||||
/*
|
||||
* TypeVariable for the Return Type of the Method m in class A;
|
||||
*/
|
||||
|
||||
|
||||
public class TV_ReturnTyp extends TV_Abstract {
|
||||
|
||||
ClassOrInterface cl;
|
||||
Method m;
|
||||
|
||||
|
||||
public TV_ReturnTyp(ClassOrInterface cl, Method m) {
|
||||
this.cl = cl;
|
||||
this.m = m;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String to_String() {
|
||||
return String.format("gamma( %s , %s ) ", cl.getClassName() , m.getName());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
24
src/de/dhbwstuttgart/strucTypes/TV_ReturnType2.java
Normal file
24
src/de/dhbwstuttgart/strucTypes/TV_ReturnType2.java
Normal file
@ -0,0 +1,24 @@
|
||||
package de.dhbwstuttgart.strucTypes;
|
||||
|
||||
public class TV_ReturnType2 extends TV_Abstract{
|
||||
|
||||
String method;
|
||||
String classname;
|
||||
|
||||
|
||||
public TV_ReturnType2(String method, String classname) {
|
||||
super();
|
||||
this.method = method;
|
||||
this.classname = classname;
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
return String.format("gamma( %s , %s ) ", this.method , this.classname);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -31,6 +31,11 @@ public class MethodCall extends Statement
|
||||
return receiver;
|
||||
}
|
||||
|
||||
// Sebastian
|
||||
public ArgumentList get_ArgList() {
|
||||
return arglist;
|
||||
}
|
||||
|
||||
// Sebastian
|
||||
public String get_Name() {
|
||||
return name;
|
||||
|
@ -3,4 +3,9 @@ Geht nicht ?
|
||||
|
||||
mt4() { return f1; }
|
||||
|
||||
Wie ist mit sowas umzugehen? Immer nur lokale Variable?
|
||||
Wie ist mit sowas umzugehen? Immer nur lokale Variable?
|
||||
|
||||
|
||||
|
||||
Felder ohne Typvariable möglich ?
|
||||
|
||||
|
@ -42,7 +42,7 @@ public class GeneralParserTest{
|
||||
//filenames.add("FieldVarTest.jav");
|
||||
|
||||
//filenames.add("StructuralTypesField.jav");
|
||||
filenames.add("StructuralTypesSimple2.jav");
|
||||
filenames.add("StructuralTypesSimple3.jav");
|
||||
|
||||
JavaTXParser parser = new JavaTXParser();
|
||||
try{
|
||||
|
@ -4,16 +4,13 @@ class A {
|
||||
A f1;
|
||||
|
||||
A f2;
|
||||
|
||||
|
||||
mt1(x) { return x; }
|
||||
|
||||
mt2(x) { return x.feld ; }
|
||||
//mt2(x) { return x.feld ; }
|
||||
|
||||
mt3(a,b,c) { return a.add(b).sub(c); }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
16
test/strucTypes/StructuralTypesSimple3.jav
Normal file
16
test/strucTypes/StructuralTypesSimple3.jav
Normal file
@ -0,0 +1,16 @@
|
||||
|
||||
class A {
|
||||
|
||||
A f1;
|
||||
|
||||
A f2;
|
||||
|
||||
|
||||
mt1(x) { return x; }
|
||||
|
||||
//mt2(x) { return x.feld ; }
|
||||
|
||||
mt3(x,y,z) { return x.sub(y).add(z); }
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user