action.h 3.5 KB

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