summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-17 06:53:27 +0200
committerkdx <kikoodx@paranoici.org>2023-06-17 06:53:27 +0200
commit04f2e54f7eac3bde22ccb6085bdd7663b125b16a (patch)
tree6ad4ff23af8f7433f5afb31cb39e1cf16708004e
parent9cc55f4c51903afbf46a2fbe7725d9aa855f795d (diff)
downloadgolem-04f2e54f7eac3bde22ccb6085bdd7663b125b16a.tar.gz
multidec
-rw-r--r--src/main.c63
-rwxr-xr-xtesting.sh5
2 files changed, 49 insertions, 19 deletions
diff --git a/src/main.c b/src/main.c
index bfaeee8..aa23df3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -132,6 +132,8 @@ expect(const Token *tok, TokenType type)
static bool
equal(const Token *tok, const char *op)
{
+ if (tok == NULL)
+ return false;
return memcmp(tok->loc, op, tok->len) == 0 && op[tok->len] == '\0';
}
@@ -490,41 +492,61 @@ static Node *
global(Token **rest, Token *tok)
{
tok = skip(tok, "global");
- Node *const node = new_word(tok);
- node->type = NOD_GLOBAL;
+ expect(tok, TOK_WORD);
- tok = tok->next;
+ Node head = {0};
+ Node *node = &head;
+ while (tok->type != TOK_EOF && !equal(tok, ";")) {
+ Node *const cur = new_word(tok);
+ node = node->next = cur;
+ cur->type = NOD_GLOBAL;
- if (equal(tok, "[")) {
tok = tok->next;
- node->lhs = expr(&tok, tok);
- tok = skip(tok, "]");
- }
- if (equal(tok, "=")) {
- tok = tok->next;
- node->val = get_number(tok);
- tok = tok->next;
+ if (equal(tok, "[")) {
+ tok = tok->next;
+ cur->lhs = expr(&tok, tok);
+ tok = skip(tok, "]");
+ }
+
+ if (equal(tok, "=")) {
+ tok = tok->next;
+ cur->val = get_number(tok);
+ tok = tok->next;
+ }
+
+ if (equal(tok, ","))
+ tok = tok->next;
}
*rest = skip(tok, ";");
- return node;
+ return head.next;
}
static Node *
local(Token **rest, Token *tok)
{
tok = skip(tok, "local");
- Node *const node = new_unary(NOD_LOCAL, tok, new_word(tok));
- tok = tok->next;
+ expect(tok, TOK_WORD);
- if (equal(tok, "=")) {
+ Node head = {0};
+ Node *node = &head;
+ while (tok->type != TOK_EOF && !equal(tok, ";")) {
+ Node *const cur = new_unary(NOD_LOCAL, tok, new_word(tok));
+ node = node->next = cur;
tok = tok->next;
- node->rhs = expr(&tok, tok);
+
+ if (equal(tok, "=")) {
+ tok = tok->next;
+ cur->rhs = expr(&tok, tok);
+ }
+
+ if (equal(tok, ","))
+ tok = tok->next;
}
*rest = skip(tok, ";");
- return node;
+ return head.next;
}
static Node *
@@ -806,8 +828,11 @@ block_stmt(Token **rest, Token *tok)
Node head = {0};
Node *cur = &head;
- while (tok->type != TOK_EOF && equal(tok, "local"))
+ while (tok->type != TOK_EOF && equal(tok, "local")) {
cur = cur->next = local(&tok, tok);
+ while (cur->next != NULL)
+ cur = cur->next;
+ }
while (tok->type != TOK_EOF && !equal(tok, "}"))
cur = cur->next = stmt(&tok, tok);
node->lhs = head.next;
@@ -1414,6 +1439,8 @@ parse(Token *tok)
if (equal(tok, "global")) {
cur = cur->next = global(&tok, tok);
+ while (cur->next != NULL)
+ cur = cur->next;
continue;
}
diff --git a/testing.sh b/testing.sh
index 21f28ec..c8f4ad3 100755
--- a/testing.sh
+++ b/testing.sh
@@ -19,7 +19,7 @@ test "$1" "main() {'a' - 'b';}"
test "$1" "main() { buzz(); } buzz() { return 42; }"
test "$1" "main() { fizz(50 + buzz() - 3) * 4; } fizz() { return 3; } buzz() { return 42; }"
test "$1" "global abc; main(){ return abc; }"
-test "$1" "global abc = 5; main() { return abc; }"
+test "$1" "global abc = 5; main() { dbg abc; return abc; }"
test "$1" "main() { return abc; } global abc = 5;"
test "$1" "global abc = 5; main(){ inc(); return abc; } inc() { abc = abc + 1; }"
test "$1" "main() { wrt('H'); wrt('e'); wrt('l'); wrt('l'); wrt('o'); wrt('!'); wrt('\n'); }"
@@ -61,4 +61,7 @@ test "$1" "main() { wrt inc('0') |> dec() |> inc() |> inc(); wrt '\n'; } inc(a)
test "$1" "main();"
test "$1" "main() {}"
test "$1" "main() {;;;;;;;;;;;;;;;;;}"
+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; }"
rm -f build/tmp.*