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

View File

@ -1,43 +1,14 @@
package bytecode; package bytecode;
import java.io.FileOutputStream;
import java.io.IOException;
import ast.ClassNode; import ast.ClassNode;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import ast.ProgramNode; import ast.ProgramNode;
public class ByteCodeGenerator { public class ByteCodeGenerator {
public void generateByteCode(ProgramNode ast) { public void generateByteCode(ProgramNode ast) {
for (ClassNode classDeclarationNode : ast.classes) { for (ClassNode classNode : 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(classNode);
"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,19 +1,4 @@
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.junit.jupiter.api.Test; 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; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -22,14 +7,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/ */
public class MainTest { public class MainTest {
@Test @Test
void testEmptyClass() { void testAdd() {
CharStream codeCharStream = null; //Calculator calculator = new Calculator();
try { //int result = calculator.add(2, 3);
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.java")); //assertEquals(5, result, "2 + 3 should equal 5");
Main.parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
}
} }
} }