add function: pstr

This commit is contained in:
Pekka Laiho 2020-12-08 17:39:26 +07:00
parent a34d528d80
commit b7ff127e6b
3 changed files with 14 additions and 2 deletions

View File

@ -290,7 +290,8 @@ doc | yes | `(doc +)` | `"Return the sum of all arguments."` | Show the docume
error | yes | `(error "invalid value")` | `error: invalid value` | Throw an exception with message as argument. error | yes | `(error "invalid value")` | `error: invalid value` | Throw an exception with message as argument.
exit | no | `(exit 1)` | | Terminate the script with given exit code using [exit](https://www.php.net/manual/en/function.exit.php). exit | no | `(exit 1)` | | Terminate the script with given exit code using [exit](https://www.php.net/manual/en/function.exit.php).
loop | yes | `(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 | yes | `(loop (fn (a) (do (print a) (coinflip))) "hello ")` | `hello hello hello false` | Call the given function repeatedly in a loop until it returns false.
print | no | `(print "hello world")` | `"hello world"null` | Print expression on the screen. Print returns null (which is shown due to the extra print in repl). Give optional second argument as `true` to show strings in readable format. print | no | `(print "hello world")` | `hello world` | Print expression on the screen. Give optional second argument as `true` to show strings in readable format. Print returns null (shown in REPL).
pstr | yes | `(pstr {"a":"b"} true)` | `"{\"a\":\"b\"}"` | Print expression to string. Give optional second argument as `true` to show strings in readable format.
read | no | `(read "(+ 1 2 3)")` | `(+ 1 2 3)` | Read a string as code and return the expression. read | no | `(read "(+ 1 2 3)")` | `(+ 1 2 3)` | Read a string as code and return the expression.
sleep | no | `(sleep 2000)` | `null` | Sleep for the given period given in milliseconds using [usleep](https://www.php.net/manual/en/function.usleep). sleep | no | `(sleep 2000)` | `null` | Sleep for the given period given in milliseconds using [usleep](https://www.php.net/manual/en/function.usleep).
timer | no | `(timer (fn (d) (sleep d)) 200)` | `0.20010209` | Measure the execution time of a function and return it in seconds. timer | no | `(timer (fn (d) (sleep d)) 200)` | `0.20010209` | Measure the execution time of a function and return it in seconds.

View File

@ -94,6 +94,12 @@ class Core implements ILib
)); ));
} }
$env->set('pstr', new CoreFunc('pstr', 'Print argument to string. Give second argument as true to show strings in readable format.', 1, 2,
function ($a, bool $readable = false) {
return $this->printer->pstr($a, $readable);
}
));
if (!$this->safemode) { if (!$this->safemode) {
$env->set('read', new CoreFunc('read', 'Read string as code.', 1, 1, $env->set('read', new CoreFunc('read', 'Read string as code.', 1, 1,
fn (string $a) => $this->reader->read($this->tokenizer->tokenize($a)) fn (string $a) => $this->reader->read($this->tokenizer->tokenize($a))

View File

@ -5,7 +5,12 @@ class Printer
{ {
public function print($ast, bool $readable = true): void public function print($ast, bool $readable = true): void
{ {
print($this->doPrint($ast, $readable)); print($this->pstr($ast, $readable));
}
public function pstr($ast, bool $readable = true): string
{
return $this->doPrint($ast, $readable);
} }
private function doPrint($a, bool $readable): string private function doPrint($a, bool $readable): string