day3_common.carbon 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. library "day3_common";
  5. import library "io_utils";
  6. // Reads "mul(a,b)", and returns (true, a, b).
  7. // On error, stops before the first invalid character and returns (false, 0, 0).
  8. // TODO: -> Optional((i32, i32))
  9. fn ReadMul() -> (bool, i32, i32) {
  10. var a: i32;
  11. var b: i32;
  12. if (ConsumeChar(0x6D) and ConsumeChar(0x75) and ConsumeChar(0x6C) and
  13. ConsumeChar(0x28) and ReadInt(&a) and ConsumeChar(0x2C) and
  14. ReadInt(&b) and ConsumeChar(0x29)) {
  15. return (true, a, b);
  16. }
  17. return (false, 0, 0);
  18. }
  19. // Reads "do()" or "don't()", and returns (true, was_do).
  20. // On error, stops before the first invalid character and returns (false, false).
  21. fn ReadDoOrDont() -> (bool, bool) {
  22. // "do"
  23. if (not ConsumeChar(0x64) or not ConsumeChar(0x6F)) {
  24. return (false, false);
  25. }
  26. var do: bool = true;
  27. // "n't"
  28. if (ConsumeChar(0x6E)) {
  29. if (not ConsumeChar(0x27) or not ConsumeChar(0x74)) {
  30. return (false, false);
  31. }
  32. do = false;
  33. }
  34. // "()"
  35. if (not ConsumeChar(0x28) or not ConsumeChar(0x29)) {
  36. return (false, false);
  37. }
  38. return (true, do);
  39. }