qic.c 38 KB

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