expr.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. #include "pocketpy/expr.h"
  2. #include "pocketpy/codeobject.h"
  3. namespace pkpy{
  4. int CodeEmitContext::get_loop() const {
  5. int index = curr_block_i;
  6. while(index >= 0){
  7. if(co->blocks[index].type == FOR_LOOP) break;
  8. if(co->blocks[index].type == WHILE_LOOP) break;
  9. index = co->blocks[index].parent;
  10. }
  11. return index;
  12. }
  13. CodeBlock* CodeEmitContext::enter_block(CodeBlockType type){
  14. if(type == FOR_LOOP) for_loop_depth++;
  15. co->blocks.push_back(CodeBlock(
  16. type, curr_block_i, for_loop_depth, (int)co->codes.size()
  17. ));
  18. curr_block_i = co->blocks.size()-1;
  19. return &co->blocks[curr_block_i];
  20. }
  21. void CodeEmitContext::exit_block(){
  22. auto curr_type = co->blocks[curr_block_i].type;
  23. if(curr_type == FOR_LOOP) for_loop_depth--;
  24. co->blocks[curr_block_i].end = co->codes.size();
  25. curr_block_i = co->blocks[curr_block_i].parent;
  26. if(curr_block_i < 0) FATAL_ERROR();
  27. if(curr_type == FOR_LOOP){
  28. // add a no op here to make block check work
  29. emit(OP_NO_OP, BC_NOARG, BC_KEEPLINE);
  30. }
  31. }
  32. // clear the expression stack and generate bytecode
  33. void CodeEmitContext::emit_expr(){
  34. if(s_expr.size() != 1){
  35. throw std::runtime_error("s_expr.size() != 1\n" + _log_s_expr());
  36. }
  37. Expr_ expr = s_expr.popx();
  38. expr->emit(this);
  39. }
  40. std::string CodeEmitContext::_log_s_expr(){
  41. std::stringstream ss;
  42. for(auto& e: s_expr.data()) ss << e->str() << " ";
  43. return ss.str();
  44. }
  45. int CodeEmitContext::emit(Opcode opcode, uint16_t arg, int line) {
  46. co->codes.push_back(Bytecode{(uint8_t)opcode, arg});
  47. co->iblocks.push_back(curr_block_i);
  48. co->lines.push_back(line);
  49. int i = co->codes.size() - 1;
  50. if(line==BC_KEEPLINE){
  51. if(i>=1) co->lines[i] = co->lines[i-1];
  52. else co->lines[i] = 1;
  53. }
  54. return i;
  55. }
  56. void CodeEmitContext::patch_jump(int index) {
  57. int target = co->codes.size();
  58. co->codes[index].arg = target;
  59. }
  60. bool CodeEmitContext::add_label(StrName name){
  61. if(co->labels.contains(name)) return false;
  62. co->labels.set(name, co->codes.size());
  63. return true;
  64. }
  65. int CodeEmitContext::add_varname(StrName name){
  66. // PK_MAX_CO_VARNAMES will be checked when pop_context(), not here
  67. int index = co->varnames_inv.try_get(name);
  68. if(index >= 0) return index;
  69. co->varnames.push_back(name);
  70. index = co->varnames.size() - 1;
  71. co->varnames_inv.set(name, index);
  72. return index;
  73. }
  74. int CodeEmitContext::add_const(PyObject* v){
  75. // simple deduplication, only works for int/float
  76. for(int i=0; i<co->consts.size(); i++){
  77. if(co->consts[i] == v) return i;
  78. }
  79. co->consts.push_back(v);
  80. return co->consts.size() - 1;
  81. }
  82. int CodeEmitContext::add_func_decl(FuncDecl_ decl){
  83. co->func_decls.push_back(decl);
  84. return co->func_decls.size() - 1;
  85. }
  86. void CodeEmitContext::emit_store_name(NameScope scope, StrName name, int line){
  87. switch(scope){
  88. case NAME_LOCAL:
  89. emit(OP_STORE_FAST, add_varname(name), line);
  90. break;
  91. case NAME_GLOBAL:
  92. emit(OP_STORE_GLOBAL, StrName(name).index, line);
  93. break;
  94. case NAME_GLOBAL_UNKNOWN:
  95. emit(OP_STORE_NAME, StrName(name).index, line);
  96. break;
  97. default: FATAL_ERROR(); break;
  98. }
  99. }
  100. void NameExpr::emit(CodeEmitContext* ctx) {
  101. int index = ctx->co->varnames_inv.try_get(name);
  102. if(scope == NAME_LOCAL && index >= 0){
  103. ctx->emit(OP_LOAD_FAST, index, line);
  104. }else{
  105. Opcode op = ctx->level <= 1 ? OP_LOAD_GLOBAL : OP_LOAD_NONLOCAL;
  106. // we cannot determine the scope when calling exec()/eval()
  107. if(scope == NAME_GLOBAL_UNKNOWN) op = OP_LOAD_NAME;
  108. ctx->emit(op, StrName(name).index, line);
  109. }
  110. }
  111. bool NameExpr::emit_del(CodeEmitContext* ctx) {
  112. switch(scope){
  113. case NAME_LOCAL:
  114. ctx->emit(OP_DELETE_FAST, ctx->add_varname(name), line);
  115. break;
  116. case NAME_GLOBAL:
  117. ctx->emit(OP_DELETE_GLOBAL, StrName(name).index, line);
  118. break;
  119. case NAME_GLOBAL_UNKNOWN:
  120. ctx->emit(OP_DELETE_NAME, StrName(name).index, line);
  121. break;
  122. default: FATAL_ERROR(); break;
  123. }
  124. return true;
  125. }
  126. bool NameExpr::emit_store(CodeEmitContext* ctx) {
  127. if(ctx->is_compiling_class){
  128. ctx->emit(OP_STORE_CLASS_ATTR, name.index, line);
  129. return true;
  130. }
  131. ctx->emit_store_name(scope, name, line);
  132. return true;
  133. }
  134. void InvertExpr::emit(CodeEmitContext* ctx) {
  135. child->emit(ctx);
  136. ctx->emit(OP_UNARY_INVERT, BC_NOARG, line);
  137. }
  138. void StarredExpr::emit(CodeEmitContext* ctx) {
  139. child->emit(ctx);
  140. ctx->emit(OP_UNARY_STAR, level, line);
  141. }
  142. bool StarredExpr::emit_store(CodeEmitContext* ctx) {
  143. if(level != 1) return false;
  144. // simply proxy to child
  145. return child->emit_store(ctx);
  146. }
  147. void NotExpr::emit(CodeEmitContext* ctx) {
  148. child->emit(ctx);
  149. ctx->emit(OP_UNARY_NOT, BC_NOARG, line);
  150. }
  151. void AndExpr::emit(CodeEmitContext* ctx) {
  152. lhs->emit(ctx);
  153. int patch = ctx->emit(OP_JUMP_IF_FALSE_OR_POP, BC_NOARG, line);
  154. rhs->emit(ctx);
  155. ctx->patch_jump(patch);
  156. }
  157. void OrExpr::emit(CodeEmitContext* ctx) {
  158. lhs->emit(ctx);
  159. int patch = ctx->emit(OP_JUMP_IF_TRUE_OR_POP, BC_NOARG, line);
  160. rhs->emit(ctx);
  161. ctx->patch_jump(patch);
  162. }
  163. void Literal0Expr::emit(CodeEmitContext* ctx){
  164. switch (token) {
  165. case TK("None"): ctx->emit(OP_LOAD_NONE, BC_NOARG, line); break;
  166. case TK("True"): ctx->emit(OP_LOAD_TRUE, BC_NOARG, line); break;
  167. case TK("False"): ctx->emit(OP_LOAD_FALSE, BC_NOARG, line); break;
  168. case TK("..."): ctx->emit(OP_LOAD_ELLIPSIS, BC_NOARG, line); break;
  169. default: FATAL_ERROR();
  170. }
  171. }
  172. void LongExpr::emit(CodeEmitContext* ctx) {
  173. VM* vm = ctx->vm;
  174. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(s)), line);
  175. ctx->emit(OP_BUILD_LONG, BC_NOARG, line);
  176. }
  177. void BytesExpr::emit(CodeEmitContext* ctx) {
  178. VM* vm = ctx->vm;
  179. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(s)), line);
  180. ctx->emit(OP_BUILD_BYTES, BC_NOARG, line);
  181. }
  182. void LiteralExpr::emit(CodeEmitContext* ctx) {
  183. VM* vm = ctx->vm;
  184. PyObject* obj = nullptr;
  185. if(std::holds_alternative<i64>(value)){
  186. i64 _val = std::get<i64>(value);
  187. if(_val >= INT16_MIN && _val <= INT16_MAX){
  188. ctx->emit(OP_LOAD_INTEGER, (uint16_t)_val, line);
  189. return;
  190. }
  191. obj = VAR(_val);
  192. }
  193. if(std::holds_alternative<f64>(value)){
  194. obj = VAR(std::get<f64>(value));
  195. }
  196. if(std::holds_alternative<Str>(value)){
  197. obj = VAR(std::get<Str>(value));
  198. }
  199. if(obj == nullptr) FATAL_ERROR();
  200. ctx->emit(OP_LOAD_CONST, ctx->add_const(obj), line);
  201. }
  202. void NegatedExpr::emit(CodeEmitContext* ctx){
  203. VM* vm = ctx->vm;
  204. // if child is a int of float, do constant folding
  205. if(child->is_literal()){
  206. LiteralExpr* lit = static_cast<LiteralExpr*>(child.get());
  207. if(std::holds_alternative<i64>(lit->value)){
  208. i64 _val = -std::get<i64>(lit->value);
  209. if(_val >= INT16_MIN && _val <= INT16_MAX){
  210. ctx->emit(OP_LOAD_INTEGER, (uint16_t)_val, line);
  211. }else{
  212. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line);
  213. }
  214. return;
  215. }
  216. if(std::holds_alternative<f64>(lit->value)){
  217. PyObject* obj = VAR(-std::get<f64>(lit->value));
  218. ctx->emit(OP_LOAD_CONST, ctx->add_const(obj), line);
  219. return;
  220. }
  221. }
  222. child->emit(ctx);
  223. ctx->emit(OP_UNARY_NEGATIVE, BC_NOARG, line);
  224. }
  225. void SliceExpr::emit(CodeEmitContext* ctx){
  226. if(start){
  227. start->emit(ctx);
  228. }else{
  229. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  230. }
  231. if(stop){
  232. stop->emit(ctx);
  233. }else{
  234. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  235. }
  236. if(step){
  237. step->emit(ctx);
  238. }else{
  239. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  240. }
  241. ctx->emit(OP_BUILD_SLICE, BC_NOARG, line);
  242. }
  243. void DictItemExpr::emit(CodeEmitContext* ctx) {
  244. if(is_starred()){
  245. PK_ASSERT(key == nullptr);
  246. value->emit(ctx);
  247. }else{
  248. value->emit(ctx);
  249. key->emit(ctx); // reverse order
  250. ctx->emit(OP_BUILD_TUPLE, 2, line);
  251. }
  252. }
  253. bool TupleExpr::emit_store(CodeEmitContext* ctx) {
  254. // TOS is an iterable
  255. // items may contain StarredExpr, we should check it
  256. int starred_i = -1;
  257. for(int i=0; i<items.size(); i++){
  258. if(!items[i]->is_starred()) continue;
  259. if(starred_i == -1) starred_i = i;
  260. else return false; // multiple StarredExpr not allowed
  261. }
  262. if(starred_i == -1){
  263. Bytecode& prev = ctx->co->codes.back();
  264. if(prev.op == OP_BUILD_TUPLE && prev.arg == items.size()){
  265. // build tuple and unpack it is meaningless
  266. prev.op = OP_NO_OP;
  267. prev.arg = BC_NOARG;
  268. }else{
  269. ctx->emit(OP_UNPACK_SEQUENCE, items.size(), line);
  270. }
  271. }else{
  272. // starred assignment target must be in a tuple
  273. if(items.size() == 1) return false;
  274. // starred assignment target must be the last one (differ from cpython)
  275. if(starred_i != items.size()-1) return false;
  276. // a,*b = [1,2,3]
  277. // stack is [1,2,3] -> [1,[2,3]]
  278. ctx->emit(OP_UNPACK_EX, items.size()-1, line);
  279. }
  280. // do reverse emit
  281. for(int i=items.size()-1; i>=0; i--){
  282. bool ok = items[i]->emit_store(ctx);
  283. if(!ok) return false;
  284. }
  285. return true;
  286. }
  287. bool TupleExpr::emit_del(CodeEmitContext* ctx){
  288. for(auto& e: items){
  289. bool ok = e->emit_del(ctx);
  290. if(!ok) return false;
  291. }
  292. return true;
  293. }
  294. void CompExpr::emit(CodeEmitContext* ctx){
  295. ctx->emit(op0(), 0, line);
  296. iter->emit(ctx);
  297. ctx->emit(OP_GET_ITER, BC_NOARG, BC_KEEPLINE);
  298. ctx->enter_block(FOR_LOOP);
  299. ctx->emit(OP_FOR_ITER, BC_NOARG, BC_KEEPLINE);
  300. bool ok = vars->emit_store(ctx);
  301. // this error occurs in `vars` instead of this line, but...nevermind
  302. PK_ASSERT(ok); // TODO: raise a SyntaxError instead
  303. if(cond){
  304. cond->emit(ctx);
  305. int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
  306. expr->emit(ctx);
  307. ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
  308. ctx->patch_jump(patch);
  309. }else{
  310. expr->emit(ctx);
  311. ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
  312. }
  313. ctx->emit(OP_LOOP_CONTINUE, ctx->get_loop(), BC_KEEPLINE);
  314. ctx->exit_block();
  315. }
  316. void FStringExpr::_load_simple_expr(CodeEmitContext* ctx, Str expr){
  317. bool repr = false;
  318. if(expr.size>=2 && expr.end()[-2]=='!'){
  319. switch(expr.end()[-1]){
  320. case 'r': repr = true; expr = expr.substr(0, expr.size-2); break;
  321. case 's': repr = false; expr = expr.substr(0, expr.size-2); break;
  322. default: break; // nothing happens
  323. }
  324. }
  325. // name or name.name
  326. std::regex pattern(R"(^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*){0,1}$)");
  327. if(std::regex_match(expr.str(), pattern)){
  328. int dot = expr.index(".");
  329. if(dot < 0){
  330. ctx->emit(OP_LOAD_NAME, StrName(expr.sv()).index, line);
  331. }else{
  332. StrName name(expr.substr(0, dot).sv());
  333. StrName attr(expr.substr(dot+1).sv());
  334. ctx->emit(OP_LOAD_NAME, name.index, line);
  335. ctx->emit(OP_LOAD_ATTR, attr.index, line);
  336. }
  337. }else{
  338. int index = ctx->add_const(py_var(ctx->vm, expr));
  339. ctx->emit(OP_LOAD_CONST, index, line);
  340. ctx->emit(OP_EVAL, BC_NOARG, line);
  341. }
  342. if(repr){
  343. ctx->emit(OP_REPR, BC_NOARG, line);
  344. }
  345. }
  346. void FStringExpr::emit(CodeEmitContext* ctx){
  347. VM* vm = ctx->vm;
  348. PK_LOCAL_STATIC const std::regex pattern(R"(\{(.*?)\})");
  349. std::cregex_iterator begin(src.begin(), src.end(), pattern);
  350. std::cregex_iterator end;
  351. int size = 0;
  352. int i = 0;
  353. for(auto it = begin; it != end; it++) {
  354. std::cmatch m = *it;
  355. if (i < m.position()) {
  356. Str literal = src.substr(i, m.position() - i);
  357. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  358. size++;
  359. }
  360. Str expr = m[1].str();
  361. int conon = expr.index(":");
  362. if(conon >= 0){
  363. _load_simple_expr(ctx, expr.substr(0, conon));
  364. Str spec = expr.substr(conon+1);
  365. ctx->emit(OP_FORMAT_STRING, ctx->add_const(VAR(spec)), line);
  366. }else{
  367. _load_simple_expr(ctx, expr);
  368. }
  369. size++;
  370. i = (int)(m.position() + m.length());
  371. }
  372. if (i < src.length()) {
  373. Str literal = src.substr(i, src.length() - i);
  374. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  375. size++;
  376. }
  377. ctx->emit(OP_BUILD_STRING, size, line);
  378. }
  379. void SubscrExpr::emit(CodeEmitContext* ctx){
  380. a->emit(ctx);
  381. b->emit(ctx);
  382. ctx->emit(OP_LOAD_SUBSCR, BC_NOARG, line);
  383. }
  384. bool SubscrExpr::emit_del(CodeEmitContext* ctx){
  385. a->emit(ctx);
  386. b->emit(ctx);
  387. ctx->emit(OP_DELETE_SUBSCR, BC_NOARG, line);
  388. return true;
  389. }
  390. bool SubscrExpr::emit_store(CodeEmitContext* ctx){
  391. a->emit(ctx);
  392. b->emit(ctx);
  393. ctx->emit(OP_STORE_SUBSCR, BC_NOARG, line);
  394. return true;
  395. }
  396. void AttribExpr::emit(CodeEmitContext* ctx){
  397. a->emit(ctx);
  398. int index = StrName(b).index;
  399. ctx->emit(OP_LOAD_ATTR, index, line);
  400. }
  401. bool AttribExpr::emit_del(CodeEmitContext* ctx) {
  402. a->emit(ctx);
  403. int index = StrName(b).index;
  404. ctx->emit(OP_DELETE_ATTR, index, line);
  405. return true;
  406. }
  407. bool AttribExpr::emit_store(CodeEmitContext* ctx){
  408. a->emit(ctx);
  409. int index = StrName(b).index;
  410. ctx->emit(OP_STORE_ATTR, index, line);
  411. return true;
  412. }
  413. void AttribExpr::emit_method(CodeEmitContext* ctx) {
  414. a->emit(ctx);
  415. int index = StrName(b).index;
  416. ctx->emit(OP_LOAD_METHOD, index, line);
  417. }
  418. void CallExpr::emit(CodeEmitContext* ctx) {
  419. bool vargs = false;
  420. bool vkwargs = false;
  421. for(auto& arg: args) if(arg->is_starred()) vargs = true;
  422. for(auto& item: kwargs) if(item.second->is_starred()) vkwargs = true;
  423. // if callable is a AttrExpr, we should try to use `fast_call` instead of use `boundmethod` proxy
  424. if(callable->is_attrib()){
  425. auto p = static_cast<AttribExpr*>(callable.get());
  426. p->emit_method(ctx); // OP_LOAD_METHOD
  427. }else{
  428. callable->emit(ctx);
  429. ctx->emit(OP_LOAD_NULL, BC_NOARG, BC_KEEPLINE);
  430. }
  431. if(vargs || vkwargs){
  432. for(auto& item: args) item->emit(ctx);
  433. ctx->emit(OP_BUILD_TUPLE_UNPACK, (uint16_t)args.size(), line);
  434. if(!kwargs.empty()){
  435. for(auto& item: kwargs){
  436. if(item.second->is_starred()){
  437. if(item.second->star_level() != 2) FATAL_ERROR();
  438. item.second->emit(ctx);
  439. }else{
  440. // k=v
  441. int index = ctx->add_const(py_var(ctx->vm, item.first));
  442. ctx->emit(OP_LOAD_CONST, index, line);
  443. item.second->emit(ctx);
  444. ctx->emit(OP_BUILD_TUPLE, 2, line);
  445. }
  446. }
  447. ctx->emit(OP_BUILD_DICT_UNPACK, (int)kwargs.size(), line);
  448. ctx->emit(OP_CALL_TP, 1, line);
  449. }else{
  450. ctx->emit(OP_CALL_TP, 0, line);
  451. }
  452. }else{
  453. // vectorcall protocal
  454. for(auto& item: args) item->emit(ctx);
  455. for(auto& item: kwargs){
  456. uint16_t index = StrName(item.first.sv()).index;
  457. ctx->emit(OP_LOAD_INTEGER, index, line);
  458. item.second->emit(ctx);
  459. }
  460. int KWARGC = kwargs.size();
  461. int ARGC = args.size();
  462. ctx->emit(OP_CALL, (KWARGC<<8)|ARGC, line);
  463. }
  464. }
  465. bool BinaryExpr::is_compare() const {
  466. switch(op){
  467. case TK("<"): case TK("<="): case TK("=="):
  468. case TK("!="): case TK(">"): case TK(">="): return true;
  469. default: return false;
  470. }
  471. }
  472. void BinaryExpr::_emit_compare(CodeEmitContext* ctx, std::vector<int>& jmps){
  473. if(lhs->is_compare()){
  474. static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
  475. }else{
  476. lhs->emit(ctx); // [a]
  477. }
  478. rhs->emit(ctx); // [a, b]
  479. ctx->emit(OP_DUP_TOP, BC_NOARG, line); // [a, b, b]
  480. ctx->emit(OP_ROT_THREE, BC_NOARG, line); // [b, a, b]
  481. switch(op){
  482. case TK("<"): ctx->emit(OP_COMPARE_LT, BC_NOARG, line); break;
  483. case TK("<="): ctx->emit(OP_COMPARE_LE, BC_NOARG, line); break;
  484. case TK("=="): ctx->emit(OP_COMPARE_EQ, BC_NOARG, line); break;
  485. case TK("!="): ctx->emit(OP_COMPARE_NE, BC_NOARG, line); break;
  486. case TK(">"): ctx->emit(OP_COMPARE_GT, BC_NOARG, line); break;
  487. case TK(">="): ctx->emit(OP_COMPARE_GE, BC_NOARG, line); break;
  488. default: UNREACHABLE();
  489. }
  490. // [b, RES]
  491. int index = ctx->emit(OP_SHORTCUT_IF_FALSE_OR_POP, BC_NOARG, line);
  492. jmps.push_back(index);
  493. }
  494. void BinaryExpr::emit(CodeEmitContext* ctx) {
  495. std::vector<int> jmps;
  496. if(is_compare() && lhs->is_compare()){
  497. // (a < b) < c
  498. static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
  499. // [b, RES]
  500. }else{
  501. // (1 + 2) < c
  502. lhs->emit(ctx);
  503. }
  504. rhs->emit(ctx);
  505. switch (op) {
  506. case TK("+"): ctx->emit(OP_BINARY_ADD, BC_NOARG, line); break;
  507. case TK("-"): ctx->emit(OP_BINARY_SUB, BC_NOARG, line); break;
  508. case TK("*"): ctx->emit(OP_BINARY_MUL, BC_NOARG, line); break;
  509. case TK("/"): ctx->emit(OP_BINARY_TRUEDIV, BC_NOARG, line); break;
  510. case TK("//"): ctx->emit(OP_BINARY_FLOORDIV, BC_NOARG, line); break;
  511. case TK("%"): ctx->emit(OP_BINARY_MOD, BC_NOARG, line); break;
  512. case TK("**"): ctx->emit(OP_BINARY_POW, BC_NOARG, line); break;
  513. case TK("<"): ctx->emit(OP_COMPARE_LT, BC_NOARG, line); break;
  514. case TK("<="): ctx->emit(OP_COMPARE_LE, BC_NOARG, line); break;
  515. case TK("=="): ctx->emit(OP_COMPARE_EQ, BC_NOARG, line); break;
  516. case TK("!="): ctx->emit(OP_COMPARE_NE, BC_NOARG, line); break;
  517. case TK(">"): ctx->emit(OP_COMPARE_GT, BC_NOARG, line); break;
  518. case TK(">="): ctx->emit(OP_COMPARE_GE, BC_NOARG, line); break;
  519. case TK("in"): ctx->emit(OP_CONTAINS_OP, 0, line); break;
  520. case TK("not in"): ctx->emit(OP_CONTAINS_OP, 1, line); break;
  521. case TK("is"): ctx->emit(OP_IS_OP, 0, line); break;
  522. case TK("is not"): ctx->emit(OP_IS_OP, 1, line); break;
  523. case TK("<<"): ctx->emit(OP_BITWISE_LSHIFT, BC_NOARG, line); break;
  524. case TK(">>"): ctx->emit(OP_BITWISE_RSHIFT, BC_NOARG, line); break;
  525. case TK("&"): ctx->emit(OP_BITWISE_AND, BC_NOARG, line); break;
  526. case TK("|"): ctx->emit(OP_BITWISE_OR, BC_NOARG, line); break;
  527. case TK("^"): ctx->emit(OP_BITWISE_XOR, BC_NOARG, line); break;
  528. case TK("@"): ctx->emit(OP_BINARY_MATMUL, BC_NOARG, line); break;
  529. default: FATAL_ERROR();
  530. }
  531. for(int i: jmps) ctx->patch_jump(i);
  532. }
  533. void TernaryExpr::emit(CodeEmitContext* ctx){
  534. cond->emit(ctx);
  535. int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, cond->line);
  536. true_expr->emit(ctx);
  537. int patch_2 = ctx->emit(OP_JUMP_ABSOLUTE, BC_NOARG, true_expr->line);
  538. ctx->patch_jump(patch);
  539. false_expr->emit(ctx);
  540. ctx->patch_jump(patch_2);
  541. }
  542. } // namespace pkpy