Generate byteode for empty class without typecheck

This commit is contained in:
i22007 2024-05-07 19:13:40 +02:00
parent ef94d309cf
commit da68aacaa0
3 changed files with 48 additions and 0 deletions

12
pom.xml
View File

@ -22,6 +22,18 @@
<version>5.9.3</version> <!-- Change the version as needed -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.13.1</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.7</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,33 @@
package bytecode;
import java.io.FileOutputStream;
import java.io.IOException;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import parser.ClassDeclarationNode;
import parser.ProgramNode;
public class ByteCodeGenerator {
public void generateByteCode(ProgramNode ast) {
for (ClassDeclarationNode classDeclarationNode : ast.classes) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null,
"java/lang/Object", null);
cw.visitEnd();
printIntoClassFile(cw.toByteArray(), classDeclarationNode.identifier);
}
}
private void printIntoClassFile(byte[] byteCode, String name) {
String filePath = name + ".class";
try {
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write(byteCode);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -23,5 +23,8 @@ public class Main {
// Optionally print or process the AST
System.out.println("Parsed " + ast.classes.size() + " classes.");
System.out.println(ast.classes.get(0).identifier);
//ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
//byteCodeGenerator.generateByteCode(ast);
}
}