Example #1
0
 /**
  * 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;
     }
 }
Example #2
0
 public function testCanPrintToken()
 {
     $name = '+';
     $type = TokenType::AdditionOperator;
     $token = new Token($name, $type);
     $string = $token->__toString();
     $this->assertEquals($string, "Token: [{$name}, {$type}]");
 }
 private function assertTokenEquals($value, $type, Token $token)
 {
     $this->assertEquals($value, $token->getValue());
     $this->assertEquals($type, $token->getType());
 }
Example #4
0
 /**
  * Insert multiplication tokens where needed (taking care of implicit mulitplication).
  *
  * @param array $tokens Input list of tokens (Token[])
  * @retval Token[]
  */
 protected function parseImplicitMultiplication(array $tokens)
 {
     $result = [];
     $lastToken = null;
     foreach ($tokens as $token) {
         if (Token::canFactorsInImplicitMultiplication($lastToken, $token)) {
             $result[] = new Token('*', TokenType::MultiplicationOperator);
         }
         $lastToken = $token;
         $result[] = $token;
     }
     return $result;
 }