action.cpp 2.4 KB

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