recursive.carbon 784 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. //
  5. // AUTOUPDATE
  6. // RUN: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: result: 10
  9. package ExplorerTest api;
  10. // Test recursive functions inside continuations.
  11. var current: i32 = 0;
  12. fn CountUpTo(x: i32) -> i32 {
  13. if (x == 0) {
  14. current = 0;
  15. __await;
  16. return 0;
  17. } else {
  18. current = 1 + CountUpTo(x - 1);
  19. __await;
  20. return current;
  21. }
  22. }
  23. fn Main() -> i32 {
  24. __continuation k {
  25. CountUpTo(5);
  26. }
  27. var sum: i32 = 0;
  28. var count: i32 = 5;
  29. while (not (count == 0)) {
  30. __run k;
  31. sum = sum + current;
  32. count = count - 1;
  33. }
  34. return sum;
  35. }