This commit is contained in:
Arthur Barraux
2025-06-12 17:00:19 +02:00
parent 04464ec2e7
commit 5818a8ecfc
14 changed files with 977 additions and 232 deletions
+49 -57
View File
@@ -1,5 +1,8 @@
#include "../include/data.h"
#include "../include/define.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void advance_char(lexer_t *lexer) {
@@ -27,74 +30,63 @@ void skip_comment(lexer_t *lexer) {
}
}
// Node creation functions
node_t *create_node(node_type_t type) {
node_t *create_list_node(void) {
node_t *node = malloc(sizeof(node_t));
if (!node)
if (!node) {
return NULL;
memset(node, 0, sizeof(node_t));
node->type = type;
return node;
}
node_t *create_symbol_node(const char *symbol) {
node_t *node = create_node(NODE_SYMBOL);
if (node) {
strncpy(node->data.symbol, symbol, MAX_SYMBOL_LENGTH - 1);
node->data.symbol[MAX_SYMBOL_LENGTH - 1] = '\0';
}
return node;
}
node_t *create_string_node(const char *string) {
node_t *node = create_node(NODE_STRING);
if (node) {
strncpy(node->data.string, string, MAX_STRING_LENGTH - 1);
node->data.string[MAX_STRING_LENGTH - 1] = '\0';
}
return node;
}
node->type = NODE_LIST;
node->data.list.child_count = 0;
node->data.list.children = NULL;
node_t *create_number_node(double number) {
node_t *node = create_node(NODE_NUMBER);
if (node) {
node->data.number = number;
}
return node;
}
node_t *create_boolean_node(bool value) {
node_t *node = create_node(NODE_BOOLEAN);
if (node) {
node->data.boolean = value;
}
return node;
}
node_t *create_function_ref_node(const char *function_name) {
node_t *node = create_node(NODE_FUNCTION_REF);
if (node) {
strncpy(node->data.function_ref, function_name, MAX_SYMBOL_LENGTH - 1);
node->data.function_ref[MAX_SYMBOL_LENGTH - 1] = '\0';
}
return node;
}
node_t *create_function_call_node(const char *function_name) {
node_t *node = create_node(NODE_FUNCTION_CALL);
if (node) {
strncpy(node->data.call.function_name, function_name,
MAX_SYMBOL_LENGTH - 1);
node->data.call.function_name[MAX_SYMBOL_LENGTH - 1] = '\0';
node->data.call.arg_count = 0;
}
return node;
}
void add_arg_to_call(node_t *call, node_t *arg) {
if (call->type == NODE_FUNCTION_CALL &&
call->data.call.arg_count < MAX_ARGS) {
call->data.call.args =
(node_t **)realloc(call->data.call.args,
(call->data.call.arg_count + 1) * sizeof(node_t *));
call->data.call.args[call->data.call.arg_count++] = arg;
}
}
void add_to_list(node_t *list, node_t *element) {
node_t **new_children = NULL;
if (!list || !element) {
return;
}
// Ensure this is actually a list node
if (list->type != NODE_LIST) {
return;
}
// If this is the first element
if (list->data.list.children == NULL) {
fprintf(stderr, "DEBUG : child number 0 added\n");
list->data.list.children = malloc(sizeof(node_t *));
if (!list->data.list.children) {
return; // Memory allocation failed
}
list->data.list.children[0] = element;
list->data.list.child_count = 1;
return;
}
// Reallocate to add new element
new_children = realloc(list->data.list.children,
sizeof(node_t *) * (list->data.list.child_count + 1));
if (!new_children) {
return; // Memory allocation failed
}
list->data.list.children = new_children;
list->data.list.children[list->data.list.child_count] = element;
list->data.list.child_count++;
fprintf(stderr, "DEBUG : child number %d added\n",
list->data.list.child_count);
}