Merge remote-tracking branch 'origin/main'

# Conflicts:
#	src/main/java/parser/Main.java
This commit is contained in:
Purplumbi 2024-05-08 11:44:13 +02:00
commit 402249e1ac
13 changed files with 123 additions and 42 deletions

2
.idea/misc.xml generated
View File

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

BIN
Tester.class Normal file

Binary file not shown.

5
Tester.java Normal file
View File

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

Binary file not shown.

View File

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

47
src/main/java/Main.java Normal file
View File

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

View File

@ -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,
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);
cw.visitEnd();
printIntoClassFile(cw.toByteArray(), classDeclarationNode.identifier);
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);

View File

@ -0,0 +1,10 @@
package bytecode;
import org.objectweb.asm.ClassWriter;
public class FieldCodeGen {
public void generateFieldCode(ClassWriter classWriter) {
}
}

View File

@ -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,
"<init>",
"()V",
null,
null);
}
}

View File

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

View File

@ -1 +0,0 @@
class Test { }

View File

@ -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() {

View File

@ -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