action.cpp 2.3 KB

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