From a98cb26083e85bb09e44265dc7f7fe05173418ae Mon Sep 17 00:00:00 2001 From: Bruder John Date: Wed, 8 May 2024 13:33:20 +0200 Subject: [PATCH] first change --- src/main/java/Example.java | 5 +++- src/main/java/Main.java | 12 ++++++--- src/main/java/bytecode/ByteCodeGenerator.java | 7 ++--- src/main/java/parser/ASTBuilder.java | 8 ------ src/main/java/semantic/SemanticAnalyzer.java | 27 ++++++++++++------- src/main/java/typechecker/TypeChecker.java | 12 --------- src/main/java/typechecker/Typer.java | 9 +++++++ 7 files changed, 42 insertions(+), 38 deletions(-) delete mode 100644 src/main/java/typechecker/TypeChecker.java create mode 100644 src/main/java/typechecker/Typer.java diff --git a/src/main/java/Example.java b/src/main/java/Example.java index 6f34d6e..6657954 100644 --- a/src/main/java/Example.java +++ b/src/main/java/Example.java @@ -1,3 +1,6 @@ -public class Example { +class Example { + + int test; + } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index ab4a4fe..bc82379 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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); } diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index b00f429..4890004 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -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(); } diff --git a/src/main/java/parser/ASTBuilder.java b/src/main/java/parser/ASTBuilder.java index fe0bdeb..df2d1b3 100644 --- a/src/main/java/parser/ASTBuilder.java +++ b/src/main/java/parser/ASTBuilder.java @@ -25,12 +25,4 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { 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); - } - - } diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index a298051..e486d16 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -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 memberNodes = classNode.members; + for (MemberNode member: memberNodes) { + analyze(member); + } + + + }else if (node instanceof ConstructorNode) { + + }else if (node instanceof ProgramNode) { ProgramNode programNode = (ProgramNode) node; - List classes = programNode.classes; - for (ClassDeclarationNode classDeklarationNode: classes) { - analyze(classDeklarationNode); + List classes = programNode.classes; + for (ClassNode classNode: classes) { + analyze(classNode); } } diff --git a/src/main/java/typechecker/TypeChecker.java b/src/main/java/typechecker/TypeChecker.java deleted file mode 100644 index 703b988..0000000 --- a/src/main/java/typechecker/TypeChecker.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/typechecker/Typer.java b/src/main/java/typechecker/Typer.java new file mode 100644 index 0000000..3f8872a --- /dev/null +++ b/src/main/java/typechecker/Typer.java @@ -0,0 +1,9 @@ +package typechecker; + +public class Typer { + + public static void Typeify(){ + + } + +}