expr.h 21 KB

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