qic.c 49 KB

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