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>
|
<build>
|
||||||
<plugins>
|
<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 -->
|
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-clean-plugin</artifactId>
|
<artifactId>maven-clean-plugin</artifactId>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package de.dhbw.horb;
|
package de.dhbw.horb;
|
||||||
|
|
||||||
|
|
||||||
import de.dhbw.horb.antlr.DecafParser;
|
import de.dhbw.horb.antlr.DecafParser;
|
||||||
import de.dhbw.horb.ast.*;
|
import de.dhbw.horb.ast.*;
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import de.dhbw.horb.ast.*;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
||||||
@Override
|
@Override
|
||||||
@ -15,7 +16,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Expression visitFunCallExpression(DecafParser.FunCallExpressionContext ctx) {
|
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
|
@Override
|
||||||
@ -35,25 +36,28 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
return generateLocation(ctx.loc());
|
return generateLocation(ctx.loc());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Expression generateConstant(DecafParser.LiteralContext ctx){
|
public static Expression generateConstant(DecafParser.LiteralContext ctx) {
|
||||||
if(ctx.number() != null)
|
if (ctx.number() != null)
|
||||||
return new IntConstant(Integer.valueOf(ctx.number().getText()));
|
return new IntConstant(Integer.valueOf(ctx.number().getText()));
|
||||||
if(ctx.boolean_() != null)
|
if (ctx.boolean_() != null)
|
||||||
return new BoolConstant(Boolean.valueOf(ctx.boolean_().getText()));
|
return new BoolConstant(Boolean.valueOf(ctx.boolean_().getText()));
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
public static Operator generateOperator(DecafParser.BinaryOpContext ctx){
|
|
||||||
if(ctx.ADD() != null)return Operator.ADD;
|
public static Operator generateOperator(DecafParser.BinaryOpContext ctx) {
|
||||||
if(ctx.SUB() != null)return Operator.SUB;
|
if (ctx.ADD() != null) return Operator.ADD;
|
||||||
if(ctx.MUL() != null)return Operator.MUL;
|
if (ctx.SUB() != null) return Operator.SUB;
|
||||||
|
if (ctx.MUL() != null) return Operator.MUL;
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
public static Binary generateBinary(DecafParser.BinaryOperationContext ctx){
|
|
||||||
|
public static Binary generateBinary(DecafParser.BinaryOperationContext ctx) {
|
||||||
ExpressionGenerator eGen = new ExpressionGenerator();
|
ExpressionGenerator eGen = new ExpressionGenerator();
|
||||||
return new Binary(eGen.visit(ctx.expr().get(0)) // left side
|
return new Binary(eGen.visit(ctx.expr().get(0)) // left side
|
||||||
, generateOperator(ctx.binaryOp()) //operator
|
, generateOperator(ctx.binaryOp()) //operator
|
||||||
, eGen.visit(ctx.expr().get(1))); //right side
|
, eGen.visit(ctx.expr().get(1))); //right side
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Location generateLocation(DecafParser.LocContext loc) {
|
public static Location generateLocation(DecafParser.LocContext loc) {
|
||||||
return new Location(loc.id().getText());
|
return new Location(loc.id().getText());
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import de.dhbw.horb.antlr.DecafParser;
|
|||||||
import de.dhbw.horb.ast.*;
|
import de.dhbw.horb.ast.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
||||||
@Override
|
@Override
|
||||||
@ -14,7 +15,7 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visitIf(DecafParser.IfContext ctx) {
|
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)
|
if (ctx.block().size() > 1)
|
||||||
elseBlock = new BlockGenerator().visit(ctx.block(1));
|
elseBlock = new BlockGenerator().visit(ctx.block(1));
|
||||||
return new IfElse(new ExpressionGenerator().visit(ctx.expr()), new BlockGenerator().visit(ctx.block(0)), elseBlock);
|
return new IfElse(new ExpressionGenerator().visit(ctx.expr()), new BlockGenerator().visit(ctx.block(0)), elseBlock);
|
||||||
|
Loading…
Reference in New Issue
Block a user