qic.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  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_BREAK,
  207. T_CONTINUE,
  208. T_PASS,
  209. T_FUNC,
  210. T_USE,
  211. T_RETURN,
  212. T_DEFER,
  213. T_REQUIRE,
  214. T_TRY,
  215. T_CATCH,
  216. T_THROW,
  217. T_GOTO,
  218. T_LPAR,
  219. T_RPAR,
  220. T_LSB,
  221. T_RSB,
  222. T_LCB,
  223. T_RCB,
  224. T_EQUALS,
  225. T_PLUSASSIGN,
  226. T_BARBAR,
  227. T_ANDAND,
  228. T_STARSTAR,
  229. T_PLUSPLUS,
  230. T_MINUSMINUS,
  231. T_SLASHSLASH,
  232. T_PLUS,
  233. T_MINUS,
  234. T_QM,
  235. T_COLON,
  236. T_BAR,
  237. T_AND,
  238. T_RAISE,
  239. T_LT,
  240. T_LTLT,
  241. T_GT,
  242. T_GTGT,
  243. T_STAR,
  244. T_SLASH,
  245. T_COMMA,
  246. T_DOT,
  247. T_ASSIGN,
  248. T_SEMI
  249. } tag;
  250. char *text;
  251. size_t fi;
  252. size_t pos;
  253. } token_t;
  254. token_t *token(int tag, char *text) {
  255. token_t *tok = malloc(sizeof(token_t));
  256. tok->tag = tag;
  257. tok->text = text;
  258. return tok;
  259. }
  260. #define TK(tk) (token(T_##tk, NULL))
  261. #define WS() while (source[*pos] == ' ' || source[*pos] == '\t' || source[*pos] == '\n' || source[*pos] == '\r') { (*pos)++; }
  262. void consume_ignored(char *source, size_t *pos) {
  263. WS();
  264. while (source[*pos] == '#') {
  265. (*pos)++;
  266. for (;;) {
  267. if (!source[*pos])
  268. break;
  269. if (source[*pos] == '\n') {
  270. (*pos)++;
  271. break;
  272. }
  273. (*pos)++;
  274. }
  275. WS();
  276. }
  277. }
  278. list_t *FILES;
  279. list_t *REQUIRED;
  280. int is_required(char *path) {
  281. for (size_t i = 0; i < REQUIRED->length; i++)
  282. if (strcmp(REQUIRED->data[i], path) == 0)
  283. return 1;
  284. return 0;
  285. }
  286. void traverse(char *source, size_t pos, size_t *line, size_t *col) {
  287. *line = 1;
  288. *col = 1;
  289. for (size_t i = 0; i < pos; i++) {
  290. if (source[i] == '\n') {
  291. (*line)++;
  292. (*col) = 1;
  293. } else (*col)++;
  294. }
  295. }
  296. void format_error(char *filename, char *source, size_t pos, char *fmt, ...) {
  297. size_t line, col;
  298. traverse(source, pos, &line, &col);
  299. va_list args;
  300. va_start(args, fmt);
  301. fprintf(stderr, "%s (%zu:%zu): ", filename, line, col);
  302. vfprintf(stderr, fmt, args);
  303. fputc('\n', stderr);
  304. va_end(args);
  305. }
  306. #define GETFNAME(fi) ((char *)((list_t *)list_index(FILES, fi))->data[0])
  307. #define GETSRC(fi) ((char *)((list_t *)list_index(FILES, fi))->data[1])
  308. #define LEX_ERROR(fmt, ...) { format_error(GETFNAME(-1), source, *pos, fmt, ##__VA_ARGS__); exit(1); }
  309. token_t *next_token(char *source, size_t *pos) {
  310. if (!source[*pos])
  311. return token(T_EOF, NULL);
  312. if (source[*pos] == '"' || source[*pos] == '\'') {
  313. char term = source[(*pos)++];
  314. buffer_t *text = buffer_new();
  315. while (source[*pos] != term) {
  316. if (!source[*pos])
  317. LEX_ERROR("unterminated string literal");
  318. char c = source[(*pos)++];
  319. if (c == '\\') {
  320. char nc = source[(*pos)++];
  321. if (!nc)
  322. continue;
  323. switch (nc) {
  324. case 'n':
  325. buffer_appends(text, "\\n");
  326. break;
  327. case 't':
  328. buffer_appends(text, "\\t");
  329. break;
  330. case 'r':
  331. buffer_appends(text, "\\r");
  332. break;
  333. case 'b':
  334. buffer_appends(text, "\\b");
  335. break;
  336. case 'e':
  337. buffer_appends(text, "\\e");
  338. break;
  339. case 's':
  340. buffer_appends(text, " ");
  341. break;
  342. case '"':
  343. buffer_appends(text, "\\\"");
  344. break;
  345. case '\\':
  346. buffer_appends(text, "\\\\");
  347. break;
  348. default:
  349. buffer_append(text, nc);
  350. break;
  351. }
  352. continue;
  353. }
  354. if (c == '"' || c == '\\')
  355. buffer_append(text, '\\');
  356. buffer_append(text, c);
  357. }
  358. (*pos)++;
  359. return token(T_STRING, buffer_read(text));
  360. } else if (isdigit(source[*pos])) {
  361. buffer_t *number = buffer_new();
  362. int dot = 0;
  363. int sub = 0;
  364. do {
  365. buffer_append(number, source[(*pos)++]);
  366. if (!dot && source[*pos] == '.') {
  367. buffer_append(number, source[(*pos)++]);
  368. if (!isdigit(source[*pos]))
  369. LEX_ERROR("illegal number literal (missing part after floating point)");
  370. dot = 1;
  371. } else if (!sub && source[*pos] == '.') {
  372. (*pos)++;
  373. sub = 1;
  374. } else if (sub) sub = 0;
  375. } while (isdigit(source[*pos]));
  376. return token(T_NUMBER, buffer_read(number));
  377. } else if (isalpha(source[*pos]) || source[*pos] == '_') {
  378. buffer_t *text = buffer_new();
  379. do {
  380. buffer_append(text, source[(*pos)++]);
  381. } while (isalpha(source[*pos]) || source[*pos] == '_' || isdigit(source[*pos]));
  382. char *name = buffer_read(text);
  383. if (strcmp(name, "var") == 0)
  384. return TK(VAR);
  385. else if (strcmp(name, "let") == 0)
  386. return TK(LET);
  387. else if (strcmp(name, "if") == 0)
  388. return TK(IF);
  389. else if (strcmp(name, "else") == 0)
  390. return TK(ELSE);
  391. else if (strcmp(name, "elif") == 0)
  392. return TK(ELIF);
  393. else if (strcmp(name, "for") == 0)
  394. return TK(FOR);
  395. else if (strcmp(name, "break") == 0)
  396. return TK(BREAK);
  397. else if (strcmp(name, "continue") == 0)
  398. return TK(CONTINUE);
  399. else if (strcmp(name, "func") == 0)
  400. return TK(FUNC);
  401. else if (strcmp(name, "use") == 0)
  402. return TK(USE);
  403. else if (strcmp(name, "return") == 0)
  404. return TK(RETURN);
  405. else if (strcmp(name, "defer") == 0)
  406. return TK(DEFER);
  407. else if (strcmp(name, "pass") == 0)
  408. return TK(PASS);
  409. else if (strcmp(name, "require") == 0)
  410. return TK(REQUIRE);
  411. else if (strcmp(name, "try") == 0)
  412. return TK(TRY);
  413. else if (strcmp(name, "catch") == 0)
  414. return TK(CATCH);
  415. else if (strcmp(name, "throw") == 0)
  416. return TK(THROW);
  417. else if (strcmp(name, "goto") == 0)
  418. return TK(GOTO);
  419. return token(T_NAME, name);
  420. } else if (strncmp(&source[*pos], "==", 2) == 0 && ++(*pos) && ++(*pos))
  421. return TK(EQUALS);
  422. else if (strncmp(&source[*pos], "+=", 2) == 0 && ++(*pos) && ++(*pos))
  423. return TK(PLUSASSIGN);
  424. else if (strncmp(&source[*pos], "||", 2) == 0 && ++(*pos) && ++(*pos))
  425. return TK(BARBAR);
  426. else if (strncmp(&source[*pos], "&&", 2) == 0 && ++(*pos) && ++(*pos))
  427. return TK(ANDAND);
  428. else if (strncmp(&source[*pos], "++", 2) == 0 && ++(*pos) && ++(*pos))
  429. return TK(PLUSPLUS);
  430. else if (strncmp(&source[*pos], "--", 2) == 0 && ++(*pos) && ++(*pos))
  431. return TK(MINUSMINUS);
  432. else if (strncmp(&source[*pos], "//", 2) == 0 && ++(*pos) && ++(*pos))
  433. return TK(SLASHSLASH);
  434. else if (strncmp(&source[*pos], "**", 2) == 0 && ++(*pos) && ++(*pos))
  435. return TK(STARSTAR);
  436. else if (source[*pos] == '(' && ++(*pos))
  437. return TK(LPAR);
  438. else if (source[*pos] == ')' && ++(*pos))
  439. return TK(RPAR);
  440. else if (source[*pos] == '[' && ++(*pos))
  441. return TK(LSB);
  442. else if (source[*pos] == ']' && ++(*pos))
  443. return TK(RSB);
  444. else if (source[*pos] == '{' && ++(*pos))
  445. return TK(LCB);
  446. else if (source[*pos] == '}' && ++(*pos))
  447. return TK(RCB);
  448. else if (source[*pos] == '+' && ++(*pos))
  449. return TK(PLUS);
  450. else if (source[*pos] == '-' && ++(*pos))
  451. return TK(MINUS);
  452. else if (source[*pos] == '*' && ++(*pos))
  453. return TK(STAR);
  454. else if (source[*pos] == '/' && ++(*pos))
  455. return TK(SLASH);
  456. else if (source[*pos] == '?' && ++(*pos))
  457. return TK(QM);
  458. else if (source[*pos] == ':' && ++(*pos))
  459. return TK(COLON);
  460. else if (source[*pos] == '=' && ++(*pos))
  461. return TK(ASSIGN);
  462. else if (source[*pos] == ';' && ++(*pos))
  463. return TK(SEMI);
  464. else if (source[*pos] == ',' && ++(*pos))
  465. return TK(COMMA);
  466. else if (source[*pos] == '.' && ++(*pos))
  467. return TK(DOT);
  468. else if (source[*pos] == '<' && ++(*pos))
  469. return TK(LT);
  470. else if (source[*pos] == '>' && ++(*pos))
  471. return TK(GT);
  472. LEX_ERROR("unexpected input")
  473. }
  474. list_t *tokenize(char *source) {
  475. size_t pos = 0;
  476. list_t *toks = list_new();
  477. do {
  478. consume_ignored(source, &pos);
  479. size_t tok_pos = pos;
  480. token_t *tok = next_token(source, &pos);
  481. tok->fi = FILES->length-1;
  482. tok->pos = tok_pos;
  483. list_push(toks, tok);
  484. if (tok->tag == T_EOF)
  485. break;
  486. } while (1);
  487. return toks;
  488. }
  489. struct _node_t {
  490. enum {
  491. N_PROGRAM,
  492. N_EXPRSTMT,
  493. N_BLOCK,
  494. N_NEGATE,
  495. N_LITERAL,
  496. N_LIST,
  497. N_TUPLE,
  498. N_NILTUPLE,
  499. N_TABLE,
  500. N_CALL,
  501. N_MEMBER,
  502. N_INDEX,
  503. N_ADD,
  504. N_SUB,
  505. N_MUL,
  506. N_DIV,
  507. N_IDIV,
  508. N_POW,
  509. N_ASSIGN,
  510. N_ASSIGN_ADD,
  511. N_EQUALS,
  512. N_LT,
  513. N_GT,
  514. N_INC,
  515. N_DEC,
  516. N_VAR,
  517. N_LET,
  518. N_IF,
  519. N_FOR,
  520. N_BREAK,
  521. N_CONTINUE,
  522. N_FUNCDEF,
  523. N_RETURN,
  524. N_DEFER,
  525. N_PASS,
  526. N_REQUIRE,
  527. N_TRY,
  528. N_THROW,
  529. N_LABEL,
  530. N_GOTO,
  531. N_IFEXPR,
  532. N_FUNCEXPR,
  533. N_LOGOR,
  534. N_LOGAND,
  535. } tag;
  536. struct _node_t *a;
  537. struct _node_t *b;
  538. struct _node_t *c;
  539. struct _node_t *d;
  540. list_t *l;
  541. table_t *h;
  542. table_t *h2;
  543. token_t *t;
  544. size_t fi;
  545. size_t pos;
  546. };
  547. typedef struct _node_t node_t;
  548. node_t *node_pos(node_t *node, size_t fi, size_t pos) {
  549. node->fi = fi;
  550. node->pos = pos;
  551. return node;
  552. }
  553. node_t *nodet(int tag, token_t *t) {
  554. node_t *node = malloc(sizeof(node_t));
  555. node->tag = tag;
  556. node->t = t;
  557. return node;
  558. }
  559. #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))
  560. node_t *nodel(int tag, list_t *l) {
  561. node_t *node = malloc(sizeof(node_t));
  562. node->tag = tag;
  563. node->l = l;
  564. return node;
  565. }
  566. #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))
  567. node_t *nodeh(int tag, table_t *h) {
  568. node_t *node = malloc(sizeof(node_t));
  569. node->tag = tag;
  570. node->h = h;
  571. return node;
  572. }
  573. #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))
  574. node_t *node0(int tag) {
  575. node_t *node = malloc(sizeof(node_t));
  576. node->tag = tag;
  577. return node;
  578. }
  579. #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))
  580. node_t *node1(int tag, node_t *a) {
  581. node_t *node = malloc(sizeof(node_t));
  582. node->tag = tag;
  583. node->a = a;
  584. return node;
  585. }
  586. #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))
  587. node_t *node1l(int tag, node_t *a, list_t *l) {
  588. node_t *node = malloc(sizeof(node_t));
  589. node->tag = tag;
  590. node->a = a;
  591. node->l = l;
  592. return node;
  593. }
  594. #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))
  595. node_t *node1t(int tag, node_t *a, token_t *t) {
  596. node_t *node = malloc(sizeof(node_t));
  597. node->tag = tag;
  598. node->a = a;
  599. node->t = t;
  600. return node;
  601. }
  602. #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))
  603. node_t *node2(int tag, node_t *a, node_t *b) {
  604. node_t *node = malloc(sizeof(node_t));
  605. node->tag = tag;
  606. node->a = a;
  607. node->b = b;
  608. return node;
  609. }
  610. #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))
  611. node_t *node2t(int tag, node_t *a, node_t *b, token_t *t) {
  612. node_t *node = malloc(sizeof(node_t));
  613. node->tag = tag;
  614. node->a = a;
  615. node->b = b;
  616. node->t = t;
  617. return node;
  618. }
  619. #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))
  620. node_t *node3(int tag, node_t *a, node_t *b, node_t *c) {
  621. node_t *node = malloc(sizeof(node_t));
  622. node->tag = tag;
  623. node->a = a;
  624. node->b = b;
  625. node->c = c;
  626. return node;
  627. }
  628. #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))
  629. node_t *node4(int tag, node_t *a, node_t *b, node_t *c, node_t *d) {
  630. node_t *node = malloc(sizeof(node_t));
  631. node->tag = tag;
  632. node->a = a;
  633. node->b = b;
  634. node->c = c;
  635. node->d = d;
  636. return node;
  637. }
  638. #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))
  639. node_t *nodef(int tag, token_t *name, table_t *params, table_t *captured, node_t *body) {
  640. node_t *node = malloc(sizeof(node_t));
  641. node->tag = tag;
  642. node->t = name;
  643. node->h = params;
  644. node->h2 = captured;
  645. node->a = body;
  646. return node;
  647. }
  648. #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))
  649. #define AT(tk) (*pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == T_##tk)
  650. #define ATP(tk, p) ((*pos)+p < tokens->length && ((token_t *)tokens->data[(*pos)+p])->tag == T_##tk)
  651. #define MATCH(tk) (AT(tk) && ++(*pos))
  652. #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); }
  653. #define EXPECT(tk, s) { if (!MATCH(tk)) PARSE_ERROR("expected %s", (s)); }
  654. node_t *parse_expr(list_t *tokens, size_t *pos);
  655. list_t *parse_sequence(list_t *tokens, size_t *pos, int term) {
  656. list_t *seq = list_new();
  657. do {
  658. if (term != -1 && *pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == term)
  659. break;
  660. list_push(seq, parse_expr(tokens, pos));
  661. } while (MATCH(COMMA));
  662. return seq;
  663. }
  664. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr);
  665. node_t *parse_primary(list_t *tokens, size_t *pos) {
  666. if (MATCH(FUNC))
  667. return parse_func(tokens, pos, 1);
  668. else if (MATCH(LPAR)) {
  669. if (MATCH(RPAR))
  670. return NODE0(NILTUPLE);
  671. node_t *a = parse_expr(tokens, pos);
  672. if (MATCH(COMMA)) {
  673. list_t *l = list_new();
  674. list_push(l, a);
  675. if (!AT(RPAR))
  676. do {
  677. node_t *n = parse_expr(tokens, pos);
  678. list_push(l, n);
  679. } while (MATCH(COMMA));
  680. a = NODEL(TUPLE, l);
  681. }
  682. EXPECT(RPAR, ")");
  683. return a;
  684. } else if (MATCH(LSB)) {
  685. list_t *a = parse_sequence(tokens, pos, T_RSB);
  686. EXPECT(RSB, "]");
  687. return NODEL(LIST, a);
  688. } else if (MATCH(LCB)) {
  689. table_t *table = table_new();
  690. do {
  691. if (AT(RCB))
  692. break;
  693. if (!AT(NAME) && !AT(STRING))
  694. PARSE_ERROR("expected identifier or string");
  695. char *key = ((token_t *)tokens->data[(*pos)++])->text;
  696. EXPECT(COLON, ":");
  697. node_t *val = parse_expr(tokens, pos);
  698. table_set(table, key, val);
  699. } while (MATCH(COMMA));
  700. EXPECT(RCB, "}");
  701. return NODEH(TABLE, table);
  702. } else if (MATCH(NUMBER) || MATCH(STRING) || MATCH(NAME))
  703. return NODET(LITERAL, tokens->data[(*pos)-1]);
  704. PARSE_ERROR("expected expression");
  705. return NULL;
  706. }
  707. size_t get_lineno(token_t *tok) {
  708. size_t line, col;
  709. traverse(GETSRC(tok->fi), tok->pos, &line, &col);
  710. return line;
  711. }
  712. #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)])))
  713. node_t *parse_call(list_t *tokens, size_t *pos) {
  714. node_t *a = parse_primary(tokens, pos);
  715. do {
  716. if (!CLIFF && MATCH(LPAR)) {
  717. list_t *b = NULL;
  718. if (!AT(RPAR))
  719. b = parse_sequence(tokens, pos, -1);
  720. EXPECT(RPAR, ")");
  721. a = NODE1l(CALL, a, b);
  722. continue;
  723. } else if (!CLIFF && MATCH(LSB)) {
  724. node_t *b = parse_expr(tokens, pos);
  725. EXPECT(RSB, "]");
  726. a = NODE2(INDEX, a, b);
  727. continue;
  728. } else if (!CLIFF && MATCH(DOT)) {
  729. if (!AT(NAME))
  730. PARSE_ERROR("expected identifier after .");
  731. a = NODE1t(MEMBER, a, tokens->data[(*pos)++]);
  732. continue;
  733. }
  734. break;
  735. } while (1);
  736. return a;
  737. }
  738. node_t *parse_postfix(list_t *tokens, size_t *pos) {
  739. node_t *a = parse_call(tokens, pos);
  740. if (CLIFF)
  741. return a;
  742. if (MATCH(PLUSPLUS))
  743. return NODE1(INC, a);
  744. else if (MATCH(MINUSMINUS))
  745. return NODE1(DEC, a);
  746. return a;
  747. }
  748. node_t *parse_unary(list_t *tokens, size_t *pos) {
  749. if (MATCH(MINUS)) {
  750. node_t *a = parse_unary(tokens, pos);
  751. return NODE1(NEGATE, a);
  752. }
  753. return parse_postfix(tokens, pos);
  754. }
  755. node_t *parse_pow(list_t *tokens, size_t *pos) {
  756. node_t *a = parse_unary(tokens, pos);
  757. do {
  758. if (MATCH(STARSTAR)) {
  759. node_t *b = parse_unary(tokens, pos);
  760. a = NODE2(POW, a, b);
  761. continue;
  762. }
  763. break;
  764. } while (1);
  765. return a;
  766. }
  767. node_t *parse_mul(list_t *tokens, size_t *pos) {
  768. node_t *a = parse_pow(tokens, pos);
  769. do {
  770. if (MATCH(STAR)) {
  771. node_t *b = parse_pow(tokens, pos);
  772. a = NODE2(MUL, a, b);
  773. continue;
  774. } else if (MATCH(SLASH)) {
  775. node_t *b = parse_pow(tokens, pos);
  776. a = NODE2(DIV, a, b);
  777. continue;
  778. } else if (MATCH(SLASHSLASH)) {
  779. node_t *b = parse_pow(tokens, pos);
  780. a = NODE2(IDIV, a, b);
  781. continue;
  782. }
  783. break;
  784. } while (1);
  785. return a;
  786. }
  787. node_t *parse_add(list_t *tokens, size_t *pos) {
  788. node_t *a = parse_mul(tokens, pos);
  789. do {
  790. if (MATCH(PLUS)) {
  791. node_t *b = parse_mul(tokens, pos);
  792. a = NODE2(ADD, a, b);
  793. continue;
  794. } else if (MATCH(MINUS)) {
  795. node_t *b = parse_mul(tokens, pos);
  796. a = NODE2(SUB, a, b);
  797. continue;
  798. }
  799. break;
  800. } while (1);
  801. return a;
  802. }
  803. node_t *parse_shift(list_t *tokens, size_t *pos) {
  804. node_t *a = parse_add(tokens, pos);
  805. return a;
  806. }
  807. node_t *parse_relation(list_t *tokens, size_t *pos) {
  808. node_t *a = parse_shift(tokens, pos);
  809. do {
  810. if (MATCH(LT)) {
  811. node_t *b = parse_shift(tokens, pos);
  812. a = NODE2(LT, a, b);
  813. continue;
  814. } else if (MATCH(GT)) {
  815. node_t *b = parse_shift(tokens, pos);
  816. a = NODE2(GT, a, b);
  817. continue;
  818. }
  819. break;
  820. } while (1);
  821. return a;
  822. }
  823. node_t *parse_equality(list_t *tokens, size_t *pos) {
  824. node_t *a = parse_relation(tokens, pos);
  825. do {
  826. if (MATCH(EQUALS)) {
  827. node_t *b = parse_relation(tokens, pos);
  828. a = NODE2(EQUALS, a, b);
  829. continue;
  830. }
  831. break;
  832. } while (1);
  833. return a;
  834. }
  835. node_t *parse_bitand(list_t *tokens, size_t *pos) {
  836. node_t *a = parse_equality(tokens, pos);
  837. return a;
  838. }
  839. node_t *parse_bitxor(list_t *tokens, size_t *pos) {
  840. node_t *a = parse_bitand(tokens, pos);
  841. return a;
  842. }
  843. node_t *parse_bitor(list_t *tokens, size_t *pos) {
  844. node_t *a = parse_bitxor(tokens, pos);
  845. return a;
  846. }
  847. node_t *parse_logand(list_t *tokens, size_t *pos) {
  848. node_t *a = parse_bitor(tokens, pos);
  849. if (MATCH(ANDAND)) {
  850. node_t *b = parse_logand(tokens, pos);
  851. return NODE2(LOGAND, a, b);
  852. }
  853. return a;
  854. }
  855. node_t *parse_logor(list_t *tokens, size_t *pos) {
  856. node_t *a = parse_logand(tokens, pos);
  857. if (MATCH(BARBAR)) {
  858. node_t *b = parse_logor(tokens, pos);
  859. return NODE2(LOGOR, a, b);
  860. }
  861. return a;
  862. }
  863. node_t *parse_assignment(list_t *tokens, size_t *pos);
  864. node_t *parse_conditional(list_t *tokens, size_t *pos) {
  865. node_t *a = parse_logor(tokens, pos);
  866. if (MATCH(QM)) {
  867. node_t *b = parse_assignment(tokens, pos);
  868. EXPECT(COLON, ":");
  869. node_t *c = parse_assignment(tokens, pos);
  870. return NODE3(IFEXPR, a, b, c);
  871. }
  872. return a;
  873. }
  874. node_t *parse_assignment(list_t *tokens, size_t *pos) {
  875. node_t *a = parse_conditional(tokens, pos);
  876. if (MATCH(ASSIGN)) {
  877. node_t *b = parse_assignment(tokens, pos);
  878. return NODE2(ASSIGN, a, b);
  879. } else if (MATCH(PLUSASSIGN)) {
  880. node_t *b = parse_assignment(tokens, pos);
  881. return NODE2(ASSIGN_ADD, a, b);
  882. }
  883. return a;
  884. }
  885. node_t *parse_expr(list_t *tokens, size_t *pos) {
  886. return parse_assignment(tokens, pos);
  887. }
  888. node_t *parse_stmt(list_t *tokens, size_t *pos);
  889. node_t *parse_block(list_t *tokens, size_t *pos) {
  890. EXPECT(LCB, "{");
  891. list_t *stmts = list_new();
  892. while (!AT(EOF) && !AT(RCB))
  893. list_push(stmts, parse_stmt(tokens, pos));
  894. EXPECT(RCB, "}");
  895. return NODEL(PROGRAM, stmts);
  896. }
  897. #define BLOCK() (MATCH(COLON)?parse_stmt(tokens, pos):parse_block(tokens, pos))
  898. node_t *parse_if(list_t *tokens, size_t *pos) {
  899. node_t *a = parse_expr(tokens, pos);
  900. node_t *b = BLOCK();
  901. node_t *c = NULL;
  902. if (MATCH(ELSE))
  903. c = BLOCK();
  904. else if (MATCH(ELIF))
  905. c = parse_if(tokens, pos);
  906. return NODE3(IF, a, b, c);
  907. }
  908. node_t *parse_var(list_t *tokens, size_t *pos, int is_let) {
  909. table_t *h = table_new();
  910. do {
  911. if(!AT(NAME))
  912. PARSE_ERROR("expected identifier");
  913. char *k = ((token_t *)tokens->data[(*pos)++])->text;
  914. node_t *v = NULL;
  915. if (is_let) {
  916. EXPECT(ASSIGN, "=");
  917. v = parse_expr(tokens, pos);
  918. } else if (MATCH(ASSIGN))
  919. v = parse_expr(tokens, pos);
  920. table_set(h, k, v);
  921. } while (MATCH(COMMA));
  922. if (is_let)
  923. return NODEH(LET, h);
  924. return NODEH(VAR, h);
  925. }
  926. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr) {
  927. token_t *name = NULL;
  928. if (!is_expr) {
  929. if(!AT(NAME))
  930. PARSE_ERROR("expected identifier");
  931. name = tokens->data[(*pos)++];
  932. }
  933. EXPECT(LPAR, "(");
  934. table_t *params = NULL;
  935. if (!AT(RPAR)) {
  936. int flag = 0;
  937. params = table_new();
  938. size_t argc = 0;
  939. do {
  940. if(!AT(NAME))
  941. PARSE_ERROR("expected identifier");
  942. char *l = ((token_t *)tokens->data[(*pos)++])->text;
  943. node_t *r = NULL;
  944. if (!flag && AT(ASSIGN))
  945. flag = 1;
  946. if (flag) {
  947. EXPECT(ASSIGN, "=");
  948. r = parse_expr(tokens, pos);
  949. }
  950. list_t *pair = list_new();
  951. size_t *argcp = malloc(sizeof(size_t));
  952. memcpy(argcp, &argc, sizeof(size_t));
  953. argc++;
  954. list_push(pair, argcp);
  955. list_push(pair, r);
  956. table_set(params, l, pair);
  957. } while (MATCH(COMMA));
  958. }
  959. EXPECT(RPAR, ")");
  960. table_t *captured = NULL;
  961. if (MATCH(USE)) {
  962. EXPECT(RPAR, "(");
  963. captured = table_new();
  964. do {
  965. if(!AT(NAME))
  966. PARSE_ERROR("expected identifier");
  967. token_t *name = tokens->data[(*pos)++];
  968. table_set(captured, name->text, NODET(LITERAL, name));
  969. } while (MATCH(COMMA));
  970. EXPECT(RPAR, ")");
  971. }
  972. node_t *body = BLOCK();
  973. if (is_expr)
  974. return NODEF(FUNCEXPR, NULL, params, captured, body);
  975. return NODEF(FUNCDEF, name, params, captured, body);
  976. }
  977. node_t *parse_stmt(list_t *tokens, size_t *pos) {
  978. if (MATCH(LCB)) {
  979. list_t *stmts = list_new();
  980. while (!AT(EOF) && !AT(RCB)) {
  981. node_t *n = parse_stmt(tokens, pos);
  982. MATCH(SEMI);
  983. list_push(stmts, n);
  984. }
  985. EXPECT(RCB, "}");
  986. return NODEL(BLOCK, stmts);
  987. } else if (MATCH(VAR))
  988. return parse_var(tokens, pos, 0);
  989. else if (MATCH(LET))
  990. return parse_var(tokens, pos, 1);
  991. else if (MATCH(IF))
  992. return parse_if(tokens, pos);
  993. else if (MATCH(FOR)) {
  994. node_t *a = NULL;
  995. node_t *b = NULL;
  996. node_t *c = NULL;
  997. if (!AT(LCB) && !AT(COLON)) {
  998. if (MATCH(VAR)) {
  999. a = parse_var(tokens, pos, 0);
  1000. EXPECT(SEMI, ";");
  1001. b = parse_expr(tokens, pos);
  1002. EXPECT(SEMI, ";");
  1003. c = parse_expr(tokens, pos);
  1004. } else a = parse_expr(tokens, pos);
  1005. }
  1006. node_t *d = BLOCK();
  1007. return NODE4(FOR, a, b, c, d);
  1008. } else if (MATCH(BREAK)) return NODE0(BREAK);
  1009. else if (MATCH(CONTINUE)) return NODE0(CONTINUE);
  1010. else if (MATCH(FUNC))
  1011. return parse_func(tokens, pos, 0);
  1012. else if (MATCH(RETURN)) {
  1013. node_t *a = NULL;
  1014. if (!AT(RCB) && !AT(EOF) && !CLIFF)
  1015. a = parse_expr(tokens, pos);
  1016. return NODE1(RETURN, a);
  1017. } else if (MATCH(DEFER)) {
  1018. node_t *a;
  1019. if (AT(LCB) || AT(COLON))
  1020. a = BLOCK();
  1021. else a = parse_stmt(tokens, pos);
  1022. return NODE1(DEFER, a);
  1023. } else if (MATCH(PASS)) return NODE0(PASS);
  1024. else if (MATCH(TRY)) {
  1025. node_t *a = BLOCK();
  1026. token_t *t = NULL;
  1027. EXPECT(CATCH, "catch");
  1028. if (!AT(COLON) && !AT(LCB)) {
  1029. if (!AT(NAME))
  1030. PARSE_ERROR("expected identifier");
  1031. t = tokens->data[(*pos)++];
  1032. }
  1033. node_t *b = BLOCK();
  1034. return NODE2t(TRY, a, b, t);
  1035. } else if (MATCH(THROW)) {
  1036. node_t *a = NULL;
  1037. if (!CLIFF)
  1038. a = parse_expr(tokens, pos);
  1039. return NODE1(THROW, a);
  1040. } else if (MATCH(GOTO)) {
  1041. if(!AT(NAME))
  1042. PARSE_ERROR("expected identifier");
  1043. token_t *t = tokens->data[(*pos)++];
  1044. return NODET(GOTO, t);
  1045. } else if (AT(NAME) && ATP(COLON, 1) && !CLIFF) {
  1046. token_t *t = tokens->data[(*pos)++];
  1047. EXPECT(COLON, ":");
  1048. return NODET(LABEL, t);
  1049. }
  1050. node_t *n = parse_expr(tokens, pos);
  1051. return NODE1(EXPRSTMT, n);
  1052. }
  1053. node_t *parse_program(list_t *tokens, size_t *pos) {
  1054. if (AT(EOF))
  1055. PARSE_ERROR("empty program");
  1056. list_t *stmts = list_new();
  1057. int flag = 0;
  1058. while (!AT(EOF) && *pos < tokens->length) {
  1059. node_t *n;
  1060. if (MATCH(REQUIRE)) {
  1061. if (flag)
  1062. PARSE_ERROR("misplaced require statement")
  1063. if (!AT(STRING))
  1064. PARSE_ERROR("expected string");
  1065. token_t *path = tokens->data[(*pos)++];
  1066. n = NODET(REQUIRE, path);
  1067. } else { n = parse_stmt(tokens, pos); flag = 1; }
  1068. MATCH(SEMI);
  1069. list_push(stmts, n);
  1070. }
  1071. return NODEL(PROGRAM, stmts);
  1072. }
  1073. node_t *parse(char *source) {
  1074. size_t pos = 0;
  1075. return parse_program(tokenize(source), &pos);
  1076. }
  1077. #define NEWGID() size_t gid = GID++
  1078. #define EMIT(fmt, ...) buffer_fmt(buf, (fmt), ##__VA_ARGS__);
  1079. #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(")"); }
  1080. #define UNOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, lstk, lbl, node->a); EMIT(")"); }
  1081. #define ASSIGN(lhs, rhs) {\
  1082. if ((lhs)->tag == N_LITERAL && (lhs)->t->tag == T_NAME) {\
  1083. EMIT("qi_set(state, false, \"%s\", ", (lhs)->t->text);\
  1084. rhs;\
  1085. EMIT(")");\
  1086. } else if ((lhs)->tag == N_INDEX) {\
  1087. EMIT("qi_index_set(state, false, ");\
  1088. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs)->a);\
  1089. EMIT(", ");\
  1090. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs)->b);\
  1091. EMIT(", ");\
  1092. rhs;\
  1093. EMIT(")");\
  1094. } else if ((lhs)->tag == N_MEMBER) {\
  1095. EMIT("qi_index_set(state, false, ");\
  1096. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs)->a);\
  1097. EMIT(", qi_make_string(state, \"%s\"), ", (lhs)->t->text);\
  1098. rhs;\
  1099. EMIT(")");\
  1100. } else COMPILE_ERROR("illegal assignment left-hand side");\
  1101. }
  1102. #define COMPASSIGN(lhs, s, rhs) {\
  1103. ASSIGN(node->a, {\
  1104. EMIT("qi_%s(state, ", s);\
  1105. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs));\
  1106. EMIT(", ");\
  1107. rhs;\
  1108. EMIT(")");\
  1109. });\
  1110. }
  1111. #define COMPILE_ERROR(fmt, ...) { format_error(GETFNAME(node->fi), GETSRC(node->fi), node->pos, fmt, ##__VA_ARGS__); exit(1); }
  1112. void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, node_t *node);
  1113. void compile_list(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, list_t *seq) {
  1114. if (!seq || seq->length < 1) {
  1115. EMIT("NULL");
  1116. return;
  1117. }
  1118. buffer_t *tbuf = buffer_new();
  1119. NEWGID();
  1120. buffer_fmt(tbuf, "qi_list_t *__list%d(qi_state_t *state) {\n", gid);
  1121. buffer_fmt(tbuf, "qi_list_t *list = qi_list_make();\n");
  1122. for (size_t i = 0; i < seq->length; i++) {
  1123. buffer_fmt(tbuf, "qi_list_push(list, ");
  1124. compile_node(gbuf, tbuf, ctx, lstk, lbl, seq->data[i]);
  1125. buffer_fmt(tbuf, ");\n");
  1126. }
  1127. buffer_fmt(tbuf, "return list;\n");
  1128. buffer_fmt(tbuf, "}\n");
  1129. buffer_appendb(gbuf, tbuf);
  1130. EMIT("__list%d(state)", gid);
  1131. }
  1132. void compile_table(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, table_t *table) {
  1133. if (!table || table->used < 1) {
  1134. EMIT("NULL");
  1135. return;
  1136. }
  1137. buffer_t *tbuf = buffer_new();
  1138. NEWGID();
  1139. buffer_fmt(tbuf, "qi_table_t *__table%d(qi_state_t *state) {\n", gid);
  1140. buffer_fmt(tbuf, "qi_table_t *table = qi_table_make();\n");
  1141. table_iterate(table, {
  1142. buffer_fmt(tbuf, "qi_table_set(table, \"%s\", ", entry.key);
  1143. compile_node(gbuf, tbuf, ctx, lstk, lbl, entry.value);
  1144. buffer_fmt(tbuf, ");\n");
  1145. });
  1146. buffer_fmt(tbuf, "return table;\n");
  1147. buffer_fmt(tbuf, "}\n");
  1148. buffer_appendb(gbuf, tbuf);
  1149. EMIT("__table%d(state)", gid);
  1150. }
  1151. #define CTXPUSH(s) list_push(ctx, (s))
  1152. #define CTXPOP() list_pop(ctx)
  1153. int in_context(list_t *ctx, char *s) {
  1154. if (!ctx->length)
  1155. return 0;
  1156. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  1157. if (strcmp(ctx->data[i], "gap") == 0)
  1158. break;
  1159. else if (strcmp(ctx->data[i], s) == 0)
  1160. return 1;
  1161. }
  1162. return 0;
  1163. }
  1164. size_t count_ctxs(list_t *ctx, char *s) {
  1165. if (!ctx->length)
  1166. return 0;
  1167. size_t k = 0;
  1168. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  1169. if (strcmp(ctx->data[i], "gap") == 0)
  1170. break;
  1171. else if (strcmp(ctx->data[i], s) == 0)
  1172. k++;
  1173. }
  1174. return k;
  1175. }
  1176. #define INCTX(s) (in_context(ctx, (s)))
  1177. #define SCOPESK (count_ctxs(ctx, "scope"))
  1178. #define TRAPSK (count_ctxs(ctx, "trap"))
  1179. #define LPUSH(i) stack_push(lstk, (i))
  1180. #define LPOP() stack_pop(lstk)
  1181. #define LID (lstk->data[lstk->length-1])
  1182. #define LBPUSH() list_push(lbl, table_new())
  1183. #define LBPOP() list_pop(lbl)
  1184. void compile_func(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, node_t *node) {
  1185. NEWGID();
  1186. buffer_t *tbuf = buffer_new();
  1187. buffer_fmt(tbuf, "qi_value_t *__func%d(qi_state_t *state, qi_size_t pargc, qi_list_t *pargs) {\n", gid);
  1188. LBPUSH();
  1189. CTXPUSH("gap");
  1190. CTXPUSH("func");
  1191. size_t optargc = 0;
  1192. if (node->h) {
  1193. table_iterate(node->h, {
  1194. list_t *pair = entry.value;
  1195. size_t argc = *(size_t *)pair->data[0];
  1196. if (pair->data[1]) {
  1197. optargc++;
  1198. buffer_fmt(tbuf, "qi_set(state, false, \"%s\", pargc >= %d? qi_list_index(pargs, %d): ", entry.key, argc+1, argc);
  1199. compile_node(gbuf, tbuf, ctx, lstk, lbl, pair->data[1]);
  1200. buffer_fmt(tbuf, ");\n");
  1201. } else
  1202. buffer_fmt(tbuf, "qi_set(state, false, \"%s\", qi_list_index(pargs, %d));\n", entry.key, argc);
  1203. argc++;
  1204. });
  1205. }
  1206. compile_node(gbuf, tbuf, ctx, lstk, lbl, node->a);
  1207. CTXPOP();
  1208. CTXPOP();
  1209. LBPOP();
  1210. buffer_fmt(tbuf, "return state->nil;\n");
  1211. buffer_fmt(tbuf, "}\n");
  1212. buffer_appendb(gbuf, tbuf);
  1213. tbuf = buffer_new();
  1214. 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);
  1215. compile_table(gbuf, tbuf, ctx, lstk, lbl, node->h2);
  1216. buffer_fmt(tbuf, ")");
  1217. if (node->tag == N_FUNCEXPR) {
  1218. buffer_appendb(buf, tbuf);
  1219. return;
  1220. }
  1221. EMIT("qi_set(state, false, \"%s\", ", node->t->text);
  1222. buffer_appendb(buf, tbuf);
  1223. EMIT(");");
  1224. }
  1225. void compile_block(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, list_t *block) {
  1226. for (size_t i = 0; i < block->length; i++) {
  1227. node_t *n = block->data[i];
  1228. if (n->tag == N_FUNCDEF) {
  1229. compile_func(gbuf, buf, ctx, lstk, lbl, n);
  1230. EMIT("\n");
  1231. } else if (n->tag == N_VAR || n->tag == N_LET) {
  1232. table_iterate(n->h, {
  1233. EMIT("qi_%s(state, \"%s\", ", n->tag == N_LET? "decl_const": "decl", entry.key);
  1234. if (entry.value)
  1235. compile_node(gbuf, buf, ctx, lstk, lbl, entry.value);
  1236. else EMIT("state->nil");
  1237. EMIT(");\n");
  1238. });
  1239. }
  1240. }
  1241. for (size_t i = 0; i < block->length; i++) {
  1242. compile_node(gbuf, buf, ctx, lstk, lbl, block->data[i]);
  1243. EMIT("\n");
  1244. }
  1245. }
  1246. const char *STD[][2] = {
  1247. {"std", "func head(l): return l[0]"},
  1248. {NULL, NULL}
  1249. };
  1250. void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl);
  1251. void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, node_t *node) {
  1252. switch (node->tag) {
  1253. case N_PROGRAM:
  1254. compile_block(gbuf, buf, ctx, lstk, lbl, node->l);
  1255. break;
  1256. case N_EXPRSTMT:
  1257. EMIT("(void)(");
  1258. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1259. EMIT(");");
  1260. break;
  1261. case N_BLOCK:
  1262. LBPUSH();
  1263. CTXPUSH("scope");
  1264. EMIT("qi_new_scope(state);\n");
  1265. compile_block(gbuf, buf, ctx, lstk, lbl, node->l);
  1266. EMIT("qi_old_scope(state);");
  1267. CTXPOP();
  1268. LBPOP();
  1269. break;
  1270. case N_LITERAL:
  1271. switch (node->t->tag) {
  1272. case T_NUMBER:
  1273. EMIT("qi_make_number(state, %s)", node->t->text);
  1274. break;
  1275. case T_STRING:
  1276. if (!*(node->t->text)) {
  1277. EMIT("state->empty_string");
  1278. } else {
  1279. EMIT("qi_make_string(state, \"%s\")", node->t->text);
  1280. }
  1281. break;
  1282. case T_NAME:
  1283. EMIT("qi_get(state, \"%s\")", node->t->text);
  1284. break;
  1285. default:
  1286. COMPILE_ERROR("not yet implemented");
  1287. }
  1288. break;
  1289. case N_LIST:
  1290. EMIT("qi_make_list(state, ");
  1291. compile_list(gbuf, buf, ctx, lstk, lbl, node->l);
  1292. EMIT(")");
  1293. break;
  1294. case N_TUPLE:
  1295. EMIT("qi_make_tuple(state, ");
  1296. compile_list(gbuf, buf, ctx, lstk, lbl, node->l);
  1297. EMIT(")");
  1298. break;
  1299. case N_NILTUPLE: EMIT("state->empty_tuple"); break;
  1300. case N_TABLE:
  1301. EMIT("qi_make_table(state, ");
  1302. compile_table(gbuf, buf, ctx, lstk, lbl, node->h);
  1303. EMIT(")");
  1304. break;
  1305. case N_CALL:
  1306. EMIT("qi_call(state, ");
  1307. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1308. EMIT(", ");
  1309. compile_list(gbuf, buf, ctx, lstk, lbl, node->l);
  1310. EMIT(")");
  1311. break;
  1312. case N_MEMBER:
  1313. EMIT("qi_index(state, ");
  1314. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1315. EMIT(", qi_make_string(state, \"%s\"))", node->t->text);
  1316. break;
  1317. case N_INDEX:
  1318. EMIT("qi_index(state, ");
  1319. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1320. EMIT(", ");
  1321. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1322. EMIT(")");
  1323. break;
  1324. case N_ASSIGN: ASSIGN(node->a, compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1325. case N_ASSIGN_ADD: COMPASSIGN(node->a, "add", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1326. case N_INC:
  1327. COMPASSIGN(node->a, "add", EMIT("state->one"));
  1328. break;
  1329. case N_DEC:
  1330. COMPASSIGN(node->a, "sub", EMIT("state->one"));
  1331. break;
  1332. case N_VAR: break;
  1333. case N_IF:
  1334. EMIT("if (_qi_truthy(state, ");
  1335. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1336. EMIT(")) {\n");
  1337. CTXPUSH("scope");
  1338. EMIT("qi_new_scope(state);\n");
  1339. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1340. EMIT("qi_old_scope(state);\n");
  1341. CTXPOP();
  1342. if (node->c) {
  1343. EMIT("} else {\n");
  1344. CTXPUSH("scope");
  1345. EMIT("qi_new_scope(state);\n");
  1346. compile_node(gbuf, buf, ctx, lstk, lbl, node->c);
  1347. EMIT("qi_old_scope(state);\n");
  1348. CTXPOP();
  1349. }
  1350. EMIT("}");
  1351. break;
  1352. case N_FOR: {
  1353. NEWGID();
  1354. CTXPUSH("scope");
  1355. EMIT("qi_new_scope(state);\n");
  1356. if (!node->a) {
  1357. EMIT("for (;;) {\n");
  1358. } else if (node->a && !node->b) {
  1359. EMIT("while (_qi_truthy(state, ");
  1360. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1361. EMIT(")) {\n");
  1362. } else {
  1363. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1364. EMIT("while (_qi_truthy(state, ");
  1365. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1366. EMIT(")) {\n");
  1367. }
  1368. LPUSH(gid);
  1369. CTXPUSH("for");
  1370. compile_node(gbuf, buf, ctx, lstk, lbl, node->d);
  1371. CTXPOP();
  1372. LPOP();
  1373. EMIT("__continue%d:;\n", gid);
  1374. if (node->c)
  1375. compile_node(gbuf, buf, ctx, lstk, lbl, node->c);
  1376. EMIT("}\n");
  1377. EMIT("__break%d:;\n", gid);
  1378. EMIT("qi_old_scope(state);\n");
  1379. CTXPOP();
  1380. } break;
  1381. case N_BREAK:
  1382. if (!INCTX("for"))
  1383. COMPILE_ERROR("break outside of a loop");
  1384. EMIT("goto __break%d;", LID);
  1385. break;
  1386. case N_CONTINUE:
  1387. if (!INCTX("for"))
  1388. COMPILE_ERROR("continue outside of a loop");
  1389. EMIT("goto __continue%d;", LID);
  1390. break;
  1391. case N_DEFER: {
  1392. NEWGID();
  1393. buffer_t *tbuf = buffer_new();
  1394. buffer_fmt(tbuf, "void __defer%d(qi_state_t *state) {\n", gid);
  1395. LBPUSH();
  1396. CTXPUSH("gap");
  1397. compile_node(gbuf, tbuf, ctx, lstk, lbl, node->a);
  1398. CTXPOP();
  1399. LBPOP();
  1400. buffer_fmt(tbuf, "\n");
  1401. buffer_fmt(tbuf, "}\n");
  1402. buffer_appendb(gbuf, tbuf);
  1403. EMIT("qi_add_defer(state, -1, __defer%d);", gid);
  1404. } break;
  1405. case N_RETURN:
  1406. if (!INCTX("func"))
  1407. COMPILE_ERROR("return outside of a function");
  1408. for (size_t i = 0; i < SCOPESK; i++)
  1409. EMIT("qi_old_scope(state);\n");
  1410. for (size_t i = 0; i < TRAPSK; i++)
  1411. EMIT("qi_unset_trap(state, trap);\n");
  1412. EMIT("return ");
  1413. if (node->a)
  1414. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1415. else EMIT("state->nil");
  1416. EMIT(";");
  1417. break;
  1418. case N_FUNCDEF: break;
  1419. case N_PASS: break;
  1420. case N_TRY:
  1421. CTXPUSH("scope");
  1422. EMIT("qi_new_scope(state);\n");
  1423. EMIT("qi_try(state, {\n");
  1424. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1425. EMIT("}, {\n");
  1426. if (node->t)
  1427. EMIT("qi_decl(state, \"%s\", trap->value);\n", node->t->text);
  1428. CTXPUSH("trap");
  1429. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1430. CTXPOP();
  1431. EMIT("}, NULL);\n");
  1432. EMIT("qi_new_scope(state);");
  1433. CTXPOP();
  1434. break;
  1435. case N_THROW:
  1436. EMIT("qi_throw(state, ");
  1437. if (node->a)
  1438. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1439. else {
  1440. EMIT("state->nil");
  1441. }
  1442. EMIT(");");
  1443. break;
  1444. case N_LABEL: {
  1445. char *label = node->t->text;
  1446. table_iterate((table_t *)list_index(lbl, -1), {
  1447. if (strcmp(entry.key, label) == 0) {
  1448. COMPILE_ERROR("duplicated label: '%s'", label);
  1449. }
  1450. });
  1451. NEWGID();
  1452. EMIT("__label%d:;", gid);
  1453. size_t *n = malloc(sizeof(size_t));
  1454. memcpy(n, &gid, sizeof(size_t));
  1455. table_set(list_index(lbl, -1), label, n);
  1456. } break;
  1457. case N_GOTO: {
  1458. ssize_t gid = -1;
  1459. char *label = node->t->text;
  1460. table_iterate((table_t *)list_index(lbl, -1), {
  1461. if (strcmp(entry.key, label) == 0) {
  1462. gid = *(size_t *)entry.value;
  1463. break;
  1464. }
  1465. });
  1466. if (gid < 0)
  1467. COMPILE_ERROR("undefined label: '%s'", label);
  1468. EMIT("goto __label%d;", gid);
  1469. } break;
  1470. case N_REQUIRE: {
  1471. char *source = NULL;
  1472. char *path = node->t->text;
  1473. for (size_t i = 0; STD[i][0]; i++) {
  1474. if (strcmp(path, STD[i][0]) == 0) {
  1475. source = (char *)STD[i][1];
  1476. break;
  1477. }
  1478. }
  1479. if (is_required(path))
  1480. break;
  1481. if (!source) {
  1482. FILE *fd = fopen(path, "rb");
  1483. if (!fd)
  1484. COMPILE_ERROR("'%s' is not a valid file path or a builtin library name", path);
  1485. buffer_t *fbuf = buffer_new();
  1486. for (;;) {
  1487. char line[512];
  1488. if (!fgets(line, sizeof(line), fd))
  1489. break;
  1490. buffer_appends(fbuf, line);
  1491. }
  1492. source = buffer_read(fbuf);
  1493. path = realpath(path, NULL);
  1494. }
  1495. list_t *pair = list_new();
  1496. list_push(pair, path);
  1497. list_push(pair, source);
  1498. list_push(FILES, pair);
  1499. compile_into(source, gbuf, buf, ctx, lstk, lbl);
  1500. list_pop(FILES);
  1501. list_push(REQUIRED, path);
  1502. } break;
  1503. case N_IFEXPR:
  1504. EMIT("(_qi_truthy(state, ");
  1505. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1506. EMIT(")? ");
  1507. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1508. EMIT(": ");
  1509. compile_node(gbuf, buf, ctx, lstk, lbl, node->c);
  1510. EMIT(")");
  1511. break;
  1512. case N_FUNCEXPR:
  1513. compile_func(gbuf, buf, ctx, lstk, lbl, node);
  1514. break;
  1515. case N_EQUALS:
  1516. BINOP("equals");
  1517. break;
  1518. case N_LT:
  1519. BINOP("lt");
  1520. break;
  1521. case N_GT:
  1522. BINOP("gt");
  1523. break;
  1524. case N_ADD:
  1525. BINOP("add");
  1526. break;
  1527. case N_SUB:
  1528. BINOP("sub");
  1529. break;
  1530. case N_MUL:
  1531. BINOP("mul");
  1532. break;
  1533. case N_DIV:
  1534. BINOP("div");
  1535. break;
  1536. case N_IDIV:
  1537. BINOP("idiv");
  1538. break;
  1539. case N_POW:
  1540. BINOP("pow");
  1541. break;
  1542. case N_NEGATE:
  1543. UNOP("negate");
  1544. break;
  1545. default:
  1546. COMPILE_ERROR("not yet implemented");
  1547. }
  1548. }
  1549. void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl) {
  1550. node_t *n = parse(source);
  1551. compile_node(gbuf, buf, ctx, lstk, lbl, n);
  1552. }
  1553. char *compile(char *source) {
  1554. list_t *ctx = list_new();
  1555. stack_t *lstk = stack_new();
  1556. list_t *lbl = list_new();
  1557. LBPUSH();
  1558. buffer_t *gbuf = buffer_new();
  1559. buffer_appends(gbuf, "#include <qirt.h>\n");
  1560. buffer_t *buf = buffer_new();
  1561. compile_into(source, gbuf, buf, ctx, lstk, lbl);
  1562. buffer_t *rbuf = buffer_new();
  1563. buffer_appendb(rbuf, gbuf);
  1564. buffer_appends(rbuf, "int main(int argc, char **argv) {\n");
  1565. buffer_appends(rbuf, "qi_state_t *state;\n");
  1566. buffer_appends(rbuf, "qi_state_init(&state);\n");
  1567. buffer_appendb(rbuf, buf);
  1568. buffer_appends(rbuf, "qi_old_scope(state);\n");
  1569. buffer_appends(rbuf, "qi_finalize();\n");
  1570. buffer_appends(rbuf, "return 0;\n");
  1571. buffer_appends(rbuf, "}\n");
  1572. return buffer_read(rbuf);
  1573. }
  1574. char *compile_file(char *filename, FILE *fd) {
  1575. buffer_t *buf = buffer_new();
  1576. for (;;) {
  1577. char line[512];
  1578. if (!fgets(line, sizeof(line), fd))
  1579. break;
  1580. buffer_appends(buf, line);
  1581. }
  1582. char *source = buffer_read(buf);
  1583. list_t *pair = list_new();
  1584. list_push(pair, filename);
  1585. list_push(pair, source);
  1586. list_push(FILES, pair);
  1587. char *out = compile(source);
  1588. list_pop(FILES);
  1589. return out;
  1590. }
  1591. int main(int argc, char **argv) {
  1592. FILES = list_new();
  1593. REQUIRED = list_new();
  1594. char *out = compile_file("<stdin>", stdin);
  1595. fwrite(out, sizeof(char), strlen(out), stdout);
  1596. return 0;
  1597. }