action.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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::MakeStatementAction(const Statement* s) -> Action* {
  26. auto* act = new Action();
  27. act->value = StatementAction({.stmt = s});
  28. return act;
  29. }
  30. auto Action::MakeValAction(const Value* v) -> Action* {
  31. auto* act = new Action();
  32. act->value = ValAction({.val = v});
  33. return act;
  34. }
  35. auto Action::GetLValAction() const -> const LValAction& {
  36. return std::get<LValAction>(value);
  37. }
  38. auto Action::GetExpressionAction() const -> const ExpressionAction& {
  39. return std::get<ExpressionAction>(value);
  40. }
  41. auto Action::GetStatementAction() const -> const StatementAction& {
  42. return std::get<StatementAction>(value);
  43. }
  44. auto Action::GetValAction() const -> const ValAction& {
  45. return std::get<ValAction>(value);
  46. }
  47. void Action::Print(llvm::raw_ostream& out) const {
  48. switch (tag()) {
  49. case ActionKind::LValAction:
  50. out << *GetLValAction().exp;
  51. break;
  52. case ActionKind::ExpressionAction:
  53. out << *GetExpressionAction().exp;
  54. break;
  55. case ActionKind::StatementAction:
  56. GetStatementAction().stmt->PrintDepth(1, out);
  57. break;
  58. case ActionKind::ValAction:
  59. out << *GetValAction().val;
  60. break;
  61. }
  62. out << "<" << pos << ">";
  63. if (results.size() > 0) {
  64. out << "(";
  65. llvm::ListSeparator sep;
  66. for (auto& result : results) {
  67. out << sep;
  68. if (result) {
  69. out << *result;
  70. }
  71. }
  72. out << ")";
  73. }
  74. }
  75. void Action::PrintList(const Stack<Action*>& ls, llvm::raw_ostream& out) {
  76. llvm::ListSeparator sep(" :: ");
  77. for (const auto& action : ls) {
  78. out << sep << *action;
  79. }
  80. }
  81. } // namespace Carbon