io_utils.carbon 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "io_utils";
  5. import Core library "io";
  6. import Core library "range";
  7. class EOFType {}
  8. class CharOrEOF {
  9. adapt i32;
  10. fn EOF() -> Self;
  11. }
  12. impl CharOrEOF as Core.Copy {
  13. fn Op[self: Self]() -> Self {
  14. return (self as i32).(Core.Copy.Op)() as Self;
  15. }
  16. }
  17. fn CharOrEOF.EOF() -> Self {
  18. // Ordering between `CharOrEOF` and `char` treats EOF as less than all `char`
  19. // values.
  20. return (-1 as i32) as CharOrEOF;
  21. }
  22. impl forall [U:! Core.ImplicitAs(char)] U as Core.ImplicitAs(CharOrEOF) {
  23. fn Convert[self: char]() -> CharOrEOF {
  24. return ((self as u8) as i32) as CharOrEOF;
  25. }
  26. }
  27. impl forall [U:! Core.ImplicitAs(CharOrEOF)]
  28. CharOrEOF as Core.EqWith(U) {
  29. fn Equal[self: Self](other: CharOrEOF) -> bool {
  30. return (self as i32) == (other as i32);
  31. }
  32. fn NotEqual[self: Self](other: CharOrEOF) -> bool {
  33. return (self as i32) != (other as i32);
  34. }
  35. }
  36. impl forall [U:! Core.ImplicitAs(CharOrEOF)]
  37. CharOrEOF as Core.OrderedWith(U) {
  38. fn Less[self: Self](other: CharOrEOF) -> bool {
  39. return (self as i32) < (other as i32);
  40. }
  41. fn LessOrEquivalent[self: Self](other: CharOrEOF) -> bool {
  42. return (self as i32) <= (other as i32);
  43. }
  44. fn Greater[self: Self](other: CharOrEOF) -> bool {
  45. return (self as i32) > (other as i32);
  46. }
  47. fn GreaterOrEquivalent[self: Self](other: CharOrEOF) -> bool {
  48. return (self as i32) >= (other as i32);
  49. }
  50. }
  51. impl forall [U:! Core.ImplicitAs(char)]
  52. CharOrEOF as Core.SubWith(U) where .Result = i32 {
  53. fn Op[self: Self](other: char) -> i32 {
  54. return (self as i32) - ((other as u8) as i32);
  55. }
  56. }
  57. var unread_char: Core.Optional(CharOrEOF)
  58. = Core.Optional(CharOrEOF).None();
  59. fn ReadChar() -> CharOrEOF {
  60. if (unread_char.HasValue()) {
  61. var result: CharOrEOF = unread_char.Get();
  62. unread_char = Core.Optional(CharOrEOF).None();
  63. return result;
  64. }
  65. var value: i32 = Core.ReadChar();
  66. if (value == Core.EOF()) {
  67. return CharOrEOF.EOF();
  68. } else {
  69. return value as CharOrEOF;
  70. }
  71. }
  72. fn UnreadChar(c: CharOrEOF) {
  73. // TODO: assert(unread_char == 0);
  74. // TODO: unread_char = c;
  75. unread_char = Core.Optional(CharOrEOF).Some(c);
  76. }
  77. fn ReadInt[N:! Core.IntLiteral()](ref p: Core.Int(N)) -> bool {
  78. var read_any_digits: bool = false;
  79. // TODO: `p = 0;` crashes the toolchain, see
  80. // https://github.com/carbon-language/carbon-lang/issues/6500.
  81. p = (0 as i32) as Core.Int(N);
  82. while (true) {
  83. var c: CharOrEOF = ReadChar();
  84. if (c < '0' or c > '9') {
  85. UnreadChar(c);
  86. break;
  87. }
  88. // TODO: Check for overflow.
  89. // TODO: p *= 10; crashes the toolchain.
  90. p *= (10 as i32) as Core.Int(N);
  91. p += (c - '0') as Core.Int(N);
  92. read_any_digits = true;
  93. }
  94. return read_any_digits;
  95. }
  96. fn PrintIntNoNewline[N:! Core.IntLiteral()](n_val: Core.Int(N)) {
  97. // TODO: var pow10: Core.Int(N) = 1; crashes the toolchain.
  98. var pow10: Core.Int(N) = (1 as i32) as Core.Int(N);
  99. var n: Core.Int(N) = n_val;
  100. // TODO: let ten: Core.Int(N) = 10;
  101. let ten: Core.Int(N) = (10 as i32) as Core.Int(N);
  102. while (n / ten >= pow10) {
  103. pow10 = pow10 * ten;
  104. }
  105. // TODO: while (pow10 != 0) {
  106. while (pow10 != ((0 as i32) as Core.Int(N))) {
  107. let d: Core.Int(N) = n / pow10;
  108. // TODO: Core.PrintChar('0' + d);
  109. Core.PrintChar(((d as u8) + (('0' as char) as u8)) as char);
  110. n = n % pow10;
  111. pow10 = pow10 / ten;
  112. }
  113. }
  114. fn PrintInt[N:! Core.IntLiteral()](n_val: Core.Int(N)) {
  115. PrintIntNoNewline(n_val);
  116. Core.PrintChar('\n');
  117. }
  118. fn PeekChar() -> CharOrEOF {
  119. var next: CharOrEOF = ReadChar();
  120. UnreadChar(next);
  121. return next;
  122. }
  123. fn ConsumeChar(c: char) -> bool {
  124. var next: CharOrEOF = ReadChar();
  125. if (next != c) {
  126. UnreadChar(next);
  127. return false;
  128. }
  129. return true;
  130. }
  131. fn SkipSpaces() -> bool {
  132. var skipped_any_spaces: bool = false;
  133. while (ConsumeChar(' ')) {
  134. skipped_any_spaces = true;
  135. }
  136. return skipped_any_spaces;
  137. }
  138. fn SkipNewline() -> bool {
  139. // Optional carriage return.
  140. ConsumeChar('\r');
  141. // Newline.
  142. // TODO: Unread the CR if it was present?
  143. return ConsumeChar('\n');
  144. }
  145. fn SkipNChars(n: i32) -> bool {
  146. for (_: i32 in Core.Range(n)) {
  147. if (ReadChar() == CharOrEOF.EOF()) {
  148. return false;
  149. }
  150. }
  151. return true;
  152. }