more refactoring and error checking

This commit is contained in:
2026-07-21 20:45:06 +02:00
parent 09b552ed8c
commit 3f04adcd6b
3 changed files with 75 additions and 57 deletions
+14 -10
View File
@@ -164,6 +164,8 @@ int parse_one(Tokens *tokens, Node *node) {
return 1;
}
// equivalent to queue.poll
// TODO: implement a 'da_poll' macro
Token tok = tokens->items[0];
tokens->items++;
tokens->count--;
@@ -171,6 +173,10 @@ int parse_one(Tokens *tokens, Node *node) {
switch (tok.kind) {
case TOKEN_KIND_PAR_LEFT: {
Nodes *nodes = malloc(sizeof(Nodes));
if (nodes == NULL) {
printf("Failed to allocate memory, something is very wrong!\n");
return 1;
}
memset(nodes, 0, sizeof(Nodes));
while (true) {
@@ -182,13 +188,13 @@ int parse_one(Tokens *tokens, Node *node) {
if (tokens->items[0].kind == TOKEN_KIND_PAR_RIGHT) {
tokens->items++;
tokens->count--;
if (tokens->count > 0) {
}
break;
}
parse_one(tokens, node);
int ret = parse_one(tokens, node);
if (ret != 0)
return ret;
nob_da_append(nodes, *node);
}
@@ -213,6 +219,8 @@ int parse_one(Tokens *tokens, Node *node) {
node->as_symbol = tok.data;
} break;
default: {
// unknown token kind, therefore an error
// should I use UNREACHABLE instead?
return 1;
}
}
@@ -242,10 +250,6 @@ int parse(Tokens *tokens, Nodes *nodes) {
Tokens toks = {.count = sub_size,
.capacity = sub_size,
.items = tokens->items + last_idx};
printf("Parsing:\n");
nob_da_foreach(Token, token, &toks) {
printf(" %s\n", token_kind_name(token->kind));
}
Node result = {0};
int ret = parse_one(&toks, &result);
@@ -336,14 +340,14 @@ int eval_one(Node input, Scopes *scopes, Node *result) {
return 0;
}
Nodes *args = malloc(sizeof(Nodes));
Nodes args = {0};
for (size_t i = 1; i < children->count; i++) {
Node n = {0};
int ret = eval_one(children->items[i], scopes, &n);
if (ret != 0) {
return ret;
}
nob_da_append(args, n);
nob_da_append(&args, n);
}
Node func_node = {0};