expr.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "common.h"
  4. #include "lexer.h"
  5. #include "error.h"
  6. #include "ceval.h"
  7. #include "str.h"
  8. namespace pkpy{
  9. struct CodeEmitContext;
  10. struct Expr{
  11. int line = 0;
  12. virtual ~Expr() = default;
  13. virtual void emit(CodeEmitContext* ctx) = 0;
  14. virtual Str str() const = 0;
  15. virtual std::vector<const Expr*> children() const { return {}; }
  16. virtual bool is_starred() const { return false; }
  17. virtual bool is_literal() const { return false; }
  18. // for OP_DELETE_XXX
  19. virtual bool emit_del(CodeEmitContext* ctx) { return false; }
  20. // for OP_STORE_XXX
  21. [[nodiscard]] virtual bool emit_store(CodeEmitContext* ctx) { return false; }
  22. };
  23. struct CodeEmitContext{
  24. VM* vm;
  25. CodeObject_ co;
  26. stack<Expr_> s_expr;
  27. CodeEmitContext(VM* vm, CodeObject_ co): vm(vm), co(co) {}
  28. int curr_block_i = 0;
  29. bool is_compiling_class = false;
  30. bool is_curr_block_loop() const {
  31. return co->blocks[curr_block_i].type == FOR_LOOP || co->blocks[curr_block_i].type == WHILE_LOOP;
  32. }
  33. void enter_block(CodeBlockType type){
  34. co->blocks.push_back(CodeBlock{
  35. type, curr_block_i, (int)co->codes.size()
  36. });
  37. curr_block_i = co->blocks.size()-1;
  38. }
  39. void exit_block(){
  40. co->blocks[curr_block_i].end = co->codes.size();
  41. curr_block_i = co->blocks[curr_block_i].parent;
  42. if(curr_block_i < 0) UNREACHABLE();
  43. }
  44. // clear the expression stack and generate bytecode
  45. void emit_expr(){
  46. if(s_expr.size() != 1){
  47. throw std::runtime_error("s_expr.size() != 1\n" + _log_s_expr());
  48. }
  49. Expr_ expr = s_expr.popx();
  50. expr->emit(this);
  51. }
  52. Str _log_s_expr(){
  53. StrStream ss;
  54. for(auto& e: s_expr.data()) ss << e->str() << " ";
  55. return ss.str();
  56. }
  57. int emit(Opcode opcode, int arg, int line) {
  58. co->codes.push_back(
  59. Bytecode{(uint16_t)opcode, (uint16_t)curr_block_i, arg, line}
  60. );
  61. int i = co->codes.size() - 1;
  62. if(line==BC_KEEPLINE){
  63. if(i>=1) co->codes[i].line = co->codes[i-1].line;
  64. else co->codes[i].line = 1;
  65. }
  66. return i;
  67. }
  68. void patch_jump(int index) {
  69. int target = co->codes.size();
  70. co->codes[index].arg = target;
  71. }
  72. bool add_label(StrName label){
  73. if(co->labels.count(label)) return false;
  74. co->labels[label] = co->codes.size();
  75. return true;
  76. }
  77. int add_name(StrName name){
  78. for(int i=0; i<co->names.size(); i++){
  79. if(co->names[i] == name) return i;
  80. }
  81. co->names.push_back(name);
  82. return co->names.size() - 1;
  83. }
  84. int add_const(PyObject* v){
  85. co->consts.push_back(v);
  86. return co->consts.size() - 1;
  87. }
  88. int add_func_decl(FuncDecl_ decl){
  89. co->func_decls.push_back(decl);
  90. return co->func_decls.size() - 1;
  91. }
  92. };
  93. // PASS
  94. struct NameExpr: Expr{
  95. StrName name;
  96. NameScope scope;
  97. NameExpr(StrName name, NameScope scope): name(name), scope(scope) {}
  98. Str str() const override { return "$" + name.str(); }
  99. void emit(CodeEmitContext* ctx) override {
  100. int index = ctx->add_name(name);
  101. ctx->emit(OP_LOAD_NAME, index, line);
  102. }
  103. bool emit_del(CodeEmitContext* ctx) override {
  104. int index = ctx->add_name(name);
  105. switch(scope){
  106. case NAME_LOCAL:
  107. ctx->emit(OP_DELETE_LOCAL, index, line);
  108. break;
  109. case NAME_GLOBAL:
  110. ctx->emit(OP_DELETE_GLOBAL, index, line);
  111. break;
  112. default: UNREACHABLE(); break;
  113. }
  114. return true;
  115. }
  116. bool emit_store(CodeEmitContext* ctx) override {
  117. int index = ctx->add_name(name);
  118. if(ctx->is_compiling_class){
  119. ctx->emit(OP_STORE_CLASS_ATTR, index, line);
  120. return true;
  121. }
  122. switch(scope){
  123. case NAME_LOCAL:
  124. ctx->emit(OP_STORE_LOCAL, index, line);
  125. break;
  126. case NAME_GLOBAL:
  127. ctx->emit(OP_STORE_GLOBAL, index, line);
  128. break;
  129. default: UNREACHABLE(); break;
  130. }
  131. return true;
  132. }
  133. };
  134. struct StarredExpr: Expr{
  135. Expr_ child;
  136. StarredExpr(Expr_&& child): child(std::move(child)) {}
  137. Str str() const override { return "*"; }
  138. std::vector<const Expr*> children() const override { return {child.get()}; }
  139. bool is_starred() const override { return true; }
  140. void emit(CodeEmitContext* ctx) override {
  141. child->emit(ctx);
  142. ctx->emit(OP_UNARY_STAR, BC_NOARG, line);
  143. }
  144. bool emit_store(CodeEmitContext* ctx) override {
  145. // simply proxy to child
  146. return child->emit_store(ctx);
  147. }
  148. };
  149. // PASS
  150. struct NotExpr: Expr{
  151. Expr_ child;
  152. NotExpr(Expr_&& child): child(std::move(child)) {}
  153. Str str() const override { return "not"; }
  154. std::vector<const Expr*> children() const override { return {child.get()}; }
  155. void emit(CodeEmitContext* ctx) override {
  156. child->emit(ctx);
  157. ctx->emit(OP_UNARY_NOT, BC_NOARG, line);
  158. }
  159. };
  160. // PASS
  161. struct AndExpr: Expr{
  162. Expr_ lhs;
  163. Expr_ rhs;
  164. Str str() const override { return "and"; }
  165. std::vector<const Expr*> children() const override { return {lhs.get(), rhs.get()}; }
  166. void emit(CodeEmitContext* ctx) override {
  167. lhs->emit(ctx);
  168. int patch = ctx->emit(OP_JUMP_IF_FALSE_OR_POP, BC_NOARG, line);
  169. rhs->emit(ctx);
  170. ctx->patch_jump(patch);
  171. }
  172. };
  173. // PASS
  174. struct OrExpr: Expr{
  175. Expr_ lhs;
  176. Expr_ rhs;
  177. Str str() const override { return "or"; }
  178. std::vector<const Expr*> children() const override { return {lhs.get(), rhs.get()}; }
  179. void emit(CodeEmitContext* ctx) override {
  180. lhs->emit(ctx);
  181. int patch = ctx->emit(OP_JUMP_IF_TRUE_OR_POP, BC_NOARG, line);
  182. rhs->emit(ctx);
  183. ctx->patch_jump(patch);
  184. }
  185. };
  186. // [None, True, False, ...]
  187. struct Literal0Expr: Expr{
  188. TokenIndex token;
  189. Literal0Expr(TokenIndex token): token(token) {}
  190. Str str() const override { return TK_STR(token); }
  191. void emit(CodeEmitContext* ctx) override {
  192. switch (token) {
  193. case TK("None"): ctx->emit(OP_LOAD_NONE, BC_NOARG, line); break;
  194. case TK("True"): ctx->emit(OP_LOAD_TRUE, BC_NOARG, line); break;
  195. case TK("False"): ctx->emit(OP_LOAD_FALSE, BC_NOARG, line); break;
  196. case TK("..."): ctx->emit(OP_LOAD_ELLIPSIS, BC_NOARG, line); break;
  197. default: UNREACHABLE();
  198. }
  199. }
  200. };
  201. // @num, @str which needs to invoke OP_LOAD_CONST
  202. struct LiteralExpr: Expr{
  203. TokenValue value;
  204. LiteralExpr(TokenValue value): value(value) {}
  205. Str str() const override {
  206. if(std::holds_alternative<i64>(value)){
  207. return std::to_string(std::get<i64>(value));
  208. }
  209. if(std::holds_alternative<f64>(value)){
  210. return std::to_string(std::get<f64>(value));
  211. }
  212. if(std::holds_alternative<Str>(value)){
  213. return std::get<Str>(value).escape(true);
  214. }
  215. UNREACHABLE();
  216. }
  217. PyObject* to_object(CodeEmitContext* ctx){
  218. VM* vm = ctx->vm;
  219. PyObject* obj = nullptr;
  220. if(std::holds_alternative<i64>(value)){
  221. obj = VAR(std::get<i64>(value));
  222. }
  223. if(std::holds_alternative<f64>(value)){
  224. obj = VAR(std::get<f64>(value));
  225. }
  226. if(std::holds_alternative<Str>(value)){
  227. obj = VAR(std::get<Str>(value));
  228. }
  229. return obj;
  230. }
  231. void emit(CodeEmitContext* ctx) override {
  232. PyObject* obj = to_object(ctx);
  233. if(obj == nullptr) UNREACHABLE();
  234. int index = ctx->add_const(obj);
  235. ctx->emit(OP_LOAD_CONST, index, line);
  236. }
  237. bool is_literal() const override { return true; }
  238. };
  239. // PASS
  240. struct NegatedExpr: Expr{
  241. Expr_ child;
  242. NegatedExpr(Expr_&& child): child(std::move(child)) {}
  243. Str str() const override { return "-"; }
  244. std::vector<const Expr*> children() const override { return {child.get()}; }
  245. void emit(CodeEmitContext* ctx) override {
  246. VM* vm = ctx->vm;
  247. // if child is a int of float, do constant folding
  248. if(child->is_literal()){
  249. LiteralExpr* lit = static_cast<LiteralExpr*>(child.get());
  250. PyObject* obj = nullptr;
  251. if(std::holds_alternative<i64>(lit->value)){
  252. obj = VAR(-std::get<i64>(lit->value));
  253. }
  254. if(std::holds_alternative<f64>(lit->value)){
  255. obj = VAR(-std::get<f64>(lit->value));
  256. }
  257. if(obj != nullptr){
  258. ctx->emit(OP_LOAD_CONST, ctx->add_const(obj), line);
  259. return;
  260. }
  261. }
  262. child->emit(ctx);
  263. ctx->emit(OP_UNARY_NEGATIVE, BC_NOARG, line);
  264. }
  265. };
  266. // PASS
  267. struct SliceExpr: Expr{
  268. Expr_ start;
  269. Expr_ stop;
  270. Expr_ step;
  271. Str str() const override { return "slice()"; }
  272. std::vector<const Expr*> children() const override {
  273. // may contain nullptr
  274. return {start.get(), stop.get(), step.get()};
  275. }
  276. void emit(CodeEmitContext* ctx) override {
  277. if(start){
  278. start->emit(ctx);
  279. }else{
  280. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  281. }
  282. if(stop){
  283. stop->emit(ctx);
  284. }else{
  285. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  286. }
  287. if(step){
  288. step->emit(ctx);
  289. }else{
  290. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  291. }
  292. ctx->emit(OP_BUILD_SLICE, BC_NOARG, line);
  293. }
  294. };
  295. struct DictItemExpr: Expr{
  296. Expr_ key;
  297. Expr_ value;
  298. Str str() const override { return "k:v"; }
  299. std::vector<const Expr*> children() const override { return {key.get(), value.get()}; }
  300. void emit(CodeEmitContext* ctx) override {
  301. value->emit(ctx);
  302. key->emit(ctx); // reverse order
  303. ctx->emit(OP_BUILD_TUPLE, 2, line);
  304. }
  305. };
  306. struct SequenceExpr: Expr{
  307. std::vector<Expr_> items;
  308. SequenceExpr(std::vector<Expr_>&& items): items(std::move(items)) {}
  309. virtual Opcode opcode() const = 0;
  310. std::vector<const Expr*> children() const override {
  311. std::vector<const Expr*> ret;
  312. for(auto& item: items) ret.push_back(item.get());
  313. return ret;
  314. }
  315. void emit(CodeEmitContext* ctx) override {
  316. for(auto& item: items) item->emit(ctx);
  317. ctx->emit(opcode(), items.size(), line);
  318. }
  319. };
  320. struct ListExpr: SequenceExpr{
  321. using SequenceExpr::SequenceExpr;
  322. Str str() const override { return "list()"; }
  323. Opcode opcode() const override { return OP_BUILD_LIST; }
  324. };
  325. struct DictExpr: SequenceExpr{
  326. using SequenceExpr::SequenceExpr;
  327. Str str() const override { return "dict()"; }
  328. Opcode opcode() const override { return OP_BUILD_DICT; }
  329. };
  330. struct SetExpr: SequenceExpr{
  331. using SequenceExpr::SequenceExpr;
  332. Str str() const override { return "set()"; }
  333. Opcode opcode() const override { return OP_BUILD_SET; }
  334. };
  335. struct TupleExpr: SequenceExpr{
  336. using SequenceExpr::SequenceExpr;
  337. Str str() const override { return "tuple()"; }
  338. Opcode opcode() const override { return OP_BUILD_TUPLE; }
  339. bool emit_store(CodeEmitContext* ctx) override {
  340. // TOS is an iterable
  341. // items may contain StarredExpr, we should check it
  342. int starred_i = -1;
  343. for(int i=0; i<items.size(); i++){
  344. if(!items[i]->is_starred()) continue;
  345. if(starred_i == -1) starred_i = i;
  346. else return false; // multiple StarredExpr not allowed
  347. }
  348. if(starred_i == -1){
  349. ctx->emit(OP_UNPACK_SEQUENCE, items.size(), line);
  350. }else{
  351. // starred assignment target must be in a tuple
  352. if(items.size() == 1) return false;
  353. // starred assignment target must be the last one (differ from CPython)
  354. if(starred_i != items.size()-1) return false;
  355. // a,*b = [1,2,3]
  356. // stack is [1,2,3] -> [1,[2,3]]
  357. ctx->emit(OP_UNPACK_EX, items.size()-1, line);
  358. }
  359. // do reverse emit
  360. for(int i=items.size()-1; i>=0; i--){
  361. bool ok = items[i]->emit_store(ctx);
  362. if(!ok) return false;
  363. }
  364. return true;
  365. }
  366. bool emit_del(CodeEmitContext* ctx) override{
  367. for(auto& e: items){
  368. bool ok = e->emit_del(ctx);
  369. if(!ok) return false;
  370. }
  371. return true;
  372. }
  373. };
  374. struct CompExpr: Expr{
  375. Expr_ expr; // loop expr
  376. Expr_ vars; // loop vars
  377. Expr_ iter; // loop iter
  378. Expr_ cond; // optional if condition
  379. virtual Opcode op0() = 0;
  380. virtual Opcode op1() = 0;
  381. void emit(CodeEmitContext* ctx){
  382. ctx->emit(op0(), 0, line);
  383. iter->emit(ctx);
  384. ctx->emit(OP_GET_ITER, BC_NOARG, BC_KEEPLINE);
  385. ctx->enter_block(FOR_LOOP);
  386. ctx->emit(OP_FOR_ITER, BC_NOARG, BC_KEEPLINE);
  387. bool ok = vars->emit_store(ctx);
  388. // this error occurs in `vars` instead of this line, but...nevermind
  389. if(!ok) UNREACHABLE(); // TODO: raise a SyntaxError instead
  390. if(cond){
  391. cond->emit(ctx);
  392. int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
  393. expr->emit(ctx);
  394. ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
  395. ctx->patch_jump(patch);
  396. }else{
  397. expr->emit(ctx);
  398. ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
  399. }
  400. ctx->emit(OP_LOOP_CONTINUE, BC_NOARG, BC_KEEPLINE);
  401. ctx->exit_block();
  402. }
  403. };
  404. struct ListCompExpr: CompExpr{
  405. Opcode op0() override { return OP_BUILD_LIST; }
  406. Opcode op1() override { return OP_LIST_APPEND; }
  407. Str str() const override { return "listcomp()"; }
  408. };
  409. struct DictCompExpr: CompExpr{
  410. Opcode op0() override { return OP_BUILD_DICT; }
  411. Opcode op1() override { return OP_DICT_ADD; }
  412. Str str() const override { return "dictcomp()"; }
  413. };
  414. struct SetCompExpr: CompExpr{
  415. Opcode op0() override { return OP_BUILD_SET; }
  416. Opcode op1() override { return OP_SET_ADD; }
  417. Str str() const override { return "setcomp()"; }
  418. };
  419. struct LambdaExpr: Expr{
  420. FuncDecl_ decl;
  421. NameScope scope;
  422. Str str() const override { return "<lambda>"; }
  423. LambdaExpr(NameScope scope){
  424. this->decl = make_sp<FuncDecl>();
  425. this->decl->name = "<lambda>";
  426. this->scope = scope;
  427. }
  428. void emit(CodeEmitContext* ctx) override {
  429. int index = ctx->add_func_decl(decl);
  430. ctx->emit(OP_LOAD_FUNCTION, index, line);
  431. }
  432. };
  433. struct FStringExpr: Expr{
  434. Str src;
  435. FStringExpr(const Str& src): src(src) {}
  436. Str str() const override {
  437. return "f" + src.escape(true);
  438. }
  439. void emit(CodeEmitContext* ctx) override {
  440. VM* vm = ctx->vm;
  441. static const std::regex pattern(R"(\{(.*?)\})");
  442. std::sregex_iterator begin(src.begin(), src.end(), pattern);
  443. std::sregex_iterator end;
  444. int size = 0;
  445. int i = 0;
  446. for(auto it = begin; it != end; it++) {
  447. std::smatch m = *it;
  448. if (i < m.position()) {
  449. std::string literal = src.substr(i, m.position() - i);
  450. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  451. size++;
  452. }
  453. ctx->emit(OP_LOAD_BUILTIN_EVAL, BC_NOARG, line);
  454. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(m[1].str())), line);
  455. ctx->emit(OP_CALL, 1, line);
  456. size++;
  457. i = (int)(m.position() + m.length());
  458. }
  459. if (i < src.size()) {
  460. std::string literal = src.substr(i, src.size() - i);
  461. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  462. size++;
  463. }
  464. ctx->emit(OP_BUILD_STRING, size, line);
  465. }
  466. };
  467. struct SubscrExpr: Expr{
  468. Expr_ a;
  469. Expr_ b;
  470. Str str() const override { return "a[b]"; }
  471. void emit(CodeEmitContext* ctx) override{
  472. a->emit(ctx);
  473. b->emit(ctx);
  474. ctx->emit(OP_LOAD_SUBSCR, BC_NOARG, line);
  475. }
  476. bool emit_del(CodeEmitContext* ctx) override {
  477. a->emit(ctx);
  478. b->emit(ctx);
  479. ctx->emit(OP_DELETE_SUBSCR, BC_NOARG, line);
  480. return true;
  481. }
  482. bool emit_store(CodeEmitContext* ctx) override {
  483. a->emit(ctx);
  484. b->emit(ctx);
  485. ctx->emit(OP_STORE_SUBSCR, BC_NOARG, line);
  486. return true;
  487. }
  488. };
  489. struct AttribExpr: Expr{
  490. Expr_ a;
  491. Str b;
  492. AttribExpr(Expr_ a, const Str& b): a(std::move(a)), b(b) {}
  493. AttribExpr(Expr_ a, Str&& b): a(std::move(a)), b(std::move(b)) {}
  494. Str str() const override { return "a.b"; }
  495. void emit(CodeEmitContext* ctx) override{
  496. a->emit(ctx);
  497. int index = ctx->add_name(b);
  498. ctx->emit(OP_LOAD_ATTR, index, line);
  499. }
  500. bool emit_del(CodeEmitContext* ctx) override {
  501. a->emit(ctx);
  502. int index = ctx->add_name(b);
  503. ctx->emit(OP_DELETE_ATTR, index, line);
  504. return true;
  505. }
  506. bool emit_store(CodeEmitContext* ctx) override {
  507. a->emit(ctx);
  508. int index = ctx->add_name(b);
  509. ctx->emit(OP_STORE_ATTR, index, line);
  510. return true;
  511. }
  512. };
  513. // PASS
  514. struct CallExpr: Expr{
  515. Expr_ callable;
  516. std::vector<Expr_> args;
  517. std::vector<std::pair<Str, Expr_>> kwargs;
  518. Str str() const override { return "call(...)"; }
  519. std::vector<const Expr*> children() const override {
  520. std::vector<const Expr*> ret;
  521. for(auto& item: args) ret.push_back(item.get());
  522. // ...ignore kwargs for simplicity
  523. return ret;
  524. }
  525. bool need_unpack() const {
  526. for(auto& item: args) if(item->is_starred()) return true;
  527. return false;
  528. }
  529. void emit(CodeEmitContext* ctx) override {
  530. VM* vm = ctx->vm;
  531. callable->emit(ctx);
  532. // emit args
  533. for(auto& item: args) item->emit(ctx);
  534. // emit kwargs
  535. for(auto& item: kwargs){
  536. // TODO: optimize this
  537. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(item.first)), line);
  538. item.second->emit(ctx);
  539. }
  540. int KWARGC = (int)kwargs.size();
  541. int ARGC = (int)args.size();
  542. if(KWARGC > 0){
  543. ctx->emit(need_unpack() ? OP_CALL_KWARGS_UNPACK : OP_CALL_KWARGS, (KWARGC<<16)|ARGC, line);
  544. }else{
  545. ctx->emit(need_unpack() ? OP_CALL_UNPACK : OP_CALL, ARGC, line);
  546. }
  547. }
  548. };
  549. struct BinaryExpr: Expr{
  550. TokenIndex op;
  551. Expr_ lhs;
  552. Expr_ rhs;
  553. Str str() const override { return TK_STR(op); }
  554. std::vector<const Expr*> children() const override {
  555. return {lhs.get(), rhs.get()};
  556. }
  557. void emit(CodeEmitContext* ctx) override {
  558. lhs->emit(ctx);
  559. rhs->emit(ctx);
  560. switch (op) {
  561. case TK("+"): ctx->emit(OP_BINARY_OP, 0, line); break;
  562. case TK("-"): ctx->emit(OP_BINARY_OP, 1, line); break;
  563. case TK("*"): ctx->emit(OP_BINARY_OP, 2, line); break;
  564. case TK("/"): ctx->emit(OP_BINARY_OP, 3, line); break;
  565. case TK("//"): ctx->emit(OP_BINARY_OP, 4, line); break;
  566. case TK("%"): ctx->emit(OP_BINARY_OP, 5, line); break;
  567. case TK("**"): ctx->emit(OP_BINARY_OP, 6, line); break;
  568. case TK("<"): ctx->emit(OP_COMPARE_OP, 0, line); break;
  569. case TK("<="): ctx->emit(OP_COMPARE_OP, 1, line); break;
  570. case TK("=="): ctx->emit(OP_COMPARE_OP, 2, line); break;
  571. case TK("!="): ctx->emit(OP_COMPARE_OP, 3, line); break;
  572. case TK(">"): ctx->emit(OP_COMPARE_OP, 4, line); break;
  573. case TK(">="): ctx->emit(OP_COMPARE_OP, 5, line); break;
  574. case TK("in"): ctx->emit(OP_CONTAINS_OP, 0, line); break;
  575. case TK("not in"): ctx->emit(OP_CONTAINS_OP, 1, line); break;
  576. case TK("is"): ctx->emit(OP_IS_OP, 0, line); break;
  577. case TK("is not"): ctx->emit(OP_IS_OP, 1, line); break;
  578. case TK("<<"): ctx->emit(OP_BITWISE_OP, 0, line); break;
  579. case TK(">>"): ctx->emit(OP_BITWISE_OP, 1, line); break;
  580. case TK("&"): ctx->emit(OP_BITWISE_OP, 2, line); break;
  581. case TK("|"): ctx->emit(OP_BITWISE_OP, 3, line); break;
  582. case TK("^"): ctx->emit(OP_BITWISE_OP, 4, line); break;
  583. default: UNREACHABLE();
  584. }
  585. }
  586. };
  587. // PASS
  588. struct TernaryExpr: Expr{
  589. Expr_ cond;
  590. Expr_ true_expr;
  591. Expr_ false_expr;
  592. Str str() const override {
  593. return "cond ? t : f";
  594. }
  595. std::vector<const Expr*> children() const override {
  596. return {cond.get(), true_expr.get(), false_expr.get()};
  597. }
  598. void emit(CodeEmitContext* ctx) override {
  599. cond->emit(ctx);
  600. int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, cond->line);
  601. true_expr->emit(ctx);
  602. int patch_2 = ctx->emit(OP_JUMP_ABSOLUTE, BC_NOARG, true_expr->line);
  603. ctx->patch_jump(patch);
  604. false_expr->emit(ctx);
  605. ctx->patch_jump(patch_2);
  606. }
  607. };
  608. } // namespace pkpy