qic.c 41 KB

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