Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e7ab233f1e | ||
|
f69e5df5e7 | ||
|
04978dafe3 |
@ -2,6 +2,9 @@ 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) {
|
||||
@ -10,7 +13,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
||||
|
||||
@Override
|
||||
public Expression visitFunCallExpression(DecafParser.FunCallExpressionContext ctx) {
|
||||
throw new RuntimeException("TODO");
|
||||
return generateFunctionCall(ctx.funcCall());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -30,6 +33,14 @@ 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()));
|
||||
|
@ -2,43 +2,74 @@ package de.dhbw.horb;
|
||||
|
||||
import de.dhbw.horb.ast.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
||||
@Override
|
||||
public Statement visitAssign(DecafParser.AssignContext ctx) {
|
||||
throw new RuntimeException("TODO");
|
||||
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());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement visitIf(DecafParser.IfContext ctx) {
|
||||
throw new RuntimeException("TODO");
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement visitWhile(DecafParser.WhileContext ctx) {
|
||||
throw new RuntimeException("TODO");
|
||||
return new While(ctx.expr().accept(new ExpressionGenerator()), generateBlock(ctx.block()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement visitReturn(DecafParser.ReturnContext ctx) {
|
||||
throw new RuntimeException("TODO");
|
||||
return new Return(ctx.expr().accept(new ExpressionGenerator()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement visitReturnVoid(DecafParser.ReturnVoidContext ctx) {
|
||||
throw new RuntimeException("TODO");
|
||||
return new ReturnVoid();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement visitBreak(DecafParser.BreakContext ctx) {
|
||||
throw new RuntimeException("TODO");
|
||||
return new Break();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement visitContinue(DecafParser.ContinueContext ctx) {
|
||||
throw new RuntimeException("TODO");
|
||||
return new Continue();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -2,5 +2,5 @@ package de.dhbw.horb.ast;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record FunctionCall(String name, List<Expression> args) implements Expression {
|
||||
public record FunctionCall(String name, List<Expression> args) implements Expression, Statement {
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package de.dhbw.horb.ast;
|
||||
|
||||
public sealed interface Statement extends Node permits Assignment, VoidFunctionCall, IfElse, While, Return, ReturnVoid, Break, Continue {
|
||||
public sealed interface Statement extends Node permits Assignment, Break, Continue, FunctionCall, IfElse, Return, ReturnVoid, VoidFunctionCall, While {
|
||||
}
|
||||
|
@ -7,12 +7,40 @@ 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()
|
||||
{
|
||||
@ -27,6 +55,6 @@ public class StatementGeneratorTest
|
||||
DecafLexer lexer = new DecafLexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
DecafParser parser = new DecafParser(tokens);
|
||||
return parser.stmt().accept(new StatementGenerator());
|
||||
return new StatementGenerator().visit(parser.stmt());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user