added Expression analysis

This commit is contained in:
Bruder John 2024-05-09 14:21:07 +02:00
parent a86bede5a7
commit 43cafcd01f
3 changed files with 46 additions and 47 deletions

View File

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

View File

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

View File

@ -8,51 +8,56 @@ import java.util.List;
public class SemanticAnalyzer {
List<Identifier> 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<MemberNode> memberNodes = classNode.members;
for (MemberNode member : memberNodes) {
analyze(member);
}
List<MemberNode> 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<StatementNode> 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<ClassNode> classes = programNode.classes;
for (ClassNode classNode: classes) {
analyze(classNode);
}
}
} else if (node instanceof ProgramNode) {
ProgramNode programNode = (ProgramNode) node;
List<ClassNode> classes = programNode.classes;
for (ClassNode classNode : classes) {
analyze(classNode);
}
}
public boolean identifierAlreadyUsed(Identifier identifier){
if(usedIdentifier.contains(identifier)){
return true;
} else {
return false;
}
}
}
}