Generate byteode for empty class without typecheck
This commit is contained in:
parent
ef94d309cf
commit
da68aacaa0
12
pom.xml
12
pom.xml
@ -22,6 +22,18 @@
|
|||||||
<version>5.9.3</version> <!-- Change the version as needed -->
|
<version>5.9.3</version> <!-- Change the version as needed -->
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
33
src/main/java/bytecode/ByteCodeGenerator.java
Normal file
33
src/main/java/bytecode/ByteCodeGenerator.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,5 +23,8 @@ public class Main {
|
|||||||
// Optionally print or process the AST
|
// Optionally print or process the AST
|
||||||
System.out.println("Parsed " + ast.classes.size() + " classes.");
|
System.out.println("Parsed " + ast.classes.size() + " classes.");
|
||||||
System.out.println(ast.classes.get(0).identifier);
|
System.out.println(ast.classes.get(0).identifier);
|
||||||
|
|
||||||
|
//ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||||
|
//byteCodeGenerator.generateByteCode(ast);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user