johns-branch #4
@ -1,6 +1,6 @@
|
||||
public class Example {
|
||||
|
||||
public int test;
|
||||
public int Example;
|
||||
|
||||
public Example(int conInput) {
|
||||
|
||||
@ -8,7 +8,9 @@ public class Example {
|
||||
|
||||
public static int test(char b){
|
||||
|
||||
test = 3;
|
||||
char Example;
|
||||
boolean Example;
|
||||
int c;
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package ast;
|
||||
|
||||
public class IdentifierNode {
|
||||
public class IdentifierNode extends ASTNode{
|
||||
|
||||
private String name;
|
||||
|
||||
|
13
src/main/java/ast/LiteralNode.java
Normal file
13
src/main/java/ast/LiteralNode.java
Normal file
@ -0,0 +1,13 @@
|
||||
package ast;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
|
||||
public class LiteralNode extends ExpressionNode {
|
||||
|
||||
int value;
|
||||
|
||||
public LiteralNode(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
@ -1,13 +1,14 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.IdentifierNode;
|
||||
import ast.expression.ExpressionNode;
|
||||
|
||||
public class AssignmentStatementNode extends StatementNode {
|
||||
public String identifier;
|
||||
public IdentifierNode identifier;
|
||||
public ExpressionNode expression;
|
||||
|
||||
public AssignmentStatementNode(String identifier, ExpressionNode expression) {
|
||||
this.identifier = identifier;
|
||||
this.identifier = new IdentifierNode(identifier);
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,16 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.IdentifierNode;
|
||||
import ast.type.TypeNode;
|
||||
import ast.expression.ExpressionNode;
|
||||
|
||||
public class VariableDeclarationStatementNode extends StatementNode {
|
||||
public TypeNode type;
|
||||
public String identifier;
|
||||
public IdentifierNode identifier;
|
||||
public ExpressionNode expression;
|
||||
public VariableDeclarationStatementNode(TypeNode type, String identifier, ExpressionNode expression) {
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
this.identifier = new IdentifierNode(identifier);
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
Binary file not shown.
@ -21,6 +21,7 @@ import parser.generated.SimpleJavaParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import parser.generated.SimpleJavaParser.LiteralContext;
|
||||
|
||||
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
@Override
|
||||
@ -196,4 +197,16 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
|
||||
return null; // Return null or throw an exception if no valid expression found
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
|
||||
LiteralContext literalContext = (LiteralContext) ctx;
|
||||
try {
|
||||
int intValue = Integer.parseInt(literalContext.getText());
|
||||
return new LiteralNode(intValue);
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
return null; // Return null or throw an exception if no valid expression found
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,57 +2,50 @@ package semantic;
|
||||
|
||||
|
||||
import ast.*;
|
||||
import ast.expression.ExpressionNode;
|
||||
import ast.member.ConstructorNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.member.MemberNode;
|
||||
|
||||
import ast.member.MethodNode;
|
||||
import ast.statement.AssignmentStatementNode;
|
||||
import ast.statement.StatementNode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SemanticAnalyzer {
|
||||
|
||||
public void analyze(ASTNode node) {
|
||||
public String analyze(ASTNode node) {
|
||||
if (node == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (node instanceof ClassNode) {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
} else if (node instanceof AssignmentNode) {
|
||||
AssignmentNode assignmentNode = (AssignmentNode) node;
|
||||
System.out.println("Assignment ");
|
||||
} else if (node instanceof AssignmentStatementNode) {
|
||||
AssignmentStatementNode assignmentStatementNode = (AssignmentStatementNode) node;
|
||||
|
||||
IdentifierNode identifierNode = assignmentStatementNode.identifier;
|
||||
|
||||
if(analyze(assignmentStatementNode.expression).equals("int")){
|
||||
System.out.println("INTEGER");
|
||||
}
|
||||
|
||||
analyze(assignmentNode.expression);
|
||||
|
||||
} else if (node instanceof LiteralNode) {
|
||||
return "int";
|
||||
} 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 ProgramNode) {
|
||||
ProgramNode programNode = (ProgramNode) node;
|
||||
List<ClassNode> classes = programNode.classes;
|
||||
@ -61,6 +54,7 @@ public class SemanticAnalyzer {
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user