wip update records

This commit is contained in:
Boolean-True 2024-05-02 13:40:34 +02:00
parent e14af08678
commit 70ae37d4c9
8 changed files with 19 additions and 13 deletions

View File

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

View File

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

View File

@ -5,16 +5,16 @@ 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.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 @Override
public Variable visitVar(DecafParser.VarContext ctx) { public LocalVariable visitVar(DecafParser.VarContext ctx) {
Id id = new Id(ctx.id().getText()); Id id = new Id(ctx.id().getText());
Type type = ASTGenerator.getType(ctx.type()); Type type = ASTGenerator.getType(ctx.type());
if(ctx.expr() != null) if(ctx.expr() != null)
return new Variable(id, type, new ExpressionGenerator().visit(ctx.expr())); return new LocalVariable(id, type, new ExpressionGenerator().visit(ctx.expr()));
return new Variable(id, type, null); return new LocalVariable(id, type, null);
} }
} }

View File

@ -7,5 +7,5 @@ import de.maishai.ast.Statement;
import java.util.List; 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

@ -4,5 +4,5 @@ import de.maishai.ast.Node;
import java.util.List; 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

@ -5,5 +5,5 @@ 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(Id name, Type type, Expression expr) implements Node { public record LocalVariable(Id name, Type type) implements Node {
} }

View File

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