mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-26 07:04:27 +00:00
add function: pstr
This commit is contained in:
parent
a34d528d80
commit
b7ff127e6b
@ -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.
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
|
@ -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) {
|
||||
$env->set('read', new CoreFunc('read', 'Read string as code.', 1, 1,
|
||||
fn (string $a) => $this->reader->read($this->tokenizer->tokenize($a))
|
||||
|
@ -5,7 +5,12 @@ class Printer
|
||||
{
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user