Ejemplo n.º 1
0
 public function execute(Runtime $vm)
 {
     foreach (array('source', 'key', 'value', 'body') as $var) {
         ${$var} = $this->{$var};
     }
     if ($source instanceof Variable) {
         /* it's a json definition */
         $val = $vm->get($source);
         if (is_null($val)) {
             throw new \RuntimeException("Cannot find variable " . $source);
         }
         $source = $vm->getValue($val);
     } else {
         $source = $vm->getValue($source);
     }
     foreach ($source as $zkey => $zvalue) {
         if ($key) {
             $vm->define($key, new Term($zkey));
         }
         $vm->define($value, $zvalue);
         foreach ($body as $stmt) {
             $vm->execute($stmt);
             if ($vm->isSuspended()) {
                 $vm->isSuspended(false);
                 break;
             }
             if ($vm->isStopped()) {
                 break 2;
             }
         }
     }
 }
Ejemplo n.º 2
0
 public function execute(Runtime $vm)
 {
     $text = preg_replace_callback("/__(@?[a-z][a-z0-9_]*?)__/i", function ($var) use($vm) {
         if ($var[1][0] == '@') {
             $var[1] = substr($var[1], 1);
             $varValue = true;
         }
         $value = $vm->get($var[1]);
         if (is_null($value)) {
             /* variable is not found, we ignore it */
             return $var[0];
         }
         $result = $vm->getValue($value);
         if (!empty($varValue)) {
             $result = var_export($result, true);
         }
         if (is_object($result) && is_callable(array($result, '__toString'))) {
             $result = (string) $result;
         }
         if (!is_scalar($result)) {
             throw new \RuntimeException("Only scalar values may be replaced. Use @ to get the string representation.");
         }
         return $result;
     }, $this->args);
     if ($this->isString()) {
         return $vm->printIndented($text, $this);
     }
     return $vm->doPrint($text);
 }
Ejemplo n.º 3
0
 public function getValue(Runtime $vm)
 {
     $str = "";
     foreach ($this->args as $part) {
         $str .= $vm->getValue($part);
     }
     return $str;
 }
Ejemplo n.º 4
0
 public function getValue(Runtime $vm, $doPrint = false)
 {
     $args = array();
     foreach ($this->args as $arg) {
         if (is_null($arg)) {
             continue;
         }
         $val = $vm->getValue($arg);
         $args[] = $val;
     }
     $function = $this->function;
     if (is_string($function) && is_callable(array($this, 'function' . $this->function))) {
         return $this->{'function' . $this->function}($args, $vm);
     }
     if ($function instanceof Variable) {
         // call methods
         if ($function->isObject()) {
             $object = $vm->getValue($function->getObjectVar());
             $method = $function->getPart(-1);
             if (!is_callable(array($object, $method))) {
                 throw new \RuntimeException(get_class($object) . '::' . $method . ' is not callable');
             }
             return call_user_func_array(array($object, $method), $args);
         } else {
             // $foo();
             $function = $vm->getValue($function);
         }
     }
     $isLocal = false;
     if ($vm->functionExists($function)) {
         $function = $vm->getFunction($function, $isLocal);
     }
     if (!is_callable($function)) {
         throw new \RuntimeException("{$function} is not a function");
     }
     $output = call_user_func_array($function, $args);
     if ($doPrint) {
         // if the function is indeed a local function
         // (and not defined in the php side) then
         // we should print and leave
         $vm->printIndented($isLocal ? $output->getBuffer() : $output, $this);
         return;
     }
     if ($isLocal && $output->getReturn()) {
         $vm->printIndented($output->getBuffer(), $this);
         return $output->getReturn();
     }
     return $output;
 }
Ejemplo n.º 5
0
 public function Execute(Runtime $vm)
 {
     $value = $vm->getValue($this->args[0]);
     if ($value) {
         $vm->execStmts($this->args[1]);
     } else {
         if (isset($this->args[2])) {
             if (is_array($this->args[2])) {
                 /* else */
                 $vm->execStmts($this->args[2]);
             } else {
                 /* else if */
                 $vm->execute($this->args[2]);
             }
         }
     }
 }
Ejemplo n.º 6
0
 public function execute(Runtime $vm)
 {
     $vm->halt($vm->getValue($this->args));
 }
Ejemplo n.º 7
0
 public function execute(Runtime $vm)
 {
     $vm->define($this->var, $vm->getValue($this->expr));
 }
Ejemplo n.º 8
0
 public function body(Runtime $vm, array $args = NULL)
 {
     $fncargs = array();
     self::fixXDebugRecursion();
     foreach ($this->args as $id => $arg) {
         if (empty($args[$id])) {
             break;
         }
         $fncargs[current($arg->getNative())] = $args[$id];
     }
     $pzVm = new Runtime($this->code);
     if ($vm->getParentVm()) {
         $pzVm->setParentVm($vm->getParentVm());
     } else {
         $pzVm->setParentVm($vm);
     }
     if (count($fncargs) > 0) {
         $pzVm->setContext($fncargs);
     }
     $pzVm->run();
     return $pzVm;
 }
Ejemplo n.º 9
0
 public function execute(Runtime $vm)
 {
     $vm->isSuspended(true);
 }
Ejemplo n.º 10
0
 public function getValue(Runtime $vm)
 {
     return $vm->get($this);
 }
Ejemplo n.º 11
0
 public function getValue(Runtime $vm)
 {
     $args = $this->args;
     if ($args instanceof self) {
         return $args;
     }
     foreach ($args as $id => $arg) {
         if ($arg instanceof Base) {
             $args[$id] = $vm->getValue($arg);
         }
     }
     switch (strtolower($args[0])) {
         case '>':
             $value = $args[1] > $args[2];
             break;
         case '<':
             $value = $args[1] < $args[2];
             break;
         case '>=':
             $value = $args[1] >= $args[2];
             break;
         case '<=':
             $value = $args[1] <= $args[2];
             break;
         case '*':
             $value = $args[1] * $args[2];
             break;
         case '/':
             $value = $args[1] / $args[2];
             break;
         case '-':
             $value = $args[1] - $args[2];
             break;
         case '+':
             $value = $args[1] + $args[2];
             break;
         case '%':
             $value = $args[1] % $args[2];
             break;
         case '==':
             $value = $args[1] === $args[2];
             break;
         case 'and':
         case '&&':
             $value = $args[1] && $args[2];
             break;
         case 'or':
         case '||':
             $value = $args[1] || $args[2];
             break;
         case '!=':
             $value = $args[1] !== $args[2];
             break;
         case 'not':
             $value = !$args[1];
             break;
         default:
             throw new \RuntimeException("{$args[0]} is not implemented");
     }
     return $value;
 }