johns-branch #4

Merged
i22005 merged 5 commits from johns-branch into main 2024-05-14 08:00:07 +00:00
8 changed files with 52 additions and 28 deletions
Showing only changes of commit 0052b5b547 - Show all commits

View File

@ -1,6 +1,6 @@
public class Example { public class Example {
public int test; public int Example;
public Example(int conInput) { public Example(int conInput) {
@ -8,7 +8,9 @@ public class Example {
public static int test(char b){ public static int test(char b){
test = 3; char Example;
boolean Example;
int c;
} }

View File

@ -1,6 +1,6 @@
package ast; package ast;
public class IdentifierNode { public class IdentifierNode extends ASTNode{
private String name; private String name;

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

View File

@ -1,13 +1,14 @@
package ast.statement; package ast.statement;
import ast.IdentifierNode;
import ast.expression.ExpressionNode; import ast.expression.ExpressionNode;
public class AssignmentStatementNode extends StatementNode { public class AssignmentStatementNode extends StatementNode {
public String identifier; public IdentifierNode identifier;
public ExpressionNode expression; public ExpressionNode expression;
public AssignmentStatementNode(String identifier, ExpressionNode expression) { public AssignmentStatementNode(String identifier, ExpressionNode expression) {
this.identifier = identifier; this.identifier = new IdentifierNode(identifier);
this.expression = expression; this.expression = expression;
} }
} }

View File

@ -1,15 +1,16 @@
package ast.statement; package ast.statement;
import ast.IdentifierNode;
import ast.type.TypeNode; import ast.type.TypeNode;
import ast.expression.ExpressionNode; import ast.expression.ExpressionNode;
public class VariableDeclarationStatementNode extends StatementNode { public class VariableDeclarationStatementNode extends StatementNode {
public TypeNode type; public TypeNode type;
public String identifier; public IdentifierNode identifier;
public ExpressionNode expression; public ExpressionNode expression;
public VariableDeclarationStatementNode(TypeNode type, String identifier, ExpressionNode expression) { public VariableDeclarationStatementNode(TypeNode type, String identifier, ExpressionNode expression) {
this.type = type; this.type = type;
this.identifier = identifier; this.identifier = new IdentifierNode(identifier);
this.expression = expression; this.expression = expression;
} }
} }

View File

@ -21,6 +21,7 @@ import parser.generated.SimpleJavaParser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import parser.generated.SimpleJavaParser.LiteralContext;
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> { public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
@ -196,4 +197,16 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; // Return null or throw an exception if no valid expression found 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
}
} }

View File

@ -2,57 +2,50 @@ package semantic;
import ast.*; import ast.*;
import ast.expression.ExpressionNode;
import ast.member.ConstructorNode; import ast.member.ConstructorNode;
import ast.member.FieldNode; import ast.member.FieldNode;
import ast.member.MemberNode; import ast.member.MemberNode;
import ast.member.MethodNode;
import ast.statement.AssignmentStatementNode;
import ast.statement.StatementNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class SemanticAnalyzer { public class SemanticAnalyzer {
public void analyze(ASTNode node) { public String analyze(ASTNode node) {
if (node == null) { if (node == null) {
return; return null;
} }
if (node instanceof ClassNode) { if (node instanceof ClassNode) {
ClassNode classNode = (ClassNode) node; ClassNode classNode = (ClassNode) node;
if (classNode.identifier == null) {
System.out.println("Klasse besitzt keinen Identifier");
}
List<MemberNode> memberNodes = classNode.members; List<MemberNode> memberNodes = classNode.members;
for (MemberNode member : memberNodes) { for (MemberNode member : memberNodes) {
analyze(member); analyze(member);
} }
} else if (node instanceof AssignmentNode) { } else if (node instanceof AssignmentStatementNode) {
AssignmentNode assignmentNode = (AssignmentNode) node; AssignmentStatementNode assignmentStatementNode = (AssignmentStatementNode) node;
System.out.println("Assignment ");
IdentifierNode identifierNode = assignmentStatementNode.identifier;
analyze(assignmentNode.expression); if(analyze(assignmentStatementNode.expression).equals("int")){
System.out.println("INTEGER");
}
} else if (node instanceof LiteralNode) {
return "int";
} else if (node instanceof MethodNode) { } else if (node instanceof MethodNode) {
MethodNode methodNode = (MethodNode) node; MethodNode methodNode = (MethodNode) node;
System.out.println("Methode: " + methodNode.identifier.getName());
List<StatementNode> statementNodes = methodNode.statements; List<StatementNode> statementNodes = methodNode.statements;
for (StatementNode statement : statementNodes) { for (StatementNode statement : statementNodes) {
analyze(statement); 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) { } else if (node instanceof ProgramNode) {
ProgramNode programNode = (ProgramNode) node; ProgramNode programNode = (ProgramNode) node;
List<ClassNode> classes = programNode.classes; List<ClassNode> classes = programNode.classes;
@ -61,6 +54,7 @@ public class SemanticAnalyzer {
} }
} }
return null;
} }
} }