statement.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 (kind()) {
  16. case Kind::Match: {
  17. const auto& match = cast<Match>(*this);
  18. out << "match (" << match.expression() << ") {";
  19. if (depth < 0 || depth > 1) {
  20. out << "\n";
  21. for (auto& clause : match.clauses()) {
  22. out << "case " << clause.pattern() << " =>\n";
  23. clause.statement().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.condition() << ")\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.pattern() << " = " << var.init() << ";";
  47. break;
  48. }
  49. case Kind::ExpressionStatement:
  50. out << cast<ExpressionStatement>(*this).expression() << ";";
  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.condition() << ")\n";
  60. if_stmt.then_block().PrintDepth(depth - 1, out);
  61. if (if_stmt.else_block()) {
  62. out << "\nelse\n";
  63. (*if_stmt.else_block())->PrintDepth(depth - 1, out);
  64. }
  65. break;
  66. }
  67. case Kind::Return: {
  68. const auto& ret = cast<Return>(*this);
  69. if (ret.is_omitted_expression()) {
  70. out << "return;";
  71. } else {
  72. out << "return " << ret.expression() << ";";
  73. }
  74. break;
  75. }
  76. case Kind::Block: {
  77. const auto& block = cast<Block>(*this);
  78. out << "{";
  79. if (depth < 0 || depth > 1) {
  80. out << "\n";
  81. }
  82. for (const auto* statement : block.statements()) {
  83. statement->PrintDepth(depth, out);
  84. if (depth < 0 || depth > 1) {
  85. out << "\n";
  86. }
  87. }
  88. out << "}";
  89. if (depth < 0 || depth > 1) {
  90. out << "\n";
  91. }
  92. break;
  93. }
  94. case Kind::Continuation: {
  95. const auto& cont = cast<Continuation>(*this);
  96. out << "continuation " << cont.continuation_variable() << " ";
  97. if (depth < 0 || depth > 1) {
  98. out << "\n";
  99. }
  100. cont.body().PrintDepth(depth - 1, out);
  101. if (depth < 0 || depth > 1) {
  102. out << "\n";
  103. }
  104. break;
  105. }
  106. case Kind::Run:
  107. out << "run " << cast<Run>(*this).argument() << ";";
  108. break;
  109. case Kind::Await:
  110. out << "await;";
  111. break;
  112. }
  113. }
  114. } // namespace Carbon