add declaration with function support

This commit is contained in:
2025-06-04 23:12:03 +01:00
parent 2bd0384060
commit 7b76b0d888
10 changed files with 125 additions and 17 deletions

View File

@@ -2,11 +2,12 @@
#include "../../lexer/token.h"
#include "../../memory.h"
#include "../parser.h"
#include "../literals/literals.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ParsedValue *parse_declaration(char *file, DArray *parsed, DArray *tokens,
ParsedValue *parse_declaration(char *file, DArray *tokens,
size_t *index) {
(*index)++;
error_if_finished(file, tokens, index);
@@ -14,6 +15,8 @@ ParsedValue *parse_declaration(char *file, DArray *parsed, DArray *tokens,
ParsedValue * parsedValue = malloc(sizeof(ParsedValue));
ParsedDeclaration * declaration = malloc(sizeof(ParsedDeclaration));
declaration->is_function = false;
declaration->from = parse_null();
parsedValue->data = declaration;
parsedValue->type = AST_DECLARATION;
@@ -23,8 +26,51 @@ ParsedValue *parse_declaration(char *file, DArray *parsed, DArray *tokens,
exit(EXIT_FAILURE);
}
declaration->name = strcpy(checked_malloc(sizeof(token->value)), token->value);
(*index)++;
if ((*index) >= tokens->size) return parsedValue;
if ((*index) >= tokens->size) {
return parsedValue;
}
token = darray_get(tokens, *index);
if (token->type == TOKEN_LPAREN) {
declaration->is_function = true;
declaration->parameters = checked_malloc(sizeof(DArray));
darray_init(declaration->parameters, sizeof(char*));
(*index)++;
error_if_finished(file, tokens, index);
while(true) {
token = darray_get(tokens, *index);
if (token->type != TOKEN_IDENTIFIER) {
fprintf(stderr, "%s:%u:%u error: parameter names need to start with a letter or _, only use letters, digits, or _, and can't be keywords.\n",
file, token->line, token->column);
exit(EXIT_FAILURE);
}
darray_push(declaration->parameters, strcpy(checked_malloc(sizeof(token->value)), token->value));
(*index)++;
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
if (token->type == TOKEN_RPAREN) {
(*index)++;
error_if_finished(file, tokens, index);
token = darray_get(tokens, *index);
break;
} else if (token->type != TOKEN_COMMA) {
fprintf(stderr, "%s:%u:%u error: expected comma\n",
file, token->line, token->column);
exit(EXIT_FAILURE);
}
(*index)++;
error_if_finished(file, tokens, index);
}
}
if (token->type != TOKEN_ASSIGN) return parsedValue;
(*index)++;
error_if_finished(file, tokens, index);
free(declaration->from);
declaration->from = parse_token(file, tokens, index, true);
return parsedValue;
}