summaryrefslogtreecommitdiff
path: root/src
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 /src
parent9cc55f4c51903afbf46a2fbe7725d9aa855f795d (diff)
downloadgolem-04f2e54f7eac3bde22ccb6085bdd7663b125b16a.tar.gz
multidec
Diffstat (limited to 'src')
-rw-r--r--src/main.c63
1 files changed, 45 insertions, 18 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;
}