Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 6 additions & 2 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,9 +12,12 @@ 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 =
///
Comment thread
jfecher marked this conversation as resolved.
Resolved
/// This will Fail if the string already contains any '\0' bytes.
from_string (s: Std.Prelude.String): String can Fail =
length = Usz s.length
assert (all (chars (clone s)) (_ != '\0'))
Comment thread
FlameyosSnowy marked this conversation as resolved.
Resolved

buffer: Ptr Char = malloc (length + 1)
memcpy buffer s.data length
ptr_store (offset buffer length) '\0'
Expand Down
2 changes: 2 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,7 @@ read_file_bytes (path: String): Vec U8 can Fs, Fail =
fail_if read_error
Vec data len capacity

/// Read the file with the path `path` and gives an owned String.
Comment thread
jfecher marked this conversation as resolved.
Resolved
read_file (path: String): String can Fs, Fail =
bytes = read_file_bytes path
fail_if (bytes.len > 0xFFFF_FFFF)
Expand Down
9 changes: 9 additions & 0 deletions stdlib/src/Math.an
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ 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 (Greatest Common Divisor)
/// For example: take the 2 numbers 8 and 12. When you divide them, they can both be divided by 1 (which is excluded, as every integer can be divided by 1), 2, and 4.
///
/// The number 8 cannot be divided by 3 (like 12) and 12 cannot be divided by 8 (like 8), but they can both be divided by 4, and that's the biggest number they can both be divided by and be given an integer.
/// Therefore; the GCD (Greatest Common Divisor) is 4.
Comment thread
jfecher marked this conversation as resolved.
Resolved
gcd (a: t) (b: t) {n: Num t} {Mod t} {Copy t}: t =
(==) = n.eq.(==)
var x = abs a
Expand All @@ -61,6 +66,10 @@ gcd (a: t) (b: t) {n: Num t} {Mod t} {Copy t}: t =
y := remainder
x

/// Finds the LCM (Least Common Multiple), meaning the smallest number that is divisible by each of the two given numbers. This is the inverse of `gcd` (Greatest Common Divisor)
Comment thread
jfecher marked this conversation as resolved.
Resolved
/// For example: take the 2 numbers 4 and 6, the smallest number that is dividable by 4 is 4, but 6 cannot divide by 4 (and be an integer), the second smallest number dividable by 4 is 8, but 6 also cannot divide by 8.
///
/// The third smallest number dividable by 4 is 12, which can be divided by 12 (resulting in 2). Therefore, 12 is the LCM (Least Common Multiple) of the 2 numbers.
Comment thread
jfecher marked this conversation as resolved.
Resolved
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
6 changes: 6 additions & 0 deletions stdlib/src/Sync/Mutex.an
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ 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 which can be created using the `of` method.
Comment thread
jfecher marked this conversation as resolved.
Resolved
///
/// When locked using `with_lock` and then other threads try to acquire the lock, the other threads will block until the acquired lock has been released by the thread.
Comment thread
jfecher marked this conversation as resolved.
Resolved
///
/// # Poisoning
/// Until further notice, the `Mutex` type has no logic for poisoning. Meaning if the thread that holds this lock has failed in any way where the thread cannot continue, the protected data may be in a partially written state and therefore be logically corrupt.
Comment thread
jfecher marked this conversation as resolved.
Resolved
type Mutex t =
// Actually a pthread_mutex_t
raw: VoidPtr
Expand Down
Loading