Merge branch 'main' into johns-branch
This commit is contained in:
commit
7249054da5
@ -20,7 +20,7 @@ public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
CharStream codeCharStream = null;
|
||||
try {
|
||||
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.java"));
|
||||
codeCharStream = CharStreams.fromPath(Paths.get("./Example.java"));
|
||||
parsefile(codeCharStream);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
|
@ -17,7 +17,7 @@ public class ClassNode extends ASTNode{
|
||||
|
||||
public void ensureConstructor(){
|
||||
if(!hasConstructor) {
|
||||
ConstructorNode constructor = new ConstructorNode("public", name);
|
||||
ConstructorNode constructor = new ConstructorNode(new TypeNode("public"), name);
|
||||
members.add(0,constructor);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package ast;
|
||||
|
||||
public class ConstructorNode extends MethodNode{
|
||||
public ConstructorNode(String visibility, String name) {
|
||||
public ConstructorNode(TypeNode visibility, String name) {
|
||||
super(visibility, name);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package ast;
|
||||
|
||||
public class FieldNode extends MemberNode {
|
||||
TypeNode type;
|
||||
String name;
|
||||
public TypeNode type;
|
||||
public String name;
|
||||
|
||||
public FieldNode(TypeNode type, String name){
|
||||
this.type = type;
|
||||
|
@ -9,9 +9,18 @@ public class MethodNode extends MemberNode{
|
||||
|
||||
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.name = name;
|
||||
this.parameters = parameters;
|
||||
this.statements = statements;
|
||||
}
|
||||
|
||||
public MethodNode(TypeNode visibility, String name){
|
||||
this.visibility = visibility;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,10 @@ package ast;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ParameterListNode {
|
||||
public class ParameterListNode extends ASTNode {
|
||||
List<ParameterNode> parameters = new ArrayList<>();
|
||||
|
||||
public ParameterListNode(){
|
||||
parameters
|
||||
public ParameterListNode(List<ParameterNode> parameters){
|
||||
this.parameters = parameters;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
public class ParameterNode extends ASTNode {
|
||||
public TypeNode type;
|
||||
public String identifier;
|
||||
package ast;
|
||||
|
||||
public ParameterNode(TypeNode type, String identifier) {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ProgramNode extends ASTNode {
|
||||
public List<ClassNode> classes = new ArrayList<>();
|
||||
|
||||
public void addClass(ClassNode classNode) {
|
||||
classes.add(classNode);
|
||||
}
|
||||
}
|
@ -1,43 +1,14 @@
|
||||
package bytecode;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import ast.ClassNode;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import ast.ProgramNode;
|
||||
import ast.ClassNode;
|
||||
|
||||
public class ByteCodeGenerator {
|
||||
|
||||
public void generateByteCode(ProgramNode ast) {
|
||||
for (ClassNode classDeclarationNode : ast.classes) {
|
||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.name, null,
|
||||
"java/lang/Object", null);
|
||||
|
||||
FieldCodeGen fieldCodeGen = new FieldCodeGen();
|
||||
fieldCodeGen.generateFieldCode(classWriter);
|
||||
|
||||
MethodCodeGen methodCodeGen = new MethodCodeGen();
|
||||
methodCodeGen.generateMethodCode(classWriter);
|
||||
|
||||
classWriter.visitEnd();
|
||||
printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.name);
|
||||
|
||||
classWriter.visitEnd();
|
||||
}
|
||||
}
|
||||
|
||||
private void printIntoClassFile(byte[] byteCode, String name) {
|
||||
String filePath = "./classFileOutput/" + name + ".class";
|
||||
|
||||
try {
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||
fileOutputStream.write(byteCode);
|
||||
fileOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
ClassCodeGen classCodeGen = new ClassCodeGen();
|
||||
classCodeGen.generateClassCode(classDeclarationNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,23 +39,17 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
|
||||
TypeNode returnType = (TypeNode) visit(ctx.type());
|
||||
String methodName = ctx.IDENTIFIER().getText();
|
||||
ParameterListNode = (ParameterListNode) visit(ctx.parameterList());
|
||||
List<ParameterNode> parameters = 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));
|
||||
}
|
||||
|
||||
ParameterListNode parameterListNode = (ParameterListNode) visit(ctx.parameterList());
|
||||
List<StatementNode> statements = new ArrayList<>();
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTNode visitType(SimpleJavaParser.TypeContext ctx) {
|
||||
return new TypeNode(ctx.getText());
|
||||
@ -78,7 +72,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user