qic.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  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_LET,
  202. T_IF,
  203. T_ELSE,
  204. T_ELIF,
  205. T_FOR,
  206. T_OF,
  207. T_BREAK,
  208. T_CONTINUE,
  209. T_PASS,
  210. T_FUNC,
  211. T_USE,
  212. T_RETURN,
  213. T_DEFER,
  214. T_REQUIRE,
  215. T_TRY,
  216. T_CATCH,
  217. T_THROW,
  218. T_GOTO,
  219. T_IS,
  220. T_IN,
  221. T_LPAR,
  222. T_RPAR,
  223. T_LSB,
  224. T_RSB,
  225. T_LCB,
  226. T_RCB,
  227. T_EQUALS,
  228. T_NOTEQUALS,
  229. T_PLUSASSIGN,
  230. T_MINUSASSIGN,
  231. T_SLASHASSIGN,
  232. T_STARASSIGN,
  233. T_SLASHSLASHASSIGN,
  234. T_PERCENTASSIGN,
  235. T_BARBAR,
  236. T_ANDAND,
  237. T_STARSTAR,
  238. T_PLUSPLUS,
  239. T_MINUSMINUS,
  240. T_SLASHSLASH,
  241. T_PLUS,
  242. T_MINUS,
  243. T_QM,
  244. T_COLON,
  245. T_BAR,
  246. T_AND,
  247. T_LT,
  248. T_LTLT,
  249. T_GT,
  250. T_GTGT,
  251. T_LE,
  252. T_GE,
  253. T_STAR,
  254. T_SLASH,
  255. T_PERCENT,
  256. T_COMMA,
  257. T_DOT,
  258. T_BANG,
  259. T_RAISE,
  260. T_TILDE,
  261. T_INLINE,
  262. T_ASSIGN,
  263. T_SEMI
  264. } tag;
  265. char *text;
  266. size_t fi;
  267. size_t pos;
  268. } token_t;
  269. token_t *token(int tag, char *text) {
  270. token_t *tok = malloc(sizeof(token_t));
  271. tok->tag = tag;
  272. tok->text = text;
  273. return tok;
  274. }
  275. #define TK(tk) (token(T_##tk, NULL))
  276. #define WS() while (source[*pos] == ' ' || source[*pos] == '\t' || source[*pos] == '\n' || source[*pos] == '\r') { (*pos)++; }
  277. void consume_ignored(char *source, size_t *pos) {
  278. WS();
  279. while (source[*pos] == '#') {
  280. (*pos)++;
  281. for (;;) {
  282. if (!source[*pos])
  283. break;
  284. if (source[*pos] == '\n') {
  285. (*pos)++;
  286. break;
  287. }
  288. (*pos)++;
  289. }
  290. WS();
  291. }
  292. }
  293. list_t *FILES;
  294. list_t *REQUIRED;
  295. int is_required(char *path) {
  296. for (size_t i = 0; i < REQUIRED->length; i++)
  297. if (strcmp(REQUIRED->data[i], path) == 0)
  298. return 1;
  299. return 0;
  300. }
  301. void traverse(char *source, size_t pos, size_t *line, size_t *col) {
  302. *line = 1;
  303. *col = 1;
  304. for (size_t i = 0; i < pos; i++) {
  305. if (source[i] == '\n') {
  306. (*line)++;
  307. (*col) = 1;
  308. } else (*col)++;
  309. }
  310. }
  311. void format_error(char *filename, char *source, size_t pos, char *fmt, ...) {
  312. size_t line, col;
  313. traverse(source, pos, &line, &col);
  314. va_list args;
  315. va_start(args, fmt);
  316. fprintf(stderr, "%s (%zu:%zu): ", filename, line, col);
  317. vfprintf(stderr, fmt, args);
  318. fputc('\n', stderr);
  319. va_end(args);
  320. }
  321. #define GETFNAME(fi) ((char *)((list_t *)list_index(FILES, fi))->data[0])
  322. #define GETSRC(fi) ((char *)((list_t *)list_index(FILES, fi))->data[1])
  323. #define LEX_ERROR(fmt, ...) { format_error(GETFNAME(-1), source, *pos, fmt, ##__VA_ARGS__); exit(1); }
  324. token_t *next_token(char *source, size_t *pos) {
  325. if (!source[*pos])
  326. return token(T_EOF, NULL);
  327. if (source[*pos] == '"' || source[*pos] == '\'') {
  328. char term = source[(*pos)++];
  329. buffer_t *text = buffer_new();
  330. while (source[*pos] != term) {
  331. if (!source[*pos])
  332. LEX_ERROR("unterminated string literal");
  333. char c = source[(*pos)++];
  334. if (c == '\\') {
  335. char nc = source[(*pos)++];
  336. if (!nc)
  337. continue;
  338. switch (nc) {
  339. case 'n':
  340. buffer_appends(text, "\\n");
  341. break;
  342. case 't':
  343. buffer_appends(text, "\\t");
  344. break;
  345. case 'r':
  346. buffer_appends(text, "\\r");
  347. break;
  348. case 'b':
  349. buffer_appends(text, "\\b");
  350. break;
  351. case 'e':
  352. buffer_appends(text, "\\e");
  353. break;
  354. case 's':
  355. buffer_appends(text, " ");
  356. break;
  357. case '"':
  358. buffer_appends(text, "\\\"");
  359. break;
  360. case '\\':
  361. buffer_appends(text, "\\\\");
  362. break;
  363. default:
  364. buffer_append(text, nc);
  365. break;
  366. }
  367. continue;
  368. }
  369. if (c == '"' || c == '\\')
  370. buffer_append(text, '\\');
  371. buffer_append(text, c);
  372. }
  373. (*pos)++;
  374. return token(T_STRING, buffer_read(text));
  375. } else if (isdigit(source[*pos])) {
  376. buffer_t *number = buffer_new();
  377. int dot = 0;
  378. int sub = 0;
  379. do {
  380. buffer_append(number, source[(*pos)++]);
  381. if (!dot && source[*pos] == '.') {
  382. buffer_append(number, source[(*pos)++]);
  383. if (!isdigit(source[*pos]))
  384. LEX_ERROR("illegal number literal (missing part after floating point)");
  385. dot = 1;
  386. } else if (!sub && source[*pos] == '.') {
  387. (*pos)++;
  388. sub = 1;
  389. } else if (sub) sub = 0;
  390. } while (isdigit(source[*pos]));
  391. return token(T_NUMBER, buffer_read(number));
  392. } else if (isalpha(source[*pos]) || source[*pos] == '_') {
  393. buffer_t *text = buffer_new();
  394. do {
  395. buffer_append(text, source[(*pos)++]);
  396. } while (isalpha(source[*pos]) || source[*pos] == '_' || isdigit(source[*pos]));
  397. char *name = buffer_read(text);
  398. if (strcmp(name, "var") == 0)
  399. return TK(VAR);
  400. else if (strcmp(name, "let") == 0)
  401. return TK(LET);
  402. else if (strcmp(name, "if") == 0)
  403. return TK(IF);
  404. else if (strcmp(name, "else") == 0)
  405. return TK(ELSE);
  406. else if (strcmp(name, "elif") == 0)
  407. return TK(ELIF);
  408. else if (strcmp(name, "for") == 0)
  409. return TK(FOR);
  410. else if (strcmp(name, "break") == 0)
  411. return TK(BREAK);
  412. else if (strcmp(name, "continue") == 0)
  413. return TK(CONTINUE);
  414. else if (strcmp(name, "func") == 0)
  415. return TK(FUNC);
  416. else if (strcmp(name, "use") == 0)
  417. return TK(USE);
  418. else if (strcmp(name, "return") == 0)
  419. return TK(RETURN);
  420. else if (strcmp(name, "defer") == 0)
  421. return TK(DEFER);
  422. else if (strcmp(name, "pass") == 0)
  423. return TK(PASS);
  424. else if (strcmp(name, "require") == 0)
  425. return TK(REQUIRE);
  426. else if (strcmp(name, "try") == 0)
  427. return TK(TRY);
  428. else if (strcmp(name, "catch") == 0)
  429. return TK(CATCH);
  430. else if (strcmp(name, "throw") == 0)
  431. return TK(THROW);
  432. else if (strcmp(name, "goto") == 0)
  433. return TK(GOTO);
  434. else if (strcmp(name, "is") == 0)
  435. return TK(IS);
  436. else if (strcmp(name, "in") == 0)
  437. return TK(IN);
  438. else if (strcmp(name, "of") == 0)
  439. return TK(OF);
  440. else if (strcmp(name, "inline") == 0)
  441. return TK(INLINE);
  442. return token(T_NAME, name);
  443. } else if (strncmp(&source[*pos], "==", 2) == 0 && ++(*pos) && ++(*pos))
  444. return TK(EQUALS);
  445. else if (strncmp(&source[*pos], "!=", 2) == 0 && ++(*pos) && ++(*pos))
  446. return TK(NOTEQUALS);
  447. else if (strncmp(&source[*pos], "+=", 2) == 0 && ++(*pos) && ++(*pos))
  448. return TK(PLUSASSIGN);
  449. else if (strncmp(&source[*pos], "-=", 2) == 0 && ++(*pos) && ++(*pos))
  450. return TK(MINUSASSIGN);
  451. else if (strncmp(&source[*pos], "*=", 2) == 0 && ++(*pos) && ++(*pos))
  452. return TK(STARASSIGN);
  453. else if (strncmp(&source[*pos], "/=", 2) == 0 && ++(*pos) && ++(*pos))
  454. return TK(SLASHASSIGN);
  455. else if (strncmp(&source[*pos], "//=", 3) == 0 && ++(*pos) && ++(*pos) && ++(*pos))
  456. return TK(SLASHSLASHASSIGN);
  457. else if (strncmp(&source[*pos], "%=", 2) == 0 && ++(*pos) && ++(*pos))
  458. return TK(PERCENTASSIGN);
  459. else if (strncmp(&source[*pos], "||", 2) == 0 && ++(*pos) && ++(*pos))
  460. return TK(BARBAR);
  461. else if (strncmp(&source[*pos], "&&", 2) == 0 && ++(*pos) && ++(*pos))
  462. return TK(ANDAND);
  463. else if (strncmp(&source[*pos], "++", 2) == 0 && ++(*pos) && ++(*pos))
  464. return TK(PLUSPLUS);
  465. else if (strncmp(&source[*pos], "--", 2) == 0 && ++(*pos) && ++(*pos))
  466. return TK(MINUSMINUS);
  467. else if (strncmp(&source[*pos], "//", 2) == 0 && ++(*pos) && ++(*pos))
  468. return TK(SLASHSLASH);
  469. else if (strncmp(&source[*pos], "**", 2) == 0 && ++(*pos) && ++(*pos))
  470. return TK(STARSTAR);
  471. else if (strncmp(&source[*pos], "<<", 2) == 0 && ++(*pos) && ++(*pos))
  472. return TK(LTLT);
  473. else if (strncmp(&source[*pos], ">>", 2) == 0 && ++(*pos) && ++(*pos))
  474. return TK(GTGT);
  475. else if (strncmp(&source[*pos], "<=", 2) == 0 && ++(*pos) && ++(*pos))
  476. return TK(LE);
  477. else if (strncmp(&source[*pos], ">=", 2) == 0 && ++(*pos) && ++(*pos))
  478. return TK(GE);
  479. else if (source[*pos] == '(' && ++(*pos))
  480. return TK(LPAR);
  481. else if (source[*pos] == ')' && ++(*pos))
  482. return TK(RPAR);
  483. else if (source[*pos] == '[' && ++(*pos))
  484. return TK(LSB);
  485. else if (source[*pos] == ']' && ++(*pos))
  486. return TK(RSB);
  487. else if (source[*pos] == '{' && ++(*pos))
  488. return TK(LCB);
  489. else if (source[*pos] == '}' && ++(*pos))
  490. return TK(RCB);
  491. else if (source[*pos] == '+' && ++(*pos))
  492. return TK(PLUS);
  493. else if (source[*pos] == '-' && ++(*pos))
  494. return TK(MINUS);
  495. else if (source[*pos] == '*' && ++(*pos))
  496. return TK(STAR);
  497. else if (source[*pos] == '/' && ++(*pos))
  498. return TK(SLASH);
  499. else if (source[*pos] == '%' && ++(*pos))
  500. return TK(PERCENT);
  501. else if (source[*pos] == '?' && ++(*pos))
  502. return TK(QM);
  503. else if (source[*pos] == ':' && ++(*pos))
  504. return TK(COLON);
  505. else if (source[*pos] == '=' && ++(*pos))
  506. return TK(ASSIGN);
  507. else if (source[*pos] == ';' && ++(*pos))
  508. return TK(SEMI);
  509. else if (source[*pos] == ',' && ++(*pos))
  510. return TK(COMMA);
  511. else if (source[*pos] == '.' && ++(*pos))
  512. return TK(DOT);
  513. else if (source[*pos] == '<' && ++(*pos))
  514. return TK(LT);
  515. else if (source[*pos] == '>' && ++(*pos))
  516. return TK(GT);
  517. else if (source[*pos] == '!' && ++(*pos))
  518. return TK(BANG);
  519. else if (source[*pos] == '|' && ++(*pos))
  520. return TK(BAR);
  521. else if (source[*pos] == '&' && ++(*pos))
  522. return TK(AND);
  523. else if (source[*pos] == '^' && ++(*pos))
  524. return TK(RAISE);
  525. else if (source[*pos] == '~' && ++(*pos))
  526. return TK(TILDE);
  527. LEX_ERROR("unexpected input")
  528. }
  529. list_t *tokenize(char *source) {
  530. size_t pos = 0;
  531. list_t *toks = list_new();
  532. do {
  533. consume_ignored(source, &pos);
  534. size_t tok_pos = pos;
  535. token_t *tok = next_token(source, &pos);
  536. tok->fi = FILES->length-1;
  537. tok->pos = tok_pos;
  538. list_push(toks, tok);
  539. if (tok->tag == T_EOF)
  540. break;
  541. } while (1);
  542. return toks;
  543. }
  544. struct _node_t {
  545. enum {
  546. N_PROGRAM,
  547. N_EXPRSTMT,
  548. N_BLOCK,
  549. N_NOT,
  550. N_NEGATE,
  551. N_BNOT,
  552. N_LITERAL,
  553. N_LIST,
  554. N_TUPLE,
  555. N_NILTUPLE,
  556. N_TABLE,
  557. N_CALL,
  558. N_MEMBER,
  559. N_INDEX,
  560. N_ADD,
  561. N_SUB,
  562. N_MUL,
  563. N_DIV,
  564. N_IDIV,
  565. N_MOD,
  566. N_POW,
  567. N_SHL,
  568. N_SHR,
  569. N_XOR,
  570. N_BOR,
  571. N_BAND,
  572. N_ASSIGN,
  573. N_ASSIGN_ADD,
  574. N_ASSIGN_SUB,
  575. N_ASSIGN_MUL,
  576. N_ASSIGN_DIV,
  577. N_ASSIGN_IDIV,
  578. N_ASSIGN_MOD,
  579. N_ASSIGN_POW,
  580. N_EQUALS,
  581. N_NOTEQUALS,
  582. N_IS,
  583. N_IN,
  584. N_NOTIS,
  585. N_NOTIN,
  586. N_LT,
  587. N_GT,
  588. N_LE,
  589. N_GE,
  590. N_INC,
  591. N_DEC,
  592. N_VAR,
  593. N_LET,
  594. N_IF,
  595. N_FOR,
  596. N_FOROF,
  597. N_BREAK,
  598. N_CONTINUE,
  599. N_FUNCDEF,
  600. N_RETURN,
  601. N_DEFER,
  602. N_PASS,
  603. N_REQUIRE,
  604. N_TRY,
  605. N_THROW,
  606. N_LABEL,
  607. N_GOTO,
  608. N_INLINE,
  609. N_IFEXPR,
  610. N_FUNCEXPR,
  611. N_LOGOR,
  612. N_LOGAND,
  613. } tag;
  614. struct _node_t *a;
  615. struct _node_t *b;
  616. struct _node_t *c;
  617. struct _node_t *d;
  618. list_t *l;
  619. table_t *h;
  620. table_t *h2;
  621. token_t *t;
  622. size_t fi;
  623. size_t pos;
  624. };
  625. typedef struct _node_t node_t;
  626. node_t *node_pos(node_t *node, size_t fi, size_t pos) {
  627. node->fi = fi;
  628. node->pos = pos;
  629. return node;
  630. }
  631. node_t *nodet(int tag, token_t *t) {
  632. node_t *node = malloc(sizeof(node_t));
  633. node->tag = tag;
  634. node->t = t;
  635. return node;
  636. }
  637. #define NODET(n, a) (node_pos(nodet(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  638. node_t *nodel(int tag, list_t *l) {
  639. node_t *node = malloc(sizeof(node_t));
  640. node->tag = tag;
  641. node->l = l;
  642. return node;
  643. }
  644. #define NODEL(n, a) (node_pos(nodel(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  645. node_t *nodeh(int tag, table_t *h) {
  646. node_t *node = malloc(sizeof(node_t));
  647. node->tag = tag;
  648. node->h = h;
  649. return node;
  650. }
  651. #define NODEH(n, a) (node_pos(nodeh(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  652. node_t *node0(int tag) {
  653. node_t *node = malloc(sizeof(node_t));
  654. node->tag = tag;
  655. return node;
  656. }
  657. #define NODE0(n) (node_pos(node0(N_##n), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  658. node_t *node1(int tag, node_t *a) {
  659. node_t *node = malloc(sizeof(node_t));
  660. node->tag = tag;
  661. node->a = a;
  662. return node;
  663. }
  664. #define NODE1(n, a) (node_pos(node1(N_##n, (a)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  665. node_t *node1l(int tag, node_t *a, list_t *l) {
  666. node_t *node = malloc(sizeof(node_t));
  667. node->tag = tag;
  668. node->a = a;
  669. node->l = l;
  670. return node;
  671. }
  672. #define NODE1l(n, a, l) (node_pos(node1l(N_##n, (a), (l)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  673. node_t *node1t(int tag, node_t *a, token_t *t) {
  674. node_t *node = malloc(sizeof(node_t));
  675. node->tag = tag;
  676. node->a = a;
  677. node->t = t;
  678. return node;
  679. }
  680. #define NODE1t(n, a, t) (node_pos(node1t(N_##n, (a), (t)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  681. node_t *node2(int tag, node_t *a, node_t *b) {
  682. node_t *node = malloc(sizeof(node_t));
  683. node->tag = tag;
  684. node->a = a;
  685. node->b = b;
  686. return node;
  687. }
  688. #define NODE2(n, a, b) (node_pos(node2(N_##n, (a), (b)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  689. node_t *node2t(int tag, node_t *a, node_t *b, token_t *t) {
  690. node_t *node = malloc(sizeof(node_t));
  691. node->tag = tag;
  692. node->a = a;
  693. node->b = b;
  694. node->t = t;
  695. return node;
  696. }
  697. #define NODE2t(n, a, b, c) (node_pos(node2t(N_##n, (a), (b), (c)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  698. node_t *node3(int tag, node_t *a, node_t *b, node_t *c) {
  699. node_t *node = malloc(sizeof(node_t));
  700. node->tag = tag;
  701. node->a = a;
  702. node->b = b;
  703. node->c = c;
  704. return node;
  705. }
  706. #define NODE3(n, a, b, c) (node_pos(node3(N_##n, (a), (b), (c)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  707. node_t *node4(int tag, node_t *a, node_t *b, node_t *c, node_t *d) {
  708. node_t *node = malloc(sizeof(node_t));
  709. node->tag = tag;
  710. node->a = a;
  711. node->b = b;
  712. node->c = c;
  713. node->d = d;
  714. return node;
  715. }
  716. #define NODE4(n, a, b, c, d) (node_pos(node4(N_##n, (a), (b), (c), (d)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  717. node_t *nodef(int tag, token_t *name, table_t *params, table_t *captured, node_t *body) {
  718. node_t *node = malloc(sizeof(node_t));
  719. node->tag = tag;
  720. node->t = name;
  721. node->h = params;
  722. node->h2 = captured;
  723. node->a = body;
  724. return node;
  725. }
  726. #define NODEF(n, a, b, c, d) (node_pos(nodef(N_##n, (a), (b), (c), (d)), ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->fi, ((token_t *)tokens->data[(*pos)>0?(*pos)-1:(*pos)])->pos))
  727. #define AT(tk) (*pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == T_##tk)
  728. #define ATP(tk, p) ((*pos)+p < tokens->length && ((token_t *)tokens->data[(*pos)+p])->tag == T_##tk)
  729. #define MATCH(tk) (AT(tk) && ++(*pos))
  730. #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); }
  731. #define EXPECT(tk, s) { if (!MATCH(tk)) PARSE_ERROR("expected %s", (s)); }
  732. node_t *parse_expr(list_t *tokens, size_t *pos);
  733. list_t *parse_sequence(list_t *tokens, size_t *pos, int term) {
  734. list_t *seq = list_new();
  735. do {
  736. if (term != -1 && *pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == term)
  737. break;
  738. list_push(seq, parse_expr(tokens, pos));
  739. } while (MATCH(COMMA));
  740. return seq;
  741. }
  742. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr);
  743. node_t *parse_primary(list_t *tokens, size_t *pos) {
  744. if (MATCH(FUNC))
  745. return parse_func(tokens, pos, 1);
  746. else if (MATCH(LPAR)) {
  747. if (MATCH(RPAR))
  748. return NODE0(NILTUPLE);
  749. node_t *a = parse_expr(tokens, pos);
  750. if (MATCH(COMMA)) {
  751. list_t *l = list_new();
  752. list_push(l, a);
  753. if (!AT(RPAR))
  754. do {
  755. node_t *n = parse_expr(tokens, pos);
  756. list_push(l, n);
  757. } while (MATCH(COMMA));
  758. a = NODEL(TUPLE, l);
  759. }
  760. EXPECT(RPAR, ")");
  761. return a;
  762. } else if (MATCH(LSB)) {
  763. list_t *a = parse_sequence(tokens, pos, T_RSB);
  764. EXPECT(RSB, "]");
  765. return NODEL(LIST, a);
  766. } else if (MATCH(LCB)) {
  767. table_t *table = table_new();
  768. do {
  769. if (AT(RCB))
  770. break;
  771. if (!AT(NAME) && !AT(STRING))
  772. PARSE_ERROR("expected identifier or string");
  773. char *key = ((token_t *)tokens->data[(*pos)++])->text;
  774. EXPECT(COLON, ":");
  775. node_t *val = parse_expr(tokens, pos);
  776. table_set(table, key, val);
  777. } while (MATCH(COMMA));
  778. EXPECT(RCB, "}");
  779. return NODEH(TABLE, table);
  780. } else if (MATCH(NUMBER) || MATCH(STRING) || MATCH(NAME))
  781. return NODET(LITERAL, tokens->data[(*pos)-1]);
  782. PARSE_ERROR("expected expression");
  783. return NULL;
  784. }
  785. size_t get_lineno(token_t *tok) {
  786. size_t line, col;
  787. traverse(GETSRC(tok->fi), tok->pos, &line, &col);
  788. return line;
  789. }
  790. #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)])))
  791. node_t *parse_call(list_t *tokens, size_t *pos) {
  792. node_t *a = parse_primary(tokens, pos);
  793. do {
  794. if (!CLIFF && MATCH(LPAR)) {
  795. list_t *b = NULL;
  796. if (!AT(RPAR))
  797. b = parse_sequence(tokens, pos, -1);
  798. EXPECT(RPAR, ")");
  799. a = NODE1l(CALL, a, b);
  800. continue;
  801. } else if (!CLIFF && MATCH(LSB)) {
  802. node_t *b = parse_expr(tokens, pos);
  803. EXPECT(RSB, "]");
  804. a = NODE2(INDEX, a, b);
  805. continue;
  806. } else if (!CLIFF && MATCH(DOT)) {
  807. if (!AT(NAME))
  808. PARSE_ERROR("expected identifier after .");
  809. a = NODE1t(MEMBER, a, tokens->data[(*pos)++]);
  810. continue;
  811. }
  812. break;
  813. } while (1);
  814. return a;
  815. }
  816. node_t *parse_postfix(list_t *tokens, size_t *pos) {
  817. node_t *a = parse_call(tokens, pos);
  818. if (CLIFF)
  819. return a;
  820. if (MATCH(PLUSPLUS))
  821. return NODE1(INC, a);
  822. else if (MATCH(MINUSMINUS))
  823. return NODE1(DEC, a);
  824. return a;
  825. }
  826. node_t *parse_unary(list_t *tokens, size_t *pos) {
  827. if (MATCH(MINUS)) {
  828. node_t *a = parse_unary(tokens, pos);
  829. return NODE1(NEGATE, a);
  830. } else if (MATCH(BANG)) {
  831. node_t *a = parse_unary(tokens, pos);
  832. return NODE1(NOT, a);
  833. } else if (MATCH(TILDE)) {
  834. node_t *a = parse_unary(tokens, pos);
  835. return NODE1(BNOT, a);
  836. }
  837. return parse_postfix(tokens, pos);
  838. }
  839. node_t *parse_pow(list_t *tokens, size_t *pos) {
  840. node_t *a = parse_unary(tokens, pos);
  841. do {
  842. if (MATCH(STARSTAR)) {
  843. node_t *b = parse_unary(tokens, pos);
  844. a = NODE2(POW, a, b);
  845. continue;
  846. }
  847. break;
  848. } while (1);
  849. return a;
  850. }
  851. node_t *parse_mul(list_t *tokens, size_t *pos) {
  852. node_t *a = parse_pow(tokens, pos);
  853. do {
  854. if (MATCH(STAR)) {
  855. node_t *b = parse_pow(tokens, pos);
  856. a = NODE2(MUL, a, b);
  857. continue;
  858. } else if (MATCH(SLASH)) {
  859. node_t *b = parse_pow(tokens, pos);
  860. a = NODE2(DIV, a, b);
  861. continue;
  862. } else if (MATCH(SLASHSLASH)) {
  863. node_t *b = parse_pow(tokens, pos);
  864. a = NODE2(IDIV, a, b);
  865. continue;
  866. } else if (MATCH(PERCENT)) {
  867. node_t *b = parse_pow(tokens, pos);
  868. a = NODE2(MOD, a, b);
  869. continue;
  870. }
  871. break;
  872. } while (1);
  873. return a;
  874. }
  875. node_t *parse_add(list_t *tokens, size_t *pos) {
  876. node_t *a = parse_mul(tokens, pos);
  877. do {
  878. if (MATCH(PLUS)) {
  879. node_t *b = parse_mul(tokens, pos);
  880. a = NODE2(ADD, a, b);
  881. continue;
  882. } else if (MATCH(MINUS)) {
  883. node_t *b = parse_mul(tokens, pos);
  884. a = NODE2(SUB, a, b);
  885. continue;
  886. }
  887. break;
  888. } while (1);
  889. return a;
  890. }
  891. node_t *parse_shift(list_t *tokens, size_t *pos) {
  892. node_t *a = parse_add(tokens, pos);
  893. do {
  894. if (MATCH(LTLT)) {
  895. node_t *b = parse_add(tokens, pos);
  896. a = NODE2(SHL, a, b);
  897. continue;
  898. } else if (MATCH(GTGT)) {
  899. node_t *b = parse_add(tokens, pos);
  900. a = NODE2(SHR, a, b);
  901. continue;
  902. }
  903. break;
  904. } while (1);
  905. return a;
  906. }
  907. node_t *parse_relation(list_t *tokens, size_t *pos) {
  908. node_t *a = parse_shift(tokens, pos);
  909. do {
  910. if (MATCH(LT)) {
  911. node_t *b = parse_shift(tokens, pos);
  912. a = NODE2(LT, a, b);
  913. continue;
  914. } else if (MATCH(GT)) {
  915. node_t *b = parse_shift(tokens, pos);
  916. a = NODE2(GT, a, b);
  917. continue;
  918. } else if (MATCH(LE)) {
  919. node_t *b = parse_shift(tokens, pos);
  920. a = NODE2(LE, a, b);
  921. continue;
  922. } else if (MATCH(GE)) {
  923. node_t *b = parse_shift(tokens, pos);
  924. a = NODE2(GE, a, b);
  925. continue;
  926. }
  927. break;
  928. } while (1);
  929. return a;
  930. }
  931. node_t *parse_equality(list_t *tokens, size_t *pos) {
  932. node_t *a = parse_relation(tokens, pos);
  933. do {
  934. if (MATCH(EQUALS)) {
  935. node_t *b = parse_relation(tokens, pos);
  936. a = NODE2(EQUALS, a, b);
  937. continue;
  938. } else if (MATCH(NOTEQUALS)) {
  939. node_t *b = parse_relation(tokens, pos);
  940. a = NODE2(NOTEQUALS, a, b);
  941. continue;
  942. } else if (MATCH(IS)) {
  943. node_t *b = parse_relation(tokens, pos);
  944. a = NODE2(IS, a, b);
  945. continue;
  946. } else if (AT(BANG) && ATP(IS, 1)) {
  947. EXPECT(BANG, "!");
  948. EXPECT(IS, "is");
  949. node_t *b = parse_relation(tokens, pos);
  950. a = NODE2(NOTIS, a, b);
  951. continue;
  952. } else if (MATCH(IN)) {
  953. node_t *b = parse_relation(tokens, pos);
  954. a = NODE2(IN, a, b);
  955. continue;
  956. } else if (AT(BANG) && ATP(IN, 1)) {
  957. EXPECT(BANG, "!");
  958. EXPECT(IN, "in");
  959. node_t *b = parse_relation(tokens, pos);
  960. a = NODE2(NOTIN, a, b);
  961. continue;
  962. }
  963. break;
  964. } while (1);
  965. return a;
  966. }
  967. node_t *parse_bitand(list_t *tokens, size_t *pos) {
  968. node_t *a = parse_equality(tokens, pos);
  969. while (MATCH(AND)) {
  970. node_t *b = parse_equality(tokens, pos);
  971. a = NODE2(BAND, a, b);
  972. }
  973. return a;
  974. }
  975. node_t *parse_bitxor(list_t *tokens, size_t *pos) {
  976. node_t *a = parse_bitand(tokens, pos);
  977. while (MATCH(RAISE)) {
  978. node_t *b = parse_bitand(tokens, pos);
  979. a = NODE2(XOR, a, b);
  980. }
  981. return a;
  982. }
  983. node_t *parse_bitor(list_t *tokens, size_t *pos) {
  984. node_t *a = parse_bitxor(tokens, pos);
  985. while (MATCH(BAR)) {
  986. node_t *b = parse_bitxor(tokens, pos);
  987. a = NODE2(BOR, a, b);
  988. }
  989. return a;
  990. }
  991. node_t *parse_logand(list_t *tokens, size_t *pos) {
  992. node_t *a = parse_bitor(tokens, pos);
  993. if (MATCH(ANDAND)) {
  994. node_t *b = parse_logand(tokens, pos);
  995. return NODE2(LOGAND, a, b);
  996. }
  997. return a;
  998. }
  999. node_t *parse_logor(list_t *tokens, size_t *pos) {
  1000. node_t *a = parse_logand(tokens, pos);
  1001. if (MATCH(BARBAR)) {
  1002. node_t *b = parse_logor(tokens, pos);
  1003. return NODE2(LOGOR, a, b);
  1004. }
  1005. return a;
  1006. }
  1007. node_t *parse_assignment(list_t *tokens, size_t *pos);
  1008. node_t *parse_conditional(list_t *tokens, size_t *pos) {
  1009. node_t *a = parse_logor(tokens, pos);
  1010. if (MATCH(QM)) {
  1011. node_t *b = parse_assignment(tokens, pos);
  1012. EXPECT(COLON, ":");
  1013. node_t *c = parse_assignment(tokens, pos);
  1014. return NODE3(IFEXPR, a, b, c);
  1015. }
  1016. return a;
  1017. }
  1018. node_t *parse_assignment(list_t *tokens, size_t *pos) {
  1019. node_t *a = parse_conditional(tokens, pos);
  1020. if (MATCH(ASSIGN)) {
  1021. node_t *b = parse_assignment(tokens, pos);
  1022. return NODE2(ASSIGN, a, b);
  1023. } else if (MATCH(PLUSASSIGN)) {
  1024. node_t *b = parse_assignment(tokens, pos);
  1025. return NODE2(ASSIGN_ADD, a, b);
  1026. } else if (MATCH(MINUSASSIGN)) {
  1027. node_t *b = parse_assignment(tokens, pos);
  1028. return NODE2(ASSIGN_SUB, a, b);
  1029. } else if (MATCH(STARASSIGN)) {
  1030. node_t *b = parse_assignment(tokens, pos);
  1031. return NODE2(ASSIGN_MUL, a, b);
  1032. } else if (MATCH(SLASHASSIGN)) {
  1033. node_t *b = parse_assignment(tokens, pos);
  1034. return NODE2(ASSIGN_DIV, a, b);
  1035. } else if (MATCH(SLASHSLASHASSIGN)) {
  1036. node_t *b = parse_assignment(tokens, pos);
  1037. return NODE2(ASSIGN_IDIV, a, b);
  1038. } else if (MATCH(PERCENTASSIGN)) {
  1039. node_t *b = parse_assignment(tokens, pos);
  1040. return NODE2(ASSIGN_MOD, a, b);
  1041. }
  1042. return a;
  1043. }
  1044. node_t *parse_expr(list_t *tokens, size_t *pos) {
  1045. return parse_assignment(tokens, pos);
  1046. }
  1047. node_t *parse_stmt(list_t *tokens, size_t *pos);
  1048. node_t *parse_block(list_t *tokens, size_t *pos) {
  1049. EXPECT(LCB, "{");
  1050. list_t *stmts = list_new();
  1051. while (!AT(EOF) && !AT(RCB))
  1052. list_push(stmts, parse_stmt(tokens, pos));
  1053. EXPECT(RCB, "}");
  1054. return NODEL(PROGRAM, stmts);
  1055. }
  1056. #define BLOCK() (CLIFF||MATCH(COLON)?parse_stmt(tokens, pos):parse_block(tokens, pos))
  1057. node_t *parse_if(list_t *tokens, size_t *pos) {
  1058. node_t *a = parse_expr(tokens, pos);
  1059. node_t *b = BLOCK();
  1060. node_t *c = NULL;
  1061. if (MATCH(ELSE))
  1062. c = BLOCK();
  1063. else if (MATCH(ELIF))
  1064. c = parse_if(tokens, pos);
  1065. return NODE3(IF, a, b, c);
  1066. }
  1067. node_t *parse_var(list_t *tokens, size_t *pos, int is_let) {
  1068. table_t *h = table_new();
  1069. do {
  1070. if(!AT(NAME))
  1071. PARSE_ERROR("expected identifier");
  1072. char *k = ((token_t *)tokens->data[(*pos)++])->text;
  1073. node_t *v = NULL;
  1074. if (is_let) {
  1075. EXPECT(ASSIGN, "=");
  1076. v = parse_expr(tokens, pos);
  1077. } else if (MATCH(ASSIGN))
  1078. v = parse_expr(tokens, pos);
  1079. table_set(h, k, v);
  1080. } while (MATCH(COMMA));
  1081. if (is_let)
  1082. return NODEH(LET, h);
  1083. return NODEH(VAR, h);
  1084. }
  1085. node_t *parse_func(list_t *tokens, size_t *pos, int is_expr) {
  1086. token_t *name = NULL;
  1087. if (!is_expr) {
  1088. if(!AT(NAME))
  1089. PARSE_ERROR("expected identifier");
  1090. name = tokens->data[(*pos)++];
  1091. }
  1092. EXPECT(LPAR, "(");
  1093. table_t *params = NULL;
  1094. if (!AT(RPAR)) {
  1095. int flag = 0;
  1096. params = table_new();
  1097. size_t argc = 0;
  1098. do {
  1099. if(!AT(NAME))
  1100. PARSE_ERROR("expected identifier");
  1101. char *l = ((token_t *)tokens->data[(*pos)++])->text;
  1102. node_t *r = NULL;
  1103. if (!flag && AT(ASSIGN))
  1104. flag = 1;
  1105. if (flag) {
  1106. EXPECT(ASSIGN, "=");
  1107. r = parse_expr(tokens, pos);
  1108. }
  1109. list_t *pair = list_new();
  1110. size_t *argcp = malloc(sizeof(size_t));
  1111. memcpy(argcp, &argc, sizeof(size_t));
  1112. argc++;
  1113. list_push(pair, argcp);
  1114. list_push(pair, r);
  1115. table_set(params, l, pair);
  1116. } while (MATCH(COMMA));
  1117. }
  1118. EXPECT(RPAR, ")");
  1119. table_t *captured = NULL;
  1120. if (MATCH(USE)) {
  1121. EXPECT(RPAR, "(");
  1122. captured = table_new();
  1123. do {
  1124. if(!AT(NAME))
  1125. PARSE_ERROR("expected identifier");
  1126. token_t *name = tokens->data[(*pos)++];
  1127. table_set(captured, name->text, NODET(LITERAL, name));
  1128. } while (MATCH(COMMA));
  1129. EXPECT(RPAR, ")");
  1130. }
  1131. node_t *body = BLOCK();
  1132. if (is_expr)
  1133. return NODEF(FUNCEXPR, NULL, params, captured, body);
  1134. return NODEF(FUNCDEF, name, params, captured, body);
  1135. }
  1136. node_t *parse_stmt(list_t *tokens, size_t *pos) {
  1137. if (MATCH(LCB)) {
  1138. list_t *stmts = list_new();
  1139. while (!AT(EOF) && !AT(RCB)) {
  1140. node_t *n = parse_stmt(tokens, pos);
  1141. MATCH(SEMI);
  1142. list_push(stmts, n);
  1143. }
  1144. EXPECT(RCB, "}");
  1145. return NODEL(BLOCK, stmts);
  1146. } else if (MATCH(VAR))
  1147. return parse_var(tokens, pos, 0);
  1148. else if (MATCH(LET))
  1149. return parse_var(tokens, pos, 1);
  1150. else if (MATCH(IF))
  1151. return parse_if(tokens, pos);
  1152. else if (MATCH(FOR)) {
  1153. node_t *a = NULL;
  1154. node_t *b = NULL;
  1155. node_t *c = NULL;
  1156. if (!AT(LCB) && !AT(COLON) && !CLIFF) {
  1157. if (MATCH(VAR)) {
  1158. if (AT(NAME) && ATP(OF, 1)) {
  1159. token_t *t = tokens->data[(*pos)++];
  1160. EXPECT(OF, "of");
  1161. a = parse_expr(tokens, pos);
  1162. b = BLOCK();
  1163. return NODE2t(FOROF, a, b, t);
  1164. }
  1165. a = parse_var(tokens, pos, 0);
  1166. EXPECT(SEMI, ";");
  1167. b = parse_expr(tokens, pos);
  1168. EXPECT(SEMI, ";");
  1169. c = parse_expr(tokens, pos);
  1170. } else a = parse_expr(tokens, pos);
  1171. }
  1172. node_t *d = BLOCK();
  1173. return NODE4(FOR, a, b, c, d);
  1174. } else if (MATCH(BREAK)) return NODE0(BREAK);
  1175. else if (MATCH(CONTINUE)) return NODE0(CONTINUE);
  1176. else if (MATCH(FUNC))
  1177. return parse_func(tokens, pos, 0);
  1178. else if (MATCH(RETURN)) {
  1179. node_t *a = NULL;
  1180. if (!AT(RCB) && !AT(EOF) && !CLIFF)
  1181. a = parse_expr(tokens, pos);
  1182. return NODE1(RETURN, a);
  1183. } else if (MATCH(DEFER)) {
  1184. node_t *a;
  1185. if (AT(LCB))
  1186. a = BLOCK();
  1187. else a = parse_stmt(tokens, pos);
  1188. return NODE1(DEFER, a);
  1189. } else if (MATCH(PASS)) return NODE0(PASS);
  1190. else if (MATCH(TRY)) {
  1191. node_t *a = BLOCK();
  1192. token_t *t = NULL;
  1193. EXPECT(CATCH, "catch");
  1194. if (!AT(COLON) && !AT(LCB) && !CLIFF) {
  1195. if (!AT(NAME))
  1196. PARSE_ERROR("expected identifier");
  1197. t = tokens->data[(*pos)++];
  1198. }
  1199. node_t *b = BLOCK();
  1200. return NODE2t(TRY, a, b, t);
  1201. } else if (MATCH(THROW)) {
  1202. node_t *a = NULL;
  1203. if (!CLIFF)
  1204. a = parse_expr(tokens, pos);
  1205. return NODE1(THROW, a);
  1206. } else if (MATCH(GOTO)) {
  1207. if(!AT(NAME))
  1208. PARSE_ERROR("expected identifier");
  1209. token_t *t = tokens->data[(*pos)++];
  1210. return NODET(GOTO, t);
  1211. } else if (AT(NAME) && ATP(COLON, 1) && !CLIFF) {
  1212. token_t *t = tokens->data[(*pos)++];
  1213. EXPECT(COLON, ":");
  1214. return NODET(LABEL, t);
  1215. } else if (MATCH(INLINE)) {
  1216. if (!AT(STRING))
  1217. PARSE_ERROR("expected string");
  1218. token_t *t = tokens->data[(*pos)++];
  1219. return NODET(INLINE, t);
  1220. }
  1221. node_t *n = parse_expr(tokens, pos);
  1222. return NODE1(EXPRSTMT, n);
  1223. }
  1224. node_t *parse_program(list_t *tokens, size_t *pos) {
  1225. if (AT(EOF))
  1226. PARSE_ERROR("empty program");
  1227. list_t *stmts = list_new();
  1228. int flag = 0;
  1229. while (!AT(EOF) && *pos < tokens->length) {
  1230. node_t *n;
  1231. if (MATCH(REQUIRE)) {
  1232. if (flag)
  1233. PARSE_ERROR("misplaced require statement")
  1234. if (!AT(STRING))
  1235. PARSE_ERROR("expected string");
  1236. token_t *path = tokens->data[(*pos)++];
  1237. n = NODET(REQUIRE, path);
  1238. } else { n = parse_stmt(tokens, pos); flag = 1; }
  1239. MATCH(SEMI);
  1240. list_push(stmts, n);
  1241. }
  1242. return NODEL(PROGRAM, stmts);
  1243. }
  1244. node_t *parse(char *source) {
  1245. size_t pos = 0;
  1246. return parse_program(tokenize(source), &pos);
  1247. }
  1248. #define NEWGID() size_t gid = GID++
  1249. #define EMIT(fmt, ...) buffer_fmt(buf, (fmt), ##__VA_ARGS__);
  1250. #define BINOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, lstk, lbl, node->a); EMIT(", "); compile_node(gbuf, buf, ctx, lstk, lbl, node->b); EMIT(")"); }
  1251. #define UNOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, lstk, lbl, node->a); EMIT(")"); }
  1252. #define ASSIGN(lhs, rhs) {\
  1253. if ((lhs)->tag == N_LITERAL && (lhs)->t->tag == T_NAME) {\
  1254. EMIT("qi_set(state, false, \"%s\", ", (lhs)->t->text);\
  1255. rhs;\
  1256. EMIT(")");\
  1257. } else if ((lhs)->tag == N_INDEX) {\
  1258. EMIT("qi_index_set(state, false, ");\
  1259. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs)->a);\
  1260. EMIT(", ");\
  1261. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs)->b);\
  1262. EMIT(", ");\
  1263. rhs;\
  1264. EMIT(")");\
  1265. } else if ((lhs)->tag == N_MEMBER) {\
  1266. EMIT("qi_index_set(state, false, ");\
  1267. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs)->a);\
  1268. EMIT(", qi_make_string(state, \"%s\"), ", (lhs)->t->text);\
  1269. rhs;\
  1270. EMIT(")");\
  1271. } else COMPILE_ERROR("illegal assignment left-hand side");\
  1272. }
  1273. #define COMPASSIGN(lhs, s, rhs) {\
  1274. ASSIGN(node->a, {\
  1275. EMIT("qi_%s(state, ", s);\
  1276. compile_node(gbuf, buf, ctx, lstk, lbl, (lhs));\
  1277. EMIT(", ");\
  1278. rhs;\
  1279. EMIT(")");\
  1280. });\
  1281. }
  1282. #define COMPILE_ERROR(fmt, ...) { format_error(GETFNAME(node->fi), GETSRC(node->fi), node->pos, fmt, ##__VA_ARGS__); exit(1); }
  1283. void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, node_t *node);
  1284. void compile_list(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, list_t *seq) {
  1285. if (!seq || seq->length < 1) {
  1286. EMIT("NULL");
  1287. return;
  1288. }
  1289. buffer_t *tbuf = buffer_new();
  1290. NEWGID();
  1291. buffer_fmt(tbuf, "qi_list_t *__list%d(qi_state_t *state) {\n", gid);
  1292. buffer_fmt(tbuf, "qi_list_t *list = qi_list_make();\n");
  1293. for (size_t i = 0; i < seq->length; i++) {
  1294. buffer_fmt(tbuf, "qi_list_push(list, ");
  1295. compile_node(gbuf, tbuf, ctx, lstk, lbl, seq->data[i]);
  1296. buffer_fmt(tbuf, ");\n");
  1297. }
  1298. buffer_fmt(tbuf, "return list;\n");
  1299. buffer_fmt(tbuf, "}\n");
  1300. buffer_appendb(gbuf, tbuf);
  1301. EMIT("__list%d(state)", gid);
  1302. }
  1303. void compile_table(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, table_t *table) {
  1304. if (!table || table->used < 1) {
  1305. EMIT("NULL");
  1306. return;
  1307. }
  1308. buffer_t *tbuf = buffer_new();
  1309. NEWGID();
  1310. buffer_fmt(tbuf, "qi_table_t *__table%d(qi_state_t *state) {\n", gid);
  1311. buffer_fmt(tbuf, "qi_table_t *table = qi_table_make();\n");
  1312. table_iterate(table, {
  1313. buffer_fmt(tbuf, "qi_table_set(table, \"%s\", ", entry.key);
  1314. compile_node(gbuf, tbuf, ctx, lstk, lbl, entry.value);
  1315. buffer_fmt(tbuf, ");\n");
  1316. });
  1317. buffer_fmt(tbuf, "return table;\n");
  1318. buffer_fmt(tbuf, "}\n");
  1319. buffer_appendb(gbuf, tbuf);
  1320. EMIT("__table%d(state)", gid);
  1321. }
  1322. #define CTXPUSH(s) list_push(ctx, (s))
  1323. #define CTXPOP() list_pop(ctx)
  1324. int in_context(list_t *ctx, char *s) {
  1325. if (!ctx->length)
  1326. return 0;
  1327. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  1328. if (strcmp(ctx->data[i], "gap") == 0)
  1329. break;
  1330. else if (strcmp(ctx->data[i], s) == 0)
  1331. return 1;
  1332. }
  1333. return 0;
  1334. }
  1335. size_t count_ctxs(list_t *ctx, char *s) {
  1336. if (!ctx->length)
  1337. return 0;
  1338. size_t k = 0;
  1339. for (ssize_t i = ctx->length - 1; i >= 0; i--) {
  1340. if (strcmp(ctx->data[i], "gap") == 0)
  1341. break;
  1342. else if (strcmp(ctx->data[i], s) == 0)
  1343. k++;
  1344. }
  1345. return k;
  1346. }
  1347. #define INCTX(s) (in_context(ctx, (s)))
  1348. #define SCOPESK (count_ctxs(ctx, "scope"))
  1349. #define TRAPSK (count_ctxs(ctx, "trap"))
  1350. #define LPUSH(i) stack_push(lstk, (i))
  1351. #define LPOP() stack_pop(lstk)
  1352. #define LID (lstk->data[lstk->length-1])
  1353. #define LBPUSH() list_push(lbl, table_new())
  1354. #define LBPOP() list_pop(lbl)
  1355. char *tempvar() {
  1356. NEWGID();
  1357. char *s = malloc(sizeof(char) * 64);
  1358. snprintf(s, 64, "__temp%zu", gid);
  1359. return s;
  1360. }
  1361. void compile_func(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, node_t *node) {
  1362. NEWGID();
  1363. buffer_t *tbuf = buffer_new();
  1364. buffer_fmt(tbuf, "qi_value_t *__func%d(qi_state_t *state, qi_size_t pargc, qi_list_t *pargs) {\n", gid);
  1365. LBPUSH();
  1366. CTXPUSH("gap");
  1367. CTXPUSH("func");
  1368. size_t optargc = 0;
  1369. if (node->h) {
  1370. table_iterate(node->h, {
  1371. list_t *pair = entry.value;
  1372. size_t argc = *(size_t *)pair->data[0];
  1373. if (pair->data[1]) {
  1374. optargc++;
  1375. buffer_fmt(tbuf, "qi_set(state, false, \"%s\", pargc >= %d? qi_list_index(pargs, %d): ", entry.key, argc+1, argc);
  1376. compile_node(gbuf, tbuf, ctx, lstk, lbl, pair->data[1]);
  1377. buffer_fmt(tbuf, ");\n");
  1378. } else
  1379. buffer_fmt(tbuf, "qi_set(state, false, \"%s\", qi_list_index(pargs, %d));\n", entry.key, argc);
  1380. argc++;
  1381. });
  1382. }
  1383. compile_node(gbuf, tbuf, ctx, lstk, lbl, node->a);
  1384. CTXPOP();
  1385. CTXPOP();
  1386. LBPOP();
  1387. buffer_fmt(tbuf, "return state->nil;\n");
  1388. buffer_fmt(tbuf, "}\n");
  1389. buffer_appendb(gbuf, tbuf);
  1390. tbuf = buffer_new();
  1391. 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);
  1392. compile_table(gbuf, tbuf, ctx, lstk, lbl, node->h2);
  1393. buffer_fmt(tbuf, ")");
  1394. if (node->tag == N_FUNCEXPR) {
  1395. buffer_appendb(buf, tbuf);
  1396. return;
  1397. }
  1398. EMIT("qi_set(state, false, \"%s\", ", node->t->text);
  1399. buffer_appendb(buf, tbuf);
  1400. EMIT(");");
  1401. }
  1402. void compile_block(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, list_t *block) {
  1403. for (size_t i = 0; i < block->length; i++) {
  1404. node_t *n = block->data[i];
  1405. if (n->tag == N_FUNCDEF) {
  1406. compile_func(gbuf, buf, ctx, lstk, lbl, n);
  1407. EMIT("\n");
  1408. } else if (n->tag == N_VAR || n->tag == N_LET) {
  1409. table_iterate(n->h, {
  1410. EMIT("qi_%s(state, \"%s\", ", n->tag == N_LET? "decl_const": "decl", entry.key);
  1411. if (entry.value)
  1412. compile_node(gbuf, buf, ctx, lstk, lbl, entry.value);
  1413. else EMIT("state->nil");
  1414. EMIT(");\n");
  1415. });
  1416. }
  1417. }
  1418. for (size_t i = 0; i < block->length; i++) {
  1419. compile_node(gbuf, buf, ctx, lstk, lbl, block->data[i]);
  1420. EMIT("\n");
  1421. }
  1422. }
  1423. const char *STD[][2] = {
  1424. {"std",
  1425. "func exit(c) {\n"
  1426. " if type(c) != \"number\":\n"
  1427. " throw \"expected first argument to be: number, but got: \" + type(c)\n"
  1428. " inline 'int code = qi_get(state, \"c\")->value.number'\n"
  1429. " inline 'exit(code)'\n"
  1430. "}\n"
  1431. "func head(l): return l[0]\n"
  1432. "func die(msg, c=1) {\n"
  1433. " println(msg)\n"
  1434. " exit(c)\n"
  1435. "}\n"
  1436. "let SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2\n"
  1437. "func frewind(file)\n"
  1438. " fseek(file, 0, SEEK_SET)\n"
  1439. "func file_read(filename) {\n"
  1440. " var file = fopen(filename, \"r\")\n"
  1441. " defer fclose(file)\n"
  1442. " fseek(file, 0, SEEK_END)\n"
  1443. " let size = ftell(file)\n"
  1444. " frewind(file)\n"
  1445. " return str(fread(file, size))\n"
  1446. "}\n"
  1447. },
  1448. {NULL, NULL}
  1449. };
  1450. char *unescape(char *s) {
  1451. buffer_t *buf = buffer_new();
  1452. for (size_t i = 0; i < strlen(s); i++) {
  1453. char c = s[i];
  1454. if (c == '\\') {
  1455. char nc = s[i+1];
  1456. if (!nc)
  1457. continue;
  1458. switch (nc) {
  1459. case 'n':
  1460. buffer_append(buf, '\n');
  1461. break;
  1462. default:
  1463. buffer_append(buf, nc);
  1464. break;
  1465. }
  1466. i++;
  1467. } else buffer_append(buf, c);
  1468. }
  1469. return buffer_read(buf);
  1470. }
  1471. void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl);
  1472. void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl, node_t *node) {
  1473. switch (node->tag) {
  1474. case N_PROGRAM:
  1475. compile_block(gbuf, buf, ctx, lstk, lbl, node->l);
  1476. break;
  1477. case N_EXPRSTMT:
  1478. EMIT("(void)(");
  1479. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1480. EMIT(");");
  1481. break;
  1482. case N_BLOCK:
  1483. LBPUSH();
  1484. CTXPUSH("scope");
  1485. EMIT("qi_new_scope(state);\n");
  1486. compile_block(gbuf, buf, ctx, lstk, lbl, node->l);
  1487. EMIT("qi_old_scope(state);");
  1488. CTXPOP();
  1489. LBPOP();
  1490. break;
  1491. case N_LITERAL:
  1492. switch (node->t->tag) {
  1493. case T_NUMBER:
  1494. EMIT("qi_make_number(state, %s)", node->t->text);
  1495. break;
  1496. case T_STRING:
  1497. if (!*(node->t->text)) {
  1498. EMIT("state->empty_string");
  1499. } else {
  1500. EMIT("qi_make_string(state, \"%s\")", node->t->text);
  1501. }
  1502. break;
  1503. case T_NAME:
  1504. EMIT("qi_get(state, \"%s\")", node->t->text);
  1505. break;
  1506. default:
  1507. COMPILE_ERROR("not yet implemented");
  1508. }
  1509. break;
  1510. case N_LIST:
  1511. EMIT("qi_make_list(state, ");
  1512. compile_list(gbuf, buf, ctx, lstk, lbl, node->l);
  1513. EMIT(")");
  1514. break;
  1515. case N_TUPLE:
  1516. EMIT("qi_make_tuple(state, ");
  1517. compile_list(gbuf, buf, ctx, lstk, lbl, node->l);
  1518. EMIT(")");
  1519. break;
  1520. case N_NILTUPLE: EMIT("state->empty_tuple"); break;
  1521. case N_TABLE:
  1522. EMIT("qi_make_table(state, ");
  1523. compile_table(gbuf, buf, ctx, lstk, lbl, node->h);
  1524. EMIT(")");
  1525. break;
  1526. case N_CALL:
  1527. EMIT("qi_call(state, ");
  1528. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1529. EMIT(", ");
  1530. compile_list(gbuf, buf, ctx, lstk, lbl, node->l);
  1531. EMIT(")");
  1532. break;
  1533. case N_MEMBER:
  1534. EMIT("qi_index(state, ");
  1535. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1536. EMIT(", qi_make_string(state, \"%s\"))", node->t->text);
  1537. break;
  1538. case N_INDEX:
  1539. EMIT("qi_index(state, ");
  1540. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1541. EMIT(", ");
  1542. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1543. EMIT(")");
  1544. break;
  1545. case N_ASSIGN: ASSIGN(node->a, compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1546. case N_ASSIGN_ADD: COMPASSIGN(node->a, "add", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1547. case N_ASSIGN_SUB: COMPASSIGN(node->a, "sub", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1548. case N_ASSIGN_MUL: COMPASSIGN(node->a, "mul", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1549. case N_ASSIGN_DIV: COMPASSIGN(node->a, "div", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1550. case N_ASSIGN_IDIV: COMPASSIGN(node->a, "idiv", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1551. case N_ASSIGN_MOD: COMPASSIGN(node->a, "mod", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1552. case N_ASSIGN_POW: COMPASSIGN(node->a, "pow", compile_node(gbuf, buf, ctx, lstk, lbl, node->b)); break;
  1553. case N_INC:
  1554. COMPASSIGN(node->a, "add", EMIT("state->one"));
  1555. break;
  1556. case N_DEC:
  1557. COMPASSIGN(node->a, "sub", EMIT("state->one"));
  1558. break;
  1559. case N_VAR: break;
  1560. case N_LET: break;
  1561. case N_IF:
  1562. EMIT("if (_qi_truthy(state, ");
  1563. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1564. EMIT(")) {\n");
  1565. CTXPUSH("scope");
  1566. EMIT("qi_new_scope(state);\n");
  1567. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1568. EMIT("qi_old_scope(state);\n");
  1569. CTXPOP();
  1570. if (node->c) {
  1571. EMIT("} else {\n");
  1572. CTXPUSH("scope");
  1573. EMIT("qi_new_scope(state);\n");
  1574. compile_node(gbuf, buf, ctx, lstk, lbl, node->c);
  1575. EMIT("qi_old_scope(state);\n");
  1576. CTXPOP();
  1577. }
  1578. EMIT("}");
  1579. break;
  1580. case N_FOR: {
  1581. NEWGID();
  1582. CTXPUSH("scope");
  1583. EMIT("qi_new_scope(state);\n");
  1584. if (!node->a) {
  1585. EMIT("for (;;) {\n");
  1586. } else if (node->a && !node->b) {
  1587. EMIT("while (_qi_truthy(state, ");
  1588. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1589. EMIT(")) {\n");
  1590. } else {
  1591. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1592. EMIT("while (_qi_truthy(state, ");
  1593. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1594. EMIT(")) {\n");
  1595. }
  1596. LPUSH(gid);
  1597. CTXPUSH("for");
  1598. compile_node(gbuf, buf, ctx, lstk, lbl, node->d);
  1599. CTXPOP();
  1600. LPOP();
  1601. if (node->c)
  1602. compile_node(gbuf, buf, ctx, lstk, lbl, node->c);
  1603. EMIT("__continue%d:;\n", gid);
  1604. EMIT("}\n");
  1605. EMIT("__break%d:;\n", gid);
  1606. EMIT("qi_old_scope(state);\n");
  1607. CTXPOP();
  1608. } break;
  1609. case N_FOROF: {
  1610. NEWGID();
  1611. char *varname = tempvar();
  1612. CTXPUSH("scope");
  1613. EMIT("qi_new_scope(state);\n");
  1614. EMIT("qi_value_t *%s = qi_iter(state, ", varname);
  1615. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1616. EMIT(");\n");
  1617. EMIT("qi_decl(state, \"%s\", state->nil);\n", node->t->text);
  1618. EMIT("for (qi_size_t length = _qi_length(state, %s), i = 0; i < length; i++) {\n", varname);
  1619. EMIT("qi_set(state, false, \"%s\", qi_index(state, %s, qi_make_number(state, i)));\n", node->t->text, varname);
  1620. LPUSH(gid);
  1621. CTXPUSH("for");
  1622. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1623. CTXPOP();
  1624. LPOP();
  1625. EMIT("__continue%d:;\n", gid);
  1626. EMIT("}\n");
  1627. EMIT("__break%d:;\n", gid);
  1628. EMIT("qi_old_scope(state);\n");
  1629. CTXPOP();
  1630. } break;
  1631. case N_BREAK:
  1632. if (!INCTX("for"))
  1633. COMPILE_ERROR("break outside of a loop");
  1634. EMIT("goto __break%d;", LID);
  1635. break;
  1636. case N_CONTINUE:
  1637. if (!INCTX("for"))
  1638. COMPILE_ERROR("continue outside of a loop");
  1639. EMIT("goto __continue%d;", LID);
  1640. break;
  1641. case N_DEFER: {
  1642. NEWGID();
  1643. buffer_t *tbuf = buffer_new();
  1644. buffer_fmt(tbuf, "void __defer%d(qi_state_t *state) {\n", gid);
  1645. LBPUSH();
  1646. CTXPUSH("gap");
  1647. compile_node(gbuf, tbuf, ctx, lstk, lbl, node->a);
  1648. CTXPOP();
  1649. LBPOP();
  1650. buffer_fmt(tbuf, "\n");
  1651. buffer_fmt(tbuf, "}\n");
  1652. buffer_appendb(gbuf, tbuf);
  1653. EMIT("qi_add_defer(state, -1, __defer%d);", gid);
  1654. } break;
  1655. case N_RETURN:
  1656. if (!INCTX("func"))
  1657. COMPILE_ERROR("return outside of a function");
  1658. for (size_t i = 0; i < SCOPESK; i++)
  1659. EMIT("qi_old_scope(state);\n");
  1660. for (size_t i = 0; i < TRAPSK; i++)
  1661. EMIT("qi_unset_trap(state, trap);\n");
  1662. EMIT("return ");
  1663. if (node->a)
  1664. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1665. else EMIT("state->nil");
  1666. EMIT(";");
  1667. break;
  1668. case N_FUNCDEF: break;
  1669. case N_PASS: break;
  1670. case N_TRY:
  1671. CTXPUSH("scope");
  1672. EMIT("qi_new_scope(state);\n");
  1673. EMIT("qi_try(state, {\n");
  1674. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1675. EMIT("}, {\n");
  1676. if (node->t)
  1677. EMIT("qi_decl(state, \"%s\", trap->value);\n", node->t->text);
  1678. CTXPUSH("trap");
  1679. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1680. CTXPOP();
  1681. EMIT("}, NULL);\n");
  1682. EMIT("qi_new_scope(state);");
  1683. CTXPOP();
  1684. break;
  1685. case N_THROW:
  1686. EMIT("qi_throw(state, ");
  1687. if (node->a)
  1688. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1689. else {
  1690. EMIT("state->nil");
  1691. }
  1692. EMIT(");");
  1693. break;
  1694. case N_LABEL: {
  1695. char *label = node->t->text;
  1696. table_iterate((table_t *)list_index(lbl, -1), {
  1697. if (strcmp(entry.key, label) == 0) {
  1698. COMPILE_ERROR("duplicated label: '%s'", label);
  1699. }
  1700. });
  1701. NEWGID();
  1702. EMIT("__label%d:;", gid);
  1703. size_t *n = malloc(sizeof(size_t));
  1704. memcpy(n, &gid, sizeof(size_t));
  1705. table_set(list_index(lbl, -1), label, n);
  1706. } break;
  1707. case N_GOTO: {
  1708. ssize_t gid = -1;
  1709. char *label = node->t->text;
  1710. table_iterate((table_t *)list_index(lbl, -1), {
  1711. if (strcmp(entry.key, label) == 0) {
  1712. gid = *(size_t *)entry.value;
  1713. break;
  1714. }
  1715. });
  1716. if (gid < 0)
  1717. COMPILE_ERROR("undefined label: '%s'", label);
  1718. EMIT("goto __label%d;", gid);
  1719. } break;
  1720. case N_REQUIRE: {
  1721. char *source = NULL;
  1722. char *path = unescape(node->t->text);
  1723. for (size_t i = 0; STD[i][0]; i++) {
  1724. if (strcmp(path, STD[i][0]) == 0) {
  1725. source = (char *)STD[i][1];
  1726. break;
  1727. }
  1728. }
  1729. if (is_required(path))
  1730. break;
  1731. if (!source) {
  1732. FILE *fd = fopen(path, "rb");
  1733. if (!fd)
  1734. COMPILE_ERROR("'%s' is not a valid file path or a builtin library name", path);
  1735. buffer_t *fbuf = buffer_new();
  1736. for (;;) {
  1737. char line[512];
  1738. if (!fgets(line, sizeof(line), fd))
  1739. break;
  1740. buffer_appends(fbuf, line);
  1741. }
  1742. source = buffer_read(fbuf);
  1743. path = realpath(path, NULL);
  1744. }
  1745. list_t *pair = list_new();
  1746. list_push(pair, path);
  1747. list_push(pair, source);
  1748. list_push(FILES, pair);
  1749. compile_into(source, gbuf, buf, ctx, lstk, lbl);
  1750. list_pop(FILES);
  1751. list_push(REQUIRED, path);
  1752. } break;
  1753. case N_IFEXPR:
  1754. EMIT("(_qi_truthy(state, ");
  1755. compile_node(gbuf, buf, ctx, lstk, lbl, node->a);
  1756. EMIT(")? ");
  1757. compile_node(gbuf, buf, ctx, lstk, lbl, node->b);
  1758. EMIT(": ");
  1759. compile_node(gbuf, buf, ctx, lstk, lbl, node->c);
  1760. EMIT(")");
  1761. break;
  1762. case N_FUNCEXPR:
  1763. compile_func(gbuf, buf, ctx, lstk, lbl, node);
  1764. break;
  1765. case N_EQUALS:
  1766. BINOP("equals");
  1767. break;
  1768. case N_NOTEQUALS:
  1769. BINOP("not_equals");
  1770. break;
  1771. case N_IS:
  1772. BINOP("is");
  1773. break;
  1774. case N_NOTIS:
  1775. BINOP("not_is");
  1776. break;
  1777. case N_IN:
  1778. BINOP("in");
  1779. break;
  1780. case N_NOTIN:
  1781. BINOP("not_in");
  1782. break;
  1783. case N_LT:
  1784. BINOP("lt");
  1785. break;
  1786. case N_GT:
  1787. BINOP("gt");
  1788. break;
  1789. case N_LE:
  1790. BINOP("le");
  1791. break;
  1792. case N_GE:
  1793. BINOP("ge");
  1794. break;
  1795. case N_ADD:
  1796. BINOP("add");
  1797. break;
  1798. case N_SUB:
  1799. BINOP("sub");
  1800. break;
  1801. case N_MUL:
  1802. BINOP("mul");
  1803. break;
  1804. case N_DIV:
  1805. BINOP("div");
  1806. break;
  1807. case N_IDIV:
  1808. BINOP("idiv");
  1809. break;
  1810. case N_MOD:
  1811. BINOP("mod");
  1812. break;
  1813. case N_POW:
  1814. BINOP("pow");
  1815. break;
  1816. case N_SHL:
  1817. BINOP("shl");
  1818. break;
  1819. case N_SHR:
  1820. BINOP("shr");
  1821. break;
  1822. case N_XOR:
  1823. BINOP("xor");
  1824. break;
  1825. case N_BOR:
  1826. BINOP("bor");
  1827. break;
  1828. case N_BAND:
  1829. BINOP("band");
  1830. break;
  1831. case N_NEGATE:
  1832. UNOP("negate");
  1833. break;
  1834. case N_NOT:
  1835. UNOP("not");
  1836. break;
  1837. case N_BNOT:
  1838. UNOP("bnot");
  1839. break;
  1840. case N_INLINE: EMIT("%s;", unescape(node->t->text)); break;
  1841. default:
  1842. COMPILE_ERROR("not yet implemented");
  1843. }
  1844. }
  1845. void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *lbl) {
  1846. node_t *n = parse(source);
  1847. compile_node(gbuf, buf, ctx, lstk, lbl, n);
  1848. }
  1849. char *compile(char *source) {
  1850. list_t *ctx = list_new();
  1851. stack_t *lstk = stack_new();
  1852. list_t *lbl = list_new();
  1853. LBPUSH();
  1854. buffer_t *gbuf = buffer_new();
  1855. buffer_appends(gbuf, "#include <qirt.h>\n");
  1856. buffer_t *buf = buffer_new();
  1857. compile_into(source, gbuf, buf, ctx, lstk, lbl);
  1858. buffer_t *rbuf = buffer_new();
  1859. buffer_appendb(rbuf, gbuf);
  1860. buffer_appends(rbuf, "int main(int argc, char **argv) {\n");
  1861. buffer_appends(rbuf, "qi_state_t *state;\n");
  1862. buffer_appends(rbuf, "qi_state_init(&state);\n");
  1863. buffer_appendb(rbuf, buf);
  1864. buffer_appends(rbuf, "qi_old_scope(state);\n");
  1865. buffer_appends(rbuf, "qi_finalize();\n");
  1866. buffer_appends(rbuf, "return 0;\n");
  1867. buffer_appends(rbuf, "}\n");
  1868. return buffer_read(rbuf);
  1869. }
  1870. char *compile_file(char *filename, FILE *fd) {
  1871. buffer_t *buf = buffer_new();
  1872. for (;;) {
  1873. char line[512];
  1874. if (!fgets(line, sizeof(line), fd))
  1875. break;
  1876. buffer_appends(buf, line);
  1877. }
  1878. char *source = buffer_read(buf);
  1879. list_t *pair = list_new();
  1880. list_push(pair, filename);
  1881. list_push(pair, source);
  1882. list_push(FILES, pair);
  1883. char *out = compile(source);
  1884. list_pop(FILES);
  1885. return out;
  1886. }
  1887. int main(int argc, char **argv) {
  1888. FILES = list_new();
  1889. REQUIRED = list_new();
  1890. char *out = compile_file("<stdin>", stdin);
  1891. fwrite(out, sizeof(char), strlen(out), stdout);
  1892. return 0;
  1893. }