changed the package system to allow for nested packages
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 6 Dec 2020 22:33:06 +0000 (16:33 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 6 Dec 2020 22:33:06 +0000 (16:33 -0600)
20 files changed:
Makefile
core/alloc.onyx
core/array.onyx
core/file.onyx
core/i32map.onyx
core/math.onyx
core/memory.onyx
core/ptrmap.onyx
core/random.onyx
core/stdio.onyx
core/string.onyx
core/sys/js.onyx
core/sys/wasi.onyx
core/wasi.onyx
onyx
progs/foo_test.onyx [new file with mode: 0644]
progs/odin_example.onyx
src/onyxparser.c
tests/general1.onyx
tests/i32map.onyx

index 6568870626909d57d81b350e048a5fd8388aaab0..386fe70374d8dc10b3696ff18f396cc03dda714c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-RELEASE=1
+RELEASE=0
 TIME=0
 
 OBJ_FILES=\
index bc7d2566a0b66c08c24c3ef194d66627fe2ea9fe..8e2f2f293ca890d3cf0336596d863029856d673b 100644 (file)
@@ -1,8 +1,11 @@
-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
@@ -106,7 +109,7 @@ heap_resize :: proc (ptr: rawptr, new_size: u32, align: u32) -> rawptr {
     }
 
     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;
 }
@@ -171,7 +174,3 @@ alloc_slice :: proc (sl: ^[] $T, count: i32) {
 }
 
 
-
-memory_init :: proc () {
-    heap_init();
-}
index a8b6f39c491ec35b2f319487a5dbc4036bc8480c..5407dcd3fcf82733e2ceebfa96bba4c812bb6c72 100644 (file)
@@ -1,15 +1,15 @@
-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;
 
@@ -17,25 +17,25 @@ array_free :: proc (arr: ^[..] $T) {
     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 {
@@ -46,7 +46,7 @@ array_insert :: proc (arr: ^[..] $T, idx: u32, x: T) {
     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 {
@@ -57,7 +57,7 @@ array_remove :: proc (arr: ^[..] $T, elem: T) {
     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 {
@@ -67,31 +67,31 @@ array_delete :: proc (arr: ^[..] $T, idx: u32) {
     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];
 }
 
@@ -99,7 +99,7 @@ array_to_slice :: proc (arr: ^[..] $T) -> [] T {
 ** 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;
@@ -113,12 +113,12 @@ array_sort :: proc (arr: ^[..] $T, cmp: proc (T, T) -> i32) {
     }
 }
 
-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);
 }
index 30e8176e17fe3d0fc8b59a6be4d314c556af5d94..b08065e7b513e48c8b0404a7885b355b0ec33926 100644 (file)
@@ -1,4 +1,4 @@
-package core_file
+package core.file
 
 // Many of these functions will be improved when
 // multiple return values are implemented.
@@ -17,7 +17,7 @@ File :: struct {
     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;
 
@@ -66,7 +66,7 @@ file_open :: proc (file: ^File, path: string, mode := OpenMode.Read, flags := FD
     return true;
 }
 
-file_close :: proc (file: File) -> bool {
+close :: proc (file: File) -> bool {
     if fd_close(file.fd) != Errno.Success {
         return false;
     }
@@ -74,15 +74,15 @@ file_close :: proc (file: File) -> bool {
     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);
 
@@ -101,15 +101,15 @@ file_get_contents_from_file :: proc (file: File) -> string {
     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);
     }
 }
index e55511904888a66bb751a34ab6c0eb90c44558b0..41b7b064eb47d68f12f480eebd5f3adbc5144f0d 100644 (file)
@@ -1,4 +1,6 @@
-package core
+package core.i32map
+
+use package core.array as array
 
 I32Map :: struct ($T) {
     hashes  : [..] i32;
@@ -11,20 +13,20 @@ I32MapEntry :: struct ($T) {
     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;
@@ -36,42 +38,42 @@ i32map_put :: proc (use imap: ^I32Map($T), key: i32, value: T) {
     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;
 }
@@ -90,7 +92,7 @@ I32MapLookupResult :: struct {
 }
 
 #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;
index 8fc6e43c7eac04fdb16342c14c11c9443075a764..fc78978f5d5fe562728c17c5446379dc21547ab7 100644 (file)
@@ -1,4 +1,8 @@
-package core
+package core.math
+
+use package core {
+    sqrt_f32, sqrt_f64
+}
 
 PI  :: 3.14159265f;
 TAU :: 6.28318330f;
@@ -48,16 +52,16 @@ cos :: proc (t_: f32) -> f32 {
        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 }
index e28d087d9855d1756fc8c128222d48a4a40035e7..ffd0fbf3c3eb14ad2e1efd863b4040b6e715e958 100644 (file)
@@ -1,12 +1,12 @@
-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;
 }
index 2cfc01b7c48cb2dca98538b3bcb2887af4b0fdce..176836825b57514985cb4b5651fe05f706f0a394 100644 (file)
@@ -1,5 +1,6 @@
-package core
+package core.ptrmap
 
+use package core.array as array
 
 PtrMap :: struct {
     hashes  : [..] i32;
@@ -13,27 +14,27 @@ PtrMapEntry :: struct {
     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],
@@ -42,37 +43,37 @@ ptrmap_put :: proc (use pmap: ^PtrMap, key: rawptr, value: rawptr) {
     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;
 }
@@ -91,7 +92,7 @@ PtrMapLookupResult :: struct {
 }
 
 #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;
index 8b27089e8fe5dc7ca31546e132ba9a6720499294..f781c21d479e8a441833565b6c682ca1fd62bce0 100644 (file)
@@ -1,4 +1,4 @@
-package core
+package core.random
 
 #private_file seed := 8675309
 
@@ -6,14 +6,15 @@ package core
 #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;
 }
index e1b2d0b3b7e8887054b7c313e3a27b37ac7c8b7f..6299da1f69be4e5374b5bb015f30fbc63e994537 100644 (file)
@@ -4,24 +4,24 @@ package core
 // 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 {
@@ -67,7 +67,7 @@ printf :: proc (format: string, va: ...) {
                     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;
@@ -80,7 +80,7 @@ printf :: proc (format: string, va: ...) {
                     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;
@@ -93,7 +93,7 @@ printf :: proc (format: string, va: ...) {
                     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;
@@ -106,7 +106,7 @@ printf :: proc (format: string, va: ...) {
                     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;
@@ -129,7 +129,7 @@ printf :: proc (format: string, va: ...) {
                     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;
@@ -159,9 +159,9 @@ print_buffer_flush :: proc () {
     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();
 }
 
index 7a7b742fcd47bdde7a5d9a73224d4937bea692a0..29afffe0d877c8bf7346d7b63d1d7bb8bee018bb 100644 (file)
@@ -1,12 +1,12 @@
-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;
@@ -23,9 +23,9 @@ string_length :: proc {
     },
 }
 
-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];
@@ -34,12 +34,12 @@ string_concat :: proc (s1: string, s2: string) -> string {
     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;
 
@@ -61,7 +61,7 @@ string_split :: proc (str: string, delim: u8) -> []string {
     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;
@@ -73,12 +73,12 @@ string_substr :: proc (str: string, sub: string) -> string {
     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;
@@ -88,7 +88,7 @@ string_contains :: proc (str: string, c: u8) -> bool {
 //     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;
@@ -97,7 +97,7 @@ string_equal :: proc (str1: string, str2: string) -> bool {
     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;
@@ -106,7 +106,7 @@ string_starts_with :: proc (str1: string, str2: string) -> bool {
     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;
@@ -117,7 +117,7 @@ string_strip_leading_whitespace :: proc (str: ^string) {
     }
 }
 
-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;
@@ -141,7 +141,7 @@ StringBuilder :: struct {
     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 {
@@ -155,7 +155,7 @@ string_builder_make :: proc (initial_cap: u32) -> StringBuilder {
     };
 }
 
-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 {
@@ -178,9 +178,9 @@ string_builder_add_string :: proc (use sb: ^StringBuilder, str: string) -> ^Stri
     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 {
@@ -261,49 +261,49 @@ f64_to_string :: proc (f: f64, buf: [] u8) -> 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");
@@ -315,19 +315,19 @@ string_read_u32 :: proc (str: ^string, out: ^u32) {
     *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;
 
@@ -345,7 +345,7 @@ string_read_line :: proc (str: ^string, out: ^string) {
     }
 }
 
-string_advance_line :: proc (str: ^string) {
+advance_line :: proc (str: ^string) {
     if str.count == 0 do return;
 
     adv := 0;
@@ -355,6 +355,6 @@ string_advance_line :: proc (str: ^string) {
     str.count -= adv + 1;
 }
 
-string_read :: proc {
-    string_read_u32, string_read_char
+read :: proc {
+    read_u32, read_char
 }
index 9bccbb2a6bee25c1a6d667c891c0be14c49844f9..83b2dbd095e10b7c3ce75298e801f567a369bcbf 100644 (file)
@@ -1,6 +1,5 @@
 package system
 
-
 use package core
 use package main as main
 
@@ -9,10 +8,10 @@ output_string :: proc (s: string) -> u32 #foreign "host" "print_str" ---
 // 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();
 
index 00c8214540215826f718f90e1322d64a3cc8a50c..cc78215345ac0a0717a2dee3c5eaa57988abb2e9 100644 (file)
@@ -2,7 +2,6 @@ package system
 
 #include_file "core/wasi"
 
-
 use package wasi
 use package core
 use package main as main
@@ -20,10 +19,10 @@ output_string :: proc (s: string) -> u32 {
 // 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;
index a35632523bcba01de6f9eda8277696de651b848d..53a9dacc99626ad062635f088411f55b4f336a60 100644 (file)
@@ -1,15 +1,5 @@
 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;
diff --git a/onyx b/onyx
index 373a69b7f7b02d0816f3636b51c406b66a5c52e1..c0dd1f34a07a76f928577a78396387c2be832141 100755 (executable)
Binary files a/onyx and b/onyx differ
diff --git a/progs/foo_test.onyx b/progs/foo_test.onyx
new file mode 100644 (file)
index 0000000..e372dfa
--- /dev/null
@@ -0,0 +1,5 @@
+package test.foo
+
+use package core
+
+print_foo :: proc () do println("Foo!");
index a4239cbad9fab843cf246b8cf24198a4e3a39a52..9a392dab54f5f83689f51d22bb045eb798a0b4ef 100644 (file)
@@ -1,7 +1,8 @@
 #include_file "core/std/wasi"
+#include_file "progs/foo_test"
 
 use package core
-
+use package test { foo as foo_pkg }
 
 Foo :: struct {
     data1 : i32;
@@ -57,6 +58,8 @@ make_bar :: proc () -> Bar {
 }
 
 main :: proc (args: [] cstring) {
+    foo_pkg.print_foo();
+
     bar := make_bar();
 
     print(sizeof Bar);
index 64ebfe6c1f45d6ba94273526b2f024313be5a2f4..b19fc5fc4479704ae62c39d7edc9ff62c7210846 100644 (file)
@@ -44,6 +44,7 @@ static AstTyped*      parse_global_declaration(OnyxParser* parser);
 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;
@@ -1839,6 +1840,27 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) {
 
             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) {
@@ -2002,6 +2024,65 @@ static AstNode* parse_top_level_statement(OnyxParser* parser) {
     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;
+}
 
 
 
@@ -2049,31 +2130,32 @@ void onyx_parser_free(OnyxParser* parser) {
 }
 
 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);
index cea27431ddb1c4e375583811f000a679e15190e3..9b4e7803d6933d89d3b15f42b9966e4b89236d29 100644 (file)
@@ -53,15 +53,15 @@ SOA :: struct {
 }
 
 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 {
@@ -184,16 +184,16 @@ main :: proc (args: [] cstring) {
     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 {
@@ -219,8 +219,8 @@ main :: proc (args: [] cstring) {
     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);
@@ -230,31 +230,31 @@ main :: proc (args: [] cstring) {
     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]));
 }
 
 
index da65ae3e041f996cb1456d0da8f4a532bcea35cc..021f92ab3125290046c7e29a556ac1f5d1d34f1a 100644 (file)
@@ -5,23 +5,23 @@ package main
 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));
 }