From 9d9dcfb9b66c8a358d3ea3ad7c7dc89df8a10c62 Mon Sep 17 00:00:00 2001 From: StefanZ3 Date: Fri, 31 May 2024 12:53:03 +0200 Subject: [PATCH] add newDecl to ASTGenerator --- src/main/java/astGenerator/ASTGenerator.java | 25 ++++++++++---------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/astGenerator/ASTGenerator.java b/src/main/java/astGenerator/ASTGenerator.java index 2d8a6d0..6c5cbc4 100644 --- a/src/main/java/astGenerator/ASTGenerator.java +++ b/src/main/java/astGenerator/ASTGenerator.java @@ -10,6 +10,7 @@ import abstractSyntaxTree.Parameter.ParameterList; import abstractSyntaxTree.Program; import abstractSyntaxTree.Statement.*; import abstractSyntaxTree.StatementExpression.AssignStatementExpression; +import abstractSyntaxTree.StatementExpression.NewStatementExpression; import gen.DecafBaseVisitor; import gen.DecafParser; @@ -184,15 +185,18 @@ public class ASTGenerator extends DecafBaseVisitor { return new AssignStatementExpression(ctx.Assign().getText(),(IExpression) left, (IExpression) right); } - + // @Override public Node visitMethodCall(DecafParser.MethodCallContext ctx) { return super.visitMethodCall(ctx); } + //add the expression list later to the constructor @Override public Node visitNewDecl(DecafParser.NewDeclContext ctx) { - return super.visitNewDecl(ctx); + String name = ctx.Identifier().getText(); + List expressions = generateExpressions(ctx.argumentList()); + return new NewStatementExpression(name); } @Override @@ -296,14 +300,11 @@ public class ASTGenerator extends DecafBaseVisitor { return super.visitInstVar(ctx); } -// @Override -// public Node visitArgumentList(DecafParser.ArgumentListContext ctx) { -// if (ctx.expression().size() == 1) { -// return visitExpression(ctx.expression(0)); -// } else if (ctx.expression().size() >= 2) { -// for(DecafParser.ExpressionContext expressionContext: ctx.expression()) { -// -// } -// } -// } + public List generateExpressions(DecafParser.ArgumentListContext ctx) { + List expressions = new ArrayList<>(); + for (DecafParser.ExpressionContext expressionContext : ctx.expression()) { + expressions.add((IExpression) visitExpression(expressionContext)); + } + return expressions; + } }