summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-18 19:16:56 +0200
committerkdx <kikoodx@paranoici.org>2023-06-18 19:16:56 +0200
commit8a1646b915c7322daea5a1b74cde60e565121cfe (patch)
treec0ea7719bfeb63afd22daee6b5ad9c201adbf966
parent3c0b034ee902432baed7030cc65cb2443a846600 (diff)
downloadgolem-8a1646b915c7322daea5a1b74cde60e565121cfe.tar.gz
inline array
-rw-r--r--src/main.c48
-rwxr-xr-xtesting.sh1
2 files changed, 40 insertions, 9 deletions
diff --git a/src/main.c b/src/main.c
index f235c54..1733b6e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
diff --git a/testing.sh b/testing.sh
index aa3945d..4b3955f 100755
--- a/testing.sh
+++ b/testing.sh
@@ -65,4 +65,5 @@ test "$1" "global a = 8, b, c = 4; main() { dbg a; dbg c; }"
test "$1" "global a, b = 6, c = 4; main() { dbg b; dbg c; }"
test "$1" "main() { local a = 5, b = 7, c; dbg a; dbg b; }"
test "$1" "main() { dbg 5 % 3; }"
+test "$1" "main() { dbg [{1, 2, 3} + 2]; dbg [{1, 2, 3}]; }"
rm -f build/tmp.*