StatementGenerator fertiggestellt

This commit is contained in:
luca9913 2023-03-08 06:35:55 +01:00
parent d362858184
commit 09cc88062e
2 changed files with 180 additions and 163 deletions

View File

@ -328,13 +328,13 @@ qualifiedName
;
literal
: integerLiteral
| floatLiteral
| CHAR_LITERAL
| STRING_LITERAL
| BOOL_LITERAL
| NULL_LITERAL
| TEXT_BLOCK // Java17
: integerLiteral # intLiteral
| floatLiteral # fltLiteral
| CHAR_LITERAL # charLiteral
| STRING_LITERAL # stringLiteral
| BOOL_LITERAL # boolLiteral
| NULL_LITERAL # nullLiteral
| TEXT_BLOCK # textBlock // Java17
;
integerLiteral
@ -675,13 +675,13 @@ lambdaBody
;
primary
: '(' expression ')'
| THIS
| SUPER
| literal
| identifier
| refType '.' CLASS
| nonWildcardTypeArguments (explicitGenericInvocationSuffix | THIS arguments)
: '(' expression ')' # primaryExpression
| THIS # primaryThis
| SUPER # primarySuper
| literal # primaryLiteral
| identifier # primaryIdentifier
| refType '.' CLASS # primaryClassref
| nonWildcardTypeArguments (explicitGenericInvocationSuffix | THIS arguments) # primaryInvocation
;
// Java17

View File

@ -1,65 +1,110 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17Parser.AndexpressionContext;
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.BitwiseorexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.BitwisexorexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.BlockstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.BoolLiteralContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.BreakstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.CastexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.CharLiteralContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ConditionalassignexpressionContext;
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.DowhileloopContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.EqualityexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ExpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.FltLiteralContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ForloopContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.IdentifierContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.IntLiteralContext;
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.MethodcallexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NewinstanceexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NullLiteralContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.OrexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PostfixexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrefixexpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryClassrefContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryExpressionContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryIdentifierContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryInvocationContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryLiteralContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimarySuperContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryThisContext;
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.StringLiteralContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.TextBlockContext;
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.antlr.Java17Parser.WhileloopContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.YieldstmtContext;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr.Operation;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide;
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.statement.Literal;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.NewClass;
import de.dhbwstuttgart.syntaxtree.statement.Receiver;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
import de.dhbwstuttgart.syntaxtree.statement.Super;
import de.dhbwstuttgart.syntaxtree.statement.This;
import de.dhbwstuttgart.syntaxtree.statement.UnaryExpr;
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.Void;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.util.*;
import java.util.stream.Collectors;
public class StatementGenerator {
@ -134,17 +179,25 @@ public class StatementGenerator {
case StmtexpressionContext stmtexpression:
return convert(stmtexpression);
case AssertstmtContext assertstmt:
/*
* case ForloopContext forloop:
* case TrycatchblockContext trycatchblock:
* case TrycatchresourceContext trycatchresource:
* case SynchronizedstmtContext synchronizedstmt:
* case ThrowstmtContext throwstmt:
* case BreakstmtContext breakstmt:
* case ContinuestmtContext continuestmt:
* case SemistmtContext semistmt:
* case LabeledstmtContext labeledstmt:
*/
return convert(assertstmt);
case ForloopContext forloop:
return convert(forloop);
case TrycatchblockContext trycatchblock:
return convert(trycatchblock);
case TrycatchresourceContext trycatchresource:
return convert(trycatchresource);
case SynchronizedstmtContext synchronizedstmt:
return convert(synchronizedstmt);
case ThrowstmtContext throwstmt:
return convert(throwstmt);
case BreakstmtContext breakstmt:
return convert(breakstmt);
case ContinuestmtContext continuestmt:
return convert(continuestmt);
case SemistmtContext semistmt:
return convert(semistmt);
case LabeledstmtContext labeledstmt:
return convert(labeledstmt);
default:
throw new NotImplementedException();
}
@ -219,13 +272,21 @@ public class StatementGenerator {
RefType newclass = convert(creator.createdName());
ArgumentList args = convertArguments(creator.classCreatorRest().arguments().expressionList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> 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;
if (!Objects.isNull(creator.classCreatorRest())) {
ArgumentList args = convertArguments(creator.classCreatorRest().arguments().expressionList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> 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;
} else {
return convert(creator.arrayCreatorRest());
}
}
private Statement convert(Java17Parser.ArrayCreatorRestContext expression) {
throw new NotImplementedException();
}
private RefType convert(Java17Parser.CreatedNameContext createdname) {
@ -260,6 +321,7 @@ public class StatementGenerator {
Statement ret = new NewClass(newclass, args, null, argTypes, innercreator.getStart());
ret.setStatement();
return ret;
}
private Statement convert(Java17Parser.ConditionalstmtContext stmt) {
@ -312,11 +374,6 @@ public class StatementGenerator {
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());
@ -393,6 +450,11 @@ public class StatementGenerator {
throw new NotImplementedException();
}
private Statement convert(Java17Parser.SemistmtContext stmt) {
// TODO
throw new NotImplementedException();
}
private Statement convert(Java17Parser.ReturnstmtContext stmt) {
if (stmt.expression() != null) {
return new Return(convert(stmt.expression()), stmt.getStart());
@ -416,7 +478,7 @@ public class StatementGenerator {
throw new NotImplementedException();
}
private Statement convert(Java17Parser.CatchClauseContext stmt) {
private Statement convert(Java17Parser.TrycatchresourceContext stmt) {
// TODO
throw new NotImplementedException();
}
@ -450,23 +512,30 @@ public class StatementGenerator {
/*
* TODO: syntaxtree for instanceof vorbereiten
* case InstanceofexpressionContext instanceof:
* case SwitchexpressionContext switchexpression:
*/
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:
*/
return convert(lambdaexpr.lambdaExpression());
case ArrayaccessexpressionContext arrayaccess:
return convert(arrayaccess);
case ShiftexpressionContext shiftexpr:
return convert(shiftexpr);
case BitwiseandexpressionContext bitwiseand:
return convert(bitwiseand);
case BitwisexorexpressionContext bitwisexor:
return convert(bitwisexor);
case BitwiseorexpressionContext bitwiseor:
return convert(bitwiseor);
case AndexpressionContext andexpr:
return convert(andexpr);
case OrexpressionContext orexpr:
return convert(orexpr);
case ConditionalassignexpressionContext condassign:
return convert(condassign);
default:
throw new NotImplementedException();
}
@ -486,34 +555,6 @@ public class StatementGenerator {
}
}
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);
@ -628,6 +669,10 @@ public class StatementGenerator {
return new FieldVar(receiver, parts[parts.length - 1], TypePlaceholder.fresh(offset), offset);
}
private Expression convert(Java17Parser.ArrayaccessexpressionContext arrayaccess) {
throw new NotImplementedException();
}
private Expression convert(Java17Parser.ConditionalassignexpressionContext expression) {
throw new NotImplementedException();
}
@ -801,77 +846,49 @@ public class StatementGenerator {
}
private Expression convert(Java17Parser.PrimaryContext primary) {
Expression expr;
if (primary.primaryNoNewArray_lfno_primary() != null) {
expr = convert(primary.primaryNoNewArray_lfno_primary());
} else {
expr = convert(primary.arrayCreationExpression());
switch (primary) {
case PrimaryExpressionContext primexpression:
return convert(primexpression.expression());
case PrimaryThisContext primthis:
return new This(primthis.getStart());
case PrimarySuperContext primsuper:
throw new NotImplementedException();
case PrimaryLiteralContext primliteral:
return convert(primliteral.literal());
case PrimaryIdentifierContext primidentifier:
return new LocalVar(primidentifier.getText(), TypePlaceholder.fresh(primidentifier.getStart()),
primidentifier.getStart());
case PrimaryClassrefContext primclassref:
throw new NotImplementedException();
case PrimaryInvocationContext priminvocation:
default:
throw new NotImplementedException();
}
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);
}
}
return expr;
}
private Expression convert(Expression expr, Java17Parser.PrimaryNoNewArray_lf_primaryContext e) {
if (e.classInstanceCreationExpression_lf_primary() != null) {
throw new NotImplementedException();
} else if (e.fieldAccess_lf_primary() != null) {
throw new NotImplementedException();
} else if (e.arrayAccess_lf_primary() != null) {
throw new NotImplementedException();
} else if (e.methodReference_lf_primary() != null) {
throw new NotImplementedException();
} else {
Java17Parser.MethodInvocation_lf_primaryContext ctxt = e.methodInvocation_lf_primary();
String methodName = ctxt.Identifier().toString();
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> 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());
}
}
private Expression convert(Java17Parser.ArrayCreatorRestContext expression) {
throw new NotImplementedException();
}
private Expression convert(Java17Parser.LiteralContext literal) {
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) {
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());
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());
return new Literal(type,
// 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());
return new Literal(type,
literal.StringLiteral().getText().substring(1, literal.StringLiteral().getText().length() - 1),
literal.getStart());
} else if (literal.NullLiteral() != null) {
return new Literal(TypePlaceholder.fresh(literal.getStart()), null,
literal.getStart());
} else {
throw new NotImplementedException();
switch (literal) {
case IntLiteralContext intliteral:
Number value = Integer.parseInt(intliteral.getText());
return new Literal(TypePlaceholder.fresh(literal.getStart()), value, intliteral.getStart());
case FltLiteralContext floatliteral:
value = Double.parseDouble(floatliteral.getText());
return new Literal(TypePlaceholder.fresh(literal.getStart()), value, floatliteral.getStart());
case CharLiteralContext charliteral:
RefType type = new RefType(reg.getName("java.lang.Character"), charliteral.getStart());
return new Literal(type, charliteral.getText().charAt(1), charliteral.getStart());
case StringLiteralContext stringliteral:
type = new RefType(reg.getName("java.lang.String"), stringliteral.getStart());
return new Literal(type, stringliteral.getText().substring(1, stringliteral.getText().length() - 1),
stringliteral.getStart());
case BoolLiteralContext boolliteral:
type = new RefType(reg.getName("java.lang.Boolean"), boolliteral.getStart());
return new Literal(type, Boolean.parseBoolean(boolliteral.getText()), boolliteral.getStart());
case NullLiteralContext nullliteral:
return new Literal(TypePlaceholder.fresh(nullliteral.getStart()), null, nullliteral.getStart());
case TextBlockContext textblock:
default:
throw new NotImplementedException();
}
}