qic.c 40 KB

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