This commit is contained in:
laurenz 2024-05-02 13:56:28 +02:00
commit 1a73384825
9 changed files with 25 additions and 19 deletions

View File

@ -20,7 +20,7 @@ public class ASTGenerator {
}
public static Class generateClass(DecafParser.ClassContext ctx) {
List<Variable> vars = new ArrayList<>();
List<LocalVariable> vars = new ArrayList<>();
if(ctx.var() != null){
vars = ctx.var().stream().map(ASTGenerator::generateVariable).toList();
}
@ -32,7 +32,7 @@ public class ASTGenerator {
return new Class(classId, vars, meths);
}
public static Variable generateVariable(DecafParser.VarContext ctx) {
public static LocalVariable generateVariable(DecafParser.VarContext ctx) {
return new VariableGenerator().visitVar(ctx);
}

View File

@ -4,14 +4,14 @@ import de.maishai.antlr.DecafBaseVisitor;
import de.maishai.antlr.DecafParser;
import de.maishai.ast.records.Statement;
import de.maishai.ast.records.Block;
import de.maishai.ast.records.Variable;
import de.maishai.ast.records.LocalVariable;
import java.util.List;
public class BlockGenerator extends DecafBaseVisitor<Block> {
@Override
public Block visitBlock(DecafParser.BlockContext ctx) {
List<Variable> vars = ctx.var().stream().map(var -> new VariableGenerator().visit(var)).toList();
List<LocalVariable> vars = ctx.var().stream().map(var -> new VariableGenerator().visit(var)).toList();
List<Statement> statements = ctx.stmt().stream().map(stmt -> new StatementGenerator().visit(stmt)).toList();
return new Block(vars, statements);
}

View File

@ -5,16 +5,16 @@ import de.maishai.antlr.DecafBaseVisitor;
import de.maishai.antlr.DecafParser;
import de.maishai.ast.Type;
import de.maishai.ast.records.Id;
import de.maishai.ast.records.Variable;
import de.maishai.ast.records.LocalVariable;
public class VariableGenerator extends DecafBaseVisitor<Variable> {
public class VariableGenerator extends DecafBaseVisitor<LocalVariable> {
@Override
public Variable visitVar(DecafParser.VarContext ctx) {
public LocalVariable visitVar(DecafParser.VarContext ctx) {
Id id = new Id(ctx.id().getText());
Type type = ASTGenerator.getType(ctx.type());
if(ctx.expr() != null)
return new Variable(id, type, new ExpressionGenerator().visit(ctx.expr()));
return new Variable(id, type, null);
return new LocalVariable(id, type, new ExpressionGenerator().visit(ctx.expr()));
return new LocalVariable(id, type, null);
}
}

View File

@ -4,5 +4,5 @@ package de.maishai.ast.records;
import java.util.List;
public record Block(List<Variable> vars, List<Statement> stmts) implements Node {
public record Block(List<LocalVariable> vars, List<Statement> stmts) implements Node {
}

View File

@ -2,5 +2,5 @@ package de.maishai.ast.records;
import java.util.List;
public record Class(Id id ,List<Variable> variables, List<Method> methods) implements Node {
public record Class(Id id , List<LocalVariable> variables, List<Method> methods) implements Node {
}

View File

@ -0,0 +1,6 @@
package de.maishai.ast.records;
import de.maishai.ast.Type;
public record Field(Id name, Type type) {
}

View File

@ -0,0 +1,7 @@
package de.maishai.ast.records;
import de.maishai.ast.Type;
public record LocalVariable(Id name, Type type) implements Node {
}

View File

@ -3,5 +3,5 @@ package de.maishai.ast.records;
import java.util.List;
public record Program(List<Class> variables) implements Node {
public record Program(List<Class> classes) implements Node {
}

View File

@ -1,7 +0,0 @@
package de.maishai.ast.records;
import de.maishai.ast.Type;
public record Variable(Id name, Type type, Expression expr) implements Node {
}