qic.c 39 KB

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