From: Brendan Hansen Date: Mon, 20 Jul 2020 02:26:10 +0000 (-0500) Subject: integers are implicitly converted if they are the same size X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=08c022a236b7002cc720f02311a333333ea088ab;p=onyx.git integers are implicitly converted if they are the same size --- diff --git a/onyx b/onyx index deefae6b..69bdc1a0 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/arrays.onyx b/progs/arrays.onyx index 5b0f5d8e..6c849775 100644 --- a/progs/arrays.onyx +++ b/progs/arrays.onyx @@ -87,23 +87,23 @@ alloc :: proc (size: u32) -> rawptr { heap_u32 :: __heap_start as ^u32; curr_offset := *heap_u32; - if curr_offset == 0 as u32 curr_offset = 8 as u32; + if curr_offset == 0 curr_offset = 8; *heap_u32 = curr_offset + size; return ((__heap_start as u32) + curr_offset) as rawptr; } - +alloc_2darr :: proc (rows: u32, cols: u32) -> ^^i32 { + arr := alloc(rows * 4) as ^^i32; + for i: 0, cols arr[i] = alloc(cols * 4) as ^i32; + return arr; +} multi_arr_test :: proc #export "main" { - arrs := alloc((10 * 4) as u32) as ^^i32; - - for i: 0, 10 { - arrs[i] = alloc((10 * 4) as u32) as ^i32; - } + arrs := alloc_2darr(10, 10); for y: 0, 10 for x: 0, 10 arrs[y][x] = x + y * 10; for y: 0, 10 for x: 0, 10 print(arrs[x][y]); diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 1542cc99..d5626dcb 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -85,20 +85,29 @@ CHECK(for, AstFor* fornode) { if (check_expression(fornode->step)) return 1; // HACK - if (fornode->start->type != &basic_types[Basic_Kind_I32]) { + if (!types_are_compatible(fornode->start->type, &basic_types[Basic_Kind_I32])) { onyx_message_add(Msg_Type_Literal, fornode->start->token->pos, "expected expression of type i32 for start"); return 1; } - if (fornode->end->type != &basic_types[Basic_Kind_I32]) { + if (!types_are_compatible(fornode->end->type, &basic_types[Basic_Kind_I32])) { onyx_message_add(Msg_Type_Literal, fornode->end->token->pos, "expected expression of type i32 for end"); return 1; } + if (fornode->step) + if (!types_are_compatible(fornode->step->type, &basic_types[Basic_Kind_I32])) { + onyx_message_add(Msg_Type_Literal, + fornode->start->token->pos, + "expected expression of type i32 for step"); + return 1; + } + + if (check_statement(fornode->stmt)) return 1; return 0; diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 118651e6..f8e9cd30 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -33,7 +33,11 @@ b32 types_are_compatible(Type* t1, Type* t2) { case Type_Kind_Basic: if (t2->kind == Type_Kind_Basic) { // HACK: Not sure if this is right way to check this? - return t1 == t2; + if (t1 == t2) return 1; + + if ((t1->Basic.flags & Basic_Flag_Integer) && (t2->Basic.flags & Basic_Flag_Integer)) { + return t1->Basic.size == t2->Basic.size; + } } break;