vm_defs.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "vm_defs.h"
  2. #include <util/generic/xrange.h>
  3. #include <util/string/builder.h>
  4. namespace NSc::NUt {
  5. namespace {
  6. TStringBuf GetStringByIdx(ui32 idx) {
  7. static const TStringBuf strings[TIdx::ValueCount] {{}, "a", "aa", "aaa"};
  8. return strings[idx % TIdx::ValueCount];
  9. }
  10. }
  11. TVMState::TVMState(TStringBuf wire, ui32 memSz, ui32 pos)
  12. : Input(wire)
  13. , Memory(memSz)
  14. , Pos(pos)
  15. {}
  16. bool TVMState::TryPushBack() {
  17. if (MayAddMoreMemory()) {
  18. Memory.emplace_back();
  19. return true;
  20. } else {
  21. return false;
  22. }
  23. }
  24. bool TVMState::TryPushFront() {
  25. if (MayAddMoreMemory()) {
  26. Pos += 1;
  27. Memory.emplace_front();
  28. return true;
  29. } else {
  30. return false;
  31. }
  32. }
  33. bool TVMState::TryPopBack() {
  34. if (Memory.size() > 1 && Pos < Memory.size() - 1) {
  35. Memory.pop_back();
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41. bool TVMState::TryPopFront() {
  42. if (Memory.size() > 1 && Pos > 0) {
  43. Memory.pop_front();
  44. Pos -= 1;
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. TString TVMState::ToString() const {
  51. TStringBuilder b;
  52. b << "pos=" << Pos << ";";
  53. for (const auto i : xrange(Memory.size())) {
  54. b << " " << i << (i == Pos ? "@" : ".") << Memory[i].ToJson().Quote();
  55. }
  56. return b;
  57. }
  58. TString TIdx::ToString() const {
  59. return TStringBuilder() << "IDX:" << Idx;
  60. }
  61. TString TPos::ToString() const {
  62. return TStringBuilder() << "POS:" << Pos;
  63. }
  64. template <class T>
  65. TString RenderToString(TStringBuf name, T type, ui32 pos) {
  66. TStringBuilder b;
  67. b << name << ":" << type;
  68. if ((ui32)-1 != pos) {
  69. b << "," << pos;
  70. }
  71. return b;
  72. }
  73. TString TRef::ToString() const {
  74. return RenderToString("REF", Type, Pos);
  75. }
  76. TString TSrc::ToString() const {
  77. return RenderToString("SRC", Type, Pos);
  78. }
  79. TString TDst::ToString() const {
  80. return RenderToString("DST", Type, Pos);
  81. }
  82. TString TPath::ToString() const {
  83. return TStringBuilder() << "PATH:" << Path.Quote();
  84. }
  85. TRef TVMAction::GetRef(ui32 arg) const noexcept {
  86. return std::get<TRef>(Arg[arg]);
  87. }
  88. TSrc TVMAction::GetSrc(ui32 arg) const noexcept {
  89. return std::get<TSrc>(Arg[arg]);
  90. }
  91. TDst TVMAction::GetDst(ui32 arg) const noexcept {
  92. return std::get<TDst>(Arg[arg]);
  93. }
  94. ui32 TVMAction::GetPos(ui32 arg) const noexcept {
  95. return std::get<TPos>(Arg[arg]).Pos;
  96. }
  97. ui32 TVMAction::GetIdx(ui32 arg) const noexcept {
  98. return std::get<TIdx>(Arg[arg]).Idx;
  99. }
  100. TStringBuf TVMAction::GetKey(ui32 arg) const noexcept {
  101. return GetString(arg);
  102. }
  103. TStringBuf TVMAction::GetString(ui32 arg) const noexcept {
  104. return GetStringByIdx(GetIdx(arg));
  105. }
  106. i64 TVMAction::GetIntNumber(ui32 arg) const noexcept {
  107. return GetIdx(arg);
  108. }
  109. TStringBuf TVMAction::GetPath(ui32 arg) const noexcept {
  110. return std::get<TPath>(Arg[arg]).Path;
  111. }
  112. struct TActionPrinter {
  113. TActionPrinter(IOutputStream& out)
  114. : Out(out)
  115. {}
  116. bool operator()(const std::monostate&) const {
  117. return true;
  118. }
  119. //TIdx, TPos, TRef, TSrc, TDst, TPath
  120. template <class T>
  121. bool operator()(const T& t) const {
  122. Out << "; " << t.ToString();
  123. return false;
  124. }
  125. IOutputStream& Out;
  126. };
  127. TString TVMAction::ToString() const {
  128. TStringBuilder out;
  129. out << Type;
  130. for (const auto& arg : Arg) {
  131. if (std::visit(TActionPrinter(out.Out), arg)) {
  132. break;
  133. }
  134. }
  135. return out;
  136. }
  137. }