diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 48 |
1 files changed, 39 insertions, 9 deletions
@@ -293,6 +293,7 @@ typedef enum { NOD_XOR, // ^ NOD_NUM, // integer NOD_STRING, // "" + NOD_ARRAY, // {} NOD_DEREF, // [a] NOD_EMPTY_STMT, NOD_BLOCK_STMT, @@ -709,6 +710,23 @@ fncall(Token **rest, Token *tok) } static Node * +array(Token **rest, Token *tok) +{ + Node *const node = new_node(NOD_ARRAY, tok); + tok = skip(tok, "{"); + + Node *cur = node; + while (tok->type != TOK_EOF && !equal(tok, "}")) { + cur = cur->lhs = expr(&tok, tok); + if (!equal(tok, "}")) + tok = skip(tok, ","); + } + *rest = skip(tok, "}"); + + return node; +} + +static Node * primary(Token **rest, Token *tok) { if (equal(tok, "(")) { @@ -746,6 +764,9 @@ primary(Token **rest, Token *tok) return node; } + if (equal(tok, "{")) + return array(rest, tok); + if (tok->type == TOK_WORD && equal(tok->next, "(")) { /* function call */ Node *node = fncall(&tok, tok); @@ -1008,6 +1029,7 @@ gen_expr(Node *node) gen_deref(node); return; case NOD_STRING: + case NOD_ARRAY: gen_string(node); return; case NOD_REF: @@ -1178,7 +1200,7 @@ gen_string(Node *node) store->next = strings; strings = store; strings_size += 1; - printf("\tLIT ,__str_%x\n", strings_size - 1); + printf("\tLIT ,__arr_%x\n", strings_size - 1); depth += 1; } @@ -1415,14 +1437,22 @@ codegen(Node *node) } for (int i = 0; i < strings_size; i++) { - printf("@__str_%x\n", strings_size - i - 1); - for (int k = 1; k < strings->lhs->len - 1;) { - char *q = strings->lhs->loc + k; - const int c = escaped_char(q, &q); - if (c == '"') /* TODO: handle this */ - error_loc(q - 2, 2, "can't escape double quotes", 0); - printf("\t%04x\n", (unsigned)c); - k = q - strings->lhs->loc; + printf("@__arr_%x\n", strings_size - i - 1); + if (strings->lhs->type == NOD_STRING) { + for (int k = 1; k < strings->lhs->len - 1;) { + char *q = strings->lhs->loc + k; + const int c = escaped_char(q, &q); + if (c == '"') /* TODO: handle this */ + error_loc(q - 2, 2, "can't escape double quotes", 0); + printf("\t%04x\n", (unsigned)c); + k = q - strings->lhs->loc; + } + } else { + Node *cur = strings->lhs->lhs; + while (cur != NULL) { + printf("\t%04x\n", const_expr(cur)); + cur = cur->lhs; + } } printf("\t0000\n"); strings = strings->next; |