Adapted Binary Expression
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
parent
d91e3ad819
commit
403ab70655
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -40,7 +40,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -38,37 +38,37 @@ public class Main {
|
|||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
|
|
||||||
// Printing the tokens
|
// Printing the tokens
|
||||||
tokenStream.fill();
|
// tokenStream.fill();
|
||||||
List<Token> tokens = tokenStream.getTokens();
|
// List<Token> tokens = tokenStream.getTokens();
|
||||||
System.out.println("-------------------- Scanner -> tokens --------------------");
|
// System.out.println("-------------------- Scanner -> tokens --------------------");
|
||||||
for (Token token : tokens) {
|
// for (Token token : tokens) {
|
||||||
String tokenType = SimpleJavaLexer.VOCABULARY.getSymbolicName(token.getType());
|
// String tokenType = SimpleJavaLexer.VOCABULARY.getSymbolicName(token.getType());
|
||||||
String tokenText = token.getText();
|
// String tokenText = token.getText();
|
||||||
// System.out.println("Token Type: " + tokenType + ", Token Text: " +
|
// // System.out.println("Token Type: " + tokenType + ", Token Text: " +
|
||||||
// tokenText);
|
// // tokenText);
|
||||||
System.out.println(tokenType + " " + tokenText);
|
// System.out.println(tokenType + " " + tokenText);
|
||||||
}
|
// }
|
||||||
System.out.println();
|
// System.out.println();
|
||||||
|
|
||||||
/* ------------------------- Parser -> Parsetree ------------------------- */
|
/* ------------------------- Parser -> Parsetree ------------------------- */
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||||
ParseTree parseTree = parser.program(); // parse the input
|
ParseTree parseTree = parser.program(); // parse the input
|
||||||
|
|
||||||
// Printing the parse tree
|
// Printing the parse tree
|
||||||
System.out.println("-------------------- Parser -> Parsetree --------------------");
|
// System.out.println("-------------------- Parser -> Parsetree --------------------");
|
||||||
System.out.println(parseTree.toStringTree(parser));
|
// System.out.println(parseTree.toStringTree(parser));
|
||||||
printTree(parseTree, parser, 0);
|
// printTree(parseTree, parser, 0);
|
||||||
System.out.println();
|
// System.out.println();
|
||||||
|
|
||||||
/* ------------------------- AST builder -> AST ------------------------- */
|
/* ------------------------- AST builder -> AST ------------------------- */
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
||||||
|
|
||||||
// Printing the AST
|
// Printing the AST
|
||||||
System.out.println("-------------------- AST builder -> AST --------------------");
|
// System.out.println("-------------------- AST builder -> AST --------------------");
|
||||||
// System.out.println("AST: " + ast.toString());
|
// // System.out.println("AST: " + ast.toString());
|
||||||
printAST(abstractSyntaxTree, 0);
|
// printAST(abstractSyntaxTree, 0);
|
||||||
System.out.println();
|
// System.out.println();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ------------------------- Semantic Analyzer -> Tast -------------------------
|
* ------------------------- Semantic Analyzer -> Tast -------------------------
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
package ast;
|
|
||||||
|
|
||||||
import ast.type.TypeNode;
|
|
||||||
import semantic.SemanticVisitor;
|
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
import visitor.Visitable;
|
|
||||||
|
|
||||||
public class VarNode implements ASTNode, Visitable {
|
|
||||||
|
|
||||||
private String identifier;
|
|
||||||
private TypeNode type;
|
|
||||||
|
|
||||||
public VarNode(TypeNode type, String identifier){
|
|
||||||
this.type = type;
|
|
||||||
this.identifier = identifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeNode getType(){
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getIdentifier(){
|
|
||||||
return identifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
|
||||||
return visitor.analyze(this);
|
|
||||||
}
|
|
||||||
}
|
|
@ -7,9 +7,9 @@ import visitor.Visitable;
|
|||||||
public class BinaryExpressionNode implements ExpressionNode, Visitable {
|
public class BinaryExpressionNode implements ExpressionNode, Visitable {
|
||||||
public ExpressionNode left;
|
public ExpressionNode left;
|
||||||
public ExpressionNode right;
|
public ExpressionNode right;
|
||||||
public String operator; // Stores the operator as a string (e.g., "+", "-", "&&")
|
public ExpresssionOperator operator; // Stores the operator as a string (e.g., "+", "-", "&&")
|
||||||
|
|
||||||
public BinaryExpressionNode(ExpressionNode left, ExpressionNode right, String operator) {
|
public BinaryExpressionNode(ExpressionNode left, ExpressionNode right, ExpresssionOperator operator) {
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
|
14
src/main/java/ast/expression/ExpresssionOperator.java
Normal file
14
src/main/java/ast/expression/ExpresssionOperator.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package ast.expression;
|
||||||
|
|
||||||
|
public enum ExpresssionOperator {
|
||||||
|
DOT, // .
|
||||||
|
PLUS, // +
|
||||||
|
MINUS, // -
|
||||||
|
MULTIPLY, // *
|
||||||
|
DIVIDE, // /
|
||||||
|
NOT, // !
|
||||||
|
ASSIGNMENT, // =
|
||||||
|
EQUALS, // ==
|
||||||
|
UNEQUALS, // !=
|
||||||
|
ERROR //TODO: Remove This
|
||||||
|
}
|
@ -18,3 +18,5 @@ public class UnaryExpressionNode implements ExpressionNode, Visitable {
|
|||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
package ast.statement;
|
package ast.statement;
|
||||||
|
|
||||||
import ast.VarNode;
|
import ast.expression.BinaryExpressionNode;
|
||||||
import ast.expression.ExpressionNode;
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class AssignmentStatementNode extends StatementNode implements Visitable {
|
public class AssignmentStatementNode extends StatementNode implements Visitable {
|
||||||
public VarNode varNode;
|
public BinaryExpressionNode expression;
|
||||||
public ExpressionNode expression;
|
|
||||||
|
|
||||||
public AssignmentStatementNode(VarNode varNode, ExpressionNode expression) {
|
public AssignmentStatementNode(BinaryExpressionNode expression) {
|
||||||
this.varNode = varNode;
|
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package parser;
|
|||||||
import ast.*;
|
import ast.*;
|
||||||
import ast.expression.BinaryExpressionNode;
|
import ast.expression.BinaryExpressionNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
|
import ast.expression.ExpresssionOperator;
|
||||||
import ast.expression.IdentifierExpressionNode;
|
import ast.expression.IdentifierExpressionNode;
|
||||||
import ast.expression.UnaryExpressionNode;
|
import ast.expression.UnaryExpressionNode;
|
||||||
import ast.member.FieldNode;
|
import ast.member.FieldNode;
|
||||||
@ -137,14 +138,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
|
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
|
||||||
VarNode varNode = (VarNode) visit(ctx.var());
|
|
||||||
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
|
BinaryExpressionNode expression = (BinaryExpressionNode) visit(ctx.expression());
|
||||||
return new AssignmentStatementNode(varNode, expression);
|
return new AssignmentStatementNode(expression);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitVar(SimpleJavaParser.VarContext ctx) {
|
public ASTNode visitVar(SimpleJavaParser.VarContext ctx) {
|
||||||
return new VarNode(new BaseTypeNode(EnumTypeNode.INT), ctx.getText());
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -180,8 +181,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
if (ctx.getChildCount() == 3 && ctx.getChild(1) instanceof TerminalNode) {
|
if (ctx.getChildCount() == 3 && ctx.getChild(1) instanceof TerminalNode) {
|
||||||
ExpressionNode left = (ExpressionNode) visit(ctx.expression(0));
|
ExpressionNode left = (ExpressionNode) visit(ctx.expression(0));
|
||||||
ExpressionNode right = (ExpressionNode) visit(ctx.expression(1));
|
ExpressionNode right = (ExpressionNode) visit(ctx.expression(1));
|
||||||
String operator = ctx.getChild(1).getText();
|
return new BinaryExpressionNode(left, right, ExpresssionOperator.ERROR);
|
||||||
return new BinaryExpressionNode(left, right, operator);
|
|
||||||
}
|
}
|
||||||
// Handle unary operations
|
// Handle unary operations
|
||||||
else if (ctx.getChildCount() == 2) {
|
else if (ctx.getChildCount() == 2) {
|
||||||
|
@ -145,21 +145,17 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
|
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
|
||||||
if (assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
boolean valid = true;
|
||||||
TypeCheckResult varResult = assignmentStatementNode.varNode.accept(this);
|
BinaryExpressionNode binaryExpressionNode = assignmentStatementNode.expression;
|
||||||
TypeCheckResult expressionResult = assignmentStatementNode.expression.accept(this);
|
var result = binaryExpressionNode.accept(this);
|
||||||
}
|
valid = valid && result.isValid();
|
||||||
return new TypeCheckResult(true, null);
|
return new TypeCheckResult(valid, null);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult analyze(VarNode toCheck) {
|
|
||||||
return new TypeCheckResult(true, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(BinaryExpressionNode toCheck) {
|
public TypeCheckResult analyze(BinaryExpressionNode toCheck) {
|
||||||
return null;
|
boolean valid = true;
|
||||||
|
return new TypeCheckResult(valid, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -3,7 +3,6 @@ package semantic;
|
|||||||
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
import ast.VarNode;
|
|
||||||
import ast.expression.BinaryExpressionNode;
|
import ast.expression.BinaryExpressionNode;
|
||||||
import ast.expression.IdentifierExpressionNode;
|
import ast.expression.IdentifierExpressionNode;
|
||||||
import ast.expression.UnaryExpressionNode;
|
import ast.expression.UnaryExpressionNode;
|
||||||
@ -24,8 +23,6 @@ public interface SemanticVisitor {
|
|||||||
|
|
||||||
TypeCheckResult analyze(AssignmentStatementNode toCheck);
|
TypeCheckResult analyze(AssignmentStatementNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(VarNode toCheck);
|
|
||||||
|
|
||||||
TypeCheckResult analyze(BinaryExpressionNode toCheck);
|
TypeCheckResult analyze(BinaryExpressionNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(IdentifierExpressionNode toCheck);
|
TypeCheckResult analyze(IdentifierExpressionNode toCheck);
|
||||||
|
@ -2,9 +2,17 @@ public class Example {
|
|||||||
|
|
||||||
public int a;
|
public int a;
|
||||||
|
|
||||||
|
public static int testMethod(char x){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
|
||||||
public static int testMethod(char x, int a){
|
public static int testMethod(char x, int a){
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@ -2,7 +2,10 @@ package semantic;
|
|||||||
|
|
||||||
|
|
||||||
import ast.*;
|
import ast.*;
|
||||||
|
import ast.expression.BinaryExpressionNode;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
|
import ast.expression.ExpresssionOperator;
|
||||||
|
import ast.expression.IdentifierExpressionNode;
|
||||||
import ast.member.FieldNode;
|
import ast.member.FieldNode;
|
||||||
import ast.member.MemberNode;
|
import ast.member.MemberNode;
|
||||||
import ast.member.MethodNode;
|
import ast.member.MethodNode;
|
||||||
@ -76,10 +79,16 @@ public class SemanticTest {
|
|||||||
|
|
||||||
List<StatementNode> statementNodeList = new ArrayList<StatementNode>();
|
List<StatementNode> statementNodeList = new ArrayList<StatementNode>();
|
||||||
|
|
||||||
VarNode varNode = new VarNode(new BaseTypeNode(EnumTypeNode.INT), "localVar1");
|
ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this");
|
||||||
ExpressionNode expressionNode = new LiteralNode(1);
|
ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
|
||||||
|
|
||||||
StatementNode statementNode1 = new AssignmentStatementNode(varNode, expressionNode);
|
ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
|
||||||
|
|
||||||
|
ExpressionNode expressionNodeRight = new LiteralNode(1);
|
||||||
|
|
||||||
|
BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT);
|
||||||
|
|
||||||
|
StatementNode statementNode1 = new AssignmentStatementNode(expressionNode);
|
||||||
statementNodeList.add(statementNode1);
|
statementNodeList.add(statementNode1);
|
||||||
|
|
||||||
MemberNode memberNode3 = new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2",parameterListNode, statementNodeList );
|
MemberNode memberNode3 = new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2",parameterListNode, statementNodeList );
|
||||||
|
Loading…
Reference in New Issue
Block a user