-RELEASE=1
+RELEASE=0
TIME=0
OBJ_FILES=\
-package core
+package core.allocator
#include_file "core/memory"
#include_file "core/intrinsics"
+use package core { memory_size, memory_grow }
+use package core.memory as memory
+
heap_allocator : Allocator;
#private
}
new_ptr := heap_alloc(new_size, align);
- memory_copy(new_ptr, ptr, old_size);
+ memory.copy(new_ptr, ptr, old_size);
heap_free(ptr);
return new_ptr;
}
}
-
-memory_init :: proc () {
- heap_init();
-}
-package core
+package core.array
// ---------------------------------
// Dynamic Arrays
// ---------------------------------
-array_init :: proc (arr: ^[..] $T, initial_cap := 4) {
+init :: proc (arr: ^[..] $T, initial_cap := 4) {
arr.count = 0;
arr.capacity = initial_cap;
arr.data = calloc(sizeof T * arr.capacity);
}
-array_free :: proc (arr: ^[..] $T) {
+free :: proc (arr: ^[..] $T) {
arr.count = 0;
arr.capacity = 0;
arr.data = null;
}
-array_clear :: proc (arr: ^[..] $T) {
+clear :: proc (arr: ^[..] $T) {
arr.count = 0;
}
-array_ensure_capacity :: proc (arr: ^[..] $T, cap: u32) {
+ensure_capacity :: proc (arr: ^[..] $T, cap: u32) {
if arr.capacity >= cap do return;
while cap > arr.capacity do arr.capacity <<= 1;
arr.data = cresize(arr.data, sizeof T * arr.capacity);
}
-array_push :: proc (arr: ^[..] $T, x: T) {
- array_ensure_capacity(arr, arr.count + 1);
+push :: proc (arr: ^[..] $T, x: T) {
+ ensure_capacity(arr, arr.count + 1);
arr.data[arr.count] = x;
arr.count += 1;
}
-array_insert :: proc (arr: ^[..] $T, idx: u32, x: T) {
- array_ensure_capacity(arr, arr.count + 1);
+insert :: proc (arr: ^[..] $T, idx: u32, x: T) {
+ ensure_capacity(arr, arr.count + 1);
arr.count += 1;
while i := arr.count; i > idx {
arr.data[idx] = x;
}
-array_remove :: proc (arr: ^[..] $T, elem: T) {
+remove :: proc (arr: ^[..] $T, elem: T) {
move := 0;
for i: 0 .. arr.count - move {
arr.count -= move;
}
-array_delete :: proc (arr: ^[..] $T, idx: u32) {
+delete :: proc (arr: ^[..] $T, idx: u32) {
if idx >= arr.count do return;
for i: idx .. arr.count - 1 {
arr.count -= 1;
}
-array_fast_delete :: proc (arr: ^[..] $T, idx: u32) {
+fast_delete :: proc (arr: ^[..] $T, idx: u32) {
if idx >= arr.count do return;
arr.data[idx] = arr.data[arr.count - 1];
arr.count -= 1;
}
-array_contains :: proc (arr: ^[..] $T, x: T) -> bool {
+contains :: proc (arr: ^[..] $T, x: T) -> bool {
for it: *arr do if it == x do return true;
return false;
}
-array_pop :: proc (arr: ^[..] $T) -> T {
+pop :: proc (arr: ^[..] $T) -> T {
arr.count -= 1;
return arr.data[arr.count];
}
-array_average :: proc (arr: ^[..] $T) -> T {
+average :: proc (arr: ^[..] $T) -> T {
sum := cast(T) 0;
for it: *arr do sum += it;
return sum / cast(T) arr.count;
}
-array_to_slice :: proc (arr: ^[..] $T) -> [] T {
+to_slice :: proc (arr: ^[..] $T) -> [] T {
return arr.data[0 .. arr.count];
}
** Simple insertion sort
** cmp should return >0 if left > right
*/
-array_sort :: proc (arr: ^[..] $T, cmp: proc (T, T) -> i32) {
+sort :: proc (arr: ^[..] $T, cmp: proc (T, T) -> i32) {
for i: 1 .. arr.count {
x := arr.data[i];
j := i - 1;
}
}
-array_fold :: proc (arr: ^[..] $T, init: $R, f: proc (T, R) -> R) -> R {
+fold :: proc (arr: ^[..] $T, init: $R, f: proc (T, R) -> R) -> R {
val := init;
for it: *arr do val = f(it, val);
return val;
}
-array_map :: proc (arr: ^[..] $T, data: $R, f: proc (T, R) -> T) {
+map :: proc (arr: ^[..] $T, data: $R, f: proc (T, R) -> T) {
for ^it: *arr do *it = f(*it, data);
}
-package core_file
+package core.file
// Many of these functions will be improved when
// multiple return values are implemented.
fd : FileDescriptor;
}
-file_open :: proc (file: ^File, path: string, mode := OpenMode.Read, flags := FDFlags.Sync) -> bool {
+open :: proc (file: ^File, path: string, mode := OpenMode.Read, flags := FDFlags.Sync) -> bool {
// Currently the directory's file descriptor appears to always be 3
DIR_FD :: 3;
return true;
}
-file_close :: proc (file: File) -> bool {
+close :: proc (file: File) -> bool {
if fd_close(file.fd) != Errno.Success {
return false;
}
return true;
}
-file_get_size :: proc (file: File) -> u64 {
+get_size :: proc (file: File) -> u64 {
fs: FileStat;
if fd_filestat_get(file.fd, ^fs) != Errno.Success do return 0l;
return fs.size;
}
-file_get_contents_from_file :: proc (file: File) -> string {
- size := cast(u32) file_get_size(file);
+get_contents_from_file :: proc (file: File) -> string {
+ size := cast(u32) get_size(file);
data := cast(^u8) alloc(context.allocator, size);
return data[0 .. size];
}
-file_get_contents :: proc {
- file_get_contents_from_file,
+get_contents :: proc {
+ get_contents_from_file,
proc (path: string) -> string {
tmp_file: File;
- if !file_open(^tmp_file, path, OpenMode.Read) do return "";
- defer file_close(tmp_file);
+ if !open(^tmp_file, path, OpenMode.Read) do return "";
+ defer close(tmp_file);
- return file_get_contents(tmp_file);
+ return get_contents(tmp_file);
}
}
-package core
+package core.i32map
+
+use package core.array as array
I32Map :: struct ($T) {
hashes : [..] i32;
value : T;
}
-i32map_init :: proc (use imap: ^I32Map($T), hash_count: i32 = 16) {
- array_init(^hashes, hash_count);
- array_init(^entries, 4);
+init :: proc (use imap: ^I32Map($T), hash_count: i32 = 16) {
+ array.init(^hashes, hash_count);
+ array.init(^entries, 4);
- for i: 0 .. hash_count do array_push(^hashes, -1);
+ for i: 0 .. hash_count do array.push(^hashes, -1);
}
-i32map_free :: proc (use imap: ^I32Map($T)) {
- array_free(^hashes);
- array_free(^entries);
+free :: proc (use imap: ^I32Map($T)) {
+ array.free(^hashes);
+ array.free(^entries);
}
-i32map_put :: proc (use imap: ^I32Map($T), key: i32, value: T) {
- lr := i32map_lookup(imap, key);
+put :: proc (use imap: ^I32Map($T), key: i32, value: T) {
+ lr := lookup(imap, key);
if lr.entry_index >= 0 {
entries[lr.entry_index].value = value;
entry.value = value;
entry.next = hashes[lr.hash_index];
- array_push(^entries, entry);
+ array.push(^entries, entry);
hashes[lr.hash_index] = entries.count - 1;
}
-i32map_has :: proc (use imap: ^I32Map($T), key: i32) -> bool {
- lr := i32map_lookup(imap, key);
+has :: proc (use imap: ^I32Map($T), key: i32) -> bool {
+ lr := lookup(imap, key);
return lr.entry_index >= 0;
}
-i32map_get :: proc (use imap: ^I32Map($T), key: i32, default := cast(T) 0) -> T {
- lr := i32map_lookup(imap, key);
+get :: proc (use imap: ^I32Map($T), key: i32, default := cast(T) 0) -> T {
+ lr := lookup(imap, key);
if lr.entry_index >= 0 do return entries[lr.entry_index].value;
return default;
}
-i32map_delete :: proc (use imap: ^I32Map($T), key: i32) {
- lr := i32map_lookup(imap, key);
+delete :: proc (use imap: ^I32Map($T), key: i32) {
+ lr := lookup(imap, key);
if lr.entry_index < 0 do return;
if lr.entry_prev < 0 do hashes[lr.hash_index] = entries[lr.entry_index].next;
else do entries[lr.entry_prev].next = entries[lr.entry_index].next;
if lr.entry_index == entries.count - 1 {
- array_pop(^entries);
+ array.pop(^entries);
return;
}
- array_fast_delete(^entries, lr.entry_index);
- last := i32map_lookup(imap, entries[lr.entry_index].key);
+ array.fast_delete(^entries, lr.entry_index);
+ last := lookup(imap, entries[lr.entry_index].key);
if last.entry_prev >= 0 do entries[last.entry_prev].next = lr.entry_index;
else do hashes[last.hash_index] = lr.entry_index;
}
-i32map_clear :: proc (use imap: ^I32Map($T)) {
+clear :: proc (use imap: ^I32Map($T)) {
for i: 0 .. hashes.count do hashes.data[i] = -1;
entries.count = 0;
}
}
#private_file
-i32map_lookup :: proc (use imap: ^I32Map($T), key: i32) -> I32MapLookupResult {
+lookup :: proc (use imap: ^I32Map($T), key: i32) -> I32MapLookupResult {
lr := I32MapLookupResult.{};
hash := cast(u32) 0xcbf29ce7 ^ cast(u32) key;
-package core
+package core.math
+
+use package core {
+ sqrt_f32, sqrt_f64
+}
PI :: 3.14159265f;
TAU :: 6.28318330f;
return res;
}
-max_poly :: proc (a: $T, b: T) -> T {
+max :: proc (a: $T, b: T) -> T {
if a >= b do return a;
return b;
}
-min_poly :: proc (a: $T, b: T) -> T {
+min :: proc (a: $T, b: T) -> T {
if a <= b do return a;
return b;
}
-sqrt_i32 :: proc (x: i32) -> i32 {
- return ~~sqrt_f32(~~x);
-}
+sqrt_i32 :: proc (x: i32) -> i32 do return ~~sqrt_f32(~~x);
+sqrt_i64 :: proc (x: i64) -> i64 do return ~~sqrt_f64(~~x);
+sqrt :: proc { sqrt_f32, sqrt_f64, sqrt_i32, sqrt_i64 }
-package core
+package core.memory
-memory_copy :: proc (dst_: rawptr, src_: rawptr, len: u32) {
+copy :: proc (dst_: rawptr, src_: rawptr, len: u32) {
dst := cast(^u8) dst_;
src := cast(^u8) src_;
for i: 0 .. len do dst[i] = src[i];
}
-memory_set :: proc (start: rawptr, length: u32, value: u8) {
+set :: proc (start: rawptr, length: u32, value: u8) {
s := cast(^u8) start;
for i: 0 .. length do s[i] = value;
}
-package core
+package core.ptrmap
+use package core.array as array
PtrMap :: struct {
hashes : [..] i32;
next : i32;
}
-ptrmap_init :: proc (use pmap: ^PtrMap, hash_count: i32 = 16) {
- array_init(^hashes, hash_count);
- array_init(^entries, 4);
+init :: proc (use pmap: ^PtrMap, hash_count: i32 = 16) {
+ array.init(^hashes, hash_count);
+ array.init(^entries, 4);
- for i: 0 .. hash_count do array_push(^hashes, -1);
+ for i: 0 .. hash_count do array.push(^hashes, -1);
}
-ptrmap_free :: proc (use pmap: ^PtrMap) {
- array_free(^hashes);
- array_free(^entries);
+free :: proc (use pmap: ^PtrMap) {
+ array.free(^hashes);
+ array.free(^entries);
}
-ptrmap_put :: proc (use pmap: ^PtrMap, key: rawptr, value: rawptr) {
- lr := ptrmap_lookup(pmap, key);
+put :: proc (use pmap: ^PtrMap, key: rawptr, value: rawptr) {
+ lr := lookup(pmap, key);
if lr.entry_index >= 0 {
entries[lr.entry_index].value = value;
return;
}
- array_push(^entries, PtrMapEntry.{
+ array.push(^entries, PtrMapEntry.{
key = key,
value = value,
next = hashes[lr.hash_index],
hashes[lr.hash_index] = entries.count - 1;
}
-ptrmap_has :: proc (use pmap: ^PtrMap, key: rawptr) -> bool {
- lr := ptrmap_lookup(pmap, key);
+has :: proc (use pmap: ^PtrMap, key: rawptr) -> bool {
+ lr := lookup(pmap, key);
return lr.entry_index >= 0;
}
-ptrmap_get :: proc (use pmap: ^PtrMap, key: rawptr) -> rawptr {
- lr := ptrmap_lookup(pmap, key);
+get :: proc (use pmap: ^PtrMap, key: rawptr) -> rawptr {
+ lr := lookup(pmap, key);
if lr.entry_index >= 0 do return entries[lr.entry_index].value;
return null;
}
-ptrmap_delete :: proc (use pmap: ^PtrMap, key: rawptr) {
- lr := ptrmap_lookup(pmap, key);
+delete :: proc (use pmap: ^PtrMap, key: rawptr) {
+ lr := lookup(pmap, key);
if lr.entry_index < 0 do return;
if lr.entry_prev < 0 do hashes[lr.hash_index] = entries[lr.entry_index].next;
else do entries[lr.entry_prev].next = entries[lr.entry_index].next;
if lr.entry_index == entries.count - 1 {
- array_pop(^entries);
+ array.pop(^entries);
return;
}
- array_fast_delete(^entries, lr.entry_index);
- last := ptrmap_lookup(pmap, entries[lr.entry_index].key);
+ array.fast_delete(^entries, lr.entry_index);
+ last := lookup(pmap, entries[lr.entry_index].key);
if last.entry_prev >= 0 do entries[last.entry_prev].next = lr.entry_index;
else do hashes[last.hash_index] = lr.entry_index;
}
-ptrmap_clear :: proc (use pmap: ^PtrMap) {
+clear :: proc (use pmap: ^PtrMap) {
for i: 0 .. hashes.count do hashes.data[i] = -1;
entries.count = 0;
}
}
#private_file
-ptrmap_lookup :: proc (use pmap: ^PtrMap, key: rawptr) -> PtrMapLookupResult {
+lookup :: proc (use pmap: ^PtrMap, key: rawptr) -> PtrMapLookupResult {
lr := PtrMapLookupResult.{};
hash := cast(u32) 0xcbf29ce7 ^ cast(u32) key;
-package core
+package core.random
#private_file seed := 8675309
#private_file RANDOM_INCREMENT :: 1013904223
// #private_file RANDOM_MODULUS :: 1 << 32
-random_seed :: proc (s: u32) do seed = s;
-random :: proc (s := ^seed) -> u32 {
+set_seed :: proc (s: u32) do seed = s;
+
+int :: proc (s := ^seed) -> u32 {
*s = *s * RANDOM_MULTIPLIER + RANDOM_INCREMENT;
return *s;
}
-random_between :: proc (lo: i32, hi: i32) -> i32 do return random() % (hi + 1 - lo) + lo;
+between :: proc (lo: i32, hi: i32) -> i32 do return int () % (hi + 1 - lo) + lo;
-random_float :: proc (lo := 0.0f, hi := 1.0f) -> f32 {
- return (cast(f32) (random() % (1 << 20)) / cast(f32) (1 << 20)) * (hi - lo) + lo;
+float :: proc (lo := 0.0f, hi := 1.0f) -> f32 {
+ return (cast(f32) (int() % (1 << 20)) / cast(f32) (1 << 20)) * (hi - lo) + lo;
}
// of the system package
use package system as system
-#private_file print_buffer : StringBuilder;
+#private_file print_buffer : str.StringBuilder;
stdio_init :: proc () {
- print_buffer = string_builder_make(2048);
+ print_buffer = str.builder_make(2048);
}
print_string :: proc (s: string) {
- string_builder_append(^print_buffer, s);
+ str.builder_append(^print_buffer, s);
if s.data[s.count - 1] == #char "\n" do print_buffer_flush();
}
-print_cstring :: proc (s: cstring) do string_builder_append(^print_buffer, s);
-print_i64 :: proc (n: i64, base := 10l) do string_builder_append(^print_buffer, n, base);
-print_i32 :: proc (n: i32, base := 10) do string_builder_append(^print_buffer, cast(i64) n, cast(u64) base);
-print_f64 :: proc (n: f64) do string_builder_append(^print_buffer, n);
-print_f32 :: proc (n: f32) do string_builder_append(^print_buffer, cast(f64) n);
-print_bool :: proc (b: bool) do string_builder_append(^print_buffer, b);
-print_ptr :: proc (p: ^void) do string_builder_append(^print_buffer, cast(i64) p, 16l);
+print_cstring :: proc (s: cstring) do str.builder_append(^print_buffer, s);
+print_i64 :: proc (n: i64, base := 10l) do str.builder_append(^print_buffer, n, base);
+print_i32 :: proc (n: i32, base := 10) do str.builder_append(^print_buffer, cast(i64) n, cast(u64) base);
+print_f64 :: proc (n: f64) do str.builder_append(^print_buffer, n);
+print_f32 :: proc (n: f32) do str.builder_append(^print_buffer, cast(f64) n);
+print_bool :: proc (b: bool) do str.builder_append(^print_buffer, b);
+print_ptr :: proc (p: ^void) do str.builder_append(^print_buffer, cast(i64) p, 16l);
print_range :: proc (r: range, sep := " ") {
for i: r {
if !vararg_get(va, ^n) do return;
ibuf : [128] u8;
- istr := i64_to_string(~~n, 10l, Buffer.{ ~~ibuf, 128 });
+ istr := str.i64_to_string(~~n, 10l, Buffer.{ ~~ibuf, 128 });
for a: istr {
buffer[len] = a;
if !vararg_get(va, ^n) do return;
ibuf : [128] u8;
- istr := i64_to_string(n, 10l, Buffer.{ ~~ibuf, 128 });
+ istr := str.i64_to_string(n, 10l, Buffer.{ ~~ibuf, 128 });
for a: istr {
buffer[len] = a;
if !vararg_get(va, ^n) do return;
fbuf : [128] u8;
- fstr := f64_to_string(~~n, fbuf[0 .. 128]);
+ fstr := str.f64_to_string(~~n, fbuf[0 .. 128]);
for a: fstr {
buffer[len] = a;
if !vararg_get(va, ^n) do return;
fbuf : [128] u8;
- fstr := f64_to_string(n, fbuf[0 .. 128]);
+ fstr := str.f64_to_string(n, fbuf[0 .. 128]);
for a: fstr {
buffer[len] = a;
if !vararg_get(va, ^n) do return;
ibuf : [128] u8;
- istr := i64_to_string(~~n, 16l, Buffer.{ ~~ibuf, 128 });
+ istr := str.i64_to_string(~~n, 16l, Buffer.{ ~~ibuf, 128 });
for a: istr {
buffer[len] = a;
if print_buffer.len == 0 do return;
^print_buffer
- |> string_builder_to_string()
+ |> str.builder_to_string()
|> system.output_string();
- ^print_buffer |> string_builder_clear();
+ ^print_buffer |> str.builder_clear();
}
-package core
+package core.str
-string_make :: proc (s: cstring) -> string {
- len :: string_length(s);
+make :: proc (s: cstring) -> string {
+ len :: length(s);
return string.{ count = len, data = s };
}
-string_length :: proc {
+length :: proc {
proc (s: ^u8) -> u32 {
len := 0;
c := s;
},
}
-string_concat :: proc (s1: string, s2: string) -> string {
- len1 :: string_length(s1);
- len2 :: string_length(s2);
+concat :: proc (s1: string, s2: string) -> string {
+ len1 :: length(s1);
+ len2 :: length(s2);
data := cast(^u8) calloc(len1 + len2);
for i: 0 .. len1 do data[i] = s1[i];
return string.{ data, len1 + len2 };
}
-string_free :: proc (s: string) do cfree(s.data);
+free :: proc (s: string) do cfree(s.data);
// This is an example doc string
// You can have as many comments as you want
-// It documents the string_split function
-string_split :: proc (str: string, delim: u8) -> []string {
+// It documents the split function
+split :: proc (str: string, delim: u8) -> []string {
delim_count := 0;
for i: 0 .. str.count do if str[i] == delim do delim_count += 1;
return strarr[0 .. delim_count + 1];
}
-string_substr :: proc (str: string, sub: string) -> string {
+substr :: proc (str: string, sub: string) -> string {
for i: 0 .. str.count {
while j := 0; j < sub.count && str[i + j] == sub[j] {
j += 1;
return str.data[0 .. 0];
}
-string_contains :: proc (str: string, c: u8) -> bool {
+contains :: proc (str: string, c: u8) -> bool {
for ch: str do if ch == c do return true;
return false;
}
-// string_compare :: proc (str1: string, str2: string) -> i32 {
+// compare :: proc (str1: string, str2: string) -> i32 {
// if str1.count != str2.count do return str1.count - str2.count;
//
// i := 0;
// return ~~(str1[i] - str2[i]);
// }
-string_equal :: proc (str1: string, str2: string) -> bool {
+equal :: proc (str1: string, str2: string) -> bool {
if str1.count != str2.count do return false;
while i := 0; i < str1.count {
if str1[i] != str2[i] do return false;
return true;
}
-string_starts_with :: proc (str1: string, str2: string) -> bool {
+starts_with :: proc (str1: string, str2: string) -> bool {
if str1.count < str2.count do return false;
while i := 0; i < str2.count {
if str1[i] != str2[i] do return false;
return true;
}
-string_strip_leading_whitespace :: proc (str: ^string) {
+strip_leading_whitespace :: proc (str: ^string) {
while true do switch str.data[0] {
case #char " ", #char "\t", #char "\n", #char "\r" {
str.data += 1;
}
}
-string_strip_trailing_whitespace :: proc (str: ^string) {
+strip_trailing_whitespace :: proc (str: ^string) {
while true do switch str.data[str.count - 1] {
case #char " ", #char "\t", #char "\n", #char "\r" {
str.count -= 1;
cap : u32 = 0;
}
-string_builder_make :: proc (initial_cap: u32) -> StringBuilder {
+builder_make :: proc (initial_cap: u32) -> StringBuilder {
data: ^u8 = null;
if initial_cap > 0 {
};
}
-string_builder_add_string :: proc (use sb: ^StringBuilder, str: string) -> ^StringBuilder {
+builder_add_string :: proc (use sb: ^StringBuilder, str: string) -> ^StringBuilder {
len_total :: len + str.count;
if cap >= len_total {
return sb;
}
-string_builder_add_cstring :: proc (use sb: ^StringBuilder, cstr: cstring) -> ^StringBuilder {
- s := string_make(cstr);
- return string_builder_add_string(sb, s);
+builder_add_cstring :: proc (use sb: ^StringBuilder, cstr: cstring) -> ^StringBuilder {
+ s := make(cstr);
+ return builder_add_string(sb, s);
}
i64_to_string :: proc (n_: i64, base: u64, buf: Buffer) -> string {
return string.{ buf.data, len };
}
-string_builder_add_i64 :: proc (use sb: ^StringBuilder, n: i64, base := 10l) -> ^StringBuilder {
+builder_add_i64 :: proc (use sb: ^StringBuilder, n: i64, base := 10l) -> ^StringBuilder {
buf : [256] u8;
s := i64_to_string(n, base, Buffer.{ cast(^void) buf, 256 });
- return string_builder_add_string(sb, s);
+ return builder_add_string(sb, s);
}
-string_builder_add_f64 :: proc (use sb: ^StringBuilder, f: f64) -> ^StringBuilder {
+builder_add_f64 :: proc (use sb: ^StringBuilder, f: f64) -> ^StringBuilder {
buf : [256] u8;
s := f64_to_string(f, buf[0 .. 256]);
- return string_builder_add_string(sb, s);
+ return builder_add_string(sb, s);
}
-string_builder_add_bool :: proc (use sb: ^StringBuilder, b: bool) -> ^StringBuilder {
+builder_add_bool :: proc (use sb: ^StringBuilder, b: bool) -> ^StringBuilder {
if b {
- return string_builder_add_string(sb, "true");
+ return builder_add_string(sb, "true");
} else {
- return string_builder_add_string(sb, "false");
+ return builder_add_string(sb, "false");
}
return null;
}
-string_builder_append :: proc {
- string_builder_add_string,
- string_builder_add_cstring,
- string_builder_add_i64,
- string_builder_add_f64,
- string_builder_add_bool,
+builder_append :: proc {
+ builder_add_string,
+ builder_add_cstring,
+ builder_add_i64,
+ builder_add_f64,
+ builder_add_bool,
}
-string_builder_to_string :: proc (use sb: ^StringBuilder) -> string {
+builder_to_string :: proc (use sb: ^StringBuilder) -> string {
return string.{ data, len };
}
-string_builder_clear :: proc (use sb: ^StringBuilder) -> ^StringBuilder {
+builder_clear :: proc (use sb: ^StringBuilder) -> ^StringBuilder {
len = 0;
return sb;
}
-string_read_u32 :: proc (str: ^string, out: ^u32) {
+read_u32 :: proc (str: ^string, out: ^u32) {
n := 0;
- string_strip_leading_whitespace(str);
+ strip_leading_whitespace(str);
while str.data[0] >= #char "0" && str.data[0] <= #char "9" {
n *= 10;
n += cast(u32) (str.data[0] - #char "0");
*out = n;
}
-string_read_char :: proc (str: ^string, out: ^u8) {
+read_char :: proc (str: ^string, out: ^u8) {
*out = str.data[0];
str.data += 1;
str.count -= 1;
}
-string_discard_chars :: proc (str: ^string, char_count := 1) {
+discard_chars :: proc (str: ^string, char_count := 1) {
str.data += char_count;
str.count -= char_count;
}
// Goes up to but not including the closest newline or EOF
-string_read_line :: proc (str: ^string, out: ^string) {
+read_line :: proc (str: ^string, out: ^string) {
out.data = str.data;
out.count = 0;
}
}
-string_advance_line :: proc (str: ^string) {
+advance_line :: proc (str: ^string) {
if str.count == 0 do return;
adv := 0;
str.count -= adv + 1;
}
-string_read :: proc {
- string_read_u32, string_read_char
+read :: proc {
+ read_u32, read_char
}
package system
-
use package core
use package main as main
// The builtin _start proc.
// Sets up everything needed for execution.
proc () #export "_start" {
- memory_init();
+ allocator.heap_init();
- context.allocator = heap_allocator;
- context.temp_allocator = heap_allocator;
+ context.allocator = allocator.heap_allocator;
+ context.temp_allocator = allocator.heap_allocator;
stdio_init();
#include_file "core/wasi"
-
use package wasi
use package core
use package main as main
// The builtin _start proc.
// Sets up everything needed for execution.
proc () #export "_start" {
- memory_init();
+ allocator.heap_init();
- context.allocator = heap_allocator;
- context.temp_allocator = heap_allocator;
+ context.allocator = allocator.heap_allocator;
+ context.temp_allocator = allocator.heap_allocator;
argc : Size;
argv_buf_size : Size;
package wasi
-
-use package core {
- memory_init,
- stdio_init,
- print_buffer_flush,
-
- heap_allocator
-}
-use package main as main
-
Size :: #type u32;
Filesize :: #type u64;
Timestamp :: #type u64;
--- /dev/null
+package test.foo
+
+use package core
+
+print_foo :: proc () do println("Foo!");
#include_file "core/std/wasi"
+#include_file "progs/foo_test"
use package core
-
+use package test { foo as foo_pkg }
Foo :: struct {
data1 : i32;
}
main :: proc (args: [] cstring) {
+ foo_pkg.print_foo();
+
bar := make_bar();
print(sizeof Bar);
static AstEnumType* parse_enum_declaration(OnyxParser* parser);
static AstTyped* parse_top_level_expression(OnyxParser* parser);
static AstNode* parse_top_level_statement(OnyxParser* parser);
+static AstPackage* parse_package_name(OnyxParser* parser);
static void consume_token(OnyxParser* parser) {
if (parser->hit_unexpected_token) return;
AstNode* pack_symbol = make_node(AstNode, Ast_Kind_Symbol);
pack_symbol->token = expect_token(parser, Token_Type_Symbol);
+
+ // CLEANUP: This is just gross.
+ if (parser->curr->type == '.') {
+ consume_token(parser);
+ pack_symbol->token->length += 1;
+
+ while (1) {
+ if (parser->hit_unexpected_token) break;
+
+ OnyxToken* symbol = expect_token(parser, Token_Type_Symbol);
+ pack_symbol->token->length += symbol->length;
+
+ if (parser->curr->type == '.') {
+ pack_symbol->token->length += 1;
+ consume_token(parser);
+ } else {
+ break;
+ }
+ }
+ }
+
upack->package = (AstPackage *) pack_symbol;
if (parser->curr->type == Token_Type_Keyword_As) {
return NULL;
}
+static AstPackage* parse_package_name(OnyxParser* parser) {
+ AstPackage* package_node = make_node(AstPackage, Ast_Kind_Package);
+
+ if (parser->curr->type != Token_Type_Keyword_Package) {
+ Package *package = program_info_package_lookup_or_create(
+ parser->program,
+ "main",
+ parser->program->global_scope,
+ parser->allocator);
+
+ package_node->token = NULL;
+ package_node->package = package;
+ return package_node;
+ }
+
+ char package_name[1024]; // CLEANUP: This could overflow, if someone decides to be dumb
+ // with their package names - brendanfh 2020/12/06
+ package_name[0] = 0;
+ package_node->token = expect_token(parser, Token_Type_Keyword_Package);
+
+ Package *package = NULL;
+
+ while (1) {
+ if (parser->hit_unexpected_token) return package_node;
+
+ OnyxToken* symbol = expect_token(parser, Token_Type_Symbol);
+
+ // This logic will need to accessible elsewhere
+ token_toggle_end(symbol);
+ strncat(package_name, symbol->text, 1023);
+ token_toggle_end(symbol);
+
+ Package *newpackage = program_info_package_lookup_or_create(
+ parser->program,
+ package_name,
+ parser->program->global_scope,
+ parser->allocator);
+
+ if (package != NULL) {
+ AstPackage* pnode = make_node(AstPackage, Ast_Kind_Package);
+ pnode->token = symbol;
+ pnode->package = newpackage;
+
+ symbol_introduce(package->scope, symbol, pnode);
+ }
+
+ package = newpackage;
+
+ if (parser->curr->type == '.') {
+ strncat(package_name, ".", 1023);
+ consume_token(parser);
+ } else {
+ break;
+ }
+ }
+
+ package_node->package = package;
+ return package_node;
+}
}
ParseResults onyx_parse(OnyxParser *parser) {
- if (parser->curr->type == Token_Type_Keyword_Package) {
- consume_token(parser);
+ // if (parser->curr->type == Token_Type_Keyword_Package) {
+ // consume_token(parser);
- OnyxToken* symbol = expect_token(parser, Token_Type_Symbol);
+ // OnyxToken* symbol = expect_token(parser, Token_Type_Symbol);
- token_toggle_end(symbol);
- Package *package = program_info_package_lookup_or_create(
- parser->program,
- symbol->text,
- parser->program->global_scope,
- parser->allocator);
- token_toggle_end(symbol);
+ // token_toggle_end(symbol);
+ // Package *package = program_info_package_lookup_or_create(
+ // parser->program,
+ // symbol->text,
+ // parser->program->global_scope,
+ // parser->allocator);
+ // token_toggle_end(symbol);
- parser->package = package;
+ // parser->package = package;
- } else {
- Package *package = program_info_package_lookup_or_create(
- parser->program,
- "main",
- parser->program->global_scope,
- parser->allocator);
+ // } else {
+ // Package *package = program_info_package_lookup_or_create(
+ // parser->program,
+ // "main",
+ // parser->program->global_scope,
+ // parser->allocator);
- parser->package = package;
- }
+ // parser->package = package;
+ // }
+ parser->package = parse_package_name(parser)->package;
parser->file_scope = scope_create(parser->allocator, parser->package->private_scope, parser->tokenizer->tokens[0].pos);
AstUsePackage* implicit_use_builtin = make_node(AstUsePackage, Ast_Kind_Use_Package);
}
soa_init :: proc (s: ^SOA) {
- array_init(^s.a);
- array_init(^s.b);
- array_init(^s.c);
+ array.init(^s.a);
+ array.init(^s.b);
+ array.init(^s.c);
}
soa_deinit :: proc (s: ^SOA) {
- array_free(^s.a);
- array_free(^s.b);
- array_free(^s.c);
+ array.free(^s.a);
+ array.free(^s.b);
+ array.free(^s.c);
}
get_range :: proc (arr: ^[..] $T) -> range {
print_arr_details(^s.b);
for i: 0 .. 100 {
- array_push(^s.a, (5 * i) % 21);
- array_push(^s.b, 3l * cast(i64) i);
- array_push(^s.c, Vec3.{ i, i * i, i * i * i });
+ array.push(^s.a, (5 * i) % 21);
+ array.push(^s.b, 3l * cast(i64) i);
+ array.push(^s.c, Vec3.{ i, i * i, i * i * i });
}
r := ^s.a |> get_range() |> by(3);
print(r);
print_array(^s.a);
print("A has 22? ");
- println(array_contains(^s.a, 22));
+ println(array.contains(^s.a, 22));
// NOTE: Iterating by value
for vec: s.c {
print("\n");
- array_sort(^s.a, cmp_dec);
- array_sort(^s.b, cmp_dec);
+ array.sort(^s.a, cmp_dec);
+ array.sort(^s.b, cmp_dec);
print_array(^s.a);
print_array(^s.b);
print_arr_details(^s.b);
print("Array A sum: ");
- println(array_fold(^s.a, 0, proc (x: i32, acc: i32) -> i32 do return x + acc;));
+ println(array.fold(^s.a, 0, proc (x: i32, acc: i32) -> i32 do return x + acc;));
print("\n");
- map : PtrMap;
- ptrmap_init(^map, 50);
- defer ptrmap_free(^map);
+ map : ptrmap.PtrMap;
+ ptrmap.init(^map, 50);
+ defer ptrmap.free(^map);
- for i: 0 .. 100 do ptrmap_put(^map, ^s.a[i], ^s.b[i]);
+ for i: 0 .. 100 do ptrmap.put(^map, ^s.a[i], ^s.b[i]);
print("Has ^a[20]? ");
- println(ptrmap_has(^map, ^s.a[20]));
+ println(ptrmap.has(^map, ^s.a[20]));
print("Has null? ");
- println(ptrmap_has(^map, null));
+ println(ptrmap.has(^map, null));
print("Value at ^a[50]: ");
- print(cast(^void) ptrmap_get(^map, ^s.a[50]));
+ print(cast(^void) ptrmap.get(^map, ^s.a[50]));
print(" == ");
println(cast(^void) (^s.b[50]));
println("Deleteing ^a[20]");
- ptrmap_delete(^map, ^s.a[20]);
+ ptrmap.delete(^map, ^s.a[20]);
print("Has ^a[20]? ");
- println(ptrmap_has(^map, ^s.a[20]));
+ println(ptrmap.has(^map, ^s.a[20]));
}
use package core
main :: proc (args: [] cstring) {
- imap : I32Map(string);
- i32map_init(^imap);
+ imap : i32map.I32Map(string);
+ i32map.init(^imap);
defer {
print("Freeing map\n");
- i32map_free(^imap);
+ i32map.free(^imap);
}
- i32map_put(^imap, 50, "Hello ");
- i32map_put(^imap, 1234, "World!");
+ i32map.put(^imap, 50, "Hello ");
+ i32map.put(^imap, 1234, "World!");
- println(i32map_has(^imap, 50));
- println(i32map_has(^imap, 51));
+ println(i32map.has(^imap, 50));
+ println(i32map.has(^imap, 51));
- printf("%s%s\n", i32map_get(^imap, 50, ""), i32map_get(^imap, 1234, ""));
+ printf("%s%s\n", i32map.get(^imap, 50, ""), i32map.get(^imap, 1234, ""));
- i32map_delete(^imap, 50);
- println(i32map_has(^imap, 50));
+ i32map.delete(^imap, 50);
+ println(i32map.has(^imap, 50));
- i32map_clear(^imap);
- println(i32map_has(^imap, 1234));
+ i32map.clear(^imap);
+ println(i32map.has(^imap, 1234));
}