added Expression analysis
This commit is contained in:
parent
a86bede5a7
commit
43cafcd01f
@ -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;
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user