format ASTGenerator

This commit is contained in:
StefanZ3 2024-07-04 09:17:34 +02:00
parent 82384886c6
commit 403b31c550

View File

@ -32,27 +32,27 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
List<FieldDecl> fieldDecls = new ArrayList<>(); List<FieldDecl> fieldDecls = new ArrayList<>();
List<MethodDecl> methodDecls = new ArrayList<>(); List<MethodDecl> methodDecls = new ArrayList<>();
boolean hasMain; boolean hasMain;
if(ctx.MainMethodDecl() != null) { if (ctx.MainMethodDecl() != null) {
hasMain = true; hasMain = true;
MethodDecl mainMethod = new MethodDecl(name, "void", "main", new ParameterList(new ArrayList<>()),(BlockStatement) visitBlock(ctx.block())); MethodDecl mainMethod = new MethodDecl(name, "void", "main", new ParameterList(new ArrayList<>()), (BlockStatement) visitBlock(ctx.block()));
methodDecls.add(mainMethod); methodDecls.add(mainMethod);
} else { } else {
hasMain = false; hasMain = false;
} }
for (DecafParser.LocalVarDeclContext fieldDecl: ctx.localVarDecl()) { for (DecafParser.LocalVarDeclContext fieldDecl : ctx.localVarDecl()) {
fieldDecls.add((FieldDecl) generateFieldDecl(fieldDecl)); fieldDecls.add((FieldDecl) generateFieldDecl(fieldDecl));
} }
for (DecafParser.ConstuctorDeclContext constDecl: ctx.constuctorDecl()) { for (DecafParser.ConstuctorDeclContext constDecl : ctx.constuctorDecl()) {
MethodDecl constructor = ((MethodDecl) visit(constDecl)); MethodDecl constructor = ((MethodDecl) visit(constDecl));
constructor.classThatContainsMethod = name; constructor.classThatContainsMethod = name;
methodDecls.add(constructor); methodDecls.add(constructor);
} }
for (DecafParser.MethodDeclContext methodDecl: ctx.methodDecl()) { for (DecafParser.MethodDeclContext methodDecl : ctx.methodDecl()) {
MethodDecl method = (MethodDecl) visit(methodDecl); MethodDecl method = (MethodDecl) visit(methodDecl);
method.classThatContainsMethod = name; method.classThatContainsMethod = name;
methodDecls.add(method); methodDecls.add(method);
} }
return new RefType(name,fieldDecls, methodDecls, hasMain); return new RefType(name, fieldDecls, methodDecls, hasMain);
} }
public Node generateFieldDecl(DecafParser.LocalVarDeclContext ctx) { public Node generateFieldDecl(DecafParser.LocalVarDeclContext ctx) {
@ -68,10 +68,10 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
public Node visitLocalVarDecl(DecafParser.LocalVarDeclContext ctx) { public Node visitLocalVarDecl(DecafParser.LocalVarDeclContext ctx) {
if (ctx.expression() != null) { if (ctx.expression() != null) {
IExpression expression = (IExpression) visit(ctx.expression()); IExpression expression = (IExpression) visit(ctx.expression());
return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText(), expression); return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText(), expression);
} else { } else {
return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText(), null); return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText(), null);
} }
} }
@Override @Override
@ -99,7 +99,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
} else { } else {
type = ctx.type().getText(); type = ctx.type().getText();
} }
return new MethodDecl("", type , name, parameterList, block); return new MethodDecl("", type, name, parameterList, block);
} }
@Override @Override
@ -110,7 +110,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
@Override @Override
public Node visitParameterList(DecafParser.ParameterListContext ctx) { public Node visitParameterList(DecafParser.ParameterListContext ctx) {
List<Parameter> parameters = new ArrayList<>(); List<Parameter> parameters = new ArrayList<>();
for (DecafParser.ParameterContext parameter: ctx.parameter()) { for (DecafParser.ParameterContext parameter : ctx.parameter()) {
parameters.add((Parameter) visit(parameter)); parameters.add((Parameter) visit(parameter));
} }
return new ParameterList(parameters); return new ParameterList(parameters);
@ -151,7 +151,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
@Override @Override
public Node visitBlock(DecafParser.BlockContext ctx) { public Node visitBlock(DecafParser.BlockContext ctx) {
List<IStatement> stmts = new ArrayList<>(); List<IStatement> stmts = new ArrayList<>();
for (DecafParser.StatementContext stmt: ctx.statement()) { for (DecafParser.StatementContext stmt : ctx.statement()) {
Node statement = visitStatement(stmt); Node statement = visitStatement(stmt);
stmts.add((IStatement) statement); stmts.add((IStatement) statement);
} }
@ -199,7 +199,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
} else if (ctx.newDecl() != null) { } else if (ctx.newDecl() != null) {
return visitNewDecl(ctx.newDecl()); return visitNewDecl(ctx.newDecl());
} }
return null; return null;
} }
@Override @Override
@ -211,7 +211,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
public Node visitAssign(DecafParser.AssignContext ctx) { public Node visitAssign(DecafParser.AssignContext ctx) {
Node right = visitExpression(ctx.expression()); Node right = visitExpression(ctx.expression());
Node left = visitAssignableExpr(ctx.assignableExpr()); Node left = visitAssignableExpr(ctx.assignableExpr());
return new AssignStatementExpression(ctx.Assign().getText(),(IExpression) left, (IExpression) right); return new AssignStatementExpression(ctx.Assign().getText(), (IExpression) left, (IExpression) right);
} }
@Override @Override
@ -219,7 +219,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
String methodName = ctx.Identifier().getText(); String methodName = ctx.Identifier().getText();
List<IExpression> arguments = generateExpressions(ctx.argumentList()); List<IExpression> arguments = generateExpressions(ctx.argumentList());
List<ReceivingMethod> receivingMethods = new ArrayList<>(); List<ReceivingMethod> receivingMethods = new ArrayList<>();
for(DecafParser.ReceivingMethodContext receivingMethod: ctx.receivingMethod()) { for (DecafParser.ReceivingMethodContext receivingMethod : ctx.receivingMethod()) {
receivingMethods.add((ReceivingMethod) visit(receivingMethod)); receivingMethods.add((ReceivingMethod) visit(receivingMethod));
} }
if (ctx.receiver() != null) { if (ctx.receiver() != null) {
@ -320,14 +320,14 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
if (ctx.IntValue() != null) { if (ctx.IntValue() != null) {
int value = Integer.parseInt(ctx.IntValue().getText()); int value = Integer.parseInt(ctx.IntValue().getText());
return new IntConstantExpression(value); return new IntConstantExpression(value);
} else if(ctx.Identifier() != null) { } else if (ctx.Identifier() != null) {
String identifier = ctx.Identifier().getText(); String identifier = ctx.Identifier().getText();
return new LocalVarIdentifier(identifier); return new LocalVarIdentifier(identifier);
} else if(ctx.instVar() != null) { } else if (ctx.instVar() != null) {
return visitInstVar(ctx.instVar()); return visitInstVar(ctx.instVar());
} else if(ctx.methodCall() != null) { } else if (ctx.methodCall() != null) {
return visitMethodCall(ctx.methodCall()); return visitMethodCall(ctx.methodCall());
} else if(ctx.calcExpr() != null) { } else if (ctx.calcExpr() != null) {
return visitCalcExpr(ctx.calcExpr()); return visitCalcExpr(ctx.calcExpr());
} }
return null; return null;
@ -336,7 +336,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
@Override @Override
public Node visitNonCalcExpr(DecafParser.NonCalcExprContext ctx) { public Node visitNonCalcExpr(DecafParser.NonCalcExprContext ctx) {
String operator; String operator;
if(ctx.nonCalcOperator().LogicalOpertor() != null) { if (ctx.nonCalcOperator().LogicalOpertor() != null) {
operator = ctx.nonCalcOperator().LogicalOpertor().getText(); operator = ctx.nonCalcOperator().LogicalOpertor().getText();
} else { } else {
operator = ctx.nonCalcOperator().ComparisonOperator().getText(); operator = ctx.nonCalcOperator().ComparisonOperator().getText();
@ -373,10 +373,10 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
List<SubReceiver> receivers = new ArrayList<>(); List<SubReceiver> receivers = new ArrayList<>();
List<ReceivingMethod> receivingMethods = new ArrayList<>(); List<ReceivingMethod> receivingMethods = new ArrayList<>();
String fieldName = ctx.Identifier().getText(); String fieldName = ctx.Identifier().getText();
for(DecafParser.SubReceiverContext subReceiver : ctx.subReceiver()) { for (DecafParser.SubReceiverContext subReceiver : ctx.subReceiver()) {
receivers.add((SubReceiver) visit(subReceiver)); receivers.add((SubReceiver) visit(subReceiver));
} }
for(DecafParser.ReceivingMethodContext receivingMethod: ctx.receivingMethod()) { for (DecafParser.ReceivingMethodContext receivingMethod : ctx.receivingMethod()) {
receivingMethods.add((ReceivingMethod) visit(receivingMethod)); receivingMethods.add((ReceivingMethod) visit(receivingMethod));
} }
return new InstVarExpression(receivers, receivingMethods, fieldName); return new InstVarExpression(receivers, receivingMethods, fieldName);