Merge branch 'main' into johns-branch

This commit is contained in:
Bruder John 2024-05-08 23:46:28 +02:00
commit 73b9eeddb0
15 changed files with 73 additions and 21 deletions

Binary file not shown.

View File

@ -0,0 +1,3 @@
public class Example {
}

View File

@ -18,9 +18,11 @@ import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws Exception {
CharStream codeCharStream = null;
try {
codeCharStream = CharStreams.fromPath(Paths.get("src/main/resources/Example.txt"));
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt"));
parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
@ -29,7 +31,7 @@ public class Main {
static void parsefile(CharStream codeCharStream){
// CharStream codeCharStream = CharStreams.fromString("class Example { } class Example2 { }");
// CharStream codeCharStream = CharStreams.fromString("class javaFileInput.Example { } class Example2 { }");
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SimpleJavaParser parser = new SimpleJavaParser(tokens);

View File

@ -1,6 +1,10 @@
package bytecode;
import ast.ClassNode;
import ast.FieldNode;
import ast.MemberNode;
import ast.MethodNode;
import java.io.File;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
@ -10,14 +14,19 @@ import java.io.IOException;
public class ClassCodeGen {
public void generateClassCode(ClassNode classNode) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.identifier.getName(), null,
Mapper mapper = new Mapper();
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.name, null,
"java/lang/Object", null);
FieldCodeGen fieldCodeGen = new FieldCodeGen();
fieldCodeGen.generateFieldCode(classWriter);
MethodCodeGen methodCodeGen = new MethodCodeGen();
methodCodeGen.generateMethodCode(classWriter);
for (MemberNode memberNode : classNode.members) {
if (memberNode instanceof FieldNode) {
FieldCodeGen fieldCodeGen = new FieldCodeGen();
fieldCodeGen.generateFieldCode(classWriter, (FieldNode) memberNode);
} else if (memberNode instanceof MethodNode) {
MethodCodeGen methodCodeGen = new MethodCodeGen();
methodCodeGen.generateMethodCode(classWriter, (MethodNode) memberNode);
}
}
classWriter.visitEnd();
printIntoClassFile(classWriter.toByteArray(), classNode.identifier.getName());
@ -26,8 +35,13 @@ public class ClassCodeGen {
}
private void printIntoClassFile(byte[] byteCode, String name) {
String filePath = "./classFileOutput/" + name + ".class";
String directoryPath = "src/main/java/classFileOutput";
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
String filePath = directoryPath + "/" + name + ".class";
try {
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write(byteCode);

View File

@ -1,10 +1,14 @@
package bytecode;
import ast.FieldNode;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Opcodes;
public class FieldCodeGen {
public void generateFieldCode(ClassWriter classWriter) {
public void generateFieldCode(ClassWriter classWriter, FieldNode fieldNode) {
Mapper mapper = new Mapper();
FieldVisitor fieldVisitor = classWriter.visitField(mapper.mapAccesTypeToOpcode(fieldNode.accessTypeNode), fieldNode.name, "", null, null);
}
}

View File

@ -0,0 +1,17 @@
package bytecode;
import ast.AccessTypeNode;
import ast.EnumAccessTypeNode;
import org.objectweb.asm.Opcodes;
public class Mapper {
public int mapAccesTypeToOpcode(AccessTypeNode type) {
switch (type.enumAccessTypeNode) {
case EnumAccessTypeNode.PUBLIC:
return Opcodes.ACC_PUBLIC;
case EnumAccessTypeNode.PRIVATE:
return Opcodes.ACC_PRIVATE;
}
return 0;
}
}

View File

@ -1,13 +1,15 @@
package bytecode;
import ast.MethodNode;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
public class MethodCodeGen {
public void generateMethodCode(ClassWriter classWriter) {
public void generateMethodCode(ClassWriter classWriter, MethodNode methodNode) {
Mapper mapper = new Mapper();
MethodVisitor constructor =
classWriter.visitMethod(Opcodes.ACC_PUBLIC,
classWriter.visitMethod(mapper.mapAccesTypeToOpcode(methodNode.visibility),
"<init>",
"()V",
null,

View File

@ -1,10 +1,10 @@
public class SimpleJavaFeatureTest {
public class AllFeaturesClassExample {
int a;
boolean b;
char c;
// Konstruktor
SimpleJavaFeatureTest(int a, boolean b, char c) {
AllFeaturesClassExample(int a, boolean b, char c) {
this.a = a;
this.b = b;
this.c = c;
@ -62,7 +62,7 @@ public class SimpleJavaFeatureTest {
}
public static void main(String[] args) {
SimpleJavaFeatureTest obj = new SimpleJavaFeatureTest(12, true, 'a');
AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
obj.controlStructures();
obj.logicalOperations();
}

View File

@ -0,0 +1,4 @@
public class EmptyClassExample {
private class Inner {
}
}

View File

@ -21,7 +21,7 @@ public class MainTest {
void testEmptyClass() {
CharStream codeCharStream = null;
try {
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.java"));
codeCharStream = CharStreams.fromPath(Paths.get("src/main/test/java/EmptyClassExample.java"));
Main.parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());

View File

@ -0,0 +1,6 @@
public class MoreFeaturesClassExample {
int hallo;
private class Inner {
int hallo2;
}
}

View File

@ -7,7 +7,7 @@
### Beispiel 2: Filled Class
String filled class =
"class Example {" +
"class javaFileInput.Example {" +
"if (x < 5) {" +
"for (int i = 0; i < 10; i++) {" +
"while (true) {" +
@ -21,7 +21,7 @@
### Beispiel 2: Filled Class
[TokClass,TokIdentifier "Example",TokLeftBrace]
[TokClass,TokIdentifier "javaFileInput.Example",TokLeftBrace]
[TokIf,TokLeftParen,TokIdentifier "x",TokLessThan,TokNumber 5,TokRightParen,TokLeftBrace]
[TokFor,TokLeftParen,TokIdentifier "int",TokIdentifier "i",TokAssign,TokNumber 0,TokSemicolon,TokIdentifier "i",TokLessThan,TokNumber 10,TokSemicolon,TokIdentifier "i",TokPlus,TokPlus,TokRightParen,TokLeftBrace]
[TokWhile,TokLeftParen,TokIdentifier "true",TokRightParen,TokLeftBrace]
@ -63,7 +63,7 @@
### Beispiel 1: Empty Class
Compiled Classfile
public class Example {
public class javaFileInput.Example {
}

View File

@ -1,5 +1,5 @@
public class Tester {
public static void main(String[] args) {
new Example();
new EmptyClassExample();
}
}