First try with visitors
This commit is contained in:
parent
651a8b6ebf
commit
1fbec0d754
@ -48,6 +48,6 @@ public class Main {
|
||||
semanticAnalyzer.analyze(ast);
|
||||
|
||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||
byteCodeGenerator.generateByteCode(ast);
|
||||
byteCodeGenerator.visit(ast);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
7
src/main/java/bytecode/visitor/ClassVisitor.java
Normal file
7
src/main/java/bytecode/visitor/ClassVisitor.java
Normal file
@ -0,0 +1,7 @@
|
||||
package bytecode.visitor;
|
||||
|
||||
import ast.ClassNode;
|
||||
|
||||
public interface ClassVisitor {
|
||||
void visit(ClassNode classNode);
|
||||
}
|
7
src/main/java/bytecode/visitor/ProgramVisitor.java
Normal file
7
src/main/java/bytecode/visitor/ProgramVisitor.java
Normal file
@ -0,0 +1,7 @@
|
||||
package bytecode.visitor;
|
||||
|
||||
import ast.ProgramNode;
|
||||
|
||||
public interface ProgramVisitor {
|
||||
void visit(ProgramNode programNode);
|
||||
}
|
11
src/main/java/bytecode/visitor/Visitable.java
Normal file
11
src/main/java/bytecode/visitor/Visitable.java
Normal file
@ -0,0 +1,11 @@
|
||||
package bytecode.visitor;
|
||||
|
||||
public interface Visitable {
|
||||
|
||||
default void accept(ProgramVisitor programVisitor) {
|
||||
|
||||
}
|
||||
default void accept(ClassVisitor classVisitor) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user