From 70ae37d4c96d1d47173e37b649255ef99cabd63d Mon Sep 17 00:00:00 2001 From: Boolean-True Date: Thu, 2 May 2024 13:40:34 +0200 Subject: [PATCH] wip update records --- src/main/java/de/maishai/ASTGenerator.java | 4 ++-- src/main/java/de/maishai/BlockGenerator.java | 4 ++-- src/main/java/de/maishai/VariableGenerator.java | 10 +++++----- src/main/java/de/maishai/ast/records/Block.java | 2 +- src/main/java/de/maishai/ast/records/Class.java | 2 +- src/main/java/de/maishai/ast/records/Field.java | 6 ++++++ .../ast/records/{Variable.java => LocalVariable.java} | 2 +- src/main/java/de/maishai/ast/records/Program.java | 2 +- 8 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 src/main/java/de/maishai/ast/records/Field.java rename src/main/java/de/maishai/ast/records/{Variable.java => LocalVariable.java} (61%) diff --git a/src/main/java/de/maishai/ASTGenerator.java b/src/main/java/de/maishai/ASTGenerator.java index c357801..f498628 100644 --- a/src/main/java/de/maishai/ASTGenerator.java +++ b/src/main/java/de/maishai/ASTGenerator.java @@ -20,7 +20,7 @@ public class ASTGenerator { } public static Class generateClass(DecafParser.ClassContext ctx) { - List vars = new ArrayList<>(); + List 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); } diff --git a/src/main/java/de/maishai/BlockGenerator.java b/src/main/java/de/maishai/BlockGenerator.java index 7d9e616..d69d731 100644 --- a/src/main/java/de/maishai/BlockGenerator.java +++ b/src/main/java/de/maishai/BlockGenerator.java @@ -4,14 +4,14 @@ import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafParser; import de.maishai.ast.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 { @Override public Block visitBlock(DecafParser.BlockContext ctx) { - List vars = ctx.var().stream().map(var -> new VariableGenerator().visit(var)).toList(); + List vars = ctx.var().stream().map(var -> new VariableGenerator().visit(var)).toList(); List statements = ctx.stmt().stream().map(stmt -> new StatementGenerator().visit(stmt)).toList(); return new Block(vars, statements); } diff --git a/src/main/java/de/maishai/VariableGenerator.java b/src/main/java/de/maishai/VariableGenerator.java index 9cebb91..f2dc523 100644 --- a/src/main/java/de/maishai/VariableGenerator.java +++ b/src/main/java/de/maishai/VariableGenerator.java @@ -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 { +public class VariableGenerator extends DecafBaseVisitor { @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); } } diff --git a/src/main/java/de/maishai/ast/records/Block.java b/src/main/java/de/maishai/ast/records/Block.java index e36affe..fc3668f 100644 --- a/src/main/java/de/maishai/ast/records/Block.java +++ b/src/main/java/de/maishai/ast/records/Block.java @@ -7,5 +7,5 @@ import de.maishai.ast.Statement; import java.util.List; -public record Block(List vars, List stmts) implements Node { +public record Block(List vars, List stmts) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/Class.java b/src/main/java/de/maishai/ast/records/Class.java index 203a48e..7ada705 100644 --- a/src/main/java/de/maishai/ast/records/Class.java +++ b/src/main/java/de/maishai/ast/records/Class.java @@ -4,5 +4,5 @@ import de.maishai.ast.Node; import java.util.List; -public record Class(Id id ,List variables, List methods) implements Node { +public record Class(Id id , List variables, List methods) implements Node { } diff --git a/src/main/java/de/maishai/ast/records/Field.java b/src/main/java/de/maishai/ast/records/Field.java new file mode 100644 index 0000000..562749c --- /dev/null +++ b/src/main/java/de/maishai/ast/records/Field.java @@ -0,0 +1,6 @@ +package de.maishai.ast.records; + +import de.maishai.ast.Type; + +public record Field(Id name, Type type) { +} diff --git a/src/main/java/de/maishai/ast/records/Variable.java b/src/main/java/de/maishai/ast/records/LocalVariable.java similarity index 61% rename from src/main/java/de/maishai/ast/records/Variable.java rename to src/main/java/de/maishai/ast/records/LocalVariable.java index adab4ea..0982ee0 100644 --- a/src/main/java/de/maishai/ast/records/Variable.java +++ b/src/main/java/de/maishai/ast/records/LocalVariable.java @@ -5,5 +5,5 @@ import de.maishai.ast.Expression; import de.maishai.ast.Node; 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 { } diff --git a/src/main/java/de/maishai/ast/records/Program.java b/src/main/java/de/maishai/ast/records/Program.java index ec638c0..8421151 100644 --- a/src/main/java/de/maishai/ast/records/Program.java +++ b/src/main/java/de/maishai/ast/records/Program.java @@ -5,5 +5,5 @@ import de.maishai.ast.Node; import java.util.List; -public record Program(List variables) implements Node { +public record Program(List classes) implements Node { }