Compare commits

..

2 Commits

Author SHA1 Message Date
Lucas
d7a8500109 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	src/main/java/Main.java
#	src/main/java/bytecode/ByteCodeGenerator.java
2024-05-08 13:14:16 +02:00
Lucas
d014cb28c7 Structure and Fixes 2024-05-08 13:10:45 +02:00
3 changed files with 58 additions and 10 deletions

View File

@ -1,6 +1,3 @@
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
@ -8,6 +5,9 @@ import org.antlr.v4.runtime.tree.ParseTree;
import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser;
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import java.io.IOException;
import java.nio.file.Paths;

View File

@ -1,14 +1,43 @@
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;
public class ByteCodeGenerator {
public void generateByteCode(ProgramNode ast) {
for (ClassNode classNode : ast.classes) {
ClassCodeGen classCodeGen = new ClassCodeGen();
classCodeGen.generateClassCode(classNode);
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();
}
}
}

View File

@ -1,4 +1,19 @@
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.junit.jupiter.api.Test;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser;
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import java.io.IOException;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -7,10 +22,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/
public class MainTest {
@Test
void testAdd() {
//Calculator calculator = new Calculator();
//int result = calculator.add(2, 3);
//assertEquals(5, result, "2 + 3 should equal 5");
void testEmptyClass() {
CharStream codeCharStream = null;
try {
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.java"));
Main.parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
}
}
}