adding subtypes
This commit is contained in:
60
src/lexer.c
60
src/lexer.c
@@ -1,6 +1,6 @@
|
||||
#include "lexer.h"
|
||||
|
||||
// any single letter is a valid variable, like 'x' or 'y'
|
||||
// token is alphabet letter, like 'x' or 'y'
|
||||
static bool token_is_variable(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
if (ft_isalpha(input[input_pos]))
|
||||
@@ -11,7 +11,7 @@ static bool token_is_variable(const char *input, int input_pos, int *token_size)
|
||||
return false;
|
||||
}
|
||||
|
||||
// number can be int "123"
|
||||
// token is int "123"
|
||||
static bool token_is_number_int(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
int number_size;
|
||||
@@ -51,7 +51,7 @@ static bool token_is_number_int(const char *input, int input_pos, int *token_siz
|
||||
return true;
|
||||
}
|
||||
|
||||
// number can be double "123.456"
|
||||
// token is double "123.456"
|
||||
static bool token_is_number_double(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
int number_size;
|
||||
@@ -98,7 +98,7 @@ static bool token_is_number_double(const char *input, int input_pos, int *token_
|
||||
return true;
|
||||
}
|
||||
|
||||
// power can be '^' and "**"
|
||||
// token is '^' or "**"
|
||||
static bool token_is_power(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
if (input[input_pos] == '^')
|
||||
@@ -114,14 +114,20 @@ static bool token_is_power(const char *input, int input_pos, int *token_size)
|
||||
return false;
|
||||
}
|
||||
|
||||
// sign can be '+' or '-'
|
||||
static bool token_is_sign(const char *input, int input_pos, int *token_size)
|
||||
// token is '+'
|
||||
static bool token_is_sign_plus(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
if (input[input_pos] == '+')
|
||||
{
|
||||
*token_size = 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// token is '-'
|
||||
static bool token_is_sign_minus(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
if (input[input_pos] == '-')
|
||||
{
|
||||
*token_size = 1;
|
||||
@@ -130,14 +136,20 @@ static bool token_is_sign(const char *input, int input_pos, int *token_size)
|
||||
return false;
|
||||
}
|
||||
|
||||
// factor can be '*' or '/' or ':'
|
||||
static bool token_is_factor(const char *input, int input_pos, int *token_size)
|
||||
// token is '*'
|
||||
static bool token_is_factor_multiplication(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
if (input[input_pos] == '*')
|
||||
{
|
||||
*token_size = 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// token is '/' or ':'
|
||||
static bool token_is_factor_division(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
if (input[input_pos] == '/')
|
||||
{
|
||||
*token_size = 1;
|
||||
@@ -151,7 +163,7 @@ static bool token_is_factor(const char *input, int input_pos, int *token_size)
|
||||
return false;
|
||||
}
|
||||
|
||||
// detect a single '='
|
||||
// token is '='
|
||||
static bool token_is_equal(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
if (input[input_pos] == '=')
|
||||
@@ -185,36 +197,55 @@ int lexerize(const char *input, token *tokens)
|
||||
if (token_is_variable(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_VARIABLE;
|
||||
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
|
||||
tokens[tokens_count].value_char = 'x';
|
||||
}
|
||||
else if (token_is_number_int(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_NUMBER_INT;
|
||||
tokens[tokens_count].value_int = ft_atoi(&input[input_pos]);
|
||||
tokens[tokens_count].type = TOKEN_NUMBER;
|
||||
tokens[tokens_count].subtype = TOKEN_NUMBER_INT;
|
||||
tokens[tokens_count].value_double = ft_atof(&input[input_pos]); // we keep info it's an int, but treat it as a double
|
||||
}
|
||||
else if (token_is_number_double(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_NUMBER_DOUBLE;
|
||||
tokens[tokens_count].type = TOKEN_NUMBER;
|
||||
tokens[tokens_count].subtype = TOKEN_NUMBER_DOUBLE;
|
||||
tokens[tokens_count].value_double = ft_atof(&input[input_pos]);
|
||||
}
|
||||
else if (token_is_power(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_POWER;
|
||||
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
|
||||
tokens[tokens_count].value_char = '^';
|
||||
}
|
||||
else if (token_is_sign(input, input_pos, &token_size))
|
||||
else if (token_is_sign_plus(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_SIGN;
|
||||
tokens[tokens_count].subtype = TOKEN_SIGN_PLUS;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_factor(input, input_pos, &token_size))
|
||||
else if (token_is_sign_minus(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_SIGN;
|
||||
tokens[tokens_count].subtype = TOKEN_SIGN_MINUS;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_factor_multiplication(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_FACTOR;
|
||||
tokens[tokens_count].subtype = TOKEN_FACTOR_MULTIPLICATION;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_factor_division(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_FACTOR;
|
||||
tokens[tokens_count].subtype = TOKEN_FACTOR_DIVISION;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_equal(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_EQUAL;
|
||||
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
|
||||
tokens[tokens_count].value_char = '=';
|
||||
}
|
||||
else
|
||||
@@ -231,6 +262,7 @@ int lexerize(const char *input, token *tokens)
|
||||
}
|
||||
|
||||
tokens[tokens_count].type = TOKEN_END;
|
||||
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
|
||||
tokens[tokens_count].value_char = '\0';
|
||||
|
||||
return tokens_count;
|
||||
|
||||
Reference in New Issue
Block a user