action.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_INTERPRETER_ACTION_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_
  6. #include <vector>
  7. #include "common/ostream.h"
  8. #include "executable_semantics/ast/expression.h"
  9. #include "executable_semantics/ast/pattern.h"
  10. #include "executable_semantics/ast/statement.h"
  11. #include "executable_semantics/interpreter/stack.h"
  12. #include "executable_semantics/interpreter/value.h"
  13. #include "llvm/Support/Compiler.h"
  14. namespace Carbon {
  15. class Action {
  16. public:
  17. enum class Kind {
  18. LValAction,
  19. ExpressionAction,
  20. PatternAction,
  21. StatementAction,
  22. };
  23. Action(const Value&) = delete;
  24. auto operator=(const Value&) -> Action& = delete;
  25. void AddResult(Nonnull<const Value*> result) { results_.push_back(result); }
  26. void Clear() {
  27. pos_ = 0;
  28. results_.clear();
  29. }
  30. static void PrintList(const Stack<Nonnull<Action*>>& ls,
  31. llvm::raw_ostream& out);
  32. void Print(llvm::raw_ostream& out) const;
  33. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  34. // Returns the enumerator corresponding to the most-derived type of this
  35. // object.
  36. auto kind() const -> Kind { return kind_; }
  37. // The position or state of the action. Starts at 0 and goes up to the number
  38. // of subexpressions.
  39. //
  40. // pos indicates how many of the entries in the following `results` vector
  41. // will be filled in the next time this action is active.
  42. // For each i < pos, results[i] contains a pointer to a Value.
  43. auto pos() const -> int { return pos_; }
  44. void set_pos(int pos) { this->pos_ = pos; }
  45. // Results from a subexpression.
  46. auto results() const -> const std::vector<Nonnull<const Value*>>& {
  47. return results_;
  48. }
  49. protected:
  50. // Constructs an Action. `kind` must be the enumerator corresponding to the
  51. // most-derived type being constructed.
  52. explicit Action(Kind kind) : kind_(kind) {}
  53. private:
  54. int pos_ = 0;
  55. std::vector<Nonnull<const Value*>> results_;
  56. const Kind kind_;
  57. };
  58. class LValAction : public Action {
  59. public:
  60. explicit LValAction(Nonnull<const Expression*> exp)
  61. : Action(Kind::LValAction), exp(exp) {}
  62. static auto classof(const Action* action) -> bool {
  63. return action->kind() == Kind::LValAction;
  64. }
  65. auto Exp() const -> Nonnull<const Expression*> { return exp; }
  66. private:
  67. Nonnull<const Expression*> exp;
  68. };
  69. class ExpressionAction : public Action {
  70. public:
  71. explicit ExpressionAction(Nonnull<const Expression*> exp)
  72. : Action(Kind::ExpressionAction), exp(exp) {}
  73. static auto classof(const Action* action) -> bool {
  74. return action->kind() == Kind::ExpressionAction;
  75. }
  76. auto Exp() const -> Nonnull<const Expression*> { return exp; }
  77. private:
  78. Nonnull<const Expression*> exp;
  79. };
  80. class PatternAction : public Action {
  81. public:
  82. explicit PatternAction(Nonnull<const Pattern*> pat)
  83. : Action(Kind::PatternAction), pat(pat) {}
  84. static auto classof(const Action* action) -> bool {
  85. return action->kind() == Kind::PatternAction;
  86. }
  87. auto Pat() const -> Nonnull<const Pattern*> { return pat; }
  88. private:
  89. Nonnull<const Pattern*> pat;
  90. };
  91. class StatementAction : public Action {
  92. public:
  93. explicit StatementAction(Nonnull<const Statement*> stmt)
  94. : Action(Kind::StatementAction), stmt(stmt) {}
  95. static auto classof(const Action* action) -> bool {
  96. return action->kind() == Kind::StatementAction;
  97. }
  98. auto Stmt() const -> Nonnull<const Statement*> { return stmt; }
  99. private:
  100. Nonnull<const Statement*> stmt;
  101. };
  102. } // namespace Carbon
  103. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_