expr.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "common.h"
  4. #include "lexer.h"
  5. #include "error.h"
  6. #include "ceval.h"
  7. namespace pkpy{
  8. struct CodeEmitContext;
  9. struct Expr{
  10. int line = 0;
  11. virtual ~Expr() = default;
  12. virtual void emit(CodeEmitContext* ctx) = 0;
  13. virtual Str str() const = 0;
  14. virtual std::vector<const Expr*> children() const { return {}; }
  15. virtual bool is_starred() const { return false; }
  16. // for OP_DELETE_XXX
  17. virtual bool emit_del(CodeEmitContext* ctx) { return false; }
  18. // for OP_STORE_XXX
  19. virtual bool emit_store(CodeEmitContext* ctx) { return false; }
  20. };
  21. struct CodeEmitContext{
  22. CodeObject_ co;
  23. VM* vm;
  24. stack<Expr_> s_expr;
  25. CodeEmitContext(VM* vm, CodeObject_ co): co(co) {}
  26. CodeEmitContext(const CodeEmitContext&) = delete;
  27. CodeEmitContext& operator=(const CodeEmitContext&) = delete;
  28. CodeEmitContext(CodeEmitContext&&) = delete;
  29. CodeEmitContext& operator=(CodeEmitContext&&) = delete;
  30. int curr_block_i = 0;
  31. bool is_compiling_class = false;
  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. co->blocks.push_back(CodeBlock{
  37. type, curr_block_i, (int)co->codes.size()
  38. });
  39. curr_block_i = co->blocks.size()-1;
  40. }
  41. void exit_block(){
  42. co->blocks[curr_block_i].end = co->codes.size();
  43. curr_block_i = co->blocks[curr_block_i].parent;
  44. if(curr_block_i < 0) UNREACHABLE();
  45. }
  46. // clear the expression stack and generate bytecode
  47. void emit_expr(){
  48. if(s_expr.size() != 1) UNREACHABLE();
  49. Expr_ expr = s_expr.popx();
  50. expr->emit(this);
  51. }
  52. int emit(Opcode opcode, int arg, int line) {
  53. co->codes.push_back(
  54. Bytecode{(uint16_t)opcode, (uint16_t)curr_block_i, arg, line}
  55. );
  56. int i = co->codes.size() - 1;
  57. if(line==BC_KEEPLINE && i>=1) co->codes[i].line = co->codes[i-1].line;
  58. return i;
  59. }
  60. void patch_jump(int index) {
  61. int target = co->codes.size();
  62. co->codes[index].arg = target;
  63. }
  64. bool add_label(StrName label){
  65. if(co->labels.count(label)) return false;
  66. co->labels[label] = co->codes.size();
  67. return true;
  68. }
  69. int add_name(StrName name){
  70. for(int i=0; i<co->names.size(); i++){
  71. if(co->names[i] == name) return i;
  72. }
  73. co->names.push_back(name);
  74. return co->names.size() - 1;
  75. }
  76. int add_const(PyObject* v){
  77. co->consts.push_back(v);
  78. return co->consts.size() - 1;
  79. }
  80. };
  81. // PASS
  82. struct NameExpr: Expr{
  83. Str name;
  84. NameScope scope;
  85. NameExpr(const Str& name, NameScope scope): name(name), scope(scope) {}
  86. NameExpr(Str&& name, NameScope scope): name(std::move(name)), scope(scope) {}
  87. Str str() const override { return "$" + name; }
  88. void emit(CodeEmitContext* ctx) override {
  89. int index = ctx->add_name(name);
  90. ctx->emit(OP_LOAD_NAME, index, line);
  91. }
  92. bool emit_del(CodeEmitContext* ctx) override {
  93. int index = ctx->add_name(name);
  94. switch(scope){
  95. case NAME_LOCAL:
  96. ctx->emit(OP_DELETE_LOCAL, index, line);
  97. break;
  98. case NAME_GLOBAL:
  99. ctx->emit(OP_DELETE_GLOBAL, index, line);
  100. break;
  101. default: UNREACHABLE(); break;
  102. }
  103. return true;
  104. }
  105. bool emit_store(CodeEmitContext* ctx) override {
  106. int index = ctx->add_name(name);
  107. switch(scope){
  108. case NAME_LOCAL:
  109. ctx->emit(OP_STORE_LOCAL, index, line);
  110. break;
  111. case NAME_GLOBAL:
  112. ctx->emit(OP_STORE_GLOBAL, index, line);
  113. break;
  114. default: UNREACHABLE(); break;
  115. }
  116. return true;
  117. }
  118. };
  119. // *号运算符,作为左值和右值效果不同
  120. struct StarredExpr: Expr{
  121. Expr_ child;
  122. StarredExpr(Expr_&& child): child(std::move(child)) {}
  123. Str str() const override { return "*"; }
  124. std::vector<const Expr*> children() const override { return {child.get()}; }
  125. bool is_starred() const override { return true; }
  126. void emit(CodeEmitContext* ctx) override {
  127. child->emit(ctx);
  128. // as a rvalue, we should do unpack here
  129. //ctx->emit(OP_UNARY_STAR, (int)false, line);
  130. }
  131. bool emit_store(CodeEmitContext* ctx) override {
  132. child->emit(ctx);
  133. // as a lvalue, we should do pack here
  134. //ctx->emit(OP_UNARY_STAR, (int)true, line);
  135. return true;
  136. }
  137. };
  138. // PASS
  139. struct NegatedExpr: Expr{
  140. Expr_ child;
  141. NegatedExpr(Expr_&& child): child(std::move(child)) {}
  142. Str str() const override { return "-"; }
  143. std::vector<const Expr*> children() const override { return {child.get()}; }
  144. void emit(CodeEmitContext* ctx) override {
  145. child->emit(ctx);
  146. ctx->emit(OP_UNARY_NEGATIVE, BC_NOARG, line);
  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. void emit(CodeEmitContext* ctx) override {
  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. if(!obj) UNREACHABLE();
  230. int index = ctx->add_const(obj);
  231. ctx->emit(OP_LOAD_CONST, index, line);
  232. }
  233. };
  234. // PASS
  235. struct SliceExpr: Expr{
  236. Expr_ start;
  237. Expr_ stop;
  238. Expr_ step;
  239. Str str() const override { return "slice()"; }
  240. std::vector<const Expr*> children() const override {
  241. // may contain nullptr
  242. return {start.get(), stop.get(), step.get()};
  243. }
  244. void emit(CodeEmitContext* ctx) override {
  245. if(start){
  246. start->emit(ctx);
  247. }else{
  248. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  249. }
  250. if(stop){
  251. stop->emit(ctx);
  252. }else{
  253. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  254. }
  255. if(step){
  256. step->emit(ctx);
  257. }else{
  258. ctx->emit(OP_LOAD_NONE, BC_NOARG, line);
  259. }
  260. ctx->emit(OP_BUILD_SLICE, BC_NOARG, line);
  261. }
  262. };
  263. struct DictItemExpr: Expr{
  264. Expr_ key;
  265. Expr_ value;
  266. Str str() const override { return "k:v"; }
  267. std::vector<const Expr*> children() const override { return {key.get(), value.get()}; }
  268. void emit(CodeEmitContext* ctx) override {
  269. key->emit(ctx);
  270. value->emit(ctx);
  271. ctx->emit(OP_BUILD_TUPLE, 2, line);
  272. }
  273. };
  274. struct SequenceExpr: Expr{
  275. std::vector<Expr_> items;
  276. SequenceExpr(std::vector<Expr_>&& items): items(std::move(items)) {}
  277. virtual Opcode opcode() const = 0;
  278. std::vector<const Expr*> children() const override {
  279. std::vector<const Expr*> ret;
  280. for(auto& item: items) ret.push_back(item.get());
  281. return ret;
  282. }
  283. void emit(CodeEmitContext* ctx) override {
  284. for(auto& item: items) item->emit(ctx);
  285. ctx->emit(opcode(), items.size(), line);
  286. }
  287. };
  288. struct ListExpr: SequenceExpr{
  289. Str str() const override { return "list()"; }
  290. Opcode opcode() const override { return OP_BUILD_LIST; }
  291. };
  292. struct DictExpr: SequenceExpr{
  293. Str str() const override { return "dict()"; }
  294. Opcode opcode() const override { return OP_BUILD_MAP; }
  295. };
  296. struct SetExpr: SequenceExpr{
  297. Str str() const override { return "set()"; }
  298. Opcode opcode() const override { return OP_BUILD_SET; }
  299. };
  300. struct TupleExpr: SequenceExpr{
  301. Str str() const override { return "tuple()"; }
  302. Opcode opcode() const override { return OP_BUILD_TUPLE; }
  303. bool emit_store(CodeEmitContext* ctx) override {
  304. // ...
  305. return true;
  306. }
  307. };
  308. struct CompExpr: Expr{
  309. Expr_ expr; // loop expr
  310. Expr_ vars; // loop vars
  311. Expr_ iter; // loop iter
  312. Expr_ cond; // optional if condition
  313. };
  314. struct ListCompExpr: CompExpr{
  315. };
  316. struct DictCompExpr: CompExpr{
  317. };
  318. struct SetCompExpr: CompExpr{
  319. };
  320. struct LambdaExpr: Expr{
  321. Function func;
  322. NameScope scope;
  323. Str str() const override { return "<lambda>"; }
  324. void emit(CodeEmitContext* ctx) override {
  325. VM* vm = ctx->vm;
  326. ctx->emit(OP_LOAD_FUNCTION, ctx->add_const(VAR(func)), line);
  327. if(scope == NAME_LOCAL){
  328. ctx->emit(OP_SETUP_CLOSURE, BC_NOARG, BC_KEEPLINE);
  329. }
  330. }
  331. };
  332. struct FStringExpr: Expr{
  333. Str src;
  334. FStringExpr(const Str& src): src(src) {}
  335. Str str() const override {
  336. return "f" + src.escape(true);
  337. }
  338. void emit(CodeEmitContext* ctx) override {
  339. VM* vm = ctx->vm;
  340. static const std::regex pattern(R"(\{(.*?)\})");
  341. std::sregex_iterator begin(src.begin(), src.end(), pattern);
  342. std::sregex_iterator end;
  343. int size = 0;
  344. int i = 0;
  345. for(auto it = begin; it != end; it++) {
  346. std::smatch m = *it;
  347. if (i < m.position()) {
  348. std::string literal = src.substr(i, m.position() - i);
  349. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  350. size++;
  351. }
  352. ctx->emit(OP_LOAD_EVAL_FN, BC_NOARG, line);
  353. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(m[1].str())), line);
  354. ctx->emit(OP_CALL, 1, line);
  355. size++;
  356. i = (int)(m.position() + m.length());
  357. }
  358. if (i < src.size()) {
  359. std::string literal = src.substr(i, src.size() - i);
  360. ctx->emit(OP_LOAD_CONST, ctx->add_const(VAR(literal)), line);
  361. size++;
  362. }
  363. ctx->emit(OP_BUILD_STRING, size, line);
  364. }
  365. };
  366. struct SubscrExpr: Expr{
  367. Expr_ a;
  368. Expr_ b;
  369. Str str() const override { return "a[b]"; }
  370. void emit(CodeEmitContext* ctx) override{
  371. a->emit(ctx);
  372. b->emit(ctx);
  373. ctx->emit(OP_LOAD_SUBSCR, BC_NOARG, line);
  374. }
  375. bool emit_del(CodeEmitContext* ctx) override {
  376. a->emit(ctx);
  377. b->emit(ctx);
  378. ctx->emit(OP_DELETE_SUBSCR, BC_NOARG, line);
  379. return true;
  380. }
  381. bool emit_store(CodeEmitContext* ctx) override {
  382. a->emit(ctx);
  383. b->emit(ctx);
  384. ctx->emit(OP_STORE_SUBSCR, BC_NOARG, line);
  385. return true;
  386. }
  387. };
  388. struct AttribExpr: Expr{
  389. Expr_ a;
  390. Str b;
  391. AttribExpr(Expr_ a, const Str& b): a(std::move(a)), b(b) {}
  392. AttribExpr(Expr_ a, Str&& b): a(std::move(a)), b(std::move(b)) {}
  393. Str str() const override { return "a.b"; }
  394. void emit(CodeEmitContext* ctx) override{
  395. a->emit(ctx);
  396. int index = ctx->add_name(b);
  397. ctx->emit(OP_LOAD_ATTR, index, line);
  398. }
  399. bool emit_del(CodeEmitContext* ctx) override {
  400. a->emit(ctx);
  401. int index = ctx->add_name(b);
  402. ctx->emit(OP_DELETE_ATTR, index, line);
  403. return true;
  404. }
  405. bool emit_store(CodeEmitContext* ctx) override {
  406. a->emit(ctx);
  407. int index = ctx->add_name(b);
  408. ctx->emit(OP_STORE_ATTR, index, line);
  409. return true;
  410. }
  411. };
  412. // PASS
  413. struct CallExpr: Expr{
  414. Expr_ callable;
  415. std::vector<Expr_> args;
  416. std::vector<std::pair<Str, Expr_>> kwargs;
  417. Str str() const override { return "call(...)"; }
  418. std::vector<const Expr*> children() const override {
  419. std::vector<const Expr*> ret;
  420. for(auto& item: args) ret.push_back(item.get());
  421. // ...ignore kwargs for simplicity
  422. return ret;
  423. }
  424. bool need_unpack() const {
  425. for(auto& item: args) if(item->is_starred()) return true;
  426. return false;
  427. }
  428. void emit(CodeEmitContext* ctx) override {
  429. callable->emit(ctx);
  430. int KWARGC = (int)kwargs.size();
  431. int ARGC = (int)args.size();
  432. if(KWARGC > 0){
  433. ctx->emit(need_unpack() ? OP_CALL_KWARGS_UNPACK : OP_CALL_KWARGS, (KWARGC<<16)|ARGC, line);
  434. }else{
  435. ctx->emit(need_unpack() ? OP_CALL_UNPACK : OP_CALL, ARGC, line);
  436. }
  437. }
  438. };
  439. struct BinaryExpr: Expr{
  440. TokenIndex op;
  441. Expr_ lhs;
  442. Expr_ rhs;
  443. Str str() const override { return TK_STR(op); }
  444. std::vector<const Expr*> children() const override {
  445. return {lhs.get(), rhs.get()};
  446. }
  447. void emit(CodeEmitContext* ctx) override {
  448. lhs->emit(ctx);
  449. rhs->emit(ctx);
  450. switch (op) {
  451. case TK("+"): ctx->emit(OP_BINARY_OP, 0, line); break;
  452. case TK("-"): ctx->emit(OP_BINARY_OP, 1, line); break;
  453. case TK("*"): ctx->emit(OP_BINARY_OP, 2, line); break;
  454. case TK("/"): ctx->emit(OP_BINARY_OP, 3, line); break;
  455. case TK("//"): ctx->emit(OP_BINARY_OP, 4, line); break;
  456. case TK("%"): ctx->emit(OP_BINARY_OP, 5, line); break;
  457. case TK("**"): ctx->emit(OP_BINARY_OP, 6, line); break;
  458. case TK("<"): ctx->emit(OP_COMPARE_OP, 0, line); break;
  459. case TK("<="): ctx->emit(OP_COMPARE_OP, 1, line); break;
  460. case TK("=="): ctx->emit(OP_COMPARE_OP, 2, line); break;
  461. case TK("!="): ctx->emit(OP_COMPARE_OP, 3, line); break;
  462. case TK(">"): ctx->emit(OP_COMPARE_OP, 4, line); break;
  463. case TK(">="): ctx->emit(OP_COMPARE_OP, 5, line); break;
  464. case TK("in"): ctx->emit(OP_CONTAINS_OP, 0, line); break;
  465. case TK("not in"): ctx->emit(OP_CONTAINS_OP, 1, line); break;
  466. case TK("is"): ctx->emit(OP_IS_OP, 0, line); break;
  467. case TK("is not"): ctx->emit(OP_IS_OP, 1, line); break;
  468. case TK("<<"): ctx->emit(OP_BITWISE_OP, 0, line); break;
  469. case TK(">>"): ctx->emit(OP_BITWISE_OP, 1, line); break;
  470. case TK("&"): ctx->emit(OP_BITWISE_OP, 2, line); break;
  471. case TK("|"): ctx->emit(OP_BITWISE_OP, 3, line); break;
  472. case TK("^"): ctx->emit(OP_BITWISE_OP, 4, line); break;
  473. default: UNREACHABLE();
  474. }
  475. }
  476. };
  477. // PASS
  478. struct TernaryExpr: Expr{
  479. Expr_ cond;
  480. Expr_ true_expr;
  481. Expr_ false_expr;
  482. Str str() const override {
  483. return "cond ? t : f";
  484. }
  485. std::vector<const Expr*> children() const override {
  486. return {cond.get(), true_expr.get(), false_expr.get()};
  487. }
  488. void emit(CodeEmitContext* ctx) override {
  489. cond->emit(ctx);
  490. int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, cond->line);
  491. true_expr->emit(ctx);
  492. int patch_2 = ctx->emit(OP_JUMP_ABSOLUTE, BC_NOARG, true_expr->line);
  493. ctx->patch_jump(patch);
  494. false_expr->emit(ctx);
  495. ctx->patch_jump(patch_2);
  496. }
  497. };
  498. } // namespace pkpy
  499. // struct TupleRef : BaseRef {
  500. // Tuple objs;
  501. // TupleRef(Tuple&& objs) : objs(std::move(objs)) {}
  502. // PyObject* get(VM* vm, Frame* frame) const{
  503. // Tuple args(objs.size());
  504. // for (int i = 0; i < objs.size(); i++) {
  505. // args[i] = vm->PyRef_AS_C(objs[i])->get(vm, frame);
  506. // }
  507. // return VAR(std::move(args));
  508. // }
  509. // void set(VM* vm, Frame* frame, PyObject* val) const{
  510. // val = vm->asIter(val);
  511. // BaseIter* iter = vm->PyIter_AS_C(val);
  512. // for(int i=0; i<objs.size(); i++){
  513. // PyObject* x;
  514. // if(is_type(objs[i], vm->tp_star_wrapper)){
  515. // auto& star = _CAST(StarWrapper&, objs[i]);
  516. // if(star.rvalue) vm->ValueError("can't use starred expression here");
  517. // if(i != objs.size()-1) vm->ValueError("* can only be used at the end");
  518. // auto ref = vm->PyRef_AS_C(star.obj);
  519. // List list;
  520. // while((x = iter->next()) != nullptr) list.push_back(x);
  521. // ref->set(vm, frame, VAR(std::move(list)));
  522. // return;
  523. // }else{
  524. // x = iter->next();
  525. // if(x == nullptr) vm->ValueError("not enough values to unpack");
  526. // vm->PyRef_AS_C(objs[i])->set(vm, frame, x);
  527. // }
  528. // }
  529. // PyObject* x = iter->next();
  530. // if(x != nullptr) vm->ValueError("too many values to unpack");
  531. // }
  532. // void del(VM* vm, Frame* frame) const{
  533. // for(int i=0; i<objs.size(); i++) vm->PyRef_AS_C(objs[i])->del(vm, frame);
  534. // }
  535. // };