From: Brendan Hansen Date: Tue, 18 Aug 2020 13:59:02 +0000 (-0500) Subject: general bugfixes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=430ebff5f93801817c428cddf89e748c9f5f0964;p=onyx.git general bugfixes --- diff --git a/core/string.onyx b/core/string.onyx new file mode 100644 index 00000000..6c472832 --- /dev/null +++ b/core/string.onyx @@ -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 index 8520d89b..00000000 --- a/core/test.onyx +++ /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 diff --git a/docs/plan b/docs/plan index 2f58f7e8..947e130d 100644 --- 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 7e7a4a81..05b31d23 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/stack_based.onyx b/progs/stack_based.onyx index 8842eaff..44889389 100644 --- a/progs/stack_based.onyx +++ b/progs/stack_based.onyx @@ -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 { diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 1fb90483..5f51a243 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -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, diff --git a/src/onyxparser.c b/src/onyxparser.c index 4d2b41e8..3296377a 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -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, ','); diff --git a/src/onyxwasm.c b/src/onyxwasm.c index f526e6b9..fe96ce89 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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;