diff --git a/src/main/java/ast/AssignmentNode.java b/src/main/java/ast/AssignmentNode.java deleted file mode 100644 index 38e7fb9..0000000 --- a/src/main/java/ast/AssignmentNode.java +++ /dev/null @@ -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; - } - -} diff --git a/src/main/java/ast/BlockNode.java b/src/main/java/ast/BlockNode.java new file mode 100644 index 0000000..2dd5307 --- /dev/null +++ b/src/main/java/ast/BlockNode.java @@ -0,0 +1,4 @@ +package ast; + +public class BlockNode { +} diff --git a/src/main/java/ast/ClassNode.java b/src/main/java/ast/ClassNode.java index dd390ee..81dc844 100644 --- a/src/main/java/ast/ClassNode.java +++ b/src/main/java/ast/ClassNode.java @@ -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 members = new ArrayList<>(); diff --git a/src/main/java/ast/ConstructorNode.java b/src/main/java/ast/ConstructorNode.java index a824f5a..94108b8 100644 --- a/src/main/java/ast/ConstructorNode.java +++ b/src/main/java/ast/ConstructorNode.java @@ -1,5 +1,7 @@ package ast; +import ast.type.AccessTypeNode; + public class ConstructorNode extends MethodNode{ public ConstructorNode(AccessTypeNode visibility, String name) { super(visibility, name); diff --git a/src/main/java/ast/ExpressionNode.java b/src/main/java/ast/ExpressionNode.java deleted file mode 100644 index 9e6f2a8..0000000 --- a/src/main/java/ast/ExpressionNode.java +++ /dev/null @@ -1,11 +0,0 @@ -package ast; - -public class ExpressionNode extends ASTNode { - - int Value; - - public ExpressionNode(int value) { - this.Value = value; - } - -} diff --git a/src/main/java/ast/FieldNode.java b/src/main/java/ast/FieldNode.java index 3d0d287..4e316c1 100644 --- a/src/main/java/ast/FieldNode.java +++ b/src/main/java/ast/FieldNode.java @@ -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); } } diff --git a/src/main/java/ast/Identifier.java b/src/main/java/ast/IdentifierNode.java similarity index 67% rename from src/main/java/ast/Identifier.java rename to src/main/java/ast/IdentifierNode.java index 2f6fe73..3300633 100644 --- a/src/main/java/ast/Identifier.java +++ b/src/main/java/ast/IdentifierNode.java @@ -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 { diff --git a/src/main/java/ast/MethodNode.java b/src/main/java/ast/MethodNode.java index d9058c5..2c4e1e9 100644 --- a/src/main/java/ast/MethodNode.java +++ b/src/main/java/ast/MethodNode.java @@ -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 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); } } diff --git a/src/main/java/ast/TypeNode.java b/src/main/java/ast/TypeNode.java index fd921b6..4733bc8 100644 --- a/src/main/java/ast/TypeNode.java +++ b/src/main/java/ast/TypeNode.java @@ -1,5 +1,7 @@ package ast; +import ast.type.EnumTypeNode; + public class TypeNode extends ASTNode { public EnumTypeNode enumTypeNode; diff --git a/src/main/java/ast/expression/BinaryExpressionNode.java b/src/main/java/ast/expression/BinaryExpressionNode.java new file mode 100644 index 0000000..e63bbca --- /dev/null +++ b/src/main/java/ast/expression/BinaryExpressionNode.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/ast/expression/ExpressionNode.java b/src/main/java/ast/expression/ExpressionNode.java new file mode 100644 index 0000000..eff9d29 --- /dev/null +++ b/src/main/java/ast/expression/ExpressionNode.java @@ -0,0 +1,7 @@ +package ast.expression; + +import ast.ASTNode; + +public class ExpressionNode extends ASTNode { + +} diff --git a/src/main/java/ast/expression/IdentifierExpressionNode.java b/src/main/java/ast/expression/IdentifierExpressionNode.java new file mode 100644 index 0000000..7631429 --- /dev/null +++ b/src/main/java/ast/expression/IdentifierExpressionNode.java @@ -0,0 +1,9 @@ +package ast.expression; + +public class IdentifierExpressionNode extends ExpressionNode { + public String name; + + public IdentifierExpressionNode(String name) { + this.name = name; + } +} diff --git a/src/main/java/ast/expression/UnaryExpressionNode.java b/src/main/java/ast/expression/UnaryExpressionNode.java new file mode 100644 index 0000000..0c77ef5 --- /dev/null +++ b/src/main/java/ast/expression/UnaryExpressionNode.java @@ -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; + } +} diff --git a/src/main/java/ast/statement/AssignmentStatementNode.java b/src/main/java/ast/statement/AssignmentStatementNode.java new file mode 100644 index 0000000..97bcd45 --- /dev/null +++ b/src/main/java/ast/statement/AssignmentStatementNode.java @@ -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; + } +} diff --git a/src/main/java/ast/statement/IfStatementNode.java b/src/main/java/ast/statement/IfStatementNode.java new file mode 100644 index 0000000..93eec91 --- /dev/null +++ b/src/main/java/ast/statement/IfStatementNode.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/ast/statement/ReturnStatementNode.java b/src/main/java/ast/statement/ReturnStatementNode.java new file mode 100644 index 0000000..4ca4dfa --- /dev/null +++ b/src/main/java/ast/statement/ReturnStatementNode.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/ast/StatementNode.java b/src/main/java/ast/statement/StatementNode.java similarity index 77% rename from src/main/java/ast/StatementNode.java rename to src/main/java/ast/statement/StatementNode.java index 059a96a..910a134 100644 --- a/src/main/java/ast/StatementNode.java +++ b/src/main/java/ast/statement/StatementNode.java @@ -1,4 +1,4 @@ -package ast; +package ast.statement; import ast.ASTNode; diff --git a/src/main/java/ast/VariableDeclarationStatementNode.java b/src/main/java/ast/statement/VariableDeclarationStatementNode.java similarity index 73% rename from src/main/java/ast/VariableDeclarationStatementNode.java rename to src/main/java/ast/statement/VariableDeclarationStatementNode.java index 6222942..1095fd6 100644 --- a/src/main/java/ast/VariableDeclarationStatementNode.java +++ b/src/main/java/ast/statement/VariableDeclarationStatementNode.java @@ -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; diff --git a/src/main/java/ast/statement/WhileStatementNode.java b/src/main/java/ast/statement/WhileStatementNode.java new file mode 100644 index 0000000..e6a10b1 --- /dev/null +++ b/src/main/java/ast/statement/WhileStatementNode.java @@ -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; + } +} diff --git a/src/main/java/ast/AccessTypeNode.java b/src/main/java/ast/type/AccessTypeNode.java similarity index 85% rename from src/main/java/ast/AccessTypeNode.java rename to src/main/java/ast/type/AccessTypeNode.java index 2ed2e55..ddacf68 100644 --- a/src/main/java/ast/AccessTypeNode.java +++ b/src/main/java/ast/type/AccessTypeNode.java @@ -1,4 +1,6 @@ -package ast; +package ast.type; + +import ast.ASTNode; public class AccessTypeNode extends ASTNode { public EnumAccessTypeNode enumAccessTypeNode; diff --git a/src/main/java/ast/EnumAccessTypeNode.java b/src/main/java/ast/type/EnumAccessTypeNode.java similarity index 75% rename from src/main/java/ast/EnumAccessTypeNode.java rename to src/main/java/ast/type/EnumAccessTypeNode.java index 2e6ffba..12cdda9 100644 --- a/src/main/java/ast/EnumAccessTypeNode.java +++ b/src/main/java/ast/type/EnumAccessTypeNode.java @@ -1,4 +1,4 @@ -package ast; +package ast.type; public enum EnumAccessTypeNode { PUBLIC, PRIVATE diff --git a/src/main/java/ast/EnumTypeNode.java b/src/main/java/ast/type/EnumTypeNode.java similarity index 74% rename from src/main/java/ast/EnumTypeNode.java rename to src/main/java/ast/type/EnumTypeNode.java index b80cd57..d858461 100644 --- a/src/main/java/ast/EnumTypeNode.java +++ b/src/main/java/ast/type/EnumTypeNode.java @@ -1,4 +1,4 @@ -package ast; +package ast.type; public enum EnumTypeNode { INT, BOOLEAN, CHAR diff --git a/src/main/java/bytecode/Mapper.java b/src/main/java/bytecode/Mapper.java index 872f8e1..168e1e2 100644 --- a/src/main/java/bytecode/Mapper.java +++ b/src/main/java/bytecode/Mapper.java @@ -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 { diff --git a/src/main/java/parser/ASTBuilder.java b/src/main/java/parser/ASTBuilder.java index e90ba95..c4f9374 100644 --- a/src/main/java/parser/ASTBuilder.java +++ b/src/main/java/parser/ASTBuilder.java @@ -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 { @Override @@ -21,7 +29,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @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 { 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 + } } diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 315a51e..940a0dc 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -8,7 +8,7 @@ import java.util.List; public class SemanticAnalyzer { - List usedIdentifier = new ArrayList<>(); + List 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 {