From 6b98bf6a58d3f45973d37381932facd028406d57 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 23 Aug 2017 03:52:24 +0200 Subject: [PATCH] =?UTF-8?q?Feld=20initialisierungen=20werden=20an=20Konstr?= =?UTF-8?q?uktoren=20angeh=C3=A4ngt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StatementGenerator.java | 25 ++++++++++++++++--- .../SyntaxTreeGenerator.java | 14 ++++++----- .../dhbwstuttgart/syntaxtree/Constructor.java | 16 ++++++++++-- .../syntaxtree/factory/ASTFactory.java | 2 +- test/javFiles/mathStruc.jav | 3 ++- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index d2fa81ff..03c72738 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -9,12 +9,11 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.*; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -import de.dhbwstuttgart.typecheck.JavaClassName; +import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typecheck.JavaClassRegistry; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; -import java.lang.reflect.Modifier; import java.util.*; public class StatementGenerator { @@ -395,7 +394,13 @@ public class StatementGenerator { }else{ 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 generateLocalVariableAssignments(List varDeclarators, RefTypeOrTPHOrWildcardOrGeneric type){ + List ret = new ArrayList<>(); + for(Java8Parser.VariableDeclaratorContext varDecl : varDeclarators){ TerminalNode name = varDecl.variableDeclaratorId().Identifier(); ret.add(new LocalVarDecl(name.getText(), type, name.getSymbol())); @@ -413,6 +418,20 @@ public class StatementGenerator { 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){ return convert(stmt.statementExpressionList()); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index c27a5ac8..941e7032 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -28,6 +28,8 @@ public class SyntaxTreeGenerator{ private String pkgName = ""; List imports = new ArrayList(); + List fieldInitializations = new ArrayList<>(); + public SyntaxTreeGenerator(JavaClassRegistry reg){ this.reg = reg; } @@ -232,7 +234,7 @@ public class SyntaxTreeGenerator{ if(parentClass.equals(new JavaClassName(name))){ //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); - return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart()); + return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); }else{ 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); //TODO: Konstruktor muss Felder initialisieren: 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) { @@ -385,7 +387,7 @@ public class SyntaxTreeGenerator{ for(Java8Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){ String fieldName = convert(varCtx.variableDeclaratorId()); if(varCtx.variableInitializer() != null){ - initializeField(fieldDeclarationContext); + initializeField(varCtx, fieldType, generics); } else{ ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart())); @@ -399,9 +401,9 @@ public class SyntaxTreeGenerator{ } // Initialize a field by creating implicit constructor. - private void initializeField(Java8Parser.FieldDeclarationContext ctx){ - //TODO - throw new NotImplementedException(); + private void initializeField(Java8Parser.VariableDeclaratorContext ctx, RefTypeOrTPHOrWildcardOrGeneric typeOfField, GenericsRegistry generics){ + StatementGenerator statementGenerator = new StatementGenerator(reg, generics, new HashMap<>()); + fieldInitializations.add(statementGenerator.generateFieldAssignment(ctx, typeOfField)); } public static int convertModifier(String modifier){ diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 08c0ba63..a56a37b0 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -1,14 +1,26 @@ package de.dhbwstuttgart.syntaxtree; +import de.dhbwstuttgart.syntaxtree.statement.Statement; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.syntaxtree.statement.Block; +import java.util.ArrayList; +import java.util.List; + 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 fieldInitializations; + + public Constructor(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset, List fieldInitializations) { + super(name, returnType, modifiers, parameterList, codeInsideConstructor, gtvDeclarations, offset); + + this.fieldInitializations = fieldInitializations; } + } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 7a355c60..e46eec3a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -78,7 +78,7 @@ public class ASTFactory { Token offset = new NullToken(); 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){ diff --git a/test/javFiles/mathStruc.jav b/test/javFiles/mathStruc.jav index 9f7ae92d..45121c5e 100644 --- a/test/javFiles/mathStruc.jav +++ b/test/javFiles/mathStruc.jav @@ -1,6 +1,7 @@ + class mathStruc { -a; +a = new mathStruc(); mathStruc(A a) { }