Visiters added

This commit is contained in:
Purplumbi 2024-05-08 13:54:39 +02:00
parent 56ce3c33c3
commit 44545f7ba6
7 changed files with 27 additions and 39 deletions

View File

@ -1,8 +1,8 @@
package ast;
public class FieldNode extends MemberNode {
TypeNode type;
String name;
public TypeNode type;
public String name;
public FieldNode(TypeNode type, String name){
this.type = type;

View File

@ -9,9 +9,13 @@ public class MethodNode extends MemberNode{
public ParameterListNode parameters;
public MethodNode(TypeNode visibility, String name, ParameterListNode parameters){
public List<StatementNode> statements = new ArrayList<>();
public MethodNode(TypeNode visibility, String name, ParameterListNode parameters,
List<StatementNode> statements){
this.visibility = visibility;
this.name = name;
this.parameters = parameters;
this.statements = statements;
}
}

View File

@ -3,10 +3,10 @@ package ast;
import java.util.ArrayList;
import java.util.List;
public class ParameterListNode {
public class ParameterListNode extends ASTNode {
List<ParameterNode> parameters = new ArrayList<>();
public ParameterListNode(){
parameters
public ParameterListNode(List<ParameterNode> parameters){
this.parameters = parameters;
}
}

View File

@ -1,11 +0,0 @@
package ast;
public class ParameterNode extends ASTNode {
public TypeNode type;
public String identifier;
public ParameterNode(TypeNode type, String identifier) {
this.type = type;
this.identifier = identifier;
}
}

View File

@ -1,9 +1,12 @@
public class ParameterNode extends ASTNode {
public TypeNode type;
public String identifier;
package ast;
public ParameterNode(TypeNode type, String identifier) {
this.type = type;
this.identifier = identifier;
import java.util.ArrayList;
import java.util.List;
public class ProgramNode extends ASTNode {
public List<ClassNode> classes = new ArrayList<>();
public void addClass(ClassNode classNode) {
classes.add(classNode);
}
}

View File

@ -1,8 +1,6 @@
package bytecode;
import java.io.FileOutputStream;
import java.io.IOException;
import ast.ProgramNode;
import ast.ClassNode;
public class ByteCodeGenerator {

View File

@ -39,23 +39,17 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
TypeNode returnType = (TypeNode) visit(ctx.type());
String methodName = ctx.IDENTIFIER().getText();
ParameterListNode = (ParameterListNode) visit(ctx.parameterList());
List<ParameterNode> parameters = new ArrayList<>();
for (SimpleJavaParser.ParameterContext parameter : ctx.parameterList()) {
parameters.add(parameter);
}
MethodNode method = new MethodNode(returnType, methodName);
for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameterList().parameter()) {
method.parameters.add((ParameterNode) visit(paramCtx));
}
ParameterListNode parameterListNode = (ParameterListNode) visit(ctx.parameterList());
List<StatementNode> statements = new ArrayList<>();
for (SimpleJavaParser.StatementContext stmtCtx : ctx.statement()) {
method.statements.add((StatementNode) visit(stmtCtx));
statements.add((StatementNode) visit(stmtCtx));
}
MethodNode method = new MethodNode(returnType, methodName, parameterListNode, statements);
return method;
}
@Override
public ASTNode visitType(SimpleJavaParser.TypeContext ctx) {
return new TypeNode(ctx.getText());
@ -78,7 +72,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) {
parameters.add((ParameterNode) visitParameter(paramCtx));
}
return new ParameterListNode(parameters); // Assuming you want to keep a dedicated node for the list
return new ParameterListNode(parameters);
}
@Override