123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971 |
- #include <ctype.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <stdlib.h>
- size_t GID = 0;
- typedef struct {
- void **data;
- size_t length;
- } list_t;
- list_t *list_new(void) {
- list_t *list = malloc(sizeof(list_t));
- list->data = NULL;
- list->length = 0;
- return list;
- }
- void list_push(list_t *l, void *v) {
- size_t i = l->length++;
- l->data = realloc(l->data, l->length * sizeof(void *));
- l->data[i] = v;
- }
- void *list_pop(list_t *l) {
- if (!l->length)
- return NULL;
- return l->data[--l->length];
- }
- void *list_index(list_t *l, ssize_t index) {
- if (!l->length)
- return NULL;
- if (index < 0)
- index += ((ssize_t)l->length);
- if (index < 0 || index >= l->length)
- return NULL;
- return l->data[index];
- }
- void list_set(list_t *l, ssize_t index, void *v) {
- if (!l->length)
- return;
- if (index < 0)
- index += ((ssize_t)l->length);
- if (index < 0 || index >= l->length)
- return;
- l->data[index] = v;
- }
- typedef struct {
- size_t *data;
- size_t length;
- } stack_t;
- stack_t *stack_new(void) {
- stack_t *stack = malloc(sizeof(list_t));
- stack->data = NULL;
- stack->length = 0;
- return stack;
- }
- void stack_push(stack_t *l, size_t v) {
- size_t i = l->length++;
- l->data = realloc(l->data, l->length * sizeof(size_t));
- l->data[i] = v;
- }
- size_t stack_pop(stack_t *l) {
- if (!l->length)
- return 0;
- return l->data[--l->length];
- }
- struct entry_t {
- char *key;
- void *value;
- };
- struct table_t {
- struct entry_t *entries;
- size_t used;
- size_t capacity;
- };
- typedef struct entry_t entry_t;
- typedef struct table_t table_t;
- table_t *table_new() {
- table_t *table = malloc(sizeof(table_t));
- table->used = 0;
- table->capacity = 32;
- table->entries = calloc(table->capacity, sizeof(entry_t));
- return table;
- }
- unsigned long ht_hash(const char* key) {
- unsigned long hash = 5381;
- int c;
- while ((c = *key++))
- hash = ((hash << 5) + hash) + c;
- return hash;
- }
- void *table_get(table_t *table, char *key) {
- if (!table->used)
- return NULL;
- unsigned long hash = ht_hash(key);
- size_t index = hash % table->capacity;
- size_t i = index;
- while (table->entries[i].key) {
- if (strcmp(table->entries[i].key, key) == 0)
- return table->entries[i].value;
- i++;
- if (i >= table->capacity)
- i = 0;
- if (i == index)
- break;
- }
- return NULL;
- }
- static void table_entry_set(entry_t *entries, char *key, void *value, size_t capacity, size_t *used) {
- unsigned long hash = ht_hash(key);
- size_t index = hash % capacity;
- size_t i = index;
- while (entries[i].key) {
- if (strcmp(entries[i].key, key) == 0) {
- entries[i].value = value;
- return;
- }
- i++;
- if (i >= capacity)
- i = 0;
- if (i == index)
- break;
- }
- if (used)
- (*used)++;
- entries[i].key = key;
- entries[i].value = value;
- }
- table_t *table_set(table_t *table, char *key, void *value) {
- if (table->used >= table->capacity) {
- size_t capacity = table->capacity + 32;
- entry_t *entries = calloc(capacity, sizeof(entry_t));
- for (size_t i = 0; i < table->capacity; i++) {
- entry_t entry = table->entries[i];
- if (entry.key)
- table_entry_set(entries, entry.key, entry.value, capacity, NULL);
- }
- table->entries = entries;
- table->capacity = capacity;
- }
- table_entry_set(table->entries, key, value, table->capacity, &table->used);
- return table;
- }
- #define table_iterate(table, code) \
- { \
- if (table->used) { \
- size_t i = 0; \
- while (i < (table)->capacity) { \
- entry_t entry = (table)->entries[i]; \
- if (entry.key) { \
- code; \
- } \
- i++; \
- } \
- } \
- }
- typedef struct {
- char *str;
- size_t size;
- } buffer_t;
- buffer_t *buffer_new(void) {
- buffer_t *buf = malloc(sizeof(buffer_t));
- buf->str = NULL;
- buf->size = 0;
- return buf;
- }
- void buffer_append(buffer_t *buf, char c) {
- buf->size++;
- void *p = malloc(sizeof(char) * buf->size);
- if (buf->str)
- memcpy(p, buf->str, buf->size - 1);
- buf->str = p;
- buf->str[buf->size - 1] = c;
- }
- char *buffer_read(buffer_t *buf) {
- if (buf->size == 0 || buf->str[buf->size - 1])
- buffer_append(buf, 0);
- return buf->str;
- }
- void buffer_appends(buffer_t *buf, char *s) {
- for (size_t i = 0; i < strlen(s); i++)
- buffer_append(buf, s[i]);
- }
- void buffer_appendb(buffer_t *dst, buffer_t *src) {
- for (size_t i = 0; i < src->size; i++)
- buffer_append(dst, src->str[i]);
- }
- void buffer_fmt(buffer_t *buf, const char *fmt, ...) {
- va_list args;
- va_start(args, fmt);
- size_t size = vsnprintf(NULL, 0, fmt, args);
- char *str = malloc(sizeof(char) * (size + 1));
- vsnprintf(str, size + 1, fmt, args);
- va_end(args);
- buffer_appends(buf, str);
- }
- typedef struct {
- enum {
- T_EOF,
- T_NUMBER,
- T_STRING,
- T_NAME,
- T_VAR,
- T_IF,
- T_ELSE,
- T_ELIF,
- T_FOR,
- T_BREAK,
- T_CONTINUE,
- T_PASS,
- T_FUNC,
- T_USE,
- T_RETURN,
- T_DEFER,
- T_REQUIRE,
- T_LPAR,
- T_RPAR,
- T_LSB,
- T_RSB,
- T_LCB,
- T_RCB,
- T_EQUALS,
- T_PLUSASSIGN,
- T_BARBAR,
- T_ANDAND,
- T_STARSTAR,
- T_PLUSPLUS,
- T_MINUSMINUS,
- T_PLUS,
- T_MINUS,
- T_QM,
- T_COLON,
- T_BAR,
- T_AND,
- T_RAISE,
- T_LT,
- T_LTLT,
- T_GT,
- T_GTGT,
- T_STAR,
- T_SLASH,
- T_COMMA,
- T_DOT,
- T_ASSIGN,
- T_SEMI
- } tag;
- char *text;
- size_t fi;
- size_t pos;
- } token_t;
- token_t *token(int tag, char *text) {
- token_t *tok = malloc(sizeof(token_t));
- tok->tag = tag;
- tok->text = text;
- return tok;
- }
- #define TK(tk) (token(T_##tk, NULL))
- #define WS() while (source[*pos] == ' ' || source[*pos] == '\t' || source[*pos] == '\n' || source[*pos] == '\r') { (*pos)++; }
- void consume_ignored(char *source, size_t *pos) {
- WS();
- while (source[*pos] == '#') {
- (*pos)++;
- for (;;) {
- if (!source[*pos])
- break;
- if (source[*pos] == '\n') {
- (*pos)++;
- break;
- }
- (*pos)++;
- }
- WS();
- }
- }
- list_t *FILES;
- int is_required(char *path) {
- for (size_t i = 0; i < FILES->length; i++) {
- list_t *pair = FILES->data[i];
- if (strcmp(pair->data[0], path) == 0)
- return 1;
- }
- return 0;
- }
- void traverse(char *source, size_t pos, size_t *line, size_t *col) {
- *line = 1;
- *col = 1;
- for (size_t i = 0; i < pos; i++) {
- if (source[i] == '\n') {
- (*line)++;
- (*col) = 1;
- } else (*col)++;
- }
- }
- void format_error(char *filename, char *source, size_t pos, char *fmt, ...) {
- size_t line, col;
- traverse(source, pos, &line, &col);
- va_list args;
- va_start(args, fmt);
- fprintf(stderr, "%s (%zu:%zu): ", filename, line, col);
- vfprintf(stderr, fmt, args);
- fputc('\n', stderr);
- va_end(args);
- }
- #define GETFNAME(fi) ((char *)((list_t *)list_index(FILES, fi))->data[0])
- #define GETSRC(fi) ((char *)((list_t *)list_index(FILES, fi))->data[1])
- #define LEX_ERROR(fmt, ...) { format_error(GETFNAME(-1), source, *pos, fmt, ##__VA_ARGS__); exit(1); }
- token_t *next_token(char *source, size_t *pos) {
- if (!source[*pos])
- return token(T_EOF, NULL);
- if (source[*pos] == '"' || source[*pos] == '\'') {
- char term = source[(*pos)++];
- buffer_t *text = buffer_new();
- while (source[*pos] != term) {
- if (!source[*pos])
- LEX_ERROR("unterminated string literal");
- char c = source[(*pos)++];
- if (c == '\\') {
- char nc = source[(*pos)++];
- if (!nc)
- continue;
- switch (nc) {
- case 'n':
- buffer_appends(text, "\\n");
- break;
- case 't':
- buffer_appends(text, "\\t");
- break;
- case 'r':
- buffer_appends(text, "\\r");
- break;
- case 'b':
- buffer_appends(text, "\\b");
- break;
- case 'e':
- buffer_appends(text, "\\e");
- break;
- case 's':
- buffer_appends(text, " ");
- break;
- case '"':
- buffer_appends(text, "\\\"");
- break;
- case '\\':
- buffer_appends(text, "\\\\");
- break;
- default:
- buffer_append(text, nc);
- break;
- }
- continue;
- }
- if (c == '"' || c == '\\')
- buffer_append(text, '\\');
- buffer_append(text, c);
- }
- (*pos)++;
- return token(T_STRING, buffer_read(text));
- } else if (isdigit(source[*pos])) {
- buffer_t *number = buffer_new();
- int dot = 0;
- int sub = 0;
- do {
- buffer_append(number, source[(*pos)++]);
- if (!dot && source[*pos] == '.') {
- buffer_append(number, source[(*pos)++]);
- if (!isdigit(source[*pos]))
- LEX_ERROR("illegal number literal (missing part after floating point)");
- dot = 1;
- } else if (!sub && source[*pos] == '.') {
- (*pos)++;
- sub = 1;
- } else if (sub) sub = 0;
- } while (isdigit(source[*pos]));
- return token(T_NUMBER, buffer_read(number));
- } else if (isalpha(source[*pos]) || source[*pos] == '_') {
- buffer_t *text = buffer_new();
- do {
- buffer_append(text, source[(*pos)++]);
- } while (isalpha(source[*pos]) || source[*pos] == '_' || isdigit(source[*pos]));
- char *name = buffer_read(text);
- if (strcmp(name, "var") == 0)
- return TK(VAR);
- else if (strcmp(name, "if") == 0)
- return TK(IF);
- else if (strcmp(name, "else") == 0)
- return TK(ELSE);
- else if (strcmp(name, "elif") == 0)
- return TK(ELIF);
- else if (strcmp(name, "for") == 0)
- return TK(FOR);
- else if (strcmp(name, "break") == 0)
- return TK(BREAK);
- else if (strcmp(name, "continue") == 0)
- return TK(CONTINUE);
- else if (strcmp(name, "func") == 0)
- return TK(FUNC);
- else if (strcmp(name, "use") == 0)
- return TK(USE);
- else if (strcmp(name, "return") == 0)
- return TK(RETURN);
- else if (strcmp(name, "defer") == 0)
- return TK(DEFER);
- else if (strcmp(name, "pass") == 0)
- return TK(PASS);
- else if (strcmp(name, "require") == 0)
- return TK(REQUIRE);
- return token(T_NAME, name);
- } else if (strncmp(&source[*pos], "==", 2) == 0 && ++(*pos) && ++(*pos))
- return TK(EQUALS);
- else if (strncmp(&source[*pos], "+=", 2) == 0 && ++(*pos) && ++(*pos))
- return TK(PLUSASSIGN);
- else if (strncmp(&source[*pos], "||", 2) == 0 && ++(*pos) && ++(*pos))
- return TK(BARBAR);
- else if (strncmp(&source[*pos], "&&", 2) == 0 && ++(*pos) && ++(*pos))
- return TK(ANDAND);
- else if (strncmp(&source[*pos], "++", 2) == 0 && ++(*pos) && ++(*pos))
- return TK(PLUSPLUS);
- else if (strncmp(&source[*pos], "--", 2) == 0 && ++(*pos) && ++(*pos))
- return TK(MINUSMINUS);
- else if (source[*pos] == '(' && ++(*pos))
- return TK(LPAR);
- else if (source[*pos] == ')' && ++(*pos))
- return TK(RPAR);
- else if (source[*pos] == '[' && ++(*pos))
- return TK(LSB);
- else if (source[*pos] == ']' && ++(*pos))
- return TK(RSB);
- else if (source[*pos] == '{' && ++(*pos))
- return TK(LCB);
- else if (source[*pos] == '}' && ++(*pos))
- return TK(RCB);
- else if (source[*pos] == '+' && ++(*pos))
- return TK(PLUS);
- else if (source[*pos] == '-' && ++(*pos))
- return TK(MINUS);
- else if (source[*pos] == '*' && ++(*pos))
- return TK(STAR);
- else if (source[*pos] == '/' && ++(*pos))
- return TK(SLASH);
- else if (source[*pos] == '?' && ++(*pos))
- return TK(QM);
- else if (source[*pos] == ':' && ++(*pos))
- return TK(COLON);
- else if (source[*pos] == '=' && ++(*pos))
- return TK(ASSIGN);
- else if (source[*pos] == ';' && ++(*pos))
- return TK(SEMI);
- else if (source[*pos] == ',' && ++(*pos))
- return TK(COMMA);
- else if (source[*pos] == '.' && ++(*pos))
- return TK(DOT);
- else if (source[*pos] == '<' && ++(*pos))
- return TK(LT);
- else if (source[*pos] == '>' && ++(*pos))
- return TK(GT);
- LEX_ERROR("unexpected input")
- }
- list_t *tokenize(char *source) {
- size_t pos = 0;
- list_t *toks = list_new();
- do {
- consume_ignored(source, &pos);
- size_t tok_pos = pos;
- token_t *tok = next_token(source, &pos);
- tok->fi = FILES->length-1;
- tok->pos = tok_pos;
- if (tok->tag == T_EOF)
- break;
- list_push(toks, tok);
- } while (1);
- return toks;
- }
- struct _node_t {
- enum {
- N_PROGRAM,
- N_EXPRSTMT,
- N_BLOCK,
- N_NEGATE,
- N_LITERAL,
- N_LIST,
- N_TUPLE,
- N_NILTUPLE,
- N_TABLE,
- N_CALL,
- N_MEMBER,
- N_INDEX,
- N_ADD,
- N_SUB,
- N_MUL,
- N_DIV,
- N_ASSIGN,
- N_ASSIGN_ADD,
- N_EQUALS,
- N_LT,
- N_GT,
- N_INC,
- N_DEC,
- N_VAR,
- N_IF,
- N_FOR,
- N_BREAK,
- N_CONTINUE,
- N_FUNCDEF,
- N_RETURN,
- N_DEFER,
- N_PASS,
- N_REQUIRE,
- N_IFEXPR,
- N_FUNCEXPR,
- N_LOGOR,
- N_LOGAND,
- } tag;
- struct _node_t *a;
- struct _node_t *b;
- struct _node_t *c;
- struct _node_t *d;
- list_t *l;
- table_t *h;
- table_t *h2;
- token_t *t;
- size_t fi;
- size_t pos;
- };
- typedef struct _node_t node_t;
- node_t *node_pos(node_t *node, size_t fi, size_t pos) {
- node->fi = fi;
- node->pos = pos;
- return node;
- }
- node_t *nodet(int tag, token_t *t) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->t = t;
- return node;
- }
- #define NODET(n, a) (node_pos(nodet(N_##n, (a)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *nodel(int tag, list_t *l) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->l = l;
- return node;
- }
- #define NODEL(n, a) (node_pos(nodel(N_##n, (a)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *nodeh(int tag, table_t *h) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->h = h;
- return node;
- }
- #define NODEH(n, a) (node_pos(nodeh(N_##n, (a)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *node0(int tag) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- return node;
- }
- #define NODE0(n) (node_pos(node0(N_##n), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *node1(int tag, node_t *a) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->a = a;
- return node;
- }
- #define NODE1(n, a) (node_pos(node1(N_##n, (a)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *node1l(int tag, node_t *a, list_t *l) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->a = a;
- node->l = l;
- return node;
- }
- #define NODE1l(n, a, l) (node_pos(node1l(N_##n, (a), (l)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *node1t(int tag, node_t *a, token_t *t) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->a = a;
- node->t = t;
- return node;
- }
- #define NODE1t(n, a, t) (node_pos(node1t(N_##n, (a), (t)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *node2(int tag, node_t *a, node_t *b) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->a = a;
- node->b = b;
- return node;
- }
- #define NODE2(n, a, b) (node_pos(node2(N_##n, (a), (b)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *node3(int tag, node_t *a, node_t *b, node_t *c) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->a = a;
- node->b = b;
- node->c = c;
- return node;
- }
- #define NODE3(n, a, b, c) (node_pos(node3(N_##n, (a), (b), (c)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *node4(int tag, node_t *a, node_t *b, node_t *c, node_t *d) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->a = a;
- node->b = b;
- node->c = c;
- node->d = d;
- return node;
- }
- #define NODE4(n, a, b, c, d) (node_pos(node4(N_##n, (a), (b), (c), (d)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- node_t *nodef(int tag, token_t *name, table_t *params, table_t *captured, node_t *body) {
- node_t *node = malloc(sizeof(node_t));
- node->tag = tag;
- node->t = name;
- node->h = params;
- node->h2 = captured;
- node->a = body;
- return node;
- }
- #define NODEF(n, a, b, c, d) (node_pos(nodef(N_##n, (a), (b), (c), (d)), ((token_t *)tokens->data[(*pos)-1])->fi, ((token_t *)tokens->data[(*pos)-1])->pos))
- #define AT(tk) (*pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == T_##tk)
- #define MATCH(tk) (AT(tk) && ++(*pos))
- #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); }
- #define EXPECT(tk, s) { if (!MATCH(tk)) PARSE_ERROR("expected %s", (s)); }
- node_t *parse_expr(list_t *tokens, size_t *pos);
- list_t *parse_sequence(list_t *tokens, size_t *pos, int term) {
- list_t *seq = list_new();
- do {
- if (term != -1 && *pos < tokens->length && ((token_t *)tokens->data[*pos])->tag == term)
- break;
- list_push(seq, parse_expr(tokens, pos));
- } while (MATCH(COMMA));
- return seq;
- }
- node_t *parse_func(list_t *tokens, size_t *pos, int is_expr);
- node_t *parse_primary(list_t *tokens, size_t *pos) {
- if (MATCH(FUNC))
- return parse_func(tokens, pos, 1);
- else if (MATCH(LPAR)) {
- if (MATCH(RPAR))
- return NODE0(NILTUPLE);
- node_t *a = parse_expr(tokens, pos);
- if (MATCH(COMMA)) {
- list_t *l = list_new();
- list_push(l, a);
- if (!AT(RPAR))
- do {
- node_t *n = parse_expr(tokens, pos);
- list_push(l, n);
- } while (MATCH(COMMA));
- a = NODEL(TUPLE, l);
- }
- EXPECT(RPAR, ")");
- return a;
- } else if (MATCH(LSB)) {
- list_t *a = parse_sequence(tokens, pos, T_RSB);
- EXPECT(RSB, "]");
- return NODEL(LIST, a);
- } else if (MATCH(LCB)) {
- table_t *table = table_new();
- do {
- if (AT(RCB))
- break;
- if (!AT(NAME) && !AT(STRING))
- PARSE_ERROR("expected identifier or string");
- char *key = ((token_t *)tokens->data[(*pos)++])->text;
- EXPECT(COLON, ":");
- node_t *val = parse_expr(tokens, pos);
- table_set(table, key, val);
- } while (MATCH(COMMA));
- EXPECT(RCB, "}");
- return NODEH(TABLE, table);
- } else if (MATCH(NUMBER) || MATCH(STRING) || MATCH(NAME))
- return NODET(LITERAL, tokens->data[(*pos)-1]);
- PARSE_ERROR("expected expression");
- return NULL;
- }
- size_t get_lineno(token_t *tok) {
- size_t line, col;
- traverse(GETSRC(tok->fi), tok->pos, &line, &col);
- return line;
- }
- #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)])))
- node_t *parse_call(list_t *tokens, size_t *pos) {
- node_t *a = parse_primary(tokens, pos);
- do {
- if (!CLIFF && MATCH(LPAR)) {
- list_t *b = NULL;
- if (!AT(RPAR))
- b = parse_sequence(tokens, pos, -1);
- EXPECT(RPAR, ")");
- a = NODE1l(CALL, a, b);
- continue;
- } else if (!CLIFF && MATCH(LSB)) {
- node_t *b = parse_expr(tokens, pos);
- EXPECT(RSB, "]");
- a = NODE2(INDEX, a, b);
- continue;
- } else if (!CLIFF && MATCH(DOT)) {
- if (!AT(NAME))
- PARSE_ERROR("expected identifier after .");
- a = NODE1t(MEMBER, a, tokens->data[(*pos)++]);
- continue;
- }
- break;
- } while (1);
- return a;
- }
- node_t *parse_postfix(list_t *tokens, size_t *pos) {
- node_t *a = parse_call(tokens, pos);
- if (CLIFF)
- return a;
- if (MATCH(PLUSPLUS))
- return NODE1(INC, a);
- else if (MATCH(MINUSMINUS))
- return NODE1(DEC, a);
- return a;
- }
- node_t *parse_unary(list_t *tokens, size_t *pos) {
- if (MATCH(MINUS)) {
- node_t *a = parse_unary(tokens, pos);
- return NODE1(NEGATE, a);
- }
- return parse_postfix(tokens, pos);
- }
- node_t *parse_pow(list_t *tokens, size_t *pos) {
- node_t *a = parse_unary(tokens, pos);
- return a;
- }
- node_t *parse_mul(list_t *tokens, size_t *pos) {
- node_t *a = parse_pow(tokens, pos);
- do {
- if (MATCH(STAR)) {
- node_t *b = parse_pow(tokens, pos);
- a = NODE2(MUL, a, b);
- continue;
- } else if (MATCH(SLASH)) {
- node_t *b = parse_pow(tokens, pos);
- a = NODE2(DIV, a, b);
- continue;
- }
- break;
- } while (1);
- return a;
- }
- node_t *parse_add(list_t *tokens, size_t *pos) {
- node_t *a = parse_mul(tokens, pos);
- do {
- if (MATCH(PLUS)) {
- node_t *b = parse_mul(tokens, pos);
- a = NODE2(ADD, a, b);
- continue;
- } else if (MATCH(MINUS)) {
- node_t *b = parse_mul(tokens, pos);
- a = NODE2(SUB, a, b);
- continue;
- }
- break;
- } while (1);
- return a;
- }
- node_t *parse_shift(list_t *tokens, size_t *pos) {
- node_t *a = parse_add(tokens, pos);
- return a;
- }
- node_t *parse_relation(list_t *tokens, size_t *pos) {
- node_t *a = parse_shift(tokens, pos);
- do {
- if (MATCH(LT)) {
- node_t *b = parse_shift(tokens, pos);
- a = NODE2(LT, a, b);
- continue;
- } else if (MATCH(GT)) {
- node_t *b = parse_shift(tokens, pos);
- a = NODE2(GT, a, b);
- continue;
- }
- break;
- } while (1);
- return a;
- }
- node_t *parse_equality(list_t *tokens, size_t *pos) {
- node_t *a = parse_relation(tokens, pos);
- do {
- if (MATCH(EQUALS)) {
- node_t *b = parse_relation(tokens, pos);
- a = NODE2(EQUALS, a, b);
- continue;
- }
- break;
- } while (1);
- return a;
- }
- node_t *parse_bitand(list_t *tokens, size_t *pos) {
- node_t *a = parse_equality(tokens, pos);
- return a;
- }
- node_t *parse_bitxor(list_t *tokens, size_t *pos) {
- node_t *a = parse_bitand(tokens, pos);
- return a;
- }
- node_t *parse_bitor(list_t *tokens, size_t *pos) {
- node_t *a = parse_bitxor(tokens, pos);
- return a;
- }
- node_t *parse_logand(list_t *tokens, size_t *pos) {
- node_t *a = parse_bitor(tokens, pos);
- if (MATCH(ANDAND)) {
- node_t *b = parse_logand(tokens, pos);
- return NODE2(LOGAND, a, b);
- }
- return a;
- }
- node_t *parse_logor(list_t *tokens, size_t *pos) {
- node_t *a = parse_logand(tokens, pos);
- if (MATCH(BARBAR)) {
- node_t *b = parse_logor(tokens, pos);
- return NODE2(LOGOR, a, b);
- }
- return a;
- }
- node_t *parse_assignment(list_t *tokens, size_t *pos);
- node_t *parse_conditional(list_t *tokens, size_t *pos) {
- node_t *a = parse_logor(tokens, pos);
- if (MATCH(QM)) {
- node_t *b = parse_assignment(tokens, pos);
- EXPECT(COLON, ":");
- node_t *c = parse_assignment(tokens, pos);
- return NODE3(IFEXPR, a, b, c);
- }
- return a;
- }
- node_t *parse_assignment(list_t *tokens, size_t *pos) {
- node_t *a = parse_conditional(tokens, pos);
- if (MATCH(ASSIGN)) {
- node_t *b = parse_assignment(tokens, pos);
- return NODE2(ASSIGN, a, b);
- } else if (MATCH(PLUSASSIGN)) {
- node_t *b = parse_assignment(tokens, pos);
- return NODE2(ASSIGN_ADD, a, b);
- }
- return a;
- }
- node_t *parse_expr(list_t *tokens, size_t *pos) {
- return parse_assignment(tokens, pos);
- }
- node_t *parse_stmt(list_t *tokens, size_t *pos);
- node_t *parse_block(list_t *tokens, size_t *pos) {
- EXPECT(LCB, "{");
- list_t *stmts = list_new();
- while (!AT(EOF) && !AT(RCB))
- list_push(stmts, parse_stmt(tokens, pos));
- EXPECT(RCB, "}");
- return NODEL(PROGRAM, stmts);
- }
- #define BLOCK() (MATCH(COLON)?parse_stmt(tokens, pos):parse_block(tokens, pos))
- node_t *parse_if(list_t *tokens, size_t *pos) {
- node_t *a = parse_expr(tokens, pos);
- node_t *b = BLOCK();
- node_t *c = NULL;
- if (MATCH(ELSE))
- c = BLOCK();
- else if (MATCH(ELIF))
- c = parse_if(tokens, pos);
- return NODE3(IF, a, b, c);
- }
- node_t *parse_var(list_t *tokens, size_t *pos) {
- table_t *h = table_new();
- do {
- if(!AT(NAME))
- PARSE_ERROR("expected identifier");
- char *k = ((token_t *)tokens->data[(*pos)++])->text;
- node_t *v = NULL;
- if (MATCH(ASSIGN))
- v = parse_expr(tokens, pos);
- table_set(h, k, v);
- } while (MATCH(COMMA));
- return NODEH(VAR, h);
- }
- node_t *parse_func(list_t *tokens, size_t *pos, int is_expr) {
- token_t *name = NULL;
- if (!is_expr) {
- if(!AT(NAME))
- PARSE_ERROR("expected identifier");
- name = tokens->data[(*pos)++];
- }
- EXPECT(LPAR, "(");
- table_t *params = NULL;
- if (!AT(RPAR)) {
- int flag = 0;
- params = table_new();
- size_t argc = 0;
- do {
- if(!AT(NAME))
- PARSE_ERROR("expected identifier");
- char *l = ((token_t *)tokens->data[(*pos)++])->text;
- node_t *r = NULL;
- if (!flag && AT(ASSIGN))
- flag = 1;
- if (flag) {
- EXPECT(ASSIGN, "=");
- r = parse_expr(tokens, pos);
- }
- list_t *pair = list_new();
- size_t *argcp = malloc(sizeof(size_t));
- memcpy(argcp, &argc, sizeof(size_t));
- argc++;
- list_push(pair, argcp);
- list_push(pair, r);
- table_set(params, l, pair);
- } while (MATCH(COMMA));
- }
- EXPECT(RPAR, ")");
- table_t *captured = NULL;
- if (MATCH(USE)) {
- EXPECT(RPAR, "(");
- captured = table_new();
- do {
- if(!AT(NAME))
- PARSE_ERROR("expected identifier");
- token_t *name = tokens->data[(*pos)++];
- table_set(captured, name->text, NODET(LITERAL, name));
- } while (MATCH(COMMA));
- EXPECT(RPAR, ")");
- }
- node_t *body = BLOCK();
- if (is_expr)
- return NODEF(FUNCEXPR, NULL, params, captured, body);
- return NODEF(FUNCDEF, name, params, captured, body);
- }
- node_t *parse_stmt(list_t *tokens, size_t *pos) {
- if (MATCH(LCB)) {
- list_t *stmts = list_new();
- while (!AT(EOF) && !AT(RCB)) {
- node_t *n = parse_stmt(tokens, pos);
- MATCH(SEMI);
- list_push(stmts, n);
- }
- EXPECT(RCB, "}");
- return NODEL(BLOCK, stmts);
- } else if (MATCH(VAR))
- return parse_var(tokens, pos);
- else if (MATCH(IF))
- return parse_if(tokens, pos);
- else if (MATCH(FOR)) {
- node_t *a = NULL;
- node_t *b = NULL;
- node_t *c = NULL;
- if (!AT(LCB) && !AT(COLON)) {
- if (MATCH(VAR)) {
- a = parse_var(tokens, pos);
- EXPECT(SEMI, ";");
- b = parse_expr(tokens, pos);
- EXPECT(SEMI, ";");
- c = parse_expr(tokens, pos);
- } else a = parse_expr(tokens, pos);
- }
- node_t *d = BLOCK();
- return NODE4(FOR, a, b, c, d);
- } else if (MATCH(BREAK)) return NODE0(BREAK);
- else if (MATCH(CONTINUE)) return NODE0(CONTINUE);
- else if (MATCH(FUNC))
- return parse_func(tokens, pos, 0);
- else if (MATCH(RETURN)) {
- node_t *a = NULL;
- if (!AT(RCB) && !AT(EOF) && !CLIFF)
- a = parse_expr(tokens, pos);
- return NODE1(RETURN, a);
- } else if (MATCH(DEFER)) {
- node_t *a;
- if (AT(LCB) || AT(COLON))
- a = BLOCK();
- else a = parse_stmt(tokens, pos);
- return NODE1(DEFER, a);
- } else if (MATCH(PASS)) return NODE0(PASS);
- else if (MATCH(REQUIRE)) {
- if (!AT(STRING))
- PARSE_ERROR("expected string");
- token_t *path = tokens->data[(*pos)++];
- return NODET(REQUIRE, path);
- }
- node_t *n = parse_expr(tokens, pos);
- return NODE1(EXPRSTMT, n);
- }
- node_t *parse_program(list_t *tokens, size_t *pos) {
- list_t *stmts = list_new();
- while (*pos < tokens->length) {
- node_t *n = parse_stmt(tokens, pos);
- MATCH(SEMI);
- list_push(stmts, n);
- }
- return NODEL(PROGRAM, stmts);
- }
- node_t *parse(char *source) {
- size_t pos = 0;
- return parse_program(tokenize(source), &pos);
- }
- #define NEWGID() size_t gid = GID++
-
- #define EMIT(fmt, ...) buffer_fmt(buf, (fmt), ##__VA_ARGS__);
- #define BINOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, lstk, node->a); EMIT(", "); compile_node(gbuf, buf, ctx, lstk, node->b); EMIT(")"); }
- #define UNOP(s) { EMIT("qi_" s "(state, "); compile_node(gbuf, buf, ctx, lstk, node->a); EMIT(")"); }
- #define ASSIGN(lhs, rhs) {\
- if ((lhs)->tag == N_LITERAL && (lhs)->t->tag == T_NAME) {\
- EMIT("qi_set(state, false, \"%s\", ", (lhs)->t->text);\
- rhs;\
- EMIT(")");\
- } else if ((lhs)->tag == N_INDEX) {\
- EMIT("qi_index_set(state, false, ");\
- compile_node(gbuf, buf, ctx, lstk, (lhs)->a);\
- EMIT(", ");\
- compile_node(gbuf, buf, ctx, lstk, (lhs)->b);\
- EMIT(", ");\
- rhs;\
- EMIT(")");\
- } else if ((lhs)->tag == N_MEMBER) {\
- EMIT("qi_index_set(state, false, ");\
- compile_node(gbuf, buf, ctx, lstk, (lhs)->a);\
- EMIT(", qi_make_string(state, \"%s\"), ", (lhs)->t->text);\
- rhs;\
- EMIT(")");\
- } else COMPILE_ERROR("illegal assignment left-hand side");\
- }
- #define COMPASSIGN(lhs, s, rhs) {\
- ASSIGN(node->a, {\
- EMIT("qi_%s(state, ", s);\
- compile_node(gbuf, buf, ctx, lstk, (lhs));\
- EMIT(", ");\
- rhs;\
- EMIT(")");\
- });\
- }
- #define COMPILE_ERROR(fmt, ...) { format_error(GETFNAME(node->fi), GETSRC(node->fi), node->pos, fmt, ##__VA_ARGS__); exit(1); }
- void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, node_t *node);
- void compile_list(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *seq) {
- if (!seq || seq->length < 1) {
- EMIT("NULL");
- return;
- }
- buffer_t *tbuf = buffer_new();
- NEWGID();
- buffer_fmt(tbuf, "qi_list_t *__list%d(qi_state_t *state) {\n", gid);
- buffer_fmt(tbuf, "qi_list_t *list = qi_list_make();\n");
- for (size_t i = 0; i < seq->length; i++) {
- buffer_fmt(tbuf, "qi_list_push(list, ");
- compile_node(gbuf, tbuf, ctx, lstk, seq->data[i]);
- buffer_fmt(tbuf, ");\n");
- }
- buffer_fmt(tbuf, "return list;\n");
- buffer_fmt(tbuf, "}\n");
- buffer_appendb(gbuf, tbuf);
- EMIT("__list%d(state)", gid);
- }
- void compile_table(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, table_t *table) {
- if (!table || table->used < 1) {
- EMIT("NULL");
- return;
- }
- buffer_t *tbuf = buffer_new();
- NEWGID();
- buffer_fmt(tbuf, "qi_table_t *__table%d(qi_state_t *state) {\n", gid);
- buffer_fmt(tbuf, "qi_table_t *table = qi_table_make();\n");
- table_iterate(table, {
- buffer_fmt(tbuf, "qi_table_set(table, \"%s\", ", entry.key);
- compile_node(gbuf, tbuf, ctx, lstk, entry.value);
- buffer_fmt(tbuf, ");\n");
- });
- buffer_fmt(tbuf, "return table;\n");
- buffer_fmt(tbuf, "}\n");
- buffer_appendb(gbuf, tbuf);
- EMIT("__table%d(state)", gid);
- }
- #define CTXPUSH(s) list_push(ctx, (s))
- #define CTXPOP() list_pop(ctx)
- int in_context(list_t *ctx, char *s) {
- if (!ctx->length)
- return 0;
- for (ssize_t i = ctx->length - 1; i >= 0; i--) {
- if (strcmp(ctx->data[i], "gap") == 0)
- break;
- else if (strcmp(ctx->data[i], s) == 0)
- return 1;
- }
- return 0;
- }
- size_t scopes_count(list_t *ctx) {
- if (!ctx->length)
- return 0;
- size_t k = 0;
- for (ssize_t i = ctx->length - 1; i >= 0; i--) {
- if (strcmp(ctx->data[i], "gap") == 0)
- break;
- else if (strcmp(ctx->data[i], "scope") == 0)
- k++;
- }
- return k;
- }
- #define INCTX(s) (in_context(ctx, (s)))
- #define SCOPESK (scopes_count(ctx))
- #define LPUSH(i) stack_push(lstk, (i))
- #define LPOP() stack_pop(lstk)
- #define LID (lstk->data[lstk->length-1])
- void compile_func(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, node_t *node) {
- NEWGID();
- buffer_t *tbuf = buffer_new();
- buffer_fmt(tbuf, "qi_value_t *__func%d(qi_state_t *state, qi_size_t pargc, qi_list_t *pargs) {\n", gid);
- CTXPUSH("gap");
- CTXPUSH("func");
- size_t optargc = 0;
- if (node->h) {
- table_iterate(node->h, {
- list_t *pair = entry.value;
- size_t argc = *(size_t *)pair->data[0];
- if (pair->data[1]) {
- optargc++;
- buffer_fmt(tbuf, "qi_set(state, false, \"%s\", pargc >= %d? qi_list_index(pargs, %d): ", entry.key, argc+1, argc);
- compile_node(gbuf, tbuf, ctx, lstk, pair->data[1]);
- buffer_fmt(tbuf, ");\n");
- } else
- buffer_fmt(tbuf, "qi_set(state, false, \"%s\", qi_list_index(pargs, %d));\n", entry.key, argc);
- argc++;
- });
- }
- compile_node(gbuf, tbuf, ctx, lstk, node->a);
- CTXPOP();
- CTXPOP();
- buffer_fmt(tbuf, "return state->nil;\n");
- buffer_fmt(tbuf, "}\n");
- buffer_appendb(gbuf, tbuf);
- tbuf = buffer_new();
- 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);
- compile_table(gbuf, tbuf, ctx, lstk, node->h2);
- buffer_fmt(tbuf, ")");
- if (node->tag == N_FUNCEXPR) {
- buffer_appendb(buf, tbuf);
- return;
- }
- EMIT("qi_set(state, false, \"%s\", ", node->t->text);
- buffer_appendb(buf, tbuf);
- EMIT(");");
- }
- void compile_block(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, list_t *block) {
- for (size_t i = 0; i < block->length; i++) {
- node_t *n = block->data[i];
- if (n->tag == N_FUNCDEF) {
- compile_func(gbuf, buf, ctx, lstk, n);
- EMIT("\n");
- } else if (n->tag == N_VAR) {
- table_iterate(n->h, {
- EMIT("qi_decl(state, \"%s\", ", entry.key);
-
- if (entry.value)
- compile_node(gbuf, buf, ctx, lstk, entry.value);
- else EMIT("state->nil");
- EMIT(");\n");
- });
- }
- }
- for (size_t i = 0; i < block->length; i++) {
- compile_node(gbuf, buf, ctx, lstk, block->data[i]);
- EMIT("\n");
- }
- }
- void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctk, stack_t *lstk);
- void compile_node(buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk, node_t *node) {
- switch (node->tag) {
- case N_PROGRAM:
- compile_block(gbuf, buf, ctx, lstk, node->l);
- break;
- case N_EXPRSTMT:
- EMIT("(void)(");
- compile_node(gbuf, buf, ctx, lstk, node->a);
- EMIT(");");
- break;
- case N_BLOCK:
- CTXPUSH("scope");
- EMIT("qi_new_scope(state);\n");
- compile_block(gbuf, buf, ctx, lstk, node->l);
- EMIT("qi_old_scope(state);");
- CTXPOP();
- break;
- case N_LITERAL:
- switch (node->t->tag) {
- case T_NUMBER:
- EMIT("qi_make_number(state, %s)", node->t->text);
- break;
- case T_STRING:
- if (!*(node->t->text)) {
- EMIT("state->empty_string");
- } else {
- EMIT("qi_make_string(state, \"%s\")", node->t->text);
- }
- break;
- case T_NAME:
- EMIT("qi_get(state, \"%s\")", node->t->text);
- break;
- default:
- COMPILE_ERROR("not yet implemented");
- }
- break;
- case N_LIST:
- EMIT("qi_make_list(state, ");
- compile_list(gbuf, buf, ctx, lstk, node->l);
- EMIT(")");
- break;
- case N_TUPLE:
- EMIT("qi_make_tuple(state, ");
- compile_list(gbuf, buf, ctx, lstk, node->l);
- EMIT(")");
- break;
- case N_NILTUPLE: EMIT("state->empty_tuple"); break;
- case N_TABLE:
- EMIT("qi_make_table(state, ");
- compile_table(gbuf, buf, ctx, lstk, node->h);
- EMIT(")");
- break;
- case N_CALL:
- EMIT("qi_call(state, ");
- compile_node(gbuf, buf, ctx, lstk, node->a);
- EMIT(", ");
- compile_list(gbuf, buf, ctx, lstk, node->l);
- EMIT(")");
- break;
- case N_MEMBER:
- EMIT("qi_index(state, ");
- compile_node(gbuf, buf, ctx, lstk, node->a);
- EMIT(", qi_make_string(state, \"%s\"))", node->t->text);
- break;
- case N_INDEX:
- EMIT("qi_index(state, ");
- compile_node(gbuf, buf, ctx, lstk, node->a);
- EMIT(", ");
- compile_node(gbuf, buf, ctx, lstk, node->b);
- EMIT(")");
- break;
- case N_ASSIGN: ASSIGN(node->a, compile_node(gbuf, buf, ctx, lstk, node->b)); break;
- case N_ASSIGN_ADD: COMPASSIGN(node->a, "add", compile_node(gbuf, buf, ctx, lstk, node->b)); break;
- case N_INC:
- COMPASSIGN(node->a, "add", EMIT("state->one"));
- break;
- case N_DEC:
- COMPASSIGN(node->a, "sub", EMIT("state->one"));
- break;
- case N_VAR: break;
- case N_IF:
- EMIT("if (_qi_truthy(state, ");
- compile_node(gbuf, buf, ctx, lstk, node->a);
- EMIT(")) {\n");
- CTXPUSH("scope");
- EMIT("qi_new_scope(state);\n");
- compile_node(gbuf, buf, ctx, lstk, node->b);
- EMIT("qi_old_scope(state);\n");
- CTXPOP();
- if (node->c) {
- EMIT("} else {\n");
- CTXPUSH("scope");
- EMIT("qi_new_scope(state);\n");
- compile_node(gbuf, buf, ctx, lstk, node->c);
- EMIT("qi_old_scope(state);\n");
- CTXPOP();
- }
- EMIT("}");
- break;
- case N_FOR: {
- NEWGID();
- CTXPUSH("scope");
- EMIT("qi_new_scope(state);\n");
- if (!node->a) {
- EMIT("for (;;) {\n");
- } else if (node->a && !node->b) {
- EMIT("while (_qi_truthy(state, ");
- compile_node(gbuf, buf, ctx, lstk, node->a);
- EMIT(")) {\n");
- } else {
- compile_node(gbuf, buf, ctx, lstk, node->a);
- EMIT("while (_qi_truthy(state, ");
- compile_node(gbuf, buf, ctx, lstk, node->b);
- EMIT(")) {\n");
- }
- LPUSH(gid);
- CTXPUSH("for");
- compile_node(gbuf, buf, ctx, lstk, node->d);
- CTXPOP();
- LPOP();
- EMIT("__continue%d:;\n", gid);
- if (node->c)
- compile_node(gbuf, buf, ctx, lstk, node->c);
- EMIT("}\n");
- EMIT("__break%d:;\n", gid);
- EMIT("qi_old_scope(state);\n");
- CTXPOP();
- } break;
- case N_BREAK:
- if (!INCTX("for"))
- COMPILE_ERROR("break outside of a loop");
- EMIT("goto __break%d;", LID);
- break;
- case N_CONTINUE:
- if (!INCTX("for"))
- COMPILE_ERROR("continue outside of a loop");
- EMIT("goto __continue%d;", LID);
- break;
- case N_DEFER: {
- NEWGID();
- buffer_t *tbuf = buffer_new();
- buffer_fmt(tbuf, "void __defer%d(qi_state_t *state) {\n", gid);
- CTXPUSH("gap");
- compile_node(gbuf, tbuf, ctx, lstk, node->a);
- CTXPOP();
- buffer_fmt(tbuf, "\n");
- buffer_fmt(tbuf, "}\n");
- buffer_appendb(gbuf, tbuf);
- EMIT("qi_add_defer(state, -1, __defer%d);", gid);
- } break;
- case N_RETURN:
- if (!INCTX("func"))
- COMPILE_ERROR("return outside of a function");
- for (size_t i = 0; i < SCOPESK; i++)
- EMIT("qi_old_scope(state);\n");
- EMIT("return ");
- if (node->a)
- compile_node(gbuf, buf, ctx, lstk, node->a);
- else EMIT("state->nil");
- EMIT(";");
- break;
- case N_FUNCDEF: break;
- case N_PASS: break;
- case N_REQUIRE: {
- char *path = node->t->text;
- if (is_required(path))
- break;
- FILE *fd = fopen(path, "rb");
- if (!fd)
- COMPILE_ERROR("failed to open: `%s'", path);
- buffer_t *fbuf = buffer_new();
- for (;;) {
- char line[512];
- if (!fgets(line, sizeof(line), fd))
- break;
- buffer_appends(fbuf, line);
- }
- char *source = buffer_read(fbuf);
- list_t *pair = list_new();
- list_push(pair, path);
- list_push(pair, source);
- list_push(FILES, pair);
- compile_into(source, gbuf, buf, ctx, lstk);
- list_pop(FILES);
- } break;
- case N_IFEXPR:
- EMIT("(_qi_truthy(state, ");
- compile_node(gbuf, buf, ctx, lstk, node->a);
- EMIT(")? ");
- compile_node(gbuf, buf, ctx, lstk, node->b);
- EMIT(": ");
- compile_node(gbuf, buf, ctx, lstk, node->c);
- EMIT(")");
- break;
- case N_FUNCEXPR:
- compile_func(gbuf, buf, ctx, lstk, node);
- break;
- case N_EQUALS:
- BINOP("equals");
- break;
- case N_LT:
- BINOP("lt");
- break;
- case N_GT:
- BINOP("gt");
- break;
- case N_ADD:
- BINOP("add");
- break;
- case N_SUB:
- BINOP("sub");
- break;
- case N_MUL:
- BINOP("mul");
- break;
- case N_DIV:
- BINOP("div");
- break;
- case N_NEGATE:
- UNOP("negate");
- break;
- default:
- COMPILE_ERROR("not yet implemented");
- }
- }
- void compile_into(char *source, buffer_t *gbuf, buffer_t *buf, list_t *ctx, stack_t *lstk) {
- node_t *n = parse(source);
- compile_node(gbuf, buf, ctx, lstk, n);
- }
- char *compile(char *source) {
- list_t *ctx = list_new();
- stack_t *lstk = stack_new();
-
- buffer_t *gbuf = buffer_new();
- buffer_appends(gbuf, "#include <qirt.h>\n");
- buffer_t *buf = buffer_new();
- compile_into(source, gbuf, buf, ctx, lstk);
- buffer_t *rbuf = buffer_new();
- buffer_appendb(rbuf, gbuf);
- buffer_appends(rbuf, "int main(int argc, char **argv) {\n");
- buffer_appends(rbuf, "qi_state_t *state;\n");
- buffer_appends(rbuf, "qi_state_init(&state);\n");
- buffer_appendb(rbuf, buf);
- buffer_appends(rbuf, "qi_old_scope(state);\n");
- buffer_appends(rbuf, "qi_finalize();\n");
- buffer_appends(rbuf, "return 0;\n");
- buffer_appends(rbuf, "}\n");
-
- return buffer_read(rbuf);
- }
- char *compile_file(char *filename, FILE *fd) {
- buffer_t *buf = buffer_new();
- for (;;) {
- char line[512];
- if (!fgets(line, sizeof(line), fd))
- break;
- buffer_appends(buf, line);
- }
- char *source = buffer_read(buf);
- list_t *pair = list_new();
- list_push(pair, filename);
- list_push(pair, source);
- list_push(FILES, pair);
- char *out = compile(source);
- list_pop(FILES);
- return out;
- }
- int main(int argc, char **argv) {
- FILES = list_new();
- char *out = compile_file("<stdin>", stdin);
- fwrite(out, sizeof(char), strlen(out), stdout);
- return 0;
- }
|