First try with visitors
This commit is contained in:
parent
651a8b6ebf
commit
1fbec0d754
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,12 +14,14 @@ 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,
|
||||||
"java/lang/Object", null);
|
"java/lang/Object", null);
|
||||||
|
|
||||||
for (MemberNode memberNode : classNode.members) {
|
for (MemberNode memberNode : classNode.members) {
|
||||||
if (memberNode instanceof FieldNode) {
|
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