compiler.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049
  1. #include "pocketpy/compiler/compiler.h"
  2. #include "pocketpy/common/vector.h"
  3. #include "pocketpy/common/name.h"
  4. #include "pocketpy/compiler/lexer.h"
  5. #include "pocketpy/objects/base.h"
  6. #include "pocketpy/objects/codeobject.h"
  7. #include "pocketpy/objects/sourcedata.h"
  8. #include "pocketpy/common/sstream.h"
  9. #include <assert.h>
  10. #include <stdbool.h>
  11. /* expr.h */
  12. typedef struct Expr Expr;
  13. typedef struct Ctx Ctx;
  14. typedef struct ExprVt {
  15. /* emit */
  16. void (*emit_)(Expr*, Ctx*);
  17. bool (*emit_del)(Expr*, Ctx*);
  18. bool (*emit_store)(Expr*, Ctx*);
  19. void (*emit_inplace)(Expr*, Ctx*);
  20. bool (*emit_istore)(Expr*, Ctx*);
  21. /* reflections */
  22. bool is_literal;
  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. bool is_ternary; // TernaryExpr
  30. void (*dtor)(Expr*);
  31. } ExprVt;
  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. PK_FREE(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_p*/ s_expr;
  61. c11_smallmap_n2d global_names;
  62. c11_smallmap_v2d co_consts_string_dedup_map; // this stores 0-based index instead of pointer
  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__prepare_loop_divert(Ctx* self, int line, bool is_break);
  68. static int 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 void Ctx__emit_jump(Ctx* self, int target, int line);
  76. static int Ctx__add_varname(Ctx* self, py_Name name);
  77. static int Ctx__add_name(Ctx* self, py_Name name);
  78. static int Ctx__add_const(Ctx* self, py_Ref);
  79. static int Ctx__add_const_string(Ctx* self, c11_sv);
  80. static void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line);
  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_n2d__get(&ctx->co->varnames_inv, self->name, -1);
  97. if(self->scope == NAME_LOCAL && index >= 0) {
  98. // we know this is a local variable
  99. Ctx__emit_(ctx, OP_LOAD_FAST, index, self->line);
  100. } else {
  101. Opcode op = ctx->level <= 1 ? OP_LOAD_GLOBAL : OP_LOAD_NONLOCAL;
  102. if(self->scope == NAME_GLOBAL) {
  103. if(ctx->co->src->is_dynamic) {
  104. op = OP_LOAD_NAME;
  105. } else {
  106. if(ctx->is_compiling_class) {
  107. // if we are compiling a class, we should use `OP_LOAD_CLASS_GLOBAL`
  108. // this is for @property.setter
  109. op = OP_LOAD_CLASS_GLOBAL;
  110. }
  111. }
  112. }
  113. Ctx__emit_(ctx, op, Ctx__add_name(ctx, self->name), self->line);
  114. }
  115. }
  116. bool NameExpr__emit_del(Expr* self_, Ctx* ctx) {
  117. NameExpr* self = (NameExpr*)self_;
  118. switch(self->scope) {
  119. case NAME_LOCAL:
  120. Ctx__emit_(ctx, OP_DELETE_FAST, Ctx__add_varname(ctx, self->name), self->line);
  121. break;
  122. case NAME_GLOBAL: {
  123. Opcode op = ctx->co->src->is_dynamic ? OP_DELETE_NAME : OP_DELETE_GLOBAL;
  124. Ctx__emit_(ctx, op, Ctx__add_name(ctx, self->name), self->line);
  125. break;
  126. }
  127. default: c11__unreachable();
  128. }
  129. return true;
  130. }
  131. bool NameExpr__emit_store(Expr* self_, Ctx* ctx) {
  132. NameExpr* self = (NameExpr*)self_;
  133. if(ctx->is_compiling_class) {
  134. Ctx__emit_(ctx, OP_STORE_CLASS_ATTR, Ctx__add_name(ctx, self->name), self->line);
  135. return true;
  136. }
  137. Ctx__emit_store_name(ctx, self->scope, self->name, self->line);
  138. return true;
  139. }
  140. NameExpr* NameExpr__new(int line, py_Name name, NameScope scope) {
  141. const static ExprVt Vt = {.emit_ = NameExpr__emit_,
  142. .emit_del = NameExpr__emit_del,
  143. .emit_store = NameExpr__emit_store,
  144. .is_name = true};
  145. NameExpr* self = PK_MALLOC(sizeof(NameExpr));
  146. self->vt = &Vt;
  147. self->line = line;
  148. self->name = name;
  149. self->scope = scope;
  150. return self;
  151. }
  152. typedef struct StarredExpr {
  153. EXPR_COMMON_HEADER
  154. Expr* child;
  155. int level;
  156. } StarredExpr;
  157. void StarredExpr__emit_(Expr* self_, Ctx* ctx) {
  158. StarredExpr* self = (StarredExpr*)self_;
  159. vtemit_(self->child, ctx);
  160. Ctx__emit_(ctx, OP_UNARY_STAR, self->level, self->line);
  161. }
  162. bool StarredExpr__emit_store(Expr* self_, Ctx* ctx) {
  163. StarredExpr* self = (StarredExpr*)self_;
  164. if(self->level != 1) return false;
  165. // simply proxy to child
  166. return vtemit_store(self->child, ctx);
  167. }
  168. void StarredExpr__dtor(Expr* self_) {
  169. StarredExpr* self = (StarredExpr*)self_;
  170. vtdelete(self->child);
  171. }
  172. StarredExpr* StarredExpr__new(int line, Expr* child, int level) {
  173. const static ExprVt Vt = {.emit_ = StarredExpr__emit_,
  174. .emit_store = StarredExpr__emit_store,
  175. .is_starred = true,
  176. .dtor = StarredExpr__dtor};
  177. StarredExpr* self = PK_MALLOC(sizeof(StarredExpr));
  178. self->vt = &Vt;
  179. self->line = line;
  180. self->child = child;
  181. self->level = level;
  182. return self;
  183. }
  184. // InvertExpr, NotExpr, NegatedExpr
  185. // NOTE: NegatedExpr always contains a non-const child. Should not generate -1 or -0.1
  186. typedef struct UnaryExpr {
  187. EXPR_COMMON_HEADER
  188. Expr* child;
  189. Opcode opcode;
  190. } UnaryExpr;
  191. void UnaryExpr__dtor(Expr* self_) {
  192. UnaryExpr* self = (UnaryExpr*)self_;
  193. vtdelete(self->child);
  194. }
  195. static void UnaryExpr__emit_(Expr* self_, Ctx* ctx) {
  196. UnaryExpr* self = (UnaryExpr*)self_;
  197. vtemit_(self->child, ctx);
  198. Ctx__emit_(ctx, self->opcode, BC_NOARG, self->line);
  199. }
  200. UnaryExpr* UnaryExpr__new(int line, Expr* child, Opcode opcode) {
  201. const static ExprVt Vt = {.emit_ = UnaryExpr__emit_, .dtor = UnaryExpr__dtor};
  202. UnaryExpr* self = PK_MALLOC(sizeof(UnaryExpr));
  203. self->vt = &Vt;
  204. self->line = line;
  205. self->child = child;
  206. self->opcode = opcode;
  207. return self;
  208. }
  209. typedef struct FStringSpecExpr {
  210. EXPR_COMMON_HEADER
  211. Expr* child;
  212. c11_sv spec;
  213. } FStringSpecExpr;
  214. void FStringSpecExpr__emit_(Expr* self_, Ctx* ctx) {
  215. FStringSpecExpr* self = (FStringSpecExpr*)self_;
  216. vtemit_(self->child, ctx);
  217. int index = Ctx__add_const_string(ctx, self->spec);
  218. Ctx__emit_(ctx, OP_FORMAT_STRING, index, self->line);
  219. }
  220. FStringSpecExpr* FStringSpecExpr__new(int line, Expr* child, c11_sv spec) {
  221. const static ExprVt Vt = {.emit_ = FStringSpecExpr__emit_, .dtor = UnaryExpr__dtor};
  222. FStringSpecExpr* self = PK_MALLOC(sizeof(FStringSpecExpr));
  223. self->vt = &Vt;
  224. self->line = line;
  225. self->child = child;
  226. self->spec = spec;
  227. return self;
  228. }
  229. typedef struct RawStringExpr {
  230. EXPR_COMMON_HEADER
  231. c11_sv value;
  232. Opcode opcode;
  233. } RawStringExpr;
  234. void RawStringExpr__emit_(Expr* self_, Ctx* ctx) {
  235. RawStringExpr* self = (RawStringExpr*)self_;
  236. int index = Ctx__add_const_string(ctx, self->value);
  237. Ctx__emit_(ctx, self->opcode, index, self->line);
  238. }
  239. RawStringExpr* RawStringExpr__new(int line, c11_sv value, Opcode opcode) {
  240. const static ExprVt Vt = {.emit_ = RawStringExpr__emit_};
  241. RawStringExpr* self = PK_MALLOC(sizeof(RawStringExpr));
  242. self->vt = &Vt;
  243. self->line = line;
  244. self->value = value;
  245. self->opcode = opcode;
  246. return self;
  247. }
  248. typedef struct ImagExpr {
  249. EXPR_COMMON_HEADER
  250. double value;
  251. } ImagExpr;
  252. void ImagExpr__emit_(Expr* self_, Ctx* ctx) {
  253. ImagExpr* self = (ImagExpr*)self_;
  254. py_TValue value;
  255. py_newfloat(&value, self->value);
  256. int index = Ctx__add_const(ctx, &value);
  257. Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line);
  258. Ctx__emit_(ctx, OP_BUILD_IMAG, BC_NOARG, self->line);
  259. }
  260. ImagExpr* ImagExpr__new(int line, double value) {
  261. const static ExprVt Vt = {.emit_ = ImagExpr__emit_};
  262. ImagExpr* self = PK_MALLOC(sizeof(ImagExpr));
  263. self->vt = &Vt;
  264. self->line = line;
  265. self->value = value;
  266. return self;
  267. }
  268. typedef struct LiteralExpr {
  269. EXPR_COMMON_HEADER
  270. const TokenValue* value;
  271. bool negated;
  272. } LiteralExpr;
  273. void LiteralExpr__emit_(Expr* self_, Ctx* ctx) {
  274. LiteralExpr* self = (LiteralExpr*)self_;
  275. switch(self->value->index) {
  276. case TokenValue_I64: {
  277. py_i64 val = self->value->_i64;
  278. if(self->negated) val = -val;
  279. Ctx__emit_int(ctx, val, self->line);
  280. break;
  281. }
  282. case TokenValue_F64: {
  283. py_TValue value;
  284. py_f64 val = self->value->_f64;
  285. if(self->negated) val = -val;
  286. py_newfloat(&value, val);
  287. int index = Ctx__add_const(ctx, &value);
  288. Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line);
  289. break;
  290. }
  291. case TokenValue_STR: {
  292. assert(!self->negated);
  293. c11_sv sv = c11_string__sv(self->value->_str);
  294. int index = Ctx__add_const_string(ctx, sv);
  295. Ctx__emit_(ctx, OP_LOAD_CONST, index, self->line);
  296. break;
  297. }
  298. default: c11__unreachable();
  299. }
  300. }
  301. LiteralExpr* LiteralExpr__new(int line, const TokenValue* value) {
  302. const static ExprVt Vt = {.emit_ = LiteralExpr__emit_, .is_literal = true};
  303. LiteralExpr* self = PK_MALLOC(sizeof(LiteralExpr));
  304. self->vt = &Vt;
  305. self->line = line;
  306. self->value = value;
  307. self->negated = false;
  308. return self;
  309. }
  310. typedef struct Literal0Expr {
  311. EXPR_COMMON_HEADER
  312. TokenIndex token;
  313. } Literal0Expr;
  314. void Literal0Expr__emit_(Expr* self_, Ctx* ctx) {
  315. Literal0Expr* self = (Literal0Expr*)self_;
  316. Opcode opcode;
  317. switch(self->token) {
  318. case TK_NONE: opcode = OP_LOAD_NONE; break;
  319. case TK_TRUE: opcode = OP_LOAD_TRUE; break;
  320. case TK_FALSE: opcode = OP_LOAD_FALSE; break;
  321. case TK_DOTDOTDOT: opcode = OP_LOAD_ELLIPSIS; break;
  322. default: c11__unreachable();
  323. }
  324. Ctx__emit_(ctx, opcode, BC_NOARG, self->line);
  325. }
  326. Literal0Expr* Literal0Expr__new(int line, TokenIndex token) {
  327. const static ExprVt Vt = {.emit_ = Literal0Expr__emit_};
  328. Literal0Expr* self = PK_MALLOC(sizeof(Literal0Expr));
  329. self->vt = &Vt;
  330. self->line = line;
  331. self->token = token;
  332. return self;
  333. }
  334. typedef struct LoadConstExpr {
  335. EXPR_COMMON_HEADER
  336. int index;
  337. } LoadConstExpr;
  338. void LoadConstExpr__emit_(Expr* self_, Ctx* ctx) {
  339. LoadConstExpr* self = (LoadConstExpr*)self_;
  340. Ctx__emit_(ctx, OP_LOAD_CONST, self->index, self->line);
  341. }
  342. LoadConstExpr* LoadConstExpr__new(int line, int index) {
  343. const static ExprVt Vt = {.emit_ = LoadConstExpr__emit_};
  344. LoadConstExpr* self = PK_MALLOC(sizeof(LoadConstExpr));
  345. self->vt = &Vt;
  346. self->line = line;
  347. self->index = index;
  348. return self;
  349. }
  350. typedef struct SliceExpr {
  351. EXPR_COMMON_HEADER
  352. Expr* start;
  353. Expr* stop;
  354. Expr* step;
  355. } SliceExpr;
  356. void SliceExpr__dtor(Expr* self_) {
  357. SliceExpr* self = (SliceExpr*)self_;
  358. vtdelete(self->start);
  359. vtdelete(self->stop);
  360. vtdelete(self->step);
  361. }
  362. void SliceExpr__emit_(Expr* self_, Ctx* ctx) {
  363. SliceExpr* self = (SliceExpr*)self_;
  364. if(self->start)
  365. vtemit_(self->start, ctx);
  366. else
  367. Ctx__emit_(ctx, OP_LOAD_NONE, BC_NOARG, self->line);
  368. if(self->stop)
  369. vtemit_(self->stop, ctx);
  370. else
  371. Ctx__emit_(ctx, OP_LOAD_NONE, BC_NOARG, self->line);
  372. if(self->step)
  373. vtemit_(self->step, ctx);
  374. else
  375. Ctx__emit_(ctx, OP_LOAD_NONE, BC_NOARG, self->line);
  376. Ctx__emit_(ctx, OP_BUILD_SLICE, BC_NOARG, self->line);
  377. }
  378. SliceExpr* SliceExpr__new(int line) {
  379. const static ExprVt Vt = {.dtor = SliceExpr__dtor, .emit_ = SliceExpr__emit_};
  380. SliceExpr* self = PK_MALLOC(sizeof(SliceExpr));
  381. self->vt = &Vt;
  382. self->line = line;
  383. self->start = NULL;
  384. self->stop = NULL;
  385. self->step = NULL;
  386. return self;
  387. }
  388. typedef struct DictItemExpr {
  389. EXPR_COMMON_HEADER
  390. Expr* key;
  391. Expr* value;
  392. } DictItemExpr;
  393. static void DictItemExpr__dtor(Expr* self_) {
  394. DictItemExpr* self = (DictItemExpr*)self_;
  395. vtdelete(self->key);
  396. vtdelete(self->value);
  397. }
  398. static void DictItemExpr__emit_(Expr* self_, Ctx* ctx) {
  399. DictItemExpr* self = (DictItemExpr*)self_;
  400. vtemit_(self->key, ctx);
  401. vtemit_(self->value, ctx);
  402. }
  403. static DictItemExpr* DictItemExpr__new(int line) {
  404. const static ExprVt Vt = {.dtor = DictItemExpr__dtor, .emit_ = DictItemExpr__emit_};
  405. DictItemExpr* self = PK_MALLOC(sizeof(DictItemExpr));
  406. self->vt = &Vt;
  407. self->line = line;
  408. self->key = NULL;
  409. self->value = NULL;
  410. return self;
  411. }
  412. // ListExpr, DictExpr, SetExpr, TupleExpr
  413. typedef struct SequenceExpr {
  414. EXPR_COMMON_HEADER
  415. Expr** items;
  416. int itemCount;
  417. Opcode opcode;
  418. } SequenceExpr;
  419. static void SequenceExpr__emit_(Expr* self_, Ctx* ctx) {
  420. SequenceExpr* self = (SequenceExpr*)self_;
  421. for(int i = 0; i < self->itemCount; i++) {
  422. Expr* item = self->items[i];
  423. vtemit_(item, ctx);
  424. }
  425. Ctx__emit_(ctx, self->opcode, self->itemCount, self->line);
  426. }
  427. void SequenceExpr__dtor(Expr* self_) {
  428. SequenceExpr* self = (SequenceExpr*)self_;
  429. for(int i = 0; i < self->itemCount; i++) {
  430. vtdelete(self->items[i]);
  431. }
  432. PK_FREE(self->items);
  433. }
  434. bool TupleExpr__emit_store(Expr* self_, Ctx* ctx) {
  435. SequenceExpr* self = (SequenceExpr*)self_;
  436. // TOS is an iterable
  437. // items may contain StarredExpr, we should check it
  438. int starred_i = -1;
  439. for(int i = 0; i < self->itemCount; i++) {
  440. Expr* e = self->items[i];
  441. if(e->vt->is_starred) {
  442. if(((StarredExpr*)e)->level > 0) {
  443. if(starred_i == -1)
  444. starred_i = i;
  445. else
  446. return false; // multiple StarredExpr not allowed
  447. }
  448. }
  449. }
  450. if(starred_i == -1) {
  451. Ctx__emit_(ctx, OP_UNPACK_SEQUENCE, self->itemCount, self->line);
  452. } else {
  453. // starred assignment target must be in a tuple
  454. if(self->itemCount == 1) return false;
  455. // starred assignment target must be the last one (differ from cpython)
  456. if(starred_i != self->itemCount - 1) return false;
  457. // a,*b = [1,2,3]
  458. // stack is [1,2,3] -> [1,[2,3]]
  459. Ctx__emit_(ctx, OP_UNPACK_EX, self->itemCount - 1, self->line);
  460. }
  461. // do reverse emit
  462. for(int i = self->itemCount - 1; i >= 0; i--) {
  463. Expr* e = self->items[i];
  464. bool ok = vtemit_store(e, ctx);
  465. if(!ok) return false;
  466. }
  467. return true;
  468. }
  469. bool TupleExpr__emit_del(Expr* self_, Ctx* ctx) {
  470. SequenceExpr* self = (SequenceExpr*)self_;
  471. for(int i = 0; i < self->itemCount; i++) {
  472. Expr* e = self->items[i];
  473. bool ok = vtemit_del(e, ctx);
  474. if(!ok) return false;
  475. }
  476. return true;
  477. }
  478. static SequenceExpr* SequenceExpr__new(int line, const ExprVt* vt, int count, Opcode opcode) {
  479. SequenceExpr* self = PK_MALLOC(sizeof(SequenceExpr));
  480. self->vt = vt;
  481. self->line = line;
  482. self->opcode = opcode;
  483. self->items = PK_MALLOC(sizeof(Expr*) * count);
  484. self->itemCount = count;
  485. return self;
  486. }
  487. SequenceExpr* FStringExpr__new(int line, int count) {
  488. const static ExprVt ListExprVt = {.dtor = SequenceExpr__dtor, .emit_ = SequenceExpr__emit_};
  489. return SequenceExpr__new(line, &ListExprVt, count, OP_BUILD_STRING);
  490. }
  491. SequenceExpr* ListExpr__new(int line, int count) {
  492. const static ExprVt ListExprVt = {.dtor = SequenceExpr__dtor, .emit_ = SequenceExpr__emit_};
  493. return SequenceExpr__new(line, &ListExprVt, count, OP_BUILD_LIST);
  494. }
  495. SequenceExpr* DictExpr__new(int line, int count) {
  496. const static ExprVt DictExprVt = {.dtor = SequenceExpr__dtor, .emit_ = SequenceExpr__emit_};
  497. return SequenceExpr__new(line, &DictExprVt, count, OP_BUILD_DICT);
  498. }
  499. SequenceExpr* SetExpr__new(int line, int count) {
  500. const static ExprVt SetExprVt = {
  501. .dtor = SequenceExpr__dtor,
  502. .emit_ = SequenceExpr__emit_,
  503. };
  504. return SequenceExpr__new(line, &SetExprVt, count, OP_BUILD_SET);
  505. }
  506. SequenceExpr* TupleExpr__new(int line, int count) {
  507. const static ExprVt TupleExprVt = {.dtor = SequenceExpr__dtor,
  508. .emit_ = SequenceExpr__emit_,
  509. .is_tuple = true,
  510. .emit_store = TupleExpr__emit_store,
  511. .emit_del = TupleExpr__emit_del};
  512. return SequenceExpr__new(line, &TupleExprVt, count, OP_BUILD_TUPLE);
  513. }
  514. typedef struct CompExpr {
  515. EXPR_COMMON_HEADER
  516. Expr* expr; // loop expr
  517. Expr* vars; // loop vars
  518. Expr* iter; // loop iter
  519. Expr* cond; // optional if condition
  520. Opcode op0;
  521. Opcode op1;
  522. } CompExpr;
  523. void CompExpr__dtor(Expr* self_) {
  524. CompExpr* self = (CompExpr*)self_;
  525. vtdelete(self->expr);
  526. vtdelete(self->vars);
  527. vtdelete(self->iter);
  528. vtdelete(self->cond);
  529. }
  530. void CompExpr__emit_(Expr* self_, Ctx* ctx) {
  531. CompExpr* self = (CompExpr*)self_;
  532. Ctx__emit_(ctx, self->op0, 0, self->line);
  533. vtemit_(self->iter, ctx);
  534. Ctx__emit_(ctx, OP_GET_ITER, BC_NOARG, BC_KEEPLINE);
  535. int block = Ctx__enter_block(ctx, CodeBlockType_FOR_LOOP);
  536. int block_start = Ctx__emit_(ctx, OP_FOR_ITER, block, BC_KEEPLINE);
  537. bool ok = vtemit_store(self->vars, ctx);
  538. // this error occurs in `vars` instead of this line, but...nevermind
  539. assert(ok); // this should raise a SyntaxError, but we just assert it
  540. if(self->cond) {
  541. vtemit_(self->cond, ctx);
  542. int patch = Ctx__emit_(ctx, OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
  543. vtemit_(self->expr, ctx);
  544. Ctx__emit_(ctx, self->op1, BC_NOARG, BC_KEEPLINE);
  545. Ctx__patch_jump(ctx, patch);
  546. } else {
  547. vtemit_(self->expr, ctx);
  548. Ctx__emit_(ctx, self->op1, BC_NOARG, BC_KEEPLINE);
  549. }
  550. Ctx__emit_jump(ctx, block_start, BC_KEEPLINE);
  551. Ctx__exit_block(ctx);
  552. }
  553. CompExpr* CompExpr__new(int line, Opcode op0, Opcode op1) {
  554. const static ExprVt Vt = {.dtor = CompExpr__dtor, .emit_ = CompExpr__emit_};
  555. CompExpr* self = PK_MALLOC(sizeof(CompExpr));
  556. self->vt = &Vt;
  557. self->line = line;
  558. self->op0 = op0;
  559. self->op1 = op1;
  560. self->expr = NULL;
  561. self->vars = NULL;
  562. self->iter = NULL;
  563. self->cond = NULL;
  564. return self;
  565. }
  566. typedef struct LambdaExpr {
  567. EXPR_COMMON_HEADER
  568. int index;
  569. } LambdaExpr;
  570. static void LambdaExpr__emit_(Expr* self_, Ctx* ctx) {
  571. LambdaExpr* self = (LambdaExpr*)self_;
  572. Ctx__emit_(ctx, OP_LOAD_FUNCTION, self->index, self->line);
  573. }
  574. LambdaExpr* LambdaExpr__new(int line, int index) {
  575. const static ExprVt Vt = {.emit_ = LambdaExpr__emit_};
  576. LambdaExpr* self = PK_MALLOC(sizeof(LambdaExpr));
  577. self->vt = &Vt;
  578. self->line = line;
  579. self->index = index;
  580. return self;
  581. }
  582. // AndExpr, OrExpr
  583. typedef struct LogicBinaryExpr {
  584. EXPR_COMMON_HEADER
  585. Expr* lhs;
  586. Expr* rhs;
  587. Opcode opcode;
  588. } LogicBinaryExpr;
  589. void LogicBinaryExpr__dtor(Expr* self_) {
  590. LogicBinaryExpr* self = (LogicBinaryExpr*)self_;
  591. vtdelete(self->lhs);
  592. vtdelete(self->rhs);
  593. }
  594. void LogicBinaryExpr__emit_(Expr* self_, Ctx* ctx) {
  595. LogicBinaryExpr* self = (LogicBinaryExpr*)self_;
  596. vtemit_(self->lhs, ctx);
  597. int patch = Ctx__emit_(ctx, self->opcode, BC_NOARG, self->line);
  598. vtemit_(self->rhs, ctx);
  599. Ctx__patch_jump(ctx, patch);
  600. }
  601. LogicBinaryExpr* LogicBinaryExpr__new(int line, Opcode opcode) {
  602. const static ExprVt Vt = {.emit_ = LogicBinaryExpr__emit_, .dtor = LogicBinaryExpr__dtor};
  603. LogicBinaryExpr* self = PK_MALLOC(sizeof(LogicBinaryExpr));
  604. self->vt = &Vt;
  605. self->line = line;
  606. self->lhs = NULL;
  607. self->rhs = NULL;
  608. self->opcode = opcode;
  609. return self;
  610. }
  611. typedef struct GroupedExpr {
  612. EXPR_COMMON_HEADER
  613. Expr* child;
  614. } GroupedExpr;
  615. void GroupedExpr__dtor(Expr* self_) {
  616. GroupedExpr* self = (GroupedExpr*)self_;
  617. vtdelete(self->child);
  618. }
  619. void GroupedExpr__emit_(Expr* self_, Ctx* ctx) {
  620. GroupedExpr* self = (GroupedExpr*)self_;
  621. vtemit_(self->child, ctx);
  622. }
  623. bool GroupedExpr__emit_del(Expr* self_, Ctx* ctx) {
  624. GroupedExpr* self = (GroupedExpr*)self_;
  625. return vtemit_del(self->child, ctx);
  626. }
  627. bool GroupedExpr__emit_store(Expr* self_, Ctx* ctx) {
  628. GroupedExpr* self = (GroupedExpr*)self_;
  629. return vtemit_store(self->child, ctx);
  630. }
  631. GroupedExpr* GroupedExpr__new(int line, Expr* child) {
  632. const static ExprVt Vt = {.dtor = GroupedExpr__dtor,
  633. .emit_ = GroupedExpr__emit_,
  634. .emit_del = GroupedExpr__emit_del,
  635. .emit_store = GroupedExpr__emit_store};
  636. GroupedExpr* self = PK_MALLOC(sizeof(GroupedExpr));
  637. self->vt = &Vt;
  638. self->line = line;
  639. self->child = child;
  640. return self;
  641. }
  642. typedef struct BinaryExpr {
  643. EXPR_COMMON_HEADER
  644. Expr* lhs;
  645. Expr* rhs;
  646. TokenIndex op;
  647. bool inplace;
  648. } BinaryExpr;
  649. static void BinaryExpr__dtor(Expr* self_) {
  650. BinaryExpr* self = (BinaryExpr*)self_;
  651. vtdelete(self->lhs);
  652. vtdelete(self->rhs);
  653. }
  654. static Opcode cmp_token2op(TokenIndex token) {
  655. switch(token) {
  656. case TK_LT: return OP_COMPARE_LT;
  657. case TK_LE: return OP_COMPARE_LE;
  658. case TK_EQ: return OP_COMPARE_EQ;
  659. case TK_NE: return OP_COMPARE_NE;
  660. case TK_GT: return OP_COMPARE_GT;
  661. case TK_GE: return OP_COMPARE_GE;
  662. default: return 0;
  663. }
  664. }
  665. #define is_compare_expr(e) ((e)->vt->is_binary && cmp_token2op(((BinaryExpr*)(e))->op))
  666. static void _emit_compare(BinaryExpr* self, Ctx* ctx, c11_vector* jmps) {
  667. if(is_compare_expr(self->lhs)) {
  668. _emit_compare((BinaryExpr*)self->lhs, ctx, jmps);
  669. } else {
  670. vtemit_(self->lhs, ctx); // [a]
  671. }
  672. vtemit_(self->rhs, ctx); // [a, b]
  673. Ctx__emit_(ctx, OP_DUP_TOP, BC_NOARG, self->line); // [a, b, b]
  674. Ctx__emit_(ctx, OP_ROT_THREE, BC_NOARG, self->line); // [b, a, b]
  675. Ctx__emit_(ctx, cmp_token2op(self->op), BC_NOARG, self->line);
  676. // [b, RES]
  677. int index = Ctx__emit_(ctx, OP_SHORTCUT_IF_FALSE_OR_POP, BC_NOARG, self->line);
  678. c11_vector__push(int, jmps, index);
  679. }
  680. static void BinaryExpr__emit_(Expr* self_, Ctx* ctx) {
  681. BinaryExpr* self = (BinaryExpr*)self_;
  682. c11_vector /*T=int*/ jmps;
  683. c11_vector__ctor(&jmps, sizeof(int));
  684. if(cmp_token2op(self->op) && is_compare_expr(self->lhs)) {
  685. // (a < b) < c
  686. BinaryExpr* e = (BinaryExpr*)self->lhs;
  687. _emit_compare(e, ctx, &jmps);
  688. // [b, RES]
  689. } else {
  690. // (1 + 2) < c
  691. if(self->inplace) {
  692. vtemit_inplace(self->lhs, ctx);
  693. } else {
  694. vtemit_(self->lhs, ctx);
  695. }
  696. }
  697. vtemit_(self->rhs, ctx);
  698. Opcode opcode;
  699. uint16_t arg = BC_NOARG;
  700. switch(self->op) {
  701. case TK_ADD: opcode = OP_BINARY_ADD; break;
  702. case TK_SUB: opcode = OP_BINARY_SUB; break;
  703. case TK_MUL: opcode = OP_BINARY_MUL; break;
  704. case TK_DIV: opcode = OP_BINARY_TRUEDIV; break;
  705. case TK_FLOORDIV: opcode = OP_BINARY_FLOORDIV; break;
  706. case TK_MOD: opcode = OP_BINARY_MOD; break;
  707. case TK_POW: opcode = OP_BINARY_POW; break;
  708. case TK_LT: opcode = OP_COMPARE_LT; break;
  709. case TK_LE: opcode = OP_COMPARE_LE; break;
  710. case TK_EQ: opcode = OP_COMPARE_EQ; break;
  711. case TK_NE: opcode = OP_COMPARE_NE; break;
  712. case TK_GT: opcode = OP_COMPARE_GT; break;
  713. case TK_GE: opcode = OP_COMPARE_GE; break;
  714. case TK_IN:
  715. opcode = OP_CONTAINS_OP;
  716. arg = 0;
  717. break;
  718. case TK_NOT_IN:
  719. opcode = OP_CONTAINS_OP;
  720. arg = 1;
  721. break;
  722. case TK_IS:
  723. opcode = OP_IS_OP;
  724. arg = 0;
  725. break;
  726. case TK_IS_NOT:
  727. opcode = OP_IS_OP;
  728. arg = 1;
  729. break;
  730. case TK_LSHIFT: opcode = OP_BINARY_LSHIFT; break;
  731. case TK_RSHIFT: opcode = OP_BINARY_RSHIFT; break;
  732. case TK_AND: opcode = OP_BINARY_AND; break;
  733. case TK_OR: opcode = OP_BINARY_OR; break;
  734. case TK_XOR: opcode = OP_BINARY_XOR; break;
  735. case TK_DECORATOR: opcode = OP_BINARY_MATMUL; break;
  736. default: c11__unreachable();
  737. }
  738. Ctx__emit_(ctx, opcode, arg, self->line);
  739. for(int i = 0; i < jmps.length; i++) {
  740. Ctx__patch_jump(ctx, c11__getitem(int, &jmps, i));
  741. }
  742. c11_vector__dtor(&jmps);
  743. }
  744. BinaryExpr* BinaryExpr__new(int line, TokenIndex op, bool inplace) {
  745. const static ExprVt Vt = {.emit_ = BinaryExpr__emit_,
  746. .dtor = BinaryExpr__dtor,
  747. .is_binary = true};
  748. BinaryExpr* self = PK_MALLOC(sizeof(BinaryExpr));
  749. self->vt = &Vt;
  750. self->line = line;
  751. self->lhs = NULL;
  752. self->rhs = NULL;
  753. self->op = op;
  754. self->inplace = inplace;
  755. return self;
  756. }
  757. typedef struct TernaryExpr {
  758. EXPR_COMMON_HEADER
  759. Expr* cond;
  760. Expr* true_expr;
  761. Expr* false_expr;
  762. } TernaryExpr;
  763. void TernaryExpr__dtor(Expr* self_) {
  764. TernaryExpr* self = (TernaryExpr*)self_;
  765. vtdelete(self->cond);
  766. vtdelete(self->true_expr);
  767. vtdelete(self->false_expr);
  768. }
  769. void TernaryExpr__emit_(Expr* self_, Ctx* ctx) {
  770. TernaryExpr* self = (TernaryExpr*)self_;
  771. vtemit_(self->cond, ctx);
  772. int patch = Ctx__emit_(ctx, OP_POP_JUMP_IF_FALSE, BC_NOARG, self->cond->line);
  773. vtemit_(self->true_expr, ctx);
  774. int patch_2 = Ctx__emit_(ctx, OP_JUMP_FORWARD, BC_NOARG, self->true_expr->line);
  775. Ctx__patch_jump(ctx, patch);
  776. vtemit_(self->false_expr, ctx);
  777. Ctx__patch_jump(ctx, patch_2);
  778. }
  779. TernaryExpr* TernaryExpr__new(int line) {
  780. const static ExprVt Vt = {
  781. .dtor = TernaryExpr__dtor,
  782. .emit_ = TernaryExpr__emit_,
  783. .is_ternary = true,
  784. };
  785. TernaryExpr* self = PK_MALLOC(sizeof(TernaryExpr));
  786. self->vt = &Vt;
  787. self->line = line;
  788. self->cond = NULL;
  789. self->true_expr = NULL;
  790. self->false_expr = NULL;
  791. return self;
  792. }
  793. typedef struct SubscrExpr {
  794. EXPR_COMMON_HEADER
  795. Expr* lhs;
  796. Expr* rhs;
  797. } SubscrExpr;
  798. void SubscrExpr__dtor(Expr* self_) {
  799. SubscrExpr* self = (SubscrExpr*)self_;
  800. vtdelete(self->lhs);
  801. vtdelete(self->rhs);
  802. }
  803. void SubscrExpr__emit_(Expr* self_, Ctx* ctx) {
  804. SubscrExpr* self = (SubscrExpr*)self_;
  805. vtemit_(self->lhs, ctx);
  806. vtemit_(self->rhs, ctx);
  807. Ctx__emit_(ctx, OP_LOAD_SUBSCR, BC_NOARG, self->line);
  808. }
  809. bool SubscrExpr__emit_store(Expr* self_, Ctx* ctx) {
  810. SubscrExpr* self = (SubscrExpr*)self_;
  811. vtemit_(self->lhs, ctx);
  812. vtemit_(self->rhs, ctx);
  813. Ctx__emit_(ctx, OP_STORE_SUBSCR, BC_NOARG, self->line);
  814. return true;
  815. }
  816. void SubscrExpr__emit_inplace(Expr* self_, Ctx* ctx) {
  817. SubscrExpr* self = (SubscrExpr*)self_;
  818. vtemit_(self->lhs, ctx);
  819. vtemit_(self->rhs, ctx);
  820. Ctx__emit_(ctx, OP_DUP_TOP_TWO, BC_NOARG, self->line);
  821. Ctx__emit_(ctx, OP_LOAD_SUBSCR, BC_NOARG, self->line);
  822. }
  823. bool SubscrExpr__emit_istore(Expr* self_, Ctx* ctx) {
  824. SubscrExpr* self = (SubscrExpr*)self_;
  825. // [a, b, val] -> [val, a, b]
  826. Ctx__emit_(ctx, OP_ROT_THREE, BC_NOARG, self->line);
  827. Ctx__emit_(ctx, OP_STORE_SUBSCR, BC_NOARG, self->line);
  828. return true;
  829. }
  830. bool SubscrExpr__emit_del(Expr* self_, Ctx* ctx) {
  831. SubscrExpr* self = (SubscrExpr*)self_;
  832. vtemit_(self->lhs, ctx);
  833. vtemit_(self->rhs, ctx);
  834. Ctx__emit_(ctx, OP_DELETE_SUBSCR, BC_NOARG, self->line);
  835. return true;
  836. }
  837. SubscrExpr* SubscrExpr__new(int line) {
  838. const static ExprVt Vt = {
  839. .dtor = SubscrExpr__dtor,
  840. .emit_ = SubscrExpr__emit_,
  841. .emit_store = SubscrExpr__emit_store,
  842. .emit_inplace = SubscrExpr__emit_inplace,
  843. .emit_istore = SubscrExpr__emit_istore,
  844. .emit_del = SubscrExpr__emit_del,
  845. .is_subscr = true,
  846. };
  847. SubscrExpr* self = PK_MALLOC(sizeof(SubscrExpr));
  848. self->vt = &Vt;
  849. self->line = line;
  850. self->lhs = NULL;
  851. self->rhs = NULL;
  852. return self;
  853. }
  854. typedef struct AttribExpr {
  855. EXPR_COMMON_HEADER
  856. Expr* child;
  857. py_Name name;
  858. } AttribExpr;
  859. void AttribExpr__dtor(Expr* self_) {
  860. AttribExpr* self = (AttribExpr*)self_;
  861. vtdelete(self->child);
  862. }
  863. void AttribExpr__emit_(Expr* self_, Ctx* ctx) {
  864. AttribExpr* self = (AttribExpr*)self_;
  865. vtemit_(self->child, ctx);
  866. Ctx__emit_(ctx, OP_LOAD_ATTR, Ctx__add_name(ctx, self->name), self->line);
  867. }
  868. bool AttribExpr__emit_del(Expr* self_, Ctx* ctx) {
  869. AttribExpr* self = (AttribExpr*)self_;
  870. vtemit_(self->child, ctx);
  871. Ctx__emit_(ctx, OP_DELETE_ATTR, Ctx__add_name(ctx, self->name), self->line);
  872. return true;
  873. }
  874. bool AttribExpr__emit_store(Expr* self_, Ctx* ctx) {
  875. AttribExpr* self = (AttribExpr*)self_;
  876. vtemit_(self->child, ctx);
  877. Ctx__emit_(ctx, OP_STORE_ATTR, Ctx__add_name(ctx, self->name), self->line);
  878. return true;
  879. }
  880. void AttribExpr__emit_inplace(Expr* self_, Ctx* ctx) {
  881. AttribExpr* self = (AttribExpr*)self_;
  882. vtemit_(self->child, ctx);
  883. Ctx__emit_(ctx, OP_DUP_TOP, BC_NOARG, self->line);
  884. Ctx__emit_(ctx, OP_LOAD_ATTR, Ctx__add_name(ctx, self->name), self->line);
  885. }
  886. bool AttribExpr__emit_istore(Expr* self_, Ctx* ctx) {
  887. // [a, val] -> [val, a]
  888. AttribExpr* self = (AttribExpr*)self_;
  889. Ctx__emit_(ctx, OP_ROT_TWO, BC_NOARG, self->line);
  890. Ctx__emit_(ctx, OP_STORE_ATTR, Ctx__add_name(ctx, self->name), self->line);
  891. return true;
  892. }
  893. AttribExpr* AttribExpr__new(int line, Expr* child, py_Name name) {
  894. const static ExprVt Vt = {.emit_ = AttribExpr__emit_,
  895. .emit_del = AttribExpr__emit_del,
  896. .emit_store = AttribExpr__emit_store,
  897. .emit_inplace = AttribExpr__emit_inplace,
  898. .emit_istore = AttribExpr__emit_istore,
  899. .dtor = AttribExpr__dtor,
  900. .is_attrib = true};
  901. AttribExpr* self = PK_MALLOC(sizeof(AttribExpr));
  902. self->vt = &Vt;
  903. self->line = line;
  904. self->child = child;
  905. self->name = name;
  906. return self;
  907. }
  908. typedef struct CallExprKwArg {
  909. py_Name key;
  910. Expr* val;
  911. } CallExprKwArg;
  912. typedef struct CallExpr {
  913. EXPR_COMMON_HEADER
  914. Expr* callable;
  915. c11_vector /*T=Expr* */ args;
  916. // **a will be interpreted as a special keyword argument: {{0}: a}
  917. c11_vector /*T=CallExprKwArg */ kwargs;
  918. } CallExpr;
  919. void CallExpr__dtor(Expr* self_) {
  920. CallExpr* self = (CallExpr*)self_;
  921. vtdelete(self->callable);
  922. c11__foreach(Expr*, &self->args, e) vtdelete(*e);
  923. c11__foreach(CallExprKwArg, &self->kwargs, e) vtdelete(e->val);
  924. c11_vector__dtor(&self->args);
  925. c11_vector__dtor(&self->kwargs);
  926. }
  927. void CallExpr__emit_(Expr* self_, Ctx* ctx) {
  928. CallExpr* self = (CallExpr*)self_;
  929. bool vargs = false; // whether there is *args as input
  930. bool vkwargs = false; // whether there is **kwargs as input
  931. c11__foreach(Expr*, &self->args, e) {
  932. if((*e)->vt->is_starred) vargs = true;
  933. }
  934. c11__foreach(CallExprKwArg, &self->kwargs, e) {
  935. if(e->val->vt->is_starred) vkwargs = true;
  936. }
  937. // if callable is a AttrExpr, we should try to use `fast_call` instead of use `boundmethod`
  938. if(self->callable->vt->is_attrib) {
  939. AttribExpr* p = (AttribExpr*)self->callable;
  940. vtemit_(p->child, ctx);
  941. Ctx__emit_(ctx, OP_LOAD_METHOD, Ctx__add_name(ctx, p->name), p->line);
  942. } else {
  943. vtemit_(self->callable, ctx);
  944. Ctx__emit_(ctx, OP_LOAD_NULL, BC_NOARG, BC_KEEPLINE);
  945. }
  946. Opcode opcode = OP_CALL;
  947. if(vargs || vkwargs) {
  948. // in this case, there is at least one *args or **kwargs as StarredExpr
  949. // OP_CALL_VARGS needs to unpack them via vectorcall_buffer
  950. opcode = OP_CALL_VARGS;
  951. }
  952. c11__foreach(Expr*, &self->args, e) { vtemit_(*e, ctx); }
  953. c11__foreach(CallExprKwArg, &self->kwargs, e) {
  954. Ctx__emit_int(ctx, (uintptr_t)e->key, self->line);
  955. vtemit_(e->val, ctx);
  956. }
  957. int KWARGC = self->kwargs.length;
  958. int ARGC = self->args.length;
  959. assert(KWARGC < 256 && ARGC < 256);
  960. Ctx__emit_(ctx, opcode, (KWARGC << 8) | ARGC, self->line);
  961. }
  962. CallExpr* CallExpr__new(int line, Expr* callable) {
  963. const static ExprVt Vt = {.dtor = CallExpr__dtor, .emit_ = CallExpr__emit_};
  964. CallExpr* self = PK_MALLOC(sizeof(CallExpr));
  965. self->vt = &Vt;
  966. self->line = line;
  967. self->callable = callable;
  968. c11_vector__ctor(&self->args, sizeof(Expr*));
  969. c11_vector__ctor(&self->kwargs, sizeof(CallExprKwArg));
  970. return self;
  971. }
  972. /* context.c */
  973. static void Ctx__ctor(Ctx* self, CodeObject* co, FuncDecl* func, int level) {
  974. self->co = co;
  975. self->func = func;
  976. self->level = level;
  977. self->curr_iblock = 0;
  978. self->is_compiling_class = false;
  979. c11_vector__ctor(&self->s_expr, sizeof(Expr*));
  980. c11_smallmap_n2d__ctor(&self->global_names);
  981. c11_smallmap_v2d__ctor(&self->co_consts_string_dedup_map);
  982. }
  983. static void Ctx__dtor(Ctx* self) {
  984. // clean the expr stack
  985. for(int i = 0; i < self->s_expr.length; i++) {
  986. vtdelete(c11__getitem(Expr*, &self->s_expr, i));
  987. }
  988. c11_vector__dtor(&self->s_expr);
  989. c11_smallmap_n2d__dtor(&self->global_names);
  990. // free the dedup map
  991. c11__foreach(c11_smallmap_v2d_KV, &self->co_consts_string_dedup_map, p_kv) {
  992. const char* p = p_kv->key.data;
  993. PK_FREE((void*)p);
  994. }
  995. c11_smallmap_v2d__dtor(&self->co_consts_string_dedup_map);
  996. }
  997. static int Ctx__prepare_loop_divert(Ctx* self, int line, bool is_break) {
  998. int index = self->curr_iblock;
  999. while(index >= 0) {
  1000. CodeBlock* block = c11__at(CodeBlock, &self->co->blocks, index);
  1001. switch(block->type) {
  1002. case CodeBlockType_WHILE_LOOP: return index;
  1003. case CodeBlockType_FOR_LOOP: {
  1004. if(is_break) Ctx__emit_(self, OP_POP_TOP, BC_NOARG, line);
  1005. return index;
  1006. }
  1007. case CodeBlockType_WITH: {
  1008. Ctx__emit_(self, OP_POP_TOP, BC_NOARG, line);
  1009. break;
  1010. }
  1011. case CodeBlockType_EXCEPT: {
  1012. Ctx__emit_(self, OP_END_EXC_HANDLING, 1, line);
  1013. break;
  1014. }
  1015. case CodeBlockType_FINALLY: {
  1016. Ctx__emit_(self, OP_END_FINALLY, 1, line);
  1017. break;
  1018. }
  1019. default: break;
  1020. }
  1021. index = block->parent;
  1022. }
  1023. return index;
  1024. }
  1025. static int Ctx__enter_block(Ctx* self, CodeBlockType type) {
  1026. CodeBlock block = {type, self->curr_iblock, self->co->codes.length, -1, -1};
  1027. c11_vector__push(CodeBlock, &self->co->blocks, block);
  1028. self->curr_iblock = self->co->blocks.length - 1;
  1029. return self->curr_iblock;
  1030. }
  1031. static void Ctx__exit_block(Ctx* self) {
  1032. CodeBlock* block = c11__at(CodeBlock, &self->co->blocks, self->curr_iblock);
  1033. block->end = self->co->codes.length;
  1034. self->curr_iblock = block->parent;
  1035. assert(self->curr_iblock >= 0);
  1036. }
  1037. static void Ctx__s_emit_decorators(Ctx* self, int count) {
  1038. if(count == 0) return;
  1039. assert(Ctx__s_size(self) >= count);
  1040. // [obj]
  1041. for(int i = 0; i < count; i++) {
  1042. Expr* deco = Ctx__s_popx(self);
  1043. vtemit_(deco, self); // [obj, f]
  1044. Ctx__emit_(self, OP_ROT_TWO, BC_NOARG, deco->line); // [f, obj]
  1045. Ctx__emit_(self, OP_LOAD_NULL, BC_NOARG, BC_KEEPLINE); // [f, obj, NULL]
  1046. Ctx__emit_(self, OP_ROT_TWO, BC_NOARG, BC_KEEPLINE); // [obj, NULL, f]
  1047. Ctx__emit_(self, OP_CALL, 1, deco->line); // [obj]
  1048. vtdelete(deco);
  1049. }
  1050. }
  1051. static int Ctx__emit_virtual(Ctx* self, Opcode opcode, uint16_t arg, int line, bool is_virtual) {
  1052. Bytecode bc = {(uint8_t)opcode, arg};
  1053. BytecodeEx bcx = {line, is_virtual, self->curr_iblock};
  1054. c11_vector__push(Bytecode, &self->co->codes, bc);
  1055. c11_vector__push(BytecodeEx, &self->co->codes_ex, bcx);
  1056. int i = self->co->codes.length - 1;
  1057. BytecodeEx* codes_ex = (BytecodeEx*)self->co->codes_ex.data;
  1058. if(line == BC_KEEPLINE) { codes_ex[i].lineno = i >= 1 ? codes_ex[i - 1].lineno : 1; }
  1059. return i;
  1060. }
  1061. static int Ctx__emit_(Ctx* self, Opcode opcode, uint16_t arg, int line) {
  1062. return Ctx__emit_virtual(self, opcode, arg, line, false);
  1063. }
  1064. // static void Ctx__revert_last_emit_(Ctx* self) {
  1065. // c11_vector__pop(&self->co->codes);
  1066. // c11_vector__pop(&self->co->codes_ex);
  1067. // }
  1068. static int Ctx__emit_int(Ctx* self, int64_t value, int line) {
  1069. if(INT16_MIN <= value && value <= INT16_MAX) {
  1070. return Ctx__emit_(self, OP_LOAD_SMALL_INT, (uint16_t)value, line);
  1071. } else {
  1072. py_TValue tmp;
  1073. py_newint(&tmp, value);
  1074. return Ctx__emit_(self, OP_LOAD_CONST, Ctx__add_const(self, &tmp), line);
  1075. }
  1076. }
  1077. static void Ctx__patch_jump(Ctx* self, int index) {
  1078. Bytecode* co_codes = (Bytecode*)self->co->codes.data;
  1079. int target = self->co->codes.length;
  1080. Bytecode__set_signed_arg(&co_codes[index], target - index);
  1081. }
  1082. static void Ctx__emit_jump(Ctx* self, int target, int line) {
  1083. int index = Ctx__emit_(self, OP_JUMP_FORWARD, BC_NOARG, line);
  1084. // should place after Ctx__emit_ because of realloc
  1085. Bytecode* co_codes = (Bytecode*)self->co->codes.data;
  1086. Bytecode__set_signed_arg(&co_codes[index], target - index);
  1087. }
  1088. static int Ctx__add_varname(Ctx* self, py_Name name) {
  1089. // PK_MAX_CO_VARNAMES will be checked when pop_context(), not here
  1090. return CodeObject__add_varname(self->co, name);
  1091. }
  1092. static int Ctx__add_name(Ctx* self, py_Name name) { return CodeObject__add_name(self->co, name); }
  1093. static int Ctx__add_const_string(Ctx* self, c11_sv key) {
  1094. if(key.size > 100) {
  1095. py_Ref tmp = c11_vector__emplace(&self->co->consts);
  1096. py_newstrv(tmp, key);
  1097. int index = self->co->consts.length - 1;
  1098. return index;
  1099. }
  1100. int* val = c11_smallmap_v2d__try_get(&self->co_consts_string_dedup_map, key);
  1101. if(val) {
  1102. return *val;
  1103. } else {
  1104. py_Ref tmp = c11_vector__emplace(&self->co->consts);
  1105. py_newstrv(tmp, key);
  1106. int index = self->co->consts.length - 1;
  1107. // dedup
  1108. char* new_buf = PK_MALLOC(key.size + 1);
  1109. memcpy(new_buf, key.data, key.size);
  1110. new_buf[key.size] = 0;
  1111. c11_smallmap_v2d__set(&self->co_consts_string_dedup_map,
  1112. (c11_sv){new_buf, key.size},
  1113. index);
  1114. return index;
  1115. }
  1116. }
  1117. static int Ctx__add_const(Ctx* self, py_Ref v) {
  1118. assert(v->type != tp_str);
  1119. c11_vector__push(py_TValue, &self->co->consts, *v);
  1120. return self->co->consts.length - 1;
  1121. }
  1122. static void Ctx__emit_store_name(Ctx* self, NameScope scope, py_Name name, int line) {
  1123. switch(scope) {
  1124. case NAME_LOCAL: Ctx__emit_(self, OP_STORE_FAST, Ctx__add_varname(self, name), line); break;
  1125. case NAME_GLOBAL: {
  1126. Opcode op = self->co->src->is_dynamic ? OP_STORE_NAME : OP_STORE_GLOBAL;
  1127. Ctx__emit_(self, op, Ctx__add_name(self, name), line);
  1128. } break;
  1129. default: c11__unreachable();
  1130. }
  1131. }
  1132. // emit top -> pop -> delete
  1133. static void Ctx__s_emit_top(Ctx* self) {
  1134. assert(self->s_expr.length);
  1135. Expr* top = c11_vector__back(Expr*, &self->s_expr);
  1136. vtemit_(top, self);
  1137. vtdelete(top);
  1138. c11_vector__pop(&self->s_expr);
  1139. }
  1140. // push
  1141. static void Ctx__s_push(Ctx* self, Expr* expr) { c11_vector__push(Expr*, &self->s_expr, expr); }
  1142. // top
  1143. static Expr* Ctx__s_top(Ctx* self) {
  1144. assert(self->s_expr.length);
  1145. return c11_vector__back(Expr*, &self->s_expr);
  1146. }
  1147. // size
  1148. static int Ctx__s_size(Ctx* self) { return self->s_expr.length; }
  1149. // pop -> delete
  1150. static void Ctx__s_pop(Ctx* self) {
  1151. assert(self->s_expr.length);
  1152. Expr* top = c11_vector__back(Expr*, &self->s_expr);
  1153. vtdelete(top);
  1154. c11_vector__pop(&self->s_expr);
  1155. }
  1156. // pop move
  1157. static Expr* Ctx__s_popx(Ctx* self) {
  1158. assert(self->s_expr.length);
  1159. Expr* top = c11_vector__back(Expr*, &self->s_expr);
  1160. c11_vector__pop(&self->s_expr);
  1161. return top;
  1162. }
  1163. /* compiler.c */
  1164. typedef struct Compiler Compiler;
  1165. typedef Error* (*PrattCallback)(Compiler* self);
  1166. typedef struct PrattRule {
  1167. PrattCallback prefix;
  1168. PrattCallback infix;
  1169. enum Precedence precedence;
  1170. } PrattRule;
  1171. const static PrattRule rules[TK__COUNT__];
  1172. typedef struct Compiler {
  1173. SourceData_ src; // weakref
  1174. Token* tokens;
  1175. int tokens_length;
  1176. int i; // current token index
  1177. c11_vector /*T=CodeEmitContext*/ contexts;
  1178. } Compiler;
  1179. static void Compiler__ctor(Compiler* self, SourceData_ src, Token* tokens, int tokens_length) {
  1180. self->src = src;
  1181. self->tokens = tokens;
  1182. self->tokens_length = tokens_length;
  1183. self->i = 0;
  1184. c11_vector__ctor(&self->contexts, sizeof(Ctx));
  1185. }
  1186. static void Compiler__dtor(Compiler* self) {
  1187. // free tokens
  1188. for(int i = 0; i < self->tokens_length; i++) {
  1189. if(self->tokens[i].value.index == TokenValue_STR) {
  1190. // PK_FREE internal string
  1191. c11_string__delete(self->tokens[i].value._str);
  1192. }
  1193. }
  1194. PK_FREE(self->tokens);
  1195. // free contexts
  1196. c11__foreach(Ctx, &self->contexts, ctx) Ctx__dtor(ctx);
  1197. c11_vector__dtor(&self->contexts);
  1198. }
  1199. /**************************************/
  1200. #define tk(i) (&self->tokens[i])
  1201. #define prev() (&self->tokens[self->i - 1])
  1202. #define curr() (&self->tokens[self->i])
  1203. #define next() (&self->tokens[self->i + 1])
  1204. #define advance() self->i++
  1205. #define mode() self->src->mode
  1206. #define ctx() (&c11_vector__back(Ctx, &self->contexts))
  1207. #define match_newlines() match_newlines_impl(self)
  1208. #define consume(expected) \
  1209. if(!match(expected)) \
  1210. return SyntaxError(self, \
  1211. "expected '%s', got '%s'", \
  1212. TokenSymbols[expected], \
  1213. TokenSymbols[curr()->type]);
  1214. #define consume_end_stmt() \
  1215. if(!match_end_stmt(self)) return SyntaxError(self, "expected statement end")
  1216. #define check(B) \
  1217. if((err = B)) return err
  1218. static NameScope name_scope(Compiler* self) {
  1219. return self->contexts.length > 1 ? NAME_LOCAL : NAME_GLOBAL;
  1220. }
  1221. Error* SyntaxError(Compiler* self, const char* fmt, ...) {
  1222. Error* err = PK_MALLOC(sizeof(Error));
  1223. err->src = self->src;
  1224. PK_INCREF(self->src);
  1225. Token* t = self->i == self->tokens_length ? prev() : curr();
  1226. err->lineno = t->line;
  1227. va_list args;
  1228. va_start(args, fmt);
  1229. vsnprintf(err->msg, sizeof(err->msg), fmt, args);
  1230. va_end(args);
  1231. return err;
  1232. }
  1233. /* Matchers */
  1234. static bool is_expression(Compiler* self, bool allow_slice) {
  1235. PrattCallback prefix = rules[curr()->type].prefix;
  1236. return prefix && (allow_slice || curr()->type != TK_COLON);
  1237. }
  1238. #define match(expected) (curr()->type == expected ? (++self->i) : 0)
  1239. static bool match_id_by_str(Compiler* self, const char* name) {
  1240. if(curr()->type == TK_ID) {
  1241. bool ok = c11__sveq2(Token__sv(curr()), name);
  1242. if(ok) advance();
  1243. return ok;
  1244. }
  1245. return false;
  1246. }
  1247. static bool match_newlines_impl(Compiler* self) {
  1248. bool consumed = false;
  1249. if(curr()->type == TK_EOL) {
  1250. while(curr()->type == TK_EOL)
  1251. advance();
  1252. consumed = true;
  1253. }
  1254. return consumed;
  1255. }
  1256. static bool match_end_stmt(Compiler* self) {
  1257. if(match(TK_SEMICOLON)) {
  1258. match_newlines();
  1259. return true;
  1260. }
  1261. if(match_newlines() || curr()->type == TK_EOF) return true;
  1262. if(curr()->type == TK_DEDENT) return true;
  1263. return false;
  1264. }
  1265. /* Expression */
  1266. /// Parse an expression and push it onto the stack.
  1267. static Error* parse_expression(Compiler* self, int precedence, bool allow_slice) {
  1268. PrattCallback prefix = rules[curr()->type].prefix;
  1269. if(!prefix || (curr()->type == TK_COLON && !allow_slice)) {
  1270. return SyntaxError(self, "expected an expression, got %s", TokenSymbols[curr()->type]);
  1271. }
  1272. advance();
  1273. Error* err;
  1274. check(prefix(self));
  1275. while(rules[curr()->type].precedence >= precedence &&
  1276. (allow_slice || curr()->type != TK_COLON)) {
  1277. TokenIndex op = curr()->type;
  1278. advance();
  1279. PrattCallback infix = rules[op].infix;
  1280. if(infix == NULL) {
  1281. return SyntaxError(self, "expected an infix operator, got %s", TokenSymbols[op]);
  1282. }
  1283. check(infix(self));
  1284. }
  1285. return NULL;
  1286. }
  1287. static Error* EXPR_TUPLE_ALLOW_SLICE(Compiler* self, bool allow_slice) {
  1288. Error* err;
  1289. check(parse_expression(self, PREC_LOWEST + 1, allow_slice));
  1290. if(!match(TK_COMMA)) return NULL;
  1291. // tuple expression // (a, )
  1292. int count = 1;
  1293. do {
  1294. if(curr()->brackets_level) match_newlines();
  1295. if(!is_expression(self, allow_slice)) break;
  1296. check(parse_expression(self, PREC_LOWEST + 1, allow_slice));
  1297. count += 1;
  1298. if(curr()->brackets_level) match_newlines();
  1299. } while(match(TK_COMMA));
  1300. // pop `count` expressions from the stack and merge them into a TupleExpr
  1301. SequenceExpr* e = TupleExpr__new(prev()->line, count);
  1302. for(int i = count - 1; i >= 0; i--) {
  1303. e->items[i] = Ctx__s_popx(ctx());
  1304. }
  1305. Ctx__s_push(ctx(), (Expr*)e);
  1306. return NULL;
  1307. }
  1308. /// Parse a simple expression.
  1309. static Error* EXPR(Compiler* self) { return parse_expression(self, PREC_LOWEST + 1, false); }
  1310. /// Parse a simple expression or a tuple of expressions.
  1311. static Error* EXPR_TUPLE(Compiler* self) { return EXPR_TUPLE_ALLOW_SLICE(self, false); }
  1312. // special case for `for loop` and `comp`
  1313. static Error* EXPR_VARS(Compiler* self) {
  1314. int count = 0;
  1315. do {
  1316. consume(TK_ID);
  1317. py_Name name = py_namev(Token__sv(prev()));
  1318. NameExpr* e = NameExpr__new(prev()->line, name, name_scope(self));
  1319. Ctx__s_push(ctx(), (Expr*)e);
  1320. count += 1;
  1321. } while(match(TK_COMMA));
  1322. if(count > 1) {
  1323. SequenceExpr* e = TupleExpr__new(prev()->line, count);
  1324. for(int i = count - 1; i >= 0; i--) {
  1325. e->items[i] = Ctx__s_popx(ctx());
  1326. }
  1327. Ctx__s_push(ctx(), (Expr*)e);
  1328. }
  1329. return NULL;
  1330. }
  1331. /* Misc */
  1332. static void push_global_context(Compiler* self, CodeObject* co) {
  1333. co->start_line = self->i == 0 ? 1 : prev()->line;
  1334. Ctx* ctx = c11_vector__emplace(&self->contexts);
  1335. Ctx__ctor(ctx, co, NULL, self->contexts.length);
  1336. }
  1337. static Error* pop_context(Compiler* self) {
  1338. // add a `return None` in the end as a guard
  1339. // previously, we only do this if the last opcode is not a return
  1340. // however, this is buggy...since there may be a jump to the end (out of bound) even if the last
  1341. // opcode is a return
  1342. Ctx__emit_virtual(ctx(), OP_RETURN_VALUE, 1, BC_KEEPLINE, true);
  1343. CodeObject* co = ctx()->co;
  1344. // find the last valid token
  1345. int j = self->i - 1;
  1346. while(tk(j)->type == TK_EOL || tk(j)->type == TK_DEDENT || tk(j)->type == TK_EOF)
  1347. j--;
  1348. co->end_line = tk(j)->line;
  1349. // some check here
  1350. c11_vector* codes = &co->codes;
  1351. if(co->nlocals > PK_MAX_CO_VARNAMES) {
  1352. return SyntaxError(self, "maximum number of local variables exceeded");
  1353. }
  1354. if(co->consts.length > 65530) {
  1355. return SyntaxError(self, "maximum number of constants exceeded");
  1356. }
  1357. // pre-compute block.end or block.end2
  1358. for(int i = 0; i < codes->length; i++) {
  1359. Bytecode* bc = c11__at(Bytecode, codes, i);
  1360. if(bc->op == OP_LOOP_CONTINUE) {
  1361. CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
  1362. Bytecode__set_signed_arg(bc, block->start - i);
  1363. } else if(bc->op == OP_LOOP_BREAK) {
  1364. CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
  1365. Bytecode__set_signed_arg(bc, (block->end2 != -1 ? block->end2 : block->end) - i);
  1366. } else if(bc->op == OP_FOR_ITER || bc->op == OP_FOR_ITER_YIELD_VALUE) {
  1367. CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
  1368. Bytecode__set_signed_arg(bc, block->end - i);
  1369. }
  1370. }
  1371. // pre-compute func->is_simple
  1372. FuncDecl* func = ctx()->func;
  1373. if(func) {
  1374. // check generator
  1375. Bytecode* codes = func->code.codes.data;
  1376. int codes_length = func->code.codes.length;
  1377. for(int i = 0; i < codes_length; i++) {
  1378. if(codes[i].op == OP_YIELD_VALUE || codes[i].op == OP_FOR_ITER_YIELD_VALUE) {
  1379. func->type = FuncType_GENERATOR;
  1380. break;
  1381. }
  1382. }
  1383. if(func->type == FuncType_UNSET) {
  1384. bool is_simple = true;
  1385. if(func->kwargs.length > 0) is_simple = false;
  1386. if(func->starred_arg >= 0) is_simple = false;
  1387. if(func->starred_kwarg >= 0) is_simple = false;
  1388. if(is_simple) {
  1389. func->type = FuncType_SIMPLE;
  1390. } else {
  1391. func->type = FuncType_NORMAL;
  1392. }
  1393. }
  1394. assert(func->type != FuncType_UNSET);
  1395. }
  1396. Ctx__dtor(ctx());
  1397. c11_vector__pop(&self->contexts);
  1398. return NULL;
  1399. }
  1400. /* Expression Callbacks */
  1401. static Error* exprLiteral(Compiler* self) {
  1402. LiteralExpr* e = LiteralExpr__new(prev()->line, &prev()->value);
  1403. Ctx__s_push(ctx(), (Expr*)e);
  1404. return NULL;
  1405. }
  1406. static Error* exprBytes(Compiler* self) {
  1407. c11_sv sv = c11_string__sv(prev()->value._str);
  1408. Ctx__s_push(ctx(), (Expr*)RawStringExpr__new(prev()->line, sv, OP_BUILD_BYTES));
  1409. return NULL;
  1410. }
  1411. static Error* exprFString(Compiler* self) {
  1412. // @fstr-begin, [@fstr-cpnt | <expr>]*, @fstr-end
  1413. int count = 0;
  1414. int line = prev()->line;
  1415. while(true) {
  1416. if(match(TK_FSTR_END)) {
  1417. SequenceExpr* e = FStringExpr__new(line, count);
  1418. for(int i = count - 1; i >= 0; i--) {
  1419. e->items[i] = Ctx__s_popx(ctx());
  1420. }
  1421. Ctx__s_push(ctx(), (Expr*)e);
  1422. return NULL;
  1423. } else if(match(TK_FSTR_CPNT)) {
  1424. // OP_LOAD_CONST
  1425. LiteralExpr* e = LiteralExpr__new(prev()->line, &prev()->value);
  1426. Ctx__s_push(ctx(), (Expr*)e);
  1427. count++;
  1428. } else {
  1429. // {a!r:.2f}
  1430. Error* err = EXPR(self);
  1431. if(err) return err;
  1432. count++;
  1433. if(match(TK_FSTR_SPEC)) {
  1434. c11_sv spec = Token__sv(prev());
  1435. // ':.2f}' -> ':.2f'
  1436. spec.size--;
  1437. Expr* child = Ctx__s_popx(ctx());
  1438. FStringSpecExpr* e = FStringSpecExpr__new(prev()->line, child, spec);
  1439. Ctx__s_push(ctx(), (Expr*)e);
  1440. }
  1441. }
  1442. }
  1443. }
  1444. static Error* exprImag(Compiler* self) {
  1445. Ctx__s_push(ctx(), (Expr*)ImagExpr__new(prev()->line, prev()->value._f64));
  1446. return NULL;
  1447. }
  1448. static FuncDecl_ push_f_context(Compiler* self, c11_sv name, int* out_index);
  1449. static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool is_lambda);
  1450. static Error* exprLambda(Compiler* self) {
  1451. Error* err;
  1452. int line = prev()->line;
  1453. int decl_index;
  1454. FuncDecl_ decl = push_f_context(self, (c11_sv){"<lambda>", 8}, &decl_index);
  1455. if(!match(TK_COLON)) {
  1456. check(_compile_f_args(self, decl, true));
  1457. consume(TK_COLON);
  1458. }
  1459. // https://github.com/pocketpy/pocketpy/issues/37
  1460. check(parse_expression(self, PREC_LAMBDA + 1, false));
  1461. Ctx__s_emit_top(ctx());
  1462. Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
  1463. check(pop_context(self));
  1464. LambdaExpr* e = LambdaExpr__new(line, decl_index);
  1465. Ctx__s_push(ctx(), (Expr*)e);
  1466. return NULL;
  1467. }
  1468. static Error* exprOr(Compiler* self) {
  1469. Error* err;
  1470. int line = prev()->line;
  1471. check(parse_expression(self, PREC_LOGICAL_OR + 1, false));
  1472. LogicBinaryExpr* e = LogicBinaryExpr__new(line, OP_JUMP_IF_TRUE_OR_POP);
  1473. e->rhs = Ctx__s_popx(ctx());
  1474. e->lhs = Ctx__s_popx(ctx());
  1475. Ctx__s_push(ctx(), (Expr*)e);
  1476. return NULL;
  1477. }
  1478. static Error* exprAnd(Compiler* self) {
  1479. Error* err;
  1480. int line = prev()->line;
  1481. check(parse_expression(self, PREC_LOGICAL_AND + 1, false));
  1482. LogicBinaryExpr* e = LogicBinaryExpr__new(line, OP_JUMP_IF_FALSE_OR_POP);
  1483. e->rhs = Ctx__s_popx(ctx());
  1484. e->lhs = Ctx__s_popx(ctx());
  1485. Ctx__s_push(ctx(), (Expr*)e);
  1486. return NULL;
  1487. }
  1488. static Error* exprTernary(Compiler* self) {
  1489. // [true_expr]
  1490. Error* err;
  1491. int line = prev()->line;
  1492. check(parse_expression(self, PREC_TERNARY + 1, false)); // [true_expr, cond]
  1493. consume(TK_ELSE);
  1494. check(parse_expression(self, PREC_TERNARY + 1, false)); // [true_expr, cond, false_expr]
  1495. TernaryExpr* e = TernaryExpr__new(line);
  1496. e->false_expr = Ctx__s_popx(ctx());
  1497. e->cond = Ctx__s_popx(ctx());
  1498. e->true_expr = Ctx__s_popx(ctx());
  1499. Ctx__s_push(ctx(), (Expr*)e);
  1500. if(e->cond->vt->is_ternary || e->false_expr->vt->is_ternary || e->true_expr->vt->is_ternary) {
  1501. return SyntaxError(self, "nested ternary expressions without `()` are ambiguous");
  1502. }
  1503. return NULL;
  1504. }
  1505. static Error* exprBinaryOp(Compiler* self) {
  1506. Error* err;
  1507. int line = prev()->line;
  1508. TokenIndex op = prev()->type;
  1509. int precedence = rules[op].precedence;
  1510. if(op != TK_POW) {
  1511. // if not right associative, increase precedence
  1512. precedence += 1;
  1513. }
  1514. check(parse_expression(self, precedence, false));
  1515. BinaryExpr* e = BinaryExpr__new(line, op, false);
  1516. if(op == TK_IN || op == TK_NOT_IN) {
  1517. e->lhs = Ctx__s_popx(ctx());
  1518. e->rhs = Ctx__s_popx(ctx());
  1519. } else {
  1520. e->rhs = Ctx__s_popx(ctx());
  1521. e->lhs = Ctx__s_popx(ctx());
  1522. }
  1523. Ctx__s_push(ctx(), (Expr*)e);
  1524. return NULL;
  1525. }
  1526. static Error* exprNot(Compiler* self) {
  1527. Error* err;
  1528. int line = prev()->line;
  1529. check(parse_expression(self, PREC_LOGICAL_NOT + 1, false));
  1530. UnaryExpr* e = UnaryExpr__new(line, Ctx__s_popx(ctx()), OP_UNARY_NOT);
  1531. Ctx__s_push(ctx(), (Expr*)e);
  1532. return NULL;
  1533. }
  1534. static Error* exprUnaryOp(Compiler* self) {
  1535. Error* err;
  1536. int line = prev()->line;
  1537. TokenIndex op = prev()->type;
  1538. check(parse_expression(self, PREC_UNARY + 1, false));
  1539. Expr* e = Ctx__s_popx(ctx());
  1540. switch(op) {
  1541. case TK_SUB: {
  1542. // constant fold
  1543. if(e->vt->is_literal) {
  1544. LiteralExpr* le = (LiteralExpr*)e;
  1545. if(le->value->index == TokenValue_I64 || le->value->index == TokenValue_F64) {
  1546. le->negated = true;
  1547. }
  1548. Ctx__s_push(ctx(), e);
  1549. } else {
  1550. Ctx__s_push(ctx(), (Expr*)UnaryExpr__new(line, e, OP_UNARY_NEGATIVE));
  1551. }
  1552. break;
  1553. }
  1554. case TK_INVERT: Ctx__s_push(ctx(), (Expr*)UnaryExpr__new(line, e, OP_UNARY_INVERT)); break;
  1555. case TK_MUL: Ctx__s_push(ctx(), (Expr*)StarredExpr__new(line, e, 1)); break;
  1556. case TK_POW: Ctx__s_push(ctx(), (Expr*)StarredExpr__new(line, e, 2)); break;
  1557. default: assert(false);
  1558. }
  1559. return NULL;
  1560. }
  1561. static Error* exprGroup(Compiler* self) {
  1562. Error* err;
  1563. int line = prev()->line;
  1564. if(match(TK_RPAREN)) {
  1565. // empty tuple
  1566. Ctx__s_push(ctx(), (Expr*)TupleExpr__new(line, 0));
  1567. return NULL;
  1568. }
  1569. match_newlines();
  1570. check(EXPR_TUPLE(self)); // () is just for change precedence
  1571. match_newlines();
  1572. consume(TK_RPAREN);
  1573. if(Ctx__s_top(ctx())->vt->is_tuple) return NULL;
  1574. GroupedExpr* g = GroupedExpr__new(line, Ctx__s_popx(ctx()));
  1575. Ctx__s_push(ctx(), (Expr*)g);
  1576. return NULL;
  1577. }
  1578. static Error* exprName(Compiler* self) {
  1579. py_Name name = py_namev(Token__sv(prev()));
  1580. NameScope scope = name_scope(self);
  1581. // promote this name to global scope if needed
  1582. if(c11_smallmap_n2d__contains(&ctx()->global_names, name)) {
  1583. if(self->src->is_dynamic) return SyntaxError(self, "cannot use global keyword here");
  1584. scope = NAME_GLOBAL;
  1585. }
  1586. NameExpr* e = NameExpr__new(prev()->line, name, scope);
  1587. Ctx__s_push(ctx(), (Expr*)e);
  1588. return NULL;
  1589. }
  1590. static Error* exprAttrib(Compiler* self) {
  1591. consume(TK_ID);
  1592. py_Name name = py_namev(Token__sv(prev()));
  1593. AttribExpr* e = AttribExpr__new(prev()->line, Ctx__s_popx(ctx()), name);
  1594. Ctx__s_push(ctx(), (Expr*)e);
  1595. return NULL;
  1596. }
  1597. static Error* exprLiteral0(Compiler* self) {
  1598. Literal0Expr* e = Literal0Expr__new(prev()->line, prev()->type);
  1599. Ctx__s_push(ctx(), (Expr*)e);
  1600. return NULL;
  1601. }
  1602. static Error* consume_comp(Compiler* self, Opcode op0, Opcode op1) {
  1603. // [expr]
  1604. Error* err;
  1605. int line = prev()->line;
  1606. bool has_cond = false;
  1607. check(EXPR_VARS(self)); // [expr, vars]
  1608. consume(TK_IN);
  1609. check(parse_expression(self, PREC_TERNARY + 1, false)); // [expr, vars, iter]
  1610. match_newlines();
  1611. if(match(TK_IF)) {
  1612. check(parse_expression(self, PREC_TERNARY + 1, false)); // [expr, vars, iter, cond]
  1613. has_cond = true;
  1614. }
  1615. CompExpr* ce = CompExpr__new(line, op0, op1);
  1616. if(has_cond) ce->cond = Ctx__s_popx(ctx());
  1617. ce->iter = Ctx__s_popx(ctx());
  1618. ce->vars = Ctx__s_popx(ctx());
  1619. ce->expr = Ctx__s_popx(ctx());
  1620. Ctx__s_push(ctx(), (Expr*)ce);
  1621. match_newlines();
  1622. return NULL;
  1623. }
  1624. static Error* exprList(Compiler* self) {
  1625. Error* err;
  1626. int line = prev()->line;
  1627. int count = 0;
  1628. do {
  1629. match_newlines();
  1630. if(curr()->type == TK_RBRACKET) break;
  1631. check(EXPR(self));
  1632. count += 1;
  1633. match_newlines();
  1634. if(count == 1 && match(TK_FOR)) {
  1635. check(consume_comp(self, OP_BUILD_LIST, OP_LIST_APPEND));
  1636. consume(TK_RBRACKET);
  1637. return NULL;
  1638. }
  1639. match_newlines();
  1640. } while(match(TK_COMMA));
  1641. consume(TK_RBRACKET);
  1642. SequenceExpr* e = ListExpr__new(line, count);
  1643. for(int i = count - 1; i >= 0; i--) {
  1644. e->items[i] = Ctx__s_popx(ctx());
  1645. }
  1646. Ctx__s_push(ctx(), (Expr*)e);
  1647. return NULL;
  1648. }
  1649. static Error* exprMap(Compiler* self) {
  1650. Error* err;
  1651. int line = prev()->line;
  1652. bool parsing_dict = false; // {...} may be dict or set
  1653. int count = 0;
  1654. do {
  1655. match_newlines();
  1656. if(curr()->type == TK_RBRACE) break;
  1657. check(EXPR(self)); // [key]
  1658. if(curr()->type == TK_COLON) { parsing_dict = true; }
  1659. if(parsing_dict) {
  1660. consume(TK_COLON);
  1661. check(EXPR(self)); // [key, value] -> [item]
  1662. DictItemExpr* item = DictItemExpr__new(prev()->line);
  1663. item->value = Ctx__s_popx(ctx());
  1664. item->key = Ctx__s_popx(ctx());
  1665. Ctx__s_push(ctx(), (Expr*)item);
  1666. }
  1667. count += 1; // key-value pair count
  1668. match_newlines();
  1669. if(count == 1 && match(TK_FOR)) {
  1670. if(parsing_dict) {
  1671. check(consume_comp(self, OP_BUILD_DICT, OP_DICT_ADD));
  1672. } else {
  1673. check(consume_comp(self, OP_BUILD_SET, OP_SET_ADD));
  1674. }
  1675. consume(TK_RBRACE);
  1676. return NULL;
  1677. }
  1678. match_newlines();
  1679. } while(match(TK_COMMA));
  1680. consume(TK_RBRACE);
  1681. SequenceExpr* se;
  1682. if(count == 0 || parsing_dict) {
  1683. se = DictExpr__new(line, count);
  1684. } else {
  1685. se = SetExpr__new(line, count);
  1686. }
  1687. for(int i = count - 1; i >= 0; i--) {
  1688. se->items[i] = Ctx__s_popx(ctx());
  1689. }
  1690. Ctx__s_push(ctx(), (Expr*)se);
  1691. return NULL;
  1692. }
  1693. static Error* read_literal(Compiler* self, py_Ref out);
  1694. static Error* exprCompileTimeCall(Compiler* self, py_ItemRef func, int line) {
  1695. Error* err;
  1696. py_push(func);
  1697. py_pushnil();
  1698. uint16_t argc = 0;
  1699. uint16_t kwargc = 0;
  1700. // copied from `exprCall`
  1701. do {
  1702. match_newlines();
  1703. if(curr()->type == TK_RPAREN) break;
  1704. if(curr()->type == TK_ID && next()->type == TK_ASSIGN) {
  1705. consume(TK_ID);
  1706. py_Name key = py_namev(Token__sv(prev()));
  1707. consume(TK_ASSIGN);
  1708. // k=v
  1709. py_pushname(key);
  1710. check(read_literal(self, py_pushtmp()));
  1711. kwargc += 1;
  1712. } else {
  1713. if(kwargc > 0) {
  1714. return SyntaxError(self, "positional argument follows keyword argument");
  1715. }
  1716. check(read_literal(self, py_pushtmp()));
  1717. argc += 1;
  1718. }
  1719. match_newlines();
  1720. } while(match(TK_COMMA));
  1721. consume(TK_RPAREN);
  1722. bool ok = py_vectorcall(argc, kwargc);
  1723. if(!ok) {
  1724. char* msg = py_formatexc();
  1725. err = SyntaxError(self, "compile-time call error:\n%s", msg);
  1726. PK_FREE(msg);
  1727. return err;
  1728. }
  1729. // TODO: optimize string dedup
  1730. int index = Ctx__add_const(ctx(), py_retval());
  1731. Ctx__s_push(ctx(), (Expr*)LoadConstExpr__new(line, index));
  1732. return NULL;
  1733. }
  1734. static Error* exprCall(Compiler* self) {
  1735. Error* err;
  1736. Expr* callable = Ctx__s_popx(ctx());
  1737. int line = prev()->line;
  1738. if(callable->vt->is_name) {
  1739. NameExpr* ne = (NameExpr*)callable;
  1740. py_ItemRef func = py_macroget(ne->name);
  1741. if(func != NULL) {
  1742. py_StackRef p0 = py_peek(0);
  1743. err = exprCompileTimeCall(self, func, line);
  1744. if(err != NULL) py_clearexc(p0);
  1745. return err;
  1746. }
  1747. }
  1748. CallExpr* e = CallExpr__new(line, callable);
  1749. Ctx__s_push(ctx(), (Expr*)e); // push onto the stack in advance
  1750. do {
  1751. match_newlines();
  1752. if(curr()->type == TK_RPAREN) break;
  1753. if(curr()->type == TK_ID && next()->type == TK_ASSIGN) {
  1754. consume(TK_ID);
  1755. py_Name key = py_namev(Token__sv(prev()));
  1756. consume(TK_ASSIGN);
  1757. check(EXPR(self));
  1758. CallExprKwArg kw = {key, Ctx__s_popx(ctx())};
  1759. c11_vector__push(CallExprKwArg, &e->kwargs, kw);
  1760. } else {
  1761. check(EXPR(self));
  1762. int star_level = 0;
  1763. Expr* top = Ctx__s_top(ctx());
  1764. if(top->vt->is_starred) star_level = ((StarredExpr*)top)->level;
  1765. if(star_level == 2) {
  1766. // **kwargs
  1767. CallExprKwArg kw = {0, Ctx__s_popx(ctx())};
  1768. c11_vector__push(CallExprKwArg, &e->kwargs, kw);
  1769. } else {
  1770. // positional argument
  1771. if(e->kwargs.length > 0) {
  1772. return SyntaxError(self, "positional argument follows keyword argument");
  1773. }
  1774. c11_vector__push(Expr*, &e->args, Ctx__s_popx(ctx()));
  1775. }
  1776. }
  1777. match_newlines();
  1778. } while(match(TK_COMMA));
  1779. consume(TK_RPAREN);
  1780. return NULL;
  1781. }
  1782. static Error* exprSlice0(Compiler* self) {
  1783. Error* err;
  1784. SliceExpr* slice = SliceExpr__new(prev()->line);
  1785. Ctx__s_push(ctx(), (Expr*)slice); // push onto the stack in advance
  1786. if(is_expression(self, false)) { // :<stop>
  1787. check(EXPR(self));
  1788. slice->stop = Ctx__s_popx(ctx());
  1789. // try optional step
  1790. if(match(TK_COLON)) { // :<stop>:<step>
  1791. check(EXPR(self));
  1792. slice->step = Ctx__s_popx(ctx());
  1793. }
  1794. } else if(match(TK_COLON)) {
  1795. if(is_expression(self, false)) { // ::<step>
  1796. check(EXPR(self));
  1797. slice->step = Ctx__s_popx(ctx());
  1798. } // else ::
  1799. } // else :
  1800. return NULL;
  1801. }
  1802. static Error* exprSlice1(Compiler* self) {
  1803. Error* err;
  1804. SliceExpr* slice = SliceExpr__new(prev()->line);
  1805. slice->start = Ctx__s_popx(ctx());
  1806. Ctx__s_push(ctx(), (Expr*)slice); // push onto the stack in advance
  1807. if(is_expression(self, false)) { // <start>:<stop>
  1808. check(EXPR(self));
  1809. slice->stop = Ctx__s_popx(ctx());
  1810. // try optional step
  1811. if(match(TK_COLON)) { // <start>:<stop>:<step>
  1812. check(EXPR(self));
  1813. slice->step = Ctx__s_popx(ctx());
  1814. }
  1815. } else if(match(TK_COLON)) { // <start>::<step>
  1816. check(EXPR(self));
  1817. slice->step = Ctx__s_popx(ctx());
  1818. } // else <start>:
  1819. return NULL;
  1820. }
  1821. static Error* exprSubscr(Compiler* self) {
  1822. Error* err;
  1823. int line = prev()->line;
  1824. match_newlines();
  1825. check(EXPR_TUPLE_ALLOW_SLICE(self, true));
  1826. match_newlines();
  1827. consume(TK_RBRACKET); // [lhs, rhs]
  1828. SubscrExpr* e = SubscrExpr__new(line);
  1829. e->rhs = Ctx__s_popx(ctx()); // [lhs]
  1830. e->lhs = Ctx__s_popx(ctx()); // []
  1831. Ctx__s_push(ctx(), (Expr*)e);
  1832. return NULL;
  1833. }
  1834. ////////////////
  1835. static Error* consume_type_hints(Compiler* self) {
  1836. Error* err;
  1837. check(EXPR(self));
  1838. Ctx__s_pop(ctx());
  1839. return NULL;
  1840. }
  1841. static Error* consume_type_hints_sv(Compiler* self, c11_sv* out) {
  1842. Error* err;
  1843. const char* start = curr()->start;
  1844. check(EXPR(self));
  1845. const char* end = prev()->start + prev()->length;
  1846. *out = (c11_sv){start, end - start};
  1847. Ctx__s_pop(ctx());
  1848. return NULL;
  1849. }
  1850. static Error* compile_stmt(Compiler* self);
  1851. static Error* compile_block_body(Compiler* self) {
  1852. Error* err;
  1853. consume(TK_COLON);
  1854. if(curr()->type != TK_EOL && curr()->type != TK_EOF) {
  1855. while(true) {
  1856. check(compile_stmt(self));
  1857. bool possible = curr()->type != TK_EOL && curr()->type != TK_EOF;
  1858. if(prev()->type != TK_SEMICOLON || !possible) break;
  1859. }
  1860. return NULL;
  1861. }
  1862. bool consumed = match_newlines();
  1863. if(!consumed) return SyntaxError(self, "expected a new line after ':'");
  1864. consume(TK_INDENT);
  1865. while(curr()->type != TK_DEDENT) {
  1866. match_newlines();
  1867. check(compile_stmt(self));
  1868. match_newlines();
  1869. }
  1870. consume(TK_DEDENT);
  1871. return NULL;
  1872. }
  1873. static Error* compile_if_stmt(Compiler* self) {
  1874. Error* err;
  1875. check(EXPR(self)); // condition
  1876. Ctx__s_emit_top(ctx());
  1877. int patch = Ctx__emit_(ctx(), OP_POP_JUMP_IF_FALSE, BC_NOARG, prev()->line);
  1878. err = compile_block_body(self);
  1879. if(err) return err;
  1880. if(match(TK_ELIF)) {
  1881. int exit_patch = Ctx__emit_(ctx(), OP_JUMP_FORWARD, BC_NOARG, prev()->line);
  1882. Ctx__patch_jump(ctx(), patch);
  1883. check(compile_if_stmt(self));
  1884. Ctx__patch_jump(ctx(), exit_patch);
  1885. } else if(match(TK_ELSE)) {
  1886. int exit_patch = Ctx__emit_(ctx(), OP_JUMP_FORWARD, BC_NOARG, prev()->line);
  1887. Ctx__patch_jump(ctx(), patch);
  1888. check(compile_block_body(self));
  1889. Ctx__patch_jump(ctx(), exit_patch);
  1890. } else {
  1891. Ctx__patch_jump(ctx(), patch);
  1892. }
  1893. return NULL;
  1894. }
  1895. static Error* compile_match_case(Compiler* self, c11_vector* patches) {
  1896. Error* err;
  1897. bool is_case_default = false;
  1898. check(EXPR(self)); // condition
  1899. Ctx__s_emit_top(ctx());
  1900. consume(TK_COLON);
  1901. bool consumed = match_newlines();
  1902. if(!consumed) return SyntaxError(self, "expected a new line after ':'");
  1903. consume(TK_INDENT);
  1904. while(curr()->type != TK_DEDENT) {
  1905. match_newlines();
  1906. if(match_id_by_str(self, "case")) {
  1907. if(is_case_default) return SyntaxError(self, "case _: must be the last one");
  1908. is_case_default = match_id_by_str(self, "_");
  1909. if(!is_case_default) {
  1910. Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, prev()->line);
  1911. check(EXPR(self)); // expr
  1912. Ctx__s_emit_top(ctx());
  1913. int patch = Ctx__emit_(ctx(), OP_POP_JUMP_IF_NOT_MATCH, BC_NOARG, prev()->line);
  1914. check(compile_block_body(self));
  1915. int break_patch = Ctx__emit_(ctx(), OP_JUMP_FORWARD, BC_NOARG, prev()->line);
  1916. c11_vector__push(int, patches, break_patch);
  1917. Ctx__patch_jump(ctx(), patch);
  1918. } else {
  1919. check(compile_block_body(self));
  1920. }
  1921. } else {
  1922. return SyntaxError(self, "expected 'case', got '%s'", TokenSymbols[curr()->type]);
  1923. }
  1924. match_newlines();
  1925. }
  1926. consume(TK_DEDENT);
  1927. for(int i = 0; i < patches->length; i++) {
  1928. int patch = c11__getitem(int, patches, i);
  1929. Ctx__patch_jump(ctx(), patch);
  1930. }
  1931. Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, prev()->line);
  1932. return NULL;
  1933. }
  1934. static Error* compile_while_loop(Compiler* self) {
  1935. Error* err;
  1936. int block = Ctx__enter_block(ctx(), CodeBlockType_WHILE_LOOP);
  1937. int block_start = c11__at(CodeBlock, &ctx()->co->blocks, block)->start;
  1938. check(EXPR(self)); // condition
  1939. Ctx__s_emit_top(ctx());
  1940. int patch = Ctx__emit_(ctx(), OP_POP_JUMP_IF_FALSE, BC_NOARG, prev()->line);
  1941. check(compile_block_body(self));
  1942. Ctx__emit_jump(ctx(), block_start, BC_KEEPLINE);
  1943. Ctx__patch_jump(ctx(), patch);
  1944. Ctx__exit_block(ctx());
  1945. // optional else clause
  1946. if(match(TK_ELSE)) {
  1947. check(compile_block_body(self));
  1948. CodeBlock* p_block = c11__at(CodeBlock, &ctx()->co->blocks, block);
  1949. p_block->end2 = ctx()->co->codes.length;
  1950. }
  1951. return NULL;
  1952. }
  1953. static Error* compile_for_loop(Compiler* self) {
  1954. Error* err;
  1955. check(EXPR_VARS(self)); // [vars]
  1956. consume(TK_IN);
  1957. check(EXPR_TUPLE(self)); // [vars, iter]
  1958. Ctx__s_emit_top(ctx()); // [vars]
  1959. Ctx__emit_(ctx(), OP_GET_ITER, BC_NOARG, BC_KEEPLINE);
  1960. int block = Ctx__enter_block(ctx(), CodeBlockType_FOR_LOOP);
  1961. int block_start = Ctx__emit_(ctx(), OP_FOR_ITER, block, BC_KEEPLINE);
  1962. Expr* vars = Ctx__s_popx(ctx());
  1963. bool ok = vtemit_store(vars, ctx());
  1964. vtdelete(vars);
  1965. if(!ok) {
  1966. // this error occurs in `vars` instead of this line, but...nevermind
  1967. return SyntaxError(self, "invalid syntax");
  1968. }
  1969. check(compile_block_body(self));
  1970. Ctx__emit_jump(ctx(), block_start, BC_KEEPLINE);
  1971. Ctx__exit_block(ctx());
  1972. // optional else clause
  1973. if(match(TK_ELSE)) {
  1974. check(compile_block_body(self));
  1975. CodeBlock* p_block = c11__at(CodeBlock, &ctx()->co->blocks, block);
  1976. p_block->end2 = ctx()->co->codes.length;
  1977. }
  1978. return NULL;
  1979. }
  1980. static Error* compile_yield_from(Compiler* self, int kw_line) {
  1981. Error* err;
  1982. if(self->contexts.length <= 1) return SyntaxError(self, "'yield from' outside function");
  1983. check(EXPR_TUPLE(self));
  1984. Ctx__s_emit_top(ctx());
  1985. Ctx__emit_(ctx(), OP_GET_ITER, BC_NOARG, kw_line);
  1986. int block = Ctx__enter_block(ctx(), CodeBlockType_FOR_LOOP);
  1987. int block_start = Ctx__emit_(ctx(), OP_FOR_ITER_YIELD_VALUE, block, kw_line);
  1988. Ctx__emit_jump(ctx(), block_start, BC_KEEPLINE);
  1989. Ctx__exit_block(ctx());
  1990. // StopIteration.value will be pushed onto the stack
  1991. return NULL;
  1992. }
  1993. Error* try_compile_assignment(Compiler* self, bool* is_assign) {
  1994. Error* err;
  1995. switch(curr()->type) {
  1996. case TK_IADD:
  1997. case TK_ISUB:
  1998. case TK_IMUL:
  1999. case TK_IDIV:
  2000. case TK_IFLOORDIV:
  2001. case TK_IMOD:
  2002. case TK_ILSHIFT:
  2003. case TK_IRSHIFT:
  2004. case TK_IAND:
  2005. case TK_IOR:
  2006. case TK_IXOR: {
  2007. if(Ctx__s_top(ctx())->vt->is_starred)
  2008. return SyntaxError(self, "can't use inplace operator with starred expression");
  2009. if(ctx()->is_compiling_class)
  2010. return SyntaxError(self, "can't use inplace operator in class definition");
  2011. advance();
  2012. // a[x] += 1; a and x should be evaluated only once
  2013. // a.x += 1; a should be evaluated only once
  2014. // -1 to remove =; inplace=true
  2015. int line = prev()->line;
  2016. TokenIndex op = (TokenIndex)(prev()->type - 1);
  2017. // [lhs]
  2018. check(EXPR_TUPLE(self)); // [lhs, rhs]
  2019. if(Ctx__s_top(ctx())->vt->is_starred)
  2020. return SyntaxError(self, "can't use starred expression here");
  2021. BinaryExpr* e = BinaryExpr__new(line, op, true);
  2022. e->rhs = Ctx__s_popx(ctx()); // [lhs]
  2023. e->lhs = Ctx__s_popx(ctx()); // []
  2024. vtemit_((Expr*)e, ctx());
  2025. bool ok = vtemit_istore(e->lhs, ctx());
  2026. vtdelete((Expr*)e);
  2027. if(!ok) return SyntaxError(self, "invalid syntax");
  2028. *is_assign = true;
  2029. return NULL;
  2030. }
  2031. case TK_ASSIGN: {
  2032. consume(TK_ASSIGN);
  2033. int n = 0; // assignment count
  2034. if(match(TK_YIELD_FROM)) {
  2035. check(compile_yield_from(self, prev()->line));
  2036. n = 1;
  2037. } else {
  2038. do {
  2039. check(EXPR_TUPLE(self));
  2040. n += 1;
  2041. } while(match(TK_ASSIGN));
  2042. // stack size is n+1
  2043. Ctx__s_emit_top(ctx());
  2044. for(int j = 1; j < n; j++)
  2045. Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
  2046. }
  2047. for(int j = 0; j < n; j++) {
  2048. if(Ctx__s_top(ctx())->vt->is_starred)
  2049. return SyntaxError(self, "can't use starred expression here");
  2050. Expr* e = Ctx__s_top(ctx());
  2051. bool ok = vtemit_store(e, ctx());
  2052. Ctx__s_pop(ctx());
  2053. if(!ok) return SyntaxError(self, "invalid syntax");
  2054. }
  2055. *is_assign = true;
  2056. return NULL;
  2057. }
  2058. default: *is_assign = false;
  2059. }
  2060. return NULL;
  2061. }
  2062. static FuncDecl_ push_f_context(Compiler* self, c11_sv name, int* out_index) {
  2063. FuncDecl_ decl = FuncDecl__rcnew(self->src, name);
  2064. decl->code.start_line = self->i == 0 ? 1 : prev()->line;
  2065. decl->nested = name_scope(self) == NAME_LOCAL;
  2066. // add_func_decl
  2067. Ctx* top_ctx = ctx();
  2068. c11_vector__push(FuncDecl_, &top_ctx->co->func_decls, decl);
  2069. *out_index = top_ctx->co->func_decls.length - 1;
  2070. // push new context
  2071. top_ctx = c11_vector__emplace(&self->contexts);
  2072. Ctx__ctor(top_ctx, &decl->code, decl, self->contexts.length);
  2073. return decl;
  2074. }
  2075. static Error* read_literal(Compiler* self, py_Ref out) {
  2076. Error* err;
  2077. advance();
  2078. const TokenValue* value = &prev()->value;
  2079. bool negated = false;
  2080. switch(prev()->type) {
  2081. case TK_SUB:
  2082. consume(TK_NUM);
  2083. value = &prev()->value;
  2084. negated = true;
  2085. case TK_NUM: {
  2086. if(value->index == TokenValue_I64) {
  2087. py_newint(out, negated ? -value->_i64 : value->_i64);
  2088. } else if(value->index == TokenValue_F64) {
  2089. py_newfloat(out, negated ? -value->_f64 : value->_f64);
  2090. } else {
  2091. c11__unreachable();
  2092. }
  2093. return NULL;
  2094. }
  2095. case TK_STR: py_newstr(out, value->_str->data); return NULL;
  2096. case TK_TRUE: py_newbool(out, true); return NULL;
  2097. case TK_FALSE: py_newbool(out, false); return NULL;
  2098. case TK_NONE: py_newnone(out); return NULL;
  2099. case TK_DOTDOTDOT: py_newellipsis(out); return NULL;
  2100. case TK_LPAREN: {
  2101. py_TValue cpnts[4];
  2102. int count = 0;
  2103. while(true) {
  2104. if(count == 4)
  2105. return SyntaxError(self, "default argument tuple exceeds 4 elements");
  2106. check(read_literal(self, &cpnts[count]));
  2107. count += 1;
  2108. if(curr()->type == TK_RPAREN) break;
  2109. consume(TK_COMMA);
  2110. if(curr()->type == TK_RPAREN) break;
  2111. }
  2112. consume(TK_RPAREN);
  2113. py_Ref p = py_newtuple(out, count);
  2114. for(int i = 0; i < count; i++) {
  2115. p[i] = cpnts[i];
  2116. }
  2117. return NULL;
  2118. }
  2119. default: {
  2120. return SyntaxError(self, "expected a literal, got '%s'", TokenSymbols[prev()->type]);
  2121. }
  2122. }
  2123. }
  2124. static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool is_lambda) {
  2125. int state = 0; // 0 for args, 1 for *args, 2 for k=v, 3 for **kwargs
  2126. Error* err;
  2127. do {
  2128. if(!is_lambda) match_newlines();
  2129. if(state >= 3) return SyntaxError(self, "**kwargs should be the last argument");
  2130. if(match(TK_MUL)) {
  2131. if(state < 1)
  2132. state = 1;
  2133. else
  2134. return SyntaxError(self, "*args should be placed before **kwargs");
  2135. } else if(match(TK_POW)) {
  2136. state = 3;
  2137. }
  2138. consume(TK_ID);
  2139. py_Name name = py_namev(Token__sv(prev()));
  2140. // check duplicate argument name
  2141. if(FuncDecl__is_duplicated_arg(decl, name)) {
  2142. return SyntaxError(self, "duplicate argument name");
  2143. }
  2144. // eat type hints
  2145. if(!is_lambda && match(TK_COLON)) check(consume_type_hints(self));
  2146. if(state == 0 && curr()->type == TK_ASSIGN) state = 2;
  2147. switch(state) {
  2148. case 0: FuncDecl__add_arg(decl, name); break;
  2149. case 1:
  2150. FuncDecl__add_starred_arg(decl, name);
  2151. state += 1;
  2152. break;
  2153. case 2: {
  2154. consume(TK_ASSIGN);
  2155. py_TValue value;
  2156. check(read_literal(self, &value));
  2157. FuncDecl__add_kwarg(decl, name, &value);
  2158. } break;
  2159. case 3:
  2160. FuncDecl__add_starred_kwarg(decl, name);
  2161. state += 1;
  2162. break;
  2163. }
  2164. } while(match(TK_COMMA));
  2165. if(!is_lambda) match_newlines();
  2166. return NULL;
  2167. }
  2168. static Error* consume_pep695_py312(Compiler* self) {
  2169. // https://peps.python.org/pep-0695/
  2170. Error* err;
  2171. if(match(TK_LBRACKET)) {
  2172. do {
  2173. consume(TK_ID);
  2174. if(match(TK_COLON)) check(consume_type_hints(self));
  2175. } while(match(TK_COMMA));
  2176. consume(TK_RBRACKET);
  2177. }
  2178. return NULL;
  2179. }
  2180. static Error* compile_function(Compiler* self, int decorators) {
  2181. Error* err;
  2182. consume(TK_ID);
  2183. c11_sv decl_name_sv = Token__sv(prev());
  2184. int decl_index;
  2185. FuncDecl_ decl = push_f_context(self, decl_name_sv, &decl_index);
  2186. consume_pep695_py312(self);
  2187. consume(TK_LPAREN);
  2188. if(!match(TK_RPAREN)) {
  2189. check(_compile_f_args(self, decl, false));
  2190. consume(TK_RPAREN);
  2191. }
  2192. if(match(TK_ARROW)) check(consume_type_hints(self));
  2193. check(compile_block_body(self));
  2194. check(pop_context(self));
  2195. if(decl->code.codes.length >= 2) {
  2196. Bytecode* codes = (Bytecode*)decl->code.codes.data;
  2197. if(codes[0].op == OP_LOAD_CONST && codes[1].op == OP_POP_TOP) {
  2198. // handle optional docstring
  2199. py_TValue* consts = decl->code.consts.data;
  2200. py_TValue* c = &consts[codes[0].arg];
  2201. if(py_isstr(c)) {
  2202. decl->docstring = py_tostr(c);
  2203. codes[0].op = OP_NO_OP;
  2204. codes[1].op = OP_NO_OP;
  2205. }
  2206. }
  2207. }
  2208. Ctx__emit_(ctx(), OP_LOAD_FUNCTION, decl_index, prev()->line);
  2209. Ctx__s_emit_decorators(ctx(), decorators);
  2210. py_Name decl_name = py_namev(decl_name_sv);
  2211. if(ctx()->is_compiling_class) {
  2212. if(decl_name == __new__ || decl_name == __init__) {
  2213. if(decl->args.length == 0) {
  2214. return SyntaxError(self,
  2215. "%s() should have at least one positional argument",
  2216. py_name2str(decl_name));
  2217. }
  2218. }
  2219. Ctx__emit_(ctx(), OP_STORE_CLASS_ATTR, Ctx__add_name(ctx(), decl_name), prev()->line);
  2220. } else {
  2221. NameExpr* e = NameExpr__new(prev()->line, decl_name, name_scope(self));
  2222. vtemit_store((Expr*)e, ctx());
  2223. vtdelete((Expr*)e);
  2224. }
  2225. return NULL;
  2226. }
  2227. static Error* compile_class(Compiler* self, int decorators) {
  2228. Error* err;
  2229. if(ctx()->level > 1) return SyntaxError(self, "class definition not allowed here");
  2230. consume(TK_ID);
  2231. py_Name name = py_namev(Token__sv(prev()));
  2232. bool has_base = false;
  2233. consume_pep695_py312(self);
  2234. if(match(TK_LPAREN)) {
  2235. if(is_expression(self, false)) {
  2236. check(EXPR(self));
  2237. has_base = true; // [base]
  2238. }
  2239. consume(TK_RPAREN);
  2240. }
  2241. if(!has_base) {
  2242. Ctx__emit_(ctx(), OP_LOAD_NONE, BC_NOARG, prev()->line);
  2243. } else {
  2244. Ctx__s_emit_top(ctx()); // []
  2245. }
  2246. Ctx__emit_(ctx(), OP_BEGIN_CLASS, Ctx__add_name(ctx(), name), BC_KEEPLINE);
  2247. c11__foreach(Ctx, &self->contexts, it) {
  2248. if(it->is_compiling_class) return SyntaxError(self, "nested class is not allowed");
  2249. }
  2250. ctx()->is_compiling_class = true;
  2251. check(compile_block_body(self));
  2252. ctx()->is_compiling_class = false;
  2253. Ctx__s_emit_decorators(ctx(), decorators);
  2254. Ctx__emit_(ctx(), OP_END_CLASS, Ctx__add_name(ctx(), name), BC_KEEPLINE);
  2255. return NULL;
  2256. }
  2257. static Error* compile_decorated(Compiler* self) {
  2258. Error* err;
  2259. int count = 0;
  2260. do {
  2261. check(EXPR(self));
  2262. count += 1;
  2263. if(!match_newlines()) return SyntaxError(self, "expected a newline after '@'");
  2264. } while(match(TK_DECORATOR));
  2265. if(match(TK_CLASS)) {
  2266. check(compile_class(self, count));
  2267. } else {
  2268. consume(TK_DEF);
  2269. check(compile_function(self, count));
  2270. }
  2271. return NULL;
  2272. }
  2273. // import a [as b]
  2274. // import a [as b], c [as d]
  2275. static Error* compile_normal_import(Compiler* self) {
  2276. do {
  2277. consume(TK_ID);
  2278. c11_sv name = Token__sv(prev());
  2279. int index = Ctx__add_const_string(ctx(), name);
  2280. Ctx__emit_(ctx(), OP_IMPORT_PATH, index, prev()->line);
  2281. if(match(TK_AS)) {
  2282. consume(TK_ID);
  2283. name = Token__sv(prev());
  2284. }
  2285. Ctx__emit_store_name(ctx(), name_scope(self), py_namev(name), prev()->line);
  2286. } while(match(TK_COMMA));
  2287. consume_end_stmt();
  2288. return NULL;
  2289. }
  2290. // from a import b [as c], d [as e]
  2291. // from a.b import c [as d]
  2292. // from . import a [as b]
  2293. // from .a import b [as c]
  2294. // from ..a import b [as c]
  2295. // from .a.b import c [as d]
  2296. // from xxx import *
  2297. static Error* compile_from_import(c11_sbuf* buf, Compiler* self) {
  2298. int dots = 0;
  2299. while(true) {
  2300. switch(curr()->type) {
  2301. case TK_DOT: dots += 1; break;
  2302. case TK_DOTDOT: dots += 2; break;
  2303. case TK_DOTDOTDOT: dots += 3; break;
  2304. default: goto __EAT_DOTS_END;
  2305. }
  2306. advance();
  2307. }
  2308. __EAT_DOTS_END:
  2309. for(int i = 0; i < dots; i++) {
  2310. c11_sbuf__write_char(buf, '.');
  2311. }
  2312. if(dots > 0) {
  2313. // @id is optional if dots > 0
  2314. if(match(TK_ID)) {
  2315. c11_sbuf__write_sv(buf, Token__sv(prev()));
  2316. while(match(TK_DOT)) {
  2317. consume(TK_ID);
  2318. c11_sbuf__write_char(buf, '.');
  2319. c11_sbuf__write_sv(buf, Token__sv(prev()));
  2320. }
  2321. }
  2322. } else {
  2323. // @id is required if dots == 0
  2324. consume(TK_ID);
  2325. c11_sbuf__write_sv(buf, Token__sv(prev()));
  2326. while(match(TK_DOT)) {
  2327. consume(TK_ID);
  2328. c11_sbuf__write_char(buf, '.');
  2329. c11_sbuf__write_sv(buf, Token__sv(prev()));
  2330. }
  2331. }
  2332. c11_string* path = c11_sbuf__submit(buf);
  2333. Ctx__emit_(ctx(),
  2334. OP_IMPORT_PATH,
  2335. Ctx__add_const_string(ctx(), c11_string__sv(path)),
  2336. prev()->line);
  2337. c11_string__delete(path);
  2338. consume(TK_IMPORT);
  2339. if(match(TK_MUL)) {
  2340. if(name_scope(self) != NAME_GLOBAL)
  2341. return SyntaxError(self, "from <module> import * can only be used in global scope");
  2342. // pop the module and import __all__
  2343. Ctx__emit_(ctx(), OP_POP_IMPORT_STAR, BC_NOARG, prev()->line);
  2344. consume_end_stmt();
  2345. return NULL;
  2346. }
  2347. bool has_bracket = match(TK_LPAREN);
  2348. do {
  2349. if(has_bracket) match_newlines();
  2350. Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
  2351. consume(TK_ID);
  2352. c11_sv name = Token__sv(prev());
  2353. Ctx__emit_(ctx(), OP_LOAD_ATTR, Ctx__add_name(ctx(), py_namev(name)), prev()->line);
  2354. if(match(TK_AS)) {
  2355. consume(TK_ID);
  2356. name = Token__sv(prev());
  2357. }
  2358. Ctx__emit_store_name(ctx(), name_scope(self), py_namev(name), prev()->line);
  2359. } while(match(TK_COMMA));
  2360. if(has_bracket) {
  2361. match_newlines();
  2362. consume(TK_RPAREN);
  2363. }
  2364. Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
  2365. consume_end_stmt();
  2366. return NULL;
  2367. }
  2368. static Error* compile_try_except(Compiler* self) {
  2369. Error* err;
  2370. int patches[8];
  2371. int patches_length = 0;
  2372. Ctx__enter_block(ctx(), CodeBlockType_TRY);
  2373. Ctx__emit_(ctx(), OP_TRY_ENTER, BC_NOARG, prev()->line);
  2374. check(compile_block_body(self));
  2375. // https://docs.python.org/3/reference/compound_stmts.html#finally-clause
  2376. /* If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed,
  2377. * including any except and else clauses. If an exception occurs in any of the clauses and is
  2378. * not handled, the exception is temporarily saved. The finally clause is executed. If there is
  2379. * a saved exception it is re-raised at the end of the finally clause. If the finally clause
  2380. * raises another exception, the saved exception is set as the context of the new exception. If
  2381. * the finally clause executes a return, break or continue statement, the saved exception is
  2382. * discarded.
  2383. */
  2384. // known issue:
  2385. // A return, break, continue in try/except block will make the finally block not executed
  2386. bool has_finally = curr()->type == TK_FINALLY;
  2387. if(!has_finally) {
  2388. patches[patches_length++] = Ctx__emit_(ctx(), OP_JUMP_FORWARD, BC_NOARG, BC_KEEPLINE);
  2389. }
  2390. Ctx__exit_block(ctx());
  2391. if(has_finally) {
  2392. consume(TK_FINALLY);
  2393. Ctx__emit_(ctx(), OP_BEGIN_FINALLY, BC_NOARG, prev()->line);
  2394. // finally only, no except block
  2395. Ctx__enter_block(ctx(), CodeBlockType_FINALLY);
  2396. check(compile_block_body(self));
  2397. Ctx__exit_block(ctx());
  2398. Ctx__emit_(ctx(), OP_END_FINALLY, BC_NOARG, BC_KEEPLINE);
  2399. // re-raise if needed
  2400. Ctx__emit_(ctx(), OP_RE_RAISE, BC_NOARG, BC_KEEPLINE);
  2401. return NULL;
  2402. }
  2403. do {
  2404. if(patches_length == 8) {
  2405. return SyntaxError(self, "maximum number of except clauses reached");
  2406. }
  2407. py_Name as_name = 0;
  2408. consume(TK_EXCEPT);
  2409. if(is_expression(self, false)) {
  2410. // except <expr>:
  2411. check(EXPR(self));
  2412. Ctx__s_emit_top(ctx());
  2413. Ctx__emit_(ctx(), OP_EXCEPTION_MATCH, BC_NOARG, prev()->line);
  2414. if(match(TK_AS)) {
  2415. // except <expr> as <name>:
  2416. consume(TK_ID);
  2417. as_name = py_namev(Token__sv(prev()));
  2418. }
  2419. } else {
  2420. // except:
  2421. Ctx__emit_(ctx(), OP_LOAD_TRUE, BC_NOARG, BC_KEEPLINE);
  2422. }
  2423. int patch = Ctx__emit_(ctx(), OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
  2424. // on match
  2425. Ctx__emit_(ctx(), OP_BEGIN_EXC_HANDLING, BC_NOARG, BC_KEEPLINE);
  2426. if(as_name) {
  2427. Ctx__emit_(ctx(), OP_PUSH_EXCEPTION, BC_NOARG, BC_KEEPLINE);
  2428. Ctx__emit_store_name(ctx(), name_scope(self), as_name, BC_KEEPLINE);
  2429. }
  2430. Ctx__enter_block(ctx(), CodeBlockType_EXCEPT);
  2431. check(compile_block_body(self));
  2432. Ctx__exit_block(ctx());
  2433. Ctx__emit_(ctx(), OP_END_EXC_HANDLING, BC_NOARG, BC_KEEPLINE);
  2434. patches[patches_length++] = Ctx__emit_(ctx(), OP_JUMP_FORWARD, BC_NOARG, BC_KEEPLINE);
  2435. Ctx__patch_jump(ctx(), patch);
  2436. } while(curr()->type == TK_EXCEPT);
  2437. // no match, re-raise
  2438. // ...
  2439. // match one & handled, jump to the end
  2440. for(int i = 0; i < patches_length; i++)
  2441. Ctx__patch_jump(ctx(), patches[i]);
  2442. if(match(TK_FINALLY)) {
  2443. Ctx__emit_(ctx(), OP_BEGIN_FINALLY, BC_NOARG, prev()->line);
  2444. Ctx__enter_block(ctx(), CodeBlockType_FINALLY);
  2445. check(compile_block_body(self));
  2446. Ctx__exit_block(ctx());
  2447. Ctx__emit_(ctx(), OP_END_FINALLY, BC_NOARG, BC_KEEPLINE);
  2448. }
  2449. // re-raise if needed
  2450. Ctx__emit_(ctx(), OP_RE_RAISE, BC_NOARG, BC_KEEPLINE);
  2451. return NULL;
  2452. }
  2453. static Error* compile_stmt(Compiler* self) {
  2454. Error* err;
  2455. if(match(TK_CLASS)) {
  2456. check(compile_class(self, 0));
  2457. return NULL;
  2458. }
  2459. advance();
  2460. int kw_line = prev()->line; // backup line number
  2461. switch(prev()->type) {
  2462. case TK_BREAK: {
  2463. int curr_loop_block = Ctx__prepare_loop_divert(ctx(), kw_line, true);
  2464. if(curr_loop_block < 0) return SyntaxError(self, "'break' outside loop");
  2465. Ctx__emit_(ctx(), OP_LOOP_BREAK, curr_loop_block, kw_line);
  2466. consume_end_stmt();
  2467. break;
  2468. }
  2469. case TK_CONTINUE: {
  2470. int curr_loop_block = Ctx__prepare_loop_divert(ctx(), kw_line, false);
  2471. if(curr_loop_block < 0) return SyntaxError(self, "'continue' not properly in loop");
  2472. Ctx__emit_(ctx(), OP_LOOP_CONTINUE, curr_loop_block, kw_line);
  2473. consume_end_stmt();
  2474. break;
  2475. }
  2476. case TK_YIELD:
  2477. if(self->contexts.length <= 1) return SyntaxError(self, "'yield' outside function");
  2478. if(match_end_stmt(self)) {
  2479. Ctx__emit_(ctx(), OP_YIELD_VALUE, 1, kw_line);
  2480. } else {
  2481. check(EXPR_TUPLE(self));
  2482. Ctx__s_emit_top(ctx());
  2483. Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line);
  2484. consume_end_stmt();
  2485. }
  2486. break;
  2487. case TK_YIELD_FROM:
  2488. check(compile_yield_from(self, kw_line));
  2489. Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, kw_line);
  2490. consume_end_stmt();
  2491. break;
  2492. case TK_RETURN:
  2493. if(self->contexts.length <= 1) return SyntaxError(self, "'return' outside function");
  2494. if(match_end_stmt(self)) {
  2495. Ctx__emit_(ctx(), OP_RETURN_VALUE, 1, kw_line);
  2496. } else {
  2497. check(EXPR_TUPLE(self));
  2498. Ctx__s_emit_top(ctx());
  2499. consume_end_stmt();
  2500. Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, kw_line);
  2501. }
  2502. break;
  2503. /*************************************************/
  2504. case TK_IF: check(compile_if_stmt(self)); break;
  2505. case TK_MATCH: {
  2506. c11_vector patches;
  2507. c11_vector__ctor(&patches, sizeof(int));
  2508. check(compile_match_case(self, &patches));
  2509. c11_vector__dtor(&patches);
  2510. break;
  2511. }
  2512. case TK_WHILE: check(compile_while_loop(self)); break;
  2513. case TK_FOR: check(compile_for_loop(self)); break;
  2514. case TK_IMPORT: check(compile_normal_import(self)); break;
  2515. case TK_FROM: {
  2516. c11_sbuf buf;
  2517. c11_sbuf__ctor(&buf);
  2518. err = compile_from_import(&buf, self);
  2519. c11_sbuf__dtor(&buf);
  2520. if(err) return err;
  2521. break;
  2522. }
  2523. case TK_DEF: check(compile_function(self, 0)); break;
  2524. case TK_DECORATOR: check(compile_decorated(self)); break;
  2525. case TK_TRY: check(compile_try_except(self)); break;
  2526. case TK_PASS: consume_end_stmt(); break;
  2527. /*************************************************/
  2528. case TK_ASSERT: {
  2529. check(EXPR(self)); // condition
  2530. Ctx__s_emit_top(ctx());
  2531. int index = Ctx__emit_(ctx(), OP_POP_JUMP_IF_TRUE, BC_NOARG, kw_line);
  2532. int has_msg = 0;
  2533. if(match(TK_COMMA)) {
  2534. check(EXPR(self)); // message
  2535. Ctx__s_emit_top(ctx());
  2536. has_msg = 1;
  2537. }
  2538. Ctx__emit_(ctx(), OP_RAISE_ASSERT, has_msg, kw_line);
  2539. Ctx__patch_jump(ctx(), index);
  2540. consume_end_stmt();
  2541. break;
  2542. }
  2543. case TK_GLOBAL:
  2544. do {
  2545. consume(TK_ID);
  2546. py_Name name = py_namev(Token__sv(prev()));
  2547. c11_smallmap_n2d__set(&ctx()->global_names, name, 0);
  2548. } while(match(TK_COMMA));
  2549. consume_end_stmt();
  2550. break;
  2551. case TK_RAISE: {
  2552. check(EXPR(self));
  2553. Ctx__s_emit_top(ctx());
  2554. Ctx__emit_(ctx(), OP_RAISE, BC_NOARG, kw_line);
  2555. consume_end_stmt();
  2556. } break;
  2557. case TK_DEL: {
  2558. check(EXPR_TUPLE(self));
  2559. Expr* e = Ctx__s_top(ctx());
  2560. if(!vtemit_del(e, ctx())) return SyntaxError(self, "invalid syntax");
  2561. Ctx__s_pop(ctx());
  2562. consume_end_stmt();
  2563. } break;
  2564. case TK_WITH: {
  2565. check(EXPR(self)); // [ <expr> ]
  2566. Ctx__s_emit_top(ctx());
  2567. Ctx__enter_block(ctx(), CodeBlockType_WITH);
  2568. NameExpr* as_name = NULL;
  2569. if(match(TK_AS)) {
  2570. consume(TK_ID);
  2571. py_Name name = py_namev(Token__sv(prev()));
  2572. as_name = NameExpr__new(prev()->line, name, name_scope(self));
  2573. }
  2574. Ctx__emit_(ctx(), OP_WITH_ENTER, BC_NOARG, prev()->line);
  2575. // [ <expr> <expr>.__enter__() ]
  2576. if(as_name) {
  2577. bool ok = vtemit_store((Expr*)as_name, ctx());
  2578. vtdelete((Expr*)as_name);
  2579. if(!ok) return SyntaxError(self, "invalid syntax");
  2580. } else {
  2581. // discard `__enter__()`'s return value
  2582. Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
  2583. }
  2584. check(compile_block_body(self));
  2585. Ctx__emit_(ctx(), OP_WITH_EXIT, BC_NOARG, prev()->line);
  2586. Ctx__exit_block(ctx());
  2587. } break;
  2588. /*************************************************/
  2589. // handle dangling expression or assignment
  2590. default: {
  2591. // do revert since we have pre-called advance() at the beginning
  2592. --self->i;
  2593. check(EXPR_TUPLE(self));
  2594. bool is_typed_name = false; // e.g. x: int
  2595. // eat variable's type hint if it is a single name
  2596. const ExprVt* top_vt = Ctx__s_top(ctx())->vt;
  2597. if(top_vt->is_name || top_vt->is_attrib) {
  2598. if(match(TK_COLON)) {
  2599. c11_sv type_hint;
  2600. check(consume_type_hints_sv(self, &type_hint));
  2601. is_typed_name = true;
  2602. if(ctx()->is_compiling_class && top_vt->is_name) {
  2603. NameExpr* ne = (NameExpr*)Ctx__s_top(ctx());
  2604. int index = Ctx__add_const_string(ctx(), type_hint);
  2605. Ctx__emit_(ctx(), OP_LOAD_CONST, index, BC_KEEPLINE);
  2606. Ctx__emit_(ctx(),
  2607. OP_ADD_CLASS_ANNOTATION,
  2608. Ctx__add_name(ctx(), ne->name),
  2609. BC_KEEPLINE);
  2610. }
  2611. }
  2612. }
  2613. bool is_assign = false;
  2614. check(try_compile_assignment(self, &is_assign));
  2615. if(!is_assign) {
  2616. if(Ctx__s_size(ctx()) > 0 && Ctx__s_top(ctx())->vt->is_starred) {
  2617. return SyntaxError(self, "can't use starred expression here");
  2618. }
  2619. if(!is_typed_name) {
  2620. Ctx__s_emit_top(ctx());
  2621. if((mode() == SINGLE_MODE) && name_scope(self) == NAME_GLOBAL) {
  2622. Ctx__emit_(ctx(), OP_PRINT_EXPR, BC_NOARG, BC_KEEPLINE);
  2623. } else {
  2624. Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
  2625. }
  2626. } else {
  2627. Ctx__s_pop(ctx());
  2628. }
  2629. }
  2630. consume_end_stmt();
  2631. break;
  2632. }
  2633. }
  2634. return NULL;
  2635. }
  2636. /////////////////////////////////////////////////////////////////
  2637. Error* Compiler__compile(Compiler* self, CodeObject* out) {
  2638. // make sure it is the first time to compile
  2639. assert(self->i == 0);
  2640. // make sure the first token is @sof
  2641. assert(tk(0)->type == TK_SOF);
  2642. push_global_context(self, out);
  2643. advance(); // skip @sof, so prev() is always valid
  2644. match_newlines(); // skip possible leading '\n'
  2645. Error* err;
  2646. if(mode() == EVAL_MODE) {
  2647. check(EXPR_TUPLE(self));
  2648. Ctx__s_emit_top(ctx());
  2649. consume(TK_EOF);
  2650. Ctx__emit_(ctx(), OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
  2651. check(pop_context(self));
  2652. return NULL;
  2653. }
  2654. while(!match(TK_EOF)) {
  2655. check(compile_stmt(self));
  2656. match_newlines();
  2657. }
  2658. check(pop_context(self));
  2659. return NULL;
  2660. }
  2661. Error* pk_compile(SourceData_ src, CodeObject* out) {
  2662. Token* tokens;
  2663. int tokens_length;
  2664. Error* err = Lexer__process(src, &tokens, &tokens_length);
  2665. if(err) return err;
  2666. #if 0
  2667. Token* data = (Token*)tokens.data;
  2668. printf("%s\n", src->filename->data);
  2669. for(int i = 0; i < tokens.length; i++) {
  2670. Token* t = data + i;
  2671. c11_string* tmp = c11_string__new2(t->start, t->length);
  2672. if(t->value.index == TokenValue_STR) {
  2673. const char* value_str = t->value._str->data;
  2674. printf("[%d] %s: %s (value._str=%s)\n",
  2675. t->line,
  2676. TokenSymbols[t->type],
  2677. tmp->data,
  2678. value_str);
  2679. } else {
  2680. printf("[%d] %s: %s\n", t->line, TokenSymbols[t->type], tmp->data);
  2681. }
  2682. c11_string__delete(tmp);
  2683. }
  2684. #endif
  2685. Compiler compiler;
  2686. Compiler__ctor(&compiler, src, tokens, tokens_length);
  2687. CodeObject__ctor(out, src, c11_string__sv(src->filename));
  2688. err = Compiler__compile(&compiler, out);
  2689. if(err) {
  2690. // dispose the code object if error occurs
  2691. CodeObject__dtor(out);
  2692. }
  2693. Compiler__dtor(&compiler);
  2694. return err;
  2695. }
  2696. // clang-format off
  2697. const static PrattRule rules[TK__COUNT__] = {
  2698. // http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
  2699. [TK_DOT] = { NULL, exprAttrib, PREC_PRIMARY },
  2700. [TK_LPAREN] = { exprGroup, exprCall, PREC_PRIMARY },
  2701. [TK_LBRACKET] = { exprList, exprSubscr, PREC_PRIMARY },
  2702. [TK_MOD] = { NULL, exprBinaryOp, PREC_FACTOR },
  2703. [TK_ADD] = { NULL, exprBinaryOp, PREC_TERM },
  2704. [TK_SUB] = { exprUnaryOp, exprBinaryOp, PREC_TERM },
  2705. [TK_MUL] = { exprUnaryOp, exprBinaryOp, PREC_FACTOR },
  2706. [TK_INVERT] = { exprUnaryOp, NULL, PREC_UNARY },
  2707. [TK_DIV] = { NULL, exprBinaryOp, PREC_FACTOR },
  2708. [TK_FLOORDIV] = { NULL, exprBinaryOp, PREC_FACTOR },
  2709. [TK_POW] = { exprUnaryOp, exprBinaryOp, PREC_EXPONENT },
  2710. [TK_GT] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2711. [TK_LT] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2712. [TK_EQ] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2713. [TK_NE] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2714. [TK_GE] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2715. [TK_LE] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2716. [TK_IN] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2717. [TK_IS] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2718. [TK_LSHIFT] = { NULL, exprBinaryOp, PREC_BITWISE_SHIFT },
  2719. [TK_RSHIFT] = { NULL, exprBinaryOp, PREC_BITWISE_SHIFT },
  2720. [TK_AND] = { NULL, exprBinaryOp, PREC_BITWISE_AND },
  2721. [TK_OR] = { NULL, exprBinaryOp, PREC_BITWISE_OR },
  2722. [TK_XOR] = { NULL, exprBinaryOp, PREC_BITWISE_XOR },
  2723. [TK_DECORATOR] = { NULL, exprBinaryOp, PREC_FACTOR },
  2724. [TK_IF] = { NULL, exprTernary, PREC_TERNARY },
  2725. [TK_NOT_IN] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2726. [TK_IS_NOT] = { NULL, exprBinaryOp, PREC_COMPARISION },
  2727. [TK_AND_KW ] = { NULL, exprAnd, PREC_LOGICAL_AND },
  2728. [TK_OR_KW] = { NULL, exprOr, PREC_LOGICAL_OR },
  2729. [TK_NOT_KW] = { exprNot, NULL, PREC_LOGICAL_NOT },
  2730. [TK_TRUE] = { exprLiteral0 },
  2731. [TK_FALSE] = { exprLiteral0 },
  2732. [TK_NONE] = { exprLiteral0 },
  2733. [TK_DOTDOTDOT] = { exprLiteral0 },
  2734. [TK_LAMBDA] = { exprLambda, },
  2735. [TK_ID] = { exprName, },
  2736. [TK_NUM] = { exprLiteral, },
  2737. [TK_STR] = { exprLiteral, },
  2738. [TK_FSTR_BEGIN] = { exprFString, },
  2739. [TK_IMAG] = { exprImag, },
  2740. [TK_BYTES] = { exprBytes, },
  2741. [TK_LBRACE] = { exprMap },
  2742. [TK_COLON] = { exprSlice0, exprSlice1, PREC_PRIMARY }
  2743. };
  2744. // clang-format on
  2745. #undef vtcall
  2746. #undef vtemit_
  2747. #undef vtemit_del
  2748. #undef vtemit_store
  2749. #undef vtemit_inplace
  2750. #undef vtemit_istore
  2751. #undef vtdelete
  2752. #undef EXPR_COMMON_HEADER
  2753. #undef is_compare_expr
  2754. #undef tk
  2755. #undef prev
  2756. #undef curr
  2757. #undef next
  2758. #undef advance
  2759. #undef mode
  2760. #undef ctx
  2761. #undef match_newlines
  2762. #undef consume
  2763. #undef consume_end_stmt
  2764. #undef check
  2765. #undef match