forked from JavaTX/JavaCompilerCore
code generator für expression hinzugefügt (vorlage für TypeExpr)
This commit is contained in:
parent
89d1b97339
commit
8e4b1679f0
@ -3,7 +3,10 @@ package de.dhbwstuttgart.strucTypes;
|
||||
import java.util.ArrayList;
|
||||
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.MethodCall;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.Return;
|
||||
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.get(0).getClass().equals(Return.class)) {
|
||||
Return exp_rt = (Return) statements.get(0);
|
||||
@ -59,28 +62,64 @@ public class Algo_TypeExpr {
|
||||
|
||||
//-> Es muss so sein dass ich diese Return Statement beurteilen kann
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
19
src/de/dhbwstuttgart/strucTypes/REPL.java
Normal file
19
src/de/dhbwstuttgart/strucTypes/REPL.java
Normal 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));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -24,6 +24,19 @@ public class MethodCall extends Statement
|
||||
this.name = methodName;
|
||||
this.receiver = receiver;
|
||||
}
|
||||
|
||||
|
||||
// Sebastian
|
||||
public Receiver get_Receiver() {
|
||||
return receiver;
|
||||
}
|
||||
|
||||
// Sebastian
|
||||
public String get_Name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public ConstraintSet getConstraints(TypeInferenceInformation info) {
|
||||
|
@ -16,6 +16,12 @@ public class Receiver extends Expression
|
||||
super(expr.getType(), expr.getOffset());
|
||||
this.expr = expr;
|
||||
}
|
||||
|
||||
// Sebastian
|
||||
public Expression get_Expression() {
|
||||
return expr;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ConstraintSet getConstraints(TypeInferenceInformation info) {
|
||||
|
6
test/strucTypes/Fragen.txt
Normal file
6
test/strucTypes/Fragen.txt
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
Geht nicht ?
|
||||
|
||||
mt4() { return f1; }
|
||||
|
||||
Wie ist mit sowas umzugehen? Immer nur lokale Variable?
|
@ -10,6 +10,7 @@ import de.dhbwstuttgart.parser.JavaTXParser;
|
||||
import de.dhbwstuttgart.strucTypes.AS_Abstract;
|
||||
import de.dhbwstuttgart.strucTypes.TC_Abstract;
|
||||
import de.dhbwstuttgart.strucTypes.Algo_Type;
|
||||
import de.dhbwstuttgart.strucTypes.Algo_TypeExpr;
|
||||
import de.dhbwstuttgart.syntaxtree.SourceFile;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
|
||||
|
||||
@ -58,6 +59,9 @@ public class GeneralParserTest{
|
||||
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
|
||||
|
@ -11,6 +11,8 @@ class A {
|
||||
|
||||
mt3(a,b,c) { return a.add(b).sub(c); }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user