From 63246b815a0c31fc5ee5bd43a6ac901a0ea2e97c Mon Sep 17 00:00:00 2001 From: Boolean-True Date: Wed, 1 May 2024 10:49:06 +0200 Subject: [PATCH] Add missing visit functions --- src/main/java/de/maishai/ASTGenerator.java | 7 ++- .../java/de/maishai/ExpressionGenerator.java | 63 ++++++++++++++----- .../java/de/maishai/StatementGenerator.java | 56 ++++++++++++++--- .../java/de/maishai/VariableGenerator.java | 17 ++--- .../java/de/maishai/ast/records/DoWhile.java | 8 +++ .../de/maishai/ast/records/MainMethod.java | 2 +- .../de/maishai/ast/records/MethodCall.java | 2 +- .../ast/records/MethodCallExpression.java | 11 ++++ .../java/de/maishai/ast/records/Variable.java | 3 +- 9 files changed, 132 insertions(+), 37 deletions(-) create mode 100644 src/main/java/de/maishai/ast/records/DoWhile.java create mode 100644 src/main/java/de/maishai/ast/records/MethodCallExpression.java diff --git a/src/main/java/de/maishai/ASTGenerator.java b/src/main/java/de/maishai/ASTGenerator.java index 5691d63..c357801 100644 --- a/src/main/java/de/maishai/ASTGenerator.java +++ b/src/main/java/de/maishai/ASTGenerator.java @@ -33,7 +33,7 @@ public class ASTGenerator { } public static Variable generateVariable(DecafParser.VarContext ctx) { - return new Variable(ctx.id().getText(), getType(ctx.type())); + return new VariableGenerator().visitVar(ctx); } public static Parameter generateParameter(DecafParser.ParamContext ctx) { @@ -50,6 +50,11 @@ public class ASTGenerator { return new Method(getReturnType(ctx.returntype()), methId, params, block); } + public static MainMethod generateMainMethod(DecafParser.MainmethContext ctx) { + Block block = new BlockGenerator().visit(ctx.block()); + return new MainMethod(block); + } + public static Type getType(DecafParser.TypeContext ctx){ if(ctx.INT() != null) return Type.INT; diff --git a/src/main/java/de/maishai/ExpressionGenerator.java b/src/main/java/de/maishai/ExpressionGenerator.java index 812416a..4ef2105 100644 --- a/src/main/java/de/maishai/ExpressionGenerator.java +++ b/src/main/java/de/maishai/ExpressionGenerator.java @@ -4,10 +4,8 @@ import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafParser; import de.maishai.ast.Expression; import de.maishai.ast.Operator; -import de.maishai.ast.records.Binary; -import de.maishai.ast.records.BoolConstant; -import de.maishai.ast.records.IntConstant; -import de.maishai.ast.records.MethodCall; +import de.maishai.ast.Type; +import de.maishai.ast.records.*; import java.util.ArrayList; import java.util.List; @@ -18,17 +16,6 @@ public class ExpressionGenerator extends DecafBaseVisitor { return generateBinary(ctx); } - @Override - public Expression visitMethodCallExpression(DecafParser.MethodCallExpressionContext ctx) { - String functionName = ctx.methCall().id().getText(); - List args = new ArrayList<>(); - for(var expr : ctx.methCall().args().expr()){ - Expression astExpr = expr.accept(this); - args.add(astExpr); - } - return new MethodCall(functionName, args); - } - @Override public Expression visitConstant(DecafParser.ConstantContext ctx) { return generateConstant(ctx.literal()); @@ -41,6 +28,22 @@ public class ExpressionGenerator extends DecafBaseVisitor { return this.visit(ctx.expr()); } + @Override + public Expression visitMethodCallExpression(DecafParser.MethodCallExpressionContext ctx) { + Id id = new Id(ctx.methCall().id().getText()); + List args = new ArrayList<>(); + for(var expr : ctx.methCall().args().expr()){ + Expression astExpr = expr.accept(this); + args.add(astExpr); + } + return new MethodCallExpression(id, args); + } + + @Override + public Id visitIdentifier(DecafParser.IdentifierContext ctx){ + return new Id(ctx.getText()); + } + public static Expression generateConstant(DecafParser.LiteralContext ctx){ if(ctx.NUMBER() != null) return new IntConstant(Integer.valueOf(ctx.NUMBER().getText())); @@ -62,4 +65,34 @@ public class ExpressionGenerator extends DecafBaseVisitor { if(ctx.MUL() != null)return Operator.MUL; throw new RuntimeException(); } + + //StatementExpression + @Override + public Expression visitAssign(DecafParser.AssignContext ctx) { + Id id = new Id(ctx.id().getText()); + Expression expr = this.visit(ctx.expr()); + return new Assignment(id, expr); + } + + @Override + public Expression visitMethodCall(DecafParser.MethodCallContext ctx) { + Id id = new Id(ctx.methCall().id().getText()); + List args = new ArrayList<>(); + for(var expr : ctx.methCall().args().expr()){ + Expression astExpr = expr.accept(this); + args.add(astExpr); + } + return new MethodCall(id, args); + } + + @Override + public Expression visitNew(DecafParser.NewContext ctx) { + Type type = ASTGenerator.getType(ctx.type()); + List args = new ArrayList<>(); + for(var expr : ctx.args().expr()){ + Expression astExpr = expr.accept(this); + args.add(astExpr); + } + return new New(type, args); + } } diff --git a/src/main/java/de/maishai/StatementGenerator.java b/src/main/java/de/maishai/StatementGenerator.java index 9a361bf..07b66f6 100644 --- a/src/main/java/de/maishai/StatementGenerator.java +++ b/src/main/java/de/maishai/StatementGenerator.java @@ -4,16 +4,13 @@ import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafParser; import de.maishai.ast.Expression; import de.maishai.ast.Statement; +import de.maishai.ast.Type; import de.maishai.ast.records.*; -public class StatementGenerator extends DecafBaseVisitor { - @Override - public Statement visitAssign(DecafParser.AssignContext ctx) { - Id id = new Id(ctx.id().getText()); - Expression expr = new ExpressionGenerator().visit(ctx.expr()); - return new Assignment(id, expr); - } +import java.util.ArrayList; +import java.util.List; +public class StatementGenerator extends DecafBaseVisitor { @Override public Statement visitIf(DecafParser.IfContext ctx) { Expression expr = new ExpressionGenerator().visit(ctx.expr()); @@ -25,6 +22,15 @@ public class StatementGenerator extends DecafBaseVisitor { return new IfElse(expr, ifBlock, null); } + @Override + public For visitFor(DecafParser.ForContext ctx) { + Expression init = new ExpressionGenerator().visit(ctx.expr(0)); + Expression expr = new ExpressionGenerator().visit(ctx.expr(1)); + Expression update = new ExpressionGenerator().visit(ctx.expr(2)); + Block block = new BlockGenerator().visit(ctx.block()); + return new For(init, expr, update, block); + } + @Override public Statement visitWhile(DecafParser.WhileContext ctx) { Expression expr = new ExpressionGenerator().visit(ctx.expr()); @@ -32,6 +38,13 @@ public class StatementGenerator extends DecafBaseVisitor { return new While(expr, block); } + @Override + public DoWhile visitDoWhile(DecafParser.DoWhileContext ctx) { + Block block = new BlockGenerator().visit(ctx.block()); + Expression expr = new ExpressionGenerator().visit(ctx.expr()); + return new DoWhile(block, expr); + } + @Override public Statement visitReturn(DecafParser.ReturnContext ctx) { return new Return(new ExpressionGenerator().visit(ctx.expr())); @@ -52,4 +65,33 @@ public class StatementGenerator extends DecafBaseVisitor { return new Continue(); } + //StatementExpression + @Override + public Statement visitAssign(DecafParser.AssignContext ctx) { + Id id = new Id(ctx.id().getText()); + Expression expr = new ExpressionGenerator().visit(ctx.expr()); + return new Assignment(id, expr); + } + + @Override + public Statement visitMethodCall(DecafParser.MethodCallContext ctx) { + Id id = new Id(ctx.methCall().id().getText()); + List args = new ArrayList<>(); + for(var expr : ctx.methCall().args().expr()){ + Expression astExpr = expr.accept(new ExpressionGenerator()); + args.add(astExpr); + } + return new MethodCall(id, args); + } + + @Override + public Statement visitNew(DecafParser.NewContext ctx) { + Type type = ASTGenerator.getType(ctx.type()); + List args = new ArrayList<>(); + for(var expr : ctx.args().expr()){ + Expression astExpr = expr.accept(new ExpressionGenerator()); + args.add(astExpr); + } + return new New(type, args); + } } diff --git a/src/main/java/de/maishai/VariableGenerator.java b/src/main/java/de/maishai/VariableGenerator.java index 894e7bf..9cebb91 100644 --- a/src/main/java/de/maishai/VariableGenerator.java +++ b/src/main/java/de/maishai/VariableGenerator.java @@ -4,22 +4,17 @@ package de.maishai; import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafParser; import de.maishai.ast.Type; +import de.maishai.ast.records.Id; import de.maishai.ast.records.Variable; public class VariableGenerator extends DecafBaseVisitor { @Override public Variable visitVar(DecafParser.VarContext ctx) { - return new Variable(ctx.id().getText(), getType(ctx.type())); - } - - public static Type getType(DecafParser.TypeContext ctx){ - if(ctx.INT() != null) - return Type.INT; - if(ctx.BOOL() != null) - return Type.BOOL; - if(ctx.CHAR() != null) - return Type.CHAR; - throw new RuntimeException(); + Id id = new Id(ctx.id().getText()); + Type type = ASTGenerator.getType(ctx.type()); + if(ctx.expr() != null) + return new Variable(id, type, new ExpressionGenerator().visit(ctx.expr())); + return new Variable(id, type, null); } } diff --git a/src/main/java/de/maishai/ast/records/DoWhile.java b/src/main/java/de/maishai/ast/records/DoWhile.java new file mode 100644 index 0000000..73dbf26 --- /dev/null +++ b/src/main/java/de/maishai/ast/records/DoWhile.java @@ -0,0 +1,8 @@ +package de.maishai.ast.records; + + +import de.maishai.ast.Expression; +import de.maishai.ast.Statement; + +public record DoWhile(Block block, Expression cond) implements Statement { +} diff --git a/src/main/java/de/maishai/ast/records/MainMethod.java b/src/main/java/de/maishai/ast/records/MainMethod.java index 0da1915..3873d02 100644 --- a/src/main/java/de/maishai/ast/records/MainMethod.java +++ b/src/main/java/de/maishai/ast/records/MainMethod.java @@ -7,5 +7,5 @@ import de.maishai.ast.ReturnType; import java.util.List; -public record MainMethod(ReturnType type, Id id, List params, Block block) implements Node { +public record MainMethod(Block block) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/MethodCall.java b/src/main/java/de/maishai/ast/records/MethodCall.java index 2f84949..d5a5d31 100644 --- a/src/main/java/de/maishai/ast/records/MethodCall.java +++ b/src/main/java/de/maishai/ast/records/MethodCall.java @@ -7,5 +7,5 @@ import de.maishai.ast.Statement; import java.util.List; -public record MethodCall(String name, List args) implements Expression, Statement { +public record MethodCall(Id id, List args) implements Expression, Statement { } diff --git a/src/main/java/de/maishai/ast/records/MethodCallExpression.java b/src/main/java/de/maishai/ast/records/MethodCallExpression.java new file mode 100644 index 0000000..ba22482 --- /dev/null +++ b/src/main/java/de/maishai/ast/records/MethodCallExpression.java @@ -0,0 +1,11 @@ +package de.maishai.ast.records; + + + +import de.maishai.ast.Expression; +import de.maishai.ast.Statement; + +import java.util.List; + +public record MethodCallExpression(Id id, List args) implements Expression, Statement { +} diff --git a/src/main/java/de/maishai/ast/records/Variable.java b/src/main/java/de/maishai/ast/records/Variable.java index 1f2901c..adab4ea 100644 --- a/src/main/java/de/maishai/ast/records/Variable.java +++ b/src/main/java/de/maishai/ast/records/Variable.java @@ -1,8 +1,9 @@ package de.maishai.ast.records; +import de.maishai.ast.Expression; import de.maishai.ast.Node; import de.maishai.ast.Type; -public record Variable(String name, Type type) implements Node { +public record Variable(Id name, Type type, Expression expr) implements Node { }