general bugfixes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 18 Aug 2020 13:59:02 +0000 (08:59 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 18 Aug 2020 13:59:02 +0000 (08:59 -0500)
core/string.onyx [new file with mode: 0644]
core/test.onyx [deleted file]
docs/plan
onyx
progs/stack_based.onyx
src/onyxchecker.c
src/onyxparser.c
src/onyxwasm.c

diff --git a/core/string.onyx b/core/string.onyx
new file mode 100644 (file)
index 0000000..6c47283
--- /dev/null
@@ -0,0 +1,59 @@
+package core
+
+use package memory
+
+Buffer :: struct {
+    length : u32    = 0;
+    data   : rawptr = null;
+}
+
+string :: struct {
+    length : u32 = 0;
+    data   : ^u8 = null;
+}
+
+string_make :: proc #overloaded { string_make_from_u8 }
+
+#private
+string_make_from_u8 :: proc (s: ^u8) -> string {
+    len :: string_length(s);
+
+    return string.{ length = len, data = s };
+}
+
+
+string_length :: proc #overloaded {
+    proc (s: ^u8) -> u32 {
+        len := 0;
+        c := s;
+        while *c != #char "\0" {
+            len += 1;
+            c += 1;
+        }
+
+        return len;
+    },
+
+    proc (s: string) -> u32 {
+        return s.length;
+    },
+}
+
+#private
+string_length_string :: proc (s: string) -> u32 do return s.length;
+
+string_concat :: proc (a: Allocator, s1: string, s2: string) -> string {
+    len1 :: string_length(s1);
+    len2 :: string_length(s2);
+
+    data := cast(^u8) alloc(a, len1 + len2);
+    for i: 0, len1 do data[i]        = s1.data[i];
+    for i: 0, len2 do data[i + len1] = s2.data[i];
+
+    return string.{ len1 + len2, data };
+}
+
+string_free :: proc (a: Allocator, s: string) do free(a, s.data);
+
+#private print_str_with_len :: proc #foreign "host" "print_str_with_len" (s: ^u8, len: u32) ---
+string_print :: proc (s: string) do print_str_with_len(s.data, s.length);
diff --git a/core/test.onyx b/core/test.onyx
deleted file mode 100644 (file)
index 8520d89..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-package core
-
-use package memory { null }
-
-Buffer :: struct {
-       length : u32    = 0;
-       data   : rawptr = null;
-}
\ No newline at end of file
index 2f58f7e809694569671d6f47d2a3480d7fc8d997..947e130dc64dd3ff86f48070b8b3ee22c9c3a18b 100644 (file)
--- a/docs/plan
+++ b/docs/plan
@@ -186,6 +186,14 @@ HOW:
 
         [X] returning structs
             - This will put forward a lot of the work that will be done for multiple return values
+            
+        [ ] Add slices
+            - Arrays without a size
+            - Converted to a struct that looks like:    
+                []T :: struct {
+                    count : u32;
+                    data  : ^T;
+                }
 
         [ ] 'use' enums and packages at an arbitrary scope
 
@@ -207,13 +215,6 @@ HOW:
 
         [ ] Type parameterized structs
 
-        [ ] Add slices
-            - Arrays without a size
-            - Converted to a struct that looks like:    
-                []T :: struct {
-                    count : u32;
-                    data  : ^T;
-                }
 
         [ ] Arrays need to be much better
             - Currently, they are basically just a pointer.
diff --git a/onyx b/onyx
index 7e7a4a81acf136b808249de5dbd4dc6b857bbe9b..05b31d2353f33b47f3aea3746f9239b2def3b75d 100755 (executable)
Binary files a/onyx and b/onyx differ
index 8842eaff26312e6e609e8bb11c431553a2b7079c..44889389b9c176b464b15557dc746ee4e17091ec 100644 (file)
@@ -4,11 +4,11 @@ package main
 
 #include_file "printing"
 #include_file "alloc"
-#include_file "test"
+#include_file "string"
 
 use package printing
 use package memory
-use package core as core
+use package core
 
 sort :: proc (arr: [N]i32, cmp: proc (i32, i32) -> i32) -> [N] i32 {
     for i: 0, N {
@@ -154,23 +154,27 @@ start :: proc #export {
     print(v2.y);
     print(v2.z);
 
-    buf := core.Buffer.{ length = 16 };
-
     un : UnionTest;
     un.f = 1.25f;
     print_hex(cast(u64) un.i);
+
+    s1 :: string_make("Hello, ");
+    s2 :: string_make("World!");
+    s3 :: string_concat(heap_allocator, s1, s2);
+    defer string_free(heap_allocator, s3);
+    string_print(s3);
 }
 
 vadd :: proc (v1: Vec3, v2: Vec3) -> Vec3 {
     return Vec3.{
-        x = v1.x + v2.x,
-        y = v1.y + v2.y,
-        z = v1.z + v2.z,
+        v1.x + v2.x,
+        v1.y + v2.y,
+        v1.z + v2.z,
     };
 }
 
 vmul :: proc (use v: Vec3, s: i32) -> Vec3 {
-    return Vec3.{ x = x * s, y = y * s, z = z * s };
+    return Vec3.{ x * s, y * s, z * s };
 }
 
 UnionTest :: struct #union {
index 1fb9048326849963432ae8a7040b11aaac7a23d9..5f51a243f70855ecdde3a279dcea16244eec30e6 100644 (file)
@@ -96,8 +96,9 @@ CHECK(for, AstFor* fornode) {
     if (check_expression(&fornode->end)) return 1;
     if (check_expression(&fornode->step)) return 1;
 
-    fornode->var->type_node = fornode->start->type_node;
-    if (check_expression((AstTyped **) &fornode->var)) return 1;
+    if (fornode->var->type_node == NULL)
+        fornode->var->type_node = fornode->start->type_node;
+    fill_in_type((AstTyped *) fornode->var);
 
     if (!type_is_integer(fornode->start->type)) {
         onyx_message_add(Msg_Type_Literal,
index 4d2b41e8cb410fd6990e0d931ca09dce0bdcf74f..3296377a6cbd6209bd0577f620ef61d091cc543e 100644 (file)
@@ -961,8 +961,6 @@ static AstNode* parse_statement(OnyxParser* parser) {
                 parser->curr->pos,
                 token_name(';'),
                 token_name(parser->curr->type));
-
-            find_token(parser, ';');
         }
         consume_token(parser);
     }
@@ -1275,10 +1273,9 @@ static AstFunction* parse_function_definition(OnyxParser* parser) {
             while (parser->curr->type != '}') {
                 if (parser->hit_unexpected_token) return (AstFunction *) ofunc;
 
-                AstTyped* sym_node = make_node(AstTyped, Ast_Kind_Symbol);
-                sym_node->token = expect_token(parser, Token_Type_Symbol);
+                AstTyped* o_node = parse_expression(parser);
 
-                bh_arr_push(ofunc->overloads, sym_node);
+                bh_arr_push(ofunc->overloads, o_node);
 
                 if (parser->curr->type != '}')
                     expect_token(parser, ',');
index f526e6b907944f74db43371ea6bb6d5b08e6a05a..fe96ce897cc488d83ac1cfede783285726a922cf 100644 (file)
@@ -1215,12 +1215,23 @@ COMPILE_FUNC(expression, AstTyped* expr) {
         case Ast_Kind_Param: {
             u64 localidx = bh_imap_get(&mod->local_map, (u64) expr);
 
-            WIL(WI_LOCAL_GET, localidx);
+            if (expr->type->kind == Type_Kind_Struct) {
+                TypeStruct* st = &expr->type->Struct;
+                
+                fori (idx, 0, st->mem_count) {
+                    WIL(WI_LOCAL_GET, localidx + idx);
+                }
+                
+            } else {
+                WIL(WI_LOCAL_GET, localidx);
+            }
+
             break;
         }
 
         case Ast_Kind_Local: {
             u64 tmp = bh_imap_get(&mod->local_map, (u64) expr);
+
             if (tmp & LOCAL_IS_WASM) {
                 if (bh_arr_last(code).type == WI_LOCAL_SET && bh_arr_last(code).data.l == tmp) {
                     bh_arr_last(code).type = WI_LOCAL_TEE;