From 508bf04793d47c2fe85fc794007cc5eb80d90df5 Mon Sep 17 00:00:00 2001 From: XOR Date: Wed, 20 Sep 2023 22:00:47 +0200 Subject: [PATCH] Add resizing to the lexer --- header/lexer.h | 8 +++++--- src/lexer.c | 31 +++++++++++++++++++++++++------ src/main.c | 4 ++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/header/lexer.h b/header/lexer.h index 2cb970d..37071d5 100755 --- a/header/lexer.h +++ b/header/lexer.h @@ -1,14 +1,16 @@ #ifndef _LEXER_H_ #define _LEXER_H_ -#include "../header/common.h" #include +#include "../header/common.h" +#include "dyn_buf.h" + //Convert the input_file into tokens -void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE]); +void lexer(FILE *input_file, dynamic_buffer_t *tokens); //Print out all the tokens in tokens void print_tokens(char tokens[][MAX_TOKEN_SIZE]); -#endif \ No newline at end of file +#endif diff --git a/src/lexer.c b/src/lexer.c index a98e62f..aef4249 100755 --- a/src/lexer.c +++ b/src/lexer.c @@ -10,7 +10,7 @@ This code contains the implementations of all functions related to the lexical a #include "../header/common.h" #include "../header/lexer.h" -void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE]) +void lexer(FILE *input_file, dynamic_buffer_t *tokens) { // Stores the current character we are examining char current_char = 0; @@ -38,6 +38,12 @@ void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE]) } } + //Each iteration requires space for one token at maximum + if(tokens->size <= tokens->used + MAX_TOKEN_SIZE) + { + resize_dynamic_buffer(tokens); + } + switch (current_char) { case EOF: @@ -68,7 +74,8 @@ void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE]) // This also is a indice of a new instruction beginning // in the tokens array, instructions are seperated by semicolons - tokens[token_index][0] = ';'; + ((char (*)[MAX_TOKEN_SIZE]) tokens->buffer)[token_index][0] = ';'; + tokens->used += MAX_TOKEN_SIZE; // Since the Instruction seperator (';') is also a token, we need to increase token_index again token_index++; @@ -84,18 +91,30 @@ void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE]) break; default: is_pos_line_start = 0; - strncat(tokens[token_index], ¤t_char, 1); + strncat(((char (*)[MAX_TOKEN_SIZE]) tokens->buffer)[token_index], ¤t_char, 1); + tokens->used += MAX_TOKEN_SIZE; break; } } + // The next block requires a maximum of 3 tokens to be empty + if(tokens->size <= tokens->used + (3 * MAX_TOKEN_SIZE)) + { + resize_dynamic_buffer(tokens); + } + // Add end of tokens marker // If there was no \n at the end of the file, we need to add a ';' if(!is_pos_line_start) { - tokens[token_index + 1][0] = ';'; - tokens[token_index + 2][0] = EOF; + ((char (*)[MAX_TOKEN_SIZE]) tokens->buffer)[token_index + 1][0] = ';'; + ((char (*)[MAX_TOKEN_SIZE]) tokens->buffer)[token_index + 2][0] = EOF; + tokens->used += 2 * MAX_TOKEN_SIZE; + } + else + { + ((char (*)[MAX_TOKEN_SIZE]) tokens->buffer)[token_index][0] = EOF; + tokens->used += MAX_TOKEN_SIZE; } - else tokens[token_index][0] = EOF; } void print_tokens(char tokens[][MAX_TOKEN_SIZE]) diff --git a/src/main.c b/src/main.c index 249ba57..4116873 100755 --- a/src/main.c +++ b/src/main.c @@ -129,14 +129,14 @@ int main(int argc, char **argv) } dynamic_buffer_t label_tokens; - init_dynamic_buffer(&label_tokens, TABLE_INIT_SIZE * MAX_TOKEN_SIZE, TABLE_GROW_SIZE * MAX_TOKEN_SIZE); + init_dynamic_buffer(&label_tokens, 3 * MAX_TOKEN_SIZE, TABLE_GROW_SIZE * MAX_TOKEN_SIZE); if (label_tokens.buffer == NULL) { printf("Error: Could not create the label_tokens array\n"); return EXIT_FAILURE; } - lexer(assembly_file, (char (*)[MAX_TOKEN_SIZE]) label_tokens.buffer); + lexer(assembly_file, &label_tokens); fclose(assembly_file); // Part I of processing labels