action.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <iostream>
  6. #include <iterator>
  7. #include <map>
  8. #include <optional>
  9. #include <utility>
  10. #include <vector>
  11. #include "executable_semantics/ast/expression.h"
  12. #include "executable_semantics/ast/function_definition.h"
  13. #include "executable_semantics/interpreter/stack.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(std::ostream& out) {
  48. switch (tag()) {
  49. case ActionKind::LValAction:
  50. PrintExp(GetLValAction().exp);
  51. break;
  52. case ActionKind::ExpressionAction:
  53. PrintExp(GetExpressionAction().exp);
  54. break;
  55. case ActionKind::StatementAction:
  56. PrintStatement(GetStatementAction().stmt, 1);
  57. break;
  58. case ActionKind::ValAction:
  59. PrintValue(GetValAction().val, out);
  60. break;
  61. }
  62. out << "<" << pos << ">";
  63. if (results.size() > 0) {
  64. out << "(";
  65. for (auto& result : results) {
  66. if (result) {
  67. PrintValue(result, out);
  68. }
  69. out << ",";
  70. }
  71. out << ")";
  72. }
  73. }
  74. void Action::PrintList(Stack<Action*> ls, std::ostream& out) {
  75. if (!ls.IsEmpty()) {
  76. ls.Pop()->Print(out);
  77. if (!ls.IsEmpty()) {
  78. out << " :: ";
  79. PrintList(ls, out);
  80. }
  81. }
  82. }
  83. } // namespace Carbon