compiler.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. #include "pocketpy/compiler/compiler.hpp"
  2. #include "pocketpy/common/config.h"
  3. #include "pocketpy/compiler/expr.hpp"
  4. #include "pocketpy/interpreter/vm.hpp"
  5. #include "pocketpy/objects/codeobject.hpp"
  6. #include <cstdarg>
  7. namespace pkpy {
  8. #define consume(expected) if(!match(expected)) return SyntaxError("expected '%s', got '%s'", pk_TokenSymbols[expected], pk_TokenSymbols[curr().type]);
  9. #define consume_end_stmt() if(!match_end_stmt()) return SyntaxError("expected statement end")
  10. #define check_newlines_repl() { bool __nml; match_newlines(&__nml); if(__nml) return NeedMoreLines(); }
  11. #define check(B) if((err = B)) return err
  12. PrattRule Compiler::rules[TK__COUNT__];
  13. NameScope Compiler::name_scope() const noexcept{
  14. auto s = contexts.size() > 1 ? NAME_LOCAL : NAME_GLOBAL;
  15. if(unknown_global_scope && s == NAME_GLOBAL) s = NAME_UNKNOWN;
  16. return s;
  17. }
  18. CodeObject* Compiler::push_global_context() noexcept{
  19. CodeObject* co = CodeObject__new(lexer.src, py_Str__sv(&lexer.src->filename));
  20. co->start_line = __i == 0 ? 1 : prev().line;
  21. contexts.push_back(CodeEmitContext(vm, co, contexts.size()));
  22. return co;
  23. }
  24. FuncDecl_ Compiler::push_f_context(c11_string name, int* out_index) noexcept{
  25. FuncDecl_ decl = FuncDecl__rcnew(lexer.src, name);
  26. decl->code->start_line = __i == 0 ? 1 : prev().line;
  27. decl->nested = name_scope() == NAME_LOCAL;
  28. // add_func_decl
  29. CodeEmitContext* ctx = &contexts.back();
  30. c11_vector__push(FuncDecl_, &ctx->co->func_decls, decl);
  31. *out_index = ctx->co->func_decls.count - 1;
  32. // push new context
  33. contexts.push_back(CodeEmitContext(vm, decl->code, contexts.size()));
  34. contexts.back().func = decl;
  35. return decl;
  36. }
  37. Error* Compiler::pop_context() noexcept{
  38. assert(ctx()->s_size() == 0);
  39. // add a `return None` in the end as a guard
  40. // previously, we only do this if the last opcode is not a return
  41. // however, this is buggy...since there may be a jump to the end (out of bound) even if the last opcode is a return
  42. ctx()->emit_(OP_RETURN_VALUE, 1, BC_KEEPLINE, true);
  43. // find the last valid token
  44. int j = __i - 1;
  45. while(tk(j).type == TK_EOL || tk(j).type == TK_DEDENT || tk(j).type == TK_EOF)
  46. j--;
  47. ctx()->co->end_line = tk(j).line;
  48. // some check here
  49. auto& codes = ctx()->co->codes;
  50. if(ctx()->co->nlocals > PK_MAX_CO_VARNAMES) {
  51. return SyntaxError("maximum number of local variables exceeded");
  52. }
  53. if(ctx()->co->consts.count > 65530) {
  54. return SyntaxError("maximum number of constants exceeded");
  55. }
  56. // pre-compute LOOP_BREAK and LOOP_CONTINUE
  57. for(int i = 0; i < codes.count; i++) {
  58. Bytecode* bc = c11__at(Bytecode, &codes, i);
  59. if(bc->op == OP_LOOP_CONTINUE) {
  60. CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
  61. Bytecode__set_signed_arg(bc, block->start - i);
  62. } else if(bc->op == OP_LOOP_BREAK) {
  63. CodeBlock* block = c11__at(CodeBlock, &ctx()->co->blocks, bc->arg);
  64. Bytecode__set_signed_arg(bc, (block->end2 != -1 ? block->end2 : block->end) - i);
  65. }
  66. }
  67. // pre-compute func->is_simple
  68. FuncDecl* func = contexts.back().func;
  69. if(func) {
  70. // check generator
  71. c11_vector__foreach(Bytecode, &func->code->codes, bc) {
  72. if(bc->op == OP_YIELD_VALUE || bc->op == OP_FOR_ITER_YIELD_VALUE) {
  73. func->type = FuncType_GENERATOR;
  74. c11_vector__foreach(Bytecode, &func->code->codes, bc) {
  75. if(bc->op == OP_RETURN_VALUE && bc->arg == BC_NOARG) {
  76. return SyntaxError("'return' with argument inside generator function");
  77. }
  78. }
  79. break;
  80. }
  81. }
  82. if(func->type == FuncType_UNSET) {
  83. bool is_simple = true;
  84. if(func->kwargs.count > 0) is_simple = false;
  85. if(func->starred_arg >= 0) is_simple = false;
  86. if(func->starred_kwarg >= 0) is_simple = false;
  87. if(is_simple) {
  88. func->type = FuncType_SIMPLE;
  89. bool is_empty = false;
  90. if(func->code->codes.count == 1) {
  91. Bytecode bc = c11__getitem(Bytecode, &func->code->codes, 0);
  92. if(bc.op == OP_RETURN_VALUE && bc.arg == 1) {
  93. is_empty = true;
  94. }
  95. }
  96. if(is_empty) func->type = FuncType_EMPTY;
  97. } else
  98. func->type = FuncType_NORMAL;
  99. }
  100. assert(func->type != FuncType_UNSET);
  101. }
  102. contexts.back().s_clean();
  103. contexts.pop_back();
  104. return NULL;
  105. }
  106. void Compiler::init_pratt_rules() noexcept{
  107. static bool initialized = false;
  108. if(initialized) return;
  109. initialized = true;
  110. }
  111. bool Compiler::match(TokenIndex expected) noexcept{
  112. if(curr().type != expected) return false;
  113. advance();
  114. return true;
  115. }
  116. bool Compiler::match_newlines(bool* need_more_lines) noexcept{
  117. bool consumed = false;
  118. if(curr().type == TK_EOL) {
  119. while(curr().type == TK_EOL) advance();
  120. consumed = true;
  121. }
  122. if(need_more_lines) {
  123. *need_more_lines = (mode() == REPL_MODE && curr().type == TK_EOF);
  124. }
  125. return consumed;
  126. }
  127. bool Compiler::match_end_stmt() noexcept{
  128. if(match(TK_SEMICOLON)) {
  129. match_newlines();
  130. return true;
  131. }
  132. if(match_newlines() || curr().type == TK_EOF) return true;
  133. if(curr().type == TK_DEDENT) return true;
  134. return false;
  135. }
  136. Error* Compiler::EXPR_TUPLE(bool allow_slice) noexcept{
  137. Error* err;
  138. check(parse_expression(PREC_LOWEST + 1, allow_slice));
  139. if(!match(TK_COMMA)) return NULL;
  140. // tuple expression
  141. int count = 1;
  142. do {
  143. if(curr().brackets_level) check_newlines_repl()
  144. if(!is_expression(allow_slice)) break;
  145. check(parse_expression(PREC_LOWEST + 1, allow_slice));
  146. count += 1;
  147. if(curr().brackets_level) check_newlines_repl();
  148. } while(match(TK_COMMA));
  149. TupleExpr* e = make_expr<TupleExpr>(count);
  150. for(int i=count-1; i>=0; i--)
  151. e->items[i] = ctx()->s_popx();
  152. ctx()->s_push(e);
  153. return NULL;
  154. }
  155. Error* Compiler::EXPR_VARS() noexcept{
  156. int count = 0;
  157. do {
  158. consume(TK_ID);
  159. ctx()->s_push(make_expr<NameExpr>(prev().str(), name_scope()));
  160. count += 1;
  161. } while(match(TK_COMMA));
  162. if(count > 1){
  163. TupleExpr* e = make_expr<TupleExpr>(count);
  164. for(int i=count-1; i>=0; i--)
  165. e->items[i] = ctx()->s_popx();
  166. ctx()->s_push(e);
  167. }
  168. return NULL;
  169. }
  170. Error* Compiler::exprLiteral() noexcept{
  171. ctx()->s_push(make_expr<LiteralExpr>(prev().value));
  172. return NULL;
  173. }
  174. Error* Compiler::exprLong() noexcept{
  175. ctx()->s_push(make_expr<LongExpr>(prev().str()));
  176. return NULL;
  177. }
  178. Error* Compiler::exprImag() noexcept{
  179. ctx()->s_push(make_expr<ImagExpr>(std::get<f64>(prev().value)));
  180. return NULL;
  181. }
  182. Error* Compiler::exprBytes() noexcept{
  183. ctx()->s_push(make_expr<BytesExpr>(std::get<Str>(prev().value)));
  184. return NULL;
  185. }
  186. Error* Compiler::exprFString() noexcept{
  187. ctx()->s_push(make_expr<FStringExpr>(std::get<Str>(prev().value)));
  188. return NULL;
  189. }
  190. Error* Compiler::exprLambda() noexcept{
  191. Error* err;
  192. int decl_index;
  193. FuncDecl_ decl = push_f_context({"<lambda>", 8}, &decl_index);
  194. int line = prev().line; // backup line
  195. if(!match(TK_COLON)) {
  196. check(_compile_f_args(decl, false));
  197. consume(TK_COLON);
  198. }
  199. // https://github.com/pocketpy/pocketpy/issues/37
  200. check(parse_expression(PREC_LAMBDA + 1));
  201. ctx()->s_emit_top();
  202. ctx()->emit_(OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
  203. check(pop_context());
  204. LambdaExpr* e = make_expr<LambdaExpr>(decl_index);
  205. e->line = line;
  206. ctx()->s_push(e);
  207. return NULL;
  208. }
  209. Error* Compiler::exprOr() noexcept{
  210. int line = prev().line;
  211. Error* err;
  212. check(parse_expression(PREC_LOGICAL_OR + 1));
  213. auto e = make_expr<OrExpr>();
  214. e->line = line;
  215. e->rhs = ctx()->s_popx();
  216. e->lhs = ctx()->s_popx();
  217. ctx()->s_push(e);
  218. return NULL;
  219. }
  220. Error* Compiler::exprAnd() noexcept{
  221. int line = prev().line;
  222. Error* err;
  223. check(parse_expression(PREC_LOGICAL_AND + 1));
  224. auto e = make_expr<AndExpr>();
  225. e->line = line;
  226. e->rhs = ctx()->s_popx();
  227. e->lhs = ctx()->s_popx();
  228. ctx()->s_push(e);
  229. return NULL;
  230. }
  231. Error* Compiler::exprTernary() noexcept{
  232. // [true_expr]
  233. Error* err;
  234. int line = prev().line;
  235. check(parse_expression(PREC_TERNARY + 1)); // [true_expr, cond]
  236. consume(TK_ELSE);
  237. check(parse_expression(PREC_TERNARY + 1)); // [true_expr, cond, false_expr]
  238. auto e = make_expr<TernaryExpr>();
  239. e->line = line;
  240. e->false_expr = ctx()->s_popx();
  241. e->cond = ctx()->s_popx();
  242. e->true_expr = ctx()->s_popx();
  243. ctx()->s_push(e);
  244. return NULL;
  245. }
  246. Error* Compiler::exprBinaryOp() noexcept{
  247. Error* err;
  248. int line = prev().line;
  249. TokenIndex op = prev().type;
  250. check(parse_expression(rules[op].precedence + 1));
  251. BinaryExpr* e = make_expr<BinaryExpr>(op);
  252. e->line = line;
  253. e->rhs = ctx()->s_popx();
  254. e->lhs = ctx()->s_popx();
  255. ctx()->s_push(e);
  256. return NULL;
  257. }
  258. Error* Compiler::exprNot() noexcept{
  259. Error* err;
  260. check(parse_expression(PREC_LOGICAL_NOT + 1));
  261. NotExpr* e = make_expr<NotExpr>(ctx()->s_popx());
  262. ctx()->s_push(e);
  263. return NULL;
  264. }
  265. Error* Compiler::exprUnaryOp() noexcept{
  266. Error* err;
  267. TokenIndex op = prev().type;
  268. check(parse_expression(PREC_UNARY + 1));
  269. switch(op) {
  270. case TK_SUB: ctx()->s_push(make_expr<NegatedExpr>(ctx()->s_popx())); break;
  271. case TK_INVERT: ctx()->s_push(make_expr<InvertExpr>(ctx()->s_popx())); break;
  272. case TK_MUL: ctx()->s_push(make_expr<StarredExpr>(ctx()->s_popx(), 1)); break;
  273. case TK_POW: ctx()->s_push(make_expr<StarredExpr>(ctx()->s_popx(), 2)); break;
  274. default: assert(false);
  275. }
  276. return NULL;
  277. }
  278. Error* Compiler::exprGroup() noexcept{
  279. Error* err;
  280. check_newlines_repl()
  281. check(EXPR_TUPLE()); // () is just for change precedence
  282. check_newlines_repl()
  283. consume(TK_RPAREN);
  284. if(ctx()->s_top()->is_tuple()) return NULL;
  285. Expr* g = make_expr<GroupedExpr>(ctx()->s_popx());
  286. ctx()->s_push(g);
  287. return NULL;
  288. }
  289. Error* Compiler::consume_comp(Opcode op0, Opcode op1) noexcept{
  290. // [expr]
  291. Error* err;
  292. bool has_cond = false;
  293. check(EXPR_VARS()); // [expr, vars]
  294. consume(TK_IN);
  295. check(parse_expression(PREC_TERNARY + 1)); // [expr, vars, iter]
  296. check_newlines_repl()
  297. if(match(TK_IF)) {
  298. check(parse_expression(PREC_TERNARY + 1)); // [expr, vars, iter, cond]
  299. has_cond = true;
  300. }
  301. CompExpr* ce = make_expr<CompExpr>(op0, op1);
  302. if(has_cond) ce->cond = ctx()->s_popx();
  303. ce->iter = ctx()->s_popx();
  304. ce->vars = ctx()->s_popx();
  305. ce->expr = ctx()->s_popx();
  306. ctx()->s_push(ce);
  307. check_newlines_repl()
  308. return NULL;
  309. }
  310. Error* Compiler::exprList() noexcept{
  311. Error* err;
  312. int line = prev().line;
  313. int count = 0;
  314. do {
  315. check_newlines_repl()
  316. if(curr().type == TK_RBRACKET) break;
  317. check(EXPR()); count += 1;
  318. check_newlines_repl()
  319. if(count == 1 && match(TK_FOR)) {
  320. check(consume_comp(OP_BUILD_LIST, OP_LIST_APPEND));
  321. consume(TK_RBRACKET);
  322. return NULL;
  323. }
  324. check_newlines_repl()
  325. } while(match(TK_COMMA));
  326. consume(TK_RBRACKET);
  327. ListExpr* e = make_expr<ListExpr>(count);
  328. e->line = line; // override line
  329. for(int i=count-1; i>=0; i--)
  330. e->items[i] = ctx()->s_popx();
  331. ctx()->s_push(e);
  332. return NULL;
  333. }
  334. Error* Compiler::exprMap() noexcept{
  335. Error* err;
  336. bool parsing_dict = false; // {...} may be dict or set
  337. int count = 0;
  338. do {
  339. check_newlines_repl()
  340. if(curr().type == TK_RBRACE) break;
  341. check(EXPR()); // [key]
  342. int star_level = ctx()->s_top()->star_level();
  343. if(star_level == 2 || curr().type == TK_COLON) { parsing_dict = true; }
  344. if(parsing_dict) {
  345. if(star_level == 2) {
  346. DictItemExpr* dict_item = make_expr<DictItemExpr>();
  347. dict_item->key = NULL;
  348. dict_item->value = ctx()->s_popx();
  349. ctx()->s_push(dict_item);
  350. } else {
  351. consume(TK_COLON);
  352. check(EXPR());
  353. DictItemExpr* dict_item = make_expr<DictItemExpr>();
  354. dict_item->value = ctx()->s_popx();
  355. dict_item->key = ctx()->s_popx();
  356. ctx()->s_push(dict_item);
  357. }
  358. }
  359. count += 1;
  360. check_newlines_repl()
  361. if(count == 1 && match(TK_FOR)) {
  362. if(parsing_dict){
  363. check(consume_comp(OP_BUILD_DICT, OP_DICT_ADD));
  364. }else{
  365. check(consume_comp(OP_BUILD_SET, OP_SET_ADD));
  366. }
  367. consume(TK_RBRACE);
  368. return NULL;
  369. }
  370. check_newlines_repl()
  371. } while(match(TK_COMMA));
  372. consume(TK_RBRACE);
  373. SequenceExpr* se;
  374. if(count == 0 || parsing_dict) {
  375. se = make_expr<DictExpr>(count);
  376. } else {
  377. se = make_expr<SetExpr>(count);
  378. }
  379. for(int i=count-1; i>=0; i--)
  380. se->items[i] = ctx()->s_popx();
  381. ctx()->s_push(se);
  382. return NULL;
  383. }
  384. Error* Compiler::exprCall() noexcept{
  385. Error* err;
  386. CallExpr* e = make_expr<CallExpr>();
  387. e->callable = ctx()->s_popx();
  388. ctx()->s_push(e); // push onto the stack in advance
  389. do {
  390. check_newlines_repl()
  391. if(curr().type == TK_RPAREN) break;
  392. if(curr().type == TK_ID && next().type == TK_ASSIGN) {
  393. consume(TK_ID);
  394. StrName key(prev().sv());
  395. consume(TK_ASSIGN);
  396. check(EXPR());
  397. e->kwargs.push_back({key, ctx()->s_popx()});
  398. } else {
  399. check(EXPR());
  400. if(ctx()->s_top()->star_level() == 2) {
  401. // **kwargs
  402. e->kwargs.push_back({"**", ctx()->s_popx()});
  403. } else {
  404. // positional argument
  405. if(!e->kwargs.empty()) return SyntaxError("positional argument follows keyword argument");
  406. e->args.push_back(ctx()->s_popx());
  407. }
  408. }
  409. check_newlines_repl()
  410. } while(match(TK_COMMA));
  411. consume(TK_RPAREN);
  412. return NULL;
  413. }
  414. Error* Compiler::exprName() noexcept{
  415. StrName name(prev().sv());
  416. NameScope scope = name_scope();
  417. if(ctx()->global_names.contains(name)) { scope = NAME_GLOBAL; }
  418. ctx()->s_push(make_expr<NameExpr>(name, scope));
  419. return NULL;
  420. }
  421. Error* Compiler::exprAttrib() noexcept{
  422. consume(TK_ID);
  423. ctx()->s_push(make_expr<AttribExpr>(ctx()->s_popx(), StrName::get(prev().sv())));
  424. return NULL;
  425. }
  426. Error* Compiler::exprSlice0() noexcept{
  427. Error* err;
  428. SliceExpr* slice = make_expr<SliceExpr>();
  429. ctx()->s_push(slice); // push onto the stack in advance
  430. if(is_expression()) { // :<stop>
  431. check(EXPR());
  432. slice->stop = ctx()->s_popx();
  433. // try optional step
  434. if(match(TK_COLON)) { // :<stop>:<step>
  435. check(EXPR());
  436. slice->step = ctx()->s_popx();
  437. }
  438. } else if(match(TK_COLON)) {
  439. if(is_expression()) { // ::<step>
  440. check(EXPR());
  441. slice->step = ctx()->s_popx();
  442. } // else ::
  443. } // else :
  444. return NULL;
  445. }
  446. Error* Compiler::exprSlice1() noexcept{
  447. Error* err;
  448. SliceExpr* slice = make_expr<SliceExpr>();
  449. slice->start = ctx()->s_popx();
  450. ctx()->s_push(slice); // push onto the stack in advance
  451. if(is_expression()) { // <start>:<stop>
  452. check(EXPR());
  453. slice->stop = ctx()->s_popx();
  454. // try optional step
  455. if(match(TK_COLON)) { // <start>:<stop>:<step>
  456. check(EXPR());
  457. slice->step = ctx()->s_popx();
  458. }
  459. } else if(match(TK_COLON)) { // <start>::<step>
  460. check(EXPR());
  461. slice->step = ctx()->s_popx();
  462. } // else <start>:
  463. return NULL;
  464. }
  465. Error* Compiler::exprSubscr() noexcept{
  466. Error* err;
  467. int line = prev().line;
  468. check_newlines_repl()
  469. check(EXPR_TUPLE(true));
  470. check_newlines_repl()
  471. consume(TK_RBRACKET); // [lhs, rhs]
  472. SubscrExpr* e = make_expr<SubscrExpr>();
  473. e->line = line;
  474. e->rhs = ctx()->s_popx(); // [lhs]
  475. e->lhs = ctx()->s_popx(); // []
  476. ctx()->s_push(e);
  477. return NULL;
  478. }
  479. Error* Compiler::exprLiteral0() noexcept{
  480. ctx()->s_push(make_expr<Literal0Expr>(prev().type));
  481. return NULL;
  482. }
  483. Error* Compiler::compile_block_body(PrattCallback callback) noexcept{
  484. Error* err;
  485. if(!callback) callback = &Compiler::compile_stmt;
  486. consume(TK_COLON);
  487. if(curr().type != TK_EOL && curr().type != TK_EOF) {
  488. while(true) {
  489. check(compile_stmt());
  490. bool possible = curr().type != TK_EOL && curr().type != TK_EOF;
  491. if(prev().type != TK_SEMICOLON || !possible) break;
  492. }
  493. return NULL;
  494. }
  495. bool need_more_lines;
  496. bool consumed = match_newlines(&need_more_lines);
  497. if(need_more_lines) return NeedMoreLines();
  498. if(!consumed) return SyntaxError("expected a new line after ':'");
  499. consume(TK_INDENT);
  500. while(curr().type != TK_DEDENT) {
  501. match_newlines();
  502. check((this->*callback)());
  503. match_newlines();
  504. }
  505. consume(TK_DEDENT);
  506. return NULL;
  507. }
  508. // import a [as b]
  509. // import a [as b], c [as d]
  510. Error* Compiler::compile_normal_import() noexcept{
  511. do {
  512. consume(TK_ID);
  513. Str name = prev().str();
  514. ctx()->emit_(OP_IMPORT_PATH, ctx()->add_const_string(name.sv()), prev().line);
  515. if(match(TK_AS)) {
  516. consume(TK_ID);
  517. name = prev().str();
  518. }
  519. ctx()->emit_store_name(name_scope(), StrName(name), prev().line);
  520. } while(match(TK_COMMA));
  521. consume_end_stmt();
  522. return NULL;
  523. }
  524. // from a import b [as c], d [as e]
  525. // from a.b import c [as d]
  526. // from . import a [as b]
  527. // from .a import b [as c]
  528. // from ..a import b [as c]
  529. // from .a.b import c [as d]
  530. // from xxx import *
  531. Error* Compiler::compile_from_import() noexcept{
  532. int dots = 0;
  533. while(true) {
  534. switch(curr().type) {
  535. case TK_DOT: dots += 1; break;
  536. case TK_DOTDOT: dots += 2; break;
  537. case TK_DOTDOTDOT: dots += 3; break;
  538. default: goto __EAT_DOTS_END;
  539. }
  540. advance();
  541. }
  542. __EAT_DOTS_END:
  543. SStream ss;
  544. for(int i = 0; i < dots; i++)
  545. ss << '.';
  546. if(dots > 0) {
  547. // @id is optional if dots > 0
  548. if(match(TK_ID)) {
  549. ss << prev().sv();
  550. while(match(TK_DOT)) {
  551. consume(TK_ID);
  552. ss << "." << prev().sv();
  553. }
  554. }
  555. } else {
  556. // @id is required if dots == 0
  557. consume(TK_ID);
  558. ss << prev().sv();
  559. while(match(TK_DOT)) {
  560. consume(TK_ID);
  561. ss << "." << prev().sv();
  562. }
  563. }
  564. ctx()->emit_(OP_IMPORT_PATH, ctx()->add_const_string(ss.str().sv()), prev().line);
  565. consume(TK_IMPORT);
  566. if(match(TK_MUL)) {
  567. if(name_scope() != NAME_GLOBAL) return SyntaxError("from <module> import * can only be used in global scope");
  568. // pop the module and import __all__
  569. ctx()->emit_(OP_POP_IMPORT_STAR, BC_NOARG, prev().line);
  570. consume_end_stmt();
  571. return NULL;
  572. }
  573. do {
  574. ctx()->emit_(OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
  575. consume(TK_ID);
  576. Str name = prev().str();
  577. ctx()->emit_(OP_LOAD_ATTR, StrName(name).index, prev().line);
  578. if(match(TK_AS)) {
  579. consume(TK_ID);
  580. name = prev().str();
  581. }
  582. ctx()->emit_store_name(name_scope(), StrName(name), prev().line);
  583. } while(match(TK_COMMA));
  584. ctx()->emit_(OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
  585. consume_end_stmt();
  586. return NULL;
  587. }
  588. bool Compiler::is_expression(bool allow_slice) noexcept{
  589. PrattCallback prefix = rules[curr().type].prefix;
  590. return prefix != nullptr && (allow_slice || curr().type != TK_COLON);
  591. }
  592. Error* Compiler::parse_expression(int precedence, bool allow_slice) noexcept{
  593. PrattCallback prefix = rules[curr().type].prefix;
  594. if(prefix == nullptr || (curr().type == TK_COLON && !allow_slice)) {
  595. return SyntaxError("expected an expression, got %s", pk_TokenSymbols[curr().type]);
  596. }
  597. advance();
  598. Error* err;
  599. check((this->*prefix)());
  600. while(rules[curr().type].precedence >= precedence && (allow_slice || curr().type != TK_COLON)) {
  601. TokenIndex op = curr().type;
  602. advance();
  603. PrattCallback infix = rules[op].infix;
  604. assert(infix != nullptr);
  605. check((this->*infix)());
  606. }
  607. return NULL;
  608. }
  609. Error* Compiler::compile_if_stmt() noexcept{
  610. Error* err;
  611. check(EXPR()); // condition
  612. ctx()->s_emit_top();
  613. int patch = ctx()->emit_(OP_POP_JUMP_IF_FALSE, BC_NOARG, prev().line);
  614. err = compile_block_body();
  615. if(err) return err;
  616. if(match(TK_ELIF)) {
  617. int exit_patch = ctx()->emit_(OP_JUMP_FORWARD, BC_NOARG, prev().line);
  618. ctx()->patch_jump(patch);
  619. check(compile_if_stmt());
  620. ctx()->patch_jump(exit_patch);
  621. } else if(match(TK_ELSE)) {
  622. int exit_patch = ctx()->emit_(OP_JUMP_FORWARD, BC_NOARG, prev().line);
  623. ctx()->patch_jump(patch);
  624. check(compile_block_body());
  625. ctx()->patch_jump(exit_patch);
  626. } else {
  627. ctx()->patch_jump(patch);
  628. }
  629. return NULL;
  630. }
  631. Error* Compiler::compile_while_loop() noexcept{
  632. Error* err;
  633. CodeBlock* block = ctx()->enter_block(CodeBlockType_WHILE_LOOP);
  634. check(EXPR()); // condition
  635. ctx()->s_emit_top();
  636. int patch = ctx()->emit_(OP_POP_JUMP_IF_FALSE, BC_NOARG, prev().line);
  637. check(compile_block_body());
  638. ctx()->emit_(OP_LOOP_CONTINUE, ctx()->get_loop(), BC_KEEPLINE, true);
  639. ctx()->patch_jump(patch);
  640. ctx()->exit_block();
  641. // optional else clause
  642. if(match(TK_ELSE)) {
  643. check(compile_block_body());
  644. block->end2 = ctx()->co->codes.count;
  645. }
  646. return NULL;
  647. }
  648. Error* Compiler::compile_for_loop() noexcept{
  649. Error* err;
  650. check(EXPR_VARS()); // [vars]
  651. consume(TK_IN);
  652. check(EXPR_TUPLE()); // [vars, iter]
  653. ctx()->s_emit_top(); // [vars]
  654. ctx()->emit_(OP_GET_ITER_NEW, BC_NOARG, BC_KEEPLINE);
  655. CodeBlock* block = ctx()->enter_block(CodeBlockType_FOR_LOOP);
  656. int for_codei = ctx()->emit_(OP_FOR_ITER, ctx()->curr_iblock, BC_KEEPLINE);
  657. Expr* vars = ctx()->s_popx();
  658. bool ok = vars->emit_store(ctx());
  659. delete_expr(vars);
  660. if(!ok) return SyntaxError(); // this error occurs in `vars` instead of this line, but...nevermind
  661. ctx()->try_merge_for_iter_store(for_codei);
  662. check(compile_block_body());
  663. ctx()->emit_(OP_LOOP_CONTINUE, ctx()->get_loop(), BC_KEEPLINE, true);
  664. ctx()->exit_block();
  665. // optional else clause
  666. if(match(TK_ELSE)) {
  667. check(compile_block_body());
  668. block->end2 = ctx()->co->codes.count;
  669. }
  670. return NULL;
  671. }
  672. Error* Compiler::compile_try_except() noexcept{
  673. Error* err;
  674. ctx()->enter_block(CodeBlockType_TRY_EXCEPT);
  675. ctx()->emit_(OP_TRY_ENTER, BC_NOARG, prev().line);
  676. check(compile_block_body());
  677. small_vector_2<int, 8> patches;
  678. patches.push_back(ctx()->emit_(OP_JUMP_FORWARD, BC_NOARG, BC_KEEPLINE));
  679. ctx()->exit_block();
  680. int finally_entry = -1;
  681. if(curr().type != TK_FINALLY) {
  682. do {
  683. StrName as_name;
  684. consume(TK_EXCEPT);
  685. if(is_expression()) {
  686. check(EXPR()); // push assumed type on to the stack
  687. ctx()->s_emit_top();
  688. ctx()->emit_(OP_EXCEPTION_MATCH, BC_NOARG, prev().line);
  689. if(match(TK_AS)) {
  690. consume(TK_ID);
  691. as_name = StrName(prev().sv());
  692. }
  693. } else {
  694. ctx()->emit_(OP_LOAD_TRUE, BC_NOARG, BC_KEEPLINE);
  695. }
  696. int patch = ctx()->emit_(OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);
  697. // on match
  698. if(!as_name.empty()) {
  699. ctx()->emit_(OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
  700. ctx()->emit_store_name(name_scope(), as_name, BC_KEEPLINE);
  701. }
  702. // pop the exception
  703. ctx()->emit_(OP_POP_EXCEPTION, BC_NOARG, BC_KEEPLINE);
  704. check(compile_block_body());
  705. patches.push_back(ctx()->emit_(OP_JUMP_FORWARD, BC_NOARG, BC_KEEPLINE));
  706. ctx()->patch_jump(patch);
  707. } while(curr().type == TK_EXCEPT);
  708. }
  709. if(match(TK_FINALLY)) {
  710. int patch = ctx()->emit_(OP_JUMP_FORWARD, BC_NOARG, BC_KEEPLINE);
  711. finally_entry = ctx()->co->codes.count;
  712. check(compile_block_body());
  713. ctx()->emit_(OP_JUMP_ABSOLUTE_TOP, BC_NOARG, BC_KEEPLINE);
  714. ctx()->patch_jump(patch);
  715. }
  716. // no match, re-raise
  717. if(finally_entry != -1) {
  718. i64 target = ctx()->co->codes.count + 2;
  719. ctx()->emit_(OP_LOAD_CONST, ctx()->add_const(VAR(target)), BC_KEEPLINE);
  720. int i = ctx()->emit_(OP_JUMP_FORWARD, BC_NOARG, BC_KEEPLINE);
  721. Bytecode* bc = c11__at(Bytecode, &ctx()->co->codes, i);
  722. Bytecode__set_signed_arg(bc, finally_entry - i);
  723. }
  724. ctx()->emit_(OP_RE_RAISE, BC_NOARG, BC_KEEPLINE);
  725. // no exception or no match, jump to the end
  726. for(int patch: patches)
  727. ctx()->patch_jump(patch);
  728. if(finally_entry != -1) {
  729. i64 target = ctx()->co->codes.count + 2;
  730. ctx()->emit_(OP_LOAD_CONST, ctx()->add_const(VAR(target)), BC_KEEPLINE);
  731. int i = ctx()->emit_(OP_JUMP_FORWARD, BC_NOARG, BC_KEEPLINE);
  732. Bytecode* bc = c11__at(Bytecode, &ctx()->co->codes, i);
  733. Bytecode__set_signed_arg(bc, finally_entry - i);
  734. }
  735. return NULL;
  736. }
  737. Error* Compiler::compile_decorated() noexcept{
  738. Error* err;
  739. int count = 0;
  740. do {
  741. check(EXPR());
  742. count += 1;
  743. bool need_more_lines;
  744. bool consumed = match_newlines(&need_more_lines);
  745. if(need_more_lines) return NeedMoreLines();
  746. if(!consumed) return SyntaxError("expected a newline after '@'");
  747. } while(match(TK_DECORATOR));
  748. if(match(TK_CLASS)) {
  749. check(compile_class(count));
  750. } else {
  751. consume(TK_DEF);
  752. check(compile_function(count));
  753. }
  754. return NULL;
  755. }
  756. Error* Compiler::try_compile_assignment(bool* is_assign) noexcept{
  757. Error* err;
  758. switch(curr().type) {
  759. case TK_IADD:
  760. case TK_ISUB:
  761. case TK_IMUL:
  762. case TK_IDIV:
  763. case TK_IFLOORDIV:
  764. case TK_IMOD:
  765. case TK_ILSHIFT:
  766. case TK_IRSHIFT:
  767. case TK_IAND:
  768. case TK_IOR:
  769. case TK_IXOR: {
  770. if(ctx()->s_top()->is_starred()) return SyntaxError();
  771. if(ctx()->is_compiling_class){
  772. return SyntaxError("can't use inplace operator in class definition");
  773. }
  774. advance();
  775. // a[x] += 1; a and x should be evaluated only once
  776. // a.x += 1; a should be evaluated only once
  777. // -1 to remove =; inplace=true
  778. int line = prev().line;
  779. TokenIndex op = (TokenIndex)(prev().type - 1);
  780. // [lhs]
  781. check(EXPR_TUPLE()); // [lhs, rhs]
  782. if(ctx()->s_top()->is_starred()) return SyntaxError();
  783. BinaryExpr* e = make_expr<BinaryExpr>(op, true);
  784. e->line = line;
  785. e->rhs = ctx()->s_popx(); // [lhs]
  786. e->lhs = ctx()->s_popx(); // []
  787. e->emit_(ctx());
  788. bool ok = e->lhs->emit_store_inplace(ctx());
  789. delete_expr(e);
  790. if(!ok) return SyntaxError();
  791. *is_assign = true;
  792. return NULL;
  793. }
  794. case TK_ASSIGN: {
  795. int n = 0;
  796. while(match(TK_ASSIGN)) {
  797. check(EXPR_TUPLE());
  798. n += 1;
  799. }
  800. // stack size is n+1
  801. ctx()->s_emit_top(); // emit and pop
  802. for(int j = 1; j < n; j++)
  803. ctx()->emit_(OP_DUP_TOP, BC_NOARG, BC_KEEPLINE);
  804. for(int j = 0; j < n; j++) {
  805. if(ctx()->s_top()->is_starred()) return SyntaxError();
  806. bool ok = ctx()->s_top()->emit_store(ctx());
  807. ctx()->s_pop();
  808. if(!ok) return SyntaxError();
  809. }
  810. *is_assign = true;
  811. return NULL;
  812. }
  813. default: *is_assign = false;
  814. }
  815. return NULL;
  816. }
  817. Error* Compiler::compile_stmt() noexcept{
  818. Error* err;
  819. if(match(TK_CLASS)) {
  820. check(compile_class());
  821. return NULL;
  822. }
  823. advance();
  824. int kw_line = prev().line; // backup line number
  825. int curr_loop_block = ctx()->get_loop();
  826. switch(prev().type) {
  827. case TK_BREAK:
  828. if(curr_loop_block < 0) return SyntaxError("'break' outside loop");
  829. ctx()->emit_(OP_LOOP_BREAK, curr_loop_block, kw_line);
  830. consume_end_stmt();
  831. break;
  832. case TK_CONTINUE:
  833. if(curr_loop_block < 0) return SyntaxError("'continue' not properly in loop");
  834. ctx()->emit_(OP_LOOP_CONTINUE, curr_loop_block, kw_line);
  835. consume_end_stmt();
  836. break;
  837. case TK_YIELD:
  838. if(contexts.size() <= 1) return SyntaxError("'yield' outside function");
  839. check(EXPR_TUPLE());
  840. ctx()->s_emit_top();
  841. ctx()->emit_(OP_YIELD_VALUE, BC_NOARG, kw_line);
  842. consume_end_stmt();
  843. break;
  844. case TK_YIELD_FROM:
  845. if(contexts.size() <= 1) return SyntaxError("'yield from' outside function");
  846. check(EXPR_TUPLE());
  847. ctx()->s_emit_top();
  848. ctx()->emit_(OP_GET_ITER_NEW, BC_NOARG, kw_line);
  849. ctx()->enter_block(CodeBlockType_FOR_LOOP);
  850. ctx()->emit_(OP_FOR_ITER_YIELD_VALUE, BC_NOARG, kw_line);
  851. ctx()->emit_(OP_LOOP_CONTINUE, ctx()->get_loop(), kw_line);
  852. ctx()->exit_block();
  853. consume_end_stmt();
  854. break;
  855. case TK_RETURN:
  856. if(contexts.size() <= 1) return SyntaxError("'return' outside function");
  857. if(match_end_stmt()) {
  858. ctx()->emit_(OP_RETURN_VALUE, 1, kw_line);
  859. } else {
  860. check(EXPR_TUPLE());
  861. ctx()->s_emit_top();
  862. consume_end_stmt();
  863. ctx()->emit_(OP_RETURN_VALUE, BC_NOARG, kw_line);
  864. }
  865. break;
  866. /*************************************************/
  867. case TK_IF: check(compile_if_stmt()); break;
  868. case TK_WHILE: check(compile_while_loop()); break;
  869. case TK_FOR: check(compile_for_loop()); break;
  870. case TK_IMPORT: check(compile_normal_import()); break;
  871. case TK_FROM: check(compile_from_import()); break;
  872. case TK_DEF: check(compile_function()); break;
  873. case TK_DECORATOR: check(compile_decorated()); break;
  874. case TK_TRY: check(compile_try_except()); break;
  875. case TK_PASS: consume_end_stmt(); break;
  876. /*************************************************/
  877. case TK_ASSERT: {
  878. check(EXPR()); // condition
  879. ctx()->s_emit_top();
  880. int index = ctx()->emit_(OP_POP_JUMP_IF_TRUE, BC_NOARG, kw_line);
  881. int has_msg = 0;
  882. if(match(TK_COMMA)) {
  883. check(EXPR()); // message
  884. ctx()->s_emit_top();
  885. has_msg = 1;
  886. }
  887. ctx()->emit_(OP_RAISE_ASSERT, has_msg, kw_line);
  888. ctx()->patch_jump(index);
  889. consume_end_stmt();
  890. break;
  891. }
  892. case TK_GLOBAL:
  893. do {
  894. consume(TK_ID);
  895. ctx()->global_names.push_back(StrName(prev().sv()));
  896. } while(match(TK_COMMA));
  897. consume_end_stmt();
  898. break;
  899. case TK_RAISE: {
  900. check(EXPR());
  901. ctx()->s_emit_top();
  902. ctx()->emit_(OP_RAISE, BC_NOARG, kw_line);
  903. consume_end_stmt();
  904. } break;
  905. case TK_DEL: {
  906. check(EXPR_TUPLE());
  907. if(!ctx()->s_top()->emit_del(ctx())) return SyntaxError();
  908. ctx()->s_pop();
  909. consume_end_stmt();
  910. } break;
  911. case TK_WITH: {
  912. check(EXPR()); // [ <expr> ]
  913. ctx()->s_emit_top();
  914. ctx()->enter_block(CodeBlockType_CONTEXT_MANAGER);
  915. Expr* as_name = nullptr;
  916. if(match(TK_AS)) {
  917. consume(TK_ID);
  918. as_name = make_expr<NameExpr>(prev().str(), name_scope());
  919. }
  920. ctx()->emit_(OP_WITH_ENTER, BC_NOARG, prev().line);
  921. // [ <expr> <expr>.__enter__() ]
  922. if(as_name) {
  923. bool ok = as_name->emit_store(ctx());
  924. delete_expr(as_name);
  925. if(!ok) return SyntaxError();
  926. } else {
  927. ctx()->emit_(OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
  928. }
  929. check(compile_block_body());
  930. ctx()->emit_(OP_WITH_EXIT, BC_NOARG, prev().line);
  931. ctx()->exit_block();
  932. } break;
  933. /*************************************************/
  934. case TK_EQ: {
  935. consume(TK_ID);
  936. if(mode() != EXEC_MODE) return SyntaxError("'label' is only available in EXEC_MODE");
  937. if(!ctx()->add_label(prev().str())) {
  938. Str escaped(prev().str().escape());
  939. return SyntaxError("label %s already exists", escaped.c_str());
  940. }
  941. consume(TK_EQ);
  942. consume_end_stmt();
  943. } break;
  944. case TK_ARROW:
  945. consume(TK_ID);
  946. if(mode() != EXEC_MODE) return SyntaxError("'goto' is only available in EXEC_MODE");
  947. ctx()->emit_(OP_GOTO, StrName(prev().sv()).index, prev().line);
  948. consume_end_stmt();
  949. break;
  950. /*************************************************/
  951. // handle dangling expression or assignment
  952. default: {
  953. advance(-1); // do revert since we have pre-called advance() at the beginning
  954. check(EXPR_TUPLE());
  955. bool is_typed_name = false; // e.g. x: int
  956. // eat variable's type hint if it is a single name
  957. if(ctx()->s_top()->is_name()) {
  958. if(match(TK_COLON)) {
  959. check(consume_type_hints());
  960. is_typed_name = true;
  961. if(ctx()->is_compiling_class) {
  962. NameExpr* ne = static_cast<NameExpr*>(ctx()->s_top());
  963. ctx()->emit_(OP_ADD_CLASS_ANNOTATION, ne->name.index, BC_KEEPLINE);
  964. }
  965. }
  966. }
  967. bool is_assign = false;
  968. check(try_compile_assignment(&is_assign));
  969. if(!is_assign) {
  970. if(ctx()->s_size() > 0 && ctx()->s_top()->is_starred()) {
  971. return SyntaxError();
  972. }
  973. if(!is_typed_name) {
  974. ctx()->s_emit_top();
  975. if((mode() == CELL_MODE || mode() == REPL_MODE) && name_scope() == NAME_GLOBAL) {
  976. ctx()->emit_(OP_PRINT_EXPR, BC_NOARG, BC_KEEPLINE);
  977. } else {
  978. ctx()->emit_(OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
  979. }
  980. } else {
  981. ctx()->s_pop();
  982. }
  983. }
  984. consume_end_stmt();
  985. break;
  986. }
  987. }
  988. return NULL;
  989. }
  990. Error* Compiler::consume_type_hints() noexcept{
  991. Error* err;
  992. check(EXPR());
  993. ctx()->s_pop();
  994. return NULL;
  995. }
  996. Error* Compiler::compile_class(int decorators) noexcept{
  997. Error* err;
  998. consume(TK_ID);
  999. int namei = StrName(prev().sv()).index;
  1000. bool has_base = false;
  1001. if(match(TK_LPAREN)) {
  1002. if(is_expression()) {
  1003. check(EXPR());
  1004. has_base = true; // [base]
  1005. }
  1006. consume(TK_RPAREN);
  1007. }
  1008. if(!has_base) {
  1009. ctx()->emit_(OP_LOAD_NONE, BC_NOARG, prev().line);
  1010. } else {
  1011. ctx()->s_emit_top(); // []
  1012. }
  1013. ctx()->emit_(OP_BEGIN_CLASS, namei, BC_KEEPLINE);
  1014. for(auto& c: this->contexts) {
  1015. if(c.is_compiling_class) return SyntaxError("nested class is not allowed");
  1016. }
  1017. ctx()->is_compiling_class = true;
  1018. check(compile_block_body());
  1019. ctx()->is_compiling_class = false;
  1020. if(decorators > 0) {
  1021. ctx()->emit_(OP_BEGIN_CLASS_DECORATION, BC_NOARG, BC_KEEPLINE);
  1022. ctx()->s_emit_decorators(decorators);
  1023. ctx()->emit_(OP_END_CLASS_DECORATION, BC_NOARG, BC_KEEPLINE);
  1024. }
  1025. ctx()->emit_(OP_END_CLASS, namei, BC_KEEPLINE);
  1026. return NULL;
  1027. }
  1028. Error* Compiler::_compile_f_args(FuncDecl* decl, bool enable_type_hints) noexcept{
  1029. int state = 0; // 0 for args, 1 for *args, 2 for k=v, 3 for **kwargs
  1030. Error* err;
  1031. do {
  1032. if(state > 3) return SyntaxError();
  1033. if(state == 3) return SyntaxError("**kwargs should be the last argument");
  1034. match_newlines();
  1035. if(match(TK_MUL)) {
  1036. if(state < 1)
  1037. state = 1;
  1038. else
  1039. return SyntaxError("*args should be placed before **kwargs");
  1040. } else if(match(TK_POW)) {
  1041. state = 3;
  1042. }
  1043. consume(TK_ID);
  1044. StrName name(prev().sv());
  1045. // check duplicate argument name
  1046. uint16_t tmp_name;
  1047. c11_vector__foreach(int, &decl->args, j) {
  1048. tmp_name = c11__getitem(uint16_t, &decl->args, *j);
  1049. if(tmp_name == name.index) return SyntaxError("duplicate argument name");
  1050. }
  1051. c11_vector__foreach(FuncDeclKwArg, &decl->kwargs, kv) {
  1052. tmp_name = c11__getitem(uint16_t, &decl->code->varnames, kv->index);
  1053. if(tmp_name == name.index) return SyntaxError("duplicate argument name");
  1054. }
  1055. if(decl->starred_arg != -1) {
  1056. tmp_name = c11__getitem(uint16_t, &decl->code->varnames, decl->starred_arg);
  1057. if(tmp_name == name.index) return SyntaxError("duplicate argument name");
  1058. }
  1059. if(decl->starred_kwarg != -1) {
  1060. tmp_name = c11__getitem(uint16_t, &decl->code->varnames, decl->starred_kwarg);
  1061. if(tmp_name == name.index) return SyntaxError("duplicate argument name");
  1062. }
  1063. // eat type hints
  1064. if(enable_type_hints && match(TK_COLON)) check(consume_type_hints());
  1065. if(state == 0 && curr().type == TK_ASSIGN) state = 2;
  1066. int index = ctx()->add_varname(name);
  1067. switch(state) {
  1068. case 0:
  1069. c11_vector__push(int, &decl->args, index);
  1070. break;
  1071. case 1:
  1072. decl->starred_arg = index;
  1073. state += 1;
  1074. break;
  1075. case 2: {
  1076. consume(TK_ASSIGN);
  1077. PyVar value;
  1078. check(read_literal(&value));
  1079. if(value == nullptr) return SyntaxError("default argument must be a literal");
  1080. FuncDecl__add_kwarg(decl, index, name.index, (const ::PyVar*)&value);
  1081. } break;
  1082. case 3:
  1083. decl->starred_kwarg = index;
  1084. state += 1;
  1085. break;
  1086. }
  1087. } while(match(TK_COMMA));
  1088. return NULL;
  1089. }
  1090. Error* Compiler::compile_function(int decorators) noexcept{
  1091. Error* err;
  1092. consume(TK_ID);
  1093. std::string_view decl_name = prev().sv();
  1094. int decl_index;
  1095. FuncDecl_ decl = push_f_context({decl_name.data(), (int)decl_name.size()}, &decl_index);
  1096. consume(TK_LPAREN);
  1097. if(!match(TK_RPAREN)) {
  1098. check(_compile_f_args(decl, true));
  1099. consume(TK_RPAREN);
  1100. }
  1101. if(match(TK_ARROW)) check(consume_type_hints());
  1102. check(compile_block_body());
  1103. check(pop_context());
  1104. if(decl->code->codes.count >= 2) {
  1105. Bytecode* codes = (Bytecode*)decl->code->codes.data;
  1106. if(codes[0].op == OP_LOAD_CONST && codes[1].op == OP_POP_TOP) {
  1107. // handle optional docstring
  1108. PyVar* c = c11__at(PyVar, &decl->code->consts, codes[0].arg);
  1109. if(is_type(*c, vm->tp_str)) {
  1110. codes[0].op = OP_NO_OP;
  1111. codes[1].op = OP_NO_OP;
  1112. decl->docstring = PK_OBJ_GET(Str, *c).c_str();
  1113. }
  1114. }
  1115. }
  1116. ctx()->emit_(OP_LOAD_FUNCTION, decl_index, prev().line);
  1117. ctx()->s_emit_decorators(decorators);
  1118. if(!ctx()->is_compiling_class) {
  1119. NameExpr* e = make_expr<NameExpr>(StrName(decl_name), name_scope());
  1120. e->emit_store(ctx());
  1121. delete_expr(e);
  1122. } else {
  1123. int index = StrName(decl_name).index;
  1124. ctx()->emit_(OP_STORE_CLASS_ATTR, index, prev().line);
  1125. }
  1126. return NULL;
  1127. }
  1128. PyVar Compiler::to_object(const TokenValue& value) noexcept{
  1129. PyVar obj = nullptr;
  1130. if(std::holds_alternative<i64>(value)) { obj = VAR(std::get<i64>(value)); }
  1131. if(std::holds_alternative<f64>(value)) { obj = VAR(std::get<f64>(value)); }
  1132. if(std::holds_alternative<Str>(value)) { obj = VAR(std::get<Str>(value)); }
  1133. assert(obj != nullptr);
  1134. return obj;
  1135. }
  1136. Error* Compiler::read_literal(PyVar* out) noexcept{
  1137. Error* err;
  1138. advance();
  1139. switch(prev().type) {
  1140. case TK_SUB: {
  1141. consume(TK_NUM);
  1142. PyVar val = to_object(prev().value);
  1143. *out = vm->py_negate(val);
  1144. return NULL;
  1145. }
  1146. case TK_NUM: *out = to_object(prev().value); return NULL;
  1147. case TK_STR: *out = to_object(prev().value); return NULL;
  1148. case TK_TRUE: *out = VAR(true); return NULL;
  1149. case TK_FALSE: *out = VAR(false); return NULL;
  1150. case TK_NONE: *out = vm->None; return NULL;
  1151. case TK_DOTDOTDOT: *out = vm->Ellipsis; return NULL;
  1152. case TK_LPAREN: {
  1153. List cpnts;
  1154. while(true) {
  1155. PyVar elem;
  1156. check(read_literal(&elem));
  1157. cpnts.push_back(elem);
  1158. if(curr().type == TK_RPAREN) break;
  1159. consume(TK_COMMA);
  1160. if(curr().type == TK_RPAREN) break;
  1161. }
  1162. consume(TK_RPAREN);
  1163. *out = VAR(cpnts.to_tuple());
  1164. return NULL;
  1165. }
  1166. default: *out = nullptr; return NULL;
  1167. }
  1168. }
  1169. Compiler::Compiler(VM* vm, std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope) noexcept:
  1170. lexer(vm, source, filename, mode){
  1171. this->vm = vm;
  1172. this->unknown_global_scope = unknown_global_scope;
  1173. init_pratt_rules();
  1174. }
  1175. Error* Compiler::compile(CodeObject** out) noexcept{
  1176. assert(__i == 0); // make sure it is the first time to compile
  1177. Error* err;
  1178. check(lexer.run());
  1179. // if(lexer.src.filename()[0] != '<'){
  1180. // printf("%s\n", lexer.src.filename().c_str());
  1181. // for(int i=0; i<lexer.nexts.size(); i++){
  1182. // printf("%s: %s\n", pk_TokenSymbols[tk(i).type], tk(i).str().escape().c_str());
  1183. // }
  1184. // }
  1185. CodeObject* code = push_global_context();
  1186. assert(curr().type == TK_SOF);
  1187. advance(); // skip @sof, so prev() is always valid
  1188. match_newlines(); // skip possible leading '\n'
  1189. if(mode() == EVAL_MODE) {
  1190. check(EXPR_TUPLE());
  1191. ctx()->s_emit_top();
  1192. consume(TK_EOF);
  1193. ctx()->emit_(OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
  1194. check(pop_context());
  1195. *out = code;
  1196. return NULL;
  1197. } else if(mode() == JSON_MODE) {
  1198. check(EXPR());
  1199. Expr* e = ctx()->s_popx();
  1200. if(!e->is_json_object()) return SyntaxError("expect a JSON object, literal or array");
  1201. consume(TK_EOF);
  1202. e->emit_(ctx());
  1203. ctx()->emit_(OP_RETURN_VALUE, BC_NOARG, BC_KEEPLINE);
  1204. check(pop_context());
  1205. *out = code;
  1206. return NULL;
  1207. }
  1208. while(!match(TK_EOF)) {
  1209. check(compile_stmt());
  1210. match_newlines();
  1211. }
  1212. check(pop_context());
  1213. *out = code;
  1214. return NULL;
  1215. }
  1216. Compiler::~Compiler(){
  1217. for(CodeEmitContext& ctx: contexts){
  1218. ctx.s_clean();
  1219. }
  1220. }
  1221. Error* Compiler::SyntaxError(const char* msg, ...) noexcept{
  1222. va_list args;
  1223. va_start(args, msg);
  1224. Error* e = lexer._error(false, "SyntaxError", msg, &args);
  1225. e->lineno = err().line;
  1226. e->cursor = err().start;
  1227. va_end(args);
  1228. return e;
  1229. }
  1230. #undef consume
  1231. #undef consume_end_stmt
  1232. #undef check
  1233. #undef check_newlines_repl
  1234. } // namespace pkpy