2020-05-31 10:50:29 +00:00
|
|
|
<?php
|
2020-12-14 01:49:07 +00:00
|
|
|
/**
|
|
|
|
* MadLisp language
|
|
|
|
* @link http://madlisp.com/
|
|
|
|
* @copyright Copyright (c) 2020 Pekka Laiho
|
|
|
|
*/
|
|
|
|
|
2020-05-31 10:50:29 +00:00
|
|
|
namespace MadLisp;
|
|
|
|
|
2020-06-05 08:33:52 +00:00
|
|
|
use Closure;
|
|
|
|
|
2020-05-31 10:50:29 +00:00
|
|
|
class UserFunc extends Func
|
|
|
|
{
|
2020-06-05 08:33:52 +00:00
|
|
|
protected $ast;
|
|
|
|
protected Env $tempEnv;
|
2020-12-05 01:13:17 +00:00
|
|
|
protected Seq $bindings;
|
2020-06-05 08:33:52 +00:00
|
|
|
|
2020-12-06 01:26:27 +00:00
|
|
|
public function __construct(Closure $closure, $ast, Env $tempEnv, Seq $bindings, bool $macro = false)
|
2020-06-05 08:33:52 +00:00
|
|
|
{
|
2020-12-06 01:26:27 +00:00
|
|
|
parent::__construct($closure, null, $macro);
|
2020-06-05 08:33:52 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2020-06-05 08:33:52 +00:00
|
|
|
public function getEnv(array $args)
|
|
|
|
{
|
2020-06-06 08:31:09 +00:00
|
|
|
$newEnv = new Env('apply', $this->tempEnv);
|
2020-06-05 08:33:52 +00:00
|
|
|
|
2020-12-19 10:45:18 +00:00
|
|
|
Util::bindArguments($newEnv, $this->bindings->getData(), $args);
|
2020-05-31 10:50:29 +00:00
|
|
|
|
2020-06-05 08:33:52 +00:00
|
|
|
return $newEnv;
|
|
|
|
}
|
2020-12-03 13:23:26 +00:00
|
|
|
|
|
|
|
public function call(array $args)
|
|
|
|
{
|
|
|
|
return ($this->closure)(...$args);
|
|
|
|
}
|
2020-05-31 10:50:29 +00:00
|
|
|
}
|