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); semanticAnalyzer.analyze(ast);
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(); 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.AccessTypeNode;
import ast.type.EnumAccessTypeNode; import ast.type.EnumAccessTypeNode;
import bytecode.visitor.ClassVisitor;
import bytecode.visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ClassNode extends ASTNode{ public class ClassNode extends ASTNode implements Visitable {
public IdentifierNode identifier; public IdentifierNode identifier;
public AccessTypeNode accessType; public AccessTypeNode accessType;
public String name; public String name;
@ -33,4 +35,9 @@ public class ClassNode extends ASTNode{
members.add(0,constructor); members.add(0,constructor);
} }
} }
@Override
public void accept(ClassVisitor classVisitor) {
classVisitor.visit(this);
}
} }

View File

@ -1,12 +1,19 @@
package ast; package ast;
import bytecode.visitor.ProgramVisitor;
import bytecode.visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ProgramNode extends ASTNode { public class ProgramNode extends ASTNode implements Visitable {
public List<ClassNode> classes = new ArrayList<>(); public List<ClassNode> classes = new ArrayList<>();
public void addClass(ClassNode classNode) { public void addClass(ClassNode classNode) {
classes.add(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.ProgramNode;
import ast.ClassNode; import ast.ClassNode;
import bytecode.visitor.ProgramVisitor;
public class ByteCodeGenerator { public class ByteCodeGenerator implements ProgramVisitor {
public void generateByteCode(ProgramNode ast) { @Override
for (ClassNode classDeclarationNode : ast.classes) { public void visit(ProgramNode programNode) {
for (ClassNode classDeclarationNode : programNode.classes) {
ClassCodeGen classCodeGen = new ClassCodeGen(); ClassCodeGen classCodeGen = new ClassCodeGen();
classCodeGen.generateClassCode(classDeclarationNode); classDeclarationNode.accept(classCodeGen);
} }
} }
} }

View File

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

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) {
}
}