From 04f2e54f7eac3bde22ccb6085bdd7663b125b16a Mon Sep 17 00:00:00 2001 From: kdx Date: Sat, 17 Jun 2023 06:53:27 +0200 Subject: multidec --- src/main.c | 63 ++++++++++++++++++++++++++++++++++++++++++++------------------ testing.sh | 5 ++++- 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.* -- cgit v1.2.3