Пример #1
0
 public function __invoke(array $nodes, Environment $env, Node $self)
 {
     if (count($nodes) !== 3) {
         throw new BadCallException(sprintf('`if` expects exactly 3 arguments, got %d near %s', count($nodes), $self->context()));
     }
     return $nodes[0]->evaluate($env) ? $nodes[1]->evaluate($env) : $nodes[2]->evaluate($env);
 }
Пример #2
0
 public function __invoke(array $nodes, Environment $env, Node $self)
 {
     if (count($nodes) < 1) {
         throw new BadCallException(sprintf('`and` expects at least 1 argument, got %d near %s', count($nodes), $self->context()));
     }
     $results = array_filter(array_map(function ($node) use($env) {
         return $node->evaluate($env);
     }, $nodes));
     return count($results) === count($nodes);
 }
Пример #3
0
 public function __invoke(array $nodes, Environment $env, Node $self)
 {
     if (count($nodes) < 2) {
         throw new BadCallException(sprintf('`lambda` expects at least 2 arguments, got %d near %s', count($nodes), $self->context()));
     }
     $args = $nodes[0];
     if (!$args instanceof ListNode) {
         throw new BadCallException(sprintf('The first argument to `lambda` must be a list of arguments near %s', $args->context()));
     }
     $argNames = $this->extractArguments($args);
     return new UserFunc(clone $env, $argNames, array_slice($nodes, 1));
 }
Пример #4
0
 public function __invoke(array $nodes, Environment $env, Node $self)
 {
     if (count($nodes) < 1) {
         throw new BadCallException(sprintf('`or` expects at least 1 argument, got %d near %s', count($nodes), $self->context()));
     }
     foreach ($nodes as $node) {
         if ($node->evaluate($env)) {
             return true;
         }
     }
     return false;
 }
Пример #5
0
 public function __invoke(array $nodes, Environment $env, Node $self)
 {
     if (count($nodes) !== count($this->argNames)) {
         throw new BadCallException(sprintf('`%s` expects %d arguments, got %d near %s', $self instanceof IdentifierNode ? $self->rawIdent() : 'Anonymous Function', count($this->argNames), count($nodes), $self->context()));
     }
     $env->put('recur', $this);
     foreach (array_combine($this->argNames, $nodes) as $name => $node) {
         $env->put($name, $node->evaluate($env));
     }
     $value = null;
     $closure = new ClosureEnvironment($env, $this->wrappedEnv);
     foreach ($this->nodes as $node) {
         $value = $node->evaluate($closure);
     }
     return $value;
 }
Пример #6
0
 public function __invoke(array $nodes, Environment $env, Node $self)
 {
     if (count($nodes) !== 2) {
         throw new BadCallException(sprintf('`define` expects exactly 2 arguments, got %d near %s', count($nodes), $self->context()));
     }
     if (!$nodes[0] instanceof IdentifierNode) {
         throw new BadCallException(sprintf('The first argument of `define` must be an identifier, got %s. %s', get_class($nodes[0]), $nodes[0]->context()));
     }
     $ident = $nodes[0]->rawIdent();
     $value = $nodes[1]->evaluate($env);
     if ($env->hasParent()) {
         $env->putParent($ident, $value);
     } else {
         $env->put($ident, $value);
     }
     return null;
 }
Пример #7
0
 private function checkNumeric($value, Node $node, Node $self)
 {
     if (!is_numeric($value)) {
         throw new BadCallException(sprintf('`%s` expected numeric arguments, got "%s" near %s', $self->rawIdent(), $value, $node->context()));
     }
 }