compiler.c 93 KB

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