diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-12 03:20:17 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-12 03:20:17 -0400 |
| commit | fc98d05ef934e7a52161182ca5c55d7b23174012 (patch) | |
| tree | 8839c3634576fe2f7eb1c0ba6ef31ba5051f6aae | |
| parent | 790bbab30374f6317ebbc1e6be6a94236e11e370 (diff) | |
Add sleep()
| -rw-r--r-- | builtins/functions.c | 9 | ||||
| -rw-r--r-- | builtins/functions.h | 1 | ||||
| -rw-r--r-- | docs/README.md | 24 | ||||
| -rw-r--r-- | environment.c | 1 |
4 files changed, 35 insertions, 0 deletions
diff --git a/builtins/functions.c b/builtins/functions.c index 5b019106..3c01f678 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -7,6 +7,7 @@ #include <stdlib.h> #include <sys/param.h> #include <sys/random.h> +#include <time.h> #include <uninorm.h> #include <unistd.h> @@ -370,4 +371,12 @@ public bool pop_flag(char **argv, int *i, const char *flag, Text_t *result) } } +public void sleep_num(double seconds) +{ + struct timespec ts; + ts.tv_sec = (time_t)seconds; + ts.tv_nsec = (long)((seconds - (double)ts.tv_sec) * 1e9); + nanosleep(&ts, NULL); +} + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/functions.h b/builtins/functions.h index 550676b4..b79c596a 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -33,5 +33,6 @@ int generic_print(const void *obj, bool colorize, const TypeInfo *type); Closure_t spawn(Closure_t fn); bool pop_flag(char **argv, int *i, const char *flag, Text_t *result); void print_stack_trace(FILE *out, int start, int stop); +void sleep_num(double seconds); // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/docs/README.md b/docs/README.md index b3e82772..763f863a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -121,6 +121,30 @@ say("world!") --- +### `sleep` + +**Description:** +Pause execution for a given number of seconds. + +**Usage:** +```markdown +sleep(seconds: Num) -> Void +``` + +**Parameters:** + +- `seconds`: How many seconds to sleep for. + +**Returns:** +Nothing. + +**Example:** +```markdown +sleep(1.5) +``` + +--- + ### `fail` **Description:** diff --git a/environment.c b/environment.c index f61aee56..290ddf97 100644 --- a/environment.c +++ b/environment.c @@ -46,6 +46,7 @@ env_t *new_compilation_unit(CORD *libname) .next=new(arg_t, .name="code", .type=Type(IntType, .bits=TYPE_IBITS32), .default_val=FakeAST(Int, .bits=IBITS32, .str="0"))), .ret=Type(AbortType))}}, {"fail", {.code="fail", .type=Type(FunctionType, .args=new(arg_t, .name="message", .type=Type(CStringType)), .ret=Type(AbortType))}}, + {"sleep", {.code="sleep_num", .type=Type(FunctionType, .args=new(arg_t, .name="seconds", .type=Type(NumType, .bits=TYPE_NBITS64)), .ret=Type(VoidType))}}, {"USE_COLOR", {.code="USE_COLOR", .type=Type(BoolType)}}, }; |
