Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions stdlib/src/C.an
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Std.Mem.transmute, deref_ptr
import Std.Stream.all
export String, copy_cstring, from_string, puts, putchar, getchar, exit, system, strlen,
malloc, calloc, realloc, free, memcpy, memcmp,
File, copy_file, fopen, fclose, fputs, fputc, fwrite,
Expand All @@ -11,12 +12,21 @@ type String = ptr: Ptr Char
impl copy_cstring: Copy String with (.*) = deref

/// Copy a prelude string, append a '\0', and return it as a new C.String
// TODO: Fail if the string already contains any '\0' bytes
from_string (s: Std.Prelude.String): String =
/// This always allocates, even if the original string already has a null terminator.
/// This is so the returned C.String can always be freed separately regardless of how it was created.
/// This will Fail if the string already contains any '\0' bytes.
from_string (s: Std.Prelude.String): String can Fail =
length = Usz s.length
buffer: Ptr Char = malloc (length + 1)

has_null_terminator = length > 0 and s.data.[length - 1] == '\0'
content_length = if has_null_terminator then length - 1 else length
assert (all (0..content_length) fn i -> s.data.[i] != '\0')

buffer: Ptr Char = malloc (content_length + 1)
memcpy buffer s.data length
ptr_store (offset buffer length) '\0'
if not has_null_terminator then
ptr_store (offset buffer length) '\0'

String buffer

/// A convenience type for translating C APIs
Expand Down
3 changes: 3 additions & 0 deletions stdlib/src/IO.an
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ read_line (): String can Fs =
array_insert buffer len '\0'
String.from_parts buffer (U32 len)

/// Read a line from stdin and gives a vector of bytes.
read_file_bytes (path: String): Vec U8 can Fs, Fail =
file = File.open path ReadBinary
var capacity = 4096usz
Expand All @@ -42,6 +43,8 @@ read_file_bytes (path: String): Vec U8 can Fs, Fail =
fail_if read_error
Vec data len capacity

/// Read the entire file at `path` into a String.
/// Fails if the file can't be read or its contents aren't UTF-8.
read_file (path: String): String can Fs, Fail =
bytes = read_file_bytes path
fail_if (bytes.len > 0xFFFF_FFFF)
Expand Down
7 changes: 7 additions & 0 deletions stdlib/src/Math.an
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ abs (x: t) {n: Num t} {Copy t}: t =
(-) = n.sub.(-)
if x < n.zero then n.zero - x else x

/// Finds the largest positive integer that divides two or more integers without a remainder, this is the Greatest Common Divisor.
/// For example, take the two numbers 8 and 12. When you divide them, they can both be divided by 1, 2, and 4. The greatest of these is 4,
/// therefore, `gcd 8 12 == 4`. This is the same as finding the highest common factor shared by both integers.
gcd (a: t) (b: t) {n: Num t} {Mod t} {Copy t}: t =
(==) = n.eq.(==)
var x = abs a
Expand All @@ -61,6 +64,10 @@ gcd (a: t) (b: t) {n: Num t} {Mod t} {Copy t}: t =
y := remainder
x

/// Finds the least common multiple (LCM) between two numbers. This is the smallest number that is divisible by both numbers. This is the inverse of the greatest common divisor (GCD).
/// For example, take the two numbers 4 and 6. The smallest multiple of 4 is itself, but 6 cannot fit into 4. The second smallest multiple of 4 is 8, but 6 cannot fit into 8 either, nor can its first multiple of 12.
///
/// The third smallest multiple of 4 is 12, which is also a multiple of 6. Therefore, `lcm 4 6 == 12`.
lcm (a: t) (b: t) {n: Num t} {Mod t} {Copy t}: t =
(==) = n.eq.(==)
(/) = n.div.(/)
Expand Down
3 changes: 1 addition & 2 deletions stdlib/src/String.an
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ is_valid_utf8_bytes (data: Ptr U8) (len: Usz): Bool =
// below can never go out of bounds because:
// - `b` is a U8, so `table[b]` indexes at most 255
// - Every byte-class entry is <= 0xb, so `utf8_type` < 16
// - Every transition entry is in 0..=8, and `state` starts at
// accept=0, so `state` is always in 0..=8
// - Every transition entry is in 0..=8, and `state` starts at accept=0, so `state` is always in 0..=8
// - The largest index is therefore 256 + 8 * 16 + 11 = 395 < 400
utf8_type = Usz (array_get_unchecked table (Usz b) |> deref)
state := Usz (array_get_unchecked table (256 + state * 16 + utf8_type) |> deref)
Expand Down
1 change: 1 addition & 0 deletions stdlib/src/Sync/Arc.an
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type ArcInner t =
count: Atomic U32
value: t

/// A thread-safe version of the `Rc` reference counted pointer.
type Arc t = ptr: Ptr (ArcInner t)

of (value: t): Arc t =
Expand Down
5 changes: 5 additions & 0 deletions stdlib/src/Sync/Mutex.an
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ pthread_mutex_destroy: fn VoidPtr -> I32 = extern "pthread_mutex_destroy"
// TODO: Remove hard-coded sizes
mutex_buf_size: Usz = 64

/// A mutual exclusion primitive that is useful for protecting data shared between threads. Can be created using `Mutex.of`.
///
/// When locked (e.g. via `with_lock`) other threads trying to acquire the lock will block until the lock has been released by the original thread.
///
/// If the thread that locked this mutex has aborted without releasing the lock, it may leave the mutex locked, potentially continuing to block other threads forever.
type Mutex t =
// Actually a pthread_mutex_t
raw: VoidPtr
Expand Down
Loading