compiler.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485
  1. #include "pocketpy/compiler/compiler.h"
  2. #include "pocketpy/compiler/lexer.h"
  3. #include "pocketpy/objects/codeobject.h"
  4. #include "pocketpy/objects/sourcedata.h"
  5. #include "pocketpy/objects/object.h"
  6. #include "pocketpy/common/strname.h"
  7. #include "pocketpy/common/config.h"
  8. #include "pocketpy/common/memorypool.h"
  9. #include <ctype.h>
  10. /* expr.h */
  11. typedef struct Expr Expr;
  12. typedef struct Ctx Ctx;
  13. typedef struct ExprVt {
  14. /* emit */
  15. void (*emit_)(Expr*, Ctx*);
  16. bool (*emit_del)(Expr*, Ctx*);
  17. bool (*emit_store)(Expr*, Ctx*);
  18. void (*emit_inplace)(Expr*, Ctx*);
  19. bool (*emit_istore)(Expr*, Ctx*);
  20. /* reflections */
  21. bool is_literal;
  22. bool is_json_object;
  23. bool is_name; // NameExpr
  24. bool is_tuple; // TupleExpr
  25. bool is_attrib; // AttribExpr
  26. bool is_subscr; // SubscrExpr
  27. bool is_starred; // StarredExpr
  28. bool is_binary; // BinaryExpr
  29. void (*dtor)(Expr*);
  30. } ExprVt;
  31. #define static_assert_expr_size(T) static_assert(sizeof(T) <= kPoolExprBlockSize, "")
  32. #define vtcall(f, self, ctx) ((self)->vt->f((self), (ctx)))
  33. #define vtemit_(self, ctx) vtcall(emit_, (self), (ctx))
  34. #define vtemit_del(self, ctx) ((self)->vt->emit_del ? vtcall(emit_del, self, ctx) : false)
  35. #define vtemit_store(self, ctx) ((self)->vt->emit_store ? vtcall(emit_store, self, ctx) : false)
  36. #define vtemit_inplace(self, ctx) \
  37. ((self)->vt->emit_inplace ? vtcall(emit_inplace, self, ctx) : vtemit_(self, ctx))
  38. #define vtemit_istore(self, ctx) \
  39. ((self)->vt->emit_istore ? vtcall(emit_istore, self, ctx) : vtemit_store(self, ctx))
  40. #define vtdelete(self) \
  41. do { \
  42. if(self) { \
  43. if((self)->vt->dtor) (self)->vt->dtor(self); \
  44. PoolExpr_dealloc(self); \
  45. } \
  46. } while(0)
  47. #define EXPR_COMMON_HEADER \
  48. const ExprVt* vt; \
  49. int line;
  50. typedef struct Expr {
  51. EXPR_COMMON_HEADER
  52. } Expr;
  53. /* context.h */
  54. typedef struct Ctx {
  55. CodeObject* co; // 1 CodeEmitContext <=> 1 CodeObject*
  56. FuncDecl* func; // optional, weakref
  57. int level;
  58. int curr_iblock;
  59. bool is_compiling_class;
  60. c11_vector /*T=Expr* */ s_expr;
  61. c11_smallmap_n2i global_names;
  62. c11_smallmap_s2n co_consts_string_dedup_map;
  63. } Ctx;
  64. typedef struct Expr Expr;
  65. static void Ctx__ctor(Ctx* self, CodeObject* co, FuncDecl* func, int level);
  66. static void Ctx__dtor(Ctx* self);
  67. static int Ctx__get_loop(Ctx* self);
  68. static CodeBlock* Ctx__enter_block(Ctx* self, CodeBlockType type);
  69. static void Ctx__exit_block(Ctx* self);
  70. static int Ctx__emit_(Ctx* self, Opcode opcode, uint16_t arg, int line);
  71. static int Ctx__emit_virtual(Ctx* self, Opcode opcode, uint16_t arg, int line, bool virtual);
  72. static void Ctx__revert_last_emit_(Ctx* self);
  73. static int Ctx__emit_int(Ctx* self, int64_t value, int line);
  74. static void Ctx__patch_jump(Ctx* self, int index);
  75. static bool Ctx__add_label(Ctx* self, py_Name name);
  76. static int Ctx__add_varname(Ctx* self, py_Name name);
  77. static int Ctx__add_const(Ctx* self, py_Ref);
  78. static int Ctx__add_const_string(Ctx* self, c11_sv);
  79. static void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line);
  80. static void Ctx__try_merge_for_iter_store(Ctx* self, int);
  81. static void Ctx__s_emit_top(Ctx*); // emit top -> pop -> delete
  82. static void Ctx__s_push(Ctx*, Expr*); // push
  83. static Expr* Ctx__s_top(Ctx*); // top
  84. static int Ctx__s_size(Ctx*); // size
  85. static void Ctx__s_pop(Ctx*); // pop -> delete
  86. static Expr* Ctx__s_popx(Ctx*); // pop move
  87. static void Ctx__s_emit_decorators(Ctx*, int count);
  88. /* expr.c */
  89. typedef struct NameExpr {
  90. EXPR_COMMON_HEADER
  91. py_Name name;
  92. NameScope scope;
  93. } NameExpr;
  94. void NameExpr__emit_(Expr* self_, Ctx* ctx) {
  95. NameExpr* self = (NameExpr*)self_;
  96. int index = c11_smallmap_n2i__get(&ctx->co->varnames_inv, self->name, -1);
  97. if(self->scope == NAME_LOCAL && index >= 0) {
  98. Ctx__emit_(ctx, OP_LOAD_FAST, index, self->line);
  99. } else {
  100. Opcode op = ctx->level <= 1 ? OP_LOAD_GLOBAL : OP_LOAD_NONLOCAL;
  101. if(ctx->is_compiling_class && self->scope == NAME_GLOBAL) {
  102. // if we are compiling a class, we should use OP_LOAD_ATTR_GLOBAL instead of
  103. // OP_LOAD_GLOBAL this supports @property.setter
  104. op = OP_LOAD_CLASS_GLOBAL;
  105. // exec()/eval() won't work with OP_LOAD_ATTR_GLOBAL in class body
  106. } else {
  107. // we cannot determine the scope when calling exec()/eval()
  108. if(self->scope == NAME_GLOBAL_UNKNOWN) op = OP_LOAD_NAME;
  109. }
  110. Ctx__emit_(ctx, op, self->name, self->line);
  111. }
  112. }
  113. bool NameExpr__emit_del(Expr* self_, Ctx* ctx) {
  114. NameExpr* self = (NameExpr*)self_;
  115. switch(self->scope) {
  116. case NAME_LOCAL:
  117. Ctx__emit_(ctx, OP_DELETE_FAST, Ctx__add_varname(ctx, self->name), self->line);
  118. break;
  119. case NAME_GLOBAL: Ctx__emit_(ctx, OP_DELETE_GLOBAL, self->name, self->line); break;
  120. case NAME_GLOBAL_UNKNOWN: Ctx__emit_(ctx, OP_DELETE_NAME, self->name, self->line); break;
  121. default: c11__unreachedable();
  122. }
  123. return true;
  124. }
  125. bool NameExpr__emit_store(Expr* self_, Ctx* ctx) {
  126. NameExpr* self = (NameExpr*)self_;
  127. if(ctx->is_compiling_class) {
  128. Ctx__emit_(ctx, OP_STORE_CLASS_ATTR, self->name, self->line);
  129. return true;
  130. }
  131. Ctx__emit_store_name(ctx, self->scope, self->name, self->line);
  132. return true;
  133. }
  134. NameExpr* NameExpr__new(int line, py_Name name, NameScope scope) {
  135. const static ExprVt Vt = {.emit_ = NameExpr__emit_,
  136. .emit_del = NameExpr__emit_del,
  137. .emit_store = NameExpr__emit_store,
  138. .is_name = true};
  139. static_assert_expr_size(NameExpr);
  140. NameExpr* self = PoolExpr_alloc();
  141. self->vt = &Vt;
  142. self->line = line;
  143. self->name = name;
  144. self->scope = scope;
  145. return self;
  146. }
  147. typedef struct StarredExpr {
  148. EXPR_COMMON_HEADER
  149. Expr* child;
  150. int level;
  151. } StarredExpr;
  152. void StarredExpr__emit_(Expr* self_, Ctx* ctx) {
  153. StarredExpr* self = (StarredExpr*)self_;
  154. vtemit_(self->child, ctx);
  155. Ctx__emit_(ctx, OP_UNARY_STAR, self->level, self->line);
  156. }
  157. bool StarredExpr__emit_store(Expr* self_, Ctx* ctx) {
  158. StarredExpr* self = (StarredExpr*)self_;
  159. if(self->level != 1) return false;
  160. // simply proxy to child
  161. return vtemit_store(self->child, ctx);
  162. }
  163. StarredExpr* StarredExpr__new(int line, Expr* child, int level) {
  164. const static ExprVt Vt = {.emit_ = StarredExpr__emit_,
  165. .emit_store = StarredExpr__emit_store,
  166. .is_starred = true};
  167. static_assert_expr_size(StarredExpr);
  168. StarredExpr* self = PoolExpr_alloc();
  169. self->vt = &Vt;
  170. self->line = line;
  171. self->child = child;
  172. self->level = level;
  173. return self;
  174. }
  175. // InvertExpr, NotExpr, NegatedExpr
  176. // NOTE: NegatedExpr always contains a non-const child. Should not generate -1 or -0.1
  177. typedef struct UnaryExpr {
  178. EXPR_COMMON_HEADER
  179. Expr* child;
  180. Opcode opcode;
  181. } UnaryExpr;
  182. void UnaryExpr__dtor(Expr* self_) {
  183. UnaryExpr* self = (UnaryExpr*)self_;
  184. vtdelete(self->child);
  185. }
  186. static void UnaryExpr__emit_(Expr* self_, Ctx* ctx) {
  187. UnaryExpr* self = (UnaryExpr*)self_;
  188. vtemit_(self->child, ctx);
  189. Ctx__emit_(ctx, self->opcode, BC_NOARG, self->line);
  190. }
  191. UnaryExpr* UnaryExpr__new(int line, Expr* child, Opcode opcode) {
  192. const static ExprVt Vt = {.emit_ = UnaryExpr__emit_, .dtor = UnaryExpr__dtor};
  193. static_assert_expr_size(UnaryExpr);
  194. UnaryExpr* self = PoolExpr_alloc();
  195. self->vt = &Vt;
  196. self->line = line;
  197. self->child = child;
  198. self->opcode = opcode;
  199. return self;
  200. }
  201. typedef struct RawStringExpr {
  202. EXPR_COMMON_HEADER
  203. c11_sv value;
  204. Opcode opcode;
  205. } RawStringExpr;
  206. void RawStringExpr__emit_(Expr* self_, Ctx* ctx) {
  207. RawStringExpr* self = (RawStringExpr*)self_;
  208. int index = Ctx__add_const_string(ctx, self->value);
  209. Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line);
  210. Ctx__emit_(ctx, self->opcode, BC_NOARG, self->line);
  211. }
  212. RawStringExpr* RawStringExpr__new(int line, c11_sv value, Opcode opcode) {
  213. const static ExprVt Vt = {.emit_ = RawStringExpr__emit_};
  214. static_assert_expr_size(RawStringExpr);
  215. RawStringExpr* self = PoolExpr_alloc();
  216. self->vt = &Vt;
  217. self->line = line;
  218. self->value = value;
  219. self->opcode = opcode;
  220. return self;
  221. }
  222. typedef struct ImagExpr {
  223. EXPR_COMMON_HEADER
  224. double value;
  225. } ImagExpr;
  226. void ImagExpr__emit_(Expr* self_, Ctx* ctx) {
  227. ImagExpr* self = (ImagExpr*)self_;
  228. py_TValue value;
  229. py_newfloat(&value, self->value);
  230. int index = Ctx__add_const(ctx, &value);
  231. Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line);
  232. Ctx__emit_(ctx, OP_BUILD_IMAG, BC_NOARG, self->line);
  233. }
  234. ImagExpr* ImagExpr__new(int line, double value) {
  235. const static ExprVt Vt = {.emit_ = ImagExpr__emit_};
  236. static_assert_expr_size(ImagExpr);
  237. ImagExpr* self = PoolExpr_alloc();
  238. self->vt = &Vt;
  239. self->line = line;
  240. self->value = value;
  241. return self;
  242. }
  243. typedef struct LiteralExpr {
  244. EXPR_COMMON_HEADER
  245. const TokenValue* value;
  246. bool negated;
  247. } LiteralExpr;
  248. void LiteralExpr__emit_(Expr* self_, Ctx* ctx) {
  249. LiteralExpr* self = (LiteralExpr*)self_;
  250. switch(self->value->index) {
  251. case TokenValue_I64: {
  252. py_i64 val = self->value->_i64;
  253. if(self->negated) val = -val;
  254. Ctx__emit_int(ctx, val, self->line);
  255. break;
  256. }
  257. case TokenValue_F64: {
  258. py_TValue value;
  259. py_f64 val = self->value->_f64;
  260. if(self->negated) val = -val;
  261. py_newfloat(&value, val);
  262. int index = Ctx__add_const(ctx, &value);
  263. Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line);
  264. break;
  265. }
  266. case TokenValue_STR: {
  267. assert(!self->negated);
  268. c11_sv sv = c11_string__sv(self->value->_str);
  269. int index = Ctx__add_const_string(ctx, sv);
  270. Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line);
  271. break;
  272. }
  273. default: c11__unreachedable();
  274. }
  275. }
  276. LiteralExpr* LiteralExpr__new(int line, const TokenValue* value) {
  277. const static ExprVt Vt = {.emit_ = LiteralExpr__emit_,
  278. .is_literal = true,
  279. .is_json_object = true};
  280. static_assert_expr_size(LiteralExpr);
  281. LiteralExpr* self = PoolExpr_alloc();
  282. self->vt = &Vt;
  283. self->line = line;
  284. self->value = value;
  285. self->negated = false;
  286. return self;
  287. }
  288. typedef struct Literal0Expr {
  289. EXPR_COMMON_HEADER
  290. TokenIndex token;
  291. } Literal0Expr;
  292. void Literal0Expr__emit_(Expr* self_, Ctx* ctx) {
  293. Literal0Expr* self = (Literal0Expr*)self_;
  294. Opcode opcode;
  295. switch(self->token) {
  296. case TK_NONE: opcode = OP_LOAD_NONE; break;
  297. case TK_TRUE: opcode = OP_LOAD_TRUE; break;
  298. case TK_FALSE: opcode = OP_LOAD_FALSE; break;
  299. case TK_DOTDOTDOT: opcode = OP_LOAD_ELLIPSIS; break;
  300. default: assert(false);
  301. }
  302. Ctx__emit_(ctx, opcode, BC_NOARG, self->line);
  303. }
  304. Literal0Expr* Literal0Expr__new(int line, TokenIndex token) {
  305. const static ExprVt Vt = {.emit_ = Literal0Expr__emit_, .is_json_object = true};
  306. static_assert_expr_size(Literal0Expr);
  307. Literal0Expr* self = PoolExpr_alloc();
  308. self->vt = &Vt;
  309. self->line = line;
  310. self->token = token;
  311. return self;
  312. }
  313. typedef struct SliceExpr {
  314. EXPR_COMMON_HEADER
  315. Expr* start;
  316. Expr* stop;
  317. Expr* step;
  318. } SliceExpr;
  319. void SliceExpr__dtor(Expr* self_) {
  320. SliceExpr* self = (SliceExpr*)self_;
  321. vtdelete(self->start);
  322. vtdelete(self->stop);
  323. vtdelete(self->step);
  324. }
  325. void SliceExpr__emit_(Expr* self_, Ctx* ctx) {
  326. SliceExpr* self = (SliceExpr*)self_;
  327. if(self->start)
  328. vtemit_(self->start, ctx);
  329. else
  330. Ctx__emit_(ctx, OP_LOAD_NONE, BC_NOARG, self->line);
  331. if(self->stop)
  332. vtemit_(self->stop, ctx);
  333. else
  334. Ctx__emit_(ctx, OP_LOAD_NONE, BC_NOARG, self->line);
  335. if(self->step)
  336. vtemit_(self->step, ctx);
  337. else
  338. Ctx__emit_(ctx, OP_LOAD_NONE, BC_NOARG, self->line);
  339. Ctx__emit_(ctx, OP_BUILD_SLICE, BC_NOARG, self->line);
  340. }
  341. SliceExpr* SliceExpr__new(int line) {
  342. const static ExprVt Vt = {.dtor = SliceExpr__dtor, .emit_ = SliceExpr__emit_};
  343. static_assert_expr_size(SliceExpr);
  344. SliceExpr* self = PoolExpr_alloc();
  345. self->vt = &Vt;
  346. self->line = line;
  347. self->start = NULL;
  348. self->stop = NULL;
  349. self->step = NULL;
  350. return self;
  351. }
  352. // ListExpr, DictExpr, SetExpr, TupleExpr
  353. typedef struct SequenceExpr {
  354. EXPR_COMMON_HEADER
  355. c11_array /*T=Expr* */ items;
  356. Opcode opcode;
  357. } SequenceExpr;
  358. static void SequenceExpr__emit_(Expr* self_, Ctx* ctx) {
  359. SequenceExpr* self = (SequenceExpr*)self_;
  360. for(int i = 0; i < self->items.count; i++) {
  361. Expr* item = c11__getitem(Expr*, &self->items, i);
  362. vtemit_(item, ctx);
  363. }
  364. Ctx__emit_(ctx, self->opcode, self->items.count, self->line);
  365. }
  366. void SequenceExpr__dtor(Expr* self_) {
  367. SequenceExpr* self = (SequenceExpr*)self_;
  368. c11__foreach(Expr*, &self->items, e) vtdelete(*e);
  369. c11_array__dtor(&self->items);
  370. }
  371. bool TupleExpr__emit_store(Expr* self_, Ctx* ctx) {
  372. SequenceExpr* self = (SequenceExpr*)self_;
  373. // TOS is an iterable
  374. // items may contain StarredExpr, we should check it
  375. int starred_i = -1;
  376. for(int i = 0; i < self->items.count; i++) {
  377. Expr* e = c11__getitem(Expr*, &self->items, i);
  378. if(e->vt->is_starred) {
  379. if(((StarredExpr*)e)->level > 0) {
  380. if(starred_i == -1)
  381. starred_i = i;
  382. else
  383. return false; // multiple StarredExpr not allowed
  384. }
  385. }
  386. }
  387. if(starred_i == -1) {
  388. Bytecode* prev = c11__at(Bytecode, &ctx->co->codes, ctx->co->codes.count - 1);
  389. if(prev->op == OP_BUILD_TUPLE && prev->arg == self->items.count) {
  390. // build tuple and unpack it is meaningless
  391. Ctx__revert_last_emit_(ctx);
  392. } else {
  393. if(prev->op == OP_FOR_ITER) {
  394. prev->op = OP_FOR_ITER_UNPACK;
  395. prev->arg = self->items.count;
  396. } else {
  397. Ctx__emit_(ctx, OP_UNPACK_SEQUENCE, self->items.count, self->line);
  398. }
  399. }
  400. } else {
  401. // starred assignment target must be in a tuple
  402. if(self->items.count == 1) return false;
  403. // starred assignment target must be the last one (differ from cpython)
  404. if(starred_i != self->items.count - 1) return false;
  405. // a,*b = [1,2,3]
  406. // stack is [1,2,3] -> [1,[2,3]]
  407. Ctx__emit_(ctx, OP_UNPACK_EX, self->items.count - 1, self->line);
  408. }
  409. // do reverse emit
  410. for(int i = self->items.count - 1; i >= 0; i--) {
  411. Expr* e = c11__getitem(Expr*, &self->items, i);
  412. bool ok = vtemit_store(e, ctx);
  413. if(!ok) return false;
  414. }
  415. return true;
  416. }
  417. bool TupleExpr__emit_del(Expr* self_, Ctx* ctx) {
  418. SequenceExpr* self = (SequenceExpr*)self_;
  419. c11__foreach(Expr*, &self->items, e) {
  420. bool ok = vtemit_del(*e, ctx);
  421. if(!ok) return false;
  422. }
  423. return true;
  424. }
  425. static SequenceExpr* SequenceExpr__new(int line, const ExprVt* vt, int count, Opcode opcode) {
  426. static_assert_expr_size(SequenceExpr);
  427. SequenceExpr* self = PoolExpr_alloc();
  428. self->vt = vt;
  429. self->line = line;
  430. self->opcode = opcode;
  431. c11_array__ctor(&self->items, sizeof(Expr*), count);
  432. return self;
  433. }
  434. SequenceExpr* ListExpr__new(int line, int count) {
  435. const static ExprVt ListExprVt = {.dtor = SequenceExpr__dtor,
  436. .emit_ = SequenceExpr__emit_,
  437. .is_json_object = true};
  438. return SequenceExpr__new(line, &ListExprVt, count, OP_BUILD_LIST);
  439. }
  440. SequenceExpr* DictExpr__new(int line, int count) {
  441. const static ExprVt DictExprVt = {.dtor = SequenceExpr__dtor,
  442. .emit_ = SequenceExpr__emit_,
  443. .is_json_object = true};
  444. return SequenceExpr__new(line, &DictExprVt, count, OP_BUILD_DICT);
  445. }
  446. SequenceExpr* SetExpr__new(int line, int count) {
  447. const static ExprVt SetExprVt = {
  448. .dtor = SequenceExpr__dtor,
  449. .emit_ = SequenceExpr__emit_,
  450. };
  451. return SequenceExpr__new(line, &SetExprVt, count, OP_BUILD_SET);
  452. }
  453. SequenceExpr* TupleExpr__new(int line, int count) {
  454. const static ExprVt TupleExprVt = {.dtor = SequenceExpr__dtor,
  455. .emit_ = SequenceExpr__emit_,
  456. .is_tuple = true,
  457. .emit_store = TupleExpr__emit_store,
  458. .emit_del = TupleExpr__emit_del};
  459. return SequenceExpr__new(line, &TupleExprVt, count, OP_BUILD_TUPLE);
  460. }
  461. typedef struct CompExpr {
  462. EXPR_COMMON_HEADER
  463. Expr* expr; // loop expr
  464. Expr* vars; // loop vars
  465. Expr* iter; // loop iter
  466. Expr* cond; // optional if condition
  467. Opcode op0;
  468. Opcode op1;
  469. } CompExpr;
  470. void CompExpr__dtor(Expr* self_) {
  471. CompExpr* self = (CompExpr*)self_;
  472. vtdelete(self->expr);
  473. vtdelete(self->vars);
  474. vtdelete(self->iter);
  475. vtdelete(self->cond);
  476. }
  477. void CompExpr__emit_(Expr* self_, Ctx* ctx) {
  478. CompExpr* self = (CompExpr*)self_;
  479. Ctx__emit_(ctx, self->op0, 0, self->line);
  480. vtemit_(self->iter, ctx);
  481. Ctx__emit_(ctx, OP_GET_ITER, BC_NOARG, BC_KEEPLINE);
  482. Ctx__enter_block(ctx, CodeBlockType_FOR_LOOP);
  483. int curr_iblock = ctx->curr_iblock;
  484. int for_codei = Ctx__emit_(ctx, OP_FOR_ITER, curr_iblock, BC_KEEPLINE);
  485. bool ok = vtemit_store(self->vars, ctx);
  486. // this error occurs in `vars` instead of this line, but...nevermind
  487. assert(ok); // this should raise a SyntaxError, but we just assert it
  488. Ctx__try_merge_for_iter_store(ctx, for_codei);
  489. if(self->cond) {
  490. vtemit_(self->cond, ctx);
  491. int patch = Ctx__emit_(ctx, OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
  492. vtemit_(self->expr, ctx);
  493. Ctx__emit_(ctx, self->op1, BC_NOARG, BC_KEEPLINE);
  494. Ctx__patch_jump(ctx, patch);
  495. } else {
  496. vtemit_(self->expr, ctx);
  497. Ctx__emit_(ctx, self->op1, BC_NOARG, BC_KEEPLINE);
  498. }
  499. Ctx__emit_(ctx, OP_LOOP_CONTINUE, curr_iblock, BC_KEEPLINE);
  500. Ctx__exit_block(ctx);
  501. }
  502. CompExpr* CompExpr__new(int line, Opcode op0, Opcode op1) {
  503. const static ExprVt Vt = {.dtor = CompExpr__dtor, .emit_ = CompExpr__emit_};
  504. static_assert_expr_size(CompExpr);
  505. CompExpr* self = PoolExpr_alloc();
  506. self->vt = &Vt;
  507. self->line = line;
  508. self->op0 = op0;
  509. self->op1 = op1;
  510. self->expr = NULL;
  511. self->vars = NULL;
  512. self->iter = NULL;
  513. self->cond = NULL;
  514. return self;
  515. }
  516. typedef struct LambdaExpr {
  517. EXPR_COMMON_HEADER
  518. int index;
  519. } LambdaExpr;
  520. static void LambdaExpr__emit_(Expr* self_, Ctx* ctx) {
  521. LambdaExpr* self = (LambdaExpr*)self_;
  522. Ctx__emit_(ctx, OP_LOAD_FUNCTION, self->index, self->line);
  523. }
  524. LambdaExpr* LambdaExpr__new(int line, int index) {
  525. const static ExprVt Vt = {.emit_ = LambdaExpr__emit_};
  526. static_assert_expr_size(LambdaExpr);
  527. LambdaExpr* self = PoolExpr_alloc();
  528. self->vt = &Vt;
  529. self->line = line;
  530. self->index = index;
  531. return self;
  532. }
  533. typedef struct FStringExpr {
  534. EXPR_COMMON_HEADER
  535. c11_sv src;
  536. } FStringExpr;
  537. static bool is_fmt_valid_char(char c) {
  538. switch(c) {
  539. // clang-format off
  540. case '-': case '=': case '*': case '#': case '@': case '!': case '~':
  541. case '<': case '>': case '^':
  542. case '.': case 'f': case 'd': case 's':
  543. case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  544. return true;
  545. default: return false;
  546. // clang-format on
  547. }
  548. }
  549. static bool is_identifier(c11_sv s) {
  550. if(s.size == 0) return false;
  551. if(!isalpha(s.data[0]) && s.data[0] != '_') return false;
  552. for(int i = 0; i < s.size; i++) {
  553. char c = s.data[i];
  554. if(!isalnum(c) && c != '_') return false;
  555. }
  556. return true;
  557. }
  558. static void _load_simple_expr(Ctx* ctx, c11_sv expr, int line) {
  559. bool repr = false;
  560. const char* expr_end = expr.data + expr.size;
  561. if(expr.size >= 2 && expr_end[-2] == '!') {
  562. switch(expr_end[-1]) {
  563. case 'r':
  564. repr = true;
  565. expr.size -= 2; // expr[:-2]
  566. break;
  567. case 's':
  568. repr = false;
  569. expr.size -= 2; // expr[:-2]
  570. break;
  571. default: break; // nothing happens
  572. }
  573. }
  574. // name or name.name
  575. bool is_fastpath = false;
  576. if(is_identifier(expr)) {
  577. // ctx->emit_(OP_LOAD_NAME, py_Name(expr.sv()).index, line);
  578. Ctx__emit_(ctx, OP_LOAD_NAME, py_name2(expr), line);
  579. is_fastpath = true;
  580. } else {
  581. int dot = c11_sv__index(expr, '.');
  582. if(dot > 0) {
  583. c11_sv a = {expr.data, dot}; // expr[:dot]
  584. c11_sv b = {expr.data + (dot + 1), expr.size - (dot + 1)}; // expr[dot+1:]
  585. if(is_identifier(a) && is_identifier(b)) {
  586. Ctx__emit_(ctx, OP_LOAD_NAME, py_name2(a), line);
  587. Ctx__emit_(ctx, OP_LOAD_ATTR, py_name2(b), line);
  588. is_fastpath = true;
  589. }
  590. }
  591. }
  592. if(!is_fastpath) {
  593. int index = Ctx__add_const_string(ctx, expr);
  594. Ctx__emit_(ctx, OP_FSTRING_EVAL, index, line);
  595. }
  596. if(repr) { Ctx__emit_(ctx, OP_REPR, BC_NOARG, line); }
  597. }
  598. static void FStringExpr__emit_(Expr* self_, Ctx* ctx) {
  599. FStringExpr* self = (FStringExpr*)self_;
  600. int i = 0; // left index
  601. int j = 0; // right index
  602. int count = 0; // how many string parts
  603. bool flag = false; // true if we are in a expression
  604. const char* src = self->src.data;
  605. while(j < self->src.size) {
  606. if(flag) {
  607. if(src[j] == '}') {
  608. // add expression
  609. c11_sv expr = {src + i, j - i}; // src[i:j]
  610. // BUG: ':' is not a format specifier in f"{stack[2:]}"
  611. int conon = c11_sv__index(expr, ':');
  612. if(conon >= 0) {
  613. c11_sv spec = {expr.data + (conon + 1),
  614. expr.size - (conon + 1)}; // expr[conon+1:]
  615. // filter some invalid spec
  616. bool ok = true;
  617. for(int k = 0; k < spec.size; k++) {
  618. char c = spec.data[k];
  619. if(!is_fmt_valid_char(c)) {
  620. ok = false;
  621. break;
  622. }
  623. }
  624. if(ok) {
  625. expr.size = conon; // expr[:conon]
  626. _load_simple_expr(ctx, expr, self->line);
  627. // ctx->emit_(OP_FORMAT_STRING, ctx->add_const_string(spec.sv()), line);
  628. Ctx__emit_(ctx,
  629. OP_FORMAT_STRING,
  630. Ctx__add_const_string(ctx, spec),
  631. self->line);
  632. } else {
  633. // ':' is not a spec indicator
  634. _load_simple_expr(ctx, expr, self->line);
  635. }
  636. } else {
  637. _load_simple_expr(ctx, expr, self->line);
  638. }
  639. flag = false;
  640. count++;
  641. }
  642. } else {
  643. if(src[j] == '{') {
  644. // look at next char
  645. if(j + 1 < self->src.size && src[j + 1] == '{') {
  646. // {{ -> {
  647. j++;
  648. Ctx__emit_(ctx,
  649. OP_LOAD_CONST,
  650. Ctx__add_const_string(ctx, (c11_sv){"{", 1}),
  651. self->line);
  652. count++;
  653. } else {
  654. // { -> }
  655. flag = true;
  656. i = j + 1;
  657. }
  658. } else if(src[j] == '}') {
  659. // look at next char
  660. if(j + 1 < self->src.size && src[j + 1] == '}') {
  661. // }} -> }
  662. j++;
  663. Ctx__emit_(ctx,
  664. OP_LOAD_CONST,
  665. Ctx__add_const_string(ctx, (c11_sv){"}", 1}),
  666. self->line);
  667. count++;
  668. } else {
  669. // } -> error
  670. // throw std::runtime_error("f-string: unexpected }");
  671. // just ignore
  672. }
  673. } else {
  674. // literal
  675. i = j;
  676. while(j < self->src.size && src[j] != '{' && src[j] != '}')
  677. j++;
  678. c11_sv literal = {src + i, j - i}; // src[i:j]
  679. Ctx__emit_(ctx, OP_LOAD_CONST, Ctx__add_const_string(ctx, literal), self->line);
  680. count++;
  681. continue; // skip j++
  682. }
  683. }
  684. j++;
  685. }
  686. if(flag) {
  687. // literal
  688. c11_sv literal = {src + i, self->src.size - i}; // src[i:]
  689. Ctx__emit_(ctx, OP_LOAD_CONST, Ctx__add_const_string(ctx, literal), self->line);
  690. count++;
  691. }
  692. Ctx__emit_(ctx, OP_BUILD_STRING, count, self->line);
  693. }
  694. FStringExpr* FStringExpr__new(int line, c11_sv src) {
  695. const static ExprVt Vt = {.emit_ = FStringExpr__emit_};
  696. static_assert_expr_size(FStringExpr);
  697. FStringExpr* self = PoolExpr_alloc();
  698. self->vt = &Vt;
  699. self->line = line;
  700. self->src = src;
  701. return self;
  702. }
  703. // AndExpr, OrExpr
  704. typedef struct LogicBinaryExpr {
  705. EXPR_COMMON_HEADER
  706. Expr* lhs;
  707. Expr* rhs;
  708. Opcode opcode;
  709. } LogicBinaryExpr;
  710. void LogicBinaryExpr__dtor(Expr* self_) {
  711. LogicBinaryExpr* self = (LogicBinaryExpr*)self_;
  712. vtdelete(self->lhs);
  713. vtdelete(self->rhs);
  714. }
  715. void LogicBinaryExpr__emit_(Expr* self_, Ctx* ctx) {
  716. LogicBinaryExpr* self = (LogicBinaryExpr*)self_;
  717. vtemit_(self->lhs, ctx);
  718. int patch = Ctx__emit_(ctx, self->opcode, BC_NOARG, self->line);
  719. vtemit_(self->rhs, ctx);
  720. Ctx__patch_jump(ctx, patch);
  721. }
  722. LogicBinaryExpr* LogicBinaryExpr__new(int line, Opcode opcode) {
  723. const static ExprVt Vt = {.emit_ = LogicBinaryExpr__emit_, .dtor = LogicBinaryExpr__dtor};
  724. static_assert_expr_size(LogicBinaryExpr);
  725. LogicBinaryExpr* self = PoolExpr_alloc();
  726. self->vt = &Vt;
  727. self->line = line;
  728. self->lhs = NULL;
  729. self->rhs = NULL;
  730. self->opcode = opcode;
  731. return self;
  732. }
  733. typedef struct GroupedExpr {
  734. EXPR_COMMON_HEADER
  735. Expr* child;
  736. } GroupedExpr;
  737. void GroupedExpr__dtor(Expr* self_) {
  738. GroupedExpr* self = (GroupedExpr*)self_;
  739. vtdelete(self->child);
  740. }
  741. void GroupedExpr__emit_(Expr* self_, Ctx* ctx) {
  742. GroupedExpr* self = (GroupedExpr*)self_;
  743. vtemit_(self->child, ctx);
  744. }
  745. bool GroupedExpr__emit_del(Expr* self_, Ctx* ctx) {
  746. GroupedExpr* self = (GroupedExpr*)self_;
  747. return vtemit_del(self->child, ctx);
  748. }
  749. bool GroupedExpr__emit_store(Expr* self_, Ctx* ctx) {
  750. GroupedExpr* self = (GroupedExpr*)self_;
  751. return vtemit_store(self->child, ctx);
  752. }
  753. GroupedExpr* GroupedExpr__new(int line, Expr* child) {
  754. const static ExprVt Vt = {.dtor = GroupedExpr__dtor,
  755. .emit_ = GroupedExpr__emit_,
  756. .emit_del = GroupedExpr__emit_del,
  757. .emit_store = GroupedExpr__emit_store};
  758. static_assert_expr_size(GroupedExpr);
  759. GroupedExpr* self = PoolExpr_alloc();
  760. self->vt = &Vt;
  761. self->line = line;
  762. self->child = child;
  763. return self;
  764. }
  765. typedef struct BinaryExpr {
  766. EXPR_COMMON_HEADER
  767. Expr* lhs;
  768. Expr* rhs;
  769. TokenIndex op;
  770. bool inplace;
  771. } BinaryExpr;
  772. static void BinaryExpr__dtor(Expr* self_) {
  773. BinaryExpr* self = (BinaryExpr*)self_;
  774. vtdelete(self->lhs);
  775. vtdelete(self->rhs);
  776. }
  777. static py_Name cmp_token2name(TokenIndex token) {
  778. switch(token) {
  779. case TK_LT: return __lt__;
  780. case TK_LE: return __le__;
  781. case TK_EQ: return __eq__;
  782. case TK_NE: return __ne__;
  783. case TK_GT: return __gt__;
  784. case TK_GE: return __ge__;
  785. default: return 0;
  786. }
  787. }
  788. #define is_compare_expr(e) ((e)->vt->is_binary && cmp_token2name(((BinaryExpr*)(e))->op))
  789. static void _emit_compare(BinaryExpr* self, Ctx* ctx, c11_vector* jmps) {
  790. if(is_compare_expr(self->lhs)) {
  791. _emit_compare((BinaryExpr*)self->lhs, ctx, jmps);
  792. } else {
  793. vtemit_(self->lhs, ctx); // [a]
  794. }
  795. vtemit_(self->rhs, ctx); // [a, b]
  796. Ctx__emit_(ctx, OP_DUP_TOP, BC_NOARG, self->line); // [a, b, b]
  797. Ctx__emit_(ctx, OP_ROT_THREE, BC_NOARG, self->line); // [b, a, b]
  798. Ctx__emit_(ctx, OP_BINARY_OP, cmp_token2name(self->op), self->line);
  799. // [b, RES]
  800. int index = Ctx__emit_(ctx, OP_JUMP_IF_FALSE_OR_POP, BC_NOARG, self->line);
  801. c11_vector__push(int, jmps, index);
  802. }
  803. static void BinaryExpr__emit_(Expr* self_, Ctx* ctx) {
  804. BinaryExpr* self = (BinaryExpr*)self_;
  805. c11_vector /*T=int*/ jmps;
  806. c11_vector__ctor(&jmps, sizeof(int));
  807. if(cmp_token2name(self->op) && is_compare_expr(self->lhs)) {
  808. // (a < b) < c
  809. BinaryExpr* e = (BinaryExpr*)self->lhs;
  810. _emit_compare(e, ctx, &jmps);
  811. // [b, RES]
  812. } else {
  813. // (1 + 2) < c
  814. if(self->inplace) {
  815. vtemit_inplace(self->lhs, ctx);
  816. } else {
  817. vtemit_(self->lhs, ctx);
  818. }
  819. }
  820. vtemit_(self->rhs, ctx);
  821. Opcode opcode = OP_BINARY_OP;
  822. uint16_t arg = BC_NOARG;
  823. switch(self->op) {
  824. case TK_ADD: arg = __add__ | (__radd__ << 8); break;
  825. case TK_SUB: arg = __sub__ | (__rsub__ << 8); break;
  826. case TK_MUL: arg = __mul__ | (__rmul__ << 8); break;
  827. case TK_DIV: arg = __truediv__ | (__rtruediv__ << 8); break;
  828. case TK_FLOORDIV: arg = __floordiv__ | (__rfloordiv__ << 8); break;
  829. case TK_MOD: arg = __mod__ | (__rmod__ << 8); break;
  830. case TK_POW: arg = __pow__ | (__rpow__ << 8); break;
  831. case TK_LT: arg = __lt__ | (__gt__ << 8); break;
  832. case TK_LE: arg = __le__ | (__ge__ << 8); break;
  833. case TK_EQ: arg = __eq__ | (__eq__ << 8); break;
  834. case TK_NE: arg = __ne__ | (__ne__ << 8); break;
  835. case TK_GT: arg = __gt__ | (__lt__ << 8); break;
  836. case TK_GE: arg = __ge__ | (__le__ << 8); break;
  837. case TK_IN:
  838. opcode = OP_CONTAINS_OP;
  839. arg = 0;
  840. break;
  841. case TK_NOT_IN:
  842. opcode = OP_CONTAINS_OP;
  843. arg = 1;
  844. break;
  845. case TK_IS:
  846. opcode = OP_IS_OP;
  847. arg = 0;
  848. break;
  849. case TK_IS_NOT:
  850. opcode = OP_IS_OP;
  851. arg = 1;
  852. break;
  853. case TK_LSHIFT: arg = __lshift__; break;
  854. case TK_RSHIFT: arg = __rshift__; break;
  855. case TK_AND: arg = __and__; break;
  856. case TK_OR: arg = __or__; break;
  857. case TK_XOR: arg = __xor__; break;
  858. case TK_DECORATOR: arg = __matmul__; break;
  859. default: assert(false);
  860. }
  861. Ctx__emit_(ctx, opcode, arg, self->line);
  862. for(int i = 0; i < jmps.count; i++) {
  863. Ctx__patch_jump(ctx, c11__getitem(int, &jmps, i));
  864. }
  865. }
  866. BinaryExpr* BinaryExpr__new(int line, TokenIndex op, bool inplace) {
  867. const static ExprVt Vt = {.emit_ = BinaryExpr__emit_, .dtor = BinaryExpr__dtor};
  868. static_assert_expr_size(BinaryExpr);
  869. BinaryExpr* self = PoolExpr_alloc();
  870. self->vt = &Vt;
  871. self->line = line;
  872. self->lhs = NULL;
  873. self->rhs = NULL;
  874. self->op = op;
  875. self->inplace = inplace;
  876. return self;
  877. }
  878. typedef struct TernaryExpr {
  879. EXPR_COMMON_HEADER
  880. Expr* cond;
  881. Expr* true_expr;
  882. Expr* false_expr;
  883. } TernaryExpr;
  884. void TernaryExpr__dtor(Expr* self_) {
  885. TernaryExpr* self = (TernaryExpr*)self_;
  886. vtdelete(self->cond);
  887. vtdelete(self->true_expr);
  888. vtdelete(self->false_expr);
  889. }
  890. void TernaryExpr__emit_(Expr* self_, Ctx* ctx) {
  891. TernaryExpr* self = (TernaryExpr*)self_;
  892. vtemit_(self->cond, ctx);
  893. int patch = Ctx__emit_(ctx, OP_POP_JUMP_IF_FALSE, BC_NOARG, self->cond->line);
  894. vtemit_(self->true_expr, ctx);
  895. int patch_2 = Ctx__emit_(ctx, OP_JUMP_FORWARD, BC_NOARG, self->true_expr->line);
  896. Ctx__patch_jump(ctx, patch);
  897. vtemit_(self->false_expr, ctx);
  898. Ctx__patch_jump(ctx, patch_2);
  899. }
  900. TernaryExpr* TernaryExpr__new(int line) {
  901. const static ExprVt Vt = {.dtor = TernaryExpr__dtor, .emit_ = TernaryExpr__emit_};
  902. static_assert_expr_size(TernaryExpr);
  903. TernaryExpr* self = PoolExpr_alloc();
  904. self->vt = &Vt;
  905. self->line = line;
  906. self->cond = NULL;
  907. self->true_expr = NULL;
  908. self->false_expr = NULL;
  909. return self;
  910. }
  911. typedef struct SubscrExpr {
  912. EXPR_COMMON_HEADER
  913. Expr* lhs;
  914. Expr* rhs;
  915. } SubscrExpr;
  916. void SubscrExpr__dtor(Expr* self_) {
  917. SubscrExpr* self = (SubscrExpr*)self_;
  918. vtdelete(self->lhs);
  919. vtdelete(self->rhs);
  920. }
  921. void SubscrExpr__emit_(Expr* self_, Ctx* ctx) {
  922. SubscrExpr* self = (SubscrExpr*)self_;
  923. vtemit_(self->lhs, ctx);
  924. vtemit_(self->rhs, ctx);
  925. Ctx__emit_(ctx, OP_LOAD_SUBSCR, BC_NOARG, self->line);
  926. }
  927. bool SubscrExpr__emit_store(Expr* self_, Ctx* ctx) {
  928. SubscrExpr* self = (SubscrExpr*)self_;
  929. vtemit_(self->lhs, ctx);
  930. vtemit_(self->rhs, ctx);
  931. Ctx__emit_(ctx, OP_STORE_SUBSCR, BC_NOARG, self->line);
  932. return true;
  933. }
  934. void SubscrExpr__emit_inplace(Expr* self_, Ctx* ctx) {
  935. SubscrExpr* self = (SubscrExpr*)self_;
  936. vtemit_(self->lhs, ctx);
  937. vtemit_(self->rhs, ctx);
  938. Ctx__emit_(ctx, OP_DUP_TOP_TWO, BC_NOARG, self->line);
  939. Ctx__emit_(ctx, OP_LOAD_SUBSCR, BC_NOARG, self->line);
  940. }
  941. bool SubscrExpr__emit_istore(Expr* self_, Ctx* ctx) {
  942. SubscrExpr* self = (SubscrExpr*)self_;
  943. // [a, b, val] -> [val, a, b]
  944. Ctx__emit_(ctx, OP_ROT_THREE, BC_NOARG, self->line);
  945. Ctx__emit_(ctx, OP_STORE_SUBSCR, BC_NOARG, self->line);
  946. return true;
  947. }
  948. bool SubscrExpr__emit_del(Expr* self_, Ctx* ctx) {
  949. SubscrExpr* self = (SubscrExpr*)self_;
  950. vtemit_(self->lhs, ctx);
  951. vtemit_(self->rhs, ctx);
  952. Ctx__emit_(ctx, OP_DELETE_SUBSCR, BC_NOARG, self->line);
  953. return true;
  954. }
  955. SubscrExpr* SubscrExpr__new(int line) {
  956. const static ExprVt Vt = {
  957. .dtor = SubscrExpr__dtor,
  958. .emit_ = SubscrExpr__emit_,
  959. .emit_store = SubscrExpr__emit_store,
  960. .emit_inplace = SubscrExpr__emit_inplace,
  961. .emit_istore = SubscrExpr__emit_istore,
  962. .emit_del = SubscrExpr__emit_del,
  963. .is_subscr = true,
  964. };
  965. static_assert_expr_size(SubscrExpr);
  966. SubscrExpr* self = PoolExpr_alloc();
  967. self->vt = &Vt;
  968. self->line = line;
  969. self->lhs = NULL;
  970. self->rhs = NULL;
  971. return self;
  972. }
  973. typedef struct AttribExpr {
  974. EXPR_COMMON_HEADER
  975. Expr* child;
  976. py_Name name;
  977. } AttribExpr;
  978. void AttribExpr__dtor(Expr* self_) {
  979. AttribExpr* self = (AttribExpr*)self_;
  980. vtdelete(self->child);
  981. }
  982. void AttribExpr__emit_(Expr* self_, Ctx* ctx) {
  983. AttribExpr* self = (AttribExpr*)self_;
  984. vtemit_(self->child, ctx);
  985. Ctx__emit_(ctx, OP_LOAD_ATTR, self->name, self->line);
  986. }
  987. bool AttribExpr__emit_del(Expr* self_, Ctx* ctx) {
  988. AttribExpr* self = (AttribExpr*)self_;
  989. vtemit_(self->child, ctx);
  990. Ctx__emit_(ctx, OP_DELETE_ATTR, self->name, self->line);
  991. return true;
  992. }
  993. bool AttribExpr__emit_store(Expr* self_, Ctx* ctx) {
  994. AttribExpr* self = (AttribExpr*)self_;
  995. vtemit_(self->child, ctx);
  996. Ctx__emit_(ctx, OP_STORE_ATTR, BC_NOARG, self->line);
  997. return true;
  998. }
  999. void AttribExpr__emit_inplace(Expr* self_, Ctx* ctx) {
  1000. AttribExpr* self = (AttribExpr*)self_;
  1001. vtemit_(self->child, ctx);
  1002. Ctx__emit_(ctx, OP_DUP_TOP, BC_NOARG, self->line);
  1003. Ctx__emit_(ctx, OP_LOAD_ATTR, self->name, self->line);
  1004. }
  1005. bool AttribExpr__emit_istore(Expr* self_, Ctx* ctx) {
  1006. // [a, val] -> [val, a]
  1007. AttribExpr* self = (AttribExpr*)self_;
  1008. Ctx__emit_(ctx, OP_ROT_TWO, BC_NOARG, self->line);
  1009. Ctx__emit_(ctx, OP_STORE_ATTR, self->name, self->line);
  1010. return true;
  1011. }
  1012. AttribExpr* AttribExpr__new(int line, Expr* child, py_Name name) {
  1013. const static ExprVt Vt = {.emit_ = AttribExpr__emit_,
  1014. .emit_del = AttribExpr__emit_del,
  1015. .emit_store = AttribExpr__emit_store,
  1016. .emit_inplace = AttribExpr__emit_inplace,
  1017. .emit_istore = AttribExpr__emit_istore,
  1018. .dtor = AttribExpr__dtor,
  1019. .is_attrib = true};
  1020. static_assert_expr_size(AttribExpr);
  1021. AttribExpr* self = PoolExpr_alloc();
  1022. self->vt = &Vt;
  1023. self->line = line;
  1024. self->child = child;
  1025. self->name = name;
  1026. return self;
  1027. }
  1028. typedef struct CallExprKwArg {
  1029. py_Name key;
  1030. Expr* val;
  1031. } CallExprKwArg;
  1032. typedef struct CallExpr {
  1033. EXPR_COMMON_HEADER
  1034. Expr* callable;
  1035. c11_vector /*T=Expr* */ args;
  1036. // **a will be interpreted as a special keyword argument: {{0}: a}
  1037. c11_vector /*T=CallExprKwArg */ kwargs;
  1038. } CallExpr;
  1039. void CallExpr__dtor(Expr* self_) {
  1040. CallExpr* self = (CallExpr*)self_;
  1041. vtdelete(self->callable);
  1042. c11__foreach(Expr*, &self->args, e) vtdelete(*e);
  1043. c11__foreach(CallExprKwArg, &self->kwargs, e) vtdelete(e->val);
  1044. c11_vector__dtor(&self->args);
  1045. c11_vector__dtor(&self->kwargs);
  1046. }
  1047. void CallExpr__emit_(Expr* self_, Ctx* ctx) {
  1048. CallExpr* self = (CallExpr*)self_;
  1049. bool vargs = false; // whether there is *args as input
  1050. bool vkwargs = false; // whether there is **kwargs as input
  1051. c11__foreach(Expr*, &self->args, e) {
  1052. if((*e)->vt->is_starred) vargs = true;
  1053. }
  1054. c11__foreach(CallExprKwArg, &self->kwargs, e) {
  1055. if(e->val->vt->is_starred) vkwargs = true;
  1056. }
  1057. // if callable is a AttrExpr, we should try to use `fast_call` instead of use `boundmethod`
  1058. if(self->callable->vt->is_attrib) {
  1059. AttribExpr* p = (AttribExpr*)self->callable;
  1060. vtemit_(p->child, ctx);
  1061. Ctx__emit_(ctx, OP_LOAD_METHOD, p->name, p->line);
  1062. } else {
  1063. vtemit_(self->callable, ctx);
  1064. Ctx__emit_(ctx, OP_LOAD_NULL, BC_NOARG, BC_KEEPLINE);
  1065. }
  1066. Opcode opcode = OP_CALL;
  1067. if(vargs || vkwargs) {
  1068. // in this case, there is at least one *args or **kwargs as StarredExpr
  1069. // OP_CALL_VARGS needs to unpack them via __vectorcall_buffer
  1070. opcode = OP_CALL_VARGS;
  1071. }
  1072. c11__foreach(Expr*, &self->args, e) { vtemit_(*e, ctx); }
  1073. c11__foreach(CallExprKwArg, &self->kwargs, e) {
  1074. Ctx__emit_int(ctx, e->key, self->line);
  1075. vtemit_(e->val, ctx);
  1076. }
  1077. int KWARGC = self->kwargs.count;
  1078. int ARGC = self->args.count;
  1079. assert(KWARGC < 256 && ARGC < 256);
  1080. Ctx__emit_(ctx, opcode, (KWARGC << 8) | ARGC, self->line);
  1081. }
  1082. CallExpr* CallExpr__new(int line, Expr* callable) {
  1083. const static ExprVt Vt = {.dtor = CallExpr__dtor, .emit_ = CallExpr__emit_};
  1084. static_assert_expr_size(CallExpr);
  1085. CallExpr* self = PoolExpr_alloc();
  1086. self->vt = &Vt;
  1087. self->line = line;
  1088. self->callable = callable;
  1089. c11_vector__ctor(&self->args, sizeof(Expr*));
  1090. c11_vector__ctor(&self->kwargs, sizeof(CallExprKwArg));
  1091. return self;
  1092. }
  1093. /* context.c */
  1094. static void Ctx__ctor(Ctx* self, CodeObject* co, FuncDecl* func, int level) {
  1095. self->co = co;
  1096. self->func = func;
  1097. self->level = level;
  1098. self->curr_iblock = 0;
  1099. self->is_compiling_class = false;
  1100. c11_vector__ctor(&self->s_expr, sizeof(Expr*));
  1101. c11_smallmap_n2i__ctor(&self->global_names);
  1102. c11_smallmap_s2n__ctor(&self->co_consts_string_dedup_map);
  1103. }
  1104. static void Ctx__dtor(Ctx* self) {
  1105. // clean the expr stack
  1106. for(int i = 0; i < self->s_expr.count; i++) {
  1107. vtdelete(c11__getitem(Expr*, &self->s_expr, i));
  1108. }
  1109. c11_vector__dtor(&self->s_expr);
  1110. c11_smallmap_n2i__dtor(&self->global_names);
  1111. c11_smallmap_s2n__dtor(&self->co_consts_string_dedup_map);
  1112. }
  1113. static bool is_small_int(int64_t value) { return value >= INT16_MIN && value <= INT16_MAX; }
  1114. static int Ctx__get_loop(Ctx* self) {
  1115. int index = self->curr_iblock;
  1116. while(index >= 0) {
  1117. CodeBlock* block = c11__at(CodeBlock, &self->co->blocks, index);
  1118. if(block->type == CodeBlockType_FOR_LOOP) break;
  1119. if(block->type == CodeBlockType_WHILE_LOOP) break;
  1120. index = block->parent;
  1121. }
  1122. return index;
  1123. }
  1124. static CodeBlock* Ctx__enter_block(Ctx* self, CodeBlockType type) {
  1125. CodeBlock block = {type, self->curr_iblock, self->co->codes.count, -1, -1};
  1126. c11_vector__push(CodeBlock, &self->co->blocks, block);
  1127. self->curr_iblock = self->co->blocks.count - 1;
  1128. return c11__at(CodeBlock, &self->co->blocks, self->curr_iblock);
  1129. }
  1130. static void Ctx__exit_block(Ctx* self) {
  1131. CodeBlock* block = c11__at(CodeBlock, &self->co->blocks, self->curr_iblock);
  1132. CodeBlockType curr_type = block->type;
  1133. block->end = self->co->codes.count;
  1134. self->curr_iblock = block->parent;
  1135. assert(self->curr_iblock >= 0);
  1136. if(curr_type == CodeBlockType_FOR_LOOP) {
  1137. // add a no op here to make block check work
  1138. Ctx__emit_virtual(self, OP_NO_OP, BC_NOARG, BC_KEEPLINE, true);
  1139. }
  1140. }
  1141. static void Ctx__s_emit_decorators(Ctx* self, int count) {
  1142. assert(Ctx__s_size(self) >= count);
  1143. // [obj]
  1144. for(int i = 0; i < count; i++) {
  1145. Expr* deco = Ctx__s_popx(self);
  1146. vtemit_(deco, self); // [obj, f]
  1147. Ctx__emit_(self, OP_ROT_TWO, BC_NOARG, deco->line); // [f, obj]
  1148. Ctx__emit_(self, OP_LOAD_NULL, BC_NOARG, BC_KEEPLINE); // [f, obj, NULL]
  1149. Ctx__emit_(self, OP_ROT_TWO, BC_NOARG, BC_KEEPLINE); // [obj, NULL, f]
  1150. Ctx__emit_(self, OP_CALL, 1, deco->line); // [obj]
  1151. vtdelete(deco);
  1152. }
  1153. }
  1154. static int Ctx__emit_virtual(Ctx* self, Opcode opcode, uint16_t arg, int line, bool is_virtual) {
  1155. Bytecode bc = {(uint8_t)opcode, arg};
  1156. BytecodeEx bcx = {line, is_virtual, self->curr_iblock};
  1157. c11_vector__push(Bytecode, &self->co->codes, bc);
  1158. c11_vector__push(BytecodeEx, &self->co->codes_ex, bcx);
  1159. int i = self->co->codes.count - 1;
  1160. BytecodeEx* codes_ex = (BytecodeEx*)self->co->codes_ex.data;
  1161. if(line == BC_KEEPLINE) { codes_ex[i].lineno = i >= 1 ? codes_ex[i - 1].lineno : 1; }
  1162. return i;
  1163. }
  1164. static int Ctx__emit_(Ctx* self, Opcode opcode, uint16_t arg, int line) {
  1165. return Ctx__emit_virtual(self, opcode, arg, line, false);
  1166. }
  1167. static void Ctx__revert_last_emit_(Ctx* self) {
  1168. c11_vector__pop(&self->co->codes);
  1169. c11_vector__pop(&self->co->codes_ex);
  1170. }
  1171. static void Ctx__try_merge_for_iter_store(Ctx* self, int i) {
  1172. // [FOR_ITER, STORE_?, ]
  1173. Bytecode* co_codes = (Bytecode*)self->co->codes.data;
  1174. if(co_codes[i].op != OP_FOR_ITER) return;
  1175. if(self->co->codes.count - i != 2) return;
  1176. uint16_t arg = co_codes[i + 1].arg;
  1177. if(co_codes[i + 1].op == OP_STORE_FAST) {
  1178. Ctx__revert_last_emit_(self);
  1179. co_codes[i].op = OP_FOR_ITER_STORE_FAST;
  1180. co_codes[i].arg = arg;
  1181. return;
  1182. }
  1183. if(co_codes[i + 1].op == OP_STORE_GLOBAL) {
  1184. Ctx__revert_last_emit_(self);
  1185. co_codes[i].op = OP_FOR_ITER_STORE_GLOBAL;
  1186. co_codes[i].arg = arg;
  1187. return;
  1188. }
  1189. }
  1190. static int Ctx__emit_int(Ctx* self, int64_t value, int line) {
  1191. if(is_small_int(value)) {
  1192. return Ctx__emit_(self, OP_LOAD_SMALL_INT, (uint16_t)value, line);
  1193. } else {
  1194. py_TValue tmp;
  1195. py_newint(&tmp, value);
  1196. return Ctx__emit_(self, OP_LOAD_CONST, Ctx__add_const(self, &tmp), line);
  1197. }
  1198. }
  1199. static void Ctx__patch_jump(Ctx* self, int index) {
  1200. Bytecode* co_codes = (Bytecode*)self->co->codes.data;
  1201. int target = self->co->codes.count;
  1202. Bytecode__set_signed_arg(&co_codes[index], target - index);
  1203. }
  1204. static bool Ctx__add_label(Ctx* self, py_Name name) {
  1205. bool ok = c11_smallmap_n2i__contains(&self->co->labels, name);
  1206. if(ok) return false;
  1207. c11_smallmap_n2i__set(&self->co->labels, name, self->co->codes.count);
  1208. return true;
  1209. }
  1210. static int Ctx__add_varname(Ctx* self, py_Name name) {
  1211. // PK_MAX_CO_VARNAMES will be checked when pop_context(), not here
  1212. int index = c11_smallmap_n2i__get(&self->co->varnames_inv, name, -1);
  1213. if(index >= 0) return index;
  1214. c11_vector__push(uint16_t, &self->co->varnames, name);
  1215. self->co->nlocals++;
  1216. index = self->co->varnames.count - 1;
  1217. c11_smallmap_n2i__set(&self->co->varnames_inv, name, index);
  1218. return index;
  1219. }
  1220. static int Ctx__add_const_string(Ctx* self, c11_sv key) {
  1221. uint16_t* val = c11_smallmap_s2n__try_get(&self->co_consts_string_dedup_map, key);
  1222. if(val) {
  1223. return *val;
  1224. } else {
  1225. py_TValue tmp;
  1226. py_newstrn(&tmp, key.data, key.size);
  1227. c11_vector__push(py_TValue, &self->co->consts, tmp);
  1228. int index = self->co->consts.count - 1;
  1229. c11_smallmap_s2n__set(&self->co_consts_string_dedup_map,
  1230. c11_string__sv(PyObject__userdata(tmp._obj)),
  1231. index);
  1232. return index;
  1233. }
  1234. }
  1235. static int Ctx__add_const(Ctx* self, py_Ref v) {
  1236. assert(v->type != tp_str);
  1237. c11_vector__push(py_TValue, &self->co->consts, *v);
  1238. return self->co->consts.count - 1;
  1239. }
  1240. static void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line) {
  1241. switch(scope) {
  1242. case NAME_LOCAL: Ctx__emit_(self, OP_STORE_FAST, Ctx__add_varname(self, name), line); break;
  1243. case NAME_GLOBAL: Ctx__emit_(self, OP_STORE_GLOBAL, name, line); break;
  1244. case NAME_GLOBAL_UNKNOWN: Ctx__emit_(self, OP_STORE_NAME, name, line); break;
  1245. default: c11__unreachedable();
  1246. }
  1247. }
  1248. // emit top -> pop -> delete
  1249. static void Ctx__s_emit_top(Ctx* self) {
  1250. Expr* top = c11_vector__back(Expr*, &self->s_expr);
  1251. vtemit_(top, self);
  1252. vtdelete(top);
  1253. c11_vector__pop(&self->s_expr);
  1254. }
  1255. // push
  1256. static void Ctx__s_push(Ctx* self, Expr* expr) { c11_vector__push(Expr*, &self->s_expr, expr); }
  1257. // top
  1258. static Expr* Ctx__s_top(Ctx* self) { return c11_vector__back(Expr*, &self->s_expr); }
  1259. // size
  1260. static int Ctx__s_size(Ctx* self) { return self->s_expr.count; }
  1261. // pop -> delete
  1262. static void Ctx__s_pop(Ctx* self) {
  1263. Expr* top = c11_vector__back(Expr*, &self->s_expr);
  1264. vtdelete(top);
  1265. c11_vector__pop(&self->s_expr);
  1266. }
  1267. // pop move
  1268. static Expr* Ctx__s_popx(Ctx* self) {
  1269. Expr* top = c11_vector__back(Expr*, &self->s_expr);
  1270. c11_vector__pop(&self->s_expr);
  1271. return top;
  1272. }
  1273. /* compiler.c */
  1274. typedef struct Compiler Compiler;
  1275. typedef Error* (*PrattCallback)(Compiler* self);
  1276. typedef struct PrattRule {
  1277. PrattCallback prefix;
  1278. PrattCallback infix;
  1279. enum Precedence precedence;
  1280. } PrattRule;
  1281. const static PrattRule rules[TK__COUNT__];
  1282. typedef struct Compiler {
  1283. pk_SourceData_ src; // weakref
  1284. pk_TokenArray tokens;
  1285. int i;
  1286. c11_vector /*T=CodeEmitContext*/ contexts;
  1287. } Compiler;
  1288. static void Compiler__ctor(Compiler* self, pk_SourceData_ src, pk_TokenArray tokens) {
  1289. self->src = src;
  1290. self->tokens = tokens;
  1291. self->i = 0;
  1292. c11_vector__ctor(&self->contexts, sizeof(Ctx));
  1293. }
  1294. static void Compiler__dtor(Compiler* self) {
  1295. pk_TokenArray__dtor(&self->tokens);
  1296. c11_vector__dtor(&self->contexts);
  1297. }
  1298. /**************************************/
  1299. #define tk(i) c11__at(Token, &self->tokens, i)
  1300. #define prev() tk(self->i - 1)
  1301. #define curr() tk(self->i)
  1302. #define next() tk(self->i + 1)
  1303. // #define err() (self->i == self->tokens.count ? prev() : curr())
  1304. #define advance() self->i++
  1305. #define mode() self->src->mode
  1306. #define ctx() (&c11_vector__back(Ctx, &self->contexts))
  1307. #define match_newlines() match_newlines_impl(self)
  1308. #define consume(expected) \
  1309. if(!match(expected)) \
  1310. return SyntaxError("expected '%s', got '%s'", \
  1311. pk_TokenSymbols[expected], \
  1312. pk_TokenSymbols[curr()->type]);
  1313. #define consume_end_stmt() \
  1314. if(!match_end_stmt(self)) return SyntaxError("expected statement end")
  1315. #define check(B) \
  1316. if((err = B)) return err
  1317. static NameScope name_scope(Compiler* self) {
  1318. NameScope s = self->contexts.count > 1 ? NAME_LOCAL : NAME_GLOBAL;
  1319. if(self->src->is_dynamic && s == NAME_GLOBAL) s = NAME_GLOBAL_UNKNOWN;
  1320. return s;
  1321. }
  1322. #define SyntaxError(...) NULL
  1323. /* Matchers */
  1324. static bool is_expression(Compiler* self, bool allow_slice) {
  1325. PrattCallback prefix = rules[curr()->type].prefix;
  1326. return prefix && (allow_slice || curr()->type != TK_COLON);
  1327. }
  1328. #define match(expected) (curr()->type == expected ? (++self->i) : 0)
  1329. static bool match_newlines_impl(Compiler* self) {
  1330. bool consumed = false;
  1331. if(curr()->type == TK_EOL) {
  1332. while(curr()->type == TK_EOL)
  1333. advance();
  1334. consumed = true;
  1335. }
  1336. return consumed;
  1337. }
  1338. static bool match_end_stmt(Compiler* self) {
  1339. if(match(TK_SEMICOLON)) {
  1340. match_newlines();
  1341. return true;
  1342. }
  1343. if(match_newlines() || curr()->type == TK_EOF) return true;
  1344. if(curr()->type == TK_DEDENT) return true;
  1345. return false;
  1346. }
  1347. /* Expression */
  1348. /// Parse an expression and push it onto the stack.
  1349. static Error* parse_expression(Compiler* self, int precedence, bool allow_slice) {
  1350. PrattCallback prefix = rules[curr()->type].prefix;
  1351. if(!prefix || (curr()->type == TK_COLON && !allow_slice)) {
  1352. return SyntaxError("expected an expression, got %s", pk_TokenSymbols[curr()->type]);
  1353. }
  1354. advance();
  1355. Error* err;
  1356. check(prefix(self));
  1357. while(rules[curr()->type].precedence >= precedence &&
  1358. (allow_slice || curr()->type != TK_COLON)) {
  1359. TokenIndex op = curr()->type;
  1360. advance();
  1361. PrattCallback infix = rules[op].infix;
  1362. assert(infix != NULL);
  1363. check(infix(self));
  1364. }
  1365. return NULL;
  1366. }
  1367. static Error* EXPR_TUPLE_ALLOW_SLICE(Compiler* self, bool allow_slice) {
  1368. Error* err;
  1369. check(parse_expression(self, PREC_LOWEST + 1, allow_slice));
  1370. if(!match(TK_COMMA)) return NULL;
  1371. // tuple expression // (a, )
  1372. int count = 1;
  1373. do {
  1374. if(curr()->brackets_level) match_newlines();
  1375. if(!is_expression(self, allow_slice)) break;
  1376. check(parse_expression(self, PREC_LOWEST + 1, allow_slice));
  1377. count += 1;
  1378. if(curr()->brackets_level) match_newlines();
  1379. } while(match(TK_COMMA));
  1380. // pop `count` expressions from the stack and merge them into a TupleExpr
  1381. SequenceExpr* e = TupleExpr__new(prev()->line, count);
  1382. for(int i = count - 1; i >= 0; i--) {
  1383. Expr* item = Ctx__s_popx(ctx());
  1384. c11__setitem(Expr*, &e->items, i, item);
  1385. }
  1386. Ctx__s_push(ctx(), (Expr*)e);
  1387. return NULL;
  1388. }
  1389. /// Parse a simple expression.
  1390. static Error* EXPR(Compiler* self) { return parse_expression(self, PREC_LOWEST + 1, false); }
  1391. /// Parse a simple expression or a tuple of expressions.
  1392. static Error* EXPR_TUPLE(Compiler* self) { return EXPR_TUPLE_ALLOW_SLICE(self, false); }
  1393. // special case for `for loop` and `comp`
  1394. static Error* EXPR_VARS(Compiler* self) {
  1395. // int count = 0;
  1396. // do {
  1397. // consume(TK_ID);
  1398. // ctx()->s_push(make_expr<NameExpr>(prev().str(), name_scope()));
  1399. // count += 1;
  1400. // } while(match(TK_COMMA));
  1401. // if(count > 1){
  1402. // TupleExpr* e = make_expr<TupleExpr>(count);
  1403. // for(int i=count-1; i>=0; i--)
  1404. // e->items[i] = Ctx__s_popx(ctx());
  1405. // ctx()->s_push(e);
  1406. // }
  1407. return NULL;
  1408. }
  1409. /* Misc */
  1410. static void push_global_context(Compiler* self, CodeObject* co) {
  1411. co->start_line = self->i == 0 ? 1 : prev()->line;
  1412. Ctx* ctx = c11_vector__emplace(&self->contexts);
  1413. Ctx__ctor(ctx, co, NULL, self->contexts.count);
  1414. }
  1415. static Error* pop_context(Compiler* self) {
  1416. // add a `return None` in the end as a guard
  1417. // previously, we only do this if the last opcode is not a return
  1418. // however, this is buggy...since there may be a jump to the end (out of bound) even if the last
  1419. // opcode is a return
  1420. Ctx__emit_virtual(ctx(), OP_RETURN_VALUE, 1, BC_KEEPLINE, true);
  1421. CodeObject* co = ctx()->co;
  1422. // find the last valid token
  1423. int j = self->i - 1;
  1424. while(tk(j)->type == TK_EOL || tk(j)->type == TK_DEDENT || tk(j)->type == TK_EOF)
  1425. j--;
  1426. co->end_line = tk(j)->line;
  1427. // some check here
  1428. c11_vector* codes = &co->codes;
  1429. if(co->nlocals > PK_MAX_CO_VARNAMES) {
  1430. return SyntaxError("maximum number of local variables exceeded");
  1431. }
  1432. if(co->consts.count > 65530) { return SyntaxError("maximum number of constants exceeded"); }
  1433. // pre-compute LOOP_BREAK and LOOP_CONTINUE
  1434. for(int i = 0; i < codes->count; i++) {
  1435. Bytecode* bc = c11__at(Bytecode, codes, i);
  1436. if(bc->op == OP_LOOP_CONTINUE) {
  1437. CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
  1438. Bytecode__set_signed_arg(bc, block->start - i);
  1439. } else if(bc->op == OP_LOOP_BREAK) {
  1440. CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
  1441. Bytecode__set_signed_arg(bc, (block->end2 != -1 ? block->end2 : block->end) - i);
  1442. }
  1443. }
  1444. // pre-compute func->is_simple
  1445. FuncDecl* func = ctx()->func;
  1446. if(func) {
  1447. // check generator
  1448. c11__foreach(Bytecode, &func->code.codes, bc) {
  1449. if(bc->op == OP_YIELD_VALUE || bc->op == OP_FOR_ITER_YIELD_VALUE) {
  1450. func->type = FuncType_GENERATOR;
  1451. c11__foreach(Bytecode, &func->code.codes, bc) {
  1452. if(bc->op == OP_RETURN_VALUE && bc->arg == BC_NOARG) {
  1453. return SyntaxError("'return' with argument inside generator function");
  1454. }
  1455. }
  1456. break;
  1457. }
  1458. }
  1459. if(func->type == FuncType_UNSET) {
  1460. bool is_simple = true;
  1461. if(func->kwargs.count > 0) is_simple = false;
  1462. if(func->starred_arg >= 0) is_simple = false;
  1463. if(func->starred_kwarg >= 0) is_simple = false;
  1464. if(is_simple) {
  1465. func->type = FuncType_SIMPLE;
  1466. bool is_empty = false;
  1467. if(func->code.codes.count == 1) {
  1468. Bytecode bc = c11__getitem(Bytecode, &func->code.codes, 0);
  1469. if(bc.op == OP_RETURN_VALUE && bc.arg == 1) { is_empty = true; }
  1470. }
  1471. if(is_empty) func->type = FuncType_EMPTY;
  1472. } else
  1473. func->type = FuncType_NORMAL;
  1474. }
  1475. assert(func->type != FuncType_UNSET);
  1476. }
  1477. Ctx__dtor(ctx());
  1478. c11_vector__pop(&self->contexts);
  1479. return NULL;
  1480. }
  1481. /* Expression Callbacks */
  1482. static Error* exprLiteral(Compiler* self) {
  1483. LiteralExpr* e = LiteralExpr__new(prev()->line, &prev()->value);
  1484. Ctx__s_push(ctx(), (Expr*)e);
  1485. return NULL;
  1486. }
  1487. static Error* exprLong(Compiler* self) {
  1488. c11_sv sv = Token__sv(prev());
  1489. Ctx__s_push(ctx(), (Expr*)RawStringExpr__new(prev()->line, sv, OP_BUILD_LONG));
  1490. return NULL;
  1491. }
  1492. static Error* exprBytes(Compiler* self) {
  1493. c11_sv sv = c11_string__sv(prev()->value._str);
  1494. Ctx__s_push(ctx(), (Expr*)RawStringExpr__new(prev()->line, sv, OP_BUILD_BYTES));
  1495. return NULL;
  1496. }
  1497. static Error* exprFString(Compiler* self) {
  1498. c11_sv sv = c11_string__sv(prev()->value._str);
  1499. Ctx__s_push(ctx(), (Expr*)FStringExpr__new(prev()->line, sv));
  1500. return NULL;
  1501. }
  1502. static Error* exprImag(Compiler* self) {
  1503. Ctx__s_push(ctx(), (Expr*)ImagExpr__new(prev()->line, prev()->value._f64));
  1504. return NULL;
  1505. }
  1506. static Error* exprLambda(Compiler* self) {
  1507. assert(false);
  1508. return NULL;
  1509. // Error* err;
  1510. // int line = prev()->line;
  1511. // int decl_index;
  1512. // FuncDecl_ decl = push_f_context({"<lambda>", 8}, &decl_index);
  1513. // if(!match(TK_COLON)) {
  1514. // check(_compile_f_args(decl, false));
  1515. // consume(TK_COLON);
  1516. // }
  1517. // // https://github.com/pocketpy/pocketpy/issues/37
  1518. // check(parse_expression(self, PREC_LAMBDA + 1, false));
  1519. // Ctx__s_emit_top(ctx());
  1520. // Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
  1521. // check(pop_context(self));
  1522. // LambdaExpr* e = LambdaExpr__new(line, decl_index);
  1523. // Ctx__s_push(ctx(), (Expr*)e);
  1524. // return NULL;
  1525. }
  1526. static Error* exprOr(Compiler* self) {
  1527. Error* err;
  1528. int line = prev()->line;
  1529. check(parse_expression(self, PREC_LOGICAL_OR + 1, false));
  1530. LogicBinaryExpr* e = LogicBinaryExpr__new(line, OP_JUMP_IF_TRUE_OR_POP);
  1531. e->rhs = Ctx__s_popx(ctx());
  1532. e->lhs = Ctx__s_popx(ctx());
  1533. Ctx__s_push(ctx(), (Expr*)e);
  1534. return NULL;
  1535. }
  1536. static Error* exprAnd(Compiler* self) {
  1537. Error* err;
  1538. int line = prev()->line;
  1539. check(parse_expression(self, PREC_LOGICAL_AND + 1, false));
  1540. LogicBinaryExpr* e = LogicBinaryExpr__new(line, OP_JUMP_IF_FALSE_OR_POP);
  1541. e->rhs = Ctx__s_popx(ctx());
  1542. e->lhs = Ctx__s_popx(ctx());
  1543. Ctx__s_push(ctx(), (Expr*)e);
  1544. return NULL;
  1545. }
  1546. static Error* exprTernary(Compiler* self) {
  1547. // [true_expr]
  1548. Error* err;
  1549. int line = prev()->line;
  1550. check(parse_expression(self, PREC_TERNARY + 1, false)); // [true_expr, cond]
  1551. consume(TK_ELSE);
  1552. check(parse_expression(self, PREC_TERNARY + 1, false)); // [true_expr, cond, false_expr]
  1553. TernaryExpr* e = TernaryExpr__new(line);
  1554. e->false_expr = Ctx__s_popx(ctx());
  1555. e->cond = Ctx__s_popx(ctx());
  1556. e->true_expr = Ctx__s_popx(ctx());
  1557. Ctx__s_push(ctx(), (Expr*)e);
  1558. return NULL;
  1559. }
  1560. static Error* exprBinaryOp(Compiler* self) {
  1561. Error* err;
  1562. int line = prev()->line;
  1563. TokenIndex op = prev()->type;
  1564. check(parse_expression(self, rules[op].precedence + 1, false));
  1565. BinaryExpr* e = BinaryExpr__new(line, op, false);
  1566. if(op == TK_IN || op == TK_NOT_IN) {
  1567. e->lhs = Ctx__s_popx(ctx());
  1568. e->rhs = Ctx__s_popx(ctx());
  1569. } else {
  1570. e->rhs = Ctx__s_popx(ctx());
  1571. e->lhs = Ctx__s_popx(ctx());
  1572. }
  1573. Ctx__s_push(ctx(), (Expr*)e);
  1574. return NULL;
  1575. }
  1576. static Error* exprNot(Compiler* self) {
  1577. Error* err;
  1578. int line = prev()->line;
  1579. check(parse_expression(self, PREC_LOGICAL_NOT + 1, false));
  1580. UnaryExpr* e = UnaryExpr__new(line, Ctx__s_popx(ctx()), OP_UNARY_NOT);
  1581. Ctx__s_push(ctx(), (Expr*)e);
  1582. return NULL;
  1583. }
  1584. static Error* exprUnaryOp(Compiler* self) {
  1585. Error* err;
  1586. int line = prev()->line;
  1587. TokenIndex op = prev()->type;
  1588. check(parse_expression(self, PREC_UNARY + 1, false));
  1589. Expr* e = Ctx__s_popx(ctx());
  1590. switch(op) {
  1591. case TK_SUB: {
  1592. // constant fold
  1593. if(e->vt->is_literal) {
  1594. LiteralExpr* le = (LiteralExpr*)e;
  1595. if(le->value->index == TokenValue_I64 || le->value->index == TokenValue_F64) {
  1596. le->negated = true;
  1597. }
  1598. Ctx__s_push(ctx(), e);
  1599. } else {
  1600. Ctx__s_push(ctx(), (Expr*)UnaryExpr__new(line, e, OP_UNARY_NEGATIVE));
  1601. }
  1602. break;
  1603. }
  1604. case TK_INVERT: Ctx__s_push(ctx(), (Expr*)UnaryExpr__new(line, e, OP_UNARY_INVERT)); break;
  1605. case TK_MUL: Ctx__s_push(ctx(), (Expr*)StarredExpr__new(line, e, 1)); break;
  1606. case TK_POW: Ctx__s_push(ctx(), (Expr*)StarredExpr__new(line, e, 2)); break;
  1607. default: assert(false);
  1608. }
  1609. return NULL;
  1610. }
  1611. static Error* exprGroup(Compiler* self) {
  1612. Error* err;
  1613. int line = prev()->line;
  1614. match_newlines();
  1615. check(EXPR_TUPLE(self)); // () is just for change precedence
  1616. match_newlines();
  1617. consume(TK_RPAREN);
  1618. if(Ctx__s_top(ctx())->vt->is_tuple) return NULL;
  1619. GroupedExpr* g = GroupedExpr__new(line, Ctx__s_popx(ctx()));
  1620. Ctx__s_push(ctx(), (Expr*)g);
  1621. return NULL;
  1622. }
  1623. static Error* exprName(Compiler* self) {
  1624. py_Name name = py_name2(Token__sv(prev()));
  1625. NameScope scope = name_scope(self);
  1626. // promote this name to global scope if needed
  1627. if(c11_smallmap_n2i__contains(&ctx()->global_names, name)) { scope = NAME_GLOBAL; }
  1628. NameExpr* e = NameExpr__new(prev()->line, name, scope);
  1629. Ctx__s_push(ctx(), (Expr*)e);
  1630. return NULL;
  1631. }
  1632. static Error* exprAttrib(Compiler* self) {
  1633. consume(TK_ID);
  1634. py_Name name = py_name2(Token__sv(prev()));
  1635. AttribExpr* e = AttribExpr__new(prev()->line, Ctx__s_popx(ctx()), name);
  1636. Ctx__s_push(ctx(), (Expr*)e);
  1637. return NULL;
  1638. }
  1639. static Error* exprLiteral0(Compiler* self) {
  1640. Literal0Expr* e = Literal0Expr__new(prev()->line, prev()->type);
  1641. Ctx__s_push(ctx(), (Expr*)e);
  1642. return NULL;
  1643. }
  1644. static Error* consume_comp(Compiler* self, Opcode op0, Opcode op1) {
  1645. // [expr]
  1646. Error* err;
  1647. int line = prev()->line;
  1648. bool has_cond = false;
  1649. check(EXPR_VARS(self)); // [expr, vars]
  1650. consume(TK_IN);
  1651. check(parse_expression(self, PREC_TERNARY + 1, false)); // [expr, vars, iter]
  1652. match_newlines();
  1653. if(match(TK_IF)) {
  1654. check(parse_expression(self, PREC_TERNARY + 1, false)); // [expr, vars, iter, cond]
  1655. has_cond = true;
  1656. }
  1657. CompExpr* ce = CompExpr__new(line, op0, op1);
  1658. if(has_cond) ce->cond = Ctx__s_popx(ctx());
  1659. ce->iter = Ctx__s_popx(ctx());
  1660. ce->vars = Ctx__s_popx(ctx());
  1661. ce->expr = Ctx__s_popx(ctx());
  1662. Ctx__s_push(ctx(), (Expr*)ce);
  1663. match_newlines();
  1664. return NULL;
  1665. }
  1666. static Error* exprList(Compiler* self) {
  1667. Error* err;
  1668. int line = prev()->line;
  1669. int count = 0;
  1670. do {
  1671. match_newlines();
  1672. if(curr()->type == TK_RBRACKET) break;
  1673. check(EXPR(self));
  1674. count += 1;
  1675. match_newlines();
  1676. if(count == 1 && match(TK_FOR)) {
  1677. check(consume_comp(self, OP_BUILD_LIST, OP_LIST_APPEND));
  1678. consume(TK_RBRACKET);
  1679. return NULL;
  1680. }
  1681. match_newlines();
  1682. } while(match(TK_COMMA));
  1683. consume(TK_RBRACKET);
  1684. SequenceExpr* e = ListExpr__new(line, count);
  1685. for(int i = count - 1; i >= 0; i--) {
  1686. c11__setitem(Expr*, &e->items, i, Ctx__s_popx(ctx()));
  1687. }
  1688. Ctx__s_push(ctx(), (Expr*)e);
  1689. return NULL;
  1690. }
  1691. static Error* exprMap(Compiler* self) {
  1692. Error* err;
  1693. int line = prev()->line;
  1694. bool parsing_dict = false; // {...} may be dict or set
  1695. int count = 0;
  1696. do {
  1697. match_newlines();
  1698. if(curr()->type == TK_RBRACE) break;
  1699. check(EXPR(self)); // [key]
  1700. if(curr()->type == TK_COLON) { parsing_dict = true; }
  1701. if(parsing_dict) {
  1702. consume(TK_COLON);
  1703. check(EXPR(self)); // [key, value]
  1704. }
  1705. count += 1; // key-value pair count
  1706. match_newlines();
  1707. if(count == 1 && match(TK_FOR)) {
  1708. if(parsing_dict) {
  1709. check(consume_comp(self, OP_BUILD_DICT, OP_DICT_ADD));
  1710. } else {
  1711. check(consume_comp(self, OP_BUILD_SET, OP_SET_ADD));
  1712. }
  1713. consume(TK_RBRACE);
  1714. return NULL;
  1715. }
  1716. match_newlines();
  1717. } while(match(TK_COMMA));
  1718. consume(TK_RBRACE);
  1719. SequenceExpr* se;
  1720. if(count == 0 || parsing_dict) {
  1721. count *= 2; // key + value
  1722. se = DictExpr__new(line, count);
  1723. } else {
  1724. se = SetExpr__new(line, count);
  1725. }
  1726. for(int i = count - 1; i >= 0; i--) {
  1727. c11__setitem(Expr*, &se->items, i, Ctx__s_popx(ctx()));
  1728. }
  1729. Ctx__s_push(ctx(), (Expr*)se);
  1730. return NULL;
  1731. }
  1732. static Error* exprCall(Compiler* self) {
  1733. Error* err;
  1734. CallExpr* e = CallExpr__new(prev()->line, Ctx__s_popx(ctx()));
  1735. Ctx__s_push(ctx(), (Expr*)e); // push onto the stack in advance
  1736. do {
  1737. match_newlines();
  1738. if(curr()->type == TK_RPAREN) break;
  1739. if(curr()->type == TK_ID && next()->type == TK_ASSIGN) {
  1740. consume(TK_ID);
  1741. py_Name key = py_name2(Token__sv(prev()));
  1742. consume(TK_ASSIGN);
  1743. check(EXPR(self));
  1744. CallExprKwArg kw = {key, Ctx__s_popx(ctx())};
  1745. c11_vector__push(CallExprKwArg, &e->kwargs, kw);
  1746. } else {
  1747. check(EXPR(self));
  1748. int star_level = 0;
  1749. Expr* top = Ctx__s_top(ctx());
  1750. if(top->vt->is_starred) star_level = ((StarredExpr*)top)->level;
  1751. if(star_level == 2) {
  1752. // **kwargs
  1753. CallExprKwArg kw = {0, Ctx__s_popx(ctx())};
  1754. c11_vector__push(CallExprKwArg, &e->kwargs, kw);
  1755. } else {
  1756. // positional argument
  1757. if(e->kwargs.count > 0) {
  1758. return SyntaxError("positional argument follows keyword argument");
  1759. }
  1760. c11_vector__push(Expr*, &e->args, Ctx__s_popx(ctx()));
  1761. }
  1762. }
  1763. match_newlines();
  1764. } while(match(TK_COMMA));
  1765. consume(TK_RPAREN);
  1766. return NULL;
  1767. }
  1768. static Error* exprSlice0(Compiler* self) {
  1769. Error* err;
  1770. SliceExpr* slice = SliceExpr__new(prev()->line);
  1771. Ctx__s_push(ctx(), (Expr*)slice); // push onto the stack in advance
  1772. if(is_expression(self, false)) { // :<stop>
  1773. check(EXPR(self));
  1774. slice->stop = Ctx__s_popx(ctx());
  1775. // try optional step
  1776. if(match(TK_COLON)) { // :<stop>:<step>
  1777. check(EXPR(self));
  1778. slice->step = Ctx__s_popx(ctx());
  1779. }
  1780. } else if(match(TK_COLON)) {
  1781. if(is_expression(self, false)) { // ::<step>
  1782. check(EXPR(self));
  1783. slice->step = Ctx__s_popx(ctx());
  1784. } // else ::
  1785. } // else :
  1786. return NULL;
  1787. }
  1788. static Error* exprSlice1(Compiler* self) {
  1789. Error* err;
  1790. SliceExpr* slice = SliceExpr__new(prev()->line);
  1791. slice->start = Ctx__s_popx(ctx());
  1792. Ctx__s_push(ctx(), (Expr*)slice); // push onto the stack in advance
  1793. if(is_expression(self, false)) { // <start>:<stop>
  1794. check(EXPR(self));
  1795. slice->stop = Ctx__s_popx(ctx());
  1796. // try optional step
  1797. if(match(TK_COLON)) { // <start>:<stop>:<step>
  1798. check(EXPR(self));
  1799. slice->step = Ctx__s_popx(ctx());
  1800. }
  1801. } else if(match(TK_COLON)) { // <start>::<step>
  1802. check(EXPR(self));
  1803. slice->step = Ctx__s_popx(ctx());
  1804. } // else <start>:
  1805. return NULL;
  1806. }
  1807. static Error* exprSubscr(Compiler* self) {
  1808. Error* err;
  1809. int line = prev()->line;
  1810. match_newlines();
  1811. check(EXPR_TUPLE_ALLOW_SLICE(self, true));
  1812. match_newlines();
  1813. consume(TK_RBRACKET); // [lhs, rhs]
  1814. SubscrExpr* e = SubscrExpr__new(line);
  1815. e->rhs = Ctx__s_popx(ctx()); // [lhs]
  1816. e->lhs = Ctx__s_popx(ctx()); // []
  1817. Ctx__s_push(ctx(), (Expr*)e);
  1818. return NULL;
  1819. }
  1820. ////////////////
  1821. static Error* consume_type_hints(Compiler* self) {
  1822. Error* err;
  1823. check(EXPR(self));
  1824. Ctx__s_pop(ctx());
  1825. return NULL;
  1826. }
  1827. static Error* compile_stmt(Compiler* self);
  1828. static Error* compile_block_body(Compiler* self, PrattCallback callback) {
  1829. Error* err;
  1830. assert(callback != NULL);
  1831. consume(TK_COLON);
  1832. if(curr()->type != TK_EOL && curr()->type != TK_EOF) {
  1833. while(true) {
  1834. check(compile_stmt(self));
  1835. bool possible = curr()->type != TK_EOL && curr()->type != TK_EOF;
  1836. if(prev()->type != TK_SEMICOLON || !possible) break;
  1837. }
  1838. return NULL;
  1839. }
  1840. bool consumed = match_newlines();
  1841. if(!consumed) return SyntaxError("expected a new line after ':'");
  1842. consume(TK_INDENT);
  1843. while(curr()->type != TK_DEDENT) {
  1844. match_newlines();
  1845. check(callback(self));
  1846. match_newlines();
  1847. }
  1848. consume(TK_DEDENT);
  1849. return NULL;
  1850. }
  1851. static Error* compile_if_stmt(Compiler* self) {
  1852. Error* err;
  1853. check(EXPR(self)); // condition
  1854. Ctx__s_emit_top(ctx());
  1855. int patch = Ctx__emit_(ctx(), OP_POP_JUMP_IF_FALSE, BC_NOARG, prev()->line);
  1856. err = compile_block_body(self, compile_stmt);
  1857. if(err) return err;
  1858. if(match(TK_ELIF)) {
  1859. int exit_patch = Ctx__emit_(ctx(), OP_JUMP_FORWARD, BC_NOARG, prev()->line);
  1860. Ctx__patch_jump(ctx(), patch);
  1861. check(compile_if_stmt(self));
  1862. Ctx__patch_jump(ctx(), exit_patch);
  1863. } else if(match(TK_ELSE)) {
  1864. int exit_patch = Ctx__emit_(ctx(), OP_JUMP_FORWARD, BC_NOARG, prev()->line);
  1865. Ctx__patch_jump(ctx(), patch);
  1866. check(compile_block_body(self, compile_stmt));
  1867. Ctx__patch_jump(ctx(), exit_patch);
  1868. } else {
  1869. Ctx__patch_jump(ctx(), patch);
  1870. }
  1871. return NULL;
  1872. }
  1873. static Error* compile_while_loop(Compiler* self) {
  1874. Error* err;
  1875. CodeBlock* block = Ctx__enter_block(ctx(), CodeBlockType_WHILE_LOOP);
  1876. check(EXPR(self)); // condition
  1877. Ctx__s_emit_top(ctx());
  1878. int patch = Ctx__emit_(ctx(), OP_POP_JUMP_IF_FALSE, BC_NOARG, prev()->line);
  1879. check(compile_block_body(self, compile_stmt));
  1880. Ctx__emit_virtual(ctx(), OP_LOOP_CONTINUE, Ctx__get_loop(ctx()), BC_KEEPLINE, true);
  1881. Ctx__patch_jump(ctx(), patch);
  1882. Ctx__exit_block(ctx());
  1883. // optional else clause
  1884. if(match(TK_ELSE)) {
  1885. check(compile_block_body(self, compile_stmt));
  1886. block->end2 = ctx()->co->codes.count;
  1887. }
  1888. return NULL;
  1889. }
  1890. static Error* compile_for_loop(Compiler* self) {
  1891. Error* err;
  1892. check(EXPR_VARS(self)); // [vars]
  1893. consume(TK_IN);
  1894. check(EXPR_TUPLE(self)); // [vars, iter]
  1895. Ctx__s_emit_top(ctx()); // [vars]
  1896. Ctx__emit_(ctx(), OP_GET_ITER_NEW, BC_NOARG, BC_KEEPLINE);
  1897. CodeBlock* block = Ctx__enter_block(ctx(), CodeBlockType_FOR_LOOP);
  1898. int for_codei = Ctx__emit_(ctx(), OP_FOR_ITER, ctx()->curr_iblock, BC_KEEPLINE);
  1899. Expr* vars = Ctx__s_popx(ctx());
  1900. bool ok = vtemit_store(vars, ctx());
  1901. vtdelete(vars);
  1902. if(!ok)
  1903. return SyntaxError(); // this error occurs in `vars` instead of this line, but...nevermind
  1904. // TODO: ??
  1905. // ctx()->try_merge_for_iter_store(for_codei);
  1906. check(compile_block_body(self, compile_stmt));
  1907. Ctx__emit_virtual(ctx(), OP_LOOP_CONTINUE, Ctx__get_loop(ctx()), BC_KEEPLINE, true);
  1908. Ctx__exit_block(ctx());
  1909. // optional else clause
  1910. if(match(TK_ELSE)) {
  1911. check(compile_block_body(self, compile_stmt));
  1912. block->end2 = ctx()->co->codes.count;
  1913. }
  1914. return NULL;
  1915. }
  1916. Error* try_compile_assignment(Compiler* self, bool* is_assign) {
  1917. Error* err;
  1918. switch(curr()->type) {
  1919. case TK_IADD:
  1920. case TK_ISUB:
  1921. case TK_IMUL:
  1922. case TK_IDIV:
  1923. case TK_IFLOORDIV:
  1924. case TK_IMOD:
  1925. case TK_ILSHIFT:
  1926. case TK_IRSHIFT:
  1927. case TK_IAND:
  1928. case TK_IOR:
  1929. case TK_IXOR: {
  1930. if(Ctx__s_top(ctx())->vt->is_starred) return SyntaxError();
  1931. if(ctx()->is_compiling_class) {
  1932. return SyntaxError("can't use inplace operator in class definition");
  1933. }
  1934. advance();
  1935. // a[x] += 1; a and x should be evaluated only once
  1936. // a.x += 1; a should be evaluated only once
  1937. // -1 to remove =; inplace=true
  1938. int line = prev()->line;
  1939. TokenIndex op = (TokenIndex)(prev()->type - 1);
  1940. // [lhs]
  1941. check(EXPR_TUPLE(self)); // [lhs, rhs]
  1942. if(Ctx__s_top(ctx())->vt->is_starred) return SyntaxError();
  1943. BinaryExpr* e = BinaryExpr__new(line, op, true);
  1944. e->rhs = Ctx__s_popx(ctx()); // [lhs]
  1945. e->lhs = Ctx__s_popx(ctx()); // []
  1946. vtemit_((Expr*)e, ctx());
  1947. bool ok = vtemit_istore(e->lhs, ctx());
  1948. vtdelete((Expr*)e);
  1949. if(!ok) return SyntaxError();
  1950. *is_assign = true;
  1951. return NULL;
  1952. }
  1953. case TK_ASSIGN: {
  1954. int n = 0;
  1955. while(match(TK_ASSIGN)) {
  1956. check(EXPR_TUPLE(self));
  1957. n += 1;
  1958. }
  1959. // stack size is n+1
  1960. Ctx__s_emit_top(ctx());
  1961. for(int j = 1; j < n; j++)
  1962. Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
  1963. for(int j = 0; j < n; j++) {
  1964. if(Ctx__s_top(ctx())->vt->is_starred) return SyntaxError();
  1965. Expr* e = Ctx__s_top(ctx());
  1966. bool ok = vtemit_store(e, ctx());
  1967. Ctx__s_pop(ctx());
  1968. if(!ok) return SyntaxError();
  1969. }
  1970. *is_assign = true;
  1971. return NULL;
  1972. }
  1973. default: *is_assign = false;
  1974. }
  1975. return NULL;
  1976. }
  1977. static Error* compile_stmt(Compiler* self) {
  1978. Error* err;
  1979. if(match(TK_CLASS)) {
  1980. // check(compile_class());
  1981. assert(false);
  1982. return NULL;
  1983. }
  1984. advance();
  1985. int kw_line = prev()->line; // backup line number
  1986. int curr_loop_block = Ctx__get_loop(ctx());
  1987. switch(prev()->type) {
  1988. case TK_BREAK:
  1989. if(curr_loop_block < 0) return SyntaxError("'break' outside loop");
  1990. Ctx__emit_(ctx(), OP_LOOP_BREAK, curr_loop_block, kw_line);
  1991. consume_end_stmt();
  1992. break;
  1993. case TK_CONTINUE:
  1994. if(curr_loop_block < 0) return SyntaxError("'continue' not properly in loop");
  1995. Ctx__emit_(ctx(), OP_LOOP_CONTINUE, curr_loop_block, kw_line);
  1996. consume_end_stmt();
  1997. break;
  1998. case TK_YIELD:
  1999. if(self->contexts.count <= 1) return SyntaxError("'yield' outside function");
  2000. check(EXPR_TUPLE(self));
  2001. Ctx__s_emit_top(ctx());
  2002. Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line);
  2003. consume_end_stmt();
  2004. break;
  2005. case TK_YIELD_FROM:
  2006. if(self->contexts.count <= 1) return SyntaxError("'yield from' outside function");
  2007. check(EXPR_TUPLE(self));
  2008. Ctx__s_emit_top(ctx());
  2009. Ctx__emit_(ctx(), OP_GET_ITER_NEW, BC_NOARG, kw_line);
  2010. Ctx__enter_block(ctx(), CodeBlockType_FOR_LOOP);
  2011. Ctx__emit_(ctx(), OP_FOR_ITER_YIELD_VALUE, BC_NOARG, kw_line);
  2012. Ctx__emit_(ctx(), OP_LOOP_CONTINUE, Ctx__get_loop(ctx()), kw_line);
  2013. Ctx__exit_block(ctx());
  2014. consume_end_stmt();
  2015. break;
  2016. case TK_RETURN:
  2017. if(self->contexts.count <= 1) return SyntaxError("'return' outside function");
  2018. if(match_end_stmt(self)) {
  2019. Ctx__emit_(ctx(), OP_RETURN_VALUE, 1, kw_line);
  2020. } else {
  2021. check(EXPR_TUPLE(self));
  2022. Ctx__s_emit_top(ctx());
  2023. consume_end_stmt();
  2024. Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, kw_line);
  2025. }
  2026. break;
  2027. /*************************************************/
  2028. case TK_IF: check(compile_if_stmt(self)); break;
  2029. case TK_WHILE: check(compile_while_loop(self)); break;
  2030. case TK_FOR: check(compile_for_loop(self)); break;
  2031. // case TK_IMPORT: check(compile_normal_import()); break;
  2032. // case TK_FROM: check(compile_from_import()); break;
  2033. // case TK_DEF: check(compile_function()); break;
  2034. // case TK_DECORATOR: check(compile_decorated()); break;
  2035. // case TK_TRY: check(compile_try_except()); break;
  2036. case TK_PASS: consume_end_stmt(); break;
  2037. /*************************************************/
  2038. case TK_ASSERT: {
  2039. check(EXPR(self)); // condition
  2040. Ctx__s_emit_top(ctx());
  2041. int index = Ctx__emit_(ctx(), OP_POP_JUMP_IF_TRUE, BC_NOARG, kw_line);
  2042. int has_msg = 0;
  2043. if(match(TK_COMMA)) {
  2044. check(EXPR(self)); // message
  2045. Ctx__s_emit_top(ctx());
  2046. has_msg = 1;
  2047. }
  2048. Ctx__emit_(ctx(), OP_RAISE_ASSERT, has_msg, kw_line);
  2049. Ctx__patch_jump(ctx(), index);
  2050. consume_end_stmt();
  2051. break;
  2052. }
  2053. case TK_GLOBAL:
  2054. do {
  2055. consume(TK_ID);
  2056. py_Name name = py_name2(Token__sv(prev()));
  2057. c11_smallmap_n2i__set(&ctx()->global_names, name, 0);
  2058. } while(match(TK_COMMA));
  2059. consume_end_stmt();
  2060. break;
  2061. case TK_RAISE: {
  2062. check(EXPR(self));
  2063. Ctx__s_emit_top(ctx());
  2064. Ctx__emit_(ctx(), OP_RAISE, BC_NOARG, kw_line);
  2065. consume_end_stmt();
  2066. } break;
  2067. case TK_DEL: {
  2068. check(EXPR_TUPLE(self));
  2069. Expr* e = Ctx__s_top(ctx());
  2070. if(!vtemit_del(e, ctx())) return SyntaxError();
  2071. Ctx__s_pop(ctx());
  2072. consume_end_stmt();
  2073. } break;
  2074. // case TK_WITH: {
  2075. // check(EXPR(self)); // [ <expr> ]
  2076. // Ctx__s_emit_top(ctx());
  2077. // Ctx__enter_block(ctx(), CodeBlockType_CONTEXT_MANAGER);
  2078. // Expr* as_name = nullptr;
  2079. // if(match(TK_AS)) {
  2080. // consume(TK_ID);
  2081. // as_name = make_expr<NameExpr>(prev().str(), name_scope());
  2082. // }
  2083. // Ctx__emit_(ctx(), OP_WITH_ENTER, BC_NOARG, prev().line);
  2084. // // [ <expr> <expr>.__enter__() ]
  2085. // if(as_name) {
  2086. // bool ok = as_name->emit_store(ctx());
  2087. // delete_expr(as_name);
  2088. // if(!ok) return SyntaxError();
  2089. // } else {
  2090. // Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
  2091. // }
  2092. // check(compile_block_body());
  2093. // Ctx__emit_(ctx(), OP_WITH_EXIT, BC_NOARG, prev().line);
  2094. // Ctx__exit_block(ctx());
  2095. // } break;
  2096. /*************************************************/
  2097. case TK_EQ: {
  2098. consume(TK_ID);
  2099. if(mode() != EXEC_MODE) return SyntaxError("'label' is only available in EXEC_MODE");
  2100. c11_sv name = Token__sv(prev());
  2101. bool ok = Ctx__add_label(ctx(), py_name2(name));
  2102. if(!ok) return SyntaxError("label %q already exists", name);
  2103. consume(TK_EQ);
  2104. consume_end_stmt();
  2105. } break;
  2106. case TK_ARROW:
  2107. consume(TK_ID);
  2108. if(mode() != EXEC_MODE) return SyntaxError("'goto' is only available in EXEC_MODE");
  2109. py_Name name = py_name2(Token__sv(prev()));
  2110. Ctx__emit_(ctx(), OP_GOTO, name, prev()->line);
  2111. consume_end_stmt();
  2112. break;
  2113. /*************************************************/
  2114. // handle dangling expression or assignment
  2115. default: {
  2116. // do revert since we have pre-called advance() at the beginning
  2117. --self->i;
  2118. check(EXPR_TUPLE(self));
  2119. bool is_typed_name = false; // e.g. x: int
  2120. // eat variable's type hint if it is a single name
  2121. if(Ctx__s_top(ctx())->vt->is_name) {
  2122. if(match(TK_COLON)) {
  2123. check(consume_type_hints(self));
  2124. is_typed_name = true;
  2125. if(ctx()->is_compiling_class) {
  2126. NameExpr* ne = (NameExpr*)Ctx__s_top(ctx());
  2127. Ctx__emit_(ctx(), OP_ADD_CLASS_ANNOTATION, ne->name, BC_KEEPLINE);
  2128. }
  2129. }
  2130. }
  2131. bool is_assign = false;
  2132. check(try_compile_assignment(self, &is_assign));
  2133. if(!is_assign) {
  2134. if(Ctx__s_size(ctx()) > 0 && Ctx__s_top(ctx())->vt->is_starred) {
  2135. return SyntaxError();
  2136. }
  2137. if(!is_typed_name) {
  2138. Ctx__s_emit_top(ctx());
  2139. if((mode() == CELL_MODE || mode() == REPL_MODE) &&
  2140. name_scope(self) == NAME_GLOBAL) {
  2141. Ctx__emit_(ctx(), OP_PRINT_EXPR, BC_NOARG, BC_KEEPLINE);
  2142. } else {
  2143. Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
  2144. }
  2145. } else {
  2146. Ctx__s_pop(ctx());
  2147. }
  2148. }
  2149. consume_end_stmt();
  2150. break;
  2151. }
  2152. }
  2153. return NULL;
  2154. }
  2155. /////////////////////////////////////////////////////////////////
  2156. Error* Compiler__compile(Compiler* self, CodeObject* out) {
  2157. // make sure it is the first time to compile
  2158. assert(self->i == 0);
  2159. // make sure the first token is @sof
  2160. assert(tk(0)->type == TK_SOF);
  2161. push_global_context(self, out);
  2162. advance(); // skip @sof, so prev() is always valid
  2163. match_newlines(); // skip possible leading '\n'
  2164. Error* err;
  2165. if(mode() == EVAL_MODE) {
  2166. check(EXPR_TUPLE(self));
  2167. Ctx__s_emit_top(ctx());
  2168. consume(TK_EOF);
  2169. Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
  2170. check(pop_context(self));
  2171. return NULL;
  2172. } else if(mode() == JSON_MODE) {
  2173. check(EXPR(self));
  2174. Expr* e = Ctx__s_popx(ctx());
  2175. if(!e->vt->is_json_object) { return SyntaxError("expect a JSON object, literal or array"); }
  2176. consume(TK_EOF);
  2177. vtemit_(e, ctx());
  2178. Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
  2179. check(pop_context(self));
  2180. return NULL;
  2181. }
  2182. while(!match(TK_EOF)) {
  2183. check(compile_stmt(self));
  2184. match_newlines();
  2185. }
  2186. check(pop_context(self));
  2187. return NULL;
  2188. }
  2189. Error* pk_compile(pk_SourceData_ src, CodeObject* out) {
  2190. pk_TokenArray tokens;
  2191. Error* err = pk_Lexer__process(src, &tokens);
  2192. if(err) return err;
  2193. // Token* data = (Token*)tokens.data;
  2194. // printf("%s\n", src->filename->data);
  2195. // for(int i = 0; i < tokens.count; i++) {
  2196. // Token* t = data + i;
  2197. // c11_string* tmp = c11_string__new2(t->start, t->length);
  2198. // printf("[%d] %s: %s\n", t->line, pk_TokenSymbols[t->type], tmp->data);
  2199. // c11_string__delete(tmp);
  2200. // }
  2201. Compiler compiler;
  2202. Compiler__ctor(&compiler, src, tokens);
  2203. CodeObject__ctor(out, src, c11_string__sv(src->filename));
  2204. err = Compiler__compile(&compiler, out);
  2205. if(err) {
  2206. // dispose the code object if error occurs
  2207. CodeObject__dtor(out);
  2208. }
  2209. Compiler__dtor(&compiler);
  2210. return err;
  2211. }
  2212. // clang-format off
  2213. const static PrattRule rules[TK__COUNT__] = {
  2214. // http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
  2215. [TK_DOT] = { NULL, exprAttrib, PREC_PRIMARY },
  2216. [TK_LPAREN] = { exprGroup, exprCall, PREC_PRIMARY },
  2217. [TK_LBRACKET] = { exprList, exprSubscr, PREC_PRIMARY },
  2218. [TK_MOD] = { NULL, exprBinaryOp, PREC_FACTOR },
  2219. [TK_ADD] = { NULL, exprBinaryOp, PREC_TERM },
  2220. [TK_SUB] = { exprUnaryOp, exprBinaryOp, PREC_TERM },
  2221. [TK_MUL] = { exprUnaryOp, exprBinaryOp, PREC_FACTOR },
  2222. [TK_INVERT] = { exprUnaryOp, NULL, PREC_UNARY },
  2223. [TK_DIV] = { NULL, exprBinaryOp, PREC_FACTOR },
  2224. [TK_FLOORDIV] = { NULL, exprBinaryOp, PREC_FACTOR },
  2225. [TK_POW] = { exprUnaryOp, exprBinaryOp, PREC_EXPONENT },
  2226. [TK_GT] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2227. [TK_LT] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2228. [TK_EQ] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2229. [TK_NE] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2230. [TK_GE] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2231. [TK_LE] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2232. [TK_IN] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2233. [TK_IS] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2234. [TK_LSHIFT] = { NULL, exprBinaryOp, PREC_BITWISE_SHIFT },
  2235. [TK_RSHIFT] = { NULL, exprBinaryOp, PREC_BITWISE_SHIFT },
  2236. [TK_AND] = { NULL, exprBinaryOp, PREC_BITWISE_AND },
  2237. [TK_OR] = { NULL, exprBinaryOp, PREC_BITWISE_OR },
  2238. [TK_XOR] = { NULL, exprBinaryOp, PREC_BITWISE_XOR },
  2239. [TK_DECORATOR] = { NULL, exprBinaryOp, PREC_FACTOR },
  2240. [TK_IF] = { NULL, exprTernary, PREC_TERNARY },
  2241. [TK_NOT_IN] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2242. [TK_IS_NOT] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2243. [TK_AND_KW ] = { NULL, exprAnd, PREC_LOGICAL_AND },
  2244. [TK_OR_KW] = { NULL, exprOr, PREC_LOGICAL_OR },
  2245. [TK_NOT_KW] = { exprNot, NULL, PREC_LOGICAL_NOT },
  2246. [TK_TRUE] = { exprLiteral0 },
  2247. [TK_FALSE] = { exprLiteral0 },
  2248. [TK_NONE] = { exprLiteral0 },
  2249. [TK_DOTDOTDOT] = { exprLiteral0 },
  2250. [TK_LAMBDA] = { exprLambda, },
  2251. [TK_ID] = { exprName, },
  2252. [TK_NUM] = { exprLiteral, },
  2253. [TK_STR] = { exprLiteral, },
  2254. [TK_FSTR] = { exprFString, },
  2255. [TK_LONG] = { exprLong, },
  2256. [TK_IMAG] = { exprImag, },
  2257. [TK_BYTES] = { exprBytes, },
  2258. [TK_LBRACE] = { exprMap },
  2259. [TK_COLON] = { exprSlice0, exprSlice1, PREC_PRIMARY }
  2260. };
  2261. // clang-format on