statement.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. struct Assignment {
  27. const Expression* lhs;
  28. const Expression* rhs;
  29. };
  30. struct VariableDefinition {
  31. const Expression* pat;
  32. const Expression* init;
  33. };
  34. struct IfStatement {
  35. const Expression* cond;
  36. const Statement* then_stmt;
  37. const Statement* else_stmt;
  38. };
  39. struct Sequence {
  40. const Statement* stmt;
  41. const Statement* next;
  42. };
  43. struct Block {
  44. const Statement* stmt;
  45. };
  46. struct While {
  47. const Expression* cond;
  48. const Statement* body;
  49. };
  50. struct Match {
  51. const Expression* exp;
  52. std::list<std::pair<const Expression*, const Statement*>>* clauses;
  53. };
  54. struct Continuation {
  55. std::string* continuation_variable;
  56. const Statement* body;
  57. };
  58. struct Run {
  59. const Expression* argument;
  60. };
  61. struct Statement {
  62. // TODO: change Statement to a class and make all members private
  63. int line_num;
  64. StatementKind tag;
  65. // Constructors
  66. static auto MakeExpStmt(int line_num, const Expression* exp)
  67. -> const Statement*;
  68. static auto MakeAssign(int line_num, const Expression* lhs,
  69. const Expression* rhs) -> const Statement*;
  70. static auto MakeVarDef(int line_num, const Expression* pat,
  71. const Expression* init) -> const Statement*;
  72. static auto MakeIf(int line_num, const Expression* cond,
  73. const Statement* then_stmt, const Statement* else_stmt)
  74. -> const Statement*;
  75. static auto MakeReturn(int line_num, const Expression* e) -> const Statement*;
  76. static auto MakeSeq(int line_num, const Statement* s1, const Statement* s2)
  77. -> const Statement*;
  78. static auto MakeBlock(int line_num, const Statement* s) -> const Statement*;
  79. static auto MakeWhile(int line_num, const Expression* cond,
  80. const Statement* body) -> const Statement*;
  81. static auto MakeBreak(int line_num) -> const Statement*;
  82. static auto MakeContinue(int line_num) -> const Statement*;
  83. static auto MakeMatch(
  84. int line_num, const Expression* exp,
  85. std::list<std::pair<const Expression*, const Statement*>>* clauses)
  86. -> const Statement*;
  87. // Returns an AST node for a continuation statement give its line number and
  88. // contituent parts.
  89. //
  90. // __continuation <continuation_variable> {
  91. // <body>
  92. // }
  93. static auto MakeContinuation(int line_num, std::string continuation_variable,
  94. const Statement* body) -> const Statement*;
  95. // Returns an AST node for a run statement give its line number and argument.
  96. //
  97. // __run <argument>;
  98. static auto MakeRun(int line_num, const Expression* argument)
  99. -> const Statement*;
  100. // Returns an AST node for an await statement give its line number.
  101. //
  102. // __await;
  103. static auto MakeAwait(int line_num) -> const Statement*;
  104. // Access to the alternatives
  105. const Expression* GetExpression() const;
  106. Assignment GetAssign() const;
  107. VariableDefinition GetVariableDefinition() const;
  108. IfStatement GetIf() const;
  109. const Expression* GetReturn() const;
  110. Sequence GetSequence() const;
  111. Block GetBlock() const;
  112. While GetWhile() const;
  113. Match GetMatch() const;
  114. Continuation GetContinuation() const;
  115. Run GetRun() const;
  116. private:
  117. union {
  118. const Expression* exp;
  119. Assignment assign;
  120. VariableDefinition variable_definition;
  121. IfStatement if_stmt;
  122. const Expression* return_stmt;
  123. Sequence sequence;
  124. Block block;
  125. While while_stmt;
  126. Match match_stmt;
  127. Continuation continuation;
  128. Run run;
  129. } u;
  130. };
  131. void PrintStatement(const Statement*, int);
  132. } // namespace Carbon
  133. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_