statement.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. };
  22. struct Statement {
  23. int line_num;
  24. StatementKind tag;
  25. union {
  26. Expression* exp;
  27. struct {
  28. Expression* lhs;
  29. Expression* rhs;
  30. } assign;
  31. struct {
  32. Expression* pat;
  33. Expression* init;
  34. } variable_definition;
  35. struct {
  36. Expression* cond;
  37. Statement* then_stmt;
  38. Statement* else_stmt;
  39. } if_stmt;
  40. Expression* return_stmt;
  41. struct {
  42. Statement* stmt;
  43. Statement* next;
  44. } sequence;
  45. struct {
  46. Statement* stmt;
  47. } block;
  48. struct {
  49. Expression* cond;
  50. Statement* body;
  51. } while_stmt;
  52. struct {
  53. Expression* exp;
  54. std::list<std::pair<Expression*, Statement*>>* clauses;
  55. } match_stmt;
  56. } u;
  57. };
  58. auto MakeExpStmt(int line_num, Expression* exp) -> Statement*;
  59. auto MakeAssign(int line_num, Expression* lhs, Expression* rhs) -> Statement*;
  60. auto MakeVarDef(int line_num, Expression* pat, Expression* init) -> Statement*;
  61. auto MakeIf(int line_num, Expression* cond, Statement* then_stmt,
  62. Statement* else_stmt) -> Statement*;
  63. auto MakeReturn(int line_num, Expression* e) -> Statement*;
  64. auto MakeSeq(int line_num, Statement* s1, Statement* s2) -> Statement*;
  65. auto MakeBlock(int line_num, Statement* s) -> Statement*;
  66. auto MakeWhile(int line_num, Expression* cond, Statement* body) -> Statement*;
  67. auto MakeBreak(int line_num) -> Statement*;
  68. auto MakeContinue(int line_num) -> Statement*;
  69. auto MakeMatch(int line_num, Expression* exp,
  70. std::list<std::pair<Expression*, Statement*>>* clauses)
  71. -> Statement*;
  72. void PrintStatement(Statement*, int);
  73. } // namespace Carbon
  74. #endif // EXECUTABLE_SEMANTICS_AST_STATEMENT_H_