vm_apply.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include "vm_apply.h"
  2. namespace NSc::NUt {
  3. #define Y_GEN_TRY_OP(op) \
  4. if (!op) { return false; }
  5. #define Y_GEN_SRC_OP(op, arg, st, act) \
  6. switch (act.GetSrc(arg).Type) { \
  7. case TSrc::T_LREF__POS: \
  8. op(st.LRef(act.GetSrc(arg).Pos)); \
  9. break; \
  10. case TSrc::T_CREF__POS: \
  11. op(st.CRef(act.GetSrc(arg).Pos)); \
  12. break; \
  13. case TSrc::T_RREF__POS: \
  14. op(st.RRef(act.GetSrc(arg).Pos)); \
  15. break; \
  16. default: \
  17. Y_ABORT(); \
  18. }
  19. #define Y_GEN_SRC_TRY_OP(op, arg, st, act) \
  20. if (st.CRef(act.GetSrc(arg).Pos).IsSameOrAncestorOf(st.Current())) { \
  21. return false; \
  22. } \
  23. Y_GEN_SRC_OP(op, arg, st, act);
  24. #define Y_GEN_DST_OP(op, arg, st, act) \
  25. switch (act.GetDst(arg).Type) { \
  26. case TDst::T_CREATE_BACK_LREF: \
  27. Y_GEN_TRY_OP(st.TryPushBack(op)) \
  28. break; \
  29. case TDst::T_CREATE_BACK_CREF: \
  30. Y_GEN_TRY_OP(st.TryPushBack(std::as_const(op))) \
  31. break; \
  32. case TDst::T_CREATE_BACK_RREF: \
  33. Y_GEN_TRY_OP(st.TryPushBack(std::move(op))) \
  34. break; \
  35. case TDst::T_CREATE_FRONT_LREF: \
  36. Y_GEN_TRY_OP(st.TryPushFront(op)) \
  37. break; \
  38. case TDst::T_CREATE_FRONT_CREF: \
  39. Y_GEN_TRY_OP(st.TryPushFront(std::as_const(op))) \
  40. break; \
  41. case TDst::T_CREATE_FRONT_RREF: \
  42. Y_GEN_TRY_OP(st.TryPushFront(std::move(op))) \
  43. break; \
  44. case TDst::T_LREF__POS: \
  45. st.LRef(act.GetDst(arg).Pos) = op; \
  46. break; \
  47. case TDst::T_CREF__POS: \
  48. st.LRef(act.GetDst(arg).Pos) = std::as_const(op); \
  49. break; \
  50. case TDst::T_RREF__POS: \
  51. st.LRef(act.GetDst(arg).Pos) = std::move(op); \
  52. break; \
  53. default: \
  54. Y_ABORT(); \
  55. }
  56. #define Y_GEN_REF_OP(op, arg, st, act) \
  57. switch (act.GetRef(arg).Type) { \
  58. case TRef::T_CREATE_BACK: \
  59. Y_GEN_TRY_OP(st.TryPushBack(op)) \
  60. break; \
  61. case TRef::T_CREATE_FRONT: \
  62. Y_GEN_TRY_OP(st.TryPushFront(op)) \
  63. break; \
  64. case TRef::T_REF__POS: \
  65. st.LRef(act.GetRef(arg).Pos) = op; \
  66. break; \
  67. default: \
  68. Y_ABORT(); \
  69. }
  70. #define Y_GEN_PTR_OP(op, arg, st, act) \
  71. if (auto* r = (op)) { \
  72. switch (act.GetRef(arg).Type) { \
  73. case TRef::T_CREATE_BACK: \
  74. Y_GEN_TRY_OP(st.TryPushBack(*r)) \
  75. break; \
  76. case TRef::T_CREATE_FRONT: \
  77. Y_GEN_TRY_OP(st.TryPushFront(*r)) \
  78. break; \
  79. case TRef::T_REF__POS: \
  80. st.LRef(act.GetRef(arg).Pos) = *r; \
  81. break; \
  82. default: \
  83. Y_ABORT(); \
  84. } \
  85. }
  86. bool ApplyNextAction(TVMState& st, TVMAction act) {
  87. switch (act.Type) {
  88. case VMA_JMP__POS:
  89. st.Pos = act.GetPos(0);
  90. return true;
  91. case VMA_CREATE_BACK:
  92. Y_GEN_TRY_OP(st.TryPushBack())
  93. return true;
  94. case VMA_CREATE_BACK__SRC:
  95. switch (act.GetSrc(0).Type) {
  96. case TSrc::T_LREF__POS:
  97. Y_GEN_TRY_OP(st.TryPushBack(st.LRef(act.GetSrc(0).Pos)))
  98. break;
  99. case TSrc::T_CREF__POS:
  100. Y_GEN_TRY_OP(st.TryPushBack(st.CRef(act.GetSrc(0).Pos)))
  101. break;
  102. case TSrc::T_RREF__POS:
  103. Y_GEN_TRY_OP(st.TryPushBack(st.RRef(act.GetSrc(0).Pos)))
  104. break;
  105. default:
  106. Y_ABORT();
  107. }
  108. return true;
  109. case VMA_CREATE_FRONT:
  110. Y_GEN_TRY_OP(st.TryPushFront())
  111. return true;
  112. case VMA_CREATE_FRONT__SRC:
  113. switch (act.GetSrc(0).Type) {
  114. case TSrc::T_LREF__POS:
  115. Y_GEN_TRY_OP(st.TryPushFront(st.LRef(act.GetSrc(0).Pos)))
  116. break;
  117. case TSrc::T_CREF__POS:
  118. Y_GEN_TRY_OP(st.TryPushFront(st.CRef(act.GetSrc(0).Pos)))
  119. break;
  120. case TSrc::T_RREF__POS:
  121. Y_GEN_TRY_OP(st.TryPushFront(st.RRef(act.GetSrc(0).Pos)))
  122. break;
  123. default:
  124. Y_ABORT();
  125. }
  126. return true;
  127. case VMA_DESTROY_BACK:
  128. return st.TryPopBack();
  129. case VMA_DESTROY_FRONT:
  130. return st.TryPopFront();
  131. case VMA_ASSIGN__SRC:
  132. Y_GEN_SRC_OP(st.Current() = , 0, st, act);
  133. return true;
  134. case VMA_SET_STRING__IDX:
  135. st.Current().SetString(act.GetString(0));
  136. return true;
  137. case VMA_SET_INT_NUMBER__IDX:
  138. st.Current().SetIntNumber(act.GetIntNumber(0));
  139. return true;
  140. case VMA_SET_DICT:
  141. st.Current().SetDict();
  142. return true;
  143. case VMA_SET_ARRAY:
  144. st.Current().SetArray();
  145. return true;
  146. case VMA_SET_NULL:
  147. st.Current().SetNull();
  148. return true;
  149. case VMA_GET_JSON:
  150. st.Current().ToJson();
  151. return true;
  152. case VMA_ARRAY_CLEAR:
  153. st.Current().ClearArray();
  154. return true;
  155. case VMA_ARRAY_PUSH:
  156. st.Current().Push();
  157. return true;
  158. case VMA_ARRAY_PUSH__SRC:
  159. Y_GEN_SRC_TRY_OP(st.Current().Push, 0, st, act);
  160. return true;
  161. case VMA_ARRAY_PUSH__DST:
  162. Y_GEN_DST_OP(st.Current().Push(), 0, st, act);
  163. return true;
  164. case VMA_ARRAY_POP__REF:
  165. Y_GEN_REF_OP(st.Current().Pop(), 0, st, act);
  166. return true;
  167. case VMA_ARRAY_INSERT__IDX:
  168. st.Current().Insert(act.GetIdx(0));
  169. return true;
  170. case VMA_ARRAY_INSERT__IDX_SRC:
  171. Y_GEN_SRC_TRY_OP(st.Current().Insert(act.GetIdx(0)) = , 1, st, act);
  172. return true;
  173. case VMA_ARRAY_INSERT__IDX_DST:
  174. Y_GEN_DST_OP(st.Current().Insert(act.GetIdx(0)), 1, st, act);
  175. return true;
  176. case VMA_ARRAY_DELETE__IDX_REF:
  177. Y_GEN_REF_OP(st.Current().Delete(act.GetIdx(0)), 1, st, act);
  178. return true;
  179. case VMA_ARRAY_GET__IDX_REF:
  180. Y_GEN_REF_OP(st.Current().Get(act.GetIdx(0)), 1, st, act);
  181. return true;
  182. case VMA_ARRAY_GET_OR_ADD__IDX:
  183. st.Current().GetOrAdd(act.GetIdx(0));
  184. return true;
  185. case VMA_ARRAY_GET_OR_ADD__IDX_SRC:
  186. Y_GEN_SRC_TRY_OP(st.Current().GetOrAdd(act.GetIdx(0)) = , 1, st, act);
  187. return true;
  188. case VMA_ARRAY_GET_OR_ADD__IDX_DST:
  189. Y_GEN_DST_OP(st.Current().GetOrAdd(act.GetIdx(0)), 1, st, act);
  190. return true;
  191. case VMA_ARRAY_GET_NO_ADD__IDX_REF:
  192. Y_GEN_PTR_OP(st.Current().GetNoAdd(act.GetIdx(0)), 1, st, act);
  193. return true;
  194. case VMA_DICT_CLEAR:
  195. st.Current().ClearDict();
  196. return true;
  197. case VMA_DICT_DELETE__IDX:
  198. st.Current().Delete(act.GetKey(0));
  199. return true;
  200. case VMA_DICT_DELETE__IDX_REF:
  201. Y_GEN_REF_OP(st.Current().Delete(act.GetKey(0)), 1, st, act);
  202. return true;
  203. case VMA_DICT_GET__IDX_REF:
  204. Y_GEN_REF_OP(st.Current().Get(act.GetKey(0)), 1, st, act);
  205. return true;
  206. case VMA_DICT_GET_OR_ADD__IDX:
  207. st.Current().GetOrAdd(act.GetKey(0));
  208. return true;
  209. case VMA_DICT_GET_OR_ADD__IDX_SRC:
  210. Y_GEN_SRC_TRY_OP(st.Current().GetOrAdd(act.GetKey(0)) = , 1, st, act);
  211. return true;
  212. case VMA_DICT_GET_OR_ADD__IDX_DST:
  213. Y_GEN_DST_OP(st.Current().GetOrAdd(act.GetKey(0)), 1, st, act);
  214. return true;
  215. case VMA_DICT_GET_NO_ADD__IDX_REF:
  216. Y_GEN_PTR_OP(st.Current().GetNoAdd(act.GetKey(0)), 1, st, act);
  217. return true;
  218. case VMA_MERGE_UPDATE__POS:
  219. st.Current().MergeUpdate(st.LRef(act.GetPos(0)));
  220. return true;
  221. case VMA_MERGE_REVERSE__POS:
  222. st.Current().ReverseMerge(st.LRef(act.GetPos(0)));
  223. return true;
  224. case VMA_MERGE_COPY_FROM__POS:
  225. st.Current().CopyFrom(st.LRef(act.GetPos(0)));
  226. return true;
  227. case VMA_SWAP__POS:
  228. st.Current().Swap(st.LRef(act.GetPos(0)));
  229. return true;
  230. case VMA_EQUAL__POS_POS:
  231. TValue::Equal(st.CRef(act.GetPos(0)), st.CRef(act.GetPos(1)));
  232. return true;
  233. case VMA_SELECT_NO_ADD__PATH_REF:
  234. Y_GEN_REF_OP(st.Current().TrySelect(act.GetPath(0)), 1, st, act);
  235. return true;
  236. case VMA_SELECT_OR_ADD__PATH_REF:
  237. Y_GEN_PTR_OP(st.Current().TrySelectOrAdd(act.GetPath(0)), 1, st, act);
  238. return true;
  239. case VMA_SELECT_AND_DELETE__PATH_REF:
  240. Y_GEN_REF_OP(st.Current().TrySelectAndDelete(act.GetPath(0)), 1, st, act);
  241. return true;
  242. default:
  243. Y_ABORT();
  244. }
  245. }
  246. }