Added Statements and Expressions for parser

This commit is contained in:
i22035 2024-05-09 13:47:38 +02:00
parent 0bce8639f5
commit 4acf4dfe24
25 changed files with 208 additions and 61 deletions

View File

@ -1,14 +0,0 @@
package ast;
public class AssignmentNode extends StatementNode {
public Identifier identifier;
public ExpressionNode expression;
public AssignmentNode(String identifier, ExpressionNode expression) {
this.identifier = new Identifier(identifier);
this.expression = expression;
}
}

View File

@ -0,0 +1,4 @@
package ast;
public class BlockNode {
}

View File

@ -1,10 +1,13 @@
package ast;
import ast.type.AccessTypeNode;
import ast.type.EnumAccessTypeNode;
import java.util.ArrayList;
import java.util.List;
public class ClassNode extends ASTNode{
public Identifier identifier;
public IdentifierNode identifier;
public AccessTypeNode accessType;
public String name;
public List<MemberNode> members = new ArrayList<>();

View File

@ -1,5 +1,7 @@
package ast;
import ast.type.AccessTypeNode;
public class ConstructorNode extends MethodNode{
public ConstructorNode(AccessTypeNode visibility, String name) {
super(visibility, name);

View File

@ -1,11 +0,0 @@
package ast;
public class ExpressionNode extends ASTNode {
int Value;
public ExpressionNode(int value) {
this.Value = value;
}
}

View File

@ -1,14 +1,16 @@
package ast;
import ast.type.AccessTypeNode;
public class FieldNode extends MemberNode {
public AccessTypeNode accessTypeNode;
public TypeNode type;
public Identifier identifier;
public IdentifierNode identifier;
public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){
this.accessTypeNode = accessTypeNode;
this.type = type;
this.identifier = new Identifier(name);
this.identifier = new IdentifierNode(name);
}
}

View File

@ -1,10 +1,10 @@
package ast;
public class Identifier {
public class IdentifierNode {
private String name;
public Identifier(String name){
public IdentifierNode(String name){
this.name = name;
}
@ -13,8 +13,8 @@ public class Identifier {
}
public boolean equals(Object obj) {
if(obj instanceof Identifier){
Identifier identifier = (Identifier) obj;
if(obj instanceof IdentifierNode){
IdentifierNode identifier = (IdentifierNode) obj;
if(name.equals(identifier.getName())){
return true;
} else {

View File

@ -1,10 +1,13 @@
package ast;
import ast.statement.StatementNode;
import ast.type.AccessTypeNode;
import java.util.ArrayList;
import java.util.List;
public class MethodNode extends MemberNode{
public Identifier identifier;
public IdentifierNode identifier;
public AccessTypeNode visibility;
public TypeNode type;
public String name;
@ -16,7 +19,7 @@ public class MethodNode extends MemberNode{
public MethodNode(AccessTypeNode visibility, TypeNode type, String name, ParameterListNode parameters,
List<StatementNode> statements){
this.visibility = visibility;
this.identifier = new Identifier(name);
this.identifier = new IdentifierNode(name);
this.type = type;
this.name = name;
this.parameters = parameters;
@ -25,6 +28,6 @@ public class MethodNode extends MemberNode{
public MethodNode(AccessTypeNode visibility, String name){
this.visibility = visibility;
this.identifier = new Identifier(name);
this.identifier = new IdentifierNode(name);
}
}

View File

@ -1,5 +1,7 @@
package ast;
import ast.type.EnumTypeNode;
public class TypeNode extends ASTNode {
public EnumTypeNode enumTypeNode;

View File

@ -0,0 +1,13 @@
package ast.expression;
public class BinaryExpressionNode extends ExpressionNode {
public ExpressionNode left;
public ExpressionNode right;
public String operator; // Stores the operator as a string (e.g., "+", "-", "&&")
public BinaryExpressionNode(ExpressionNode left, ExpressionNode right, String operator) {
this.left = left;
this.right = right;
this.operator = operator;
}
}

View File

@ -0,0 +1,7 @@
package ast.expression;
import ast.ASTNode;
public class ExpressionNode extends ASTNode {
}

View File

@ -0,0 +1,9 @@
package ast.expression;
public class IdentifierExpressionNode extends ExpressionNode {
public String name;
public IdentifierExpressionNode(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,11 @@
package ast.expression;
public class UnaryExpressionNode extends ExpressionNode {
public ExpressionNode expression;
public String operator; // Stores the operator (e.g., "-", "!")
public UnaryExpressionNode(ExpressionNode expression, String operator) {
this.expression = expression;
this.operator = operator;
}
}

View File

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

View File

@ -0,0 +1,15 @@
package ast.statement;
import ast.expression.ExpressionNode;
public class IfStatementNode extends StatementNode {
public ExpressionNode condition;
public StatementNode thenStatement;
public StatementNode elseStatement;
public IfStatementNode(ExpressionNode condition, StatementNode thenStatement, StatementNode elseStatement) {
this.condition = condition;
this.thenStatement = thenStatement;
this.elseStatement = elseStatement;
}
}

View File

@ -0,0 +1,11 @@
package ast.statement;
import ast.expression.ExpressionNode;
public class ReturnStatementNode extends StatementNode {
public ExpressionNode expression;
public ReturnStatementNode(ExpressionNode expression) {
this.expression = expression;
}
}

View File

@ -1,4 +1,4 @@
package ast;
package ast.statement;
import ast.ASTNode;

View File

@ -1,10 +1,12 @@
package ast;
package ast.statement;
import ast.TypeNode;
import ast.expression.ExpressionNode;
public class VariableDeclarationStatementNode extends StatementNode {
public TypeNode type;
public String identifier;
public ExpressionNode expression; // Nullable for cases without initialization
public ExpressionNode expression;
public VariableDeclarationStatementNode(TypeNode type, String identifier, ExpressionNode expression) {
this.type = type;
this.identifier = identifier;

View File

@ -0,0 +1,13 @@
package ast.statement;
import ast.expression.ExpressionNode;
public class WhileStatementNode extends StatementNode {
public ExpressionNode condition;
public StatementNode body;
public WhileStatementNode(ExpressionNode condition, StatementNode body) {
this.condition = condition;
this.body = body;
}
}

View File

@ -1,4 +1,6 @@
package ast;
package ast.type;
import ast.ASTNode;
public class AccessTypeNode extends ASTNode {
public EnumAccessTypeNode enumAccessTypeNode;

View File

@ -1,4 +1,4 @@
package ast;
package ast.type;
public enum EnumAccessTypeNode {
PUBLIC, PRIVATE

View File

@ -1,4 +1,4 @@
package ast;
package ast.type;
public enum EnumTypeNode {
INT, BOOLEAN, CHAR

View File

@ -1,7 +1,7 @@
package bytecode;
import ast.AccessTypeNode;
import ast.EnumAccessTypeNode;
import ast.type.AccessTypeNode;
import ast.type.EnumAccessTypeNode;
import org.objectweb.asm.Opcodes;
public class Mapper {

View File

@ -1,12 +1,20 @@
package parser;
import ast.*;
import ast.expression.BinaryExpressionNode;
import ast.expression.ExpressionNode;
import ast.expression.IdentifierExpressionNode;
import ast.expression.UnaryExpressionNode;
import ast.statement.*;
import ast.type.AccessTypeNode;
import ast.type.EnumAccessTypeNode;
import ast.type.EnumTypeNode;
import org.antlr.v4.runtime.tree.TerminalNode;
import parser.generated.SimpleJavaBaseVisitor;
import parser.generated.SimpleJavaParser;
import java.util.ArrayList;
import java.util.List;
import parser.generated.SimpleJavaParser.LiteralContext;
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
@ -21,7 +29,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
ClassNode classNode = new ClassNode((AccessTypeNode) visit(ctx.accessType()),ctx.IDENTIFIER().getText());
classNode.identifier = new Identifier(ctx.IDENTIFIER().getText());
classNode.identifier = new IdentifierNode(ctx.IDENTIFIER().getText());
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
classNode.addMember((MemberNode) visit(member));
}
@ -108,26 +116,78 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null;
}
@Override
public ASTNode visitVariableDeclarationStatement(SimpleJavaParser.VariableDeclarationStatementContext ctx) {
TypeNode type = (TypeNode) visit(ctx.type());
String identifier = ctx.IDENTIFIER().getText();
ExpressionNode expression = null;
if (ctx.expression() != null) {
expression = (ExpressionNode) visit(ctx.expression());
}
return new VariableDeclarationStatementNode(type, identifier, expression);
}
@Override
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
ExpressionNode expressionNode = (ExpressionNode) visit(ctx.expression());
return new AssignmentNode(ctx.IDENTIFIER().getText(), expressionNode);
String identifier = ctx.IDENTIFIER().getText();
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
return new AssignmentStatementNode(identifier, expression);
}
@Override
public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) {
ExpressionNode condition = (ExpressionNode) visit(ctx.expression());
StatementNode thenStatement = (StatementNode) visit(ctx.statement(0)); // The 'then' branch
StatementNode elseStatement = null;
if (ctx.statement().size() > 1) {
elseStatement = (StatementNode) visit(ctx.statement(1)); // The 'else' branch, if present
}
return new IfStatementNode(condition, thenStatement, elseStatement);
}
@Override
public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) {
ExpressionNode condition = (ExpressionNode) visit(ctx.expression()); // Visit the condition part of the while statement
StatementNode body = (StatementNode) visit(ctx.statement()); // Visit the body part of the while statement
return new WhileStatementNode(condition, body);
}
@Override
public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) {
ExpressionNode expression = null;
if (ctx.expression() != null) {
expression = (ExpressionNode) visit(ctx.expression()); // Visit the expression part of the return statement, if it exists
}
return new ReturnStatementNode(expression);
}
@Override
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
if(ctx.literal() != null){
return visitLiteral(ctx.literal());
// Handle binary operations
if (ctx.getChildCount() == 3 && ctx.getChild(1) instanceof TerminalNode) {
ExpressionNode left = (ExpressionNode) visit(ctx.expression(0));
ExpressionNode right = (ExpressionNode) visit(ctx.expression(1));
String operator = ctx.getChild(1).getText();
return new BinaryExpressionNode(left, right, operator);
}
return null;
}
@Override
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
if(ctx.INTEGERLITERAL() != null){
return new ExpressionNode(Integer.parseInt(ctx.INTEGERLITERAL().getSymbol().getText()));
// Handle unary operations
else if (ctx.getChildCount() == 2) {
String operator = ctx.getChild(0).getText();
ExpressionNode expression = (ExpressionNode) visit(ctx.expression(0));
return new UnaryExpressionNode(expression, operator);
}
// Handle parentheses
else if (ctx.getChildCount() == 3 && ctx.getChild(0).getText().equals("(")) {
return visit(ctx.expression(0)); // Simply return the inner expression
}
// Handle literals and identifiers
else if (ctx.literal() != null) {
return visit(ctx.literal());
}
else if (ctx.IDENTIFIER() != null) {
return new IdentifierExpressionNode(ctx.IDENTIFIER().getText());
}
return null;
}
return null; // Return null or throw an exception if no valid expression found
}
}

View File

@ -8,7 +8,7 @@ import java.util.List;
public class SemanticAnalyzer {
List<Identifier> usedIdentifier = new ArrayList<>();
List<IdentifierNode> usedIdentifier = new ArrayList<>();
public void analyze(ASTNode node) {
if (node == null) return;
@ -47,7 +47,7 @@ public class SemanticAnalyzer {
}
public boolean identifierAlreadyUsed(Identifier identifier){
public boolean identifierAlreadyUsed(IdentifierNode identifier){
if(usedIdentifier.contains(identifier)){
return true;
} else {