compiler.c 94 KB

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