madlisp/src/UserFunc.php

48 lines
951 B
PHP
Raw Normal View History

<?php
namespace MadLisp;
use Closure;
class UserFunc extends Func
{
protected $ast;
protected Env $tempEnv;
2020-12-05 01:13:17 +00:00
protected Seq $bindings;
2020-12-05 01:13:17 +00:00
public function __construct(Closure $closure, $ast, Env $tempEnv, Seq $bindings)
{
parent::__construct($closure, null);
$this->ast = $ast;
$this->tempEnv = $tempEnv;
$this->bindings = $bindings;
}
public function getAst()
{
return $this->ast;
}
2020-12-05 01:13:17 +00:00
public function getBindings(): Seq
{
return $this->bindings;
}
public function getEnv(array $args)
{
2020-06-06 08:31:09 +00:00
$newEnv = new Env('apply', $this->tempEnv);
2020-12-05 01:13:17 +00:00
$bindings = $this->bindings->getData();
for ($i = 0; $i < count($bindings); $i++) {
$newEnv->set($bindings[$i]->getName(), $args[$i] ?? null);
}
return $newEnv;
}
public function call(array $args)
{
return ($this->closure)(...$args);
}
}