FieldAccessType added
This commit is contained in:
parent
5529704693
commit
7c54c0f04c
@ -4,10 +4,16 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ClassNode extends ASTNode{
|
public class ClassNode extends ASTNode{
|
||||||
|
public AccessTypeNode accessType;
|
||||||
public String name;
|
public String name;
|
||||||
public List<MemberNode> members = new ArrayList<>();
|
public List<MemberNode> members = new ArrayList<>();
|
||||||
public boolean hasConstructor = false;
|
public boolean hasConstructor = false;
|
||||||
|
|
||||||
|
public ClassNode(AccessTypeNode accessType, String name){
|
||||||
|
this.accessType = accessType;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
public void addMember(MemberNode member) {
|
public void addMember(MemberNode member) {
|
||||||
if (member instanceof ConstructorNode) {
|
if (member instanceof ConstructorNode) {
|
||||||
this.hasConstructor = true;
|
this.hasConstructor = true;
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
public class FieldNode extends MemberNode {
|
public class FieldNode extends MemberNode {
|
||||||
|
public AccessTypeNode accessTypeNode;
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String name;
|
public String name;
|
||||||
|
|
||||||
public FieldNode(TypeNode type, String name){
|
public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){
|
||||||
|
this.accessTypeNode = accessTypeNode;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
@ -19,20 +19,20 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
||||||
ClassNode classNode = new ClassNode();
|
ClassNode classNode = new ClassNode((AccessTypeNode) visit(ctx.accessType()),ctx.IDENTIFIER().getText());
|
||||||
classNode.name = ctx.IDENTIFIER().getText();
|
|
||||||
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
||||||
classNode.addMember((MemberNode) visit(member));
|
classNode.addMember((MemberNode) visit(member));
|
||||||
}
|
}
|
||||||
classNode.ensureConstructor(); // Check and add default constructor if needed
|
classNode.ensureConstructor();
|
||||||
return classNode;
|
return classNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
|
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
|
||||||
|
AccessTypeNode accessType = (AccessTypeNode) visit(ctx.accessType());
|
||||||
TypeNode type = (TypeNode) visit(ctx.type());
|
TypeNode type = (TypeNode) visit(ctx.type());
|
||||||
String identifier = ctx.IDENTIFIER().getText();
|
String identifier = ctx.IDENTIFIER().getText();
|
||||||
return new FieldNode(type, identifier);
|
return new FieldNode(accessType, type, identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -51,9 +51,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
return method;
|
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
|
@Override
|
||||||
public ASTNode visitType(SimpleJavaParser.TypeContext ctx) {
|
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) {
|
switch (typeStr) {
|
||||||
case "int":
|
case "int":
|
||||||
return new TypeNode(EnumTypeNode.INT);
|
return new TypeNode(EnumTypeNode.INT);
|
||||||
@ -90,21 +106,4 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
return null;
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user