compiler.c 100 KB

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