From 7c54c0f04c47e16dff787a0102e3f44a94718a72 Mon Sep 17 00:00:00 2001 From: Purplumbi Date: Wed, 8 May 2024 15:04:25 +0200 Subject: [PATCH] FieldAccessType added --- src/main/java/ast/ClassNode.java | 6 ++++ src/main/java/ast/FieldNode.java | 4 ++- src/main/java/parser/ASTBuilder.java | 43 ++++++++++++++-------------- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/main/java/ast/ClassNode.java b/src/main/java/ast/ClassNode.java index 0bf768b..722f1af 100644 --- a/src/main/java/ast/ClassNode.java +++ b/src/main/java/ast/ClassNode.java @@ -4,10 +4,16 @@ import java.util.ArrayList; import java.util.List; public class ClassNode extends ASTNode{ + public AccessTypeNode accessType; public String name; public List members = new ArrayList<>(); public boolean hasConstructor = false; + public ClassNode(AccessTypeNode accessType, String name){ + this.accessType = accessType; + this.name = name; + } + public void addMember(MemberNode member) { if (member instanceof ConstructorNode) { this.hasConstructor = true; diff --git a/src/main/java/ast/FieldNode.java b/src/main/java/ast/FieldNode.java index 948701f..0ac7cd4 100644 --- a/src/main/java/ast/FieldNode.java +++ b/src/main/java/ast/FieldNode.java @@ -1,10 +1,12 @@ package ast; public class FieldNode extends MemberNode { + public AccessTypeNode accessTypeNode; public TypeNode type; public String name; - public FieldNode(TypeNode type, String name){ + public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){ + this.accessTypeNode = accessTypeNode; this.type = type; this.name = name; } diff --git a/src/main/java/parser/ASTBuilder.java b/src/main/java/parser/ASTBuilder.java index 48c26a5..931fc76 100644 --- a/src/main/java/parser/ASTBuilder.java +++ b/src/main/java/parser/ASTBuilder.java @@ -19,20 +19,20 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { - ClassNode classNode = new ClassNode(); - classNode.name = ctx.IDENTIFIER().getText(); + ClassNode classNode = new ClassNode((AccessTypeNode) visit(ctx.accessType()),ctx.IDENTIFIER().getText()); for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) { classNode.addMember((MemberNode) visit(member)); } - classNode.ensureConstructor(); // Check and add default constructor if needed + classNode.ensureConstructor(); return classNode; } @Override public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) { + AccessTypeNode accessType = (AccessTypeNode) visit(ctx.accessType()); TypeNode type = (TypeNode) visit(ctx.type()); String identifier = ctx.IDENTIFIER().getText(); - return new FieldNode(type, identifier); + return new FieldNode(accessType, type, identifier); } @Override @@ -51,9 +51,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { return method; } + @Override + public ASTNode visitParameterList(SimpleJavaParser.ParameterListContext ctx) { + List parameters = new ArrayList<>(); + for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) { + parameters.add((ParameterNode) visitParameter(paramCtx)); + } + return new ParameterListNode(parameters); + } + + @Override + public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) { + TypeNode type = (TypeNode) visit(ctx.type()); + String identifier = ctx.IDENTIFIER().getText(); + return new ParameterNode(type, identifier); + } + @Override public ASTNode visitType(SimpleJavaParser.TypeContext ctx) { - String typeStr = ctx.getText(); // Get the text of the type from the context + String typeStr = ctx.getText(); switch (typeStr) { case "int": return new TypeNode(EnumTypeNode.INT); @@ -90,21 +106,4 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { return null; } - @Override - public ASTNode visitParameterList(SimpleJavaParser.ParameterListContext ctx) { - List parameters = new ArrayList<>(); - for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) { - parameters.add((ParameterNode) visitParameter(paramCtx)); - } - return new ParameterListNode(parameters); - } - - @Override - public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) { - TypeNode type = (TypeNode) visit(ctx.type()); // Assuming visitType returns a TypeNode - String identifier = ctx.IDENTIFIER().getText(); - return new ParameterNode(type, identifier); - } - - }