action.cpp 2.7 KB

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