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