first change

This commit is contained in:
Bruder John 2024-05-08 13:33:20 +02:00
parent 2539b7cff1
commit a98cb26083
7 changed files with 42 additions and 38 deletions

View File

@ -1,3 +1,6 @@
public class Example {
class Example {
int test;
}

View File

@ -1,13 +1,14 @@
import ast.ClassNode;
import ast.ProgramNode;
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 semantic.SemanticAnalyzer;
import java.io.IOException;
import java.nio.file.Paths;
@ -37,10 +38,13 @@ public class Main {
// 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);
for (ClassNode classNode : ast.classes) {
System.out.println(classNode.name);
}
SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
semanticAnalyzer.analyze(ast);
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
byteCodeGenerator.generateByteCode(ast);
}

View File

@ -3,6 +3,7 @@ package bytecode;
import java.io.FileOutputStream;
import java.io.IOException;
import ast.ClassNode;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import ast.ProgramNode;
@ -10,9 +11,9 @@ import ast.ProgramNode;
public class ByteCodeGenerator {
public void generateByteCode(ProgramNode ast) {
for (ClassDeclarationNode classDeclarationNode : ast.classes) {
for (ClassNode classDeclarationNode : ast.classes) {
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null,
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.name, null,
"java/lang/Object", null);
FieldCodeGen fieldCodeGen = new FieldCodeGen();
@ -22,7 +23,7 @@ public class ByteCodeGenerator {
methodCodeGen.generateMethodCode(classWriter);
classWriter.visitEnd();
printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.identifier);
printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.name);
classWriter.visitEnd();
}

View File

@ -25,12 +25,4 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return classNode;
}
@Override
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
TypeNode type = (TypeNode) visit(ctx.type());
String identifier = ctx.IDENTIFIER().getText();
return new FieldNode(type, identifier);
}
}

View File

@ -1,8 +1,7 @@
package semantic;
import parser.ASTNode;
import parser.ClassDeclarationNode;
import parser.ProgramNode;
import ast.*;
import java.util.List;
@ -10,18 +9,26 @@ public class SemanticAnalyzer {
public void analyze(ASTNode node) {
if (node == null) return;
if (node instanceof ClassDeclarationNode) {
if (node instanceof ClassNode) {
ClassDeclarationNode classDeclarationNode = (ClassDeclarationNode) node;
if(classDeclarationNode.identifier == null){
ClassNode classNode = (ClassNode) node;
if(classNode.name == null){
System.out.println("Klasse besitzt keinen Identifier");
}
} else if (node instanceof ProgramNode) {
List<MemberNode> memberNodes = classNode.members;
for (MemberNode member: memberNodes) {
analyze(member);
}
}else if (node instanceof ConstructorNode) {
}else if (node instanceof ProgramNode) {
ProgramNode programNode = (ProgramNode) node;
List<ClassDeclarationNode> classes = programNode.classes;
for (ClassDeclarationNode classDeklarationNode: classes) {
analyze(classDeklarationNode);
List<ClassNode> classes = programNode.classes;
for (ClassNode classNode: classes) {
analyze(classNode);
}
}

View File

@ -1,12 +0,0 @@
package typechecker;
import parser.generated.SimpleJavaParser;
public class TypeChecker {
public Void visitYourExpression(SimpleJavaParser.ClassDeclarationContext ctx) {
// Hier kannst du die Art des Ausdrucks überprüfen und den entsprechenden Typ festlegen
// Beispiel: Wenn es sich um eine Zuweisung handelt, überprüfe den Typ des Ausdrucks auf der rechten Seite und überprüfe, ob er mit dem Typ der Variablen auf der linken Seite kompatibel ist.
return null;
}
}

View File

@ -0,0 +1,9 @@
package typechecker;
public class Typer {
public static void Typeify(){
}
}