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