split files
This commit is contained in:
52
src/lexer.c
Normal file
52
src/lexer.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "lexer.h"
|
||||
#include "errors.h"
|
||||
|
||||
static int skip_whitespace(const char *input, int input_pos)
|
||||
{
|
||||
while (ft_isspace(input[input_pos]))
|
||||
{
|
||||
input_pos++;
|
||||
}
|
||||
return input_pos;
|
||||
}
|
||||
|
||||
static int token_is_plus(const char *input, int input_pos)
|
||||
{
|
||||
return (input[input_pos] == '+');
|
||||
}
|
||||
|
||||
int lexerize(const char *input, token tokens[MAX_TOKENS])
|
||||
{
|
||||
int token_count = 0;
|
||||
int input_pos = 0;
|
||||
int token_size = 0;
|
||||
|
||||
while (input[input_pos])
|
||||
{
|
||||
input_pos = skip_whitespace(input, input_pos);
|
||||
|
||||
if (input[input_pos] == '\0')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
token_size = token_is_plus(input, input_pos);
|
||||
if (token_size)
|
||||
{
|
||||
tokens[token_count].type = TOKEN_PLUS;
|
||||
tokens[token_count].var_value = '+';
|
||||
}
|
||||
|
||||
if (token_size == 0)
|
||||
{
|
||||
stop_errors(ERROR_UNKNOWN_TOKEN);
|
||||
}
|
||||
token_count++;
|
||||
input_pos += token_size;
|
||||
}
|
||||
|
||||
tokens[token_count].type = TOKEN_END;
|
||||
tokens[token_count].var_value = '\0';
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user