qic.c 59 KB

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