recursive.carbon 1002 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. // RUN: %{executable_semantics} %s 2>&1 | \
  6. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
  7. // RUN: %{executable_semantics} --trace %s 2>&1 | \
  8. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
  9. // AUTOUPDATE: %{executable_semantics} %s
  10. // CHECK: result: 10
  11. package ExecutableSemanticsTest api;
  12. // Test recursive functions inside continuations.
  13. var current: i32 = 0;
  14. fn CountUpTo(x: i32) -> i32 {
  15. if (x == 0) {
  16. current = 0;
  17. __await;
  18. return 0;
  19. } else {
  20. current = 1 + CountUpTo(x - 1);
  21. __await;
  22. return current;
  23. }
  24. }
  25. fn Main() -> i32 {
  26. __continuation k {
  27. CountUpTo(5);
  28. }
  29. var sum: i32 = 0;
  30. var count: i32 = 5;
  31. while (not (count == 0)) {
  32. __run k;
  33. sum = sum + current;
  34. count = count - 1;
  35. }
  36. return sum;
  37. }