bugfixes with numeric literals
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 8 Dec 2020 02:22:42 +0000 (20:22 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 8 Dec 2020 02:22:42 +0000 (20:22 -0600)
onyx
src/onyxparser.c
src/onyxutils.c
tests/general1

diff --git a/onyx b/onyx
index 74a264f42710764c445963fee8d90a5bf12f3441..d33443f99987d6565ff2753e9458a53018815f80 100755 (executable)
Binary files a/onyx and b/onyx differ
index 4b5b111967fbcab37ba0273d94159f1ed031e13a..cd7b85eb0c01617dab21bda1bf9ae070ee23da36 100644 (file)
@@ -149,7 +149,16 @@ static AstNumLit* parse_int_literal(OnyxParser* parser) {
     i64 value = strtoll(int_node->token->text, &first_invalid, 0);
 
     int_node->value.l = value;
-    int_node->type_node = (AstType *) &basic_type_int_unsized;
+
+    // NOTE: Hex literals are unsigned.
+    if (int_node->token->length >= 2 && int_node->token->text[1] == 'x') {
+        if ((u64) value >= ((u64) 1 << 32))
+            int_node->type_node = (AstType *) &basic_type_u64;
+        else
+            int_node->type_node = (AstType *) &basic_type_u32;
+    } else {
+        int_node->type_node = (AstType *) &basic_type_int_unsized;
+    }
 
     token_toggle_end(int_node->token);
     return int_node;
index 566314c2a901b41f96f7abd37ae90f139af94475..4288c717f3d79a8349e9792b0ac242437a98c8d4 100644 (file)
@@ -705,6 +705,10 @@ b32 convert_numlit_to_type(AstNumLit* num, Type* type) {
                     num->type = type;
                     return 1;
                 }
+                
+                onyx_report_error(num->token->pos, "Integer constant with value '%l' does not fit into %d-bits.",
+                        num->value.l,
+                        type->Basic.size * 8);
 
             } else {
                 i64 value = (i64) num->value.l;
@@ -713,21 +717,25 @@ b32 convert_numlit_to_type(AstNumLit* num, Type* type) {
                                 num->value.i = (i8) value;
                                 num->type = type;
                                 return 1;
-                            }
+                            } break;
                     case 2: if (-32768 <= value && value <= 32767) {
                                 num->value.i = (i16) value;
                                 num->type = type;
                                 return 1;
-                            }
-                    case 4: if (-247483648 <= value && value <= 2147483647) {
+                            } break;
+                    case 4: if (-2147483648 <= value && value <= 2147483647) {
                                 num->value.i = (i32) value;
                                 num->type = type;
                                 return 1;
-                            }
+                            } break;
                     case 8: {   num->type = type;
                                 return 1;
-                            }
+                            } break;
                 }
+
+                onyx_report_error(num->token->pos, "Integer constant with value '%l' does not fit into %d-bits.",
+                        num->value.l,
+                        type->Basic.size * 8);
             }
         }
 
@@ -807,10 +815,13 @@ Type* resolve_expression_type(AstTyped* node) {
 
     if (node->kind == Ast_Kind_NumLit) {
         if (node->type->Basic.kind == Basic_Kind_Int_Unsized) {
-            convert_numlit_to_type((AstNumLit *) node, &basic_types[Basic_Kind_I32]);
+            if ((((u64) ((AstNumLit *) node)->value.l) >> 32) > 0)
+                convert_numlit_to_type((AstNumLit *) node, &basic_types[Basic_Kind_I64]);
+            else
+                convert_numlit_to_type((AstNumLit *) node, &basic_types[Basic_Kind_I32]);
         }
         else if (node->type->Basic.kind == Basic_Kind_Float_Unsized) {
-            convert_numlit_to_type((AstNumLit *) node, &basic_types[Basic_Kind_F32]);
+            convert_numlit_to_type((AstNumLit *) node, &basic_types[Basic_Kind_F64]);
         }
     }
 
index 23a35a4521db5fcaff36502dddcbf78eb961d6f5..0c5e1cd1239295858a41a6c27be26e8f8546b3e1 100644 (file)
@@ -21,7 +21,7 @@ Evens from 6 to 34:
 Array details:
        Size: 0
        Capacity: 4
-       Data ptr: 0x10A38
+       Data ptr: 0x10A28
        Size of elements: 4
        Alignment of elements: 4
 
@@ -29,7 +29,7 @@ Array details:
 Array details:
        Size: 0
        Capacity: 4
-       Data ptr: 0x10A58
+       Data ptr: 0x10A48
        Size of elements: 8
        Alignment of elements: 8
 
@@ -37,8 +37,8 @@ Array details:
 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12
 A has 22? false
 Vec3(0, 0, 0) Vec3(1, 1, 1) Vec3(2, 4, 8) Vec3(3, 9, 27) Vec3(4, 16, 64) Vec3(5, 25, 125) Vec3(6, 36, 216) Vec3(7, 49, 343) Vec3(8, 64, 512) Vec3(9, 81, 729) Vec3(10, 100, 1000) Vec3(11, 121, 1331) Vec3(12, 144, 1728) Vec3(13, 169, 2197) Vec3(14, 196, 2744) Vec3(15, 225, 3375) Vec3(16, 256, 4096) Vec3(17, 289, 4913) Vec3(18, 324, 5832) Vec3(19, 361, 6859) Vec3(20, 400, 8000) Vec3(21, 441, 9261) Vec3(22, 484, 10648) Vec3(23, 529, 12167) Vec3(24, 576, 13824) Vec3(25, 625, 15625) Vec3(26, 676, 17576) Vec3(27, 729, 19683) Vec3(28, 784, 21952) Vec3(29, 841, 24389) Vec3(30, 900, 27000) Vec3(31, 961, 29791) Vec3(32, 1024, 32768) Vec3(33, 1089, 35937) Vec3(34, 1156, 39304) Vec3(35, 1225, 42875) Vec3(36, 1296, 46656) Vec3(37, 1369, 50653) Vec3(38, 1444, 54872) Vec3(39, 1521, 59319) Vec3(40, 1600, 64000) Vec3(41, 1681, 68921) Vec3(42, 1764, 74088) Vec3(43, 1849, 79507) Vec3(44, 1936, 85184) Vec3(45, 2025, 91125) Vec3(46, 2116, 97336) Vec3(47, 2209, 103823) Vec3(48, 2304, 110592) Vec3(49, 2401, 117649) Vec3(50, 2500, 125000) Vec3(51, 2601, 132651) Vec3(52, 2704, 140608) Vec3(53, 2809, 148877) Vec3(54, 2916, 157464) Vec3(55, 3025, 166375) Vec3(56, 3136, 175616) Vec3(57, 3249, 185193) Vec3(58, 3364, 195112) Vec3(59, 3481, 205379) Vec3(60, 3600, 216000) Vec3(61, 3721, 226981) Vec3(62, 3844, 238328) Vec3(63, 3969, 250047) Vec3(64, 4096, 262144) Vec3(65, 4225, 274625) Vec3(66, 4356, 287496) Vec3(67, 4489, 300763) Vec3(68, 4624, 314432) Vec3(69, 4761, 328509) Vec3(70, 4900, 343000) Vec3(71, 5041, 357911) Vec3(72, 5184, 373248) Vec3(73, 5329, 389017) Vec3(74, 5476, 405224) Vec3(75, 5625, 421875) Vec3(76, 5776, 438976) Vec3(77, 5929, 456533) Vec3(78, 6084, 474552) Vec3(79, 6241, 493039) Vec3(80, 6400, 512000) Vec3(81, 6561, 531441) Vec3(82, 6724, 551368) Vec3(83, 6889, 571787) Vec3(84, 7056, 592704) Vec3(85, 7225, 614125) Vec3(86, 7396, 636056) Vec3(87, 7569, 658503) Vec3(88, 7744, 681472) Vec3(89, 7921, 704969) Vec3(90, 8100, 729000) Vec3(91, 8281, 753571) Vec3(92, 8464, 778688) Vec3(93, 8649, 804357) Vec3(94, 8836, 830584) Vec3(95, 9025, 857375) Vec3(96, 9216, 884736) Vec3(97, 9409, 912673) Vec3(98, 9604, 941192) Vec3(99, 9801, 970299) 
-0x11CE8 0x11CF4 0x11D00 0x11D0C 0x11D18 0x11D24 0x11D30 0x11D3C 0x11D48 0x11D54 0x11D60 0x11D6C 0x11D78 0x11D84 0x11D90 0x11D9C 0x11DA8 0x11DB4 0x11DC0 0x11DCC 0x11DD8 0x11DE4 0x11DF0 0x11DFC 0x11E08 0x11E14 0x11E20 0x11E2C 0x11E38 0x11E44 0x11E50 0x11E5C 0x11E68 0x11E74 0x11E80 0x11E8C 0x11E98 0x11EA4 0x11EB0 0x11EBC 0x11EC8 0x11ED4 0x11EE0 0x11EEC 0x11EF8 0x11F04 0x11F10 0x11F1C 0x11F28 0x11F34 0x11F40 0x11F4C 0x11F58 0x11F64 0x11F70 0x11F7C 0x11F88 0x11F94 0x11FA0 0x11FAC 0x11FB8 0x11FC4 0x11FD0 0x11FDC 0x11FE8 0x11FF4 0x12000 0x1200C 0x12018 0x12024 0x12030 0x1203C 0x12048 0x12054 0x12060 0x1206C 0x12078 0x12084 0x12090 0x1209C 0x120A8 0x120B4 0x120C0 0x120CC 0x120D8 0x120E4 0x120F0 0x120FC 0x12108 0x12114 0x12120 0x1212C 0x12138 0x12144 0x12150 0x1215C 0x12168 0x12174 0x12180 0x1218
-1854 1858 1862 1866 1870 1874 1878 1882 1886 1890 1894 1898 
+0x11CD8 0x11CE4 0x11CF0 0x11CFC 0x11D08 0x11D14 0x11D20 0x11D2C 0x11D38 0x11D44 0x11D50 0x11D5C 0x11D68 0x11D74 0x11D80 0x11D8C 0x11D98 0x11DA4 0x11DB0 0x11DBC 0x11DC8 0x11DD4 0x11DE0 0x11DEC 0x11DF8 0x11E04 0x11E10 0x11E1C 0x11E28 0x11E34 0x11E40 0x11E4C 0x11E58 0x11E64 0x11E70 0x11E7C 0x11E88 0x11E94 0x11EA0 0x11EAC 0x11EB8 0x11EC4 0x11ED0 0x11EDC 0x11EE8 0x11EF4 0x11F00 0x11F0C 0x11F18 0x11F24 0x11F30 0x11F3C 0x11F48 0x11F54 0x11F60 0x11F6C 0x11F78 0x11F84 0x11F90 0x11F9C 0x11FA8 0x11FB4 0x11FC0 0x11FCC 0x11FD8 0x11FE4 0x11FF0 0x11FFC 0x12008 0x12014 0x12020 0x1202C 0x12038 0x12044 0x12050 0x1205C 0x12068 0x12074 0x12080 0x1208C 0x12098 0x120A4 0x120B0 0x120BC 0x120C8 0x120D4 0x120E0 0x120EC 0x120F8 0x12104 0x12110 0x1211C 0x12128 0x12134 0x12140 0x1214C 0x12158 0x12164 0x12170 0x1217
+1838 1842 1846 1850 1854 1858 1862 1866 1870 1874 1878 1882 
 20 20 20 20 20 19 19 19 19 19 18 18 18 18 18 17 17 17 17 16 16 16 16 15 15 15 15 15 14 14 14 14 14 13 13 13 13 13 12 12 12 12 12 11 11 11 11 10 10 10 10 10 9 9 9 9 9 8 8 8 8 8 7 7 7 7 7 6 6 6 6 5 5 5 5 5 4 4 4 4 4 3 3 3 3 3 2 2 2 2 2 1 1 1 1 0 0 0 0 0
 297 294 291 288 285 282 279 276 273 270 267 264 261 258 255 252 249 246 243 240 237 234 231 228 225 222 219 216 213 210 207 204 201 198 195 192 189 186 183 180 177 174 171 168 165 162 159 156 153 150 147 144 141 138 135 132 129 126 123 120 117 114 111 108 105 102 99 96 93 90 87 84 81 78 75 72 69 66 63 60 57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9 6 3 0
 After adding...
@@ -46,7 +46,7 @@ After adding...
 Array details:
        Size: 100
        Capacity: 128
-       Data ptr: 0x116C8
+       Data ptr: 0x116B8
        Size of elements: 4
        Alignment of elements: 4
 
@@ -54,7 +54,7 @@ Array details:
 Array details:
        Size: 100
        Capacity: 128
-       Data ptr: 0x118D8
+       Data ptr: 0x118C8
        Size of elements: 8
        Alignment of elements: 8
 
@@ -62,7 +62,7 @@ Array A sum: 999
 
 Has ^a[20]? true
 Has null? false
-Value at ^a[50]: 0x11A68 == 0x11A68
+Value at ^a[50]: 0x11A58 == 0x11A58
 Deleteing ^a[20]
 Has ^a[20]? false
 Clearing SOA...