action.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include "executable_semantics/interpreter/action.h"
  5. #include <iterator>
  6. #include <map>
  7. #include <optional>
  8. #include <utility>
  9. #include <vector>
  10. #include "executable_semantics/ast/expression.h"
  11. #include "executable_semantics/ast/function_definition.h"
  12. #include "executable_semantics/common/arena.h"
  13. #include "executable_semantics/interpreter/stack.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. namespace Carbon {
  16. auto Action::MakeLValAction(const Expression* e) -> Action* {
  17. auto* act = global_arena->New<Action>();
  18. act->value = LValAction({.exp = e});
  19. return act;
  20. }
  21. auto Action::MakeExpressionAction(const Expression* e) -> Action* {
  22. auto* act = global_arena->New<Action>();
  23. act->value = ExpressionAction({.exp = e});
  24. return act;
  25. }
  26. auto Action::MakePatternAction(const Pattern* p) -> Action* {
  27. auto* act = global_arena->New<Action>();
  28. act->value = PatternAction({.pattern = p});
  29. return act;
  30. }
  31. auto Action::MakeStatementAction(const Statement* s) -> Action* {
  32. auto* act = global_arena->New<Action>();
  33. act->value = StatementAction({.stmt = s});
  34. return act;
  35. }
  36. auto Action::MakeValAction(const Value* v) -> Action* {
  37. auto* act = global_arena->New<Action>();
  38. act->value = ValAction({.val = v});
  39. return act;
  40. }
  41. auto Action::GetLValAction() const -> const LValAction& {
  42. return std::get<LValAction>(value);
  43. }
  44. auto Action::GetExpressionAction() const -> const ExpressionAction& {
  45. return std::get<ExpressionAction>(value);
  46. }
  47. auto Action::GetPatternAction() const -> const PatternAction& {
  48. return std::get<PatternAction>(value);
  49. }
  50. auto Action::GetStatementAction() const -> const StatementAction& {
  51. return std::get<StatementAction>(value);
  52. }
  53. auto Action::GetValAction() const -> const ValAction& {
  54. return std::get<ValAction>(value);
  55. }
  56. void Action::Print(llvm::raw_ostream& out) const {
  57. switch (tag()) {
  58. case ActionKind::LValAction:
  59. out << *GetLValAction().exp;
  60. break;
  61. case ActionKind::ExpressionAction:
  62. out << *GetExpressionAction().exp;
  63. break;
  64. case ActionKind::PatternAction:
  65. out << *GetPatternAction().pattern;
  66. break;
  67. case ActionKind::StatementAction:
  68. GetStatementAction().stmt->PrintDepth(1, out);
  69. break;
  70. case ActionKind::ValAction:
  71. out << *GetValAction().val;
  72. break;
  73. }
  74. out << "<" << pos << ">";
  75. if (results.size() > 0) {
  76. out << "(";
  77. llvm::ListSeparator sep;
  78. for (auto& result : results) {
  79. out << sep;
  80. if (result) {
  81. out << *result;
  82. }
  83. }
  84. out << ")";
  85. }
  86. }
  87. void Action::PrintList(const Stack<Action*>& ls, llvm::raw_ostream& out) {
  88. llvm::ListSeparator sep(" :: ");
  89. for (const auto& action : ls) {
  90. out << sep << *action;
  91. }
  92. }
  93. } // namespace Carbon