statement.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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, Expression exp) -> const Statement*;
  67. static auto MakeAssign(int line_num, Expression lhs, Expression rhs)
  68. -> const Statement*;
  69. static auto MakeVarDef(int line_num, Expression pat, Expression init)
  70. -> const Statement*;
  71. static auto MakeIf(int line_num, Expression cond, const Statement* then_stmt,
  72. const Statement* else_stmt) -> const Statement*;
  73. static auto MakeReturn(int line_num, Expression e) -> const Statement*;
  74. static auto MakeSeq(int line_num, const Statement* s1, const Statement* s2)
  75. -> const Statement*;
  76. static auto MakeBlock(int line_num, const Statement* s) -> const Statement*;
  77. static auto MakeWhile(int line_num, Expression cond, const Statement* body)
  78. -> const Statement*;
  79. static auto MakeBreak(int line_num) -> const Statement*;
  80. static auto MakeContinue(int line_num) -> const Statement*;
  81. static auto MakeMatch(
  82. int line_num, Expression exp,
  83. std::list<std::pair<const Expression*, const Statement*>>* clauses)
  84. -> const Statement*;
  85. // Returns an AST node for a continuation statement give its line number and
  86. // contituent parts.
  87. //
  88. // __continuation <continuation_variable> {
  89. // <body>
  90. // }
  91. static auto MakeContinuation(int line_num, std::string continuation_variable,
  92. const Statement* body) -> const Statement*;
  93. // Returns an AST node for a run statement give its line number and argument.
  94. //
  95. // __run <argument>;
  96. static auto MakeRun(int line_num, Expression argument) -> const Statement*;
  97. // Returns an AST node for an await statement give its line number.
  98. //
  99. // __await;
  100. static auto MakeAwait(int line_num) -> const Statement*;
  101. // Access to the alternatives
  102. const Expression* GetExpression() const;
  103. Assignment GetAssign() const;
  104. VariableDefinition GetVariableDefinition() const;
  105. IfStatement GetIf() const;
  106. const Expression* GetReturn() const;
  107. Sequence GetSequence() const;
  108. Block GetBlock() const;
  109. While GetWhile() const;
  110. Match GetMatch() const;
  111. Continuation GetContinuation() const;
  112. Run GetRun() const;
  113. private:
  114. union {
  115. const Expression* exp;
  116. Assignment assign;
  117. VariableDefinition variable_definition;
  118. IfStatement if_stmt;
  119. const Expression* return_stmt;
  120. Sequence sequence;
  121. Block block;
  122. While while_stmt;
  123. Match match_stmt;
  124. Continuation continuation;
  125. Run run;
  126. } u;
  127. };
  128. void PrintStatement(const Statement*, int);
  129. } // namespace Carbon
  130. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_