Feld initialisierungen werden an Konstruktoren angehängt

This commit is contained in:
JanUlrich 2017-08-23 03:52:24 +02:00
parent bbc61c67a9
commit 6b98bf6a58
5 changed files with 47 additions and 13 deletions

View File

@ -9,12 +9,11 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.*;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typecheck.JavaClassName; import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typecheck.JavaClassRegistry; import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.runtime.tree.TerminalNode;
import java.lang.reflect.Modifier;
import java.util.*; import java.util.*;
public class StatementGenerator { public class StatementGenerator {
@ -395,7 +394,13 @@ public class StatementGenerator {
}else{ }else{
type = TypeGenerator.convert(declaration.unannTypeOrAuto().unannType(), reg, generics); type = TypeGenerator.convert(declaration.unannTypeOrAuto().unannType(), reg, generics);
} }
for(Java8Parser.VariableDeclaratorContext varDecl : declaration.variableDeclaratorList().variableDeclarator()){ ret.addAll(generateLocalVariableAssignments(declaration.variableDeclaratorList().variableDeclarator(), type));
return ret;
}
private List<Statement> generateLocalVariableAssignments(List<Java8Parser.VariableDeclaratorContext> varDeclarators, RefTypeOrTPHOrWildcardOrGeneric type){
List<Statement> ret = new ArrayList<>();
for(Java8Parser.VariableDeclaratorContext varDecl : varDeclarators){
TerminalNode name = varDecl.variableDeclaratorId().Identifier(); TerminalNode name = varDecl.variableDeclaratorId().Identifier();
ret.add(new LocalVarDecl(name.getText(), type, name.getSymbol())); ret.add(new LocalVarDecl(name.getText(), type, name.getSymbol()));
@ -413,6 +418,20 @@ public class StatementGenerator {
return ret; return ret;
} }
public Statement generateFieldAssignment(Java8Parser.VariableDeclaratorContext varDecl, RefTypeOrTPHOrWildcardOrGeneric type){
TerminalNode name = varDecl.variableDeclaratorId().Identifier();
Expression initValue;
if(varDecl.variableInitializer().arrayInitializer() != null){
throw new NotImplementedException();
}else{
initValue = convert(varDecl.variableInitializer().expression());
}
return (new Assign(
new FieldVar(new This(varDecl.getStart()), name.getText(),
new Void(varDecl.getStart()), varDecl.getStart()),
initValue, name.getSymbol()));
}
private Statement convert(Java8Parser.ForUpdateContext stmt){ private Statement convert(Java8Parser.ForUpdateContext stmt){
return convert(stmt.statementExpressionList()); return convert(stmt.statementExpressionList());
} }

View File

@ -28,6 +28,8 @@ public class SyntaxTreeGenerator{
private String pkgName = ""; private String pkgName = "";
List<JavaClassName> imports = new ArrayList(); List<JavaClassName> imports = new ArrayList();
List<Statement> fieldInitializations = new ArrayList<>();
public SyntaxTreeGenerator(JavaClassRegistry reg){ public SyntaxTreeGenerator(JavaClassRegistry reg){
this.reg = reg; this.reg = reg;
} }
@ -232,7 +234,7 @@ public class SyntaxTreeGenerator{
if(parentClass.equals(new JavaClassName(name))){ if(parentClass.equals(new JavaClassName(name))){
//TODO: Constructor darf nicht Rückgabetyp void bekommen: Hier als Rückgabetyp die Klasse inklusive generische Variablen //TODO: Constructor darf nicht Rückgabetyp void bekommen: Hier als Rückgabetyp die Klasse inklusive generische Variablen
//retType = TypeGenerator.convertTypeName(name, gtvDeclarations, header.getStart(), reg, localGenerics); //retType = TypeGenerator.convertTypeName(name, gtvDeclarations, header.getStart(), reg, localGenerics);
return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart()); return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations);
}else{ }else{
return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart()); return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart());
} }
@ -315,7 +317,7 @@ public class SyntaxTreeGenerator{
ParameterList params = new ParameterList(new ArrayList<>(), offset); ParameterList params = new ParameterList(new ArrayList<>(), offset);
//TODO: Konstruktor muss Felder initialisieren: //TODO: Konstruktor muss Felder initialisieren:
Block block = new Block(new ArrayList<>(), offset); Block block = new Block(new ArrayList<>(), offset);
return new Constructor(className, classType, modifiers, params, block, classGenerics, offset); return new Constructor(className, classType, modifiers, params, block, classGenerics, offset, fieldInitializations);
} }
private RefType convert(Java8Parser.SuperclassContext superclass) { private RefType convert(Java8Parser.SuperclassContext superclass) {
@ -385,7 +387,7 @@ public class SyntaxTreeGenerator{
for(Java8Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){ for(Java8Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){
String fieldName = convert(varCtx.variableDeclaratorId()); String fieldName = convert(varCtx.variableDeclaratorId());
if(varCtx.variableInitializer() != null){ if(varCtx.variableInitializer() != null){
initializeField(fieldDeclarationContext); initializeField(varCtx, fieldType, generics);
} }
else{ else{
ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart())); ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart()));
@ -399,9 +401,9 @@ public class SyntaxTreeGenerator{
} }
// Initialize a field by creating implicit constructor. // Initialize a field by creating implicit constructor.
private void initializeField(Java8Parser.FieldDeclarationContext ctx){ private void initializeField(Java8Parser.VariableDeclaratorContext ctx, RefTypeOrTPHOrWildcardOrGeneric typeOfField, GenericsRegistry generics){
//TODO StatementGenerator statementGenerator = new StatementGenerator(reg, generics, new HashMap<>());
throw new NotImplementedException(); fieldInitializations.add(statementGenerator.generateFieldAssignment(ctx, typeOfField));
} }
public static int convertModifier(String modifier){ public static int convertModifier(String modifier){

View File

@ -1,14 +1,26 @@
package de.dhbwstuttgart.syntaxtree; package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.statement.Block;
import java.util.ArrayList;
import java.util.List;
public class Constructor extends Method { public class Constructor extends Method {
public Constructor(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block block, GenericDeclarationList gtvDeclarations, Token offset) { /**
super(name, returnType, modifiers, parameterList, block, gtvDeclarations, offset); * Das sind die Statements, welche die Felder der zugehörigen Klasse dieses Konstruktor initialisieren
*/
private final List<Statement> fieldInitializations;
public Constructor(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset, List<Statement> fieldInitializations) {
super(name, returnType, modifiers, parameterList, codeInsideConstructor, gtvDeclarations, offset);
this.fieldInitializations = fieldInitializations;
} }
} }

View File

@ -78,7 +78,7 @@ public class ASTFactory {
Token offset = new NullToken(); Token offset = new NullToken();
int modifier = constructor.getModifiers(); int modifier = constructor.getModifiers();
return new de.dhbwstuttgart.syntaxtree.Constructor(name,returnType, modifier, parameterList, block, gtvDeclarations, offset); return new de.dhbwstuttgart.syntaxtree.Constructor(name,returnType, modifier, parameterList, block, gtvDeclarations, offset, new ArrayList<>());
} }
public Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){ public Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){

View File

@ -1,6 +1,7 @@
class mathStruc<A> { class mathStruc<A> {
a; a = new mathStruc<String>();
mathStruc(A a) { } mathStruc(A a) { }