random little changes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 22 Mar 2021 02:44:03 +0000 (21:44 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 22 Mar 2021 02:44:03 +0000 (21:44 -0500)
bin/onyx
core/env.onyx [new file with mode: 0644]
core/io/file.onyx
core/map.onyx
core/std.onyx
docs/bugs
src/onyxastnodes.c
src/onyxparser.c
src/onyxsymres.c

index 8aa4189de856ccd4f443ae3c6bf309084a9db09d..cf48b2c31aeea3415f0b6f02903cd9f0803589ac 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
diff --git a/core/env.onyx b/core/env.onyx
new file mode 100644 (file)
index 0000000..7f91b55
--- /dev/null
@@ -0,0 +1,56 @@
+package core.env
+
+use package build_opts as build_opts
+#if build_opts.Runtime != build_opts.Runtime_Wasi {
+    #error "'core.env' is only available with the 'wasi' runtime.";
+}
+
+
+use package wasi { environ_get, environ_sizes_get, Size }
+use package core.map as map
+use package core.memory as memory
+use package core.string as string
+
+Environment :: struct {
+    vars             : map.Map(str, str);
+
+    buffer           : [] u8;
+    buffer_allocator : Allocator;
+}
+
+get_env :: (allocator := context.allocator) -> Environment {
+    env_count, env_buf_size : Size;
+    environ_sizes_get(^env_count, ^env_buf_size);
+
+    env_var := memory.make_slice(cstr, env_count, allocator=allocator);
+    env_buf := memory.make_slice(u8, env_buf_size, allocator=allocator);
+
+    environ_get(env_var.data, env_buf.data);
+
+    while i := cast(i32) (env_var.count - 1); i >= 0 {
+        defer i -= 1;
+
+        env_var[i] = cast(cstr) (cast(^u32) env_var.data)[i];
+    }
+
+    env_map := map.make(str, str, "");
+    for env: env_var {
+        s := string.make(env);
+        var := string.read_until(^s, #char "=");
+        map.put(^env_map, var, string.advance(s, 1));
+    }
+
+    raw_free(allocator, env_var.data);
+
+    return .{
+        vars             = env_map,
+        buffer           = env_buf,
+        buffer_allocator = allocator,
+    };
+}
+
+free_env :: (use env: ^Environment) {
+    map.free(^vars);
+
+    raw_free(buffer_allocator, buffer.data);
+}
index 8d4f66d47c13841594bf2ace2508dbac74471c83..99c1759c0c6eab6e55c546027531d38dc63e5c10 100644 (file)
@@ -117,7 +117,7 @@ file_close :: (file: File) -> bool {
 file_write :: (file: File, data: str) {
     vec := IOVec.{ buf = cast(u32) data.data, len = data.count };
     tmp : Size;
-    wasi.fd_write(file.fd, IOVecArray.{ cast(u32) ^vec, 1 }, ^tmp);
+    wasi.fd_write(file.fd, .{ cast(u32) ^vec, 1 }, ^tmp);
     wasi.fd_datasync(file.fd);
 }
 
@@ -141,7 +141,7 @@ get_contents_from_file :: (file: File) -> str {
 
     dummy2: u32;
     buf := IOVec.{ cast(u32) data, size };
-    wasi.fd_pread(file.fd, IOVecArray.{ cast(u32) ^buf, 1 }, 0, ^dummy2);
+    wasi.fd_pread(file.fd, .{ cast(u32) ^buf, 1 }, 0, ^dummy2);
 
     wasi.fd_seek(file.fd, prev_loc, Whence.Set, ^dummy);
 
@@ -153,7 +153,7 @@ get_contents :: proc {
 
     proc (path: str) -> str {
         tmp_file, success := file_open(path, OpenMode.Read);
-        if !success do return str.{ null, 0 };
+        if !success do return .{ null, 0 };
         defer file_close(tmp_file);
 
         return get_contents(tmp_file);
@@ -167,8 +167,8 @@ FileStream :: struct {
 
 open :: (path: str, mode := OpenMode.Read) -> (Error, FileStream) {
     fs := FileStream.{
-        stream = Stream.{ vtable = null },
-        file   = File.{ fd = -1 },
+        stream = .{ vtable = null },
+        file   = .{ fd = -1 },
     };
 
     file, success := file_open(path, mode);
index 9c5887064a78f566923982218a39ff6e777d71da..cbdb47b9bb9b1fa6df5b67024e42e90219704f52 100644 (file)
@@ -111,10 +111,9 @@ empty :: (use map: ^Map($K, $V)) -> bool {
 
 hash_function :: proc {
     (key: rawptr) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; },
-    (key: i32) -> u32 { return 0xcbf29ce7 ^ cast(u32) key; },
-    (key: i64) -> u32 { return cast(u32) (cast(u64) 0xcbf29ce7 ^ cast(u64) key); },
-
-    (key: str) -> u32 {
+    (key: i32)    -> u32 { return 0xcbf29ce7 ^ cast(u32) key; },
+    (key: i64)    -> u32 { return cast(u32) (cast(u64) 0xcbf29ce7 ^ cast(u64) key); },
+    (key: str)    -> u32 {
         hash: u32 = 5381;
         for ch: key do hash += (hash << 5) + ~~ch;
         return hash;
index ec6ad5c2a60023f87e9da470c546896ae3267533..58428eb4e7a1744d4a6af414c740046df0b01136 100644 (file)
@@ -30,6 +30,7 @@ use package build_opts as build_opts
     #load "core/sys/wasi"
     #load "core/wasi"
     #load "core/io/file"
+    #load "core/env"
 }
 
 #if build_opts.Runtime == build_opts.Runtime_Js {
index 94500bebbbcea0a60aa95c4e13024130ece54ed3..56a444387126ced56a697ab23986a65298068333 100644 (file)
--- a/docs/bugs
+++ b/docs/bugs
@@ -36,6 +36,24 @@ List of known bugs:
         SN.foo()
     }
 
+[ ] There is a segfault when passing an anonymous struct literal to an untyped vararg.
+    
+    vararg_proc :: (va: ...) {
+        ...
+    }
+
+    vararg_proc(.{ 1, 2 });
+
+[ ] This currently does not work but I think it should:
+
+    baked_proc :: (x: $T, $func: (T) -> T) -> T {
+        return func(x);
+    }
+
+    The problem is that 'T' is an unresolved symbol because the context of the function type
+    does not have the solved value of T in it.
+
+
 List of things to change:
 [X] Currently, there is no way to use the initialized members of a structure without using a struct literal.
     There should be a initialize intrinsic procedure that takes a pointer to anything and initializes it.
index 52c7ecc718f73726fa3b06214b57e37924f2106f..fb654767f55767d33b5dcf6187d31d20d22746d4 100644 (file)
@@ -429,6 +429,7 @@ b32 type_check_or_auto_cast(AstTyped** pnode, Type* type) {
     if (node_is_type((AstNode *) node)) return 0;
 
     if (node->kind == Ast_Kind_Struct_Literal && node->type_node == NULL) {
+        if (type->kind == Type_Kind_VarArgs) type = type->VarArgs.ptr_to_data->Pointer.elem;
         node->type = type;
 
         add_entities_for_node(NULL, (AstNode *) node, NULL, NULL);
index 7e53af02fd973b16b9e412f01a0014b2b9a4ccda..ae264191261f830c23adad5ee10675cdc0e5aab2 100644 (file)
@@ -2389,6 +2389,8 @@ static AstPackage* parse_package_name(OnyxParser* parser) {
             token_toggle_end(symbol);
             symbol_subpackage_introduce(package->scope, symbol->text, pnode);
             token_toggle_end(symbol);
+
+            package_reinsert_use_packages(package);
         }
 
         package = newpackage;
index 43f7a2db373770ef6aedbd24cf3067eb169d4ac3..efb5ec6ece34c03626122cdc0591e48b263d5330 100644 (file)
@@ -1018,6 +1018,7 @@ static SymresStatus symres_struct_defaults(AstType* t) {
 }
 
 static SymresStatus symres_polyproc(AstPolyProc* pp) {
+    pp->flags |= Ast_Flag_Comptime;
     pp->poly_scope = curr_scope;
 
     bh_arr_each(AstPolyParam, param, pp->poly_params) {