/** * Node factory, creating an appropriate Node from a Token. * * Based on the provided Token, returns a TerminalNode if the * token type is PosInt, Integer, RealNumber, Identifier or Constant * otherwise returns null. * * @param Token $token Provided token * @retval Node|null */ public static function factory(Token $token) { switch ($token->getType()) { case TokenType::PosInt: case TokenType::Integer: $x = intval($token->getValue()); return new NumberNode($x); case TokenType::RealNumber: $x = floatval($token->getValue()); return new NumberNode($x); case TokenType::Identifier: return new VariableNode($token->getValue()); case TokenType::Constant: return new ConstantNode($token->getValue()); case TokenType::FunctionName: return new FunctionNode($token->getValue(), null); case TokenType::OpenParenthesis: return new SubExpressionNode($token->getValue()); case TokenType::AdditionOperator: case TokenType::SubtractionOperator: case TokenType::MultiplicationOperator: case TokenType::DivisionOperator: case TokenType::ExponentiationOperator: return new ExpressionNode(null, $token->getValue(), null); default: // echo "Node factory returning null on $token\n"; return null; } }
private function assertTokenEquals($value, $type, Token $token) { $this->assertEquals($value, $token->getValue()); $this->assertEquals($type, $token->getType()); }