Merge branch 'main' into johns-branch

This commit is contained in:
Bruder John 2024-05-08 14:21:14 +02:00
commit 7249054da5
9 changed files with 37 additions and 60 deletions

View File

@ -20,7 +20,7 @@ public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
CharStream codeCharStream = null; CharStream codeCharStream = null;
try { try {
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.java")); codeCharStream = CharStreams.fromPath(Paths.get("./Example.java"));
parsefile(codeCharStream); parsefile(codeCharStream);
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage()); System.err.println("Error reading the file: " + e.getMessage());

View File

@ -17,7 +17,7 @@ public class ClassNode extends ASTNode{
public void ensureConstructor(){ public void ensureConstructor(){
if(!hasConstructor) { if(!hasConstructor) {
ConstructorNode constructor = new ConstructorNode("public", name); ConstructorNode constructor = new ConstructorNode(new TypeNode("public"), name);
members.add(0,constructor); members.add(0,constructor);
} }
} }

View File

@ -1,7 +1,7 @@
package ast; package ast;
public class ConstructorNode extends MethodNode{ public class ConstructorNode extends MethodNode{
public ConstructorNode(String visibility, String name) { public ConstructorNode(TypeNode visibility, String name) {
super(visibility, name); super(visibility, name);
} }
} }

View File

@ -1,8 +1,8 @@
package ast; package ast;
public class FieldNode extends MemberNode { public class FieldNode extends MemberNode {
TypeNode type; public TypeNode type;
String name; public String name;
public FieldNode(TypeNode type, String name){ public FieldNode(TypeNode type, String name){
this.type = type; this.type = type;

View File

@ -9,9 +9,18 @@ public class MethodNode extends MemberNode{
public ParameterListNode parameters; 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.visibility = visibility;
this.name = name; this.name = name;
this.parameters = parameters; this.parameters = parameters;
this.statements = statements;
}
public MethodNode(TypeNode visibility, String name){
this.visibility = visibility;
this.name = name;
} }
} }

View File

@ -3,10 +3,10 @@ package ast;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ParameterListNode { public class ParameterListNode extends ASTNode {
List<ParameterNode> parameters = new ArrayList<>(); List<ParameterNode> parameters = new ArrayList<>();
public ParameterListNode(){ public ParameterListNode(List<ParameterNode> parameters){
parameters this.parameters = parameters;
} }
} }

View File

@ -1,9 +1,12 @@
public class ParameterNode extends ASTNode { package ast;
public TypeNode type;
public String identifier;
public ParameterNode(TypeNode type, String identifier) { import java.util.ArrayList;
this.type = type; import java.util.List;
this.identifier = identifier;
public class ProgramNode extends ASTNode {
public List<ClassNode> classes = new ArrayList<>();
public void addClass(ClassNode classNode) {
classes.add(classNode);
} }
} }

View File

@ -1,43 +1,14 @@
package bytecode; 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.ProgramNode;
import ast.ClassNode;
public class ByteCodeGenerator { public class ByteCodeGenerator {
public void generateByteCode(ProgramNode ast) { public void generateByteCode(ProgramNode ast) {
for (ClassNode classDeclarationNode : ast.classes) { for (ClassNode classDeclarationNode : ast.classes) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); ClassCodeGen classCodeGen = new ClassCodeGen();
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.name, null, classCodeGen.generateClassCode(classDeclarationNode);
"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();
} }
} }
} }

View File

@ -39,23 +39,17 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) { public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
TypeNode returnType = (TypeNode) visit(ctx.type()); TypeNode returnType = (TypeNode) visit(ctx.type());
String methodName = ctx.IDENTIFIER().getText(); String methodName = ctx.IDENTIFIER().getText();
ParameterListNode = (ParameterListNode) visit(ctx.parameterList()); ParameterListNode parameterListNode = (ParameterListNode) visit(ctx.parameterList());
List<ParameterNode> parameters = new ArrayList<>(); List<StatementNode> statements = 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));
}
for (SimpleJavaParser.StatementContext stmtCtx : ctx.statement()) { 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; return method;
} }
@Override @Override
public ASTNode visitType(SimpleJavaParser.TypeContext ctx) { public ASTNode visitType(SimpleJavaParser.TypeContext ctx) {
return new TypeNode(ctx.getText()); return new TypeNode(ctx.getText());
@ -78,7 +72,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) { for (SimpleJavaParser.ParameterContext paramCtx : ctx.parameter()) {
parameters.add((ParameterNode) visitParameter(paramCtx)); 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 @Override