compiler.c 95 KB

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