add declaring and assignment with parser LHS support

This commit is contained in:
2025-06-04 21:20:44 +01:00
parent 9417cbe57a
commit 2bd0384060
17 changed files with 244 additions and 87 deletions

View File

@@ -0,0 +1,30 @@
#include "declaration.h"
#include "../../lexer/token.h"
#include "../../memory.h"
#include "../parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ParsedValue *parse_declaration(char *file, DArray *parsed, DArray *tokens,
size_t *index) {
(*index)++;
error_if_finished(file, tokens, index);
Token *token = darray_get(tokens, *index);
ParsedValue * parsedValue = malloc(sizeof(ParsedValue));
ParsedDeclaration * declaration = malloc(sizeof(ParsedDeclaration));
parsedValue->data = declaration;
parsedValue->type = AST_DECLARATION;
if (token->type != TOKEN_IDENTIFIER) {
fprintf(stderr, "%s:%u:%u error: declaration requires an identifier\n",
file, token->line, token->column);
exit(EXIT_FAILURE);
}
declaration->name = strcpy(checked_malloc(sizeof(token->value)), token->value);
(*index)++;
if ((*index) >= tokens->size) return parsedValue;
token = darray_get(tokens, *index);
return parsedValue;
}

View File

@@ -0,0 +1,16 @@
#ifndef DECLARATION_H
#define DECLARATION_H
#include "../parser.h"
#include "../../lexer/token.h" // for Token
typedef struct {
char * name;
bool is_function;
DArray args; // string[]
ParsedValue * from;
} ParsedDeclaration;
// Function declaration for parsing an identifier
ParsedValue *parse_declaration(char *file, DArray *parsed, DArray *tokens, size_t *index);
#endif // DECLARATION_H