diff options
author | JJ | 2024-01-02 21:49:25 +0000 |
---|---|---|
committer | JJ | 2024-01-02 21:50:47 +0000 |
commit | fde7a1311543badd1052a757d1e2c68272be1188 (patch) | |
tree | abe438ea65ac8d90a1848593846531b59d0e1ce3 | |
parent | 4852f1caabcbcd5ad3dd5696bfe4bc428d653f94 (diff) |
std: more fleshing out. merge std.magic back into std.prelude.
-rw-r--r-- | std/hashes.pk | 4 | ||||
-rw-r--r-- | std/math.pk | 1 | ||||
-rw-r--r-- | std/net.pk | 1 | ||||
-rw-r--r-- | std/os.pk | 10 | ||||
-rw-r--r-- | std/prelude/arrays.pk (renamed from std/magic/arrays.pk) | 2 | ||||
-rw-r--r-- | std/prelude/booleans.pk (renamed from std/magic/booleans.pk) | 2 | ||||
-rw-r--r-- | std/prelude/debug.pk | 32 | ||||
-rw-r--r-- | std/prelude/io.pk | 21 | ||||
-rw-r--r-- | std/prelude/numbers.pk (renamed from std/magic/numbers.pk) | 2 | ||||
-rw-r--r-- | std/prelude/pointers.pk (renamed from std/magic/pointers.pk) | 0 | ||||
-rw-r--r-- | std/prelude/results.pk | 2 | ||||
-rw-r--r-- | std/prelude/strings.pk | 3 | ||||
-rw-r--r-- | std/rand.pk | 1 | ||||
-rw-r--r-- | std/threads.pk | 1 |
14 files changed, 75 insertions, 7 deletions
diff --git a/std/hashes.pk b/std/hashes.pk new file mode 100644 index 0000000..911c9d2 --- /dev/null +++ b/std/hashes.pk @@ -0,0 +1,4 @@ +## std.hashes: Hash functions on types. + +pub type Hash[H] = interface + hash[H](Self): H diff --git a/std/math.pk b/std/math.pk new file mode 100644 index 0000000..e62f3c4 --- /dev/null +++ b/std/math.pk @@ -0,0 +1 @@ +## std.math: Utility types and functions for basic mathematics. diff --git a/std/net.pk b/std/net.pk new file mode 100644 index 0000000..1c5567c --- /dev/null +++ b/std/net.pk @@ -0,0 +1 @@ +## std.net: A cross-platform implementation of the Sockets API. diff --git a/std/os.pk b/std/os.pk new file mode 100644 index 0000000..c28796f --- /dev/null +++ b/std/os.pk @@ -0,0 +1,10 @@ +## std.os: OS-specific functions, as well as some general abstractions. + +pub func exit(code: uint = 0): never = + ... + +pub func sleep(milliseconds: uint) = + ... + +pub func admin: bool = + ... diff --git a/std/magic/arrays.pk b/std/prelude/arrays.pk index a0207c1..2e4a4d4 100644 --- a/std/magic/arrays.pk +++ b/std/prelude/arrays.pk @@ -1,5 +1,5 @@ ## std.arrays: The array[T, S] primitive and associated functions. -## A stub module for documentation. +## A stub module for documentation. Mostly compiler magic. ## Primitive fixed-size arrays. Their size is statically known at compile-time. pub type array[T, S: static[uint]] diff --git a/std/magic/booleans.pk b/std/prelude/booleans.pk index 3c7e28e..bd81925 100644 --- a/std/magic/booleans.pk +++ b/std/prelude/booleans.pk @@ -1,5 +1,5 @@ ## std.booleans: Booleans and related types. Mostly compiler magic. -## A stub module for documentation. +## A stub module for documentation. Mostly compiler magic. pub type unit = union[sole] pub type bool = union[false, true] diff --git a/std/prelude/debug.pk b/std/prelude/debug.pk new file mode 100644 index 0000000..3af1def --- /dev/null +++ b/std/prelude/debug.pk @@ -0,0 +1,32 @@ +## std.debug: Useful functions for debugging. +## This module is imported by default. + +## The special ... syntax is used to mark unimplemented parts of code. +## Such code will compile, but panic upon being called at runtime. +## It is usable almost anywhere, including in type declarations, thanks to compiler magic. +pub func ...: never + +## The `assert` macro checks that a provided assertation is true, +## and panics and dumps information if it is not. +## Asserts remain in release builds. If not desired, see `debug_assert` +pub macro assert(cond: bool) = + quote: + if not `cond`: + panic "assertation failed!\n {}".fmt(dbg(`cond`)) + +## The `debug_assert` function provides an assert that is compiled out in release builds. +## This is useful for debugging performance-critical code. +pub macro debug_assert(cond: bool) + quote: + when debug: # fixme: where is this coming from? + assert `cond` + +## The `discard` function consumes any type. +## Useful for throwing away the result of a computation. +pub func discard[T](self: T) = + return + +## The `panic` function prints a message to `stderr` and quits. +pub func panic(message: str): never = + stderr.write(message, "\n") + std.os.exit(1) diff --git a/std/prelude/io.pk b/std/prelude/io.pk index e621e01..7a3f059 100644 --- a/std/prelude/io.pk +++ b/std/prelude/io.pk @@ -1,5 +1,7 @@ -## std.io: Platform-independent I/O constructs. +## std.io: Platform-independent I/O constructs. Mostly revolve around files. ## This module is imported by default. +# reference: https://nim-lang.org/docs/syncio.html +# reference: https://doc.rust-lang.org/stable/std/io/ pub type File = ... pub type FileMode = union[Read, Write, ReadWrite, Append] @@ -13,7 +15,7 @@ pub const stderr: File = ... pub func open(path: str, mode: FileMode): File = ... -pub func close(file: File) = +pub func close(file: owned File) = ... pub func write(file: File, values: varargs[str]) = @@ -27,3 +29,18 @@ pub func lines(file: File): Iter[str] = pub func chars(file: File): Iter[chr] = ... + +pub func exists(file: File): bool = + ... + +pub func hidden(file: File): bool = + ... + +pub func size(file: File): uint = + ... + +pub func dir(file: File): str = + ... + +pub func path(file: File): str = + ... diff --git a/std/magic/numbers.pk b/std/prelude/numbers.pk index 007f4b7..7857d34 100644 --- a/std/magic/numbers.pk +++ b/std/prelude/numbers.pk @@ -1,5 +1,5 @@ ## std.numbers: Operations on numbers and such. -## A stub module for documentation. +## A stub module for documentation. Mostly compiler magic. # reference: https://en.wikipedia.org/wiki/IEEE_754 ## The default integer type. Size depends on architecture. diff --git a/std/magic/pointers.pk b/std/prelude/pointers.pk index e2a95cf..e2a95cf 100644 --- a/std/magic/pointers.pk +++ b/std/prelude/pointers.pk diff --git a/std/prelude/results.pk b/std/prelude/results.pk index 7dc5a11..81bccf0 100644 --- a/std/prelude/results.pk +++ b/std/prelude/results.pk @@ -1,7 +1,7 @@ ## std.results: Result types. ## This module is imported by default. -use std[options, format] +use std.[options, format] ## The Result type. Represents either success or failure. pub type Result[T, E] = union diff --git a/std/prelude/strings.pk b/std/prelude/strings.pk index 6abca31..2f48b67 100644 --- a/std/prelude/strings.pk +++ b/std/prelude/strings.pk @@ -89,7 +89,7 @@ pub func chars(self: str): list[chr] # todo: useful? pub func &(a: str, b: str): str = ... -## Syntatic sugar for string appending. +## Syntactic sugar for string appending. pub func &=(a: mut str, b: owned str) = a.push(b) @@ -98,3 +98,4 @@ pub func &=(a: mut str, b: owned str) = # pub func iter(self: str): StrIter # pub func next(self: mut StrIter) # pub func peek(self: mut StrIter) +# pub func peek_nth(self: mut StrIter, i: uint) diff --git a/std/rand.pk b/std/rand.pk new file mode 100644 index 0000000..f608a78 --- /dev/null +++ b/std/rand.pk @@ -0,0 +1 @@ +## std.rand: Functions for randomness diff --git a/std/threads.pk b/std/threads.pk new file mode 100644 index 0000000..2b1e98f --- /dev/null +++ b/std/threads.pk @@ -0,0 +1 @@ +## std.threads: Primitives for threads and thread-safe code. |