action.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/statement.h"
  10. #include "executable_semantics/interpreter/stack.h"
  11. #include "executable_semantics/interpreter/value.h"
  12. namespace Carbon {
  13. enum class ActionKind {
  14. LValAction,
  15. ExpressionAction,
  16. StatementAction,
  17. ValAction,
  18. };
  19. struct LValAction {
  20. static constexpr ActionKind Kind = ActionKind::LValAction;
  21. const Expression* exp;
  22. };
  23. struct ExpressionAction {
  24. static constexpr ActionKind Kind = ActionKind::ExpressionAction;
  25. const Expression* exp;
  26. };
  27. struct StatementAction {
  28. static constexpr ActionKind Kind = ActionKind::StatementAction;
  29. const Statement* stmt;
  30. };
  31. struct ValAction {
  32. static constexpr ActionKind Kind = ActionKind::ValAction;
  33. const Value* val;
  34. };
  35. struct Action {
  36. static auto MakeLValAction(const Expression* e) -> Action*;
  37. static auto MakeExpressionAction(const Expression* e) -> Action*;
  38. static auto MakeStatementAction(const Statement* s) -> Action*;
  39. static auto MakeValAction(const Value* v) -> Action*;
  40. static void PrintList(const Stack<Action*>& ls, llvm::raw_ostream& out);
  41. auto GetLValAction() const -> const LValAction&;
  42. auto GetExpressionAction() const -> const ExpressionAction&;
  43. auto GetStatementAction() const -> const StatementAction&;
  44. auto GetValAction() const -> const ValAction&;
  45. void Print(llvm::raw_ostream& out) const;
  46. inline auto tag() const -> ActionKind {
  47. return std::visit([](const auto& t) { return t.Kind; }, value);
  48. }
  49. // The position or state of the action. Starts at 0 and goes up to the number
  50. // of subexpressions.
  51. //
  52. // pos indicates how many of the entries in the following `results` vector
  53. // will be filled in the next time this action is active.
  54. // For each i < pos, results[i] contains a pointer to a Value.
  55. int pos = 0;
  56. // Results from a subexpression.
  57. std::vector<const Value*> results;
  58. private:
  59. std::variant<LValAction, ExpressionAction, StatementAction, ValAction> value;
  60. };
  61. } // namespace Carbon
  62. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_