Compare commits

..

No commits in common. "d7a8500109a5d13fb0b58b8625146e9ca7964e5f" and "ce2ea07f9686513fd6ece4ae8378e21f034bf72c" have entirely different histories.

3 changed files with 10 additions and 58 deletions

View File

@ -1,3 +1,6 @@
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;
@ -5,9 +8,6 @@ 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,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;
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();
for (ClassNode classNode : ast.classes) {
ClassCodeGen classCodeGen = new ClassCodeGen();
classCodeGen.generateClassCode(classNode);
}
}
}

View File

@ -1,19 +1,4 @@
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;
@ -22,14 +7,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/
public class MainTest {
@Test
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());
}
void testAdd() {
//Calculator calculator = new Calculator();
//int result = calculator.add(2, 3);
//assertEquals(5, result, "2 + 3 should equal 5");
}
}