mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-29 00:28:04 +00:00
Add missing visit functions
This commit is contained in:
parent
12623821f7
commit
63246b815a
@ -33,7 +33,7 @@ public class ASTGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Variable generateVariable(DecafParser.VarContext ctx) {
|
public static Variable generateVariable(DecafParser.VarContext ctx) {
|
||||||
return new Variable(ctx.id().getText(), getType(ctx.type()));
|
return new VariableGenerator().visitVar(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Parameter generateParameter(DecafParser.ParamContext ctx) {
|
public static Parameter generateParameter(DecafParser.ParamContext ctx) {
|
||||||
@ -50,6 +50,11 @@ public class ASTGenerator {
|
|||||||
return new Method(getReturnType(ctx.returntype()), methId, params, block);
|
return new Method(getReturnType(ctx.returntype()), methId, params, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static MainMethod generateMainMethod(DecafParser.MainmethContext ctx) {
|
||||||
|
Block block = new BlockGenerator().visit(ctx.block());
|
||||||
|
return new MainMethod(block);
|
||||||
|
}
|
||||||
|
|
||||||
public static Type getType(DecafParser.TypeContext ctx){
|
public static Type getType(DecafParser.TypeContext ctx){
|
||||||
if(ctx.INT() != null)
|
if(ctx.INT() != null)
|
||||||
return Type.INT;
|
return Type.INT;
|
||||||
|
@ -4,10 +4,8 @@ import de.maishai.antlr.DecafBaseVisitor;
|
|||||||
import de.maishai.antlr.DecafParser;
|
import de.maishai.antlr.DecafParser;
|
||||||
import de.maishai.ast.Expression;
|
import de.maishai.ast.Expression;
|
||||||
import de.maishai.ast.Operator;
|
import de.maishai.ast.Operator;
|
||||||
import de.maishai.ast.records.Binary;
|
import de.maishai.ast.Type;
|
||||||
import de.maishai.ast.records.BoolConstant;
|
import de.maishai.ast.records.*;
|
||||||
import de.maishai.ast.records.IntConstant;
|
|
||||||
import de.maishai.ast.records.MethodCall;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -18,17 +16,6 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
return generateBinary(ctx);
|
return generateBinary(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Expression visitMethodCallExpression(DecafParser.MethodCallExpressionContext ctx) {
|
|
||||||
String functionName = ctx.methCall().id().getText();
|
|
||||||
List<Expression> args = new ArrayList<>();
|
|
||||||
for(var expr : ctx.methCall().args().expr()){
|
|
||||||
Expression astExpr = expr.accept(this);
|
|
||||||
args.add(astExpr);
|
|
||||||
}
|
|
||||||
return new MethodCall(functionName, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Expression visitConstant(DecafParser.ConstantContext ctx) {
|
public Expression visitConstant(DecafParser.ConstantContext ctx) {
|
||||||
return generateConstant(ctx.literal());
|
return generateConstant(ctx.literal());
|
||||||
@ -41,6 +28,22 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
return this.visit(ctx.expr());
|
return this.visit(ctx.expr());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Expression visitMethodCallExpression(DecafParser.MethodCallExpressionContext ctx) {
|
||||||
|
Id id = new Id(ctx.methCall().id().getText());
|
||||||
|
List<Expression> args = new ArrayList<>();
|
||||||
|
for(var expr : ctx.methCall().args().expr()){
|
||||||
|
Expression astExpr = expr.accept(this);
|
||||||
|
args.add(astExpr);
|
||||||
|
}
|
||||||
|
return new MethodCallExpression(id, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Id visitIdentifier(DecafParser.IdentifierContext ctx){
|
||||||
|
return new Id(ctx.getText());
|
||||||
|
}
|
||||||
|
|
||||||
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()));
|
||||||
@ -62,4 +65,34 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
if(ctx.MUL() != null)return Operator.MUL;
|
if(ctx.MUL() != null)return Operator.MUL;
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//StatementExpression
|
||||||
|
@Override
|
||||||
|
public Expression visitAssign(DecafParser.AssignContext ctx) {
|
||||||
|
Id id = new Id(ctx.id().getText());
|
||||||
|
Expression expr = this.visit(ctx.expr());
|
||||||
|
return new Assignment(id, expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Expression visitMethodCall(DecafParser.MethodCallContext ctx) {
|
||||||
|
Id id = new Id(ctx.methCall().id().getText());
|
||||||
|
List<Expression> args = new ArrayList<>();
|
||||||
|
for(var expr : ctx.methCall().args().expr()){
|
||||||
|
Expression astExpr = expr.accept(this);
|
||||||
|
args.add(astExpr);
|
||||||
|
}
|
||||||
|
return new MethodCall(id, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Expression visitNew(DecafParser.NewContext ctx) {
|
||||||
|
Type type = ASTGenerator.getType(ctx.type());
|
||||||
|
List<Expression> args = new ArrayList<>();
|
||||||
|
for(var expr : ctx.args().expr()){
|
||||||
|
Expression astExpr = expr.accept(this);
|
||||||
|
args.add(astExpr);
|
||||||
|
}
|
||||||
|
return new New(type, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,16 +4,13 @@ import de.maishai.antlr.DecafBaseVisitor;
|
|||||||
import de.maishai.antlr.DecafParser;
|
import de.maishai.antlr.DecafParser;
|
||||||
import de.maishai.ast.Expression;
|
import de.maishai.ast.Expression;
|
||||||
import de.maishai.ast.Statement;
|
import de.maishai.ast.Statement;
|
||||||
|
import de.maishai.ast.Type;
|
||||||
import de.maishai.ast.records.*;
|
import de.maishai.ast.records.*;
|
||||||
|
|
||||||
public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
import java.util.ArrayList;
|
||||||
@Override
|
import java.util.List;
|
||||||
public Statement visitAssign(DecafParser.AssignContext ctx) {
|
|
||||||
Id id = new Id(ctx.id().getText());
|
|
||||||
Expression expr = new ExpressionGenerator().visit(ctx.expr());
|
|
||||||
return new Assignment(id, expr);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
||||||
@Override
|
@Override
|
||||||
public Statement visitIf(DecafParser.IfContext ctx) {
|
public Statement visitIf(DecafParser.IfContext ctx) {
|
||||||
Expression expr = new ExpressionGenerator().visit(ctx.expr());
|
Expression expr = new ExpressionGenerator().visit(ctx.expr());
|
||||||
@ -25,6 +22,15 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
|||||||
return new IfElse(expr, ifBlock, null);
|
return new IfElse(expr, ifBlock, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public For visitFor(DecafParser.ForContext ctx) {
|
||||||
|
Expression init = new ExpressionGenerator().visit(ctx.expr(0));
|
||||||
|
Expression expr = new ExpressionGenerator().visit(ctx.expr(1));
|
||||||
|
Expression update = new ExpressionGenerator().visit(ctx.expr(2));
|
||||||
|
Block block = new BlockGenerator().visit(ctx.block());
|
||||||
|
return new For(init, expr, update, block);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visitWhile(DecafParser.WhileContext ctx) {
|
public Statement visitWhile(DecafParser.WhileContext ctx) {
|
||||||
Expression expr = new ExpressionGenerator().visit(ctx.expr());
|
Expression expr = new ExpressionGenerator().visit(ctx.expr());
|
||||||
@ -32,6 +38,13 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
|||||||
return new While(expr, block);
|
return new While(expr, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DoWhile visitDoWhile(DecafParser.DoWhileContext ctx) {
|
||||||
|
Block block = new BlockGenerator().visit(ctx.block());
|
||||||
|
Expression expr = new ExpressionGenerator().visit(ctx.expr());
|
||||||
|
return new DoWhile(block, expr);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement visitReturn(DecafParser.ReturnContext ctx) {
|
public Statement visitReturn(DecafParser.ReturnContext ctx) {
|
||||||
return new Return(new ExpressionGenerator().visit(ctx.expr()));
|
return new Return(new ExpressionGenerator().visit(ctx.expr()));
|
||||||
@ -52,4 +65,33 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
|||||||
return new Continue();
|
return new Continue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//StatementExpression
|
||||||
|
@Override
|
||||||
|
public Statement visitAssign(DecafParser.AssignContext ctx) {
|
||||||
|
Id id = new Id(ctx.id().getText());
|
||||||
|
Expression expr = new ExpressionGenerator().visit(ctx.expr());
|
||||||
|
return new Assignment(id, expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Statement visitMethodCall(DecafParser.MethodCallContext ctx) {
|
||||||
|
Id id = new Id(ctx.methCall().id().getText());
|
||||||
|
List<Expression> args = new ArrayList<>();
|
||||||
|
for(var expr : ctx.methCall().args().expr()){
|
||||||
|
Expression astExpr = expr.accept(new ExpressionGenerator());
|
||||||
|
args.add(astExpr);
|
||||||
|
}
|
||||||
|
return new MethodCall(id, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Statement visitNew(DecafParser.NewContext ctx) {
|
||||||
|
Type type = ASTGenerator.getType(ctx.type());
|
||||||
|
List<Expression> args = new ArrayList<>();
|
||||||
|
for(var expr : ctx.args().expr()){
|
||||||
|
Expression astExpr = expr.accept(new ExpressionGenerator());
|
||||||
|
args.add(astExpr);
|
||||||
|
}
|
||||||
|
return new New(type, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,22 +4,17 @@ package de.maishai;
|
|||||||
import de.maishai.antlr.DecafBaseVisitor;
|
import de.maishai.antlr.DecafBaseVisitor;
|
||||||
import de.maishai.antlr.DecafParser;
|
import de.maishai.antlr.DecafParser;
|
||||||
import de.maishai.ast.Type;
|
import de.maishai.ast.Type;
|
||||||
|
import de.maishai.ast.records.Id;
|
||||||
import de.maishai.ast.records.Variable;
|
import de.maishai.ast.records.Variable;
|
||||||
|
|
||||||
public class VariableGenerator extends DecafBaseVisitor<Variable> {
|
public class VariableGenerator extends DecafBaseVisitor<Variable> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Variable visitVar(DecafParser.VarContext ctx) {
|
public Variable visitVar(DecafParser.VarContext ctx) {
|
||||||
return new Variable(ctx.id().getText(), getType(ctx.type()));
|
Id id = new Id(ctx.id().getText());
|
||||||
}
|
Type type = ASTGenerator.getType(ctx.type());
|
||||||
|
if(ctx.expr() != null)
|
||||||
public static Type getType(DecafParser.TypeContext ctx){
|
return new Variable(id, type, new ExpressionGenerator().visit(ctx.expr()));
|
||||||
if(ctx.INT() != null)
|
return new Variable(id, type, null);
|
||||||
return Type.INT;
|
|
||||||
if(ctx.BOOL() != null)
|
|
||||||
return Type.BOOL;
|
|
||||||
if(ctx.CHAR() != null)
|
|
||||||
return Type.CHAR;
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
src/main/java/de/maishai/ast/records/DoWhile.java
Normal file
8
src/main/java/de/maishai/ast/records/DoWhile.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package de.maishai.ast.records;
|
||||||
|
|
||||||
|
|
||||||
|
import de.maishai.ast.Expression;
|
||||||
|
import de.maishai.ast.Statement;
|
||||||
|
|
||||||
|
public record DoWhile(Block block, Expression cond) implements Statement {
|
||||||
|
}
|
@ -7,5 +7,5 @@ import de.maishai.ast.ReturnType;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public record MainMethod(ReturnType type, Id id, List<Parameter> params, Block block) implements Node {
|
public record MainMethod(Block block) implements Node {
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,5 @@ import de.maishai.ast.Statement;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public record MethodCall(String name, List<Expression> args) implements Expression, Statement {
|
public record MethodCall(Id id, List<Expression> args) implements Expression, Statement {
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package de.maishai.ast.records;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import de.maishai.ast.Expression;
|
||||||
|
import de.maishai.ast.Statement;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record MethodCallExpression(Id id, List<Expression> args) implements Expression, Statement {
|
||||||
|
}
|
@ -1,8 +1,9 @@
|
|||||||
package de.maishai.ast.records;
|
package de.maishai.ast.records;
|
||||||
|
|
||||||
|
|
||||||
|
import de.maishai.ast.Expression;
|
||||||
import de.maishai.ast.Node;
|
import de.maishai.ast.Node;
|
||||||
import de.maishai.ast.Type;
|
import de.maishai.ast.Type;
|
||||||
|
|
||||||
public record Variable(String name, Type type) implements Node {
|
public record Variable(Id name, Type type, Expression expr) implements Node {
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user