From 66c9481b3ee8bc15492472d7cee7d6d900303845 Mon Sep 17 00:00:00 2001 From: i22007 Date: Sun, 23 Jun 2024 13:43:42 -0400 Subject: [PATCH] Add all accept methods --- src/main/java/ast/ProgramNode.java | 7 ++ .../DotSubstractionNode.java | 6 ++ .../expressions/unaryexpressions/NotNode.java | 16 +++- src/main/java/ast/members/FieldNode.java | 7 ++ .../ast/statement/BlockStatementNode.java | 2 + .../ast/statementexpressions/AssignNode.java | 6 ++ .../statementexpressions/AssignableNode.java | 6 ++ .../NewDeclarationNode.java | 6 ++ .../crementexpressions/DecrementNode.java | 7 ++ .../crementexpressions/IncrementNode.java | 6 ++ .../ChainedMethodNode.java | 16 +++- .../MethodCallNode.java | 6 ++ .../TargetNode.java | 16 +++- .../LocalVariableDeclarationNode.java | 6 ++ src/main/java/ast/statements/ReturnNode.java | 6 ++ .../java/ast/statements/StatementNode.java | 4 +- src/main/java/ast/statements/WhileNode.java | 20 ++++- src/main/java/bytecode/MethodCodeGen.java | 75 +++++++++++++++++-- .../java/bytecode/visitor/MethodVisitor.java | 26 ++++++- 19 files changed, 230 insertions(+), 14 deletions(-) diff --git a/src/main/java/ast/ProgramNode.java b/src/main/java/ast/ProgramNode.java index 7fbe3e0..b13d78e 100644 --- a/src/main/java/ast/ProgramNode.java +++ b/src/main/java/ast/ProgramNode.java @@ -1,5 +1,7 @@ package ast; +import bytecode.visitor.MethodVisitor; +import bytecode.visitor.ProgramVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; import visitor.Visitable; @@ -18,4 +20,9 @@ public class ProgramNode implements ASTNode, Visitable { public TypeCheckResult accept(SemanticVisitor visitor) { return visitor.analyze(this); } + + @Override + public void accept(ProgramVisitor programVisitor) { + programVisitor.visit(this); + } } diff --git a/src/main/java/ast/expressions/binaryexpressions/DotSubstractionNode.java b/src/main/java/ast/expressions/binaryexpressions/DotSubstractionNode.java index 9537e15..c5fa214 100644 --- a/src/main/java/ast/expressions/binaryexpressions/DotSubstractionNode.java +++ b/src/main/java/ast/expressions/binaryexpressions/DotSubstractionNode.java @@ -4,6 +4,7 @@ import ast.expressions.unaryexpressions.MemberAccessNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; import ast.type.type.*; import ast.type.ValueNode; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; @@ -41,4 +42,9 @@ public class DotSubstractionNode extends BinaryNode { return null; } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + } diff --git a/src/main/java/ast/expressions/unaryexpressions/NotNode.java b/src/main/java/ast/expressions/unaryexpressions/NotNode.java index 017dfe1..e0c31a3 100644 --- a/src/main/java/ast/expressions/unaryexpressions/NotNode.java +++ b/src/main/java/ast/expressions/unaryexpressions/NotNode.java @@ -2,12 +2,26 @@ package ast.expressions.unaryexpressions; import ast.ASTNode; import ast.expressions.IExpressionNode; +import bytecode.visitor.MethodVisitor; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; +import visitor.Visitable; -public class NotNode implements ASTNode { +public class NotNode implements ASTNode, Visitable { public IExpressionNode expression; public NotNode(IExpressionNode expression) { this.expression = expression; } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return null; + } + } diff --git a/src/main/java/ast/members/FieldNode.java b/src/main/java/ast/members/FieldNode.java index 86d2f59..edf434c 100644 --- a/src/main/java/ast/members/FieldNode.java +++ b/src/main/java/ast/members/FieldNode.java @@ -2,6 +2,8 @@ package ast.members; import ast.type.AccessModifierNode; import ast.type.type.ITypeNode; +import bytecode.visitor.ClassVisitor; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; import visitor.Visitable; @@ -22,4 +24,9 @@ public class FieldNode implements MemberNode, Visitable { return visitor.analyze(this); } + @Override + public void accept(ClassVisitor classVisitor) { + classVisitor.visit(this); + } + } diff --git a/src/main/java/ast/statement/BlockStatementNode.java b/src/main/java/ast/statement/BlockStatementNode.java index 524e004..6005d1a 100644 --- a/src/main/java/ast/statement/BlockStatementNode.java +++ b/src/main/java/ast/statement/BlockStatementNode.java @@ -1,5 +1,7 @@ package ast.statement; +import ast.statements.IStatementNode; + import java.util.List; public class BlockStatementNode { diff --git a/src/main/java/ast/statementexpressions/AssignNode.java b/src/main/java/ast/statementexpressions/AssignNode.java index 4a5022f..c620abd 100644 --- a/src/main/java/ast/statementexpressions/AssignNode.java +++ b/src/main/java/ast/statementexpressions/AssignNode.java @@ -1,6 +1,7 @@ package ast.statementexpressions; import ast.expressions.IExpressionNode; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; @@ -18,4 +19,9 @@ public class AssignNode implements IStatementExpressionNode { return visitor.analyze(this); } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + } diff --git a/src/main/java/ast/statementexpressions/AssignableNode.java b/src/main/java/ast/statementexpressions/AssignableNode.java index d3a1677..8f527e0 100644 --- a/src/main/java/ast/statementexpressions/AssignableNode.java +++ b/src/main/java/ast/statementexpressions/AssignableNode.java @@ -1,6 +1,7 @@ package ast.statementexpressions; import ast.expressions.unaryexpressions.MemberAccessNode; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; @@ -22,4 +23,9 @@ public class AssignableNode implements IStatementExpressionNode { return visitor.analyze(this); } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + } diff --git a/src/main/java/ast/statementexpressions/NewDeclarationNode.java b/src/main/java/ast/statementexpressions/NewDeclarationNode.java index d7a37bc..0964a6c 100644 --- a/src/main/java/ast/statementexpressions/NewDeclarationNode.java +++ b/src/main/java/ast/statementexpressions/NewDeclarationNode.java @@ -1,6 +1,7 @@ package ast.statementexpressions; import ast.expressions.IExpressionNode; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; @@ -24,4 +25,9 @@ public class NewDeclarationNode implements IStatementExpressionNode { return visitor.analyze(this); } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + } diff --git a/src/main/java/ast/statementexpressions/crementexpressions/DecrementNode.java b/src/main/java/ast/statementexpressions/crementexpressions/DecrementNode.java index 48cb82c..b9801c3 100644 --- a/src/main/java/ast/statementexpressions/crementexpressions/DecrementNode.java +++ b/src/main/java/ast/statementexpressions/crementexpressions/DecrementNode.java @@ -2,8 +2,10 @@ package ast.statementexpressions.crementexpressions; import ast.statementexpressions.AssignableNode; import ast.statementexpressions.IStatementExpressionNode; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; +import visitor.Visitable; public class DecrementNode implements IStatementExpressionNode { public CrementType crementType; @@ -18,4 +20,9 @@ public class DecrementNode implements IStatementExpressionNode { return visitor.analyze(this); } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + } diff --git a/src/main/java/ast/statementexpressions/crementexpressions/IncrementNode.java b/src/main/java/ast/statementexpressions/crementexpressions/IncrementNode.java index f283db4..f2c8828 100644 --- a/src/main/java/ast/statementexpressions/crementexpressions/IncrementNode.java +++ b/src/main/java/ast/statementexpressions/crementexpressions/IncrementNode.java @@ -2,6 +2,7 @@ package ast.statementexpressions.crementexpressions; import ast.statementexpressions.AssignableNode; import ast.statementexpressions.IStatementExpressionNode; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; @@ -18,4 +19,9 @@ public class IncrementNode implements IStatementExpressionNode { return visitor.analyze(this); } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + } diff --git a/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/ChainedMethodNode.java b/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/ChainedMethodNode.java index b9fa8e4..ae01b85 100644 --- a/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/ChainedMethodNode.java +++ b/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/ChainedMethodNode.java @@ -2,11 +2,15 @@ package ast.statementexpressions.methodcallstatementnexpressions; import ast.ASTNode; import ast.expressions.IExpressionNode; +import bytecode.visitor.MethodVisitor; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; +import visitor.Visitable; import java.util.ArrayList; import java.util.List; -public class ChainedMethodNode implements ASTNode { +public class ChainedMethodNode implements ASTNode, Visitable { public String identifier; public List expressions = new ArrayList<>(); @@ -17,4 +21,14 @@ public class ChainedMethodNode implements ASTNode { public void addExpression(IExpressionNode expression) { expressions.add(expression); } + + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return null; + } } diff --git a/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/MethodCallNode.java b/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/MethodCallNode.java index 5228ed2..88d9ac5 100644 --- a/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/MethodCallNode.java +++ b/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/MethodCallNode.java @@ -2,6 +2,7 @@ package ast.statementexpressions.methodcallstatementnexpressions; import ast.expressions.IExpressionNode; import ast.statements.IStatementNode; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; @@ -32,4 +33,9 @@ public class MethodCallNode implements IStatementNode { return visitor.analyze(this); } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + } diff --git a/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/TargetNode.java b/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/TargetNode.java index 01e2344..c3fec66 100644 --- a/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/TargetNode.java +++ b/src/main/java/ast/statementexpressions/methodcallstatementnexpressions/TargetNode.java @@ -3,8 +3,12 @@ package ast.statementexpressions.methodcallstatementnexpressions; import ast.ASTNode; import ast.expressions.unaryexpressions.MemberAccessNode; import ast.statementexpressions.NewDeclarationNode; +import bytecode.visitor.MethodVisitor; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; +import visitor.Visitable; -public class TargetNode implements ASTNode { +public class TargetNode implements ASTNode, Visitable { public Boolean thisTar; public MemberAccessNode memberAccess; public NewDeclarationNode newDeclaration; @@ -25,4 +29,14 @@ public class TargetNode implements ASTNode { public TargetNode(String identifier) { this.identifier = identifier; } + + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return null; + } } \ No newline at end of file diff --git a/src/main/java/ast/statements/LocalVariableDeclarationNode.java b/src/main/java/ast/statements/LocalVariableDeclarationNode.java index 881c437..726ea24 100644 --- a/src/main/java/ast/statements/LocalVariableDeclarationNode.java +++ b/src/main/java/ast/statements/LocalVariableDeclarationNode.java @@ -2,6 +2,7 @@ package ast.statements; import ast.expressions.IExpressionNode; import ast.type.type.*; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; @@ -23,4 +24,9 @@ public class LocalVariableDeclarationNode implements IStatementNode { return visitor.analyze(this); } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + } diff --git a/src/main/java/ast/statements/ReturnNode.java b/src/main/java/ast/statements/ReturnNode.java index e25cf6f..fed9c44 100644 --- a/src/main/java/ast/statements/ReturnNode.java +++ b/src/main/java/ast/statements/ReturnNode.java @@ -1,6 +1,7 @@ package ast.statements; import ast.expressions.IExpressionNode; +import bytecode.visitor.MethodVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; @@ -21,4 +22,9 @@ public class ReturnNode implements IStatementNode { return visitor.analyze(this); } + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + } diff --git a/src/main/java/ast/statements/StatementNode.java b/src/main/java/ast/statements/StatementNode.java index 24985c1..dccc841 100644 --- a/src/main/java/ast/statements/StatementNode.java +++ b/src/main/java/ast/statements/StatementNode.java @@ -1,2 +1,4 @@ -package ast.statements;public class StatementNode { +package ast.statements; + +public class StatementNode { } diff --git a/src/main/java/ast/statements/WhileNode.java b/src/main/java/ast/statements/WhileNode.java index fe9bfd2..8a881a8 100644 --- a/src/main/java/ast/statements/WhileNode.java +++ b/src/main/java/ast/statements/WhileNode.java @@ -2,13 +2,27 @@ package ast.statements; import ast.ASTNode; import ast.expressions.IExpressionNode; +import bytecode.visitor.MethodVisitor; +import semantic.SemanticVisitor; +import typechecker.TypeCheckResult; +import visitor.Visitable; -public class WhileNode extends StatementNode implements ASTNode { - IExpressionNode expression; - BlockNode block; +public class WhileNode extends StatementNode implements ASTNode, Visitable { + public IExpressionNode expression; + public BlockNode block; public WhileNode(IExpressionNode expression, BlockNode block) { this.expression = expression; this.block = block; } + + @Override + public void accept(MethodVisitor methodVisitor) { + methodVisitor.visit(this); + } + + @Override + public TypeCheckResult accept(SemanticVisitor visitor) { + return null; + } } diff --git a/src/main/java/bytecode/MethodCodeGen.java b/src/main/java/bytecode/MethodCodeGen.java index 52fc698..aea940d 100644 --- a/src/main/java/bytecode/MethodCodeGen.java +++ b/src/main/java/bytecode/MethodCodeGen.java @@ -9,6 +9,13 @@ import ast.members.MainMethodNode; import ast.members.MethodNode; import ast.parameters.ParameterNode; import ast.statementexpressions.AssignNode; +import ast.statementexpressions.AssignableNode; +import ast.statementexpressions.NewDeclarationNode; +import ast.statementexpressions.crementexpressions.DecrementNode; +import ast.statementexpressions.crementexpressions.IncrementNode; +import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode; +import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; +import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statements.*; import ast.type.type.BaseType; import org.objectweb.asm.ClassWriter; @@ -108,10 +115,10 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { } @Override - public void visit(CalculationNode calculat) { - calculat.dotExpression.accept(this); - calculat.calculationExpression.accept(this); - switch (calculat.operator) { + public void visit(CalculationNode calculationNode) { + calculationNode.dotExpression.accept(this); + calculationNode.calculationExpression.accept(this); + switch (calculationNode.operator) { case PLUS: methodVisitor.visitInsn(IADD); break; @@ -140,7 +147,9 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { @Override public void visit(DotSubstractionNode dotSubstractionNode) { + if(dotSubstractionNode.memberAccess == null) { // local var + } } @Override @@ -183,12 +192,33 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { case EQUAL: nonCalculationNode.unaryExpression.accept(this); nonCalculationNode.expression.accept(this); - + if (nonCalculationNode.unaryExpression.getType() instanceof BaseType && nonCalculationNode.expression.getType() instanceof BaseType) { + methodVisitor.visitJumpInsn(IF_ICMPNE, labelFalse); + } else { + methodVisitor.visitJumpInsn(IF_ACMPNE, labelFalse); + } break; case NOT_EQUAL: + nonCalculationNode.unaryExpression.accept(this); + nonCalculationNode.expression.accept(this); + if (nonCalculationNode.unaryExpression.getType() instanceof BaseType && nonCalculationNode.expression.getType() instanceof BaseType) { + methodVisitor.visitJumpInsn(IF_ACMPEQ, labelFalse); + } else { + methodVisitor.visitJumpInsn(IF_ACMPEQ, labelFalse); + } break; } + Label labelEndLogicalExpression = new Label(); + + methodVisitor.visitLabel(labelTrue); + methodVisitor.visitInsn(ICONST_1); // true + methodVisitor.visitJumpInsn(GOTO, labelEndLogicalExpression); + + methodVisitor.visitLabel(labelFalse); + methodVisitor.visitInsn(ICONST_0); // false + + methodVisitor.visitLabel(labelEndLogicalExpression); } @@ -259,6 +289,11 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { } } + @Override + public void visit(NewDeclarationNode newDeclarationNode) { + + } + @Override public void visit(ReturnNode returnNode) { if (returnNode.voidReturn) { // Return nothing @@ -276,4 +311,34 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor { public void visit(WhileNode whileNode) { } + + @Override + public void visit(DecrementNode decrementNode) { + + } + + @Override + public void visit(IncrementNode incrementNode) { + + } + + @Override + public void visit(ChainedMethodNode chainedMethodNode) { + + } + + @Override + public void visit(MethodCallNode methodCallNode) { + + } + + @Override + public void visit(TargetNode targetNode) { + + } + + @Override + public void visit(AssignableNode assignableNode) { + + } } diff --git a/src/main/java/bytecode/visitor/MethodVisitor.java b/src/main/java/bytecode/visitor/MethodVisitor.java index 8900815..3426646 100644 --- a/src/main/java/bytecode/visitor/MethodVisitor.java +++ b/src/main/java/bytecode/visitor/MethodVisitor.java @@ -8,30 +8,52 @@ import ast.members.ConstructorNode; import ast.members.MainMethodNode; import ast.members.MethodNode; import ast.statementexpressions.AssignNode; +import ast.statementexpressions.AssignableNode; +import ast.statementexpressions.NewDeclarationNode; +import ast.statementexpressions.crementexpressions.DecrementNode; +import ast.statementexpressions.crementexpressions.IncrementNode; +import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode; +import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; +import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statements.*; public interface MethodVisitor { + // members void visit(ConstructorNode constructorNode); void visit(MethodNode methodNode); void visit(MainMethodNode mainMethodNode); + // Binary expressions void visit(BinaryNode binaryNode); void visit(CalculationNode calculationNode); void visit(DotNode dotNode); void visit(DotSubstractionNode dotSubstractionNode); void visit(NonCalculationNode nonCalculationNode); + // Unary expressions void visit(MemberAccessNode memberAccessNode); void visit(NotNode notExpressionNode); void visit(UnaryNode unaryExpressionNode); + // statements void visit(ElseNode elseNode); void visit(IfElseNode ifElseNode); void visit(IfNode ifNode); - void visit(AssignNode assignNode); - void visit(LocalVariableDeclarationNode localVariableDeclarationNode); void visit(ReturnNode returnNode); void visit(WhileNode whileNode); + + // statement expression + void visit(DecrementNode decrementNode); + void visit(IncrementNode incrementNode); + + void visit(ChainedMethodNode chainedMethodNode); + void visit(MethodCallNode methodCallNode); + void visit(TargetNode targetNode); + + void visit(AssignableNode assignableNode); + void visit(AssignNode assignNode); + void visit(NewDeclarationNode newDeclarationNode); + }