experimental_continuation6.carbon 658 B

123456789101112131415161718192021222324252627282930313233
  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. // Test recursive functions inside continuations.
  5. var current: Int = 0;
  6. fn CountUpTo(x: Int) -> Int {
  7. if (x == 0) {
  8. current = 0;
  9. __await;
  10. return 0;
  11. } else {
  12. current = 1 + CountUpTo(x - 1);
  13. __await;
  14. return current;
  15. }
  16. }
  17. fn main() -> Int {
  18. __continuation k {
  19. CountUpTo(5);
  20. }
  21. var sum: Int = 0;
  22. var count: Int = 5;
  23. while (not (count == 0)) {
  24. __run k;
  25. sum = sum + current;
  26. count = count - 1;
  27. }
  28. return sum;
  29. }