From 12a6588128c7b739f5e57f36c0a94c5305733c5e Mon Sep 17 00:00:00 2001 From: Nicolas Abril Date: Tue, 18 Jun 2024 18:58:34 +0200 Subject: [PATCH] Add builtin sleep in float seconds --- CHANGELOG.md | 3 +++ cspell.json | 5 +++-- src/fun/builtins.bend | 12 +++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efa0aae2..bdb535a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project does not currently adhere to a particular versioning scheme. - Improve error messages for redefinition of types and objects. ([#485][gh-485]) - Don't allow tabs to be used for indentation or spacing. ([#463][gh-463]) +- Rename builtin function `sleep` to `IO/nanosleep`. ([#581][gh-581]) ### Fixed @@ -21,6 +22,7 @@ and this project does not currently adhere to a particular versioning scheme. - Create new type of top-level definition for writing native HVM definitions. ([#586][gh-586]) - Add `log` and `atan2` builtin functions. ([#583][gh-583]) - Add `to_f24`, `to_u24` and `to_i24` number casting builtin functions. ([#582][gh-582]) +- Add `IO/sleep` builtin function to sleep for a given amount of seconds as a float. ([#581][gh-581]) ## [0.2.35] - 2024-06-06 @@ -341,6 +343,7 @@ and this project does not currently adhere to a particular versioning scheme. [gh-516]: https://github.com/HigherOrderCO/Bend/issues/516 [gh-526]: https://github.com/HigherOrderCO/Bend/issues/526 [gh-528]: https://github.com/HigherOrderCO/Bend/issues/528 +[gh-581]: https://github.com/HigherOrderCO/Bend/issues/581 [gh-582]: https://github.com/HigherOrderCO/Bend/issues/582 [gh-583]: https://github.com/HigherOrderCO/Bend/issues/583 [gh-586]: https://github.com/HigherOrderCO/Bend/issues/586 diff --git a/cspell.json b/cspell.json index faf42b2d..72e96227 100644 --- a/cspell.json +++ b/cspell.json @@ -61,6 +61,7 @@ "mult", "namegen", "nams", + "nanosleep", "nats", "newtype", "nilary", @@ -75,6 +76,7 @@ "postcondition", "powi", "prec", + "proto", "Pythonish", "quadtree", "quadtrees", @@ -107,14 +109,13 @@ "succ", "supercombinator", "supercombinators", + "tlsv", "TSPL", "tunr", "unbounds", "undefer", "vectorize", "vectorizes", - "tlsv", - "proto", "walkdir" ], "files": [ diff --git a/src/fun/builtins.bend b/src/fun/builtins.bend index fd262b3c..6c8fe2cc 100644 --- a/src/fun/builtins.bend +++ b/src/fun/builtins.bend @@ -89,18 +89,24 @@ def call(func, argm): return IO/Call(IO/MAGIC, func, argm, lambda x: IO/Done(IO/MAGIC, x)) -print text = (IO/Call IO/MAGIC "PUT_TEXT" text @x (IO/Done IO/MAGIC x)) +print text = (IO/Call IO/MAGIC "WRITE" (1, text) @x (IO/Done IO/MAGIC x)) #input = (IO/Call IO/MAGIC "GET_TEXT" * @x (IO/Done IO/MAGIC x)) #read_file path = (IO/Call IO/MAGIC "GET_FILE" path @x (IO/Done IO/MAGIC x)) #write_file path text = (IO/Call IO/MAGIC "PUT_FILE" (path, text) @x (IO/Done IO/MAGIC x)) # Returns a monotonically increasing nanosecond timestamp as an u48 encoded as a pair of u24s. -get_time = (IO/Call IO/MAGIC "GET_TIME" * @x (IO/Done IO/MAGIC x)) +IO/get_time = (IO/Call IO/MAGIC "GET_TIME" * @x (IO/Done IO/MAGIC x)) # Sleeps for the given number of nanoseconds, given by an u48 encoded as a pair of u24s. -sleep hi_lo = (IO/Call IO/MAGIC "PUT_TIME" hi_lo @x (IO/Done IO/MAGIC x)) +IO/nanosleep hi_lo = (IO/Call IO/MAGIC "SLEEP" hi_lo @x (IO/Done IO/MAGIC x)) +# Sleeps for a given amount of seconds as a float. +def IO/sleep(seconds): + nanos = seconds * 1_000_000_000.0 + lo = to_u24(nanos % 0x1_000_000.0) + hi = to_u24(nanos / 0x1_000_000.0) + return IO/nanosleep((hi, lo)) # Lazy thunks # We can defer the evaluation of a function by wrapping it in a thunk