action.cpp 2.5 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 <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. void PrintAct(Action* act, std::ostream& out) {
  17. switch (act->tag) {
  18. case ActionKind::DeleteTmpAction:
  19. std::cout << "delete_tmp(" << act->u.delete_tmp << ")";
  20. break;
  21. case ActionKind::ExpToLValAction:
  22. out << "exp=>lval";
  23. break;
  24. case ActionKind::LValAction:
  25. case ActionKind::ExpressionAction:
  26. PrintExp(act->u.exp);
  27. break;
  28. case ActionKind::StatementAction:
  29. PrintStatement(act->u.stmt, 1);
  30. break;
  31. case ActionKind::ValAction:
  32. PrintValue(act->u.val, out);
  33. break;
  34. }
  35. out << "<" << act->pos << ">";
  36. if (act->results.size() > 0) {
  37. out << "(";
  38. for (auto& result : act->results) {
  39. if (result) {
  40. PrintValue(result, out);
  41. }
  42. out << ",";
  43. }
  44. out << ")";
  45. }
  46. }
  47. void PrintActList(Stack<Action*> ls, std::ostream& out) {
  48. if (!ls.IsEmpty()) {
  49. PrintAct(ls.Pop(), out);
  50. if (!ls.IsEmpty()) {
  51. out << " :: ";
  52. PrintActList(ls, out);
  53. }
  54. }
  55. }
  56. auto MakeExpAct(const Expression* e) -> Action* {
  57. auto* act = new Action();
  58. act->tag = ActionKind::ExpressionAction;
  59. act->u.exp = e;
  60. act->pos = -1;
  61. return act;
  62. }
  63. auto MakeLvalAct(const Expression* e) -> Action* {
  64. auto* act = new Action();
  65. act->tag = ActionKind::LValAction;
  66. act->u.exp = e;
  67. act->pos = -1;
  68. return act;
  69. }
  70. auto MakeStmtAct(const Statement* s) -> Action* {
  71. auto* act = new Action();
  72. act->tag = ActionKind::StatementAction;
  73. act->u.stmt = s;
  74. act->pos = -1;
  75. return act;
  76. }
  77. auto MakeValAct(const Value* v) -> Action* {
  78. auto* act = new Action();
  79. act->tag = ActionKind::ValAction;
  80. act->u.val = v;
  81. act->pos = -1;
  82. return act;
  83. }
  84. auto MakeExpToLvalAct() -> Action* {
  85. auto* act = new Action();
  86. act->tag = ActionKind::ExpToLValAction;
  87. act->pos = -1;
  88. return act;
  89. }
  90. auto MakeDeleteAct(Address a) -> Action* {
  91. auto* act = new Action();
  92. act->tag = ActionKind::DeleteTmpAction;
  93. act->pos = -1;
  94. act->u.delete_tmp = a;
  95. return act;
  96. }
  97. } // namespace Carbon