From: Brendan Hansen Date: Mon, 10 Aug 2020 14:23:33 +0000 (-0500) Subject: small improvement for binary operator checking X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9322338a692b399b986edba8cec48dc9b4da7d79;p=onyx.git small improvement for binary operator checking --- diff --git a/docs/plan b/docs/plan index ca7bcea6..6ef779a7 100644 --- a/docs/plan +++ b/docs/plan @@ -156,7 +156,7 @@ HOW: [X] Char literals - [ ] Properly checking binary operators + [X] Properly checking binary operators - Shouldn't be able to add two structs/arrays together [ ] Array literals diff --git a/include/onyxtypes.h b/include/onyxtypes.h index f997d897..fb2fa7e9 100644 --- a/include/onyxtypes.h +++ b/include/onyxtypes.h @@ -124,6 +124,7 @@ b32 type_is_array(Type* tyoe); b32 type_is_struct(Type* type); b32 type_is_bool(Type* type); b32 type_is_integer(Type* type); +b32 type_is_numeric(Type* type); b32 type_results_in_void(Type* type); #endif // #ifndef ONYX_TYPES diff --git a/onyx b/onyx index 5c7d1617..cc695e09 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/alloc_test.onyx b/progs/alloc_test.onyx index 7e6156ad..c3298c3b 100644 --- a/progs/alloc_test.onyx +++ b/progs/alloc_test.onyx @@ -11,9 +11,9 @@ deferred_example :: proc -> i32 { for i: 0, 8 do arr[i] = cast(i64) (i * i); - walker := cast(^i64) arr; + walker := arr; for _: 0, 8 { - print_u64(cast(i64) *walker); + print_u64(*walker); walker += 1; } diff --git a/progs/stack_based.onyx b/progs/stack_based.onyx index 75a8dd43..b8f409ec 100644 --- a/progs/stack_based.onyx +++ b/progs/stack_based.onyx @@ -53,7 +53,9 @@ start :: proc #export { print_bin(42l); print_hex(42l); - print_hex(cast(u64) #char "a"); + for i: #char "a", #char "f" { + print_hex(cast(u64) i); + } a := 12345; diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 6307ed75..8be74b0a 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -504,6 +504,20 @@ CHECK(binaryop, AstBinaryOp* binop, b32 assignment_is_ok) { || binop->operation == Binary_Op_Bool_Or) return check_binaryop_bool(binop); + if (!type_is_numeric(binop->left->type)) { + onyx_message_add(Msg_Type_Literal, + binop->token->pos, + "expected numeric type for left side of binary operator"); + return 1; + } + + if (!type_is_numeric(binop->right->type)) { + onyx_message_add(Msg_Type_Literal, + binop->token->pos, + "expected numeric type for right side of binary operator"); + return 1; + } + if (type_is_pointer(binop->right->type)) { onyx_message_add(Msg_Type_Literal, binop->token->pos, diff --git a/src/onyxtypes.c b/src/onyxtypes.c index c6891d4f..df03d677 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -477,6 +477,12 @@ b32 type_is_integer(Type* type) { return type->Basic.kind >= Basic_Kind_I8 && type->Basic.kind <= Basic_Kind_U64; } +b32 type_is_numeric(Type* type) { + if (type->kind != Type_Kind_Basic) return 0; + + return type->Basic.kind >= Basic_Kind_I8 && type->Basic.kind <= Basic_Kind_F64; +} + b32 type_results_in_void(Type* type) { return (type == NULL) || (type->kind == Type_Kind_Basic && type->Basic.kind == Basic_Kind_Void)