You are here

public function SassScriptParser::evaluate in Sassy 7

Same name and namespace in other branches
  1. 7.3 phpsass/script/SassScriptParser.php \SassScriptParser::evaluate()

* Evaluate a SassScript. *

Parameters

string expression to parse: * @param SassContext the context in which the expression is evaluated * @param integer the environment in which the expression is evaluated * @return SassLiteral parsed value

File

phamlp/sass/script/SassScriptParser.php, line 80

Class

SassScriptParser
SassScriptParser class. Parses SassScript. SassScript is lexed into {@link http://en.wikipedia.org/wiki/Reverse_Polish_notation Reverse Polish notation} by the SassScriptLexer and the calculated result returned. @package PHamlP @subpackage Sass.script

Code

public function evaluate($expression, $context, $environment = self::DEFAULT_ENV) {
  self::$context = $context;
  $operands = array();
  $tokens = $this
    ->parse($expression, $context, $environment);
  while (count($tokens)) {
    $token = array_shift($tokens);
    if ($token instanceof SassScriptFunction) {
      array_push($operands, $token
        ->perform());
    }
    elseif ($token instanceof SassLiteral) {
      if ($token instanceof SassString) {
        $token = new SassString($this
          ->interpolate($token
          ->toString(), self::$context));
      }
      array_push($operands, $token);
    }
    else {
      $args = array();
      for ($i = 0, $c = $token->operandCount; $i < $c; $i++) {
        $args[] = array_pop($operands);
      }
      array_push($operands, $token
        ->perform($args));
    }
  }
  return array_shift($operands);
}