add toggle for debug output

This commit is contained in:
Pekka Laiho 2020-12-05 08:42:56 +07:00
parent 1ece3649bc
commit 90f4d7860d
3 changed files with 14 additions and 0 deletions

View File

@ -162,6 +162,7 @@ quote | `(quote (1 2 3))` | `(1 2 3)` | Return the argument without evaluation.
Name | Example | Example result | Description Name | Example | Example result | Description
----- | ------- | -------------- | ----------- ----- | ------- | -------------- | -----------
debug | `(debug)` | `true` | Toggle debug output.
doc | `(doc +)` | `"Return the sum of all arguments."` | Show description of a built-in function. doc | `(doc +)` | `"Return the sum of all arguments."` | Show description of a built-in function.
loop | `(loop (fn (a) (do (print a) (coinflip))) "hello ")` | `hello hello hello false` | Call the given function repeatedly in a loop until it returns false. loop | `(loop (fn (a) (do (print a) (coinflip))) "hello ")` | `hello hello hello false` | Call the given function repeatedly in a loop until it returns false.
meta | `(meta (env) "name")` | `"root/user"` | Read meta information of an entity. meta | `(meta (env) "name")` | `"root/user"` | Read meta information of an entity.

View File

@ -294,6 +294,11 @@ class Evaller
} }
} }
public function getDebug(): bool
{
return $this->debug;
}
public function setDebug(bool $val): void public function setDebug(bool $val): void
{ {
$this->debug = $val; $this->debug = $val;

View File

@ -18,6 +18,14 @@ class LispFactory
$env->set('__DIR__', null); $env->set('__DIR__', null);
// Register core functions // Register core functions
$env->set('debug', new CoreFunc('debug', 'Toggle debug mode.', 0, 0,
function () use ($eval) {
$val = !$eval->getDebug();
$eval->setDebug($val);
return $val;
}
));
$env->set('doc', new CoreFunc('doc', 'Get documentation for a function.', 1, 1, $env->set('doc', new CoreFunc('doc', 'Get documentation for a function.', 1, 1,
function ($a) { function ($a) {
if ($a instanceof Func) { if ($a instanceof Func) {