From 91340125ef156054c02f7835cb868bdd649b383a Mon Sep 17 00:00:00 2001 From: StefanZ3 Date: Fri, 10 May 2024 06:23:09 +0200 Subject: [PATCH] added ast generator --- src/main/java/ASTGenerator.java | 21 +++++++++++++++++++ src/main/java/Compiler.java | 1 - src/main/java/abstractSyntaxTree/Node.java | 4 ++++ src/main/java/abstractSyntaxTree/Program.java | 1 - 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ASTGenerator.java create mode 100644 src/main/java/abstractSyntaxTree/Node.java diff --git a/src/main/java/ASTGenerator.java b/src/main/java/ASTGenerator.java new file mode 100644 index 0000000..dc167ce --- /dev/null +++ b/src/main/java/ASTGenerator.java @@ -0,0 +1,21 @@ +import abstractSyntaxTree.Class.IClass; +import abstractSyntaxTree.Class.RefType; +import abstractSyntaxTree.Node; +import abstractSyntaxTree.Program; +import gen.DecafBaseVisitor; +import gen.DecafParser; + +import java.util.ArrayList; +import java.util.List; + +public class ASTGenerator extends DecafBaseVisitor { + @Override + public Node visitProgram(DecafParser.ProgramContext ctx) { + List classes = new ArrayList<>(); + for (DecafParser.ClassdeclContext classDecl : ctx.classdecl()) { + Node refType = new RefType(classDecl.Identifier().getText(), new ArrayList<>(), new ArrayList<>()); + classes.add((RefType) refType); + } + return new Program(classes); + } +} diff --git a/src/main/java/Compiler.java b/src/main/java/Compiler.java index 4ec7a10..ff0aa5b 100644 --- a/src/main/java/Compiler.java +++ b/src/main/java/Compiler.java @@ -1,7 +1,6 @@ import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.MethodDecl; import abstractSyntaxTree.Class.RefType; -import abstractSyntaxTree.Node; import abstractSyntaxTree.Program; import abstractSyntaxTree.Statement.BlockStatement; import gen.DecafLexer; diff --git a/src/main/java/abstractSyntaxTree/Node.java b/src/main/java/abstractSyntaxTree/Node.java new file mode 100644 index 0000000..67475b0 --- /dev/null +++ b/src/main/java/abstractSyntaxTree/Node.java @@ -0,0 +1,4 @@ +package abstractSyntaxTree; + +public interface Node { +} diff --git a/src/main/java/abstractSyntaxTree/Program.java b/src/main/java/abstractSyntaxTree/Program.java index 6d7a733..e51d87a 100644 --- a/src/main/java/abstractSyntaxTree/Program.java +++ b/src/main/java/abstractSyntaxTree/Program.java @@ -3,7 +3,6 @@ package abstractSyntaxTree; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.MethodDecl; -import org.objectweb.asm.MethodVisitor; import abstractSyntaxTree.Class.RefType; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes;