mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 13:24:46 +00:00
support for (doc)
This commit is contained in:
parent
9e113faf78
commit
9e4dffef2b
@ -6,17 +6,16 @@ use Closure;
|
||||
class CoreFunc extends Func
|
||||
{
|
||||
protected string $name;
|
||||
protected string $doc;
|
||||
protected int $minArgs;
|
||||
protected int $maxArgs;
|
||||
|
||||
public function __construct(string $name, string $doc, int $minArgs, int $maxArgs, Closure $closure)
|
||||
{
|
||||
parent::__construct($closure, $doc);
|
||||
|
||||
$this->name = $name;
|
||||
$this->doc = $doc;
|
||||
$this->minArgs = $minArgs;
|
||||
$this->maxArgs = $maxArgs;
|
||||
$this->closure = $closure;
|
||||
}
|
||||
|
||||
public function call(array $args)
|
||||
|
@ -6,10 +6,12 @@ use Closure;
|
||||
abstract class Func
|
||||
{
|
||||
protected Closure $closure;
|
||||
protected ?string $doc;
|
||||
|
||||
public function __construct(Closure $closure)
|
||||
public function __construct(Closure $closure, ?string $doc = null)
|
||||
{
|
||||
$this->closure = $closure;
|
||||
$this->doc = $doc;
|
||||
}
|
||||
|
||||
public function getClosure(): Closure
|
||||
@ -17,6 +19,11 @@ abstract class Func
|
||||
return $this->closure;
|
||||
}
|
||||
|
||||
public function getDoc(): ?string
|
||||
{
|
||||
return $this->doc;
|
||||
}
|
||||
|
||||
public function call(array $args)
|
||||
{
|
||||
return ($this->closure)(...$args);
|
||||
|
10
src/Lisp.php
10
src/Lisp.php
@ -29,6 +29,16 @@ class Lisp
|
||||
|
||||
public function register(Env $env): void
|
||||
{
|
||||
$env->set('doc', new CoreFunc('doc', 'Get documentation for a function.', 1, 1,
|
||||
function ($a) {
|
||||
if ($a instanceof Func) {
|
||||
return $a->getDoc();
|
||||
}
|
||||
|
||||
throw new MadLispException('first argument to doc is not function');
|
||||
}
|
||||
));
|
||||
|
||||
$env->set('eval', new CoreFunc('eval', 'Evaluate arguments.', 1, -1,
|
||||
function (...$args) use ($env) {
|
||||
$results = $this->eval->eval($args, $env);
|
||||
|
Loading…
Reference in New Issue
Block a user