diff --git a/Makefile b/Makefile index 9e8ae1a..32b9af2 100644 --- a/Makefile +++ b/Makefile @@ -33,10 +33,14 @@ RESET = "\e[0m" # FILES : NAME = computorv1 D_LIB = ./libft -D_SRCS = ./src D_HEADERS = ./headers -SRCS = computorv1.c -HEADERS = computorv1.h +HEADERS = computorv1.h \ + errors.h \ + lexer.h +D_SRCS = ./src +SRCS = computorv1.c \ + errors.c \ + lexer.c # COMPILATION CONFIG : CC = gcc @@ -49,7 +53,7 @@ D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) VPATH = $(D_SRCS) F_INCLUDES = $(HEADERS:%=$(D_HEADERS)/%) -INCLUDES = -I$(D_HEADERS) +INCLUDES = -I$(D_HEADERS) -I$(D_LIB)/includes ifeq "$(D_OBJS)" "." RM_OBJS = rm -f $(OBJS) else diff --git a/headers/computorv1.h b/headers/computorv1.h index f1ed01a..3ed57d7 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -1,37 +1,6 @@ #ifndef COMPUTORV1_H #define COMPUTORV1_H -#include "../libft/includes/libft.h" - -typedef enum -{ - ERROR_BASIC, // 0 - ERROR_UNKNOWN_TOKEN, // 1 -} program_error; - -typedef enum -{ - TOKEN_PLUS, // + - TOKEN_MINUS, // - - TOKEN_VARIABLE, // x, y, etc. - TOKEN_NUMBER, // int or double - TOKEN_POWER, // ^ or ** - TOKEN_MULTIPLICATION, // * - TOKEN_DIVISION, // / - TOKEN_END // null (end of input) -} TokenType; - -typedef struct -{ - TokenType type; - union - { - double num_value; // For NUMBER - char var_value; // For VARIABLE (single char, e.g., 'x') - }; -} t_token; - -#define MAX_TOKENS 100 -t_token tokens[MAX_TOKENS]; +#include "libft.h" #endif \ No newline at end of file diff --git a/headers/errors.h b/headers/errors.h new file mode 100644 index 0000000..0f7313b --- /dev/null +++ b/headers/errors.h @@ -0,0 +1,14 @@ +#ifndef ERRORS_H +#define ERRORS_H + +#include "../libft/includes/libft.h" + +typedef enum +{ + ERROR_BASIC = 0, + ERROR_UNKNOWN_TOKEN = -1, +} program_error; + +int stop_errors(int err); + +#endif \ No newline at end of file diff --git a/headers/lexer.h b/headers/lexer.h new file mode 100644 index 0000000..7e7da5d --- /dev/null +++ b/headers/lexer.h @@ -0,0 +1,31 @@ +#ifndef LEXER_H +#define LEXER_H + +#include "../libft/includes/libft.h" + +typedef enum +{ + TOKEN_PLUS, // + + TOKEN_MINUS, // - + TOKEN_VARIABLE, // x, y, etc. + TOKEN_NUMBER, // int or double + TOKEN_POWER, // ^ or ** + TOKEN_MULTIPLICATION, // * + TOKEN_DIVISION, // / + TOKEN_END // null (end of input) +} token_type; + +typedef struct +{ + token_type type; + union + { + double num_value; // For NUMBER + char var_value; // For VARIABLE (single char, e.g., 'x') + }; +} token; + +#define MAX_TOKENS 100 +int lexerize(const char *input, token tokens[MAX_TOKENS]); + +#endif \ No newline at end of file diff --git a/src/computorv1.c b/src/computorv1.c index b495bc6..46326b6 100644 --- a/src/computorv1.c +++ b/src/computorv1.c @@ -1,71 +1,6 @@ - #include "computorv1.h" - -int stop_errors(int err) -{ - switch (err) - { - case -ERROR_UNKNOWN_TOKEN: - ft_putstr_fd("error: unknown token\n", STDERR_FILENO); - break; - - default: - ft_putstr_fd("unknown error\n", STDERR_FILENO); - break; - } - - exit(err); -} - -int skip_whitespace(const char *input, int input_pos) -{ - while (ft_isspace(input[input_pos])) - { - input_pos++; - } - return input_pos; -} - -int token_is_plus(const char *input, int input_pos) -{ - return (input[input_pos] == '+'); -} - -int lexerize(const char *input) -{ - 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; - } - - // Add end token - tokens[token_count].type = TOKEN_END; - tokens[token_count].var_value = '\0'; - return 1; -} +#include "lexer.h" +#include "errors.h" int main(int ac, char **av) { @@ -86,7 +21,8 @@ int main(int ac, char **av) i++; } - ret = lexerize(av[1]); + token tokens[MAX_TOKENS]; + ret = lexerize(av[1], tokens); if (ret <= 0) { stop_errors(ret); diff --git a/src/errors.c b/src/errors.c new file mode 100644 index 0000000..2ffb257 --- /dev/null +++ b/src/errors.c @@ -0,0 +1,17 @@ +#include "errors.h" + +int stop_errors(int err) +{ + switch (err) + { + case ERROR_UNKNOWN_TOKEN: + ft_putstr_fd("error: unknown token\n", STDERR_FILENO); + break; + + default: + ft_putstr_fd("unknown error\n", STDERR_FILENO); + break; + } + + exit(err); +} \ No newline at end of file diff --git a/src/lexer.c b/src/lexer.c new file mode 100644 index 0000000..e6fcb0b --- /dev/null +++ b/src/lexer.c @@ -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; +}