diff --git a/Makefile b/Makefile index 32b9af2..d4653d2 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ $(NAME): $(OBJS) run: $(NAME) @echo $(YELLOW)"run"$(RESET) - @./$(NAME) "3 * x^2 + 1 * x^1 - 2 * x^0" + @./$(NAME) "3 * x^2 + 1 * x^1 - 2 * x^0 = 5 * x^1" clean: $(RM_OBJS) diff --git a/headers/computorv1.h b/headers/computorv1.h index 3ed57d7..91f70cc 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -1,6 +1,4 @@ #ifndef COMPUTORV1_H #define COMPUTORV1_H -#include "libft.h" - #endif \ No newline at end of file diff --git a/headers/lexer.h b/headers/lexer.h index 84b5253..8402982 100644 --- a/headers/lexer.h +++ b/headers/lexer.h @@ -1,19 +1,16 @@ #ifndef LEXER_H #define LEXER_H -#include "../libft/includes/libft.h" - typedef enum { - TOKEN_VARIABLE, // x, y, etc. - TOKEN_NUMBER_INT, // int - TOKEN_NUMBER_DOUBLE, // double - TOKEN_POWER, // ^ or ** - TOKEN_PLUS, // + - TOKEN_MINUS, // - - TOKEN_MULTIPLICATION, // * - TOKEN_DIVISION, // / - TOKEN_END // null (end of input) + TOKEN_VARIABLE, // x, y, etc. + TOKEN_NUMBER_INT, // int + TOKEN_NUMBER_DOUBLE, // double + TOKEN_POWER, // ^ or ** + TOKEN_SIGN, // + or - + TOKEN_FACTOR, // * or / + TOKEN_EQUAL, // = + TOKEN_END // null (end of input) } token_type; typedef struct diff --git a/src/computorv1.c b/src/computorv1.c index 1aa490f..b425602 100644 --- a/src/computorv1.c +++ b/src/computorv1.c @@ -1,4 +1,5 @@ #include "computorv1.h" +#include "libft.h" #include "lexer.h" #include "errors.h" #include // tmp for float debug @@ -79,14 +80,12 @@ int main(int ac, char **av) ft_printf("%20s", "TOKEN_NUMBER_DOUBLE"); if (tokens[i].type == TOKEN_POWER) ft_printf("%20s", "TOKEN_POWER"); - if (tokens[i].type == TOKEN_PLUS) - ft_printf("%20s", "TOKEN_PLUS"); - if (tokens[i].type == TOKEN_MINUS) - ft_printf("%20s", "TOKEN_MINUS"); - if (tokens[i].type == TOKEN_MULTIPLICATION) - ft_printf("%20s", "TOKEN_MULTIPLICATION"); - if (tokens[i].type == TOKEN_DIVISION) - ft_printf("%20s", "TOKEN_DIVISION"); + if (tokens[i].type == TOKEN_SIGN) + ft_printf("%20s", "TOKEN_SIGN"); + if (tokens[i].type == TOKEN_FACTOR) + ft_printf("%20s", "TOKEN_FACTOR"); + if (tokens[i].type == TOKEN_EQUAL) + ft_printf("%20s", "TOKEN_EQUAL"); if (tokens[i].type == TOKEN_END) ft_printf("%20s", "TOKEN_END"); diff --git a/src/lexer.c b/src/lexer.c index 02b50f0..788f57c 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -1,8 +1,9 @@ #include "lexer.h" +#include "libft.h" #include "errors.h" #include -// any single letter is a valid variable, like "x" or "y" +// any single letter is a valid variable, like 'x' or 'y' static bool token_is_variable(const char *input, int input_pos, int *token_size) { if (ft_isalpha(input[input_pos])) @@ -100,7 +101,7 @@ static bool token_is_number_double(const char *input, int input_pos, int *token_ return true; } -// power can be "^" and "**" +// power can be '^' and "**" static bool token_is_power(const char *input, int input_pos, int *token_size) { if (input[input_pos] == '^') @@ -116,10 +117,10 @@ static bool token_is_power(const char *input, int input_pos, int *token_size) return false; } -// detect a single "+" is valid -static bool token_is_plus(const char *input, int input_pos, int *token_size) +// sign can be '+' or '-' +static bool token_is_sign(const char *input, int input_pos, int *token_size) { - if (input[input_pos] == '+') + if (input[input_pos] == '+' || input[input_pos] == '-') { *token_size = 1; return true; @@ -127,10 +128,10 @@ static bool token_is_plus(const char *input, int input_pos, int *token_size) return false; } -// detect a single "-" -static bool token_is_minus(const char *input, int input_pos, int *token_size) +// factor can be '*' or '/' or ':' +static bool token_is_factor(const char *input, int input_pos, int *token_size) { - if (input[input_pos] == '-') + if (input[input_pos] == '*' || input[input_pos] == '/' || input[input_pos] == ':') { *token_size = 1; return true; @@ -138,21 +139,10 @@ static bool token_is_minus(const char *input, int input_pos, int *token_size) return false; } -// detect a single "*" -static bool token_is_multiplication(const char *input, int input_pos, int *token_size) +// detect a single '=' +static bool token_is_equal(const char *input, int input_pos, int *token_size) { - if (input[input_pos] == '*') - { - *token_size = 1; - return true; - } - return false; -} - -// detect a single "/" -static bool token_is_division(const char *input, int input_pos, int *token_size) -{ - if (input[input_pos] == '+') + if (input[input_pos] == '=') { *token_size = 1; return true; @@ -200,25 +190,20 @@ void lexerize(const char *input, token *tokens) tokens[token_count].type = TOKEN_POWER; tokens[token_count].value_char = '^'; } - else if (token_is_plus(input, input_pos, &token_size)) + else if (token_is_sign(input, input_pos, &token_size)) { - tokens[token_count].type = TOKEN_PLUS; - tokens[token_count].value_char = '+'; + tokens[token_count].type = TOKEN_SIGN; + tokens[token_count].value_char = input[input_pos]; } - else if (token_is_minus(input, input_pos, &token_size)) + else if (token_is_factor(input, input_pos, &token_size)) { - tokens[token_count].type = TOKEN_MINUS; - tokens[token_count].value_char = '-'; + tokens[token_count].type = TOKEN_FACTOR; + tokens[token_count].value_char = input[input_pos]; } - else if (token_is_multiplication(input, input_pos, &token_size)) + else if (token_is_equal(input, input_pos, &token_size)) { - tokens[token_count].type = TOKEN_MULTIPLICATION; - tokens[token_count].value_char = '*'; - } - else if (token_is_division(input, input_pos, &token_size)) - { - tokens[token_count].type = TOKEN_DIVISION; - tokens[token_count].value_char = '/'; + tokens[token_count].type = TOKEN_EQUAL; + tokens[token_count].value_char = '='; } else {