From 43cafcd01f757c7eb033375906368f4271a1bc64 Mon Sep 17 00:00:00 2001 From: Bruder John Date: Thu, 9 May 2024 14:21:07 +0200 Subject: [PATCH] added Expression analysis --- src/main/java/CompilerInput.txt | 8 +- src/main/java/ast/ExpressionNode.java | 4 +- src/main/java/semantic/SemanticAnalyzer.java | 81 +++++++++++--------- 3 files changed, 46 insertions(+), 47 deletions(-) diff --git a/src/main/java/CompilerInput.txt b/src/main/java/CompilerInput.txt index a7d717c..578b848 100644 --- a/src/main/java/CompilerInput.txt +++ b/src/main/java/CompilerInput.txt @@ -2,16 +2,10 @@ public class Example { public int test; - public static int test(char b){ + public static int TestMethod(char b){ test = 3; } -} - -public class Test { - - public char test; - } \ No newline at end of file diff --git a/src/main/java/ast/ExpressionNode.java b/src/main/java/ast/ExpressionNode.java index 9e6f2a8..689e1c3 100644 --- a/src/main/java/ast/ExpressionNode.java +++ b/src/main/java/ast/ExpressionNode.java @@ -2,10 +2,10 @@ package ast; public class ExpressionNode extends ASTNode { - int Value; + public int value; public ExpressionNode(int value) { - this.Value = value; + this.value = value; } } diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 315a51e..48f316e 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -8,51 +8,56 @@ import java.util.List; public class SemanticAnalyzer { - List usedIdentifier = new ArrayList<>(); + public void analyze(ASTNode node) { + if (node == null) { + return; + } - public void analyze(ASTNode node) { - if (node == null) return; + if (node instanceof ClassNode) { - if (node instanceof ClassNode) { + ClassNode classNode = (ClassNode) node; + if (classNode.identifier == null) { + System.out.println("Klasse besitzt keinen Identifier"); + } - ClassNode classNode = (ClassNode) node; - if(classNode.identifier == null){ - System.out.println("Klasse besitzt keinen Identifier"); - } + List memberNodes = classNode.members; + for (MemberNode member : memberNodes) { + analyze(member); + } - List memberNodes = classNode.members; - for (MemberNode member: memberNodes) { - analyze(member); - } + } else if (node instanceof AssignmentNode) { + AssignmentNode assignmentNode = (AssignmentNode) node; + System.out.println("Assignment "); + + + + analyze(assignmentNode.expression); + + } else if (node instanceof MethodNode) { + MethodNode methodNode = (MethodNode) node; + System.out.println("Methode: " + methodNode.identifier.getName()); + List statementNodes = methodNode.statements; + for (StatementNode statement : statementNodes) { + analyze(statement); + } + + } else if (node instanceof ExpressionNode) { + ExpressionNode expressionNode = (ExpressionNode) node; + System.out.println("Klasse besitzt keinen Expression " + expressionNode.value); + + } else if (node instanceof FieldNode) { + + FieldNode fieldNode = (FieldNode) node; - }else if (node instanceof ConstructorNode) { - - }else if (node instanceof FieldNode) { - - FieldNode fieldNode = (FieldNode) node; - if(identifierAlreadyUsed(fieldNode.identifier)){ - //throw new RuntimeException("Error: Identifier already used"); - System.out.println("Error: Identifier already used"); - } - usedIdentifier.add(fieldNode.identifier); - - }else if (node instanceof ProgramNode) { - ProgramNode programNode = (ProgramNode) node; - List classes = programNode.classes; - for (ClassNode classNode: classes) { - analyze(classNode); - } - } - + } else if (node instanceof ProgramNode) { + ProgramNode programNode = (ProgramNode) node; + List classes = programNode.classes; + for (ClassNode classNode : classes) { + analyze(classNode); + } } - public boolean identifierAlreadyUsed(Identifier identifier){ - if(usedIdentifier.contains(identifier)){ - return true; - } else { - return false; - } - } + } } \ No newline at end of file