diff --git a/Makefile b/Makefile index 835ca44..82b7364 100644 --- a/Makefile +++ b/Makefile @@ -96,8 +96,12 @@ $(NAME): $(OBJS) $(CC) $(OBJS) -o $@ $(LFLAGS) run: $(NAME) - @echo $(YELLOW)"run"$(RESET) - @./$(NAME) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" + @echo $(BLUE)"\n -> run \n"$(RESET) + ./$(NAME) -d "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" + @echo $(BLUE)"\n -> run \n"$(RESET) + ./$(NAME) -d "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x^1" + @echo $(BLUE)"\n -> run \n"$(RESET) + ./$(NAME) -d "3*x^2 + 2*x = 0" clean: $(RM_OBJS) diff --git a/README.md b/README.md index 4cc5cfc..1bcb8ef 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,12 @@ this project uses submodules (maybe recursively), so either : EXPONENT -> double }[] 3. reduce + -> polynom : + { + a -> double + b -> double + c -> double + }[] 4. print reduced form 5. find degree 6. print degree diff --git a/headers/computorv1.h b/headers/computorv1.h index 7583197..19b6a98 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -8,6 +8,16 @@ #include // for va_list #include +/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * COMPUTORV1.C + */ + +typedef enum +{ + MODE_ARGV, // + MODE_STDIN, // +} e_program_mode; + /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * LEXER.C */ @@ -96,6 +106,7 @@ void reduce(s_term *terms, s_polynom *polynom); typedef enum { ERROR_BASE = 1, // start at 1 to avoid exit(0) for errors + ARGUMENTS_ERROR, ERROR_TOKEN_COUNT, ERROR_UNKNOWN_TOKEN, ERROR_NUMBER_TOO_BIG, @@ -126,5 +137,6 @@ extern char *input_g_err; extern s_token *tokens_g_err; extern s_term *terms_g_err; extern s_polynom *polynom_g_err; +extern bool debug_mode; #endif \ No newline at end of file diff --git a/src/computorv1.c b/src/computorv1.c index 239fb42..b50b7ea 100644 --- a/src/computorv1.c +++ b/src/computorv1.c @@ -10,6 +10,7 @@ char *input_g_err; s_token *tokens_g_err; s_term *terms_g_err; s_polynom *polynom_g_err; +bool debug_mode; /** * PROGRAM @@ -80,20 +81,13 @@ static void terms_fill_null(s_term *terms, size_t terms_count_prediction) } } -int main(int ac, char **av) +static void launch_argv(char *input) { int ret; size_t arg_len; size_t terms_count_prediction; - char *input; - - if (ac < 2) - { - return 0; - } // init - input = av[1]; input_g_err = input; remove_spaces(input); @@ -128,6 +122,132 @@ int main(int ac, char **av) // debug print_state(); +} + +static void clean_copy_input(char *input, char *line) +{ + size_t i; + size_t j; + size_t len; + size_t len_trim_end; + size_t len_trim_start; + + len = ft_strlen(line); + + // get len minus : ' | " | \n | + i = len; + while (i > 0) + { + if (ft_strchr("\"\'\n", line[i])) + i--; + if (ft_isspace(line[i])) + i--; + break; + } + len_trim_end = i; + + // get len of leading chars : ' | " | | \n + i = 0; + while (i < len_trim_end) + { + if (ft_strchr("\"\'\n", line[i])) + i++; + if (ft_isspace(line[i])) + i++; + break; + } + len_trim_start = i; + + // copy into input + i = len_trim_start; + j = 0; + while (i <= len_trim_end) + { + input[j] = line[i]; + j++; + i++; + } + input[j] = '\0'; +} + +static void launch_stdin() +{ + char *line; + size_t len; + + line = NULL; + len = 0; + + // get input + getline(&line, &len, stdin); + + // prepare input + char input[len]; + clean_copy_input(input, line); + + // launch input + launch_argv(input); + + // FREE LINE ! + free(line); +} + +int main(int ac, char **av) +{ + char *input; + e_program_mode program_mode; + + debug_mode = false; + + // check arguments + program_mode = MODE_ARGV; + if (ac > 3) + { + stop_errors(ARGUMENTS_ERROR, "too many arguments"); + } + else if (ac == 3) + { + if ((ft_strcmp(av[1], "-d") == 0)) + { + debug_mode = true; + input = av[2]; + } + else if ((ft_strcmp(av[2], "-d") == 0)) + { + debug_mode = true; + input = av[1]; + } + else + { + stop_errors(ARGUMENTS_ERROR, "3rd argument is not a valid flag: '%s'", av[2]); + } + } + else if (ac == 2) + { + if ((ft_strcmp(av[1], "-d") == 0)) + { + debug_mode = true; + program_mode = MODE_STDIN; + } + else + { + input = av[1]; + } + } + else if (ac < 2) + { + program_mode = MODE_STDIN; + } + + // launch calculator + if (program_mode == MODE_ARGV) + { + launch_argv(input); + } + else if (program_mode == MODE_STDIN) + { + launch_stdin(); + } return (0); } diff --git a/src/utils/errors.c b/src/utils/errors.c index 8310c06..da96e44 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -104,6 +104,8 @@ static void print_context_polynom() void print_state() { + if (!debug_mode) + return; ft_dprintf(STDERR_FILENO, "\nSTATE :\n"); if (input_g_err) print_context_input(); @@ -123,6 +125,7 @@ int stop_errors(e_program_error err, const char *details, ...) // map error codes to messages const char *error_messages[ERROR_SENTINEL] = { [ERROR_BASE] = "undefined error", + [ARGUMENTS_ERROR] = "arguments error", [ERROR_UNKNOWN_TOKEN] = "LEXER - unknown token", [ERROR_NUMBER_TOO_BIG] = "LEXER - number is too big", [ERROR_PARSING] = "PARSER - too much terms to parse",