From 36f3d2755a589b9b23a1a041d170edbb5954616a Mon Sep 17 00:00:00 2001 From: Boolean-True Date: Tue, 14 May 2024 15:01:22 +0200 Subject: [PATCH] Finish Compiler for Multiple Classes --- src/main/java/de/maishai/Compiler.java | 35 +++++++++++-------- .../typedast/typedclass/TypedProgram.java | 14 ++------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/main/java/de/maishai/Compiler.java b/src/main/java/de/maishai/Compiler.java index 7301c38..9b1bb68 100644 --- a/src/main/java/de/maishai/Compiler.java +++ b/src/main/java/de/maishai/Compiler.java @@ -47,22 +47,25 @@ public class Compiler { } public static TypedProgram generateTypedASTFromAst(Program ast) { - TypedClass typedAST = new TypedClass(); - TypedProgram typedProgram = new TypedProgram(ast); - return typedProgram; + TypedProgram typedAST = new TypedProgram(ast); + return typedAST; } - public static byte[] generateByteCodeArrayFromTypedAst(TypedProgram typedAST) { + public static byte[] generateByteCodeArrayFromTypedAst(TypedClass typedAST) { return typedAST.codeGen(); } - public static byte[] generateByteCodeArray(List fromSources) { + public static List generateByteCodeArray(List fromSources) { Program ast = generateAST(fromSources); TypedProgram typedAST = generateTypedASTFromAst(ast); - return generateByteCodeArrayFromTypedAst(typedAST); + List byteCode = new ArrayList<>(); + for (TypedClass c : typedAST.getTypedClasses()) { + byteCode.add(generateByteCodeArrayFromTypedAst(c)); + } + return byteCode; } - public static byte[] generateByteCodeArrayFromFile(List sourcePaths) { + public static List generateByteCodeArrayFromFile(List sourcePaths) { List classes = new ArrayList<>(); for (String sourcePath : sourcePaths) { ANTLRInputStream antlrInputStream; @@ -81,17 +84,21 @@ public class Compiler { } Program program = new Program(classes); TypedProgram typedAST = generateTypedASTFromAst(program); - //TODO: Für jede Klasse einzeln Bytecode generieren - return generateByteCodeArrayFromTypedAst(typedAST); + List byteCode = new ArrayList<>(); + for (TypedClass c : typedAST.getTypedClasses()) { + byteCode.add(generateByteCodeArrayFromTypedAst(c)); + } + return byteCode; } - public static void generateByteCodeFileFromFile(List sourcePath, String classname) { - byte[] bytes = generateByteCodeArrayFromFile(sourcePath); - //TODO: Für jede Klasse einzeln Bytecode schreiben - CodeGenUtils.writeClassfile(bytes, classname); + public static void generateByteCodeFileFromFile(List sourcePath, List classname) { + List bytes = generateByteCodeArrayFromFile(sourcePath); + for (int i = 0; i < bytes.size(); i++) { + CodeGenUtils.writeClassfile(bytes.get(i), classname.get(i)); + } } public static void main(String[] args) { - generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassCanBeTyped.java"), "ClassCanBeTyped"); + generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassCanBeTyped.java"), List.of("ClassCanBeTyped")); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java index 3c636bd..c52567a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java @@ -2,12 +2,14 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Program; import de.maishai.typedast.ClassContext; +import lombok.Getter; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; import java.util.ArrayList; import java.util.List; +@Getter public class TypedProgram { private List typedClasses = new ArrayList<>(); @@ -16,18 +18,8 @@ public class TypedProgram { startConversion(program); } public void startConversion(Program program){ - + //TODO: implement program.classes().toString(); - } - - public byte[] codeGen() { - //TODO: implement this quickly - - return null; - } - - - }