statement.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef EXECUTABLE_SEMANTICS_AST_STATEMENT_H_
  5. #define EXECUTABLE_SEMANTICS_AST_STATEMENT_H_
  6. #include <list>
  7. #include "executable_semantics/ast/expression.h"
  8. namespace Carbon {
  9. enum class StatementKind {
  10. ExpressionStatement,
  11. Assign,
  12. VariableDefinition,
  13. If,
  14. Return,
  15. Sequence,
  16. Block,
  17. While,
  18. Break,
  19. Continue,
  20. Match,
  21. Continuation, // Create a first-class continuation.
  22. Run, // Run a continuation to the next await or until it finishes..
  23. Await, // Pause execution of the continuation.
  24. };
  25. struct Statement {
  26. int line_num;
  27. StatementKind tag;
  28. union {
  29. Expression* exp;
  30. struct {
  31. Expression* lhs;
  32. Expression* rhs;
  33. } assign;
  34. struct {
  35. Expression* pat;
  36. Expression* init;
  37. } variable_definition;
  38. struct {
  39. Expression* cond;
  40. Statement* then_stmt;
  41. Statement* else_stmt;
  42. } if_stmt;
  43. Expression* return_stmt;
  44. struct {
  45. Statement* stmt;
  46. Statement* next;
  47. } sequence;
  48. struct {
  49. Statement* stmt;
  50. } block;
  51. struct {
  52. Expression* cond;
  53. Statement* body;
  54. } while_stmt;
  55. struct {
  56. Expression* exp;
  57. std::list<std::pair<Expression*, Statement*>>* clauses;
  58. } match_stmt;
  59. struct {
  60. std::string* continuation_variable;
  61. Statement* body;
  62. } continuation;
  63. struct {
  64. Expression* argument;
  65. } run;
  66. } u;
  67. };
  68. auto MakeExpStmt(int line_num, Expression* exp) -> Statement*;
  69. auto MakeAssign(int line_num, Expression* lhs, Expression* rhs) -> Statement*;
  70. auto MakeVarDef(int line_num, Expression* pat, Expression* init) -> Statement*;
  71. auto MakeIf(int line_num, Expression* cond, Statement* then_stmt,
  72. Statement* else_stmt) -> Statement*;
  73. auto MakeReturn(int line_num, Expression* e) -> Statement*;
  74. auto MakeSeq(int line_num, Statement* s1, Statement* s2) -> Statement*;
  75. auto MakeBlock(int line_num, Statement* s) -> Statement*;
  76. auto MakeWhile(int line_num, Expression* cond, Statement* body) -> Statement*;
  77. auto MakeBreak(int line_num) -> Statement*;
  78. auto MakeContinue(int line_num) -> Statement*;
  79. auto MakeMatch(int line_num, Expression* exp,
  80. std::list<std::pair<Expression*, Statement*>>* clauses)
  81. -> Statement*;
  82. // Returns an AST node for a continuation statement give its line number and
  83. // contituent parts.
  84. //
  85. // __continuation <continuation_variable> {
  86. // <body>
  87. // }
  88. auto MakeContinuationStatement(int line_num, std::string continuation_variable,
  89. Statement* body) -> Statement*;
  90. // Returns an AST node for a run statement give its line number and argument.
  91. //
  92. // __run <argument>;
  93. auto MakeRun(int line_num, Expression* argument) -> Statement*;
  94. // Returns an AST node for an await statement give its line number.
  95. //
  96. // __await;
  97. auto MakeAwait(int line_num) -> Statement*;
  98. void PrintStatement(Statement*, int);
  99. } // namespace Carbon
  100. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_