FieldAccessType added

This commit is contained in:
Purplumbi 2024-05-08 15:04:25 +02:00
parent 5529704693
commit 7c54c0f04c
3 changed files with 30 additions and 23 deletions

View File

@ -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<MemberNode> 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;

View File

@ -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;
}

View File

@ -19,20 +19,20 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@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<ASTNode> {
return method;
}
@Override
public ASTNode visitParameterList(SimpleJavaParser.ParameterListContext ctx) {
List<ParameterNode> 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<ASTNode> {
return null;
}
@Override
public ASTNode visitParameterList(SimpleJavaParser.ParameterListContext ctx) {
List<ParameterNode> 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);
}
}