Merge remote-tracking branch 'origin/main'

This commit is contained in:
i22011 2024-05-08 09:53:22 +02:00
commit 3b72d09df5
5 changed files with 52 additions and 2 deletions

4
.gitignore vendored
View File

@ -74,4 +74,6 @@ fabric.properties
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
.idea/caches/build_file_checksums.ser
/target

2
.idea/misc.xml generated
View File

@ -40,7 +40,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

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);
}
}