statement.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. namespace Carbon {
  8. auto Statement::GetExpressionStatement() const -> const ExpressionStatement& {
  9. return std::get<ExpressionStatement>(value);
  10. }
  11. auto Statement::GetAssign() const -> const Assign& {
  12. return std::get<Assign>(value);
  13. }
  14. auto Statement::GetVariableDefinition() const -> const VariableDefinition& {
  15. return std::get<VariableDefinition>(value);
  16. }
  17. auto Statement::GetIf() const -> const If& { return std::get<If>(value); }
  18. auto Statement::GetReturn() const -> const Return& {
  19. return std::get<Return>(value);
  20. }
  21. auto Statement::GetSequence() const -> const Sequence& {
  22. return std::get<Sequence>(value);
  23. }
  24. auto Statement::GetBlock() const -> const Block& {
  25. return std::get<Block>(value);
  26. }
  27. auto Statement::GetWhile() const -> const While& {
  28. return std::get<While>(value);
  29. }
  30. auto Statement::GetBreak() const -> const Break& {
  31. return std::get<Break>(value);
  32. }
  33. auto Statement::GetContinue() const -> const Continue& {
  34. return std::get<Continue>(value);
  35. }
  36. auto Statement::GetMatch() const -> const Match& {
  37. return std::get<Match>(value);
  38. }
  39. auto Statement::GetContinuation() const -> const Continuation& {
  40. return std::get<Continuation>(value);
  41. }
  42. auto Statement::GetRun() const -> const Run& { return std::get<Run>(value); }
  43. auto Statement::GetAwait() const -> const Await& {
  44. return std::get<Await>(value);
  45. }
  46. auto Statement::MakeExpressionStatement(int line_num, const Expression* exp)
  47. -> const Statement* {
  48. auto* s = global_arena->New<Statement>();
  49. s->line_num = line_num;
  50. s->value = ExpressionStatement({.exp = exp});
  51. return s;
  52. }
  53. auto Statement::MakeAssign(int line_num, const Expression* lhs,
  54. const Expression* rhs) -> const Statement* {
  55. auto* s = global_arena->New<Statement>();
  56. s->line_num = line_num;
  57. s->value = Assign({.lhs = lhs, .rhs = rhs});
  58. return s;
  59. }
  60. auto Statement::MakeVariableDefinition(int line_num, const Pattern* pat,
  61. const Expression* init)
  62. -> const Statement* {
  63. auto* s = global_arena->New<Statement>();
  64. s->line_num = line_num;
  65. s->value = VariableDefinition({.pat = pat, .init = init});
  66. return s;
  67. }
  68. auto Statement::MakeIf(int line_num, const Expression* cond,
  69. const Statement* then_stmt, const Statement* else_stmt)
  70. -> const Statement* {
  71. auto* s = global_arena->New<Statement>();
  72. s->line_num = line_num;
  73. s->value = If({.cond = cond, .then_stmt = then_stmt, .else_stmt = else_stmt});
  74. return s;
  75. }
  76. auto Statement::MakeWhile(int line_num, const Expression* cond,
  77. const Statement* body) -> const Statement* {
  78. auto* s = global_arena->New<Statement>();
  79. s->line_num = line_num;
  80. s->value = While({.cond = cond, .body = body});
  81. return s;
  82. }
  83. auto Statement::MakeBreak(int line_num) -> const Statement* {
  84. auto* s = global_arena->New<Statement>();
  85. s->line_num = line_num;
  86. s->value = Break();
  87. return s;
  88. }
  89. auto Statement::MakeContinue(int line_num) -> const Statement* {
  90. auto* s = global_arena->New<Statement>();
  91. s->line_num = line_num;
  92. s->value = Continue();
  93. return s;
  94. }
  95. auto Statement::MakeReturn(int line_num, const Expression* exp,
  96. bool is_omitted_exp) -> const Statement* {
  97. auto* s = global_arena->New<Statement>();
  98. s->line_num = line_num;
  99. if (exp == nullptr) {
  100. CHECK(is_omitted_exp);
  101. exp = global_arena->New<TupleLiteral>(line_num);
  102. }
  103. s->value = Return({.exp = exp, .is_omitted_exp = is_omitted_exp});
  104. return s;
  105. }
  106. auto Statement::MakeSequence(int line_num, const Statement* s1,
  107. const Statement* s2) -> const Statement* {
  108. auto* s = global_arena->New<Statement>();
  109. s->line_num = line_num;
  110. s->value = Sequence({.stmt = s1, .next = s2});
  111. return s;
  112. }
  113. auto Statement::MakeBlock(int line_num, const Statement* stmt)
  114. -> const Statement* {
  115. auto* s = global_arena->New<Statement>();
  116. s->line_num = line_num;
  117. s->value = Block({.stmt = stmt});
  118. return s;
  119. }
  120. auto Statement::MakeMatch(
  121. int line_num, const Expression* exp,
  122. std::list<std::pair<const Pattern*, const Statement*>>* clauses)
  123. -> const Statement* {
  124. auto* s = global_arena->New<Statement>();
  125. s->line_num = line_num;
  126. s->value = Match({.exp = exp, .clauses = clauses});
  127. return s;
  128. }
  129. // Returns an AST node for a continuation statement give its line number and
  130. // parts.
  131. auto Statement::MakeContinuation(int line_num,
  132. std::string continuation_variable,
  133. const Statement* body) -> const Statement* {
  134. auto* s = global_arena->New<Statement>();
  135. s->line_num = line_num;
  136. s->value =
  137. Continuation({.continuation_variable = std::move(continuation_variable),
  138. .body = body});
  139. return s;
  140. }
  141. // Returns an AST node for a run statement give its line number and argument.
  142. auto Statement::MakeRun(int line_num, const Expression* argument)
  143. -> const Statement* {
  144. auto* s = global_arena->New<Statement>();
  145. s->line_num = line_num;
  146. s->value = Run({.argument = argument});
  147. return s;
  148. }
  149. // Returns an AST node for an await statement give its line number.
  150. auto Statement::MakeAwait(int line_num) -> const Statement* {
  151. auto* s = global_arena->New<Statement>();
  152. s->line_num = line_num;
  153. s->value = Await();
  154. return s;
  155. }
  156. void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
  157. if (depth == 0) {
  158. out << " ... ";
  159. return;
  160. }
  161. switch (tag()) {
  162. case StatementKind::Match:
  163. out << "match (" << *GetMatch().exp << ") {";
  164. if (depth < 0 || depth > 1) {
  165. out << "\n";
  166. for (auto& clause : *GetMatch().clauses) {
  167. out << "case " << *clause.first << " =>\n";
  168. clause.second->PrintDepth(depth - 1, out);
  169. out << "\n";
  170. }
  171. } else {
  172. out << "...";
  173. }
  174. out << "}";
  175. break;
  176. case StatementKind::While:
  177. out << "while (" << *GetWhile().cond << ")\n";
  178. GetWhile().body->PrintDepth(depth - 1, out);
  179. break;
  180. case StatementKind::Break:
  181. out << "break;";
  182. break;
  183. case StatementKind::Continue:
  184. out << "continue;";
  185. break;
  186. case StatementKind::VariableDefinition:
  187. out << "var " << *GetVariableDefinition().pat << " = "
  188. << *GetVariableDefinition().init << ";";
  189. break;
  190. case StatementKind::ExpressionStatement:
  191. out << *GetExpressionStatement().exp << ";";
  192. break;
  193. case StatementKind::Assign:
  194. out << *GetAssign().lhs << " = " << *GetAssign().rhs << ";";
  195. break;
  196. case StatementKind::If:
  197. out << "if (" << *GetIf().cond << ")\n";
  198. GetIf().then_stmt->PrintDepth(depth - 1, out);
  199. if (GetIf().else_stmt) {
  200. out << "\nelse\n";
  201. GetIf().else_stmt->PrintDepth(depth - 1, out);
  202. }
  203. break;
  204. case StatementKind::Return:
  205. if (GetReturn().is_omitted_exp) {
  206. out << "return;";
  207. } else {
  208. out << "return " << *GetReturn().exp << ";";
  209. }
  210. break;
  211. case StatementKind::Sequence:
  212. GetSequence().stmt->PrintDepth(depth, out);
  213. if (depth < 0 || depth > 1) {
  214. out << "\n";
  215. } else {
  216. out << " ";
  217. }
  218. if (GetSequence().next) {
  219. GetSequence().next->PrintDepth(depth - 1, out);
  220. }
  221. break;
  222. case StatementKind::Block:
  223. out << "{";
  224. if (depth < 0 || depth > 1) {
  225. out << "\n";
  226. }
  227. if (GetBlock().stmt) {
  228. GetBlock().stmt->PrintDepth(depth, out);
  229. if (depth < 0 || depth > 1) {
  230. out << "\n";
  231. }
  232. }
  233. out << "}";
  234. if (depth < 0 || depth > 1) {
  235. out << "\n";
  236. }
  237. break;
  238. case StatementKind::Continuation:
  239. out << "continuation " << GetContinuation().continuation_variable << " ";
  240. if (depth < 0 || depth > 1) {
  241. out << "\n";
  242. }
  243. GetContinuation().body->PrintDepth(depth - 1, out);
  244. if (depth < 0 || depth > 1) {
  245. out << "\n";
  246. }
  247. break;
  248. case StatementKind::Run:
  249. out << "run " << *GetRun().argument << ";";
  250. break;
  251. case StatementKind::Await:
  252. out << "await;";
  253. break;
  254. }
  255. }
  256. } // namespace Carbon