We are building an interpreter for a language as part of the requirements in one of our undergraduate course. Part of the project involves writing a lexer and a parser. The parser generates an AST. Obviously, the AST would have nodes.
One of the nodes of that AST is the ArithmeticOperationNode
. This node will be a node containing any operations involving arithmentic. The code below is a minimized version of the node.
package ds.ast;
import exceptions.CompilerException;
import exceptions.RuntimeException;
public class ArithmeticOperationNode extends OperationNode
{
public ArithmeticOperationNode(String operation, Computable operator1, Computable operator2, int startingLine, int startingColumn)
{
super(operation, operator1, operator2, startingLine, startingColumn);
}
public Value compute() throws RuntimeException, CompilerException
{
...
}
}
NOTE: OperationNode
implements an interface called Computable
.
compute()
will be responsible for performing the arithmetic operation between operator1
and operator2
. operator1
and operator2
may be another arithmetic expression. The function will be called when we traverse the AST during execution. There are a lot of nodes that follow the same design, i.e. having a compute()
function.
Am I violating the Single Responsibility Principle with this? I have a feeling that I am doing so.