Compare commits

..

1 Commits

Author SHA1 Message Date
d0c24b9ac8 Fix StatementGeneratorTest 2024-03-20 12:58:17 +00:00
5 changed files with 12 additions and 82 deletions

View File

@ -2,9 +2,6 @@ package de.dhbw.horb;
import de.dhbw.horb.ast.*;
import java.util.Collections;
import java.util.List;
public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
@Override
public Expression visitBinaryOperation(DecafParser.BinaryOperationContext ctx) {
@ -13,7 +10,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
@Override
public Expression visitFunCallExpression(DecafParser.FunCallExpressionContext ctx) {
return generateFunctionCall(ctx.funcCall());
throw new RuntimeException("TODO");
}
@Override
@ -33,14 +30,6 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
return generateLocation(ctx.loc());
}
public static FunctionCall generateFunctionCall(DecafParser.FuncCallContext ctx) {
String name = ctx.id().getText();
List<DecafParser.ExprContext> expressions = ctx.args() == null ? Collections.emptyList() : ctx.args().expr();
List<Expression> args = expressions.stream()
.map(exp -> new ExpressionGenerator().visit(exp)).toList();
return new FunctionCall(name, args);
}
public static Expression generateConstant(DecafParser.LiteralContext ctx){
if(ctx.number() != null)
return new IntConstant(Integer.valueOf(ctx.number().getText()));

View File

@ -2,74 +2,43 @@ package de.dhbw.horb;
import de.dhbw.horb.ast.*;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
public class StatementGenerator extends DecafBaseVisitor<Statement> {
@Override
public Statement visitAssign(DecafParser.AssignContext ctx) {
Location loc = ExpressionGenerator.generateLocation(ctx.loc());
Expression exp = ctx.expr().accept(new ExpressionGenerator());
return new Assignment(loc, exp);
}
@Override
public Statement visitFunctionCall(DecafParser.FunctionCallContext ctx) {
return ExpressionGenerator.generateFunctionCall(ctx.funcCall());
throw new RuntimeException("TODO");
}
@Override
public Statement visitIf(DecafParser.IfContext ctx) {
Expression exp = ctx.accept(new ExpressionGenerator());
Block ifBlock = generateBlock(ctx.block(0));
Block elseBlock = ctx.block().size() > 1 ?
generateBlock(ctx.block(1)) :
new Block(Collections.emptyList(), Collections.emptyList());
return new IfElse(exp, ifBlock, elseBlock);
throw new RuntimeException("TODO");
}
@Override
public Statement visitWhile(DecafParser.WhileContext ctx) {
return new While(ctx.expr().accept(new ExpressionGenerator()), generateBlock(ctx.block()));
throw new RuntimeException("TODO");
}
@Override
public Statement visitReturn(DecafParser.ReturnContext ctx) {
return new Return(ctx.expr().accept(new ExpressionGenerator()));
throw new RuntimeException("TODO");
}
@Override
public Statement visitReturnVoid(DecafParser.ReturnVoidContext ctx) {
return new ReturnVoid();
throw new RuntimeException("TODO");
}
@Override
public Statement visitBreak(DecafParser.BreakContext ctx) {
return new Break();
throw new RuntimeException("TODO");
}
@Override
public Statement visitContinue(DecafParser.ContinueContext ctx) {
return new Continue();
throw new RuntimeException("TODO");
}
public Block generateBlock(DecafParser.BlockContext ctx) {
List<Variable> vars = ctx.var().stream().map(StatementGenerator::generateVariable).toList();
List<Statement> stmt = ctx.stmt().stream().map(this::visit).toList();
return new Block(vars, stmt);
}
public static Variable generateVariable(DecafParser.VarContext ctx) {
Type type;
if (ctx.type().INT() != null) {
type = Type.INT;
} else if (ctx.type().BOOL() != null) {
type = Type.BOOL;
} else if (ctx.type().VOID() != null) {
type = Type.VOID;
} else {
throw new RuntimeException("Unknown Type");
}
return new Variable(ctx.id().getText(), type);
}
}

View File

@ -2,5 +2,5 @@ package de.dhbw.horb.ast;
import java.util.List;
public record FunctionCall(String name, List<Expression> args) implements Expression, Statement {
public record FunctionCall(String name, List<Expression> args) implements Expression {
}

View File

@ -1,4 +1,4 @@
package de.dhbw.horb.ast;
public sealed interface Statement extends Node permits Assignment, Break, Continue, FunctionCall, IfElse, Return, ReturnVoid, VoidFunctionCall, While {
public sealed interface Statement extends Node permits Assignment, VoidFunctionCall, IfElse, While, Return, ReturnVoid, Break, Continue {
}

View File

@ -7,40 +7,12 @@ import org.antlr.v4.runtime.CommonTokenStream;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
/**
* Unit test for AST Generation of Expressions.
*/
public class StatementGeneratorTest
{
@Test
public void ifTest() {
Statement stmt = generateStatement("if (true) { break; }");
assertTrue(stmt instanceof IfElse);
IfElse ifElse = (IfElse) stmt;
assertEquals(0, (ifElse.elseBlock().stmts().size()));
assertTrue(ifElse.ifBlock().stmts().get(0) instanceof Break);
}
@Test
public void ifElseTest() {
Statement stmt = generateStatement("if (true) { int x; } else { return; }");
assertTrue(stmt instanceof IfElse);
IfElse ifElse = (IfElse) stmt;
assertEquals("x", ifElse.ifBlock().vars().get(0).name());
assertTrue(ifElse.elseBlock().stmts().get(0) instanceof ReturnVoid);
}
@Test
public void methodCall() {
Statement stmt = generateStatement("foo();");
assertTrue(stmt instanceof FunctionCall);
FunctionCall functionCall = (FunctionCall) stmt;
assertEquals(0, functionCall.args().size());
assertEquals("foo", functionCall.name());
}
@Test
public void whileTest()
{
@ -55,6 +27,6 @@ public class StatementGeneratorTest
DecafLexer lexer = new DecafLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
DecafParser parser = new DecafParser(tokens);
return new StatementGenerator().visit(parser.stmt());
return parser.stmt().accept(new StatementGenerator());
}
}