diff --git a/pom.xml b/pom.xml index 6ec4b17..6dc0747 100644 --- a/pom.xml +++ b/pom.xml @@ -32,22 +32,6 @@ - - org.antlr - antlr4-maven-plugin - 4.11.1 - - true - - - - antlr - - antlr4 - - - - maven-clean-plugin diff --git a/src/main/java/de/dhbw/horb/ASTGenerator.java b/src/main/java/de/dhbw/horb/ASTGenerator.java index ca13d9b..c94ce07 100644 --- a/src/main/java/de/dhbw/horb/ASTGenerator.java +++ b/src/main/java/de/dhbw/horb/ASTGenerator.java @@ -1,5 +1,6 @@ package de.dhbw.horb; + import de.dhbw.horb.antlr.DecafParser; import de.dhbw.horb.ast.*; diff --git a/src/main/java/de/dhbw/horb/ExpressionGenerator.java b/src/main/java/de/dhbw/horb/ExpressionGenerator.java index 4dabd95..a4f163d 100644 --- a/src/main/java/de/dhbw/horb/ExpressionGenerator.java +++ b/src/main/java/de/dhbw/horb/ExpressionGenerator.java @@ -6,6 +6,7 @@ import de.dhbw.horb.ast.*; import java.util.ArrayList; import java.util.List; +import java.util.Optional; public class ExpressionGenerator extends DecafBaseVisitor { @Override @@ -15,7 +16,7 @@ public class ExpressionGenerator extends DecafBaseVisitor { @Override public Expression visitFunCallExpression(DecafParser.FunCallExpressionContext ctx) { - return new FunctionCall(ctx.funcCall().id().getText(), ctx.funcCall().args().expr().stream().map(arg -> arg.accept(this)).toList()); + return new FunctionCall(ctx.funcCall().id().getText(), Optional.of(ctx.funcCall().args()).map(arg -> arg.expr().stream().map(this::visit).toList()).orElseGet(ArrayList::new)); } @Override @@ -35,25 +36,28 @@ public class ExpressionGenerator extends DecafBaseVisitor { return generateLocation(ctx.loc()); } - public static Expression generateConstant(DecafParser.LiteralContext ctx){ - if(ctx.number() != null) + public static Expression generateConstant(DecafParser.LiteralContext ctx) { + if (ctx.number() != null) return new IntConstant(Integer.valueOf(ctx.number().getText())); - if(ctx.boolean_() != null) + if (ctx.boolean_() != null) return new BoolConstant(Boolean.valueOf(ctx.boolean_().getText())); throw new RuntimeException(); } - public static Operator generateOperator(DecafParser.BinaryOpContext ctx){ - if(ctx.ADD() != null)return Operator.ADD; - if(ctx.SUB() != null)return Operator.SUB; - if(ctx.MUL() != null)return Operator.MUL; + + public static Operator generateOperator(DecafParser.BinaryOpContext ctx) { + if (ctx.ADD() != null) return Operator.ADD; + if (ctx.SUB() != null) return Operator.SUB; + if (ctx.MUL() != null) return Operator.MUL; throw new RuntimeException(); } - public static Binary generateBinary(DecafParser.BinaryOperationContext ctx){ + + public static Binary generateBinary(DecafParser.BinaryOperationContext ctx) { ExpressionGenerator eGen = new ExpressionGenerator(); return new Binary(eGen.visit(ctx.expr().get(0)) // left side , generateOperator(ctx.binaryOp()) //operator , eGen.visit(ctx.expr().get(1))); //right side } + public static Location generateLocation(DecafParser.LocContext loc) { return new Location(loc.id().getText()); } diff --git a/src/main/java/de/dhbw/horb/StatementGenerator.java b/src/main/java/de/dhbw/horb/StatementGenerator.java index 3679c62..d7a2a15 100644 --- a/src/main/java/de/dhbw/horb/StatementGenerator.java +++ b/src/main/java/de/dhbw/horb/StatementGenerator.java @@ -5,6 +5,7 @@ import de.dhbw.horb.antlr.DecafParser; import de.dhbw.horb.ast.*; import java.util.ArrayList; +import java.util.Collections; public class StatementGenerator extends DecafBaseVisitor { @Override @@ -14,7 +15,7 @@ public class StatementGenerator extends DecafBaseVisitor { @Override public Statement visitIf(DecafParser.IfContext ctx) { - Block elseBlock = new Block(new ArrayList<>(), new ArrayList<>()); + Block elseBlock = new Block(Collections.emptyList(), Collections.emptyList()); if (ctx.block().size() > 1) elseBlock = new BlockGenerator().visit(ctx.block(1)); return new IfElse(new ExpressionGenerator().visit(ctx.expr()), new BlockGenerator().visit(ctx.block(0)), elseBlock);