forked from stan/AntlrAssignment3Template
refactor
This commit is contained in:
parent
d6b4b41d3c
commit
968189a100
16
pom.xml
16
pom.xml
@ -32,22 +32,6 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin> <!-- ANTLR parser generation -->
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-maven-plugin</artifactId>
|
||||
<version>4.11.1</version>
|
||||
<configuration>
|
||||
<visitor>true</visitor>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>antlr</id>
|
||||
<goals>
|
||||
<goal>antlr4</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.dhbw.horb;
|
||||
|
||||
|
||||
import de.dhbw.horb.antlr.DecafParser;
|
||||
import de.dhbw.horb.ast.*;
|
||||
|
||||
|
@ -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<Expression> {
|
||||
@Override
|
||||
@ -15,7 +16,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
||||
|
||||
@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<Expression> {
|
||||
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());
|
||||
}
|
||||
|
@ -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<Statement> {
|
||||
@Override
|
||||
@ -14,7 +15,7 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
||||
|
||||
@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);
|
||||
|
Loading…
Reference in New Issue
Block a user