expr.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "common.h"
  4. #include "lexer.h"
  5. #include "error.h"
  6. #include "vm.h"
  7. namespace pkpy{
  8. struct CodeEmitContext;
  9. struct Expr;
  10. typedef std::unique_ptr<Expr> Expr_;
  11. struct Expr{
  12. int line = 0;
  13. virtual ~Expr() = default;
  14. virtual void emit(CodeEmitContext* ctx) = 0;
  15. virtual std::string str() const = 0;
  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. virtual bool is_compare() const { return false; }
  20. virtual int star_level() const { return 0; }
  21. virtual bool is_tuple() const { return false; }
  22. bool is_starred() const { return star_level() > 0; }
  23. // for OP_DELETE_XXX
  24. [[nodiscard]] virtual bool emit_del(CodeEmitContext* ctx) {
  25. PK_UNUSED(ctx);
  26. return false;
  27. }
  28. // for OP_STORE_XXX
  29. [[nodiscard]] virtual bool emit_store(CodeEmitContext* ctx) {
  30. PK_UNUSED(ctx);
  31. return false;
  32. }
  33. };
  34. struct CodeEmitContext{
  35. VM* vm;
  36. CodeObject_ co;
  37. // some bugs on MSVC (error C2280) when using std::vector<Expr_>
  38. // so we use stack_no_copy instead
  39. stack_no_copy<Expr_> s_expr;
  40. int level;
  41. std::set<Str> global_names;
  42. CodeEmitContext(VM* vm, CodeObject_ co, int level): vm(vm), co(co), level(level) {}
  43. int curr_block_i = 0;
  44. bool is_compiling_class = false;
  45. int for_loop_depth = 0;
  46. bool is_curr_block_loop() const {
  47. return co->blocks[curr_block_i].type == FOR_LOOP || co->blocks[curr_block_i].type == WHILE_LOOP;
  48. }
  49. void enter_block(CodeBlockType type){
  50. if(type == FOR_LOOP) for_loop_depth++;
  51. co->blocks.push_back(CodeBlock(
  52. type, curr_block_i, for_loop_depth, (int)co->codes.size()
  53. ));
  54. curr_block_i = co->blocks.size()-1;
  55. }
  56. void exit_block(){
  57. auto curr_type = co->blocks[curr_block_i].type;
  58. if(curr_type == FOR_LOOP) for_loop_depth--;
  59. co->blocks[curr_block_i].end = co->codes.size();
  60. curr_block_i = co->blocks[curr_block_i].parent;
  61. if(curr_block_i < 0) FATAL_ERROR();
  62. if(curr_type == FOR_LOOP){
  63. // add a no op here to make block check work
  64. emit(OP_NO_OP, BC_NOARG, BC_KEEPLINE);
  65. }
  66. }
  67. // clear the expression stack and generate bytecode
  68. void emit_expr(){
  69. if(s_expr.size() != 1){
  70. throw std::runtime_error("s_expr.size() != 1\n" + _log_s_expr());
  71. }
  72. Expr_ expr = s_expr.popx();
  73. expr->emit(this);
  74. }
  75. std::string _log_s_expr(){
  76. std::stringstream ss;
  77. for(auto& e: s_expr.data()) ss << e->str() << " ";
  78. return ss.str();
  79. }
  80. int emit(Opcode opcode, int arg, int line) {
  81. co->codes.push_back(
  82. Bytecode{(uint16_t)opcode, (uint16_t)curr_block_i, arg}
  83. );
  84. co->lines.push_back(line);
  85. int i = co->codes.size() - 1;
  86. if(line==BC_KEEPLINE){
  87. if(i>=1) co->lines[i] = co->lines[i-1];
  88. else co->lines[i] = 1;
  89. }
  90. return i;
  91. }
  92. void patch_jump(int index) {
  93. int target = co->codes.size();
  94. co->codes[index].arg = target;
  95. }
  96. bool add_label(StrName name){
  97. if(co->labels.contains(name)) return false;
  98. co->labels.set(name, co->codes.size());
  99. return true;
  100. }
  101. int add_varname(StrName name){
  102. int index = co->varnames_inv.try_get(name);
  103. if(index >= 0) return index;
  104. co->varnames.push_back(name);
  105. index = co->varnames.size() - 1;
  106. co->varnames_inv.set(name, index);
  107. return index;
  108. }
  109. int add_const(PyObject* v){
  110. // simple deduplication, only works for int/float
  111. for(int i=0; i<co->consts.size(); i++){
  112. if(co->consts[i] == v) return i;
  113. }
  114. co->consts.push_back(v);
  115. return co->consts.size() - 1;
  116. }
  117. int add_func_decl(FuncDecl_ decl){
  118. co->func_decls.push_back(decl);
  119. return co->func_decls.size() - 1;
  120. }
  121. };
  122. struct NameExpr: Expr{
  123. StrName name;
  124. NameScope scope;
  125. NameExpr(StrName name, NameScope scope): name(name), scope(scope) {}
  126. std::string str() const override { return fmt("Name(", name.escape(), ")"); }
  127. void emit(CodeEmitContext* ctx) override {
  128. int index = ctx->co->varnames_inv.try_get(name);
  129. if(scope == NAME_LOCAL && index >= 0){
  130. ctx->emit(OP_LOAD_FAST, index, line);
  131. }else{
  132. Opcode op = ctx->level <= 1 ? OP_LOAD_GLOBAL : OP_LOAD_NONLOCAL;
  133. // we cannot determine the scope when calling exec()/eval()
  134. if(scope == NAME_GLOBAL_UNKNOWN) op = OP_LOAD_NAME;
  135. ctx->emit(op, StrName(name).index, line);
  136. }
  137. }
  138. bool emit_del(CodeEmitContext* ctx) override {
  139. switch(scope){
  140. case NAME_LOCAL:
  141. ctx->emit(OP_DELETE_FAST, ctx->add_varname(name), line);
  142. break;
  143. case NAME_GLOBAL:
  144. ctx->emit(OP_DELETE_GLOBAL, StrName(name).index, line);
  145. break;
  146. case NAME_GLOBAL_UNKNOWN:
  147. ctx->emit(OP_DELETE_NAME, StrName(name).index, line);
  148. break;
  149. default: FATAL_ERROR(); break;
  150. }
  151. return true;
  152. }
  153. bool emit_store(CodeEmitContext* ctx) override {
  154. if(ctx->is_compiling_class){
  155. int index = StrName(name).index;
  156. ctx->emit(OP_STORE_CLASS_ATTR, index, line);
  157. return true;
  158. }
  159. switch(scope){
  160. case NAME_LOCAL:
  161. ctx->emit(OP_STORE_FAST, ctx->add_varname(name), line);
  162. break;
  163. case NAME_GLOBAL:
  164. ctx->emit(OP_STORE_GLOBAL, StrName(name).index, line);
  165. break;
  166. case NAME_GLOBAL_UNKNOWN:
  167. ctx->emit(OP_STORE_NAME, StrName(name).index, line);
  168. break;
  169. default: FATAL_ERROR(); break;
  170. }
  171. return true;
  172. }
  173. };
  174. struct StarredExpr: Expr{
  175. int level;
  176. Expr_ child;
  177. StarredExpr(int level, Expr_&& child): level(level), child(std::move(child)) {}
  178. std::string str() const override { return fmt("Starred(level=", level, ")"); }
  179. int star_level() const override { return level; }
  180. void emit(CodeEmitContext* ctx) override {
  181. child->emit(ctx);
  182. ctx->emit(OP_UNARY_STAR, level, line);
  183. }
  184. bool emit_store(CodeEmitContext* ctx) override {
  185. if(level != 1) return false;
  186. // simply proxy to child
  187. return child->emit_store(ctx);
  188. }
  189. };
  190. struct NotExpr: Expr{
  191. Expr_ child;
  192. NotExpr(Expr_&& child): child(std::move(child)) {}
  193. std::string str() const override { return "Not()"; }
  194. void emit(CodeEmitContext* ctx) override {
  195. child->emit(ctx);
  196. ctx->emit(OP_UNARY_NOT, BC_NOARG, line);
  197. }
  198. };
  199. struct AndExpr: Expr{
  200. Expr_ lhs;
  201. Expr_ rhs;
  202. std::string str() const override { return "And()"; }
  203. void emit(CodeEmitContext* ctx) override {
  204. lhs->emit(ctx);
  205. int patch = ctx->emit(OP_JUMP_IF_FALSE_OR_POP, BC_NOARG, line);
  206. rhs->emit(ctx);
  207. ctx->patch_jump(patch);
  208. }
  209. };
  210. struct OrExpr: Expr{
  211. Expr_ lhs;
  212. Expr_ rhs;
  213. std::string str() const override { return "Or()"; }
  214. void emit(CodeEmitContext* ctx) override {
  215. lhs->emit(ctx);
  216. int patch = ctx->emit(OP_JUMP_IF_TRUE_OR_POP, BC_NOARG, line);
  217. rhs->emit(ctx);
  218. ctx->patch_jump(patch);
  219. }
  220. };
  221. // [None, True, False, ...]
  222. struct Literal0Expr: Expr{
  223. TokenIndex token;
  224. Literal0Expr(TokenIndex token): token(token) {}
  225. std::string str() const override { return TK_STR(token); }
  226. void emit(CodeEmitContext* ctx) override {
  227. switch (token) {
  228. case TK("None"): ctx->emit(OP_LOAD_NONE, BC_NOARG, line); break;
  229. case TK("True"): ctx->emit(OP_LOAD_TRUE, BC_NOARG, line); break;
  230. case TK("False"): ctx->emit(OP_LOAD_FALSE, BC_NOARG, line); break;
  231. case TK("..."): ctx->emit(OP_LOAD_ELLIPSIS, BC_NOARG, line); break;
  232. default: FATAL_ERROR();
  233. }
  234. }
  235. bool is_json_object() const override { return true; }
  236. };
  237. struct LongExpr: Expr{
  238. Str s;
  239. LongExpr(const Str& s): s(s) {}
  240. std::string str() const override { return s.str(); }
  241. void emit(CodeEmitContext* ctx) override {
  242. VM* vm = ctx->vm;
  243. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(s)), line);
  244. ctx->emit(OP_BUILD_LONG, BC_NOARG, line);
  245. }
  246. };
  247. // @num, @str which needs to invoke OP_LOAD_CONST
  248. struct LiteralExpr: Expr{
  249. TokenValue value;
  250. LiteralExpr(TokenValue value): value(value) {}
  251. std::string str() const override {
  252. if(std::holds_alternative<i64>(value)){
  253. return std::to_string(std::get<i64>(value));
  254. }
  255. if(std::holds_alternative<f64>(value)){
  256. return std::to_string(std::get<f64>(value));
  257. }
  258. if(std::holds_alternative<Str>(value)){
  259. Str s = std::get<Str>(value).escape();
  260. return s.str();
  261. }
  262. FATAL_ERROR();
  263. }
  264. void emit(CodeEmitContext* ctx) override {
  265. VM* vm = ctx->vm;
  266. PyObject* obj = nullptr;
  267. if(std::holds_alternative<i64>(value)){
  268. i64 _val = std::get<i64>(value);
  269. if(_val >= INT16_MIN && _val <= INT16_MAX){
  270. ctx->emit(OP_LOAD_INTEGER, (int)_val, line);
  271. return;
  272. }
  273. obj = VAR(_val);
  274. }
  275. if(std::holds_alternative<f64>(value)){
  276. obj = VAR(std::get<f64>(value));
  277. }
  278. if(std::holds_alternative<Str>(value)){
  279. obj = VAR(std::get<Str>(value));
  280. }
  281. if(obj == nullptr) FATAL_ERROR();
  282. ctx->emit(OP_LOAD_CONST, ctx->add_const(obj), line);
  283. }
  284. bool is_literal() const override { return true; }
  285. bool is_json_object() const override { return true; }
  286. };
  287. struct NegatedExpr: Expr{
  288. Expr_ child;
  289. NegatedExpr(Expr_&& child): child(std::move(child)) {}
  290. std::string str() const override { return "Negated()"; }
  291. void emit(CodeEmitContext* ctx) override {
  292. VM* vm = ctx->vm;
  293. // if child is a int of float, do constant folding
  294. if(child->is_literal()){
  295. LiteralExpr* lit = static_cast<LiteralExpr*>(child.get());
  296. if(std::holds_alternative<i64>(lit->value)){
  297. i64 _val = -std::get<i64>(lit->value);
  298. if(_val >= INT16_MIN && _val <= INT16_MAX){
  299. ctx->emit(OP_LOAD_INTEGER, (int)_val, line);
  300. }else{
  301. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(_val)), line);
  302. }
  303. return;
  304. }
  305. if(std::holds_alternative<f64>(lit->value)){
  306. PyObject* obj = VAR(-std::get<f64>(lit->value));
  307. ctx->emit(OP_LOAD_CONST, ctx->add_const(obj), line);
  308. return;
  309. }
  310. }
  311. child->emit(ctx);
  312. ctx->emit(OP_UNARY_NEGATIVE, BC_NOARG, line);
  313. }
  314. bool is_json_object() const override {
  315. return child->is_literal();
  316. }
  317. };
  318. struct SliceExpr: Expr{
  319. Expr_ start;
  320. Expr_ stop;
  321. Expr_ step;
  322. std::string str() const override { return "Slice()"; }
  323. void emit(CodeEmitContext* ctx) override {
  324. if(start){
  325. start->emit(ctx);
  326. }else{
  327. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  328. }
  329. if(stop){
  330. stop->emit(ctx);
  331. }else{
  332. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  333. }
  334. if(step){
  335. step->emit(ctx);
  336. }else{
  337. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  338. }
  339. ctx->emit(OP_BUILD_SLICE, BC_NOARG, line);
  340. }
  341. };
  342. struct DictItemExpr: Expr{
  343. Expr_ key; // maybe nullptr if it is **kwargs
  344. Expr_ value;
  345. std::string str() const override { return "DictItem()"; }
  346. int star_level() const override { return value->star_level(); }
  347. void emit(CodeEmitContext* ctx) override {
  348. if(is_starred()){
  349. PK_ASSERT(key == nullptr);
  350. value->emit(ctx);
  351. }else{
  352. value->emit(ctx);
  353. key->emit(ctx); // reverse order
  354. ctx->emit(OP_BUILD_TUPLE, 2, line);
  355. }
  356. }
  357. };
  358. struct SequenceExpr: Expr{
  359. std::vector<Expr_> items;
  360. SequenceExpr(std::vector<Expr_>&& items): items(std::move(items)) {}
  361. virtual Opcode opcode() const = 0;
  362. void emit(CodeEmitContext* ctx) override {
  363. for(auto& item: items) item->emit(ctx);
  364. ctx->emit(opcode(), items.size(), line);
  365. }
  366. };
  367. struct ListExpr: SequenceExpr{
  368. using SequenceExpr::SequenceExpr;
  369. std::string str() const override { return "List()"; }
  370. Opcode opcode() const override {
  371. for(auto& e: items) if(e->is_starred()) return OP_BUILD_LIST_UNPACK;
  372. return OP_BUILD_LIST;
  373. }
  374. bool is_json_object() const override { return true; }
  375. };
  376. struct DictExpr: SequenceExpr{
  377. using SequenceExpr::SequenceExpr;
  378. std::string str() const override { return "Dict()"; }
  379. Opcode opcode() const override {
  380. for(auto& e: items) if(e->is_starred()) return OP_BUILD_DICT_UNPACK;
  381. return OP_BUILD_DICT;
  382. }
  383. bool is_json_object() const override { return true; }
  384. };
  385. struct SetExpr: SequenceExpr{
  386. using SequenceExpr::SequenceExpr;
  387. std::string str() const override { return "Set()"; }
  388. Opcode opcode() const override {
  389. for(auto& e: items) if(e->is_starred()) return OP_BUILD_SET_UNPACK;
  390. return OP_BUILD_SET;
  391. }
  392. };
  393. struct TupleExpr: SequenceExpr{
  394. using SequenceExpr::SequenceExpr;
  395. std::string str() const override { return "Tuple()"; }
  396. bool is_tuple() const override { return true; }
  397. Opcode opcode() const override {
  398. for(auto& e: items) if(e->is_starred()) return OP_BUILD_TUPLE_UNPACK;
  399. return OP_BUILD_TUPLE;
  400. }
  401. bool emit_store(CodeEmitContext* ctx) override {
  402. // TOS is an iterable
  403. // items may contain StarredExpr, we should check it
  404. int starred_i = -1;
  405. for(int i=0; i<items.size(); i++){
  406. if(!items[i]->is_starred()) continue;
  407. if(starred_i == -1) starred_i = i;
  408. else return false; // multiple StarredExpr not allowed
  409. }
  410. if(starred_i == -1){
  411. Bytecode& prev = ctx->co->codes.back();
  412. if(prev.op == OP_BUILD_TUPLE && prev.arg == items.size()){
  413. // build tuple and unpack it is meaningless
  414. prev.op = OP_NO_OP;
  415. prev.arg = BC_NOARG;
  416. }else{
  417. ctx->emit(OP_UNPACK_SEQUENCE, items.size(), line);
  418. }
  419. }else{
  420. // starred assignment target must be in a tuple
  421. if(items.size() == 1) return false;
  422. // starred assignment target must be the last one (differ from cpython)
  423. if(starred_i != items.size()-1) return false;
  424. // a,*b = [1,2,3]
  425. // stack is [1,2,3] -> [1,[2,3]]
  426. ctx->emit(OP_UNPACK_EX, items.size()-1, line);
  427. }
  428. // do reverse emit
  429. for(int i=items.size()-1; i>=0; i--){
  430. bool ok = items[i]->emit_store(ctx);
  431. if(!ok) return false;
  432. }
  433. return true;
  434. }
  435. bool emit_del(CodeEmitContext* ctx) override{
  436. for(auto& e: items){
  437. bool ok = e->emit_del(ctx);
  438. if(!ok) return false;
  439. }
  440. return true;
  441. }
  442. };
  443. struct CompExpr: Expr{
  444. Expr_ expr; // loop expr
  445. Expr_ vars; // loop vars
  446. Expr_ iter; // loop iter
  447. Expr_ cond; // optional if condition
  448. virtual Opcode op0() = 0;
  449. virtual Opcode op1() = 0;
  450. void emit(CodeEmitContext* ctx){
  451. ctx->emit(op0(), 0, line);
  452. iter->emit(ctx);
  453. ctx->emit(OP_GET_ITER, BC_NOARG, BC_KEEPLINE);
  454. ctx->enter_block(FOR_LOOP);
  455. ctx->emit(OP_FOR_ITER, BC_NOARG, BC_KEEPLINE);
  456. bool ok = vars->emit_store(ctx);
  457. // this error occurs in `vars` instead of this line, but...nevermind
  458. PK_ASSERT(ok); // TODO: raise a SyntaxError instead
  459. if(cond){
  460. cond->emit(ctx);
  461. int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
  462. expr->emit(ctx);
  463. ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
  464. ctx->patch_jump(patch);
  465. }else{
  466. expr->emit(ctx);
  467. ctx->emit(op1(), BC_NOARG, BC_KEEPLINE);
  468. }
  469. ctx->emit(OP_LOOP_CONTINUE, BC_NOARG, BC_KEEPLINE);
  470. ctx->exit_block();
  471. }
  472. };
  473. struct ListCompExpr: CompExpr{
  474. Opcode op0() override { return OP_BUILD_LIST; }
  475. Opcode op1() override { return OP_LIST_APPEND; }
  476. std::string str() const override { return "ListComp()"; }
  477. };
  478. struct DictCompExpr: CompExpr{
  479. Opcode op0() override { return OP_BUILD_DICT; }
  480. Opcode op1() override { return OP_DICT_ADD; }
  481. std::string str() const override { return "DictComp()"; }
  482. };
  483. struct SetCompExpr: CompExpr{
  484. Opcode op0() override { return OP_BUILD_SET; }
  485. Opcode op1() override { return OP_SET_ADD; }
  486. std::string str() const override { return "SetComp()"; }
  487. };
  488. struct LambdaExpr: Expr{
  489. FuncDecl_ decl;
  490. std::string str() const override { return "Lambda()"; }
  491. LambdaExpr(FuncDecl_ decl): decl(decl) {}
  492. void emit(CodeEmitContext* ctx) override {
  493. int index = ctx->add_func_decl(decl);
  494. ctx->emit(OP_LOAD_FUNCTION, index, line);
  495. }
  496. };
  497. struct FStringExpr: Expr{
  498. Str src;
  499. FStringExpr(const Str& src): src(src) {}
  500. std::string str() const override {
  501. return fmt("f", src.escape());
  502. }
  503. void _load_simple_expr(CodeEmitContext* ctx, Str expr){
  504. // TODO: pre compile this into a function
  505. int dot = expr.index(".");
  506. if(dot < 0){
  507. ctx->emit(OP_LOAD_NAME, StrName(expr.sv()).index, line);
  508. }else{
  509. StrName name(expr.substr(0, dot).sv());
  510. StrName attr(expr.substr(dot+1).sv());
  511. ctx->emit(OP_LOAD_NAME, name.index, line);
  512. ctx->emit(OP_LOAD_ATTR, attr.index, line);
  513. }
  514. }
  515. void emit(CodeEmitContext* ctx) override {
  516. VM* vm = ctx->vm;
  517. static const std::regex pattern(R"(\{(.*?)\})");
  518. std::cregex_iterator begin(src.begin(), src.end(), pattern);
  519. std::cregex_iterator end;
  520. int size = 0;
  521. int i = 0;
  522. for(auto it = begin; it != end; it++) {
  523. std::cmatch m = *it;
  524. if (i < m.position()) {
  525. Str literal = src.substr(i, m.position() - i);
  526. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  527. size++;
  528. }
  529. Str expr = m[1].str();
  530. int conon = expr.index(":");
  531. if(conon >= 0){
  532. _load_simple_expr(ctx, expr.substr(0, conon));
  533. Str spec = expr.substr(conon+1);
  534. ctx->emit(OP_FORMAT_STRING, ctx->add_const(VAR(spec)), line);
  535. }else{
  536. _load_simple_expr(ctx, expr);
  537. }
  538. size++;
  539. i = (int)(m.position() + m.length());
  540. }
  541. if (i < src.length()) {
  542. Str literal = src.substr(i, src.length() - i);
  543. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  544. size++;
  545. }
  546. ctx->emit(OP_BUILD_STRING, size, line);
  547. }
  548. };
  549. struct SubscrExpr: Expr{
  550. Expr_ a;
  551. Expr_ b;
  552. std::string str() const override { return "Subscr()"; }
  553. void emit(CodeEmitContext* ctx) override{
  554. a->emit(ctx);
  555. b->emit(ctx);
  556. ctx->emit(OP_LOAD_SUBSCR, BC_NOARG, line);
  557. }
  558. bool emit_del(CodeEmitContext* ctx) override {
  559. a->emit(ctx);
  560. b->emit(ctx);
  561. ctx->emit(OP_DELETE_SUBSCR, BC_NOARG, line);
  562. return true;
  563. }
  564. bool emit_store(CodeEmitContext* ctx) override {
  565. a->emit(ctx);
  566. b->emit(ctx);
  567. ctx->emit(OP_STORE_SUBSCR, BC_NOARG, line);
  568. return true;
  569. }
  570. };
  571. struct AttribExpr: Expr{
  572. Expr_ a;
  573. Str b;
  574. AttribExpr(Expr_ a, const Str& b): a(std::move(a)), b(b) {}
  575. AttribExpr(Expr_ a, Str&& b): a(std::move(a)), b(std::move(b)) {}
  576. std::string str() const override { return "Attrib()"; }
  577. void emit(CodeEmitContext* ctx) override{
  578. a->emit(ctx);
  579. int index = StrName(b).index;
  580. ctx->emit(OP_LOAD_ATTR, index, line);
  581. }
  582. bool emit_del(CodeEmitContext* ctx) override {
  583. a->emit(ctx);
  584. int index = StrName(b).index;
  585. ctx->emit(OP_DELETE_ATTR, index, line);
  586. return true;
  587. }
  588. bool emit_store(CodeEmitContext* ctx) override {
  589. a->emit(ctx);
  590. int index = StrName(b).index;
  591. ctx->emit(OP_STORE_ATTR, index, line);
  592. return true;
  593. }
  594. void emit_method(CodeEmitContext* ctx) {
  595. a->emit(ctx);
  596. int index = StrName(b).index;
  597. ctx->emit(OP_LOAD_METHOD, index, line);
  598. }
  599. bool is_attrib() const override { return true; }
  600. };
  601. struct CallExpr: Expr{
  602. Expr_ callable;
  603. std::vector<Expr_> args;
  604. // **a will be interpreted as a special keyword argument: {"**": a}
  605. std::vector<std::pair<Str, Expr_>> kwargs;
  606. std::string str() const override { return "Call()"; }
  607. void emit(CodeEmitContext* ctx) override {
  608. bool vargs = false;
  609. bool vkwargs = false;
  610. for(auto& arg: args) if(arg->is_starred()) vargs = true;
  611. for(auto& item: kwargs) if(item.second->is_starred()) vkwargs = true;
  612. // if callable is a AttrExpr, we should try to use `fast_call` instead of use `boundmethod` proxy
  613. if(callable->is_attrib()){
  614. auto p = static_cast<AttribExpr*>(callable.get());
  615. p->emit_method(ctx); // OP_LOAD_METHOD
  616. }else{
  617. callable->emit(ctx);
  618. ctx->emit(OP_LOAD_NULL, BC_NOARG, BC_KEEPLINE);
  619. }
  620. if(vargs || vkwargs){
  621. for(auto& item: args) item->emit(ctx);
  622. ctx->emit(OP_BUILD_TUPLE_UNPACK, (int)args.size(), line);
  623. if(!kwargs.empty()){
  624. for(auto& item: kwargs){
  625. if(item.second->is_starred()){
  626. if(item.second->star_level() != 2) FATAL_ERROR();
  627. item.second->emit(ctx);
  628. }else{
  629. // k=v
  630. int index = ctx->add_const(py_var(ctx->vm, item.first));
  631. ctx->emit(OP_LOAD_CONST, index, line);
  632. item.second->emit(ctx);
  633. ctx->emit(OP_BUILD_TUPLE, 2, line);
  634. }
  635. }
  636. ctx->emit(OP_BUILD_DICT_UNPACK, (int)kwargs.size(), line);
  637. ctx->emit(OP_CALL_TP, 1, line);
  638. }else{
  639. ctx->emit(OP_CALL_TP, 0, line);
  640. }
  641. }else{
  642. // vectorcall protocal
  643. for(auto& item: args) item->emit(ctx);
  644. for(auto& item: kwargs){
  645. int index = StrName(item.first.sv()).index;
  646. ctx->emit(OP_LOAD_INTEGER, index, line);
  647. item.second->emit(ctx);
  648. }
  649. int KWARGC = (int)kwargs.size();
  650. int ARGC = (int)args.size();
  651. ctx->emit(OP_CALL, (KWARGC<<16)|ARGC, line);
  652. }
  653. }
  654. };
  655. struct GroupedExpr: Expr{
  656. Expr_ a;
  657. std::string str() const override { return "Grouped()"; }
  658. GroupedExpr(Expr_&& a): a(std::move(a)) {}
  659. void emit(CodeEmitContext* ctx) override{
  660. a->emit(ctx);
  661. }
  662. bool emit_del(CodeEmitContext* ctx) override {
  663. return a->emit_del(ctx);
  664. }
  665. bool emit_store(CodeEmitContext* ctx) override {
  666. return a->emit_store(ctx);
  667. }
  668. };
  669. struct BinaryExpr: Expr{
  670. TokenIndex op;
  671. Expr_ lhs;
  672. Expr_ rhs;
  673. std::string str() const override { return TK_STR(op); }
  674. bool is_compare() const override {
  675. switch(op){
  676. case TK("<"): case TK("<="): case TK("=="):
  677. case TK("!="): case TK(">"): case TK(">="): return true;
  678. default: return false;
  679. }
  680. }
  681. void _emit_compare(CodeEmitContext* ctx, std::vector<int>& jmps){
  682. if(lhs->is_compare()){
  683. static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
  684. }else{
  685. lhs->emit(ctx); // [a]
  686. }
  687. rhs->emit(ctx); // [a, b]
  688. ctx->emit(OP_DUP_TOP, BC_NOARG, line); // [a, b, b]
  689. ctx->emit(OP_ROT_THREE, BC_NOARG, line); // [b, a, b]
  690. switch(op){
  691. case TK("<"): ctx->emit(OP_COMPARE_LT, BC_NOARG, line); break;
  692. case TK("<="): ctx->emit(OP_COMPARE_LE, BC_NOARG, line); break;
  693. case TK("=="): ctx->emit(OP_COMPARE_EQ, BC_NOARG, line); break;
  694. case TK("!="): ctx->emit(OP_COMPARE_NE, BC_NOARG, line); break;
  695. case TK(">"): ctx->emit(OP_COMPARE_GT, BC_NOARG, line); break;
  696. case TK(">="): ctx->emit(OP_COMPARE_GE, BC_NOARG, line); break;
  697. default: UNREACHABLE();
  698. }
  699. // [b, RES]
  700. int index = ctx->emit(OP_SHORTCUT_IF_FALSE_OR_POP, BC_NOARG, line);
  701. jmps.push_back(index);
  702. }
  703. void emit(CodeEmitContext* ctx) override {
  704. std::vector<int> jmps;
  705. if(is_compare() && lhs->is_compare()){
  706. // (a < b) < c
  707. static_cast<BinaryExpr*>(lhs.get())->_emit_compare(ctx, jmps);
  708. // [b, RES]
  709. }else{
  710. // (1 + 2) < c
  711. lhs->emit(ctx);
  712. }
  713. rhs->emit(ctx);
  714. switch (op) {
  715. case TK("+"): ctx->emit(OP_BINARY_ADD, BC_NOARG, line); break;
  716. case TK("-"): ctx->emit(OP_BINARY_SUB, BC_NOARG, line); break;
  717. case TK("*"): ctx->emit(OP_BINARY_MUL, BC_NOARG, line); break;
  718. case TK("/"): ctx->emit(OP_BINARY_TRUEDIV, BC_NOARG, line); break;
  719. case TK("//"): ctx->emit(OP_BINARY_FLOORDIV, BC_NOARG, line); break;
  720. case TK("%"): ctx->emit(OP_BINARY_MOD, BC_NOARG, line); break;
  721. case TK("**"): ctx->emit(OP_BINARY_POW, BC_NOARG, line); break;
  722. case TK("<"): ctx->emit(OP_COMPARE_LT, BC_NOARG, line); break;
  723. case TK("<="): ctx->emit(OP_COMPARE_LE, BC_NOARG, line); break;
  724. case TK("=="): ctx->emit(OP_COMPARE_EQ, BC_NOARG, line); break;
  725. case TK("!="): ctx->emit(OP_COMPARE_NE, BC_NOARG, line); break;
  726. case TK(">"): ctx->emit(OP_COMPARE_GT, BC_NOARG, line); break;
  727. case TK(">="): ctx->emit(OP_COMPARE_GE, BC_NOARG, line); break;
  728. case TK("in"): ctx->emit(OP_CONTAINS_OP, 0, line); break;
  729. case TK("not in"): ctx->emit(OP_CONTAINS_OP, 1, line); break;
  730. case TK("is"): ctx->emit(OP_IS_OP, 0, line); break;
  731. case TK("is not"): ctx->emit(OP_IS_OP, 1, line); break;
  732. case TK("<<"): ctx->emit(OP_BITWISE_LSHIFT, BC_NOARG, line); break;
  733. case TK(">>"): ctx->emit(OP_BITWISE_RSHIFT, BC_NOARG, line); break;
  734. case TK("&"): ctx->emit(OP_BITWISE_AND, BC_NOARG, line); break;
  735. case TK("|"): ctx->emit(OP_BITWISE_OR, BC_NOARG, line); break;
  736. case TK("^"): ctx->emit(OP_BITWISE_XOR, BC_NOARG, line); break;
  737. case TK("@"): ctx->emit(OP_BINARY_MATMUL, BC_NOARG, line); break;
  738. default: FATAL_ERROR();
  739. }
  740. for(int i: jmps) ctx->patch_jump(i);
  741. }
  742. };
  743. struct TernaryExpr: Expr{
  744. Expr_ cond;
  745. Expr_ true_expr;
  746. Expr_ false_expr;
  747. std::string str() const override { return "Ternary()"; }
  748. void emit(CodeEmitContext* ctx) override {
  749. cond->emit(ctx);
  750. int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, cond->line);
  751. true_expr->emit(ctx);
  752. int patch_2 = ctx->emit(OP_JUMP_ABSOLUTE, BC_NOARG, true_expr->line);
  753. ctx->patch_jump(patch);
  754. false_expr->emit(ctx);
  755. ctx->patch_jump(patch_2);
  756. }
  757. };
  758. } // namespace pkpy