choice.carbon 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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: 0
  11. package ExecutableSemanticsTest api;
  12. choice Ints {
  13. None,
  14. One(i32),
  15. Two(i32,i32)
  16. }
  17. fn Main() -> i32 {
  18. var x: auto = Ints.None();
  19. var y: auto = Ints.One(42);
  20. var n: auto = 0;
  21. match (y) {
  22. case Ints.None =>
  23. n = n + 2;
  24. case Ints.One(x: auto) =>
  25. n = x + 1 - 42;
  26. case Ints.Two(a: auto, b: auto) =>
  27. n = 2;
  28. }
  29. match (x) {
  30. case Ints.One(x: auto) =>
  31. n = x + 2;
  32. case Ints.None() =>
  33. n = n - 1;
  34. case Ints.Two(x: auto, y: auto) =>
  35. n = 5;
  36. }
  37. return n;
  38. }
  39. // Test some alternate syntaxes
  40. choice MoreInts {
  41. None(),
  42. One(i32),
  43. Two(i32,i32),
  44. }