Bläddra i källkod

Use explicit conversion between integer types in examples. (#4792)

Richard Smith 1 år sedan
förälder
incheckning
fb1a9ba20f

+ 2 - 24
examples/advent2024/day5_common.carbon

@@ -8,31 +8,9 @@ library "day5_common";
 
 import library "io_utils";
 
-// fn LeftShift(a: Core.UInt(100), b: i32) -> Core.UInt(100) = "int.left_shift";
-fn LeftShift_u100(a: Core.UInt(100), b: Core.UInt(100)) -> Core.UInt(100) = "int.left_shift";
-
-// TODO: Add a builtin that can do this!
-fn Promote(a: i32) -> Core.UInt(100) {
-  var result: Core.UInt(100) = 0;
-  var bit_i32: i32 = 1;
-  var bit_u100: Core.UInt(100) = 1;
-  while (bit_i32 != 0) {
-    if (a & bit_i32 != 0) {
-      result = result | bit_u100;
-    }
-    bit_i32 <<= 1;
-    // TODO: bit_u100 <<= 1;
-    bit_u100 = bit_u100 << 1;
-  }
-  return result;
-}
-
-fn LeftShift(a: Core.UInt(100), b: i32) -> Core.UInt(100) {
-  return LeftShift_u100(a, Promote(b));
-}
-
 fn PageMask(page: i32) -> Core.UInt(100) {
-  return LeftShift(1 as Core.UInt(100), page);
+  // TODO: return (1 as Core.UInt(100)) << page;
+  return (1 as Core.UInt(100)) << (page as Core.UInt(100));
 }
 
 class Rules {

+ 1 - 17
examples/advent2024/day9_common.carbon

@@ -9,22 +9,6 @@ library "day9_common";
 import Core library "io";
 import library "io_utils";
 
-// TODO: Add a builtin that can do this!
-fn Promote(a: i32) -> i64 {
-  var result: i64 = 0;
-  var bit_i32: i32 = 1;
-  var bit_i64: i64 = 1;
-  while (bit_i32 != 0) {
-    if (a & bit_i32 != 0) {
-      result = result | bit_i64;
-    }
-    bit_i32 <<= 1;
-    // TODO: bit_i64 <<= 1;
-    bit_i64 = bit_i64 << 1;
-  }
-  return result;
-}
-
 class SectorList {
   fn Read() -> SectorList {
     returned var me: SectorList;
@@ -125,7 +109,7 @@ class SectorList {
     var i: i32 = 0;
     while (i < self.size) {
       if (self.data[i] != -1) {
-        total = total + Promote(i * self.data[i]);
+        total = total + ((i * self.data[i]) as i64);
       }
       ++i;
     }

+ 2 - 30
examples/advent2024/io_utils.carbon

@@ -57,25 +57,7 @@ fn ReadInt64(p: i64*) -> bool {
     }
     // TODO: Check for overflow.
     *p = *p * 10;
-    if (c == 0x31) {
-      *p = *p + 1;
-    } else if (c == 0x32) {
-      *p = *p + 2;
-    } else if (c == 0x33) {
-      *p = *p + 3;
-    } else if (c == 0x34) {
-      *p = *p + 4;
-    } else if (c == 0x35) {
-      *p = *p + 5;
-    } else if (c == 0x36) {
-      *p = *p + 6;
-    } else if (c == 0x37) {
-      *p = *p + 7;
-    } else if (c == 0x38) {
-      *p = *p + 8;
-    } else if (c == 0x39) {
-      *p = *p + 9;
-    }
+    *p = *p + ((c - 0x30) as i64);
     read_any_digits = true;
   }
   return read_any_digits;
@@ -89,17 +71,7 @@ fn PrintInt64NoNewline(n_val: i64) {
   }
   while (pow10 != 0) {
     let d: i64 = n / pow10;
-    // TODO: Core.PrintChar(0x30 + (d as i32));
-    if (d == 0) { Core.PrintChar(0x30); }
-    else if (d == 1) { Core.PrintChar(0x31); }
-    else if (d == 2) { Core.PrintChar(0x32); }
-    else if (d == 3) { Core.PrintChar(0x33); }
-    else if (d == 4) { Core.PrintChar(0x34); }
-    else if (d == 5) { Core.PrintChar(0x35); }
-    else if (d == 6) { Core.PrintChar(0x36); }
-    else if (d == 7) { Core.PrintChar(0x37); }
-    else if (d == 8) { Core.PrintChar(0x38); }
-    else if (d == 9) { Core.PrintChar(0x39); }
+    Core.PrintChar((d + 0x30) as i32);
     n = n % pow10;
     pow10 = pow10 / 10;
   }