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; package ast;
public class FieldNode extends MemberNode { public class FieldNode extends MemberNode {
TypeNode type; public TypeNode type;
String name; public String name;
public FieldNode(TypeNode type, String name){ public FieldNode(TypeNode type, String name){
this.type = type; this.type = type;

View File

@ -9,9 +9,13 @@ public class MethodNode extends MemberNode{
public ParameterListNode parameters; 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.visibility = visibility;
this.name = name; this.name = name;
this.parameters = parameters; this.parameters = parameters;
this.statements = statements;
} }
} }

View File

@ -3,10 +3,10 @@ package ast;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ParameterListNode { public class ParameterListNode extends ASTNode {
List<ParameterNode> parameters = new ArrayList<>(); List<ParameterNode> parameters = new ArrayList<>();
public ParameterListNode(){ public ParameterListNode(List<ParameterNode> parameters){
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 { package ast;
public TypeNode type;
public String identifier;
public ParameterNode(TypeNode type, String identifier) { import java.util.ArrayList;
this.type = type; import java.util.List;
this.identifier = identifier;
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; package bytecode;
import java.io.FileOutputStream; import ast.ProgramNode;
import java.io.IOException;
import ast.ClassNode; import ast.ClassNode;
public class ByteCodeGenerator { public class ByteCodeGenerator {

View File

@ -39,23 +39,17 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) { public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
TypeNode returnType = (TypeNode) visit(ctx.type()); TypeNode returnType = (TypeNode) visit(ctx.type());
String methodName = ctx.IDENTIFIER().getText(); String methodName = ctx.IDENTIFIER().getText();
ParameterListNode = (ParameterListNode) visit(ctx.parameterList()); ParameterListNode parameterListNode = (ParameterListNode) visit(ctx.parameterList());
List<ParameterNode> parameters = new ArrayList<>(); List<StatementNode> statements = 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));
}
for (SimpleJavaParser.StatementContext stmtCtx : ctx.statement()) { 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; return method;
} }
@Override @Override
public ASTNode visitType(SimpleJavaParser.TypeContext ctx) { public ASTNode visitType(SimpleJavaParser.TypeContext ctx) {
return new TypeNode(ctx.getText()); return new TypeNode(ctx.getText());
@ -78,7 +72,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) { for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) {
parameters.add((ParameterNode) visitParameter(paramCtx)); 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 @Override