change over to a linked list system and start trying to fix bug which causes the parser to not output anything meaningful due to memory deallocation

This commit is contained in:
2025-05-28 22:51:27 +01:00
parent 43bc7663fc
commit b82e351daf
13 changed files with 465 additions and 176 deletions

View File

@@ -1,47 +1,13 @@
#include <stdlib.h>
#include <string.h>
#include "token.h"
#include <stdio.h>
#include <stdlib.h>
#define INITIAL_CAPACITY 64
TokenStruct* init_token() {
TokenStruct *tokenStruct = malloc(sizeof(TokenStruct));
if (tokenStruct == NULL) {
// handle malloc failure
return NULL;
}
tokenStruct->count = 0;
tokenStruct->capacity = INITIAL_CAPACITY;
tokenStruct->tokens = malloc(sizeof(Token) * INITIAL_CAPACITY);
if (tokenStruct->tokens == NULL) {
// handle malloc failure
free(tokenStruct);
return NULL;
}
return tokenStruct;
}
void add_token(TokenStruct* token,TokenType type, const char* value, int line, int column) {
if (token->count >= token->capacity) {
token->capacity *= 2;
token->tokens = realloc(token->tokens, sizeof(Token) * token->capacity);
}
token->tokens[token->count].type = type;
token->tokens[token->count].value = strdup(value);
token->tokens[token->count].line = line;
token->tokens[token->count].column = column;
token->count++;
}
void free_tokens(TokenStruct* token) {
for (int i = 0; i < token->count; ++i) {
free(token->tokens[i].value);
}
free(token->tokens);
token->tokens = NULL;
token->count = 0;
token->capacity = 0;
free(token);
Token *create_token(TokenType type, int line, int column, char *value) {
Token * token = malloc(sizeof(Token));
printf("%s\n", value);
token->type = type;
token->line=line;
token->column=column;
token->value=value;
return token;
}