diff --git a/.idea/misc.xml b/.idea/misc.xml index 2d71b1f..7cf0be7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -40,7 +40,7 @@ - + \ No newline at end of file diff --git a/Tester.class b/Tester.class new file mode 100644 index 0000000..69949dc Binary files /dev/null and b/Tester.class differ diff --git a/Tester.java b/Tester.java new file mode 100644 index 0000000..92c20ee --- /dev/null +++ b/Tester.java @@ -0,0 +1,5 @@ +public class Tester { + public static void main(String[] args) { + new Example(); + } +} diff --git a/classFileOutput/Example.class b/classFileOutput/Example.class new file mode 100644 index 0000000..e44ef84 Binary files /dev/null and b/classFileOutput/Example.class differ diff --git a/src/main/java/Example.java b/src/main/java/Example.java new file mode 100644 index 0000000..6f34d6e --- /dev/null +++ b/src/main/java/Example.java @@ -0,0 +1,3 @@ +public class Example { +} + diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..ab4a4fe --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,47 @@ +import bytecode.ByteCodeGenerator; +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.ClassDeclarationNode; +import parser.ProgramNode; +import parser.generated.SimpleJavaLexer; +import parser.generated.SimpleJavaParser; + +import java.io.IOException; +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/java/Example.java")); + parsefile(codeCharStream); + } catch (IOException e) { + System.err.println("Error reading the file: " + e.getMessage()); + } + } + + + static void parsefile(CharStream codeCharStream){ + // CharStream codeCharStream = CharStreams.fromString("class Example { } class Example2 { }"); + SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); + CommonTokenStream tokens = new CommonTokenStream(lexer); + SimpleJavaParser parser = new SimpleJavaParser(tokens); + + ParseTree tree = parser.program(); // parse the input + + ASTBuilder builder = new ASTBuilder(); + ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST + + // Optionally print or process the AST + System.out.println("Parsed " + ast.classes.size() + " classes with identifiers/names:"); + for (ClassDeclarationNode classNode : ast.classes) { + System.out.println(classNode.identifier); + } + + ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(); + byteCodeGenerator.generateByteCode(ast); + } +} \ No newline at end of file diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index 876ec63..b00f429 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -2,6 +2,7 @@ package bytecode; import java.io.FileOutputStream; import java.io.IOException; + import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; import ast.ProgramNode; @@ -10,16 +11,25 @@ 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); + ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); + classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null, + "java/lang/Object", null); + + FieldCodeGen fieldCodeGen = new FieldCodeGen(); + fieldCodeGen.generateFieldCode(classWriter); + + MethodCodeGen methodCodeGen = new MethodCodeGen(); + methodCodeGen.generateMethodCode(classWriter); + + classWriter.visitEnd(); + printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.identifier); + + classWriter.visitEnd(); } } private void printIntoClassFile(byte[] byteCode, String name) { - String filePath = name + ".class"; + String filePath = "./classFileOutput/" + name + ".class"; try { FileOutputStream fileOutputStream = new FileOutputStream(filePath); diff --git a/src/main/java/bytecode/FieldCodeGen.java b/src/main/java/bytecode/FieldCodeGen.java new file mode 100644 index 0000000..4cca429 --- /dev/null +++ b/src/main/java/bytecode/FieldCodeGen.java @@ -0,0 +1,10 @@ +package bytecode; + +import org.objectweb.asm.ClassWriter; + +public class FieldCodeGen { + + public void generateFieldCode(ClassWriter classWriter) { + + } +} diff --git a/src/main/java/bytecode/MethodCodeGen.java b/src/main/java/bytecode/MethodCodeGen.java new file mode 100644 index 0000000..a54fda8 --- /dev/null +++ b/src/main/java/bytecode/MethodCodeGen.java @@ -0,0 +1,16 @@ +package bytecode; + +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +public class MethodCodeGen { + public void generateMethodCode(ClassWriter classWriter) { + MethodVisitor constructor = + classWriter.visitMethod(Opcodes.ACC_PUBLIC, + "", + "()V", + null, + null); + } +} diff --git a/src/main/java/parser/Main.java b/src/main/java/parser/Main.java deleted file mode 100644 index c587771..0000000 --- a/src/main/java/parser/Main.java +++ /dev/null @@ -1,29 +0,0 @@ -package parser; - -import ast.ProgramNode; -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; - -public class Main { - public static void main(String[] args) throws Exception { - // Assuming args[0] contains the path to the input file - CharStream codeCharStream = CharStreams.fromString("class Test { }"); - SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); - CommonTokenStream tokens = new CommonTokenStream(lexer); - SimpleJavaParser parser = new SimpleJavaParser(tokens); - - ParseTree tree = parser.program(); // parse the input - - ASTBuilder builder = new ASTBuilder(); - ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST - - // 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); - } -} \ No newline at end of file diff --git a/src/main/java/parser/Test.txt b/src/main/java/parser/Test.txt deleted file mode 100644 index 5a0c977..0000000 --- a/src/main/java/parser/Test.txt +++ /dev/null @@ -1 +0,0 @@ -class Test { } \ No newline at end of file diff --git a/src/main/test/java/SimpleJavaFeatureTest.java b/src/main/test/java/SimpleJavaFeatureTest.java index 4854fb7..b71f63b 100644 --- a/src/main/test/java/SimpleJavaFeatureTest.java +++ b/src/main/test/java/SimpleJavaFeatureTest.java @@ -9,6 +9,11 @@ public class SimpleJavaFeatureTest { this.b = b; this.c = c; } + private class InnerClass { + void innerMethod() { + System.out.println("Inner class method"); + } + } // Methode zur Demonstration von Kontrollstrukturen void controlStructures() { diff --git a/src/main/test/java/TestSpecs.md b/src/main/test/java/TestSpecs.md index 3626c74..5a0eb55 100644 --- a/src/main/test/java/TestSpecs.md +++ b/src/main/test/java/TestSpecs.md @@ -2,7 +2,7 @@ ## Scanner Input ### Beispiel 1: Empty Class - String empty class = "class name {}"; + String empty class = "public class Name {}"; ### Beispiel 2: Filled Class @@ -17,7 +17,7 @@ ## Scanner Output ### Beispiel 1: Empty Class - [TokClass,TokIdentifier "name",TokLeftBrace,TokRightBrace] + [TokPublic,TokClass,TokIdentifier "Name",TokLeftBrace,TokRightBrace] ### Beispiel 2: Filled Class @@ -29,9 +29,10 @@ [TokRightBrace] # Parser -## Parser Input (Scanner Output) +## Parser Input +(Scanner Output) -## Parser Output -> AST +## Parser Output (AST) ### Beispiel 1: Empty Class @@ -41,7 +42,8 @@ # Semantische Analyse / Typcheck -## Typcheck Input (Parser Output) +## Typcheck Input +(Parser Output = AST) ## Typcheck Output @@ -53,5 +55,18 @@ # Bytecodegenerierung +## Bytecodegenerierung Input +(Typcheck Output = vom Typcheck eventuell manipulierter AST) +## Bytecodegenerierung Output + +### Beispiel 1: Empty Class +Compiled Classfile + + public class Example { + } + + + +### Beispiel 2: Filled Class \ No newline at end of file