예제 #1
0
 /**
  * Apply processing to a single node
  * 
  * @param Node $node
  */
 public function applyToNode(Node $node)
 {
     $expression = $node->getInstruction($this);
     $ast = $this->getParser()->parse($expression);
     $variables = $node->getResult() ?: [];
     $arithmetic = new Hoa\Math\Visitor\Arithmetic();
     foreach ($variables as $name => $value) {
         // Constants are upper case and variables lower case. We don't really care, so using a workaround.
         if (preg_match('/^[A-Z_][A-Z0-9_]*$/', $name)) {
             $arithmetic->addConstant($name, $value);
         } else {
             $arithmetic->addVariable($name, function () use($value) {
                 return $value;
             });
         }
     }
     $result = $arithmetic->visit($ast);
     $node->setResult($result);
 }
예제 #2
0
 public function case_visitor_variable()
 {
     $this->given($compiler = Compiler\Llk\Llk::load(new File\Read('hoa://Library/Math/Arithmetic.pp')), $visitor = new CUT(), $variableName = 'a_variable', $variableValue = 42)->when($visitor->addVariable($variableName, function () use($variableValue) {
         return $variableValue;
     }))->then->float($visitor->visit($compiler->parse($variableName . ' * 2')))->isEqualTo($variableValue * 2);
 }