move functions 'sleep' and 'timer' to Core

This commit is contained in:
Pekka Laiho 2020-12-05 11:32:25 +07:00
parent 35224bce8a
commit 35ebe02e0a
3 changed files with 42 additions and 38 deletions

View File

@ -165,12 +165,14 @@ Name | Example | Example result | Description
debug | `(debug)` | `true` | Toggle debug output. debug | `(debug)` | `true` | Toggle debug output.
doc | `(doc +)` | `"Return the sum of all arguments."` | Show the documentation string for a function. doc | `(doc +)` | `"Return the sum of all arguments."` | Show the documentation string for a function.
doc | `(doc myfn "Documentation string.")` | `"Documentation string."` | Set the documentation string for a function. doc | `(doc myfn "Documentation string.")` | `"Documentation string."` | Set the documentation string for a 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.
meta | `(meta (env) "name")` | `"root/user"` | Read meta information of an entity.
read | `(read "(+ 1 2 3)")` | `(+ 1 2 3)` | Read a string as code and return the expression.
print | `(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.
error | `(error "invalid value")` | `error: invalid value` | Throw an exception with message as argument. error | `(error "invalid value")` | `error: invalid value` | Throw an exception with message as argument.
exit | `(exit 1)` | | Terminate the script with given exit code using [exit](https://www.php.net/manual/en/function.exit.php). exit | `(exit 1)` | | Terminate the script with given exit code using [exit](https://www.php.net/manual/en/function.exit.php).
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.
print | `(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.
read | `(read "(+ 1 2 3)")` | `(+ 1 2 3)` | Read a string as code and return the expression.
sleep | `(sleep 2000)` | `null` | Sleep for the given period given in milliseconds using [usleep](https://www.php.net/manual/en/function.usleep).
timer | `(timer (fn (d) (sleep d)) 200)` | `0.20010209` | Measure the execution time of a function and return it in seconds.
### Collection functions ### Collection functions
@ -331,8 +333,6 @@ Name | Example | Example result | Description
time | `(time)` | `1592011969` | Return the current unix timestamp using [time](https://www.php.net/manual/en/function.time). time | `(time)` | `1592011969` | Return the current unix timestamp using [time](https://www.php.net/manual/en/function.time).
date | `(date "Y-m-d H:i:s")` | `"2020-06-13 08:33:29"` | Format the current time and date using [date](https://www.php.net/manual/en/function.date.php). date | `(date "Y-m-d H:i:s")` | `"2020-06-13 08:33:29"` | Format the current time and date using [date](https://www.php.net/manual/en/function.date.php).
strtotime | `(strtotime "2020-06-13 08:34:47")` | `1592012087` | Parse datetime string into unix timestamp using [strtotime](https://www.php.net/manual/en/function.strtotime.php). strtotime | `(strtotime "2020-06-13 08:34:47")` | `1592012087` | Parse datetime string into unix timestamp using [strtotime](https://www.php.net/manual/en/function.strtotime.php).
sleep | `(sleep 2000)` | `null` | Sleep for the given period given in milliseconds using [usleep](https://www.php.net/manual/en/function.usleep).
timer | `(timer (fn (d) (sleep d)) 200)` | `0.20010209` | Measure the execution time of a function and return it in seconds.
### Type functions ### Type functions

View File

@ -60,6 +60,22 @@ class Core implements ILib
} }
)); ));
// This is allowed in safe-mode, because the evaluation should be wrapped in a try-catch in embedded use.
$env->set('error', new CoreFunc('error', 'Throw an exception using argument (string) as message.', 1, 1,
function (string $error) {
// We should probably use another exception type to distinguish user-thrown errors from built-in errors.
throw new MadLispException($error);
}
));
if (!$this->safemode) {
$env->set('exit', new CoreFunc('exit', 'Terminate the script with given exit code.', 0, 1,
function (int $status = 0) {
exit($status);
}
));
}
$env->set('loop', new CoreFunc('loop', 'Call the given function repeatedly in a loop until it returns false.', 1, -1, $env->set('loop', new CoreFunc('loop', 'Call the given function repeatedly in a loop until it returns false.', 1, -1,
function (Func $f, ...$args) { function (Func $f, ...$args) {
do { do {
@ -97,12 +113,6 @@ class Core implements ILib
)); ));
} }
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))
));
}
if (!$this->safemode) { if (!$this->safemode) {
$env->set('print', new CoreFunc('print', 'Print argument. Give second argument as true to show strings in readable format.', 1, 2, $env->set('print', new CoreFunc('print', 'Print argument. Give second argument as true to show strings in readable format.', 1, 2,
function ($a, bool $readable = false) { function ($a, bool $readable = false) {
@ -112,18 +122,28 @@ class Core implements ILib
)); ));
} }
// This is allowed in safe-mode, because the evaluation should be wrapped in a try-catch in embedded use. if (!$this->safemode) {
$env->set('error', new CoreFunc('error', 'Throw an exception using argument (string) as message.', 1, 1, $env->set('read', new CoreFunc('read', 'Read string as code.', 1, 1,
function (string $error) { fn (string $a) => $this->reader->read($this->tokenizer->tokenize($a))
// We should probably use another exception type to distinguish user-thrown errors from built-in errors. ));
throw new MadLispException($error); }
}
));
if (!$this->safemode) { if (!$this->safemode) {
$env->set('exit', new CoreFunc('exit', 'Terminate the script with given exit code.', 0, 1, $env->set('sleep', new CoreFunc('sleep', 'Sleep (wait) for the specified time in milliseconds.', 1, 1,
function (int $status = 0) { function (int $time) {
exit($status); usleep($time * 1000);
return null;
}
));
}
if (!$this->safemode) {
$env->set('timer', new CoreFunc('timer', 'Measure the execution time of a function and return it in seconds.', 1, -1,
function (Func $f, ...$args) {
$start = microtime(true);
$f->call($args);
$end = microtime(true);
return $end - $start;
} }
)); ));
} }

View File

@ -20,21 +20,5 @@ class Time implements ILib
$env->set('strtotime', new CoreFunc('strtotime', 'Parse datetime string into unix timestamp. Optional second argument can be used to give time for relative formats.', 1, 2, $env->set('strtotime', new CoreFunc('strtotime', 'Parse datetime string into unix timestamp. Optional second argument can be used to give time for relative formats.', 1, 2,
fn (string $format, ?int $time = null) => strtotime($format, $time !== null ? $time : time()) fn (string $format, ?int $time = null) => strtotime($format, $time !== null ? $time : time())
)); ));
$env->set('sleep', new CoreFunc('sleep', 'Sleep (wait) for the specified time in milliseconds.', 1, 1,
function (int $time) {
usleep($time * 1000);
return null;
}
));
$env->set('timer', new CoreFunc('timer', 'Measure the execution time of a function and return it in seconds.', 1, -1,
function (Func $f, ...$args) {
$start = microtime(true);
$f->call($args);
$end = microtime(true);
return $end - $start;
}
));
} }
} }