experimental_continuation6.carbon 696 B

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