From 0e981ce95c164b9437f71a551b2a82fe98371f69 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Sun, 12 Feb 2023 07:51:31 +0100 Subject: [PATCH] convert(StatementContext) Implementierung fortgesetzt --- .../parser/antlr/Java17Parser.g4 | 38 +- .../StatementGenerator.java | 797 ++++++++++-------- 2 files changed, 448 insertions(+), 387 deletions(-) diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index 37f7168d..f8bdfca0 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -521,25 +521,25 @@ localTypeDeclaration ; statement - : blockLabel=block - | ASSERT expression (':' expression)? ';' - | IF parExpression statement (ELSE statement)? - | FOR '(' forControl ')' statement - | WHILE parExpression statement - | DO statement WHILE parExpression ';' - | TRY block (catchClause+ finallyBlock? | finallyBlock) - | TRY resourceSpecification block catchClause* finallyBlock? - | SWITCH parExpression '{' switchBlockStatementGroup* switchLabel* '}' - | SYNCHRONIZED parExpression block - | RETURN expression? ';' - | THROW expression ';' - | BREAK identifier? ';' - | CONTINUE identifier? ';' - | YIELD expression ';' // Java17 - | SEMI - | statementExpression=expression ';' - | switchExpression ';'? // Java17 - | identifierLabel=identifier ':' statement + : blockLabel=block #blockstmt + | ASSERT expression (':' expression)? ';' #assertstmt + | IF parExpression statement (ELSE statement)? #conditionalstmt + | FOR '(' forControl ')' statement #forloop + | WHILE parExpression statement #whileloop + | DO statement WHILE parExpression ';' #dowhileloop + | TRY block (catchClause+ finallyBlock? | finallyBlock) #trycatchblock + | TRY resourceSpecification block catchClause* finallyBlock? #trycatchresource + | SWITCH parExpression '{' switchBlockStatementGroup* switchLabel* '}' #switchstmt + | SYNCHRONIZED parExpression block #synchronizedstmt + | RETURN expression? ';' #returnstmt + | THROW expression ';' #throstmt + | BREAK identifier? ';' #breakstmt + | CONTINUE identifier? ';' #continuestmt + | YIELD expression ';' #yieldstmt // Java17 + | SEMI #semistmt + | statementExpression=expression ';' #stmtexpression + | switchExpression ';'? #switchexpression // Java17 + | identifierLabel=identifier ':' statement #identifierlabel ; catchClause diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 3c3ad88d..aa2204e3 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -3,6 +3,10 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.antlr.Java17Parser; +import de.dhbwstuttgart.parser.antlr.Java17Parser.AssertstmtContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.BlockstmtContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.ConditionalstmtContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.ForloopContext; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.*; @@ -22,11 +26,14 @@ import java.util.stream.Collectors; public class StatementGenerator { private JavaClassRegistry reg; - private Map fields; //PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen + private Map fields; // PL 2018-11-01 fields eingefuegt, damit die fields + // immer die gleiche TPH bekommen private Map localVars; private GenericsRegistry generics; - public StatementGenerator(JavaClassRegistry reg, GenericsRegistry generics, Map fields, Map localVars){ + public StatementGenerator(JavaClassRegistry reg, GenericsRegistry generics, + Map fields, + Map localVars) { this.reg = reg; this.generics = generics; this.fields = fields; @@ -36,23 +43,26 @@ public class StatementGenerator { public ParameterList convert(Java17Parser.FormalParameterListContext formalParameterListContext) { List ret = new ArrayList<>(); List fps = new ArrayList<>(); - if(formalParameterListContext == null || formalParameterListContext.lastFormalParameter() == null) - return new ParameterList(ret, new NullToken()); //Dann ist die Parameterliste leer + if (formalParameterListContext == null || formalParameterListContext.lastFormalParameter() == null) + return new ParameterList(ret, new NullToken()); // Dann ist die Parameterliste leer - if(formalParameterListContext.lastFormalParameter().formalParameter() == null)throw new NotImplementedException(); + /* + * Restrukturierung Java17-Grammatik + * Zeile wird nicht mehr benötigt, da Regel für Parameter-Liste nicht mehr + * rekursiv ist. "lastFormalParameter" hat kein Kind "formalParameter" mehr. + * + * if(formalParameterListContext.lastFormalParameter().formalParameter() == + * null) throw new NotImplementedException(); + */ - if(formalParameterListContext != null && formalParameterListContext.formalParameters() != null - && formalParameterListContext.formalParameters().formalParameter() != null){ - fps = new ArrayList<>(formalParameterListContext.formalParameters().formalParameter()); - } - fps.add(formalParameterListContext.lastFormalParameter().formalParameter()); + fps = formalParameterListContext.formalParameter(); - for(Java17Parser.FormalParameterContext fp : fps){ + for (Java17Parser.FormalParameterContext fp : fps) { String paramName = SyntaxTreeGenerator.convert(fp.variableDeclaratorId()); RefTypeOrTPHOrWildcardOrGeneric type; - if(fp.unannType() != null){ - type = TypeGenerator.convert(fp.unannType(), reg, generics); - }else{ + if (fp.typeType() != null) { + type = TypeGenerator.convert(fp.typeType(), reg, generics); + } else { type = TypePlaceholder.fresh(fp.getStart()); } ret.add(new FormalParameter(paramName, type, fp.getStart())); @@ -61,11 +71,27 @@ public class StatementGenerator { return new ParameterList(ret, ret.get(0).getOffset()); } - /* - * StatementGeneration: - */ + /* + * StatementGeneration: + */ private Statement convert(Java17Parser.StatementContext stmt) { + switch (stmt) { + case AssertstmtContext assertstmt: + return convert(assertstmt); + break; + case BlockstmtContext blockstmt: + return convert(blockstmt.block(), false); + break; + case ConditionalstmtContext condition: + return convert(condition); + break; + case ForloopContext forloop: + break; + default: + throw new NotImplementedException(); + + } if (stmt.statementWithoutTrailingSubstatement() != null) { return convert(stmt.statementWithoutTrailingSubstatement()); } else if (stmt.whileStatement() != null) { @@ -77,69 +103,77 @@ public class StatementGenerator { } else if (stmt.ifThenStatement() != null) { return convert(stmt.ifThenStatement()); } else if (stmt.labeledStatement() != null) { - return convert(stmt.labeledStatement() ); - }else throw new NotImplementedException(); + return convert(stmt.labeledStatement()); + } else + throw new NotImplementedException(); } - private Statement convert(Java17Parser.StatementNoShortIfContext stmt){ - if(stmt.statementWithoutTrailingSubstatement() != null){ - return convert(stmt.statementWithoutTrailingSubstatement()); - }else if(stmt.labeledStatementNoShortIf() != null){ - return convert(stmt.labeledStatementNoShortIf()); - }else if(stmt.ifThenElseStatementNoShortIf() != null){ - return convert(stmt.ifThenElseStatementNoShortIf()); - }else if(stmt.whileStatementNoShortIf() != null){ - return convert(stmt.whileStatementNoShortIf()); - }else if(stmt.forStatementNoShortIf() != null){ - return convert(stmt.forStatementNoShortIf()); - }else throw new NotImplementedException(); - } + /* + * Restrukturierung Java17-Grammatik + * Fällt weg + * private Statement convert(Java17Parser.StatementNoShortIfContext stmt) { + * if (stmt.statementWithoutTrailingSubstatement() != null) { + * return convert(stmt.statementWithoutTrailingSubstatement()); + * } else if (stmt.labeledStatementNoShortIf() != null) { + * return convert(stmt.labeledStatementNoShortIf()); + * } else if (stmt.ifThenElseStatementNoShortIf() != null) { + * return convert(stmt.ifThenElseStatementNoShortIf()); + * } else if (stmt.whileStatementNoShortIf() != null) { + * return convert(stmt.whileStatementNoShortIf()); + * } else if (stmt.forStatementNoShortIf() != null) { + * return convert(stmt.forStatementNoShortIf()); + * } else + * throw new NotImplementedException(); + * } + */ private Statement convert(Java17Parser.StatementWithoutTrailingSubstatementContext stmt) { - if(stmt.block() != null){ + if (stmt.block() != null) { return convert(stmt.block(), false); - }else if(stmt.emptyStatement() != null){ + } else if (stmt.emptyStatement() != null) { return new EmptyStmt(stmt.getStart()); - }else if(stmt.expressionStatement() != null){ + } else if (stmt.expressionStatement() != null) { return convert(stmt.expressionStatement()); - }else if(stmt.assertStatement() != null){ + } else if (stmt.assertStatement() != null) { return convert(stmt.assertStatement()); - }else if(stmt.switchStatement() != null){ + } else if (stmt.switchStatement() != null) { return convert(stmt.switchStatement()); - }else if(stmt.doStatement() != null){ + } else if (stmt.doStatement() != null) { return convert(stmt.doStatement()); - }else if(stmt.breakStatement() != null){ + } else if (stmt.breakStatement() != null) { return convert(stmt.breakStatement()); - }else if(stmt.continueStatement() != null){ + } else if (stmt.continueStatement() != null) { return convert(stmt.continueStatement()); - }else if(stmt.returnStatement() != null){ + } else if (stmt.returnStatement() != null) { return convert(stmt.returnStatement()); - }else if(stmt.synchronizedStatement() != null){ + } else if (stmt.synchronizedStatement() != null) { return convert(stmt.synchronizedStatement()); - }else if(stmt.throwStatement() != null){ + } else if (stmt.throwStatement() != null) { return convert(stmt.throwStatement()); - }else if(stmt.tryStatement() != null){ + } else if (stmt.tryStatement() != null) { return convert(stmt.tryStatement()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } public Block convert(Java17Parser.BlockContext block, boolean addTrailingReturn) { List statements = new ArrayList<>(); - if(block.blockStatements() != null) - for(Java17Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()){ + if (block.blockStatements() != null) + for (Java17Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()) { List stmt = convert(statementContext); statements.addAll(stmt); } - if(addTrailingReturn)statements = SyntacticSugar.addTrailingReturn(statements); + if (addTrailingReturn) + statements = SyntacticSugar.addTrailingReturn(statements); return new Block(statements, block.getStart()); } private List convert(Java17Parser.BlockStatementContext statementContext) { - if(statementContext.localVariableDeclarationStatement() != null){ + if (statementContext.localVariableDeclarationStatement() != null) { return convert(statementContext.localVariableDeclarationStatement()); - }else if(statementContext.classDeclaration() != null){ + } else if (statementContext.classDeclaration() != null) { throw new NotImplementedException(); - }else{ + } else { return Arrays.asList(convert(statementContext.statement())); } } @@ -151,84 +185,89 @@ public class StatementGenerator { private Statement convert(Java17Parser.LabeledStatementContext labeledStatementContext) { throw new NotImplementedException(); - //return convert(labeledStatementContext.statement()); + // return convert(labeledStatementContext.statement()); } - private Statement convert(Java17Parser.LabeledStatementNoShortIfContext stmt){ + private Statement convert(Java17Parser.LabeledStatementNoShortIfContext stmt) { throw new NotImplementedException(); - //return convert(stmt.statementNoShortIf()); + // return convert(stmt.statementNoShortIf()); } - private Statement convert(Java17Parser.ExpressionStatementContext stmt){ + private Statement convert(Java17Parser.ExpressionStatementContext stmt) { return convert(stmt.statementExpression()); } private Statement convert(Java17Parser.StatementExpressionContext stmt) { - if(stmt.assignment() != null){ + if (stmt.assignment() != null) { return convert(stmt.assignment()); - }else if(stmt.preIncrementExpression() != null){ + } else if (stmt.preIncrementExpression() != null) { return convert(stmt.preIncrementExpression()); - }else if(stmt.preDecrementExpression() != null){ + } else if (stmt.preDecrementExpression() != null) { return convert(stmt.preDecrementExpression()); - }else if(stmt.postIncrementExpression() != null){ + } else if (stmt.postIncrementExpression() != null) { return convert(stmt.postIncrementExpression()); - }else if(stmt.postDecrementExpression() != null){ + } else if (stmt.postDecrementExpression() != null) { return convert(stmt.postDecrementExpression()); - }else if(stmt.methodInvocation() != null){ + } else if (stmt.methodInvocation() != null) { return convert(stmt.methodInvocation()); - }else if(stmt.classInstanceCreationExpression() != null){ + } else if (stmt.classInstanceCreationExpression() != null) { return convert(stmt.classInstanceCreationExpression()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } - public Receiver getReceiver(Expression expr){ - if(expr instanceof StaticClassName){ + public Receiver getReceiver(Expression expr) { + if (expr instanceof StaticClassName) { return (Receiver) expr; - }else { + } else { return new ExpressionReceiver(expr); } } private Statement convert(Java17Parser.MethodInvocationContext methodInvocationContext) { String name; - if(methodInvocationContext.methodName()!=null){ + if (methodInvocationContext.methodName() != null) { name = methodInvocationContext.methodName().Identifier().getText(); - }else{// if(methodInvocationContext.Identifier() != null){ + } else {// if(methodInvocationContext.Identifier() != null){ name = methodInvocationContext.Identifier().getText(); } Expression receiver; - if(methodInvocationContext.typeName() != null){ - receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart()); - }else if(methodInvocationContext.expressionName()!=null){ + if (methodInvocationContext.typeName() != null) { + receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), + methodInvocationContext.typeName().getStart()); + } else if (methodInvocationContext.expressionName() != null) { receiver = convert(methodInvocationContext.expressionName()); - }else if(methodInvocationContext.primary() != null){ + } else if (methodInvocationContext.primary() != null) { receiver = convert(methodInvocationContext.primary()); - }else if(methodInvocationContext.toString().startsWith("super")){ + } else if (methodInvocationContext.toString().startsWith("super")) { receiver = new Super(methodInvocationContext.getStart()); - }else if(methodInvocationContext.methodName() != null){ + } else if (methodInvocationContext.methodName() != null) { receiver = new This(methodInvocationContext.getStart()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); ArgumentList argumentList = convert(methodInvocationContext.argumentList()); ArrayList argTypes = argumentList.getArguments().stream() - .map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); - MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), - getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), - argTypes, methodInvocationContext.getStart()); + .map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())) + .collect(Collectors.toCollection(ArrayList::new)); + MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), + getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), + argTypes, methodInvocationContext.getStart()); ret.setStatement(); return ret; } private ArgumentList convert(Java17Parser.ArgumentListContext argumentListContext) { - if(argumentListContext == null)return new ArgumentList(new ArrayList<>(), new NullToken()); + if (argumentListContext == null) + return new ArgumentList(new ArrayList<>(), new NullToken()); List args = new ArrayList<>(); Token offset = new NullToken(); - for(Java17Parser.ExpressionContext expr : argumentListContext.expression()){ + for (Java17Parser.ExpressionContext expr : argumentListContext.expression()) { args.add(convert(expr)); } - if(args.size()>0)offset = args.get(0).getOffset(); + if (args.size() > 0) + offset = args.get(0).getOffset(); return new ArgumentList(args, offset); } @@ -236,106 +275,112 @@ public class StatementGenerator { /** * Der Parser kann nicht zwischen einer lokalen Variable, einem Feldzugriff und * einer Klassenangabe unterscheiden. + * * @param expression * @param offset * @return */ - private Expression generateLocalOrFieldVarOrClassName(String expression, Token offset){ + private Expression generateLocalOrFieldVarOrClassName(String expression, Token offset) { String[] parts = expression.split("\\."); - if(parts.length < 2){ - //Check for localVar: - if(localVars.get(expression) != null){ + if (parts.length < 2) { + // Check for localVar: + if (localVars.get(expression) != null) { return new LocalVar(expression, localVars.get(expression), offset); - }else{ - if(fields.get(expression) != null){//PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen - return new FieldVar(new This(offset), expression, fields.get(expression), offset); - } else { - //kann eigentlich nicht vorkommen - //Dann Muss es ein Feld sein! - return new FieldVar(new This(offset), expression, TypePlaceholder.fresh(offset), offset); - }} + if (fields.get(expression) != null) {// PL 2018-11-01 fields eingefuegt, damit die fields immer die + // gleiche TPH bekommen + return new FieldVar(new This(offset), expression, fields.get(expression), offset); + + } else { + // kann eigentlich nicht vorkommen + // Dann Muss es ein Feld sein! + return new FieldVar(new This(offset), expression, TypePlaceholder.fresh(offset), offset); + } + } } return generateFieldVarOrClassname(expression, offset); } - private Expression generateFieldVarOrClassname(String expression, Token offset){ + private Expression generateFieldVarOrClassname(String expression, Token offset) { String[] parts = expression.split("\\."); String whole = ""; Expression receiver = null; - for(String part : parts){ - whole+=part; - //Check for Classname: - if(reg.contains(whole)){ + for (String part : parts) { + whole += part; + // Check for Classname: + if (reg.contains(whole)) { receiver = new StaticClassName(reg.getName(whole), offset); } - whole+="."; + whole += "."; } - if(parts.length < 2 || parts[0].contentEquals("this") ){ + if (parts.length < 2 || parts[0].contentEquals("this")) { receiver = new This(offset); } else if (parts[0].contentEquals("super")) { - receiver = new Super(offset); - }else if(receiver == null){ //Handelt es sich um keinen Statischen Klassennamen: - String part = expression.substring(0,expression.length() - (1 + parts[parts.length-1].length())); + receiver = new Super(offset); + } else if (receiver == null) { // Handelt es sich um keinen Statischen Klassennamen: + String part = expression.substring(0, expression.length() - (1 + parts[parts.length - 1].length())); receiver = generateLocalOrFieldVarOrClassName(part, offset); } - return new FieldVar(receiver, parts[parts.length-1], TypePlaceholder.fresh(offset) ,offset); + return new FieldVar(receiver, parts[parts.length - 1], TypePlaceholder.fresh(offset), offset); } private Expression convert(Java17Parser.ExpressionNameContext expressionNameContext) { return generateLocalOrFieldVarOrClassName(expressionNameContext.getText(), expressionNameContext.getStart()); } - private Statement convert(Java17Parser.ClassInstanceCreationExpressionContext newExpression) { Java17Parser.TypeArgumentsContext genericArgs = null; - if(newExpression.expressionName()!= null)throw new NotImplementedException(); - if(newExpression.typeArgumentsOrDiamond()!= null){ - if(newExpression.typeArgumentsOrDiamond().typeArguments()!=null){ + if (newExpression.expressionName() != null) + throw new NotImplementedException(); + if (newExpression.typeArgumentsOrDiamond() != null) { + if (newExpression.typeArgumentsOrDiamond().typeArguments() != null) { genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments(); } } - if(newExpression.typeArguments()!= null)throw new NotImplementedException(); + if (newExpression.typeArguments() != null) + throw new NotImplementedException(); TerminalNode identifier = newExpression.Identifier(0); - RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(),genericArgs,identifier.getSymbol(),reg,generics); + RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(), genericArgs, + identifier.getSymbol(), reg, generics); ArgumentList args = convert(newExpression.argumentList()); ArrayList argTypes = args.getArguments().stream() - .map(x -> TypePlaceholder.fresh(newExpression.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); + .map(x -> TypePlaceholder.fresh(newExpression.getStart())) + .collect(Collectors.toCollection(ArrayList::new)); Statement ret = new NewClass(newClass, args, null, argTypes, newExpression.getStart()); ret.setStatement(); return ret; } private Statement convert(Java17Parser.PreIncrementExpressionContext stmt) { - Expression argument = convert(stmt.unaryExpression()); - Token offset = stmt.getStart(); - Statement ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset); - ret.setStatement(); - return ret; - } + Expression argument = convert(stmt.unaryExpression()); + Token offset = stmt.getStart(); + Statement ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), + offset); + ret.setStatement(); + return ret; + } - private Statement convert(Java17Parser.PreDecrementExpressionContext stmt) { - Statement ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), + private Statement convert(Java17Parser.PreDecrementExpressionContext stmt) { + Statement ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); - ret.setStatement(); + ret.setStatement(); return ret; } private Statement convert(Java17Parser.PostIncrementExpressionContext stmt) { - Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), + Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); - ret.setStatement(); + ret.setStatement(); return ret; } private Statement convert(Java17Parser.PostDecrementExpressionContext stmt) { - Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), + Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); - ret.setStatement(); + ret.setStatement(); return ret; } @@ -348,142 +393,147 @@ public class StatementGenerator { private AssignLeftSide convert(Java17Parser.LeftHandSideContext leftHandSide) { Expression leftSide = generateLocalOrFieldVarOrClassName(leftHandSide.getText(), leftHandSide.getStart()); - if(leftSide instanceof FieldVar)return new AssignToField((FieldVar) leftSide); - else if (leftSide instanceof LocalVar)return new AssignToLocal((LocalVar) leftSide); - else throw new NotImplementedException(); + if (leftSide instanceof FieldVar) + return new AssignToField((FieldVar) leftSide); + else if (leftSide instanceof LocalVar) + return new AssignToLocal((LocalVar) leftSide); + else + throw new NotImplementedException(); } - private Statement convert(Java17Parser.IfThenStatementContext stmt){ - //TODO - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.IfThenElseStatementContext stmt){ + private Statement convert(Java17Parser.ConditionalstmtContext stmt) { + if (Objects.isNull(stmt.ELSE())) + throw new NotImplementedException(); Expression expr = convert(stmt.parExpression().expression()); Statement thenBlock = convert(stmt.statementNoShortIf()); Statement elseBlock = convert(stmt.statement()); return new IfStmt(TypePlaceholder.fresh(stmt.getStart()), expr, thenBlock, elseBlock, stmt.getStart()); } - private Statement convert(Java17Parser.IfThenElseStatementNoShortIfContext stmt){ - //TODO + private Statement convert(Java17Parser.IfThenElseStatementNoShortIfContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.AssertStatementContext stmt){ - //TODO + private Statement convert(Java17Parser.AssertstmtContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.SwitchStatementContext stmt){ - //TODO + private Statement convert(Java17Parser.SwitchStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.SwitchBlockContext stmt){ - //TODO + private Statement convert(Java17Parser.SwitchBlockContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.SwitchBlockStatementGroupContext stmt){ - //TODO + private Statement convert(Java17Parser.SwitchBlockStatementGroupContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.WhileStatementContext stmt){ + private Statement convert(Java17Parser.WhileStatementContext stmt) { Expression expr = convert(stmt.parExpression().expression()); Statement block = convert(stmt.statement()); - return new WhileStmt(expr, block,stmt.getStart()); + return new WhileStmt(expr, block, stmt.getStart()); } - private Statement convert(Java17Parser.WhileStatementNoShortIfContext stmt){ - //TODO + private Statement convert(Java17Parser.WhileStatementNoShortIfContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.DoStatementContext stmt){ + private Statement convert(Java17Parser.DoStatementContext stmt) { Statement block = convert(stmt.statement()); Expression expr = convert(stmt.parExpression().expression()); - return new DoStmt(expr,block,stmt.getStart()); + return new DoStmt(expr, block, stmt.getStart()); } - private Statement convert(Java17Parser.ForStatementContext stmt){ - if(stmt.basicForStatement() != null){ + private Statement convert(Java17Parser.ForStatementContext stmt) { + if (stmt.basicForStatement() != null) { return convert(stmt.basicForStatement()); - }else if(stmt.enhancedForStatement() != null){ + } else if (stmt.enhancedForStatement() != null) { return convert(stmt.enhancedForStatement()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } - private Statement convert(Java17Parser.ForStatementNoShortIfContext stmt){ - if(stmt.basicForStatementNoShortIf() != null){ + private Statement convert(Java17Parser.ForStatementNoShortIfContext stmt) { + if (stmt.basicForStatementNoShortIf() != null) { return convert(stmt.basicForStatementNoShortIf()); - }else if(stmt.enhancedForStatementNoShortIf() != null){ + } else if (stmt.enhancedForStatementNoShortIf() != null) { return convert(stmt.enhancedForStatementNoShortIf()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } - private Statement convert(Java17Parser.BasicForStatementContext stmt){ - //TODO + private Statement convert(Java17Parser.BasicForStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.BasicForStatementNoShortIfContext stmt){ - //TODO + private Statement convert(Java17Parser.BasicForStatementNoShortIfContext stmt) { + // TODO throw new NotImplementedException(); } - private List convert(Java17Parser.ForInitContext stmt){ - if(stmt.statementExpressionList() != null){ + private List convert(Java17Parser.ForInitContext stmt) { + if (stmt.statementExpressionList() != null) { return Arrays.asList(convert(stmt.statementExpressionList())); - }else if(stmt.localVariableDeclaration() != null){ + } else if (stmt.localVariableDeclaration() != null) { return convert(stmt.localVariableDeclaration()); - }else throw new NotImplementedException(); + } else + throw new NotImplementedException(); } private List convert(Java17Parser.LocalVariableDeclarationContext declaration) { List ret = new ArrayList<>(); - if(declaration.variableModifier() != null && declaration.variableModifier().size() > 0){ - //TODO + if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) { + // TODO throw new NotImplementedException(); } RefTypeOrTPHOrWildcardOrGeneric type; - if(declaration.unannTypeOrAuto().unannType() == null){ + if (declaration.unannTypeOrAuto().unannType() == null) { type = TypePlaceholder.fresh(declaration.getStart()); - }else{ + } else { type = TypeGenerator.convert(declaration.unannTypeOrAuto().unannType(), reg, generics); } ret.addAll(generateLocalVariableAssignments(declaration.variableDeclaratorList().variableDeclarator(), type)); return ret; } - private List generateLocalVariableAssignments(List varDeclarators, RefTypeOrTPHOrWildcardOrGeneric type){ + private List generateLocalVariableAssignments( + List varDeclarators, RefTypeOrTPHOrWildcardOrGeneric type) { List ret = new ArrayList<>(); - for(Java17Parser.VariableDeclaratorContext varDecl : varDeclarators){ + for (Java17Parser.VariableDeclaratorContext varDecl : varDeclarators) { TerminalNode name = varDecl.variableDeclaratorId().Identifier(); ret.add(new LocalVarDecl(name.getText(), type, name.getSymbol())); this.localVars.put(name.getText(), type); - if(varDecl.variableInitializer() != null){ + if (varDecl.variableInitializer() != null) { Expression initValue; - if(varDecl.variableInitializer().arrayInitializer() != null){ + if (varDecl.variableInitializer().arrayInitializer() != null) { throw new NotImplementedException(); - }else{ + } else { initValue = convert(varDecl.variableInitializer().expression()); } - ret.add(new Assign(new AssignToLocal(new LocalVar(name.getText(), type, name.getSymbol())) - , initValue, name.getSymbol())); + ret.add(new Assign(new AssignToLocal(new LocalVar(name.getText(), type, name.getSymbol())), initValue, + name.getSymbol())); } } return ret; } - public Statement generateFieldAssignment(Java17Parser.VariableDeclaratorContext varDecl, RefTypeOrTPHOrWildcardOrGeneric type){ + public Statement generateFieldAssignment(Java17Parser.VariableDeclaratorContext varDecl, + RefTypeOrTPHOrWildcardOrGeneric type) { TerminalNode name = varDecl.variableDeclaratorId().Identifier(); Expression initValue; - if(varDecl.variableInitializer().arrayInitializer() != null){ + if (varDecl.variableInitializer().arrayInitializer() != null) { throw new NotImplementedException(); - }else{ + } else { initValue = convert(varDecl.variableInitializer().expression()); } return (new Assign(new AssignToField( @@ -492,204 +542,205 @@ public class StatementGenerator { initValue, name.getSymbol())); } - private Statement convert(Java17Parser.ForUpdateContext stmt){ + private Statement convert(Java17Parser.ForUpdateContext stmt) { return convert(stmt.statementExpressionList()); } - private Statement convert(Java17Parser.StatementExpressionListContext stmt){ - //TODO + private Statement convert(Java17Parser.StatementExpressionListContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.EnhancedForStatementContext stmt){ - //TODO + private Statement convert(Java17Parser.EnhancedForStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.EnhancedForStatementNoShortIfContext stmt){ - //TODO + private Statement convert(Java17Parser.EnhancedForStatementNoShortIfContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.BreakStatementContext stmt){ - //TODO + private Statement convert(Java17Parser.BreakStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.ContinueStatementContext stmt){ - //TODO + private Statement convert(Java17Parser.ContinueStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.ReturnStatementContext stmt){ - if(stmt.expression() != null){ - return new Return( convert(stmt.expression()),stmt.getStart() ); - }else{ + private Statement convert(Java17Parser.ReturnStatementContext stmt) { + if (stmt.expression() != null) { + return new Return(convert(stmt.expression()), stmt.getStart()); + } else { return new ReturnVoid(stmt.getStart()); } } - private Statement convert(Java17Parser.ThrowStatementContext stmt){ - //TODO + private Statement convert(Java17Parser.ThrowStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.SynchronizedStatementContext stmt){ - //TODO + private Statement convert(Java17Parser.SynchronizedStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.TryStatementContext stmt){ - //TODO + private Statement convert(Java17Parser.TryStatementContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.CatchesContext stmt){ - //TODO + private Statement convert(Java17Parser.CatchesContext stmt) { + // TODO throw new NotImplementedException(); } - private Statement convert(Java17Parser.CatchClauseContext stmt){ - //TODO + private Statement convert(Java17Parser.CatchClauseContext stmt) { + // TODO throw new NotImplementedException(); } -/* -***************+ Expression Conversions: - */ + /* + *************** + Expression Conversions: + */ private Expression convert(Java17Parser.ExpressionContext expression) { - if(expression.lambdaExpression()!=null){ + if (expression.lambdaExpression() != null) { return convert(expression.lambdaExpression()); - }else{ + } else { return convert(expression.assignmentExpression()); } } private Expression convert(Java17Parser.AssignmentExpressionContext expression) { - if(expression.conditionalExpression() != null){ + if (expression.conditionalExpression() != null) { return convert(expression.conditionalExpression()); - }else{ + } else { return convert(expression.assignment()); } } private Expression convert(Java17Parser.ConditionalExpressionContext expression) { - if(expression.conditionalOrExpression() != null){ + if (expression.conditionalOrExpression() != null) { return convert(expression.conditionalOrExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java17Parser.ConditionalOrExpressionContext expression) { - if(expression.conditionalOrExpression() == null){ + if (expression.conditionalOrExpression() == null) { return convert(expression.conditionalAndExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java17Parser.ConditionalAndExpressionContext expression) { - if(expression.conditionalAndExpression() == null){ + if (expression.conditionalAndExpression() == null) { return convert(expression.inclusiveOrExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java17Parser.InclusiveOrExpressionContext expression) { - if(expression.inclusiveOrExpression() == null){ + if (expression.inclusiveOrExpression() == null) { return convert(expression.exclusiveOrExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java17Parser.ExclusiveOrExpressionContext expression) { - if(expression.exclusiveOrExpression() == null){ + if (expression.exclusiveOrExpression() == null) { return convert(expression.andExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java17Parser.AndExpressionContext expression) { - if(expression.andExpression() == null){ + if (expression.andExpression() == null) { return convert(expression.equalityExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java17Parser.EqualityExpressionContext expression) { - if(expression.equalityExpression() == null){ + if (expression.equalityExpression() == null) { return convert(expression.relationalExpression()); - }else{ + } else { String operator = expression.getChild(1).getText(); Expression leftSide = convert(expression.equalityExpression()); Expression rightSide = convert(expression.relationalExpression()); - return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), leftSide, rightSide, expression.getStart()); + return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), + leftSide, rightSide, expression.getStart()); } } private Expression convert(Java17Parser.RelationalExpressionContext expression) { - if(expression.relationalExpression() == null) { + if (expression.relationalExpression() == null) { return convert(expression.shiftExpression()); - }else { + } else { String operator = expression.getChild(1).getText(); return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), convert(expression.relationalExpression()), convert(expression.shiftExpression()), expression.getStart()); } - //}else throw new NotImplementedException(); + // }else throw new NotImplementedException(); } private BinaryExpr.Operator convertBinaryOperator(String operator) { - //return BinaryExpr.Operator.ADD; - if(operator.equals("+")) { - return BinaryExpr.Operator.ADD; - }else if(operator.equals("-")) { - return BinaryExpr.Operator.SUB; - }else if(operator.equals("*")) { - return BinaryExpr.Operator.MUL; - }else if(operator.equals("&")) { - return BinaryExpr.Operator.AND; - }else if(operator.equals("|")) { - return BinaryExpr.Operator.OR; - }else if(operator.equals("/")) { - return BinaryExpr.Operator.DIV; - }else if(operator.equals("<")) { - return BinaryExpr.Operator.LESSTHAN; - }else if(operator.equals(">")) { - return BinaryExpr.Operator.BIGGERTHAN; - }else if(operator.equals(">=")) { - return BinaryExpr.Operator.BIGGEREQUAL; - } else if(operator.equals("<=")) { - return BinaryExpr.Operator.LESSEQUAL; - } else if(operator.equals("==")) { - return BinaryExpr.Operator.EQUAL; - } else if(operator.equals("!=")) { - return BinaryExpr.Operator.NOTEQUAL; - } else { - throw new NotImplementedException(); - } -// throw new NotImplementedException(); + // return BinaryExpr.Operator.ADD; + if (operator.equals("+")) { + return BinaryExpr.Operator.ADD; + } else if (operator.equals("-")) { + return BinaryExpr.Operator.SUB; + } else if (operator.equals("*")) { + return BinaryExpr.Operator.MUL; + } else if (operator.equals("&")) { + return BinaryExpr.Operator.AND; + } else if (operator.equals("|")) { + return BinaryExpr.Operator.OR; + } else if (operator.equals("/")) { + return BinaryExpr.Operator.DIV; + } else if (operator.equals("<")) { + return BinaryExpr.Operator.LESSTHAN; + } else if (operator.equals(">")) { + return BinaryExpr.Operator.BIGGERTHAN; + } else if (operator.equals(">=")) { + return BinaryExpr.Operator.BIGGEREQUAL; + } else if (operator.equals("<=")) { + return BinaryExpr.Operator.LESSEQUAL; + } else if (operator.equals("==")) { + return BinaryExpr.Operator.EQUAL; + } else if (operator.equals("!=")) { + return BinaryExpr.Operator.NOTEQUAL; + } else { + throw new NotImplementedException(); + } + // throw new NotImplementedException(); } private Expression convert(Java17Parser.ShiftExpressionContext expression) { - if(expression.shiftExpression() == null){ + if (expression.shiftExpression() == null) { return convert(expression.additiveExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java17Parser.AdditiveExpressionContext expression) { - if(expression.additiveExpression() == null){ + if (expression.additiveExpression() == null) { return convert(expression.multiplicativeExpression()); - }else { + } else { Expression leftSide = convert(expression.additiveExpression()); Expression rightSide = convert(expression.multiplicativeExpression()); BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); @@ -699,76 +750,81 @@ public class StatementGenerator { } private Expression convert(Java17Parser.MultiplicativeExpressionContext expression) { - if(expression.multiplicativeExpression() == null){ + if (expression.multiplicativeExpression() == null) { return convert(expression.unaryExpression()); - }else{ - Expression leftSide = convert(expression.multiplicativeExpression()); - Expression rightSide = convert(expression.unaryExpression()); - BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); - Token offset = expression.getStart(); - return new BinaryExpr(op, TypePlaceholder.fresh(offset), leftSide, rightSide, offset); + } else { + Expression leftSide = convert(expression.multiplicativeExpression()); + Expression rightSide = convert(expression.unaryExpression()); + BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); + Token offset = expression.getStart(); + return new BinaryExpr(op, TypePlaceholder.fresh(offset), leftSide, rightSide, offset); } } private Expression convert(Java17Parser.UnaryExpressionContext expression) { - if(expression.preIncrementExpression() != null){ + if (expression.preIncrementExpression() != null) { return convert(expression.preIncrementExpression()); - }else if(expression.preDecrementExpression() != null){ + } else if (expression.preDecrementExpression() != null) { return convert(expression.preDecrementExpression()); - }else if(expression.unaryExpressionNotPlusMinus() != null){ + } else if (expression.unaryExpressionNotPlusMinus() != null) { return convert(expression.unaryExpressionNotPlusMinus()); - }else if(expression.getText().startsWith("+")){ + } else if (expression.getText().startsWith("+")) { return new UnaryExpr(UnaryExpr.Operation.PLUS, convert(expression.unaryExpression()), TypePlaceholder.fresh(expression.getStart()), expression.getStart()); - }else if(expression.getText().startsWith("-")){ + } else if (expression.getText().startsWith("-")) { return new UnaryExpr(UnaryExpr.Operation.MINUS, convert(expression.unaryExpression()), TypePlaceholder.fresh(expression.getStart()), expression.getStart()); - }else{ - //Diese Exceptions sollte nie geworfen werden. - //Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben. + } else { + // Diese Exceptions sollte nie geworfen werden. + // Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben. throw new NotImplementedException(); } } private Expression convert(Java17Parser.UnaryExpressionNotPlusMinusContext expression) { - if(expression.postfixExpression() != null){ + if (expression.postfixExpression() != null) { return convert(expression.postfixExpression()); - }else if(expression.castExpression() != null){ + } else if (expression.castExpression() != null) { return convert(expression.castExpression()); - }else{ + } else { throw new NotImplementedException(); } } private Expression convert(Java17Parser.CastExpressionContext expressionContext) { Expression expr = null; - if(expressionContext.unaryExpression() != null){ + if (expressionContext.unaryExpression() != null) { throw new NotImplementedException(); - }else if(expressionContext.unaryExpressionNotPlusMinus() != null){ + } else if (expressionContext.unaryExpressionNotPlusMinus() != null) { expr = convert(expressionContext.unaryExpressionNotPlusMinus()); - }else if(expressionContext.lambdaExpression() != null){ + } else if (expressionContext.lambdaExpression() != null) { expr = convert(expressionContext.lambdaExpression()); } - return new CastExpr(TypeGenerator.convert(expressionContext.referenceType(), reg, generics),expr, expressionContext.getStart()); + return new CastExpr(TypeGenerator.convert(expressionContext.referenceType(), reg, generics), expr, + expressionContext.getStart()); } private Expression convert(Java17Parser.PostfixExpressionContext expression) { Expression expr; - if(expression.primary() != null){ + if (expression.primary() != null) { expr = convert(expression.primary()); - }else{ + } else { expr = convert(expression.expressionName()); } - if(expression.postDecrementExpression_lf_postfixExpression() == null && - expression.postIncrementExpression_lf_postfixExpression() == null){ + if (expression.postDecrementExpression_lf_postfixExpression() == null && + expression.postIncrementExpression_lf_postfixExpression() == null) { return expr; } - for(Java17Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression.postIncrementExpression_lf_postfixExpression()){ - expr = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, expr, TypePlaceholder.fresh(inc.getStart()), inc.getStart()); + for (Java17Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression + .postIncrementExpression_lf_postfixExpression()) { + expr = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, expr, TypePlaceholder.fresh(inc.getStart()), + inc.getStart()); } - for(Java17Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression.postDecrementExpression_lf_postfixExpression()){ - expr = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(dec.getStart()), dec.getStart()); + for (Java17Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression + .postDecrementExpression_lf_postfixExpression()) { + expr = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(dec.getStart()), + dec.getStart()); } return expr; @@ -776,14 +832,14 @@ public class StatementGenerator { private Expression convert(Java17Parser.PrimaryContext primary) { Expression expr; - if(primary.primaryNoNewArray_lfno_primary()!=null){ + if (primary.primaryNoNewArray_lfno_primary() != null) { expr = convert(primary.primaryNoNewArray_lfno_primary()); - }else{ + } else { expr = convert(primary.arrayCreationExpression()); } - if(primary.primaryNoNewArray_lf_primary() != null && primary.primaryNoNewArray_lf_primary().size()>0){ - for(Java17Parser.PrimaryNoNewArray_lf_primaryContext e : primary.primaryNoNewArray_lf_primary()){ + if (primary.primaryNoNewArray_lf_primary() != null && primary.primaryNoNewArray_lf_primary().size() > 0) { + for (Java17Parser.PrimaryNoNewArray_lf_primaryContext e : primary.primaryNoNewArray_lf_primary()) { expr = convert(expr, e); } } @@ -791,54 +847,54 @@ public class StatementGenerator { } private Expression convert(Expression expr, Java17Parser.PrimaryNoNewArray_lf_primaryContext e) { - if(e.classInstanceCreationExpression_lf_primary() != null){ + if (e.classInstanceCreationExpression_lf_primary() != null) { throw new NotImplementedException(); - }else if(e.fieldAccess_lf_primary() != null){ - + } else if (e.fieldAccess_lf_primary() != null) { + throw new NotImplementedException(); - }else if(e.arrayAccess_lf_primary() != null){ + } else if (e.arrayAccess_lf_primary() != null) { throw new NotImplementedException(); - }else if(e.methodReference_lf_primary() != null){ + } else if (e.methodReference_lf_primary() != null) { throw new NotImplementedException(); - }else { + } else { Java17Parser.MethodInvocation_lf_primaryContext ctxt = e.methodInvocation_lf_primary(); String methodName = ctxt.Identifier().toString(); ArrayList argTypes = ctxt.argumentList().expression().stream() - .map(x -> TypePlaceholder.fresh(e.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); - return new MethodCall(TypePlaceholder.fresh(e.getStart()), getReceiver(expr), methodName, - convert(ctxt.argumentList()), TypePlaceholder.fresh(e.getStart()), argTypes, e.getStart()); + .map(x -> TypePlaceholder.fresh(e.getStart())) + .collect(Collectors.toCollection(ArrayList::new)); + return new MethodCall(TypePlaceholder.fresh(e.getStart()), getReceiver(expr), methodName, + convert(ctxt.argumentList()), TypePlaceholder.fresh(e.getStart()), argTypes, e.getStart()); } } - + private Expression convert(Java17Parser.ArrayCreationExpressionContext expression) { throw new NotImplementedException(); } private Expression convert(Java17Parser.PrimaryNoNewArray_lfno_primaryContext expression) { - if(expression.literal() != null){ + if (expression.literal() != null) { return convert(expression.literal()); - }else if(expression.parExpression()!=null){ + } else if (expression.parExpression() != null) { return convert(expression.parExpression().expression()); - }else if(expression.methodInvocation_lfno_primary() != null){ + } else if (expression.methodInvocation_lfno_primary() != null) { return convert(expression.methodInvocation_lfno_primary()); - }else if(expression.classInstanceCreationExpression_lfno_primary() != null) { + } else if (expression.classInstanceCreationExpression_lfno_primary() != null) { return convert(expression.classInstanceCreationExpression_lfno_primary()); - }else if(expression.getText().equals("this")) { + } else if (expression.getText().equals("this")) { return new This(expression.getStart()); - }else if(expression.fieldAccess_lfno_primary() != null){ + } else if (expression.fieldAccess_lfno_primary() != null) { return convert(expression.fieldAccess_lfno_primary()); - }else if(expression.methodReference_lfno_primary() != null){ + } else if (expression.methodReference_lfno_primary() != null) { throw new NotImplementedException(); - }else if(expression.typeName() != null){ + } else if (expression.typeName() != null) { throw new NotImplementedException(); - }else if(expression.unannPrimitiveType() != null){ + } else if (expression.unannPrimitiveType() != null) { throw new NotImplementedException(); - }else if(expression.arrayAccess_lfno_primary() != null){ + } else if (expression.arrayAccess_lfno_primary() != null) { throw new NotImplementedException(); - }else if(expression.fieldAccess_lfno_primary() != null){ + } else if (expression.fieldAccess_lfno_primary() != null) { throw new NotImplementedException(); - }else{ + } else { throw new NotImplementedException(); } } @@ -849,126 +905,131 @@ public class StatementGenerator { private Expression convert(Java17Parser.ClassInstanceCreationExpression_lfno_primaryContext newExpression) { Java17Parser.TypeArgumentsContext genericArgs = null; - if(newExpression.expressionName()!= null)throw new NotImplementedException(); - if(newExpression.typeArgumentsOrDiamond()!= null){ - if(newExpression.typeArgumentsOrDiamond().typeArguments()!=null){ + if (newExpression.expressionName() != null) + throw new NotImplementedException(); + if (newExpression.typeArgumentsOrDiamond() != null) { + if (newExpression.typeArgumentsOrDiamond().typeArguments() != null) { genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments(); } } - if(newExpression.typeArguments()!= null)throw new NotImplementedException(); + if (newExpression.typeArguments() != null) + throw new NotImplementedException(); TerminalNode identifier = newExpression.Identifier(0); - RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(),genericArgs,identifier.getSymbol(),reg,generics); + RefType newClass = (RefType) TypeGenerator.convertTypeName(identifier.getText(), genericArgs, + identifier.getSymbol(), reg, generics); ArgumentList args = convert(newExpression.argumentList()); ArrayList argTypes = args.getArguments().stream() - .map(x -> TypePlaceholder.fresh(newExpression.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); + .map(x -> TypePlaceholder.fresh(newExpression.getStart())) + .collect(Collectors.toCollection(ArrayList::new)); return new NewClass(newClass, args, null, argTypes, newExpression.getStart()); } private Expression convert(Java17Parser.LiteralContext literal) { - if(literal.IntegerLiteral() != null){ + if (literal.IntegerLiteral() != null) { Number value = Integer.parseInt(literal.IntegerLiteral().getText()); return new Literal(TypePlaceholder.fresh(literal.getStart()), value, literal.getStart()); - } else if(literal.FloatingPointLiteral()!= null){ + } else if (literal.FloatingPointLiteral() != null) { Number value = Double.parseDouble(literal.FloatingPointLiteral().getText()); return new Literal(TypePlaceholder.fresh(literal.getStart()), value, literal.getStart()); - }else if(literal.BooleanLiteral() != null){ - RefType type = new RefType(reg.getName("java.lang.Boolean"),literal.getStart()); + } else if (literal.BooleanLiteral() != null) { + RefType type = new RefType(reg.getName("java.lang.Boolean"), literal.getStart()); return new Literal(type, Boolean.parseBoolean(literal.BooleanLiteral().getText()), literal.getStart()); - }else if(literal.CharacterLiteral() != null){ - RefType type = new RefType(reg.getName("java.lang.Character"),literal.getStart()); + } else if (literal.CharacterLiteral() != null) { + RefType type = new RefType(reg.getName("java.lang.Character"), literal.getStart()); return new Literal(type, - // das gibt immer ' zurück, der Char befindet sich in Position 1 - //literal.CharacterLiteral().getText().charAt(0), + // das gibt immer ' zurück, der Char befindet sich in Position 1 + // literal.CharacterLiteral().getText().charAt(0), literal.CharacterLiteral().getText().charAt(1), literal.getStart()); - }else if(literal.StringLiteral()!=null){ - RefType type = new RefType(reg.getName("java.lang.String"),literal.getStart()); + } else if (literal.StringLiteral() != null) { + RefType type = new RefType(reg.getName("java.lang.String"), literal.getStart()); return new Literal(type, - literal.StringLiteral().getText().substring(1, literal.StringLiteral().getText().length()-1), + literal.StringLiteral().getText().substring(1, literal.StringLiteral().getText().length() - 1), literal.getStart()); - }else if(literal.NullLiteral() != null){ + } else if (literal.NullLiteral() != null) { return new Literal(TypePlaceholder.fresh(literal.getStart()), null, literal.getStart()); - }else { + } else { throw new NotImplementedException(); } } private Expression convert(Java17Parser.MethodInvocation_lfno_primaryContext methodInvocationContext) { String name; - if(methodInvocationContext.methodName()!=null){ + if (methodInvocationContext.methodName() != null) { name = methodInvocationContext.methodName().Identifier().getText(); - }else{// if(methodInvocationContext.Identifier() != null){ + } else {// if(methodInvocationContext.Identifier() != null){ name = methodInvocationContext.Identifier().getText(); } Expression receiver; - if(methodInvocationContext.typeName() != null){ - receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), methodInvocationContext.typeName().getStart()); - }else if(methodInvocationContext.expressionName()!=null){ + if (methodInvocationContext.typeName() != null) { + receiver = generateLocalOrFieldVarOrClassName(methodInvocationContext.typeName().getText(), + methodInvocationContext.typeName().getStart()); + } else if (methodInvocationContext.expressionName() != null) { receiver = convert(methodInvocationContext.expressionName()); - }else if(methodInvocationContext.toString().startsWith("super")) { + } else if (methodInvocationContext.toString().startsWith("super")) { receiver = new Super(methodInvocationContext.getStart()); - }else if(methodInvocationContext.methodName() != null){ + } else if (methodInvocationContext.methodName() != null) { receiver = new This(methodInvocationContext.methodName().getStart()); - }else { + } else { throw new NotImplementedException(); } ArgumentList argumentList = convert(methodInvocationContext.argumentList()); ArrayList argTypes = argumentList.getArguments().stream() - .map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())) - .collect(Collectors.toCollection(ArrayList::new)); - MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), - getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), - argTypes, methodInvocationContext.getStart()); + .map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())) + .collect(Collectors.toCollection(ArrayList::new)); + MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), + getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), + argTypes, methodInvocationContext.getStart()); return ret; } private Expression convert(Java17Parser.LambdaExpressionContext expression) { Java17Parser.LambdaParametersContext lambdaParams = expression.lambdaParameters(); ParameterList params; - if(lambdaParams.Identifier() != null){ + if (lambdaParams.Identifier() != null) { List parameterList = new ArrayList<>(); parameterList.add(new FormalParameter(lambdaParams.Identifier().getText(), TypePlaceholder.fresh(lambdaParams.getStart()), lambdaParams.getStart())); params = new ParameterList(parameterList, lambdaParams.getStart()); - }else if(lambdaParams.formalParameterList() != null){ + } else if (lambdaParams.formalParameterList() != null) { params = convert(lambdaParams.formalParameterList()); - //}else if( lambdaParams.inferredFormalParameterList != null){ - }else { + // }else if( lambdaParams.inferredFormalParameterList != null){ + } else { params = new ParameterList(new ArrayList<>(), expression.getStart()); } - HashMap lambdaLocals = new HashMap<>(); + HashMap lambdaLocals = new HashMap<>(); lambdaLocals.putAll(localVars); - for(FormalParameter param : params.getFormalparalist()){ + for (FormalParameter param : params.getFormalparalist()) { lambdaLocals.put(param.getName(), param.getType()); } StatementGenerator lambdaGenerator = new StatementGenerator(reg, generics, fields, lambdaLocals); Block block; - if(expression.lambdaBody().expression() != null){ + if (expression.lambdaBody().expression() != null) { List statements = new ArrayList<>(); statements.add(new Return(lambdaGenerator.convert(expression.lambdaBody().expression()), expression.lambdaBody().expression().getStart())); block = new Block(statements, expression.lambdaBody().getStart()); - }else{ + } else { block = lambdaGenerator.convert(expression.lambdaBody().block(), true); } List funNParams = new ArrayList<>(); - funNParams.add(TypePlaceholder.fresh(expression.getStart()));//ret-Type - params.getFormalparalist().forEach(formalParameter -> //Für jeden Parameter einen TPH anfügen: - funNParams.add(TypePlaceholder.fresh(expression.getStart()))); + funNParams.add(TypePlaceholder.fresh(expression.getStart()));// ret-Type + params.getFormalparalist().forEach(formalParameter -> // Für jeden Parameter einen TPH anfügen: + funNParams.add(TypePlaceholder.fresh(expression.getStart()))); RefTypeOrTPHOrWildcardOrGeneric lambdaType = TypePlaceholder.fresh(expression.getStart()); - //RefType lambdaType = new RefType(reg.getName("Fun"+params.getFormalparalist().size()), - //funNParams, name.getStart()); + // RefType lambdaType = new + // RefType(reg.getName("Fun"+params.getFormalparalist().size()), + // funNParams, name.getStart()); return new LambdaExpression(lambdaType, params, block, expression.getStart()); } }