From d362858184838f9283e841c120b248ecda01166e Mon Sep 17 00:00:00 2001 From: luca9913 Date: Wed, 1 Mar 2023 06:49:59 +0100 Subject: [PATCH] =?UTF-8?q?Umbau=20des=20StatementGenerators=20fortgef?= =?UTF-8?q?=C3=BChrt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parser/antlr/Java17Parser.g4 | 8 +- .../StatementGenerator.java | 968 ++++++++---------- .../SyntaxTreeGenerator/TypeGenerator.java | 40 +- 3 files changed, 489 insertions(+), 527 deletions(-) diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index 588c468c..eee359cb 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -620,7 +620,7 @@ expression | SUPER superSuffix | explicitGenericInvocation ) # dottedexpression - | expression '[' expression ']' #arrayexpression + | expression '[' expression ']' #arrayaccessexpression | methodCall #methodcallexpression | NEW creator #newinstanceexpression | '(' annotation* typeType ('&' typeType)* ')' expression #castexpression @@ -628,10 +628,10 @@ expression | prefix=('+'|'-'|'++'|'--'|'~'|'!') expression #prefixexpression | expression bop=('*'|'/'|'%') expression #mathmuldivmodexpression | expression bop=('+'|'-') expression #mathaddsubexpression - | expression ('<' '<' | '>' '>' '>' | '>' '>') expression #shifexpression - | expression bop=('<=' | '>=' | '>' | '<') expression #compareexpression + | expression ('<' '<' | '>' '>' '>' | '>' '>') expression #shiftexpression + | expression bop=('<=' | '>=' | '>' | '<') expression #relationalexpression | expression bop=INSTANCEOF (typeType | pattern) #instanceofexpression - | expression bop=('==' | '!=') expression #compareequalityexpression + | expression bop=('==' | '!=') expression #equalityexpression | expression bop='&' expression #bitwiseandexpression | expression bop='^' expression #bitwisexorexpression | expression bop='|' expression #bitwiseorexpression diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 1fb0c6a6..c8023387 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -3,24 +3,34 @@ 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.ArrayaccessexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.AssertstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.AssignexpressionContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.BitwiseandexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BlockstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.BreakstmtContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.CastexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ConditionalstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ContinuestmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.DottedexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ForControlContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ForloopContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.IdentifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.LabeledstmtContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.LambdaLVTIParameterContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.LambdaexpressionContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.MathaddsubexpressionContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.MathmuldivmodexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.MethodcallexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.MethodCallContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.NewinstanceexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PostfixexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrefixexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryexpressionContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.RelationalexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ReturnstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SemistmtContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.ShiftexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.StmtexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionstmtContext; @@ -30,9 +40,11 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.ThrowstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.WhileloopContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.YieldstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.DowhileloopContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.EqualityexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ExpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchblockContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchresourceContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.TypeArgumentsContext; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.*; @@ -169,90 +181,340 @@ public class StatementGenerator { Token offset = stmt.getStart(); switch (expr) { case DottedexpressionContext dotted: - return convertDottedExpression(dotted, offset); + return convertToStatement(dotted, offset); case MethodcallexpressionContext methodc: - return convertMethodCall(methodc.methodCall(), offset); + return convert(methodc.methodCall(), offset); case NewinstanceexpressionContext newinstance: - - break; + return convert(newinstance.creator()); case AssignexpressionContext assignment: - return convertAssignment(assignment); - case PostfixexpressionContext postfix: - return convertPostfixStatementExpression(postfix.expression(), postfix.postfix.getText(), offset); - case PrefixexpressionContext prefix: - return convertPrefixStatementExpression(prefix.expression(), prefix.prefix.getText(), offset); - default: - throw new NotImplementedException(); - } - } - - private Statement convertAssignment(AssignexpressionContext expr) { - switch (expr.bop.getText()) { - case "=": - ExpressionContext leftside = expr.expression(0); - AssignLeftSide leftHandSide = convert(leftside.getText(), leftside.getStart()); - Statement ret = new Assign(leftHandSide, convert(expr.expression(1)), expr.getStart()); + Statement ret = convert(assignment); + ret.setStatement(); + return ret; + case PostfixexpressionContext postfix: + ret = convert(postfix); + ret.setStatement(); + return ret; + case PrefixexpressionContext prefix: + ret = convert(prefix); ret.setStatement(); return ret; - case "+=": - case "-=": - case "*=": - case "/=": - case "&=": - case "|=": - case "^=": - case ">>=": - case ">>>=": - case "<<=": - case "%=": default: throw new NotImplementedException(); } } - private Statement convertPrefixStatementExpression(ExpressionContext expr, String op, Token offset) { - Expression argument = convert(expr); - Statement ret; - if (op.equals("++")) { - ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset); - ret.setStatement(); - return ret; - } else if (op.equals("--")) { - ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, argument, TypePlaceholder.fresh(offset), offset); - ret.setStatement(); - return ret; - } else { - throw new NotImplementedException(); - } - } - - private Statement convertPostfixStatementExpression(ExpressionContext expr, String op, Token offset) { - Expression argument = convert(expr); - Statement ret; - if (op.equals("++")) { - ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, argument, TypePlaceholder.fresh(offset), offset); - ret.setStatement(); - return ret; - } else if (op.equals("--")) { - ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, argument, TypePlaceholder.fresh(offset), offset); - ret.setStatement(); - return ret; - } else { - throw new NotImplementedException(); - } - } - - private Statement convertDottedExpression(DottedexpressionContext expr, Token offset) { + private Statement convertToStatement(DottedexpressionContext expr, Token offset) { if (!Objects.isNull(expr.methodCall())) { - return convertMethodCall(expr.methodCall(), expr.expression(), offset); + return convert(expr.methodCall(), expr.expression(), offset); } else if (!Objects.isNull(expr.NEW())) { - return null; + return convert(expr.innerCreator()); } else { throw new NotImplementedException(); } } - private MethodCall convertMethodCall(MethodCallContext expr, Token offset) { + private Statement convert(Java17Parser.CreatorContext creator) { + if (creator.nonWildcardTypeArguments() != null) + throw new NotImplementedException(); + + RefType newclass = convert(creator.createdName()); + + ArgumentList args = convertArguments(creator.classCreatorRest().arguments().expressionList()); + ArrayList argTypes = args.getArguments().stream() + .map(x -> TypePlaceholder.fresh(creator.getStart())) + .collect(Collectors.toCollection(ArrayList::new)); + Statement ret = new NewClass(newclass, args, null, argTypes, creator.getStart()); + ret.setStatement(); + return ret; + } + + private RefType convert(Java17Parser.CreatedNameContext createdname) { + Java17Parser.TypeArgumentsContext genericArgs = null; + int typeargcount = createdname.typeArgumentsOrDiamond().size(); + if (typeargcount == 1) { + genericArgs = createdname.typeArgumentsOrDiamond().get(0).typeArguments(); + } else { + // nach Java8 sind auch mehrere Typangaben im creator möglich. nach annahme wird + // dies aber noch nicht unterstützt. + throw new NotImplementedException(); + } + + IdentifierContext identifier = createdname.identifier().get(0); + return (RefType) TypeGenerator.convertTypeName(identifier.getText(), genericArgs, + identifier.getStart(), reg, generics); + } + + private Statement convert(Java17Parser.InnerCreatorContext innercreator) { + Java17Parser.NonWildcardTypeArgumentsContext genericArgs = null; + + if (!Objects.isNull(innercreator.nonWildcardTypeArgumentsOrDiamond())) { + genericArgs = innercreator.nonWildcardTypeArgumentsOrDiamond().nonWildcardTypeArguments(); + } + IdentifierContext identifier = innercreator.identifier(); + RefType newclass = (RefType) TypeGenerator.convertTypeName(identifier.getText(), genericArgs, + identifier.getStart(), reg, generics); + ArgumentList args = convertArguments(innercreator.classCreatorRest().arguments().expressionList()); + ArrayList argTypes = args.getArguments().stream() + .map(x -> TypePlaceholder.fresh(innercreator.getStart())) + .collect(Collectors.toCollection(ArrayList::new)); + Statement ret = new NewClass(newclass, args, null, argTypes, innercreator.getStart()); + ret.setStatement(); + return ret; + } + + private Statement convert(Java17Parser.ConditionalstmtContext stmt) { + if (stmt.statement().size() < 2) + throw new NotImplementedException(); + Expression expr = convert(stmt.parExpression().expression()); + Statement thenBlock = convert(stmt.statement(0)); + Statement elseBlock = convert(stmt.statement(1)); + return new IfStmt(TypePlaceholder.fresh(stmt.getStart()), expr, thenBlock, elseBlock, stmt.getStart()); + } + + private Statement convert(Java17Parser.AssertstmtContext stmt) { + // TODO + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.SwitchstmtContext stmt) { + // TODO + throw new NotImplementedException(); + } + + private Expression convert(Java17Parser.SwitchExpressionContext switchexpression) { + // TODO + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.YieldstmtContext yieldstmt) { + // TODO + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.SwitchBlockStatementGroupContext stmt) { + // TODO + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.WhileloopContext stmt) { + Expression expr = convert(stmt.parExpression().expression()); + Statement block = convert(stmt.statement()); + return new WhileStmt(expr, block, stmt.getStart()); + } + + private Statement convert(Java17Parser.DowhileloopContext stmt) { + Statement block = convert(stmt.statement()); + Expression expr = convert(stmt.parExpression().expression()); + return new DoStmt(expr, block, stmt.getStart()); + } + + private Statement convert(Java17Parser.ForloopContext stmt) { + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.ExpressionListContext exprlist) { + // TODO + throw new NotImplementedException(); + } + + private ArgumentList convertArguments(Java17Parser.ExpressionListContext arglist) { + if (arglist == null) + return new ArgumentList(new ArrayList<>(), new NullToken()); + Token offset = new NullToken(); + List arguments = new ArrayList(); + for (ExpressionContext arg : arglist.expression()) { + arguments.add(convert(arg)); + } + if (arguments.size() > 0) + offset = arguments.get(0).getOffset(); + return new ArgumentList(arguments, offset); + } + + private List convert(Java17Parser.LocalVariableDeclarationContext declaration) { + List ret = new ArrayList<>(); + if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) { + // TODO + throw new NotImplementedException(); + } + RefTypeOrTPHOrWildcardOrGeneric type; + if (Objects.isNull(declaration.typeType()) || !Objects.isNull(declaration.VAR())) { + type = TypePlaceholder.fresh(declaration.getStart()); + } else { + type = TypeGenerator.convert(declaration.typeType(), reg, generics); + } + ret.addAll(generateLocalVariableAssignments(declaration.variableDeclarators().variableDeclarator(), type)); + return ret; + } + + private List generateLocalVariableAssignments( + List varDeclarators, RefTypeOrTPHOrWildcardOrGeneric type) { + List ret = new ArrayList<>(); + for (Java17Parser.VariableDeclaratorContext varDecl : varDeclarators) { + IdentifierContext name = varDecl.variableDeclaratorId().identifier(); + + ret.add(new LocalVarDecl(name.getText(), type, name.getStart())); + this.localVars.put(name.getText(), type); + if (varDecl.variableInitializer() != null) { + Expression initValue; + if (varDecl.variableInitializer().arrayInitializer() != null) { + throw new NotImplementedException(); + } else { + initValue = convert(varDecl.variableInitializer().expression()); + } + ret.add(new Assign(new AssignToLocal(new LocalVar(name.getText(), type, name.getStart())), initValue, + name.getStart())); + } + } + return ret; + } + + public Statement generateFieldAssignment(Java17Parser.VariableDeclaratorContext varDecl, + RefTypeOrTPHOrWildcardOrGeneric type) { + IdentifierContext name = varDecl.variableDeclaratorId().identifier(); + Expression initValue; + if (varDecl.variableInitializer().arrayInitializer() != null) { + throw new NotImplementedException(); + } else { + initValue = convert(varDecl.variableInitializer().expression()); + } + return (new Assign(new AssignToField( + new FieldVar(new This(varDecl.getStart()), name.getText(), + type, varDecl.getStart())), + initValue, name.getStart())); + } + + private Statement convert(Java17Parser.BreakstmtContext stmt) { + // TODO + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.ContinuestmtContext stmt) { + // TODO + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.ReturnstmtContext stmt) { + if (stmt.expression() != null) { + return new Return(convert(stmt.expression()), stmt.getStart()); + } else { + return new ReturnVoid(stmt.getStart()); + } + } + + private Statement convert(Java17Parser.ThrowstmtContext stmt) { + // TODO + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.SynchronizedstmtContext stmt) { + // TODO + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.TrycatchblockContext stmt) { + // TODO + throw new NotImplementedException(); + } + + private Statement convert(Java17Parser.CatchClauseContext stmt) { + // TODO + throw new NotImplementedException(); + } + + /* + *************** + Expression Conversions: + */ + + private Expression convert(Java17Parser.ExpressionContext expression) { + switch (expression) { + case PrimaryexpressionContext primary: + return convert(primary.primary()); + case DottedexpressionContext dotted: + return convert(new DottedexpressionContext(expression), expression.getStart()); + case MethodcallexpressionContext methodcall: + return convert(methodcall.methodCall(), methodcall.getStart()); + case NewinstanceexpressionContext newinstance: + return convert(newinstance.creator()); + case CastexpressionContext castexpr: + return convert(castexpr); + case PostfixexpressionContext postfexpr: + return convert(postfexpr); + case PrefixexpressionContext prefexpr: + return convert(prefexpr); + case MathmuldivmodexpressionContext mathexpr: + return convert(mathexpr); + case MathaddsubexpressionContext mathexpr: + return convert(mathexpr); + case RelationalexpressionContext comparison: + return convert(comparison); + /* + * TODO: syntaxtree for instanceof vorbereiten + * case InstanceofexpressionContext instanceof: + */ + case EqualityexpressionContext equal: + return convert(equal); + case AssignexpressionContext assignment: + return convert(assignment); + case LambdaexpressionContext lambdaexpr: + return convert(lambdaexpr); + /* + * case ArrayaccessexpressionContext arrayaccess: + * case ShiftexpressionContext shiftexpr: + * case BitwiseandexpressionContext bitwiseand: + * case BitwisexorexpressionContext bitwisexor: + * case BitwiseorexpressionContext bitwiseor: + * case AndexpressionContext andexpr: + * case OrexpressionContext orexpr: + * case ConditionalassignexpressionContext condassign: + */ + default: + throw new NotImplementedException(); + } + } + + private Expression convert(DottedexpressionContext expr, Token offset) { + if (!Objects.isNull(expr.methodCall())) { + return convert(expr.methodCall(), expr.expression(), offset); + } else if (!Objects.isNull(expr.THIS())) { + return new This(offset); + } else if (!Objects.isNull(expr.NEW()) && Objects.isNull(expr.nonWildcardTypeArguments())) { + return convert(expr.innerCreator()); + } else { + // Für alle anderen Optionen, wie Feldzugriff, Aufrufe von super oder explizite + // generische Invokationen + throw new NotImplementedException(); + } + } + + private Expression convert(Java17Parser.PrimaryNoNewArray_lfno_primaryContext expression) { + if (expression.literal() != null) { + return convert(expression.literal()); + } else if (expression.parExpression() != null) { + return convert(expression.parExpression().expression()); + } else if (expression.methodInvocation_lfno_primary() != null) { + return convert(expression.methodInvocation_lfno_primary()); + } else if (expression.classInstanceCreationExpression_lfno_primary() != null) { + return convert(expression.classInstanceCreationExpression_lfno_primary()); + } else if (expression.getText().equals("this")) { + return new This(expression.getStart()); + } else if (expression.fieldAccess_lfno_primary() != null) { + return convert(expression.fieldAccess_lfno_primary()); + } else if (expression.methodReference_lfno_primary() != null) { + throw new NotImplementedException(); + } else if (expression.typeName() != null) { + throw new NotImplementedException(); + } else if (expression.unannPrimitiveType() != null) { + throw new NotImplementedException(); + } else if (expression.arrayAccess_lfno_primary() != null) { + throw new NotImplementedException(); + } else if (expression.fieldAccess_lfno_primary() != null) { + throw new NotImplementedException(); + } else { + throw new NotImplementedException(); + } + } + + private MethodCall convert(MethodCallContext expr, Token offset) { String name = "this"; Expression receiver = new This(offset); if (expr.identifier() != null) { @@ -275,7 +537,7 @@ public class StatementGenerator { return ret; } - private MethodCall convertMethodCall(MethodCallContext expr, ExpressionContext receiver, Token offset) { + private MethodCall convert(MethodCallContext expr, ExpressionContext receiver, Token offset) { String name = "this"; if (expr.identifier() != null) { name = expr.identifier().getText(); @@ -366,29 +628,39 @@ public class StatementGenerator { return new FieldVar(receiver, parts[parts.length - 1], TypePlaceholder.fresh(offset), offset); } - private Statement convert(Java17Parser.NewinstanceexpressionContext newExpression) { - Java17Parser.TypeArgumentsContext genericArgs = null; - if (newExpression.expressionName() != null) - throw new NotImplementedException(); - if (newExpression.typeArgumentsOrDiamond() != null) { - if (newExpression.typeArgumentsOrDiamond().typeArguments() != null) { - genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments(); - } + private Expression convert(Java17Parser.ConditionalassignexpressionContext expression) { + throw new NotImplementedException(); + } + + private Expression convert(Java17Parser.OrexpressionContext expression) { + throw new NotImplementedException(); + } + + private Expression convert(Java17Parser.AndexpressionContext expression) { + throw new NotImplementedException(); + } + + private Statement convert(AssignexpressionContext expr) { + switch (expr.bop.getText()) { + case "=": + ExpressionContext leftside = expr.expression(0); + AssignLeftSide leftHandSide = convert(leftside.getText(), leftside.getStart()); + Statement ret = new Assign(leftHandSide, convert(expr.expression(1)), expr.getStart()); + return ret; + case "+=": + case "-=": + case "*=": + case "/=": + case "&=": + case "|=": + case "^=": + case ">>=": + case ">>>=": + case "<<=": + case "%=": + default: + 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); - - ArgumentList args = convert(newExpression.argumentList()); - ArrayList argTypes = args.getArguments().stream() - .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 AssignLeftSide convert(String leftHandSide, Token start) { @@ -401,270 +673,31 @@ public class StatementGenerator { throw new NotImplementedException(); } - 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 Expression convert(Java17Parser.BitwiseorexpressionContext expression) { throw new NotImplementedException(); } - private Statement convert(Java17Parser.AssertstmtContext stmt) { - // TODO + private Expression convert(Java17Parser.BitwisexorexpressionContext expression) { throw new NotImplementedException(); } - private Statement convert(Java17Parser.SwitchstmtContext stmt) { - // TODO + private Expression convert(Java17Parser.BitwiseandexpressionContext expression) { throw new NotImplementedException(); } - private Expression convert(Java17Parser.SwitchExpressionContext switchexpression) { - // TODO - throw new NotImplementedException(); + private Expression convert(Java17Parser.EqualityexpressionContext expression) { + String operator = expression.bop.getText(); + Expression leftSide = convert(expression.expression(0)); + Expression rightSide = convert(expression.expression(1)); + return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), + leftSide, rightSide, expression.getStart()); } - private Statement convert(Java17Parser.YieldstmtContext yieldstmt) { - // TODO - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.SwitchBlockContext stmt) { - // TODO - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.SwitchBlockStatementGroupContext stmt) { - // TODO - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.WhileloopContext stmt) { - Expression expr = convert(stmt.parExpression().expression()); - Statement block = convert(stmt.statement()); - return new WhileStmt(expr, block, stmt.getStart()); - } - - private Statement convert(Java17Parser.DowhileloopContext stmt) { - Statement block = convert(stmt.statement()); - Expression expr = convert(stmt.parExpression().expression()); - return new DoStmt(expr, block, stmt.getStart()); - } - - private Statement convert(Java17Parser.ForloopContext stmt) { - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.ExpressionListContext exprlist) { - // TODO - throw new NotImplementedException(); - } - - private ArgumentList convertArguments(Java17Parser.ExpressionListContext arglist) { - if (arglist == null) - return new ArgumentList(new ArrayList<>(), new NullToken()); - Token offset = new NullToken(); - List arguments = new ArrayList(); - for (ExpressionContext arg : arglist.expression()) { - arguments.add(convert(arg)); - } - if (arguments.size() > 0) - offset = arguments.get(0).getOffset(); - return new ArgumentList(arguments, offset); - } - - private List convert(Java17Parser.LocalVariableDeclarationContext declaration) { - List ret = new ArrayList<>(); - if (declaration.variableModifier() != null && declaration.variableModifier().size() > 0) { - // TODO - throw new NotImplementedException(); - } - RefTypeOrTPHOrWildcardOrGeneric type; - if (declaration.unannTypeOrAuto().unannType() == null) { - type = TypePlaceholder.fresh(declaration.getStart()); - } 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) { - List ret = new ArrayList<>(); - 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) { - Expression initValue; - if (varDecl.variableInitializer().arrayInitializer() != null) { - throw new NotImplementedException(); - } else { - initValue = convert(varDecl.variableInitializer().expression()); - } - 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) { - 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 AssignToField( - new FieldVar(new This(varDecl.getStart()), name.getText(), - type, varDecl.getStart())), - initValue, name.getSymbol())); - } - - private Statement convert(Java17Parser.BreakstmtContext stmt) { - // TODO - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.ContinueStatementContext stmt) { - // TODO - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.ReturnstmtContext 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 - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.SynchronizedStatementContext stmt) { - // TODO - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.TrycatchblockContext stmt) { - // TODO - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.CatchesContext stmt) { - // TODO - throw new NotImplementedException(); - } - - private Statement convert(Java17Parser.CatchClauseContext stmt) { - // TODO - throw new NotImplementedException(); - } - - /* - *************** + Expression Conversions: - */ - - private Expression convert(Java17Parser.ExpressionContext expression) { - if (expression.lambdaExpression() != null) { - return convert(expression.lambdaExpression()); - } else { - return convert(expression.assignmentExpression()); - } - } - - private Expression convert(Java17Parser.AssignmentExpressionContext expression) { - if (expression.conditionalExpression() != null) { - return convert(expression.conditionalExpression()); - } else { - return convert(expression.assignment()); - } - } - - private Expression convert(Java17Parser.ConditionalExpressionContext expression) { - if (expression.conditionalOrExpression() != null) { - return convert(expression.conditionalOrExpression()); - } else { - throw new NotImplementedException(); - } - } - - private Expression convert(Java17Parser.ConditionalOrExpressionContext expression) { - if (expression.conditionalOrExpression() == null) { - return convert(expression.conditionalAndExpression()); - } else { - throw new NotImplementedException(); - } - } - - private Expression convert(Java17Parser.ConditionalAndExpressionContext expression) { - if (expression.conditionalAndExpression() == null) { - return convert(expression.inclusiveOrExpression()); - } else { - throw new NotImplementedException(); - } - } - - private Expression convert(Java17Parser.InclusiveOrExpressionContext expression) { - if (expression.inclusiveOrExpression() == null) { - return convert(expression.exclusiveOrExpression()); - } else { - throw new NotImplementedException(); - } - } - - private Expression convert(Java17Parser.ExclusiveOrExpressionContext expression) { - if (expression.exclusiveOrExpression() == null) { - return convert(expression.andExpression()); - } else { - throw new NotImplementedException(); - } - } - - private Expression convert(Java17Parser.AndExpressionContext expression) { - if (expression.andExpression() == null) { - return convert(expression.equalityExpression()); - } else { - throw new NotImplementedException(); - } - } - - private Expression convert(Java17Parser.EqualityExpressionContext expression) { - if (expression.equalityExpression() == null) { - return convert(expression.relationalExpression()); - } 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()); - } - } - - private Expression convert(Java17Parser.RelationalExpressionContext expression) { - if (expression.relationalExpression() == null) { - return convert(expression.shiftExpression()); - } 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(); + private Expression convert(Java17Parser.RelationalexpressionContext expression) { + String operator = expression.bop.getText(); + return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), + convert(expression.expression(0)), + convert(expression.expression(1)), expression.getStart()); } private BinaryExpr.Operator convertBinaryOperator(String operator) { @@ -699,106 +732,72 @@ public class StatementGenerator { // throw new NotImplementedException(); } - private Expression convert(Java17Parser.ShiftExpressionContext expression) { - if (expression.shiftExpression() == null) { - return convert(expression.additiveExpression()); + private Expression convert(Java17Parser.ShiftexpressionContext expression) { + throw new NotImplementedException(); + } + + private Expression convert(Java17Parser.MathaddsubexpressionContext expression) { + Expression leftSide = convert(expression.expression(0)); + Expression rightSide = convert(expression.expression(1)); + BinaryExpr.Operator op = convertBinaryOperator(expression.bop.getText()); + Token offset = expression.getStart(); + return new BinaryExpr(op, TypePlaceholder.fresh(expression.getStart()), leftSide, rightSide, offset); + } + + private Expression convert(Java17Parser.MathmuldivmodexpressionContext expression) { + Expression leftSide = convert(expression.expression(0)); + Expression rightSide = convert(expression.expression(1)); + BinaryExpr.Operator op = convertBinaryOperator(expression.bop.getText()); + Token offset = expression.getStart(); + return new BinaryExpr(op, TypePlaceholder.fresh(offset), leftSide, rightSide, offset); + } + + private Statement convert(PrefixexpressionContext prefixexpr) { + Expression expr = convert(prefixexpr.expression()); + Token op = prefixexpr.prefix; + Statement ret; + if (op.getText().equals("++")) { + ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, expr, TypePlaceholder.fresh(op), op); + ret.setStatement(); + return ret; + } else if (op.getText().equals("--")) { + ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, expr, TypePlaceholder.fresh(op), op); + ret.setStatement(); + return ret; } else { throw new NotImplementedException(); } } - private Expression convert(Java17Parser.AdditiveExpressionContext expression) { - - if (expression.additiveExpression() == null) { - return convert(expression.multiplicativeExpression()); - } else { - Expression leftSide = convert(expression.additiveExpression()); - Expression rightSide = convert(expression.multiplicativeExpression()); - BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); - Token offset = expression.getStart(); - return new BinaryExpr(op, TypePlaceholder.fresh(expression.getStart()), leftSide, rightSide, offset); + private Statement convert(Java17Parser.PostfixexpressionContext postfixexpr) { + Expression expr = convert(postfixexpr.expression()); + Token op = postfixexpr.postfix; + switch (op.getText()) { + case "++": + return new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, expr, TypePlaceholder.fresh(op), op); + case "--": + return new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(op), op); + default: + throw new NotImplementedException(); } } - private Expression convert(Java17Parser.MultiplicativeExpressionContext expression) { - 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); - } - } - - private Expression convert(Java17Parser.UnaryExpressionContext expression) { - if (expression.preIncrementExpression() != null) { - return convert(expression.preIncrementExpression()); - } else if (expression.preDecrementExpression() != null) { - return convert(expression.preDecrementExpression()); - } else if (expression.unaryExpressionNotPlusMinus() != null) { - return convert(expression.unaryExpressionNotPlusMinus()); - } 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("-")) { - 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. - throw new NotImplementedException(); - } - } - - private Expression convert(Java17Parser.UnaryExpressionNotPlusMinusContext expression) { - if (expression.postfixExpression() != null) { - return convert(expression.postfixExpression()); - } else if (expression.castExpression() != null) { - return convert(expression.castExpression()); + private Expression convert(Java17Parser.CastexpressionContext castexpr) { + ExpressionContext expr = castexpr.expression(); + Expression exprast = null; + if (expr instanceof LambdaexpressionContext) { + exprast = convert(expr); + } else if (expr instanceof PrefixexpressionContext pfe) { + if (!pfe.prefix.equals("~") && !pfe.prefix.equals("!")) { + throw new NotImplementedException(); + } else { + exprast = convert(expr); + } } else { throw new NotImplementedException(); } - } - - private Expression convert(Java17Parser.CastExpressionContext expressionContext) { - Expression expr = null; - if (expressionContext.unaryExpression() != null) { - throw new NotImplementedException(); - } else if (expressionContext.unaryExpressionNotPlusMinus() != null) { - expr = convert(expressionContext.unaryExpressionNotPlusMinus()); - } else if (expressionContext.lambdaExpression() != null) { - expr = convert(expressionContext.lambdaExpression()); - } - return new CastExpr(TypeGenerator.convert(expressionContext.referenceType(), reg, generics), expr, - expressionContext.getStart()); - } - - private Expression convert(Java17Parser.PostfixExpressionContext expression) { - Expression expr; - if (expression.primary() != null) { - expr = convert(expression.primary()); - } else { - expr = convert(expression.expressionName()); - } - 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.PostDecrementExpression_lf_postfixExpressionContext dec : expression - .postDecrementExpression_lf_postfixExpression()) { - expr = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(dec.getStart()), - dec.getStart()); - } - - return expr; + return new CastExpr(TypeGenerator.convert(castexpr.typeType(0), reg, generics), exprast, + castexpr.getStart()); } private Expression convert(Java17Parser.PrimaryContext primary) { @@ -838,65 +837,10 @@ public class StatementGenerator { } } - private Expression convert(Java17Parser.ArrayCreationExpressionContext expression) { + private Expression convert(Java17Parser.ArrayCreatorRestContext expression) { throw new NotImplementedException(); } - private Expression convert(Java17Parser.PrimaryNoNewArray_lfno_primaryContext expression) { - if (expression.literal() != null) { - return convert(expression.literal()); - } else if (expression.parExpression() != null) { - return convert(expression.parExpression().expression()); - } else if (expression.methodInvocation_lfno_primary() != null) { - return convert(expression.methodInvocation_lfno_primary()); - } else if (expression.classInstanceCreationExpression_lfno_primary() != null) { - return convert(expression.classInstanceCreationExpression_lfno_primary()); - } else if (expression.getText().equals("this")) { - return new This(expression.getStart()); - } else if (expression.fieldAccess_lfno_primary() != null) { - return convert(expression.fieldAccess_lfno_primary()); - } else if (expression.methodReference_lfno_primary() != null) { - throw new NotImplementedException(); - } else if (expression.typeName() != null) { - throw new NotImplementedException(); - } else if (expression.unannPrimitiveType() != null) { - throw new NotImplementedException(); - } else if (expression.arrayAccess_lfno_primary() != null) { - throw new NotImplementedException(); - } else if (expression.fieldAccess_lfno_primary() != null) { - throw new NotImplementedException(); - } else { - throw new NotImplementedException(); - } - } - - private Expression convert(Java17Parser.FieldAccess_lfno_primaryContext fieldAccess_lfno_primaryContext) { - throw new NotImplementedException(); - } - - 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) { - genericArgs = newExpression.typeArgumentsOrDiamond().typeArguments(); - } - } - if (newExpression.typeArguments() != null) - throw new NotImplementedException(); - - TerminalNode identifier = newExpression.Identifier(0); - 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)); - return new NewClass(newClass, args, null, argTypes, newExpression.getStart()); - } - private Expression convert(Java17Parser.LiteralContext literal) { if (literal.IntegerLiteral() != null) { Number value = Integer.parseInt(literal.IntegerLiteral().getText()); @@ -931,48 +875,28 @@ public class StatementGenerator { } } - private Expression convert(Java17Parser.MethodInvocation_lfno_primaryContext methodInvocationContext) { - String name; - if (methodInvocationContext.methodName() != null) { - name = methodInvocationContext.methodName().Identifier().getText(); - } 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) { - receiver = convert(methodInvocationContext.expressionName()); - } else if (methodInvocationContext.toString().startsWith("super")) { - receiver = new Super(methodInvocationContext.getStart()); - } else if (methodInvocationContext.methodName() != null) { - receiver = new This(methodInvocationContext.methodName().getStart()); - } 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()); - return ret; - } - private Expression convert(Java17Parser.LambdaExpressionContext expression) { Java17Parser.LambdaParametersContext lambdaParams = expression.lambdaParameters(); ParameterList params; - if (lambdaParams.Identifier() != null) { + if (lambdaParams.identifier().size() > 0) { List parameterList = new ArrayList<>(); - parameterList.add(new FormalParameter(lambdaParams.Identifier().getText(), - TypePlaceholder.fresh(lambdaParams.getStart()), lambdaParams.getStart())); + for (IdentifierContext identifier : lambdaParams.identifier()) { + Token offset = identifier.getStart(); + parameterList.add(new FormalParameter(identifier.getText(), + TypePlaceholder.fresh(offset), offset)); + } params = new ParameterList(parameterList, lambdaParams.getStart()); } else if (lambdaParams.formalParameterList() != null) { params = convert(lambdaParams.formalParameterList()); // }else if( lambdaParams.inferredFormalParameterList != null){ + } else if (!Objects.isNull(lambdaParams.lambdaLVTIList())) { + List parameterList = new ArrayList<>(); + for (LambdaLVTIParameterContext param : lambdaParams.lambdaLVTIList().lambdaLVTIParameter()) { + Token offset = param.getStart(); + parameterList.add(new FormalParameter(param.identifier().getText(), + TypePlaceholder.fresh(offset), offset)); + } + params = new ParameterList(parameterList, lambdaParams.getStart()); } else { params = new ParameterList(new ArrayList<>(), expression.getStart()); } diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index 7798c188..d8a4e60f 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -177,7 +177,7 @@ public class TypeGenerator { public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(String name, Token offset, JavaClassRegistry reg, GenericsRegistry generics) { - return convertTypeName(name, null, offset, reg, generics); + return convertTypeName(name, (Java17Parser.TypeArgumentsContext) null, offset, reg, generics); } public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName( @@ -207,6 +207,34 @@ public class TypeGenerator { } } + public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName( + String name, Java17Parser.NonWildcardTypeArgumentsContext typeArguments, Token offset, + JavaClassRegistry reg, + GenericsRegistry generics) { + if (!reg.contains(name)) { // Dann könnte es ein generischer Type oder ein FunN$$-Type sein + if (generics.contains(name)) { + return new GenericRefType(name, offset); + } else { + Pattern p = Pattern.compile("Fun(\\d+)[$][$]"); + Matcher m = p.matcher(name); + if (m.matches()) {// es ist FunN$$-Type + return new RefType(new JavaClassName(name), convert(typeArguments, reg, generics), offset); + } else { + throw new TypeinferenceException("Der Typ " + name + " ist nicht vorhanden", offset); + } + } + } + if (typeArguments == null) { + List params = new ArrayList<>(); + for (int i = 0; i < reg.getNumberOfGenerics(name); i++) { + params.add(TypePlaceholder.fresh(offset)); + } + return new RefType(reg.getName(name), params, offset); + } else { + return new RefType(reg.getName(name), convert(typeArguments, reg, generics), offset); + } + } + public static List convert(Java17Parser.TypeArgumentsContext typeArguments, JavaClassRegistry reg, GenericsRegistry generics) { List ret = new ArrayList<>(); @@ -220,4 +248,14 @@ public class TypeGenerator { } return ret; } + + public static List convert( + Java17Parser.NonWildcardTypeArgumentsContext typeArguments, + JavaClassRegistry reg, GenericsRegistry generics) { + List ret = new ArrayList<>(); + for (Java17Parser.TypeTypeContext arg : typeArguments.typeList().typeType()) { + ret.add(convert(arg, reg, generics)); + } + return ret; + } }