From: Brendan Hansen Date: Sun, 6 Dec 2020 22:33:06 +0000 (-0600) Subject: changed the package system to allow for nested packages X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=42d4a52c978187a9556c150f26645d27c3152fd5;p=onyx.git changed the package system to allow for nested packages --- diff --git a/Makefile b/Makefile index 65688706..386fe703 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -RELEASE=1 +RELEASE=0 TIME=0 OBJ_FILES=\ diff --git a/core/alloc.onyx b/core/alloc.onyx index bc7d2566..8e2f2f29 100644 --- a/core/alloc.onyx +++ b/core/alloc.onyx @@ -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(); -} diff --git a/core/array.onyx b/core/array.onyx index a8b6f39c..5407dcd3 100644 --- a/core/array.onyx +++ b/core/array.onyx @@ -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); } diff --git a/core/file.onyx b/core/file.onyx index 30e8176e..b08065e7 100644 --- a/core/file.onyx +++ b/core/file.onyx @@ -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); } } diff --git a/core/i32map.onyx b/core/i32map.onyx index e5551190..41b7b064 100644 --- a/core/i32map.onyx +++ b/core/i32map.onyx @@ -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; diff --git a/core/math.onyx b/core/math.onyx index 8fc6e43c..fc78978f 100644 --- a/core/math.onyx +++ b/core/math.onyx @@ -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 } diff --git a/core/memory.onyx b/core/memory.onyx index e28d087d..ffd0fbf3 100644 --- a/core/memory.onyx +++ b/core/memory.onyx @@ -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; } diff --git a/core/ptrmap.onyx b/core/ptrmap.onyx index 2cfc01b7..17683682 100644 --- a/core/ptrmap.onyx +++ b/core/ptrmap.onyx @@ -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; diff --git a/core/random.onyx b/core/random.onyx index 8b27089e..f781c21d 100644 --- a/core/random.onyx +++ b/core/random.onyx @@ -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; } diff --git a/core/stdio.onyx b/core/stdio.onyx index e1b2d0b3..6299da1f 100644 --- a/core/stdio.onyx +++ b/core/stdio.onyx @@ -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(); } diff --git a/core/string.onyx b/core/string.onyx index 7a7b742f..29afffe0 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -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 } diff --git a/core/sys/js.onyx b/core/sys/js.onyx index 9bccbb2a..83b2dbd0 100644 --- a/core/sys/js.onyx +++ b/core/sys/js.onyx @@ -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(); diff --git a/core/sys/wasi.onyx b/core/sys/wasi.onyx index 00c82145..cc782153 100644 --- a/core/sys/wasi.onyx +++ b/core/sys/wasi.onyx @@ -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; diff --git a/core/wasi.onyx b/core/wasi.onyx index a3563252..53a9dacc 100644 --- a/core/wasi.onyx +++ b/core/wasi.onyx @@ -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 373a69b7..c0dd1f34 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/foo_test.onyx b/progs/foo_test.onyx new file mode 100644 index 00000000..e372dfa3 --- /dev/null +++ b/progs/foo_test.onyx @@ -0,0 +1,5 @@ +package test.foo + +use package core + +print_foo :: proc () do println("Foo!"); diff --git a/progs/odin_example.onyx b/progs/odin_example.onyx index a4239cba..9a392dab 100644 --- a/progs/odin_example.onyx +++ b/progs/odin_example.onyx @@ -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); diff --git a/src/onyxparser.c b/src/onyxparser.c index 64ebfe6c..b19fc5fc 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -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); diff --git a/tests/general1.onyx b/tests/general1.onyx index cea27431..9b4e7803 100644 --- a/tests/general1.onyx +++ b/tests/general1.onyx @@ -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])); } diff --git a/tests/i32map.onyx b/tests/i32map.onyx index da65ae3e..021f92ab 100644 --- a/tests/i32map.onyx +++ b/tests/i32map.onyx @@ -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)); }