qic.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <stdlib.h>
  5. size_t GID = 0;
  6. typedef struct {
  7. void **data;
  8. size_t length;
  9. } list_t;
  10. list_t *list_new(void) {
  11. list_t *list = malloc(sizeof(list_t));
  12. list->data = NULL;
  13. list->length = 0;
  14. return list;
  15. }
  16. void list_push(list_t *l, void *v) {
  17. size_t i = l->length++;
  18. l->data = realloc(l->data, l->length * sizeof(void *));
  19. l->data[i] = v;
  20. }
  21. void *list_pop(list_t *l) {
  22. if (!l->length)
  23. return NULL;
  24. return l->data[--l->length];
  25. }
  26. void *list_index(list_t *l, ssize_t index) {
  27. if (!l->length)
  28. return NULL;
  29. if (index < 0)
  30. index += ((ssize_t)l->length);
  31. if (index < 0 || index >= l->length)
  32. return NULL;
  33. return l->data[index];
  34. }
  35. void list_set(list_t *l, ssize_t index, void *v) {
  36. if (!l->length)
  37. return;
  38. if (index < 0)
  39. index += ((ssize_t)l->length);
  40. if (index < 0 || index >= l->length)
  41. return;
  42. l->data[index] = v;
  43. }
  44. typedef struct {
  45. size_t *data;
  46. size_t length;
  47. } stack_t;
  48. stack_t *stack_new(void) {
  49. stack_t *stack = malloc(sizeof(list_t));
  50. stack->data = NULL;
  51. stack->length = 0;
  52. return stack;
  53. }
  54. void stack_push(stack_t *l, size_t v) {
  55. size_t i = l->length++;
  56. l->data = realloc(l->data, l->length * sizeof(size_t));
  57. l->data[i] = v;
  58. }
  59. size_t stack_pop(stack_t *l) {
  60. if (!l->length)
  61. return 0;
  62. return l->data[--l->length];
  63. }
  64. struct entry_t {
  65. char *key;
  66. void *value;
  67. };
  68. struct table_t {
  69. struct entry_t *entries;
  70. size_t used;
  71. size_t capacity;
  72. };
  73. typedef struct entry_t entry_t;
  74. typedef struct table_t table_t;
  75. table_t *table_new() {
  76. table_t *table = malloc(sizeof(table_t));
  77. table->used = 0;
  78. table->capacity = 32;
  79. table->entries = calloc(table->capacity, sizeof(entry_t));
  80. return table;
  81. }
  82. unsigned long ht_hash(const char* key) {
  83. unsigned long hash = 5381;
  84. int c;
  85. while ((c = *key++))
  86. hash = ((hash << 5) + hash) + c;
  87. return hash;
  88. }
  89. void *table_get(table_t *table, char *key) {
  90. if (!table->used)
  91. return NULL;
  92. unsigned long hash = ht_hash(key);
  93. size_t index = hash % table->capacity;
  94. size_t i = index;
  95. while (table->entries[i].key) {
  96. if (strcmp(table->entries[i].key, key) == 0)
  97. return table->entries[i].value;
  98. i++;
  99. if (i >= table->capacity)
  100. i = 0;
  101. if (i == index)
  102. break;
  103. }
  104. return NULL;
  105. }
  106. static void table_entry_set(entry_t *entries, char *key, void *value, size_t capacity, size_t *used) {
  107. unsigned long hash = ht_hash(key);
  108. size_t index = hash % capacity;
  109. size_t i = index;
  110. while (entries[i].key) {
  111. if (strcmp(entries[i].key, key) == 0) {
  112. entries[i].value = value;
  113. return;
  114. }
  115. i++;
  116. if (i >= capacity)
  117. i = 0;
  118. if (i == index)
  119. break;
  120. }
  121. if (used)
  122. (*used)++;
  123. entries[i].key = key;
  124. entries[i].value = value;
  125. }
  126. table_t *table_set(table_t *table, char *key, void *value) {
  127. if (table->used >= table->capacity) {
  128. size_t capacity = table->capacity + 32;
  129. entry_t *entries = calloc(capacity, sizeof(entry_t));
  130. for (size_t i = 0; i < table->capacity; i++) {
  131. entry_t entry = table->entries[i];
  132. if (entry.key)
  133. table_entry_set(entries, entry.key, entry.value, capacity, NULL);
  134. }
  135. table->entries = entries;
  136. table->capacity = capacity;
  137. }
  138. table_entry_set(table->entries, key, value, table->capacity, &table->used);
  139. return table;
  140. }
  141. #define table_iterate(table, code) \
  142. { \
  143. if ((table)->used) { \
  144. size_t i = 0; \
  145. while (i < (table)->capacity) { \
  146. entry_t entry = (table)->entries[i]; \
  147. if (entry.key) { \
  148. code; \
  149. } \
  150. i++; \
  151. } \
  152. } \
  153. }
  154. typedef struct {
  155. char *str;
  156. size_t size;
  157. } buffer_t;
  158. buffer_t *buffer_new(void) {
  159. buffer_t *buf = malloc(sizeof(buffer_t));
  160. buf->str = NULL;
  161. buf->size = 0;
  162. return buf;
  163. }
  164. void buffer_append(buffer_t *buf, char c) {
  165. buf->size++;
  166. void *p = malloc(sizeof(char) * buf->size);
  167. if (buf->str)
  168. memcpy(p, buf->str, buf->size - 1);
  169. buf->str = p;
  170. buf->str[buf->size - 1] = c;
  171. }
  172. char *buffer_read(buffer_t *buf) {
  173. if (buf->size == 0 || buf->str[buf->size - 1])
  174. buffer_append(buf, 0);
  175. return buf->str;
  176. }
  177. void buffer_appends(buffer_t *buf, char *s) {
  178. for (size_t i = 0; i < strlen(s); i++)
  179. buffer_append(buf, s[i]);
  180. }
  181. void buffer_appendb(buffer_t *dst, buffer_t *src) {
  182. for (size_t i = 0; i < src->size; i++)
  183. buffer_append(dst, src->str[i]);
  184. }
  185. void buffer_fmt(buffer_t *buf, const char *fmt, ...) {
  186. va_list args;
  187. va_start(args, fmt);
  188. size_t size = vsnprintf(NULL, 0, fmt, args);
  189. char *str = malloc(sizeof(char) * (size + 1));
  190. vsnprintf(str, size + 1, fmt, args);
  191. va_end(args);
  192. buffer_appends(buf, str);
  193. }
  194. typedef struct {
  195. enum {
  196. T_EOF,
  197. T_NUMBER,
  198. T_STRING,
  199. T_NAME,
  200. T_VAR,
  201. T_LET,
  202. T_IF,
  203. T_ELSE,
  204. T_ELIF,
  205. T_FOR,
  206. T_OF,
  207. T_BREAK,
  208. T_CONTINUE,
  209. T_PASS,
  210. T_FUNC,
  211. T_USE,
  212. T_RETURN,
  213. T_DEFER,
  214. T_REQUIRE,
  215. T_TRY,
  216. T_CATCH,
  217. T_THROW,
  218. T_GOTO,
  219. T_IS,
  220. T_IN,
  221. T_LPAR,
  222. T_RPAR,
  223. T_LSB,
  224. T_RSB,
  225. T_LCB,
  226. T_RCB,
  227. T_EQUALS,
  228. T_NOTEQUALS,
  229. T_PLUSASSIGN,
  230. T_MINUSASSIGN,
  231. T_SLASHASSIGN,
  232. T_STARASSIGN,
  233. T_SLASHSLASHASSIGN,
  234. T_PERCENTASSIGN,
  235. T_BARBAR,
  236. T_ANDAND,
  237. T_STARSTAR,
  238. T_PLUSPLUS,
  239. T_MINUSMINUS,
  240. T_SLASHSLASH,
  241. T_PLUS,
  242. T_MINUS,
  243. T_QM,
  244. T_COLON,
  245. T_BAR,
  246. T_AND,
  247. T_LT,
  248. T_LTLT,
  249. T_GT,
  250. T_GTGT,
  251. T_LE,
  252. T_GE,
  253. T_STAR,
  254. T_SLASH,
  255. T_PERCENT,
  256. T_COMMA,
  257. T_DOT,
  258. T_BANG,
  259. T_RAISE,
  260. T_TILDE,
  261. T_INLINE,
  262. T_ASSIGN,
  263. T_SEMI
  264. } tag;
  265. char *text;
  266. size_t fi;
  267. size_t pos;
  268. } token_t;
  269. token_t *token(int tag, char *text) {
  270. token_t *tok = malloc(sizeof(token_t));
  271. tok->tag = tag;
  272. tok->text = text;
  273. return tok;
  274. }
  275. #define TK(tk) (token(T_##tk, NULL))
  276. #define WS() while (source[*pos] == ' ' || source[*pos] == '\t' || source[*pos] == '\n' || source[*pos] == '\r') { (*pos)++; }
  277. void consume_ignored(char *source, size_t *pos) {
  278. WS();
  279. while (source[*pos] == '#') {
  280. (*pos)++;
  281. for (;;) {
  282. if (!source[*pos])
  283. break;
  284. if (source[*pos] == '\n') {
  285. (*pos)++;
  286. break;
  287. }
  288. (*pos)++;
  289. }
  290. WS();
  291. }
  292. }
  293. list_t *FILES;
  294. list_t *REQUIRED;
  295. int is_required(char *path) {
  296. for (size_t i = 0; i < REQUIRED->length; i++)
  297. if (strcmp(REQUIRED->data[i], path) == 0)
  298. return 1;
  299. return 0;
  300. }
  301. void traverse(char *source, size_t pos, size_t *line, size_t *col) {
  302. *line = 1;
  303. *col = 1;
  304. for (size_t i = 0; i < pos; i++) {
  305. if (source[i] == '\n') {
  306. (*line)++;
  307. (*col) = 1;
  308. } else (*col)++;
  309. }
  310. }
  311. void format_error(char *filename, char *source, size_t pos, char *fmt, ...) {
  312. size_t line, col;
  313. traverse(source, pos, &line, &col);
  314. va_list args;
  315. va_start(args, fmt);
  316. fprintf(stderr, "%s (%zu:%zu): ", filename, line, col);
  317. vfprintf(stderr, fmt, args);
  318. fputc('\n', stderr);
  319. va_end(args);
  320. }
  321. #define GETFNAME(fi) ((char *)((list_t *)list_index(FILES, fi))->data[0])
  322. #define GETSRC(fi) ((char *)((list_t *)list_index(FILES, fi))->data[1])
  323. #define LEX_ERROR(fmt, ...) { format_error(GETFNAME(-1), source, *pos, fmt, ##__VA_ARGS__); exit(1); }
  324. token_t *next_token(char *source, size_t *pos) {
  325. if (!source[*pos])
  326. return token(T_EOF, NULL);
  327. if (source[*pos] == '"' || source[*pos] == '\'' || source[*pos] == '`') {
  328. char term = source[(*pos)++];
  329. buffer_t *text = buffer_new();
  330. while (source[*pos] != term) {
  331. if (!source[*pos])
  332. LEX_ERROR("unterminated string literal");
  333. char c = source[(*pos)++];
  334. if (c == '\n' && term != '`')
  335. LEX_ERROR("unterminated string literal");
  336. if (term != '`' && c == '\\') {
  337. char nc = source[(*pos)++];
  338. if (!nc)
  339. continue;
  340. switch (nc) {
  341. case 'n':
  342. buffer_appends(text, "\\n");
  343. break;
  344. case 't':
  345. buffer_appends(text, "\\t");
  346. break;
  347. case 'r':
  348. buffer_appends(text, "\\r");
  349. break;
  350. case 'b':
  351. buffer_appends(text, "\\b");
  352. break;
  353. case 'e':
  354. buffer_appends(text, "\\e");
  355. break;
  356. case 's':
  357. buffer_appends(text, " ");
  358. break;
  359. case '"':
  360. buffer_appends(text, "\\\"");
  361. break;
  362. case '\\':
  363. buffer_appends(text, "\\\\");
  364. break;
  365. case '\n':
  366. buffer_appends(text, "\\n");
  367. break;
  368. default:
  369. buffer_append(text, nc);
  370. break;
  371. }
  372. continue;
  373. }
  374. if (c == '"' || c == '\\')
  375. buffer_append(text, '\\');
  376. else if (c == '\n')
  377. buffer_appends(text, "\\n");
  378. buffer_append(text, c);
  379. }
  380. (*pos)++;
  381. return token(T_STRING, buffer_read(text));
  382. } else if ((source[*pos] == '.' && isdigit(source[(*pos)+1])) || isdigit(source[*pos])) {
  383. buffer_t *number = buffer_new();
  384. int dot = 0;
  385. int sub = 0;
  386. int skip = 0;
  387. if (source[*pos] == '.') {
  388. buffer_append(number, '0');
  389. skip = 1;
  390. }
  391. do {
  392. if (skip) skip = 0;
  393. else
  394. buffer_append(number, source[(*pos)++]);
  395. if (!dot && source[*pos] == '.') {
  396. buffer_append(number, source[(*pos)++]);
  397. if (!isdigit(source[*pos]))
  398. LEX_ERROR("illegal number literal (missing part after floating point)");
  399. dot = 1;
  400. } else if (!sub && source[*pos] == '_') {
  401. (*pos)++;
  402. if (!isdigit(source[*pos]))
  403. LEX_ERROR("illegal number literal (missing part after underscore)");
  404. sub = 1;
  405. } else if (sub) sub = 0;
  406. } while (isdigit(source[*pos]));
  407. return token(T_NUMBER, buffer_read(number));
  408. } else if (isalpha(source[*pos]) || source[*pos] == '_') {
  409. buffer_t *text = buffer_new();
  410. do {
  411. buffer_append(text, source[(*pos)++]);
  412. } while (isalpha(source[*pos]) || source[*pos] == '_' || isdigit(source[*pos]));
  413. char *name = buffer_read(text);
  414. if (strcmp(name, "var") == 0)
  415. return TK(VAR);
  416. else if (strcmp(name, "let") == 0)
  417. return TK(LET);
  418. else if (strcmp(name, "if") == 0)
  419. return TK(IF);
  420. else if (strcmp(name, "else") == 0)
  421. return TK(ELSE);
  422. else if (strcmp(name, "elif") == 0)
  423. return TK(ELIF);
  424. else if (strcmp(name, "for") == 0)
  425. return TK(FOR);
  426. else if (strcmp(name, "break") == 0)
  427. return TK(BREAK);
  428. else if (strcmp(name, "continue") == 0)
  429. return TK(CONTINUE);
  430. else if (strcmp(name, "func") == 0)
  431. return TK(FUNC);
  432. else if (strcmp(name, "use") == 0)
  433. return TK(USE);
  434. else if (strcmp(name, "return") == 0)
  435. return TK(RETURN);
  436. else if (strcmp(name, "defer") == 0)
  437. return TK(DEFER);
  438. else if (strcmp(name, "pass") == 0)
  439. return TK(PASS);
  440. else if (strcmp(name, "require") == 0)
  441. return TK(REQUIRE);
  442. else if (strcmp(name, "try") == 0)
  443. return TK(TRY);
  444. else if (strcmp(name, "catch") == 0)
  445. return TK(CATCH);
  446. else if (strcmp(name, "throw") == 0)
  447. return TK(THROW);
  448. else if (strcmp(name, "goto") == 0)
  449. return TK(GOTO);
  450. else if (strcmp(name, "is") == 0)
  451. return TK(IS);
  452. else if (strcmp(name, "in") == 0)
  453. return TK(IN);
  454. else if (strcmp(name, "of") == 0)
  455. return TK(OF);
  456. else if (strcmp(name, "inline") == 0)
  457. return TK(INLINE);
  458. return token(T_NAME, name);
  459. } else if (strncmp(&source[*pos], "==", 2) == 0 && ++(*pos) && ++(*pos))
  460. return TK(EQUALS);
  461. else if (strncmp(&source[*pos], "!=", 2) == 0 && ++(*pos) && ++(*pos))
  462. return TK(NOTEQUALS);
  463. else if (strncmp(&source[*pos], "+=", 2) == 0 && ++(*pos) && ++(*pos))
  464. return TK(PLUSASSIGN);
  465. else if (strncmp(&source[*pos], "-=", 2) == 0 && ++(*pos) && ++(*pos))
  466. return TK(MINUSASSIGN);
  467. else if (strncmp(&source[*pos], "*=", 2) == 0 && ++(*pos) && ++(*pos))
  468. return TK(STARASSIGN);
  469. else if (strncmp(&source[*pos], "/=", 2) == 0 && ++(*pos) && ++(*pos))
  470. return TK(SLASHASSIGN);
  471. else if (strncmp(&source[*pos], "//=", 3) == 0 && ++(*pos) && ++(*pos) && ++(*pos))
  472. return TK(SLASHSLASHASSIGN);
  473. else if (strncmp(&source[*pos], "%=", 2) == 0 && ++(*pos) && ++(*pos))
  474. return TK(PERCENTASSIGN);
  475. else if (strncmp(&source[*pos], "||", 2) == 0 && ++(*pos) && ++(*pos))
  476. return TK(BARBAR);
  477. else if (strncmp(&source[*pos], "&&", 2) == 0 && ++(*pos) && ++(*pos))
  478. return TK(ANDAND);
  479. else if (strncmp(&source[*pos], "++", 2) == 0 && ++(*pos) && ++(*pos))
  480. return TK(PLUSPLUS);
  481. else if (strncmp(&source[*pos], "--", 2) == 0 && ++(*pos) && ++(*pos))
  482. return TK(MINUSMINUS);
  483. else if (strncmp(&source[*pos], "//", 2) == 0 && ++(*pos) && ++(*pos))
  484. return TK(SLASHSLASH);
  485. else if (strncmp(&source[*pos], "**", 2) == 0 && ++(*pos) && ++(*pos))
  486. return TK(STARSTAR);
  487. else if (strncmp(&source[*pos], "<<", 2) == 0 && ++(*pos) && ++(*pos))
  488. return TK(LTLT);
  489. else if (strncmp(&source[*pos], ">>", 2) == 0 && ++(*pos) && ++(*pos))
  490. return TK(GTGT);
  491. else if (strncmp(&source[*pos], "<=", 2) == 0 && ++(*pos) && ++(*pos))
  492. return TK(LE);
  493. else if (strncmp(&source[*pos], ">=", 2) == 0 && ++(*pos) && ++(*pos))
  494. return TK(GE);
  495. else if (source[*pos] == '(' && ++(*pos))
  496. return TK(LPAR);
  497. else if (source[*pos] == ')' && ++(*pos))
  498. return TK(RPAR);
  499. else if (source[*pos] == '[' && ++(*pos))
  500. return TK(LSB);
  501. else if (source[*pos] == ']' && ++(*pos))
  502. return TK(RSB);
  503. else if (source[*pos] == '{' && ++(*pos))
  504. return TK(LCB);
  505. else if (source[*pos] == '}' && ++(*pos))
  506. return TK(RCB);
  507. else if (source[*pos] == '+' && ++(*pos))
  508. return TK(PLUS);
  509. else if (source[*pos] == '-' && ++(*pos))
  510. return TK(MINUS);
  511. else if (source[*pos] == '*' && ++(*pos))
  512. return TK(STAR);
  513. else if (source[*pos] == '/' && ++(*pos))
  514. return TK(SLASH);
  515. else if (source[*pos] == '%' && ++(*pos))
  516. return TK(PERCENT);
  517. else if (source[*pos] == '?' && ++(*pos))
  518. return TK(QM);
  519. else if (source[*pos] == ':' && ++(*pos))
  520. return TK(COLON);
  521. else if (source[*pos] == '=' && ++(*pos))
  522. return TK(ASSIGN);
  523. else if (source[*pos] == ';' && ++(*pos))
  524. return TK(SEMI);
  525. else if (source[*pos] == ',' && ++(*pos))
  526. return TK(COMMA);
  527. else if (source[*pos] == '.' && ++(*pos))
  528. return TK(DOT);
  529. else if (source[*pos] == '<' && ++(*pos))
  530. return TK(LT);
  531. else if (source[*pos] == '>' && ++(*pos))
  532. return TK(GT);
  533. else if (source[*pos] == '!' && ++(*pos))
  534. return TK(BANG);
  535. else if (source[*pos] == '|' && ++(*pos))
  536. return TK(BAR);
  537. else if (source[*pos] == '&' && ++(*pos))
  538. return TK(AND);
  539. else if (source[*pos] == '^' && ++(*pos))
  540. return TK(RAISE);
  541. else if (source[*pos] == '~' && ++(*pos))
  542. return TK(TILDE);
  543. LEX_ERROR("unexpected input")
  544. }
  545. list_t *tokenize(char *source) {
  546. size_t pos = 0;
  547. list_t *toks = list_new();
  548. do {
  549. consume_ignored(source, &pos);
  550. size_t tok_pos = pos;
  551. token_t *tok = next_token(source, &pos);
  552. tok->fi = FILES->length-1;
  553. tok->pos = tok_pos;
  554. list_push(toks, tok);
  555. if (tok->tag == T_EOF)
  556. break;
  557. } while (1);
  558. return toks;
  559. }
  560. struct _node_t {
  561. enum {
  562. N_PROGRAM,
  563. N_EXPRSTMT,
  564. N_BLOCK,
  565. N_NOT,
  566. N_NEGATE,
  567. N_BNOT,
  568. N_LITERAL,
  569. N_LIST,
  570. N_TUPLE,
  571. N_NILTUPLE,
  572. N_TABLE,
  573. N_CALL,
  574. N_MEMBER,
  575. N_INDEX,
  576. N_ADD,
  577. N_SUB,
  578. N_MUL,
  579. N_DIV,
  580. N_IDIV,
  581. N_MOD,
  582. N_POW,
  583. N_SHL,
  584. N_SHR,
  585. N_XOR,
  586. N_BOR,
  587. N_BAND,
  588. N_ASSIGN,
  589. N_ASSIGN_ADD,
  590. N_ASSIGN_SUB,
  591. N_ASSIGN_MUL,
  592. N_ASSIGN_DIV,
  593. N_ASSIGN_IDIV,
  594. N_ASSIGN_MOD,
  595. N_ASSIGN_POW,
  596. N_EQUALS,
  597. N_NOTEQUALS,
  598. N_IS,
  599. N_IN,
  600. N_NOTIS,
  601. N_NOTIN,
  602. N_LT,
  603. N_GT,
  604. N_LE,
  605. N_GE,
  606. N_INC,
  607. N_DEC,
  608. N_VAR,
  609. N_LET,
  610. N_IF,
  611. N_FOR,
  612. N_FOROF,
  613. N_BREAK,
  614. N_CONTINUE,
  615. N_FUNCDEF,
  616. N_RETURN,
  617. N_DEFER,
  618. N_PASS,
  619. N_REQUIRE,
  620. N_TRY,
  621. N_THROW,
  622. N_LABEL,
  623. N_GOTO,
  624. N_INLINE,
  625. N_IFEXPR,
  626. N_FUNCEXPR,
  627. N_LOGOR,
  628. N_LOGAND,
  629. } tag;
  630. struct _node_t *a;
  631. struct _node_t *b;
  632. struct _node_t *c;
  633. struct _node_t *d;
  634. list_t *l;
  635. table_t *h;
  636. table_t *h2;
  637. token_t *t;
  638. size_t fi;
  639. size_t pos;
  640. };
  641. typedef struct _node_t node_t;
  642. node_t *node_pos(node_t *node, size_t fi, size_t pos) {
  643. node->fi = fi;
  644. node->pos = pos;
  645. return node;
  646. }
  647. node_t *nodet(int tag, token_t *t) {
  648. node_t *node = malloc(sizeof(node_t));
  649. node->tag = tag;
  650. node->t = t;
  651. return node;
  652. }
  653. #define NODET(n, a) (node_pos(nodet(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  654. node_t *nodel(int tag, list_t *l) {
  655. node_t *node = malloc(sizeof(node_t));
  656. node->tag = tag;
  657. node->l = l;
  658. return node;
  659. }
  660. #define NODEL(n, a) (node_pos(nodel(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  661. node_t *nodeh(int tag, table_t *h) {
  662. node_t *node = malloc(sizeof(node_t));
  663. node->tag = tag;
  664. node->h = h;
  665. return node;
  666. }
  667. #define NODEH(n, a) (node_pos(nodeh(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  668. node_t *node0(int tag) {
  669. node_t *node = malloc(sizeof(node_t));
  670. node->tag = tag;
  671. return node;
  672. }
  673. #define NODE0(n) (node_pos(node0(N_##n), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  674. node_t *node1(int tag, node_t *a) {
  675. node_t *node = malloc(sizeof(node_t));
  676. node->tag = tag;
  677. node->a = a;
  678. return node;
  679. }
  680. #define NODE1(n, a) (node_pos(node1(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  681. node_t *node1l(int tag, node_t *a, list_t *l) {
  682. node_t *node = malloc(sizeof(node_t));
  683. node->tag = tag;
  684. node->a = a;
  685. node->l = l;
  686. return node;
  687. }
  688. #define NODE1l(n, a, l) (node_pos(node1l(N_##n, (a), (l)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  689. node_t *node1t(int tag, node_t *a, token_t *t) {
  690. node_t *node = malloc(sizeof(node_t));
  691. node->tag = tag;
  692. node->a = a;
  693. node->t = t;
  694. return node;
  695. }
  696. #define NODE1t(n, a, t) (node_pos(node1t(N_##n, (a), (t)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  697. node_t *node2(int tag, node_t *a, node_t *b) {
  698. node_t *node = malloc(sizeof(node_t));
  699. node->tag = tag;
  700. node->a = a;
  701. node->b = b;
  702. return node;
  703. }
  704. #define NODE2(n, a, b) (node_pos(node2(N_##n, (a), (b)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  705. node_t *node2t(int tag, node_t *a, node_t *b, token_t *t) {
  706. node_t *node = malloc(sizeof(node_t));
  707. node->tag = tag;
  708. node->a = a;
  709. node->b = b;
  710. node->t = t;
  711. return node;
  712. }
  713. #define NODE2t(n, a, b, c) (node_pos(node2t(N_##n, (a), (b), (c)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  714. node_t *node3(int tag, node_t *a, node_t *b, node_t *c) {
  715. node_t *node = malloc(sizeof(node_t));
  716. node->tag = tag;
  717. node->a = a;
  718. node->b = b;
  719. node->c = c;
  720. return node;
  721. }
  722. #define NODE3(n, a, b, c) (node_pos(node3(N_##n, (a), (b), (c)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  723. node_t *node4(int tag, node_t *a, node_t *b, node_t *c, node_t *d) {
  724. node_t *node = malloc(sizeof(node_t));
  725. node->tag = tag;
  726. node->a = a;
  727. node->b = b;
  728. node->c = c;
  729. node->d = d;
  730. return node;
  731. }
  732. #define NODE4(n, a, b, c, d) (node_pos(node4(N_##n, (a), (b), (c), (d)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  733. node_t *nodef(int tag, token_t *name, table_t *params, table_t *captured, node_t *body) {
  734. node_t *node = malloc(sizeof(node_t));
  735. node->tag = tag;
  736. node->t = name;
  737. node->h = params;
  738. node->h2 = captured;
  739. node->a = body;
  740. return node;
  741. }
  742. #define NODEF(n, a, b, c, d) (node_pos(nodef(N_##n, (a), (b), (c), (d)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  743. #define AT(tk) (*pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == T_##tk)
  744. #define ATP(tk, p) ((*pos)+p < tokens->length && ((token_t *)tokens->data[(*pos)+p])->tag == T_##tk)
  745. #define MATCH(tk) (AT(tk) && ++(*pos))
  746. #define PARSE_ERROR(fmt, ...) { format_error(GETFNAME(((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi), GETSRC(((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos, fmt, ##__VA_ARGS__); exit(1); }
  747. #define EXPECT(tk, s) { if (!MATCH(tk)) PARSE_ERROR("expected %s", (s)); }
  748. node_t *parse_expr(list_t *tokens, size_t *pos);
  749. list_t *parse_sequence(list_t *tokens, size_t *pos, int term) {
  750. list_t *seq = list_new();
  751. do {
  752. if (term != -1 && *pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == term)
  753. break;
  754. list_push(seq, parse_expr(tokens, pos));
  755. } while (MATCH(COMMA));
  756. return seq;
  757. }
  758. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr);
  759. node_t *parse_primary(list_t *tokens, size_t *pos) {
  760. if (MATCH(FUNC))
  761. return parse_func(tokens, pos, 1);
  762. else if (MATCH(LPAR)) {
  763. if (MATCH(RPAR))
  764. return NODE0(NILTUPLE);
  765. node_t *a = parse_expr(tokens, pos);
  766. if (MATCH(COMMA)) {
  767. list_t *l = list_new();
  768. list_push(l, a);
  769. if (!AT(RPAR))
  770. do {
  771. node_t *n = parse_expr(tokens, pos);
  772. list_push(l, n);
  773. } while (MATCH(COMMA));
  774. a = NODEL(TUPLE, l);
  775. }
  776. EXPECT(RPAR, ")");
  777. return a;
  778. } else if (MATCH(LSB)) {
  779. list_t *a = parse_sequence(tokens, pos, T_RSB);
  780. EXPECT(RSB, "]");
  781. return NODEL(LIST, a);
  782. } else if (MATCH(LCB)) {
  783. table_t *table = table_new();
  784. do {
  785. if (AT(RCB))
  786. break;
  787. if (!AT(NAME) && !AT(STRING))
  788. PARSE_ERROR("expected identifier or string");
  789. char *key = ((token_t *)tokens->data[(*pos)++])->text;
  790. EXPECT(COLON, ":");
  791. node_t *val = parse_expr(tokens, pos);
  792. table_set(table, key, val);
  793. } while (MATCH(COMMA));
  794. EXPECT(RCB, "}");
  795. return NODEH(TABLE, table);
  796. } else if (MATCH(NUMBER) || MATCH(STRING) || MATCH(NAME))
  797. return NODET(LITERAL, tokens->data[(*pos)-1]);
  798. PARSE_ERROR("expected expression");
  799. return NULL;
  800. }
  801. size_t get_lineno(token_t *tok) {
  802. size_t line, col;
  803. traverse(GETSRC(tok->fi), tok->pos, &line, &col);
  804. return line;
  805. }
  806. #define CLIFF (get_lineno(((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])) != get_lineno(((token_t *)tokens->data[(*pos)>=tokens->length?tokens->length-1:(*pos)])))
  807. node_t *parse_call(list_t *tokens, size_t *pos) {
  808. node_t *a = parse_primary(tokens, pos);
  809. do {
  810. if (!CLIFF && MATCH(LPAR)) {
  811. list_t *b = NULL;
  812. if (!AT(RPAR))
  813. b = parse_sequence(tokens, pos, -1);
  814. EXPECT(RPAR, ")");
  815. a = NODE1l(CALL, a, b);
  816. continue;
  817. } else if (!CLIFF && MATCH(LSB)) {
  818. node_t *b = parse_expr(tokens, pos);
  819. EXPECT(RSB, "]");
  820. a = NODE2(INDEX, a, b);
  821. continue;
  822. } else if (!CLIFF && MATCH(DOT)) {
  823. if (!AT(NAME))
  824. PARSE_ERROR("expected identifier after .");
  825. a = NODE1t(MEMBER, a, tokens->data[(*pos)++]);
  826. continue;
  827. }
  828. break;
  829. } while (1);
  830. return a;
  831. }
  832. node_t *parse_postfix(list_t *tokens, size_t *pos) {
  833. node_t *a = parse_call(tokens, pos);
  834. if (CLIFF)
  835. return a;
  836. if (MATCH(PLUSPLUS))
  837. return NODE1(INC, a);
  838. else if (MATCH(MINUSMINUS))
  839. return NODE1(DEC, a);
  840. return a;
  841. }
  842. node_t *parse_unary(list_t *tokens, size_t *pos) {
  843. if (MATCH(MINUS)) {
  844. node_t *a = parse_unary(tokens, pos);
  845. return NODE1(NEGATE, a);
  846. } else if (MATCH(BANG)) {
  847. node_t *a = parse_unary(tokens, pos);
  848. return NODE1(NOT, a);
  849. } else if (MATCH(TILDE)) {
  850. node_t *a = parse_unary(tokens, pos);
  851. return NODE1(BNOT, a);
  852. }
  853. return parse_postfix(tokens, pos);
  854. }
  855. node_t *parse_pow(list_t *tokens, size_t *pos) {
  856. node_t *a = parse_unary(tokens, pos);
  857. do {
  858. if (MATCH(STARSTAR)) {
  859. node_t *b = parse_unary(tokens, pos);
  860. a = NODE2(POW, a, b);
  861. continue;
  862. }
  863. break;
  864. } while (1);
  865. return a;
  866. }
  867. node_t *parse_mul(list_t *tokens, size_t *pos) {
  868. node_t *a = parse_pow(tokens, pos);
  869. do {
  870. if (MATCH(STAR)) {
  871. node_t *b = parse_pow(tokens, pos);
  872. a = NODE2(MUL, a, b);
  873. continue;
  874. } else if (MATCH(SLASH)) {
  875. node_t *b = parse_pow(tokens, pos);
  876. a = NODE2(DIV, a, b);
  877. continue;
  878. } else if (MATCH(SLASHSLASH)) {
  879. node_t *b = parse_pow(tokens, pos);
  880. a = NODE2(IDIV, a, b);
  881. continue;
  882. } else if (MATCH(PERCENT)) {
  883. node_t *b = parse_pow(tokens, pos);
  884. a = NODE2(MOD, a, b);
  885. continue;
  886. }
  887. break;
  888. } while (1);
  889. return a;
  890. }
  891. node_t *parse_add(list_t *tokens, size_t *pos) {
  892. node_t *a = parse_mul(tokens, pos);
  893. do {
  894. if (MATCH(PLUS)) {
  895. node_t *b = parse_mul(tokens, pos);
  896. a = NODE2(ADD, a, b);
  897. continue;
  898. } else if (MATCH(MINUS)) {
  899. node_t *b = parse_mul(tokens, pos);
  900. a = NODE2(SUB, a, b);
  901. continue;
  902. }
  903. break;
  904. } while (1);
  905. return a;
  906. }
  907. node_t *parse_shift(list_t *tokens, size_t *pos) {
  908. node_t *a = parse_add(tokens, pos);
  909. do {
  910. if (MATCH(LTLT)) {
  911. node_t *b = parse_add(tokens, pos);
  912. a = NODE2(SHL, a, b);
  913. continue;
  914. } else if (MATCH(GTGT)) {
  915. node_t *b = parse_add(tokens, pos);
  916. a = NODE2(SHR, a, b);
  917. continue;
  918. }
  919. break;
  920. } while (1);
  921. return a;
  922. }
  923. node_t *parse_relation(list_t *tokens, size_t *pos) {
  924. node_t *a = parse_shift(tokens, pos);
  925. do {
  926. if (MATCH(LT)) {
  927. node_t *b = parse_shift(tokens, pos);
  928. a = NODE2(LT, a, b);
  929. continue;
  930. } else if (MATCH(GT)) {
  931. node_t *b = parse_shift(tokens, pos);
  932. a = NODE2(GT, a, b);
  933. continue;
  934. } else if (MATCH(LE)) {
  935. node_t *b = parse_shift(tokens, pos);
  936. a = NODE2(LE, a, b);
  937. continue;
  938. } else if (MATCH(GE)) {
  939. node_t *b = parse_shift(tokens, pos);
  940. a = NODE2(GE, a, b);
  941. continue;
  942. }
  943. break;
  944. } while (1);
  945. return a;
  946. }
  947. node_t *parse_equality(list_t *tokens, size_t *pos) {
  948. node_t *a = parse_relation(tokens, pos);
  949. do {
  950. if (MATCH(EQUALS)) {
  951. node_t *b = parse_relation(tokens, pos);
  952. a = NODE2(EQUALS, a, b);
  953. continue;
  954. } else if (MATCH(NOTEQUALS)) {
  955. node_t *b = parse_relation(tokens, pos);
  956. a = NODE2(NOTEQUALS, a, b);
  957. continue;
  958. } else if (MATCH(IS)) {
  959. node_t *b = parse_relation(tokens, pos);
  960. a = NODE2(IS, a, b);
  961. continue;
  962. } else if (AT(BANG) && ATP(IS, 1)) {
  963. EXPECT(BANG, "!");
  964. EXPECT(IS, "is");
  965. node_t *b = parse_relation(tokens, pos);
  966. a = NODE2(NOTIS, a, b);
  967. continue;
  968. } else if (MATCH(IN)) {
  969. node_t *b = parse_relation(tokens, pos);
  970. a = NODE2(IN, a, b);
  971. continue;
  972. } else if (AT(BANG) && ATP(IN, 1)) {
  973. EXPECT(BANG, "!");
  974. EXPECT(IN, "in");
  975. node_t *b = parse_relation(tokens, pos);
  976. a = NODE2(NOTIN, a, b);
  977. continue;
  978. }
  979. break;
  980. } while (1);
  981. return a;
  982. }
  983. node_t *parse_bitand(list_t *tokens, size_t *pos) {
  984. node_t *a = parse_equality(tokens, pos);
  985. while (MATCH(AND)) {
  986. node_t *b = parse_equality(tokens, pos);
  987. a = NODE2(BAND, a, b);
  988. }
  989. return a;
  990. }
  991. node_t *parse_bitxor(list_t *tokens, size_t *pos) {
  992. node_t *a = parse_bitand(tokens, pos);
  993. while (MATCH(RAISE)) {
  994. node_t *b = parse_bitand(tokens, pos);
  995. a = NODE2(XOR, a, b);
  996. }
  997. return a;
  998. }
  999. node_t *parse_bitor(list_t *tokens, size_t *pos) {
  1000. node_t *a = parse_bitxor(tokens, pos);
  1001. while (MATCH(BAR)) {
  1002. node_t *b = parse_bitxor(tokens, pos);
  1003. a = NODE2(BOR, a, b);
  1004. }
  1005. return a;
  1006. }
  1007. node_t *parse_logand(list_t *tokens, size_t *pos) {
  1008. node_t *a = parse_bitor(tokens, pos);
  1009. if (MATCH(ANDAND)) {
  1010. node_t *b = parse_logand(tokens, pos);
  1011. return NODE2(LOGAND, a, b);
  1012. }
  1013. return a;
  1014. }
  1015. node_t *parse_logor(list_t *tokens, size_t *pos) {
  1016. node_t *a = parse_logand(tokens, pos);
  1017. if (MATCH(BARBAR)) {
  1018. node_t *b = parse_logor(tokens, pos);
  1019. return NODE2(LOGOR, a, b);
  1020. }
  1021. return a;
  1022. }
  1023. node_t *parse_assignment(list_t *tokens, size_t *pos);
  1024. node_t *parse_conditional(list_t *tokens, size_t *pos) {
  1025. node_t *a = parse_logor(tokens, pos);
  1026. if (MATCH(QM)) {
  1027. node_t *b = parse_assignment(tokens, pos);
  1028. EXPECT(COLON, ":");
  1029. node_t *c = parse_assignment(tokens, pos);
  1030. return NODE3(IFEXPR, a, b, c);
  1031. }
  1032. return a;
  1033. }
  1034. node_t *parse_assignment(list_t *tokens, size_t *pos) {
  1035. node_t *a = parse_conditional(tokens, pos);
  1036. if (MATCH(ASSIGN)) {
  1037. node_t *b = parse_assignment(tokens, pos);
  1038. return NODE2(ASSIGN, a, b);
  1039. } else if (MATCH(PLUSASSIGN)) {
  1040. node_t *b = parse_assignment(tokens, pos);
  1041. return NODE2(ASSIGN_ADD, a, b);
  1042. } else if (MATCH(MINUSASSIGN)) {
  1043. node_t *b = parse_assignment(tokens, pos);
  1044. return NODE2(ASSIGN_SUB, a, b);
  1045. } else if (MATCH(STARASSIGN)) {
  1046. node_t *b = parse_assignment(tokens, pos);
  1047. return NODE2(ASSIGN_MUL, a, b);
  1048. } else if (MATCH(SLASHASSIGN)) {
  1049. node_t *b = parse_assignment(tokens, pos);
  1050. return NODE2(ASSIGN_DIV, a, b);
  1051. } else if (MATCH(SLASHSLASHASSIGN)) {
  1052. node_t *b = parse_assignment(tokens, pos);
  1053. return NODE2(ASSIGN_IDIV, a, b);
  1054. } else if (MATCH(PERCENTASSIGN)) {
  1055. node_t *b = parse_assignment(tokens, pos);
  1056. return NODE2(ASSIGN_MOD, a, b);
  1057. }
  1058. return a;
  1059. }
  1060. node_t *parse_expr(list_t *tokens, size_t *pos) {
  1061. return parse_assignment(tokens, pos);
  1062. }
  1063. node_t *parse_stmt(list_t *tokens, size_t *pos);
  1064. node_t *parse_block(list_t *tokens, size_t *pos) {
  1065. EXPECT(LCB, "{");
  1066. list_t *stmts = list_new();
  1067. while (!AT(EOF) && !AT(RCB))
  1068. list_push(stmts, parse_stmt(tokens, pos));
  1069. EXPECT(RCB, "}");
  1070. return NODEL(PROGRAM, stmts);
  1071. }
  1072. #define BLOCK() (CLIFF||MATCH(COLON)?parse_stmt(tokens, pos):parse_block(tokens, pos))
  1073. node_t *parse_if(list_t *tokens, size_t *pos) {
  1074. node_t *a = parse_expr(tokens, pos);
  1075. node_t *b = BLOCK();
  1076. node_t *c = NULL;
  1077. if (MATCH(ELSE))
  1078. c = BLOCK();
  1079. else if (MATCH(ELIF))
  1080. c = parse_if(tokens, pos);
  1081. return NODE3(IF, a, b, c);
  1082. }
  1083. node_t *parse_var(list_t *tokens, size_t *pos, int is_let) {
  1084. table_t *h = table_new();
  1085. do {
  1086. if(!AT(NAME))
  1087. PARSE_ERROR("expected identifier");
  1088. char *k = ((token_t *)tokens->data[(*pos)++])->text;
  1089. node_t *v = NULL;
  1090. if (is_let) {
  1091. EXPECT(ASSIGN, "=");
  1092. v = parse_expr(tokens, pos);
  1093. } else if (MATCH(ASSIGN))
  1094. v = parse_expr(tokens, pos);
  1095. table_set(h, k, v);
  1096. } while (MATCH(COMMA));
  1097. if (is_let)
  1098. return NODEH(LET, h);
  1099. return NODEH(VAR, h);
  1100. }
  1101. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr) {
  1102. token_t *name = NULL;
  1103. if (!is_expr) {
  1104. if(!AT(NAME))
  1105. PARSE_ERROR("expected identifier");
  1106. name = tokens->data[(*pos)++];
  1107. }
  1108. EXPECT(LPAR, "(");
  1109. table_t *params = NULL;
  1110. if (!AT(RPAR)) {
  1111. int flag = 0;
  1112. params = table_new();
  1113. size_t argc = 0;
  1114. do {
  1115. if(!AT(NAME))
  1116. PARSE_ERROR("expected identifier");
  1117. char *l = ((token_t *)tokens->data[(*pos)++])->text;
  1118. node_t *r = NULL;
  1119. if (!flag && AT(ASSIGN))
  1120. flag = 1;
  1121. if (flag) {
  1122. EXPECT(ASSIGN, "=");
  1123. r = parse_expr(tokens, pos);
  1124. }
  1125. list_t *pair = list_new();
  1126. size_t *argcp = malloc(sizeof(size_t));
  1127. memcpy(argcp, &argc, sizeof(size_t));
  1128. argc++;
  1129. list_push(pair, argcp);
  1130. list_push(pair, r);
  1131. table_set(params, l, pair);
  1132. } while (MATCH(COMMA));
  1133. }
  1134. EXPECT(RPAR, ")");
  1135. table_t *captured = NULL;
  1136. if (MATCH(USE)) {
  1137. EXPECT(RPAR, "(");
  1138. captured = table_new();
  1139. do {
  1140. if(!AT(NAME))
  1141. PARSE_ERROR("expected identifier");
  1142. token_t *name = tokens->data[(*pos)++];
  1143. table_set(captured, name->text, NODET(LITERAL, name));
  1144. } while (MATCH(COMMA));
  1145. EXPECT(RPAR, ")");
  1146. }
  1147. int colon = AT(COLON);
  1148. node_t *body = BLOCK();
  1149. if (colon && body->tag == N_EXPRSTMT)
  1150. body = NODE1(RETURN, body->a);
  1151. if (is_expr)
  1152. return NODEF(FUNCEXPR, NULL, params, captured, body);
  1153. return NODEF(FUNCDEF, name, params, captured, body);
  1154. }
  1155. node_t *parse_stmt(list_t *tokens, size_t *pos) {
  1156. if (MATCH(LCB)) {
  1157. list_t *stmts = list_new();
  1158. while (!AT(EOF) && !AT(RCB)) {
  1159. node_t *n = parse_stmt(tokens, pos);
  1160. MATCH(SEMI);
  1161. list_push(stmts, n);
  1162. }
  1163. EXPECT(RCB, "}");
  1164. return NODEL(BLOCK, stmts);
  1165. } else if (MATCH(VAR))
  1166. return parse_var(tokens, pos, 0);
  1167. else if (MATCH(LET))
  1168. return parse_var(tokens, pos, 1);
  1169. else if (MATCH(IF))
  1170. return parse_if(tokens, pos);
  1171. else if (MATCH(FOR)) {
  1172. node_t *a = NULL;
  1173. node_t *b = NULL;
  1174. node_t *c = NULL;
  1175. if (!AT(LCB) && !AT(COLON) && !CLIFF) {
  1176. if (MATCH(VAR)) {
  1177. if (AT(NAME) && ATP(OF, 1)) {
  1178. token_t *t = tokens->data[(*pos)++];
  1179. EXPECT(OF, "of");
  1180. a = parse_expr(tokens, pos);
  1181. b = BLOCK();
  1182. return NODE2t(FOROF, a, b, t);
  1183. }
  1184. a = parse_var(tokens, pos, 0);
  1185. EXPECT(SEMI, ";");
  1186. b = parse_expr(tokens, pos);
  1187. EXPECT(SEMI, ";");
  1188. c = parse_expr(tokens, pos);
  1189. } else a = parse_expr(tokens, pos);
  1190. }
  1191. node_t *d = BLOCK();
  1192. return NODE4(FOR, a, b, c, d);
  1193. } else if (MATCH(BREAK)) return NODE0(BREAK);
  1194. else if (MATCH(CONTINUE)) return NODE0(CONTINUE);
  1195. else if (MATCH(FUNC))
  1196. return parse_func(tokens, pos, 0);
  1197. else if (MATCH(RETURN)) {
  1198. node_t *a = NULL;
  1199. if (!AT(RCB) && !AT(EOF) && !CLIFF)
  1200. a = parse_expr(tokens, pos);
  1201. return NODE1(RETURN, a);
  1202. } else if (MATCH(DEFER)) {
  1203. node_t *a;
  1204. if (AT(LCB))
  1205. a = BLOCK();
  1206. else a = parse_stmt(tokens, pos);
  1207. return NODE1(DEFER, a);
  1208. } else if (MATCH(PASS)) return NODE0(PASS);
  1209. else if (MATCH(TRY)) {
  1210. node_t *a = BLOCK();
  1211. token_t *t = NULL;
  1212. EXPECT(CATCH, "catch");
  1213. if (!AT(COLON) && !AT(LCB) && !CLIFF) {
  1214. if (!AT(NAME))
  1215. PARSE_ERROR("expected identifier");
  1216. t = tokens->data[(*pos)++];
  1217. }
  1218. node_t *b = BLOCK();
  1219. return NODE2t(TRY, a, b, t);
  1220. } else if (MATCH(THROW)) {
  1221. node_t *a = NULL;
  1222. if (!CLIFF)
  1223. a = parse_expr(tokens, pos);
  1224. return NODE1(THROW, a);
  1225. } else if (MATCH(GOTO)) {
  1226. if(!AT(NAME))
  1227. PARSE_ERROR("expected identifier");
  1228. token_t *t = tokens->data[(*pos)++];
  1229. return NODET(GOTO, t);
  1230. } else if (AT(NAME) && ATP(COLON, 1) && !CLIFF) {
  1231. token_t *t = tokens->data[(*pos)++];
  1232. EXPECT(COLON, ":");
  1233. return NODET(LABEL, t);
  1234. } else if (MATCH(INLINE)) {
  1235. if (!AT(STRING))
  1236. PARSE_ERROR("expected string");
  1237. token_t *t = tokens->data[(*pos)++];
  1238. return NODET(INLINE, t);
  1239. }
  1240. node_t *n = parse_expr(tokens, pos);
  1241. return NODE1(EXPRSTMT, n);
  1242. }
  1243. node_t *parse_program(list_t *tokens, size_t *pos) {
  1244. if (AT(EOF))
  1245. PARSE_ERROR("empty program");
  1246. list_t *stmts = list_new();
  1247. int flag = 0;
  1248. while (!AT(EOF) && *pos < tokens->length) {
  1249. node_t *n;
  1250. if (MATCH(REQUIRE)) {
  1251. if (flag)
  1252. PARSE_ERROR("misplaced require statement")
  1253. if (!AT(STRING))
  1254. PARSE_ERROR("expected string");
  1255. token_t *path = tokens->data[(*pos)++];
  1256. n = NODET(REQUIRE, path);
  1257. } else { n = parse_stmt(tokens, pos); flag = 1; }
  1258. MATCH(SEMI);
  1259. list_push(stmts, n);
  1260. }
  1261. return NODEL(PROGRAM, stmts);
  1262. }
  1263. node_t *parse(char *source) {
  1264. size_t pos = 0;
  1265. return parse_program(tokenize(source), &pos);
  1266. }
  1267. #define NEWGID() size_t gid = GID++
  1268. #define EMIT(fmt, ...) buffer_fmt(buf, (fmt), ##__VA_ARGS__);
  1269. #define BINOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, lstk, lbl, node->a); EMIT(", "); compile_node(gbuf, buf, ctx, lstk, lbl, node->b); EMIT(")"); }
  1270. #define UNOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, lstk, lbl, node->a); EMIT(")"); }
  1271. #define ASSIGN(lhs, rhs) {\
  1272. if ((lhs)->tag == N_LITERAL && (lhs)->t->tag == T_NAME) {\
  1273. EMIT("qi_set(state, false, \"%s\", ", (lhs)->t->text);\
  1274. rhs;\
  1275. EMIT(")");\
  1276. } else if ((lhs)->tag == N_INDEX) {\
  1277. EMIT("qi_index_set(state, false, ");\
  1278. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs)->a);\
  1279. EMIT(", ");\
  1280. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs)->b);\
  1281. EMIT(", ");\
  1282. rhs;\
  1283. EMIT(")");\
  1284. } else if ((lhs)->tag == N_MEMBER) {\
  1285. EMIT("qi_index_set(state, false, ");\
  1286. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs)->a);\
  1287. EMIT(", qi_make_string(state, \"%s\"), ", (lhs)->t->text);\
  1288. rhs;\
  1289. EMIT(")");\
  1290. } else COMPILE_ERROR("illegal assignment left-hand side");\
  1291. }
  1292. #define COMPASSIGN(lhs, s, rhs) {\
  1293. ASSIGN(node->a, {\
  1294. EMIT("qi_%s(state, ", s);\
  1295. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs));\
  1296. EMIT(", ");\
  1297. rhs;\
  1298. EMIT(")");\
  1299. });\
  1300. }
  1301. #define COMPILE_ERROR(fmt, ...) { format_error(GETFNAME(node->fi), GETSRC(node->fi), node->pos, fmt, ##__VA_ARGS__); exit(1); }
  1302. void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, node_t *node);
  1303. void compile_list(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, list_t *seq) {
  1304. if (!seq || seq->length < 1) {
  1305. EMIT("NULL");
  1306. return;
  1307. }
  1308. buffer_t *tbuf = buffer_new();
  1309. NEWGID();
  1310. buffer_fmt(tbuf, "qi_list_t *__list%d(qi_state_t *state) {\n", gid);
  1311. buffer_fmt(tbuf, "qi_list_t *list = qi_list_make();\n");
  1312. for (size_t i = 0; i < seq->length; i++) {
  1313. buffer_fmt(tbuf, "qi_list_push(list, ");
  1314. compile_node(gbuf, tbuf, ctx, lstk, lbl, seq->data[i]);
  1315. buffer_fmt(tbuf, ");\n");
  1316. }
  1317. buffer_fmt(tbuf, "return list;\n");
  1318. buffer_fmt(tbuf, "}\n");
  1319. buffer_appendb(gbuf, tbuf);
  1320. EMIT("__list%d(state)", gid);
  1321. }
  1322. void compile_table(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, table_t *table) {
  1323. if (!table || table->used < 1) {
  1324. EMIT("NULL");
  1325. return;
  1326. }
  1327. buffer_t *tbuf = buffer_new();
  1328. NEWGID();
  1329. buffer_fmt(tbuf, "qi_table_t *__table%d(qi_state_t *state) {\n", gid);
  1330. buffer_fmt(tbuf, "qi_table_t *table = qi_table_make();\n");
  1331. table_iterate(table, {
  1332. buffer_fmt(tbuf, "qi_table_set(table, \"%s\", ", entry.key);
  1333. compile_node(gbuf, tbuf, ctx, lstk, lbl, entry.value);
  1334. buffer_fmt(tbuf, ");\n");
  1335. });
  1336. buffer_fmt(tbuf, "return table;\n");
  1337. buffer_fmt(tbuf, "}\n");
  1338. buffer_appendb(gbuf, tbuf);
  1339. EMIT("__table%d(state)", gid);
  1340. }
  1341. #define CTXPUSH(s) list_push(ctx, (s))
  1342. #define CTXPOP() list_pop(ctx)
  1343. int in_context(list_t *ctx, char *s) {
  1344. if (!ctx->length)
  1345. return 0;
  1346. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  1347. if (strcmp(ctx->data[i], "gap") == 0)
  1348. break;
  1349. else if (strcmp(ctx->data[i], s) == 0)
  1350. return 1;
  1351. }
  1352. return 0;
  1353. }
  1354. size_t count_ctxs(list_t *ctx, char *s) {
  1355. if (!ctx->length)
  1356. return 0;
  1357. size_t k = 0;
  1358. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  1359. if (strcmp(ctx->data[i], "gap") == 0)
  1360. break;
  1361. else if (strcmp(ctx->data[i], s) == 0)
  1362. k++;
  1363. }
  1364. return k;
  1365. }
  1366. #define INCTX(s) (in_context(ctx, (s)))
  1367. #define SCOPESK (count_ctxs(ctx, "scope"))
  1368. #define TRAPSK (count_ctxs(ctx, "trap"))
  1369. #define LPUSH(i) stack_push(lstk, (i))
  1370. #define LPOP() stack_pop(lstk)
  1371. #define LID (lstk->data[lstk->length-1])
  1372. #define LBPUSH() list_push(lbl, table_new())
  1373. #define LBPOP() list_pop(lbl)
  1374. char *tempvar() {
  1375. NEWGID();
  1376. char *s = malloc(sizeof(char) * 64);
  1377. snprintf(s, 64, "__temp%zu", gid);
  1378. return s;
  1379. }
  1380. void compile_func(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, node_t *node) {
  1381. NEWGID();
  1382. buffer_t *tbuf = buffer_new();
  1383. buffer_fmt(tbuf, "qi_value_t *__func%d(qi_state_t *state, qi_size_t pargc, qi_list_t *pargs) {\n", gid);
  1384. LBPUSH();
  1385. CTXPUSH("gap");
  1386. CTXPUSH("func");
  1387. size_t optargc = 0;
  1388. if (node->h) {
  1389. table_iterate(node->h, {
  1390. list_t *pair = entry.value;
  1391. size_t argc = *(size_t *)pair->data[0];
  1392. if (pair->data[1]) {
  1393. optargc++;
  1394. buffer_fmt(tbuf, "qi_set(state, false, \"%s\", pargc >= %d? qi_list_index(pargs, %d): ", entry.key, argc+1, argc);
  1395. compile_node(gbuf, tbuf, ctx, lstk, lbl, pair->data[1]);
  1396. buffer_fmt(tbuf, ");\n");
  1397. } else
  1398. buffer_fmt(tbuf, "qi_set(state, false, \"%s\", qi_list_index(pargs, %d));\n", entry.key, argc);
  1399. argc++;
  1400. });
  1401. }
  1402. compile_node(gbuf, tbuf, ctx, lstk, lbl, node->a);
  1403. CTXPOP();
  1404. CTXPOP();
  1405. LBPOP();
  1406. buffer_fmt(tbuf, "return state->nil;\n");
  1407. buffer_fmt(tbuf, "}\n");
  1408. buffer_appendb(gbuf, tbuf);
  1409. tbuf = buffer_new();
  1410. buffer_fmt(tbuf, "qi_make_function(state, \"%s\", %d, __func%d, ", node->t? node->t->text: "<anon>", !node->h? 0: (node->h->used - optargc), gid);
  1411. compile_table(gbuf, tbuf, ctx, lstk, lbl, node->h2);
  1412. buffer_fmt(tbuf, ")");
  1413. if (node->tag == N_FUNCEXPR) {
  1414. buffer_appendb(buf, tbuf);
  1415. return;
  1416. }
  1417. EMIT("qi_set(state, false, \"%s\", ", node->t->text);
  1418. buffer_appendb(buf, tbuf);
  1419. EMIT(");");
  1420. }
  1421. void compile_block(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, list_t *block) {
  1422. for (size_t i = 0; i < block->length; i++) {
  1423. node_t *n = block->data[i];
  1424. if (n->tag == N_FUNCDEF) {
  1425. compile_func(gbuf, buf, ctx, lstk, lbl, n);
  1426. EMIT("\n");
  1427. } else if (n->tag == N_VAR || n->tag == N_LET) {
  1428. table_iterate(n->h, {
  1429. EMIT("qi_%s(state, \"%s\", ", n->tag == N_LET? "decl_const": "decl", entry.key);
  1430. if (entry.value)
  1431. compile_node(gbuf, buf, ctx, lstk, lbl, entry.value);
  1432. else EMIT("state->nil");
  1433. EMIT(");\n");
  1434. });
  1435. }
  1436. }
  1437. for (size_t i = 0; i < block->length; i++) {
  1438. node_t *n = block->data[i];
  1439. if (n->tag == N_FUNCDEF || n->tag == N_VAR || n->tag == N_LET)
  1440. continue;
  1441. compile_node(gbuf, buf, ctx, lstk, lbl, n);
  1442. EMIT("\n");
  1443. }
  1444. }
  1445. const char *STD[][2] = {
  1446. {"std",
  1447. "func exit(c) {\n"
  1448. " if type(c) != \"number\"\n"
  1449. " throw \"expected first argument to be: number, but got: \" + type(c)\n"
  1450. " inline `int code = qi_get(state, \"c\")->value.number`\n"
  1451. " inline `exit(code)`\n"
  1452. "}\n"
  1453. "func head(l): return l[0]\n"
  1454. "func die(msg, c=1) {\n"
  1455. " println(msg)\n"
  1456. " exit(c)\n"
  1457. "}\n"
  1458. "let SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2\n"
  1459. "func frewind(file)\n"
  1460. " return fseek(file, 0, SEEK_SET)\n"
  1461. "func file_read(filename) {\n"
  1462. " var file = fopen(filename, \"r\")\n"
  1463. " defer fclose(file)\n"
  1464. " fseek(file, 0, SEEK_END)\n"
  1465. " let size = ftell(file)\n"
  1466. " frewind(file)\n"
  1467. " return str(fread(file, size))\n"
  1468. "}\n"
  1469. "func is_defined(name) {\n"
  1470. " if type(name) != \"string\"\n"
  1471. " throw \"expected first argument to be: string, but got: \" + type(name)\n"
  1472. " inline `bool b = qi_find(state, qi_get(state, \"name\")->value.string) != NULL`\n"
  1473. " inline `return qi_make_boolean(state, b)`\n"
  1474. "}\n"
  1475. },
  1476. {"str",
  1477. "let STR_LETTERS = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n"
  1478. "let STR_ASCII_LC = \"abcdefghijklmnopqrstuvwxyz\"\n"
  1479. "let STR_ASCII_UC = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n"
  1480. "let STR_DIGITS = \"0123456789\"\n"
  1481. "func is_char(c): return type(c) == \"string\" && len(c) == 1\n"
  1482. },
  1483. {NULL, NULL}
  1484. };
  1485. char *unescape(char *s) {
  1486. buffer_t *buf = buffer_new();
  1487. for (size_t i = 0; i < strlen(s); i++) {
  1488. char c = s[i];
  1489. if (c == '\\') {
  1490. char nc = s[i+1];
  1491. if (!nc)
  1492. continue;
  1493. switch (nc) {
  1494. case 'n':
  1495. buffer_append(buf, '\n');
  1496. break;
  1497. default:
  1498. buffer_append(buf, nc);
  1499. break;
  1500. }
  1501. i++;
  1502. } else buffer_append(buf, c);
  1503. }
  1504. return buffer_read(buf);
  1505. }
  1506. void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl);
  1507. int require_once(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, char *path) {
  1508. char *source = NULL;
  1509. for (size_t i = 0; STD[i][0]; i++) {
  1510. if (strcmp(path, STD[i][0]) == 0) {
  1511. source = (char *)STD[i][1];
  1512. break;
  1513. }
  1514. }
  1515. if (is_required(path))
  1516. return 1;
  1517. if (!source) {
  1518. FILE *fd = fopen(path, "rb");
  1519. if (!fd)
  1520. return -1;
  1521. buffer_t *fbuf = buffer_new();
  1522. for (;;) {
  1523. char line[512];
  1524. if (!fgets(line, sizeof(line), fd))
  1525. break;
  1526. buffer_appends(fbuf, line);
  1527. }
  1528. source = buffer_read(fbuf);
  1529. path = realpath(path, NULL);
  1530. }
  1531. list_t *pair = list_new();
  1532. list_push(pair, path);
  1533. list_push(pair, source);
  1534. list_push(FILES, pair);
  1535. compile_into(source, gbuf, buf, ctx, lstk, lbl);
  1536. list_pop(FILES);
  1537. list_push(REQUIRED, path);
  1538. return 0;
  1539. }
  1540. void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, node_t *node) {
  1541. switch (node->tag) {
  1542. case N_PROGRAM:
  1543. compile_block(gbuf, buf, ctx, lstk, lbl, node->l);
  1544. break;
  1545. case N_EXPRSTMT:
  1546. EMIT("(void)(");
  1547. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1548. EMIT(");");
  1549. break;
  1550. case N_BLOCK:
  1551. LBPUSH();
  1552. CTXPUSH("scope");
  1553. EMIT("qi_new_scope(state);\n");
  1554. compile_block(gbuf, buf, ctx, lstk, lbl, node->l);
  1555. EMIT("qi_old_scope(state);");
  1556. CTXPOP();
  1557. LBPOP();
  1558. break;
  1559. case N_LITERAL:
  1560. switch (node->t->tag) {
  1561. case T_NUMBER:
  1562. EMIT("qi_make_number(state, %s)", node->t->text);
  1563. break;
  1564. case T_STRING:
  1565. if (!*(node->t->text)) {
  1566. EMIT("state->empty_string");
  1567. } else {
  1568. EMIT("qi_make_string(state, \"%s\")", node->t->text);
  1569. }
  1570. break;
  1571. case T_NAME:
  1572. EMIT("qi_get(state, \"%s\")", node->t->text);
  1573. break;
  1574. default:
  1575. COMPILE_ERROR("not yet implemented");
  1576. }
  1577. break;
  1578. case N_LIST:
  1579. EMIT("qi_make_list(state, ");
  1580. compile_list(gbuf, buf, ctx, lstk, lbl, node->l);
  1581. EMIT(")");
  1582. break;
  1583. case N_TUPLE:
  1584. EMIT("qi_make_tuple(state, ");
  1585. compile_list(gbuf, buf, ctx, lstk, lbl, node->l);
  1586. EMIT(")");
  1587. break;
  1588. case N_NILTUPLE: EMIT("state->empty_tuple"); break;
  1589. case N_TABLE:
  1590. EMIT("qi_make_table(state, ");
  1591. compile_table(gbuf, buf, ctx, lstk, lbl, node->h);
  1592. EMIT(")");
  1593. break;
  1594. case N_CALL:
  1595. EMIT("qi_call(state, ");
  1596. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1597. EMIT(", ");
  1598. compile_list(gbuf, buf, ctx, lstk, lbl, node->l);
  1599. EMIT(")");
  1600. break;
  1601. case N_MEMBER:
  1602. EMIT("qi_index(state, ");
  1603. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1604. EMIT(", qi_make_string(state, \"%s\"))", node->t->text);
  1605. break;
  1606. case N_INDEX:
  1607. EMIT("qi_index(state, ");
  1608. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1609. EMIT(", ");
  1610. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1611. EMIT(")");
  1612. break;
  1613. case N_ASSIGN: ASSIGN(node->a, compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1614. case N_ASSIGN_ADD: COMPASSIGN(node->a, "add", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1615. case N_ASSIGN_SUB: COMPASSIGN(node->a, "sub", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1616. case N_ASSIGN_MUL: COMPASSIGN(node->a, "mul", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1617. case N_ASSIGN_DIV: COMPASSIGN(node->a, "div", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1618. case N_ASSIGN_IDIV: COMPASSIGN(node->a, "idiv", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1619. case N_ASSIGN_MOD: COMPASSIGN(node->a, "mod", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1620. case N_ASSIGN_POW: COMPASSIGN(node->a, "pow", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1621. case N_INC:
  1622. COMPASSIGN(node->a, "add", EMIT("state->one"));
  1623. break;
  1624. case N_DEC:
  1625. COMPASSIGN(node->a, "sub", EMIT("state->one"));
  1626. break;
  1627. case N_VAR: break;
  1628. case N_LET: break;
  1629. case N_IF:
  1630. EMIT("if (_qi_truthy(state, ");
  1631. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1632. EMIT(")) {\n");
  1633. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1634. if (node->c) {
  1635. EMIT("} else {\n");
  1636. compile_node(gbuf, buf, ctx, lstk, lbl, node->c);
  1637. }
  1638. EMIT("}");
  1639. break;
  1640. case N_FOR: {
  1641. NEWGID();
  1642. if (!node->a) {
  1643. EMIT("for (;;) {\n");
  1644. } else if (node->a && !node->b) {
  1645. EMIT("while (_qi_truthy(state, ");
  1646. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1647. EMIT(")) {\n");
  1648. } else {
  1649. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1650. EMIT("while (_qi_truthy(state, ");
  1651. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1652. EMIT(")) {\n");
  1653. }
  1654. LPUSH(gid);
  1655. CTXPUSH("for");
  1656. compile_node(gbuf, buf, ctx, lstk, lbl, node->d);
  1657. CTXPOP();
  1658. LPOP();
  1659. if (node->c)
  1660. compile_node(gbuf, buf, ctx, lstk, lbl, node->c);
  1661. EMIT("__continue%d:;\n", gid);
  1662. EMIT("}\n");
  1663. EMIT("__break%d:;\n", gid);
  1664. } break;
  1665. case N_FOROF: {
  1666. NEWGID();
  1667. char *varname = tempvar();
  1668. EMIT("qi_value_t *%s = qi_iter(state, ", varname);
  1669. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1670. EMIT(");\n");
  1671. CTXPUSH("scope");
  1672. EMIT("qi_new_scope(state);\n");
  1673. EMIT("qi_decl(state, \"%s\", state->nil);\n", node->t->text);
  1674. EMIT("for (qi_size_t length = _qi_length(state, %s), i = 0; i < length; i++) {\n", varname);
  1675. EMIT("qi_set(state, false, \"%s\", qi_index(state, %s, qi_make_number(state, i)));\n", node->t->text, varname);
  1676. LPUSH(gid);
  1677. CTXPUSH("for");
  1678. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1679. CTXPOP();
  1680. LPOP();
  1681. EMIT("__continue%d:;\n", gid);
  1682. EMIT("}\n");
  1683. EMIT("__break%d:;\n", gid);
  1684. EMIT("qi_old_scope(state);");
  1685. CTXPOP();
  1686. } break;
  1687. case N_BREAK:
  1688. if (!INCTX("for"))
  1689. COMPILE_ERROR("break outside of a loop");
  1690. EMIT("goto __break%d;", LID);
  1691. break;
  1692. case N_CONTINUE:
  1693. if (!INCTX("for"))
  1694. COMPILE_ERROR("continue outside of a loop");
  1695. EMIT("goto __continue%d;", LID);
  1696. break;
  1697. case N_DEFER: {
  1698. NEWGID();
  1699. buffer_t *tbuf = buffer_new();
  1700. buffer_fmt(tbuf, "void __defer%d(qi_state_t *state) {\n", gid);
  1701. LBPUSH();
  1702. CTXPUSH("gap");
  1703. compile_node(gbuf, tbuf, ctx, lstk, lbl, node->a);
  1704. CTXPOP();
  1705. LBPOP();
  1706. buffer_fmt(tbuf, "\n");
  1707. buffer_fmt(tbuf, "}\n");
  1708. buffer_appendb(gbuf, tbuf);
  1709. EMIT("qi_add_defer(state, -1, __defer%d);", gid);
  1710. } break;
  1711. case N_RETURN:
  1712. if (!INCTX("func"))
  1713. COMPILE_ERROR("return outside of a function");
  1714. for (size_t i = 0; i < SCOPESK; i++)
  1715. EMIT("qi_old_scope(state);\n");
  1716. for (size_t i = 0; i < TRAPSK; i++)
  1717. EMIT("qi_unset_trap(state, trap);\n");
  1718. EMIT("return ");
  1719. if (node->a)
  1720. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1721. else EMIT("state->nil");
  1722. EMIT(";");
  1723. break;
  1724. case N_FUNCDEF: break;
  1725. case N_PASS: break;
  1726. case N_TRY:
  1727. EMIT("qi_try(state, {\n");
  1728. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1729. EMIT("}, {\n");
  1730. if (node->t)
  1731. EMIT("qi_decl(state, \"%s\", trap->value);\n", node->t->text);
  1732. CTXPUSH("trap");
  1733. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1734. CTXPOP();
  1735. EMIT("}, NULL);\n");
  1736. break;
  1737. case N_THROW:
  1738. EMIT("qi_throw(state, ");
  1739. if (node->a)
  1740. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1741. else {
  1742. EMIT("state->nil");
  1743. }
  1744. EMIT(");");
  1745. break;
  1746. case N_LABEL: {
  1747. char *label = node->t->text;
  1748. table_iterate((table_t *)list_index(lbl, -1), {
  1749. if (strcmp(entry.key, label) == 0) {
  1750. COMPILE_ERROR("duplicated label: '%s'", label);
  1751. }
  1752. });
  1753. NEWGID();
  1754. EMIT("__label%d:;", gid);
  1755. size_t *n = malloc(sizeof(size_t));
  1756. memcpy(n, &gid, sizeof(size_t));
  1757. table_set(list_index(lbl, -1), label, n);
  1758. } break;
  1759. case N_GOTO: {
  1760. ssize_t gid = -1;
  1761. char *label = node->t->text;
  1762. table_iterate((table_t *)list_index(lbl, -1), {
  1763. if (strcmp(entry.key, label) == 0) {
  1764. gid = *(size_t *)entry.value;
  1765. break;
  1766. }
  1767. });
  1768. if (gid < 0)
  1769. COMPILE_ERROR("undefined label: '%s'", label);
  1770. EMIT("goto __label%d;", gid);
  1771. } break;
  1772. case N_REQUIRE: {
  1773. char *path = unescape(node->t->text);
  1774. if (require_once(gbuf, buf, ctx, lstk, lbl, path) < 0)
  1775. COMPILE_ERROR("'%s' is not a valid file path or a builtin library name", path);
  1776. } break;
  1777. case N_IFEXPR:
  1778. EMIT("(_qi_truthy(state, ");
  1779. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1780. EMIT(")? ");
  1781. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1782. EMIT(": ");
  1783. compile_node(gbuf, buf, ctx, lstk, lbl, node->c);
  1784. EMIT(")");
  1785. break;
  1786. case N_FUNCEXPR:
  1787. compile_func(gbuf, buf, ctx, lstk, lbl, node);
  1788. break;
  1789. case N_EQUALS:
  1790. BINOP("equals");
  1791. break;
  1792. case N_NOTEQUALS:
  1793. BINOP("not_equals");
  1794. break;
  1795. case N_IS:
  1796. BINOP("is");
  1797. break;
  1798. case N_NOTIS:
  1799. BINOP("not_is");
  1800. break;
  1801. case N_IN:
  1802. BINOP("in");
  1803. break;
  1804. case N_NOTIN:
  1805. BINOP("not_in");
  1806. break;
  1807. case N_LT:
  1808. BINOP("lt");
  1809. break;
  1810. case N_GT:
  1811. BINOP("gt");
  1812. break;
  1813. case N_LE:
  1814. BINOP("le");
  1815. break;
  1816. case N_GE:
  1817. BINOP("ge");
  1818. break;
  1819. case N_ADD:
  1820. BINOP("add");
  1821. break;
  1822. case N_SUB:
  1823. BINOP("sub");
  1824. break;
  1825. case N_MUL:
  1826. BINOP("mul");
  1827. break;
  1828. case N_DIV:
  1829. BINOP("div");
  1830. break;
  1831. case N_IDIV:
  1832. BINOP("idiv");
  1833. break;
  1834. case N_MOD:
  1835. BINOP("mod");
  1836. break;
  1837. case N_POW:
  1838. BINOP("pow");
  1839. break;
  1840. case N_SHL:
  1841. BINOP("shl");
  1842. break;
  1843. case N_SHR:
  1844. BINOP("shr");
  1845. break;
  1846. case N_XOR:
  1847. BINOP("xor");
  1848. break;
  1849. case N_BOR:
  1850. BINOP("bor");
  1851. break;
  1852. case N_BAND:
  1853. BINOP("band");
  1854. break;
  1855. case N_NEGATE:
  1856. UNOP("negate");
  1857. break;
  1858. case N_NOT:
  1859. UNOP("not");
  1860. break;
  1861. case N_BNOT:
  1862. UNOP("bnot");
  1863. break;
  1864. case N_INLINE: EMIT("%s;", unescape(node->t->text)); break;
  1865. default:
  1866. COMPILE_ERROR("not yet implemented");
  1867. }
  1868. }
  1869. void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl) {
  1870. node_t *n = parse(source);
  1871. compile_node(gbuf, buf, ctx, lstk, lbl, n);
  1872. }
  1873. char *compile(char *source) {
  1874. list_t *ctx = list_new();
  1875. stack_t *lstk = stack_new();
  1876. list_t *lbl = list_new();
  1877. LBPUSH();
  1878. buffer_t *gbuf = buffer_new();
  1879. buffer_appends(gbuf, "#include <qirt.h>\n");
  1880. buffer_t *buf = buffer_new();
  1881. require_once(gbuf, buf, ctx, lstk, lbl, "std");
  1882. compile_into(source, gbuf, buf, ctx, lstk, lbl);
  1883. buffer_t *rbuf = buffer_new();
  1884. buffer_appendb(rbuf, gbuf);
  1885. buffer_appends(rbuf, "int main(int argc, char **argv) {\n");
  1886. buffer_appends(rbuf, "qi_state_t *state;\n");
  1887. buffer_appends(rbuf, "qi_state_init(&state);\n");
  1888. buffer_appendb(rbuf, buf);
  1889. buffer_appends(rbuf, "qi_old_scope(state);\n");
  1890. buffer_appends(rbuf, "qi_finalize();\n");
  1891. buffer_appends(rbuf, "return 0;\n");
  1892. buffer_appends(rbuf, "}\n");
  1893. return buffer_read(rbuf);
  1894. }
  1895. char *compile_file(char *filename, FILE *fd) {
  1896. buffer_t *buf = buffer_new();
  1897. for (;;) {
  1898. char line[512];
  1899. if (!fgets(line, sizeof(line), fd))
  1900. break;
  1901. buffer_appends(buf, line);
  1902. }
  1903. char *source = buffer_read(buf);
  1904. list_t *pair = list_new();
  1905. list_push(pair, filename);
  1906. list_push(pair, source);
  1907. list_push(FILES, pair);
  1908. char *out = compile(source);
  1909. list_pop(FILES);
  1910. return out;
  1911. }
  1912. int main(int argc, char **argv) {
  1913. FILES = list_new();
  1914. REQUIRED = list_new();
  1915. char *out = compile_file("<stdin>", stdin);
  1916. fwrite(out, sizeof(char), strlen(out), stdout);
  1917. return 0;
  1918. }