small improvement for binary operator checking
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 10 Aug 2020 14:23:33 +0000 (09:23 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 10 Aug 2020 14:23:33 +0000 (09:23 -0500)
docs/plan
include/onyxtypes.h
onyx
progs/alloc_test.onyx
progs/stack_based.onyx
src/onyxchecker.c
src/onyxtypes.c

index ca7bcea67018d4e90699bd840782d49f9d4b0a81..6ef779a791c9da80c508f5b9dbd027b40868e1cc 100644 (file)
--- 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
index f997d89786b6838c013fa330f485df8bdbb93d93..fb2fa7e9a8258cd3200505ae4a4160f9e3d5e97c 100644 (file)
@@ -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 5c7d161715bb4e7c70c41744143e35291c75aa90..cc695e09207abd7690ae61278ce3b30081b33bfc 100755 (executable)
Binary files a/onyx and b/onyx differ
index 7e6156ad85b7532b4f2ea835e0e60298f70cc1ed..c3298c3b9c34be2ff9e3f31a5b0b897027f485ef 100644 (file)
@@ -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;
     }
 
index 75a8dd43b02457dfbfec5b5a9a11fa27af8edbc8..b8f409ec9b7ac749089738d11385c0ef989a52e3 100644 (file)
@@ -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;
 
index 6307ed75f438b34e224e9f241efe060920dc857e..8be74b0a5d2ff242430b62bf92796c3b504f8498 100644 (file)
@@ -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,
index c6891d4f46c6b0e8c5f5daf341f68809b5a35b80..df03d67764c5574872938e7481c23cac12601cae 100644 (file)
@@ -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)