Add resizing to the lexer

This commit is contained in:
XOR 2023-09-20 22:00:47 +02:00
parent 1cf8f9cae2
commit 508bf04793
3 changed files with 32 additions and 11 deletions

View file

@ -1,14 +1,16 @@
#ifndef _LEXER_H_
#define _LEXER_H_
#include "../header/common.h"
#include <stdio.h>
#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
#endif

View file

@ -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], &current_char, 1);
strncat(((char (*)[MAX_TOKEN_SIZE]) tokens->buffer)[token_index], &current_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])

View file

@ -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