Added Statements and Expressions for parser
This commit is contained in:
parent
0bce8639f5
commit
4acf4dfe24
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
4
src/main/java/ast/BlockNode.java
Normal file
4
src/main/java/ast/BlockNode.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package ast;
|
||||||
|
|
||||||
|
public class BlockNode {
|
||||||
|
}
|
@ -1,10 +1,13 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
|
import ast.type.AccessTypeNode;
|
||||||
|
import ast.type.EnumAccessTypeNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ClassNode extends ASTNode{
|
public class ClassNode extends ASTNode{
|
||||||
public Identifier identifier;
|
public IdentifierNode identifier;
|
||||||
public AccessTypeNode accessType;
|
public AccessTypeNode accessType;
|
||||||
public String name;
|
public String name;
|
||||||
public List<MemberNode> members = new ArrayList<>();
|
public List<MemberNode> members = new ArrayList<>();
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
|
import ast.type.AccessTypeNode;
|
||||||
|
|
||||||
public class ConstructorNode extends MethodNode{
|
public class ConstructorNode extends MethodNode{
|
||||||
public ConstructorNode(AccessTypeNode visibility, String name) {
|
public ConstructorNode(AccessTypeNode visibility, String name) {
|
||||||
super(visibility, name);
|
super(visibility, name);
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
package ast;
|
|
||||||
|
|
||||||
public class ExpressionNode extends ASTNode {
|
|
||||||
|
|
||||||
int Value;
|
|
||||||
|
|
||||||
public ExpressionNode(int value) {
|
|
||||||
this.Value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,14 +1,16 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
|
import ast.type.AccessTypeNode;
|
||||||
|
|
||||||
public class FieldNode extends MemberNode {
|
public class FieldNode extends MemberNode {
|
||||||
public AccessTypeNode accessTypeNode;
|
public AccessTypeNode accessTypeNode;
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public Identifier identifier;
|
public IdentifierNode identifier;
|
||||||
|
|
||||||
public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){
|
public FieldNode(AccessTypeNode accessTypeNode, TypeNode type, String name){
|
||||||
this.accessTypeNode = accessTypeNode;
|
this.accessTypeNode = accessTypeNode;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.identifier = new Identifier(name);
|
this.identifier = new IdentifierNode(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
public class Identifier {
|
public class IdentifierNode {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
public Identifier(String name){
|
public IdentifierNode(String name){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,8 +13,8 @@ public class Identifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if(obj instanceof Identifier){
|
if(obj instanceof IdentifierNode){
|
||||||
Identifier identifier = (Identifier) obj;
|
IdentifierNode identifier = (IdentifierNode) obj;
|
||||||
if(name.equals(identifier.getName())){
|
if(name.equals(identifier.getName())){
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
@ -1,10 +1,13 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
|
import ast.statement.StatementNode;
|
||||||
|
import ast.type.AccessTypeNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MethodNode extends MemberNode{
|
public class MethodNode extends MemberNode{
|
||||||
public Identifier identifier;
|
public IdentifierNode identifier;
|
||||||
public AccessTypeNode visibility;
|
public AccessTypeNode visibility;
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String name;
|
public String name;
|
||||||
@ -16,7 +19,7 @@ public class MethodNode extends MemberNode{
|
|||||||
public MethodNode(AccessTypeNode visibility, TypeNode type, String name, ParameterListNode parameters,
|
public MethodNode(AccessTypeNode visibility, TypeNode type, String name, ParameterListNode parameters,
|
||||||
List<StatementNode> statements){
|
List<StatementNode> statements){
|
||||||
this.visibility = visibility;
|
this.visibility = visibility;
|
||||||
this.identifier = new Identifier(name);
|
this.identifier = new IdentifierNode(name);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.parameters = parameters;
|
this.parameters = parameters;
|
||||||
@ -25,6 +28,6 @@ public class MethodNode extends MemberNode{
|
|||||||
|
|
||||||
public MethodNode(AccessTypeNode visibility, String name){
|
public MethodNode(AccessTypeNode visibility, String name){
|
||||||
this.visibility = visibility;
|
this.visibility = visibility;
|
||||||
this.identifier = new Identifier(name);
|
this.identifier = new IdentifierNode(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
|
import ast.type.EnumTypeNode;
|
||||||
|
|
||||||
public class TypeNode extends ASTNode {
|
public class TypeNode extends ASTNode {
|
||||||
public EnumTypeNode enumTypeNode;
|
public EnumTypeNode enumTypeNode;
|
||||||
|
|
||||||
|
13
src/main/java/ast/expression/BinaryExpressionNode.java
Normal file
13
src/main/java/ast/expression/BinaryExpressionNode.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
7
src/main/java/ast/expression/ExpressionNode.java
Normal file
7
src/main/java/ast/expression/ExpressionNode.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package ast.expression;
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
|
|
||||||
|
public class ExpressionNode extends ASTNode {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package ast.expression;
|
||||||
|
|
||||||
|
public class IdentifierExpressionNode extends ExpressionNode {
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public IdentifierExpressionNode(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
11
src/main/java/ast/expression/UnaryExpressionNode.java
Normal file
11
src/main/java/ast/expression/UnaryExpressionNode.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
13
src/main/java/ast/statement/AssignmentStatementNode.java
Normal file
13
src/main/java/ast/statement/AssignmentStatementNode.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
15
src/main/java/ast/statement/IfStatementNode.java
Normal file
15
src/main/java/ast/statement/IfStatementNode.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
11
src/main/java/ast/statement/ReturnStatementNode.java
Normal file
11
src/main/java/ast/statement/ReturnStatementNode.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package ast;
|
package ast.statement;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
|
|
@ -1,10 +1,12 @@
|
|||||||
package ast;
|
package ast.statement;
|
||||||
|
|
||||||
|
import ast.TypeNode;
|
||||||
|
import ast.expression.ExpressionNode;
|
||||||
|
|
||||||
public class VariableDeclarationStatementNode extends StatementNode {
|
public class VariableDeclarationStatementNode extends StatementNode {
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
public ExpressionNode expression; // Nullable for cases without initialization
|
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 = identifier;
|
13
src/main/java/ast/statement/WhileStatementNode.java
Normal file
13
src/main/java/ast/statement/WhileStatementNode.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,6 @@
|
|||||||
package ast;
|
package ast.type;
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
|
|
||||||
public class AccessTypeNode extends ASTNode {
|
public class AccessTypeNode extends ASTNode {
|
||||||
public EnumAccessTypeNode enumAccessTypeNode;
|
public EnumAccessTypeNode enumAccessTypeNode;
|
@ -1,4 +1,4 @@
|
|||||||
package ast;
|
package ast.type;
|
||||||
|
|
||||||
public enum EnumAccessTypeNode {
|
public enum EnumAccessTypeNode {
|
||||||
PUBLIC, PRIVATE
|
PUBLIC, PRIVATE
|
@ -1,4 +1,4 @@
|
|||||||
package ast;
|
package ast.type;
|
||||||
|
|
||||||
public enum EnumTypeNode {
|
public enum EnumTypeNode {
|
||||||
INT, BOOLEAN, CHAR
|
INT, BOOLEAN, CHAR
|
@ -1,7 +1,7 @@
|
|||||||
package bytecode;
|
package bytecode;
|
||||||
|
|
||||||
import ast.AccessTypeNode;
|
import ast.type.AccessTypeNode;
|
||||||
import ast.EnumAccessTypeNode;
|
import ast.type.EnumAccessTypeNode;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
|
||||||
public class Mapper {
|
public class Mapper {
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
package parser;
|
package parser;
|
||||||
|
|
||||||
import ast.*;
|
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.SimpleJavaBaseVisitor;
|
||||||
import parser.generated.SimpleJavaParser;
|
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
|
||||||
@ -21,7 +29,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
@Override
|
@Override
|
||||||
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
||||||
ClassNode classNode = new ClassNode((AccessTypeNode) visit(ctx.accessType()),ctx.IDENTIFIER().getText());
|
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()) {
|
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
||||||
classNode.addMember((MemberNode) visit(member));
|
classNode.addMember((MemberNode) visit(member));
|
||||||
}
|
}
|
||||||
@ -108,26 +116,78 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
return null;
|
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
|
@Override
|
||||||
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
|
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
|
||||||
ExpressionNode expressionNode = (ExpressionNode) visit(ctx.expression());
|
String identifier = ctx.IDENTIFIER().getText();
|
||||||
return new AssignmentNode(ctx.IDENTIFIER().getText(), expressionNode);
|
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
|
@Override
|
||||||
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
|
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
|
||||||
if(ctx.literal() != null){
|
// Handle binary operations
|
||||||
return visitLiteral(ctx.literal());
|
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;
|
// Handle unary operations
|
||||||
}
|
else if (ctx.getChildCount() == 2) {
|
||||||
|
String operator = ctx.getChild(0).getText();
|
||||||
@Override
|
ExpressionNode expression = (ExpressionNode) visit(ctx.expression(0));
|
||||||
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
|
return new UnaryExpressionNode(expression, operator);
|
||||||
if(ctx.INTEGERLITERAL() != null){
|
}
|
||||||
return new ExpressionNode(Integer.parseInt(ctx.INTEGERLITERAL().getSymbol().getText()));
|
// 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class SemanticAnalyzer {
|
public class SemanticAnalyzer {
|
||||||
|
|
||||||
List<Identifier> usedIdentifier = new ArrayList<>();
|
List<IdentifierNode> usedIdentifier = new ArrayList<>();
|
||||||
|
|
||||||
public void analyze(ASTNode node) {
|
public void analyze(ASTNode node) {
|
||||||
if (node == null) return;
|
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)){
|
if(usedIdentifier.contains(identifier)){
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user