code generator für expression hinzugefügt (vorlage für TypeExpr)

This commit is contained in:
sebastian 2017-03-19 14:39:09 +01:00
parent 89d1b97339
commit 8e4b1679f0
7 changed files with 106 additions and 17 deletions

View File

@ -3,7 +3,10 @@ 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.Expression;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar; import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.Return; import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.Statement; import de.dhbwstuttgart.syntaxtree.statement.Statement;
@ -31,7 +34,7 @@ public class Algo_TypeExpr {
// Methode enthält nur ein Return statment das ein feld zurückgibt // Methode enthält nur ein Return Statement das ein feld zurückgibt
if (statements.size() == 1) { if (statements.size() == 1) {
if (statements.get(0).getClass().equals(Return.class)) { if (statements.get(0).getClass().equals(Return.class)) {
Return exp_rt = (Return) statements.get(0); Return exp_rt = (Return) statements.get(0);
@ -62,9 +65,55 @@ public class Algo_TypeExpr {
}
public static String expression2code(List<AS_Abstract> ass , List<Statement> statements ) {
String res = "";
// Leere methode
if (statements.size() == 0) {
res = "---";
}
// Methode mit einer Return Expression
if (statements.size() == 1) {
if (statements.get(0).getClass() == Return.class) {
Return ret = (Return) statements.get(0);
Statement ret2 = (Statement) ret.get_Expression();
res = statement2code(ass,ret2);
}
}
return res;
}
public static String statement2code(List<AS_Abstract> ass , Statement statement) {
// TODO: Else if
if ( statement.getClass().equals(LocalVar.class) ) {
LocalVar lvar = (LocalVar) statement;
String exp_str = lvar.get_expression();
return exp_str;
}
if (statement.getClass().equals(FieldVar.class)) {
// FieldVar ist nicht implementiert
}
if (statement.getClass().equals(MethodCall.class)) {
MethodCall mtcall = (MethodCall) statement;
Statement receiver = (Statement) mtcall.get_Receiver().get_Expression();
String str = statement2code(ass,receiver);
return str + "."+ mtcall.get_Name() + "()";
}
return "Fehler";
}
@ -78,14 +127,4 @@ public class Algo_TypeExpr {
}
} }

View File

@ -0,0 +1,19 @@
package de.dhbwstuttgart.strucTypes;
public class REPL {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer i = 1;
System.out.println(i.getClass() == Integer.class);
System.out.println(i.getClass().equals(Integer.class));
}
}

View File

@ -25,6 +25,19 @@ public class MethodCall extends Statement
this.receiver = receiver; this.receiver = receiver;
} }
// Sebastian
public Receiver get_Receiver() {
return receiver;
}
// Sebastian
public String get_Name() {
return name;
}
@Override @Override
public ConstraintSet getConstraints(TypeInferenceInformation info) { public ConstraintSet getConstraints(TypeInferenceInformation info) {
ConstraintSet ret = receiver.getConstraints(info); ConstraintSet ret = receiver.getConstraints(info);

View File

@ -17,6 +17,12 @@ public class Receiver extends Expression
this.expr = expr; this.expr = expr;
} }
// Sebastian
public Expression get_Expression() {
return expr;
}
@Override @Override
public ConstraintSet getConstraints(TypeInferenceInformation info) { public ConstraintSet getConstraints(TypeInferenceInformation info) {
return expr.getConstraints(info); return expr.getConstraints(info);

View File

@ -0,0 +1,6 @@
Geht nicht ?
mt4() { return f1; }
Wie ist mit sowas umzugehen? Immer nur lokale Variable?

View File

@ -10,6 +10,7 @@ import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.strucTypes.AS_Abstract; import de.dhbwstuttgart.strucTypes.AS_Abstract;
import de.dhbwstuttgart.strucTypes.TC_Abstract; import de.dhbwstuttgart.strucTypes.TC_Abstract;
import de.dhbwstuttgart.strucTypes.Algo_Type; import de.dhbwstuttgart.strucTypes.Algo_Type;
import de.dhbwstuttgart.strucTypes.Algo_TypeExpr;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr; import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
@ -58,6 +59,9 @@ public class GeneralParserTest{
Algo_Type type = new Algo_Type( ass , sf.getClasses().get(0)); Algo_Type type = new Algo_Type( ass , sf.getClasses().get(0));
// Print der Expression:
System.out.println(Algo_TypeExpr.expression2code(ass, sf.getClasses().get(0).getMethods().get(0).get_Block().statements));
System.out.println(Algo_TypeExpr.expression2code(ass, sf.getClasses().get(0).getMethods().get(2).get_Block().statements));
// Erstellen der TypeExpression // Erstellen der TypeExpression

View File

@ -13,5 +13,7 @@ class A {
} }