PHPSA\Compiler\Expression::compile PHP Method

compile() public method

public compile ( object | string $expr ) : CompiledExpression
$expr object | string
return PHPSA\CompiledExpression
    public function compile($expr)
    {
        if (is_string($expr)) {
            return new CompiledExpression(CompiledExpression::STRING, $expr);
        }
        if (is_null($expr)) {
            return new CompiledExpression(CompiledExpression::NULL);
        }
        if (!is_object($expr)) {
            throw new InvalidArgumentException('$expr must be string/object/null');
        }
        if ($expr instanceof Node\Scalar) {
            $scalar = new \PHPSA\Compiler\Scalar($this->context, $this->eventManager);
            return $scalar->compile($expr);
        }
        $this->eventManager->fire(ExpressionBeforeCompile::EVENT_NAME, new ExpressionBeforeCompile($expr, $this->context));
        $className = get_class($expr);
        switch ($className) {
            case Node\Arg::class:
                /**
                 * @todo Better compile
                 */
                return $this->compile($expr->value);
                /**
                 * Names
                 */
            /**
             * Names
             */
            case Node\Name::class:
                return $this->getNodeName($expr);
            case Node\Name\FullyQualified::class:
                return $this->getFullyQualifiedNodeName($expr);
        }
        $expressionCompiler = $this->factory($expr);
        if (!$expressionCompiler) {
            $this->context->debug("Expression compiler is not implemented for {$className}");
            return new CompiledExpression(CompiledExpression::UNIMPLEMENTED);
        }
        $result = $expressionCompiler->pass($expr, $this->context);
        if (!$result instanceof CompiledExpression) {
            throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler));
        }
        return $result;
    }

Usage Example

Example #1
0
 /**
  * @param \PhpParser\Node\Expr\BinaryOp\Concat $expr
  * @param Context $context
  * @return CompiledExpression
  */
 protected function compile($expr, Context $context)
 {
     $compiler = new Expression($context);
     $leftExpression = $compiler->compile($expr->left);
     $rightExpression = $compiler->compile($expr->right);
     switch ($leftExpression->getType()) {
         case CompiledExpression::ARR:
             $context->notice('unsupported-operand-types', 'Unsupported operand types -{array}', $expr);
             break;
     }
     switch ($rightExpression->getType()) {
         case CompiledExpression::ARR:
             $context->notice('unsupported-operand-types', 'Unsupported operand types -{array}', $expr);
             break;
     }
     switch ($leftExpression->getType()) {
         case CompiledExpression::STRING:
         case CompiledExpression::NUMBER:
         case CompiledExpression::INTEGER:
         case CompiledExpression::DOUBLE:
             switch ($rightExpression->getType()) {
                 case CompiledExpression::STRING:
                 case CompiledExpression::NUMBER:
                 case CompiledExpression::INTEGER:
                 case CompiledExpression::DOUBLE:
                     return new CompiledExpression(CompiledExpression::STRING, $leftExpression->getValue() . $rightExpression->getValue());
                     break;
             }
             break;
     }
     return new CompiledExpression(CompiledExpression::NULL);
 }
All Usage Examples Of PHPSA\Compiler\Expression::compile