statement.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "executable_semantics/ast/statement.h"
  5. #include "common/check.h"
  6. #include "executable_semantics/common/arena.h"
  7. #include "llvm/Support/Casting.h"
  8. namespace Carbon {
  9. using llvm::cast;
  10. void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
  11. if (depth == 0) {
  12. out << " ... ";
  13. return;
  14. }
  15. switch (Tag()) {
  16. case Kind::Match: {
  17. const auto& match = cast<Match>(*this);
  18. out << "match (" << *match.Exp() << ") {";
  19. if (depth < 0 || depth > 1) {
  20. out << "\n";
  21. for (auto& clause : match.Clauses()) {
  22. out << "case " << *clause.first << " =>\n";
  23. clause.second->PrintDepth(depth - 1, out);
  24. out << "\n";
  25. }
  26. } else {
  27. out << "...";
  28. }
  29. out << "}";
  30. break;
  31. }
  32. case Kind::While: {
  33. const auto& while_stmt = cast<While>(*this);
  34. out << "while (" << *while_stmt.Cond() << ")\n";
  35. while_stmt.Body()->PrintDepth(depth - 1, out);
  36. break;
  37. }
  38. case Kind::Break:
  39. out << "break;";
  40. break;
  41. case Kind::Continue:
  42. out << "continue;";
  43. break;
  44. case Kind::VariableDefinition: {
  45. const auto& var = cast<VariableDefinition>(*this);
  46. out << "var " << *var.Pat() << " = " << *var.Init() << ";";
  47. break;
  48. }
  49. case Kind::ExpressionStatement:
  50. out << *cast<ExpressionStatement>(*this).Exp() << ";";
  51. break;
  52. case Kind::Assign: {
  53. const auto& assign = cast<Assign>(*this);
  54. out << *assign.Lhs() << " = " << *assign.Rhs() << ";";
  55. break;
  56. }
  57. case Kind::If: {
  58. const auto& if_stmt = cast<If>(*this);
  59. out << "if (" << *if_stmt.Cond() << ")\n";
  60. if_stmt.ThenStmt()->PrintDepth(depth - 1, out);
  61. if (if_stmt.ElseStmt()) {
  62. out << "\nelse\n";
  63. (*if_stmt.ElseStmt())->PrintDepth(depth - 1, out);
  64. }
  65. break;
  66. }
  67. case Kind::Return: {
  68. const auto& ret = cast<Return>(*this);
  69. if (ret.IsOmittedExp()) {
  70. out << "return;";
  71. } else {
  72. out << "return " << *ret.Exp() << ";";
  73. }
  74. break;
  75. }
  76. case Kind::Sequence: {
  77. const auto& seq = cast<Sequence>(*this);
  78. seq.Stmt()->PrintDepth(depth, out);
  79. if (depth < 0 || depth > 1) {
  80. out << "\n";
  81. } else {
  82. out << " ";
  83. }
  84. if (seq.Next()) {
  85. (*seq.Next())->PrintDepth(depth - 1, out);
  86. }
  87. break;
  88. }
  89. case Kind::Block: {
  90. const auto& block = cast<Block>(*this);
  91. out << "{";
  92. if (depth < 0 || depth > 1) {
  93. out << "\n";
  94. }
  95. if (block.Stmt()) {
  96. (*block.Stmt())->PrintDepth(depth, out);
  97. if (depth < 0 || depth > 1) {
  98. out << "\n";
  99. }
  100. }
  101. out << "}";
  102. if (depth < 0 || depth > 1) {
  103. out << "\n";
  104. }
  105. break;
  106. }
  107. case Kind::Continuation: {
  108. const auto& cont = cast<Continuation>(*this);
  109. out << "continuation " << cont.ContinuationVariable() << " ";
  110. if (depth < 0 || depth > 1) {
  111. out << "\n";
  112. }
  113. cont.Body()->PrintDepth(depth - 1, out);
  114. if (depth < 0 || depth > 1) {
  115. out << "\n";
  116. }
  117. break;
  118. }
  119. case Kind::Run:
  120. out << "run " << *cast<Run>(*this).Argument() << ";";
  121. break;
  122. case Kind::Await:
  123. out << "await;";
  124. break;
  125. }
  126. }
  127. } // namespace Carbon