Merge pull request #592 from HigherOrderCO/581-add-friendlier-sleep-functions

#581 Add builtin sleep in float seconds
This commit is contained in:
Nicolas Abril 2024-06-18 19:31:02 +00:00 committed by GitHub
commit 84661fd12a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 5 deletions

View File

@ -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

View File

@ -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": [

View File

@ -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