First try with visitors

This commit is contained in:
i22007 2024-05-13 20:15:17 +02:00
parent 651a8b6ebf
commit 1fbec0d754
8 changed files with 56 additions and 10 deletions

View File

@ -48,6 +48,6 @@ public class Main {
semanticAnalyzer.analyze(ast);
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
byteCodeGenerator.generateByteCode(ast);
byteCodeGenerator.visit(ast);
}
}

View File

@ -5,10 +5,12 @@ import ast.member.MemberNode;
import ast.type.AccessTypeNode;
import ast.type.EnumAccessTypeNode;
import bytecode.visitor.ClassVisitor;
import bytecode.visitor.Visitable;
import java.util.ArrayList;
import java.util.List;
public class ClassNode extends ASTNode{
public class ClassNode extends ASTNode implements Visitable {
public IdentifierNode identifier;
public AccessTypeNode accessType;
public String name;
@ -33,4 +35,9 @@ public class ClassNode extends ASTNode{
members.add(0,constructor);
}
}
@Override
public void accept(ClassVisitor classVisitor) {
classVisitor.visit(this);
}
}

View File

@ -1,12 +1,19 @@
package ast;
import bytecode.visitor.ProgramVisitor;
import bytecode.visitor.Visitable;
import java.util.ArrayList;
import java.util.List;
public class ProgramNode extends ASTNode {
public class ProgramNode extends ASTNode implements Visitable {
public List<ClassNode> classes = new ArrayList<>();
public void addClass(ClassNode classNode) {
classes.add(classNode);
}
@Override
public void accept(ProgramVisitor programVisitor) {
programVisitor.visit(this);
}
}

View File

@ -2,13 +2,15 @@ package bytecode;
import ast.ProgramNode;
import ast.ClassNode;
import bytecode.visitor.ProgramVisitor;
public class ByteCodeGenerator {
public class ByteCodeGenerator implements ProgramVisitor {
public void generateByteCode(ProgramNode ast) {
for (ClassNode classDeclarationNode : ast.classes) {
@Override
public void visit(ProgramNode programNode) {
for (ClassNode classDeclarationNode : programNode.classes) {
ClassCodeGen classCodeGen = new ClassCodeGen();
classCodeGen.generateClassCode(classDeclarationNode);
classDeclarationNode.accept(classCodeGen);
}
}
}

View File

@ -1,9 +1,12 @@
package bytecode;
import ast.ClassNode;
import ast.ProgramNode;
import ast.member.FieldNode;
import ast.member.MemberNode;
import ast.member.MethodNode;
import bytecode.visitor.ClassVisitor;
import bytecode.visitor.ProgramVisitor;
import java.io.File;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
@ -11,12 +14,14 @@ import org.objectweb.asm.Opcodes;
import java.io.FileOutputStream;
import java.io.IOException;
public class ClassCodeGen {
public void generateClassCode(ClassNode classNode) {
public class ClassCodeGen implements ClassVisitor {
@Override
public void visit(ClassNode classNode) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
Mapper mapper = new Mapper();
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.name, null,
"java/lang/Object", null);
"java/lang/Object", null);
for (MemberNode memberNode : classNode.members) {
if (memberNode instanceof FieldNode) {

View File

@ -0,0 +1,7 @@
package bytecode.visitor;
import ast.ClassNode;
public interface ClassVisitor {
void visit(ClassNode classNode);
}

View File

@ -0,0 +1,7 @@
package bytecode.visitor;
import ast.ProgramNode;
public interface ProgramVisitor {
void visit(ProgramNode programNode);
}

View File

@ -0,0 +1,11 @@
package bytecode.visitor;
public interface Visitable {
default void accept(ProgramVisitor programVisitor) {
}
default void accept(ClassVisitor classVisitor) {
}
}