better flag handling
This commit is contained in:
16
Makefile
16
Makefile
@@ -96,16 +96,16 @@ $(NAME): $(OBJS)
|
||||
$(CC) $(OBJS) -o $@ $(LFLAGS)
|
||||
|
||||
run: $(NAME)
|
||||
@echo $(BLUE)"\n -> run \n"$(RESET)
|
||||
./$(NAME) -d "3.4 * x^2.3 + 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)
|
||||
@echo $(B_PURPLE)"\n---------------------------------------------\n1. run with flag '-d' as last \n"$(RESET)
|
||||
./$(NAME) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" -d
|
||||
@echo $(B_PURPLE)"\n---------------------------------------------\n2. run without flag \n"$(RESET)
|
||||
./$(NAME) "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x^1"
|
||||
@echo $(B_PURPLE)"\n---------------------------------------------\n3. run with flag '-d' as first \n"$(RESET)
|
||||
./$(NAME) -d "3*x^2 + 2*x = 0"
|
||||
@echo $(BLUE)"\n -> run \n"$(RESET)
|
||||
@echo $(B_PURPLE)"\n---------------------------------------------\n4. run with wrong flag '-e' \n"$(RESET)
|
||||
-./$(NAME) -d -e "3*x^2 + 2*x -7x^4 = x^4"
|
||||
@echo $(B_PURPLE)"\n---------------------------------------------\n5. run \n"$(RESET)
|
||||
./$(NAME) -d "3*x^2 + 2*x -7x^3 = x^3"
|
||||
@echo $(BLUE)"\n -> run \n"$(RESET)
|
||||
./$(NAME) -d "3*x^2 + 2*x -7x^4 = x^4"
|
||||
|
||||
clean:
|
||||
$(RM_OBJS)
|
||||
|
||||
@@ -119,6 +119,7 @@ extern s_token *tokens_g_err;
|
||||
extern s_term *terms_g_err;
|
||||
extern double *polynom_g_err;
|
||||
extern int polynom_len_g_err;
|
||||
extern bool debug_mode;
|
||||
extern bool flag_debug_mode;
|
||||
extern bool flag_loop_mode;
|
||||
|
||||
#endif
|
||||
@@ -6,7 +6,8 @@
|
||||
* GLOBALS
|
||||
*/
|
||||
|
||||
bool debug_mode;
|
||||
bool flag_debug_mode;
|
||||
bool flag_loop_mode;
|
||||
char *input_g_err;
|
||||
s_token *tokens_g_err;
|
||||
s_term *terms_g_err;
|
||||
@@ -231,50 +232,51 @@ static void launch_stdin()
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int i;
|
||||
char *input;
|
||||
e_program_mode program_mode;
|
||||
|
||||
debug_mode = false;
|
||||
// init flags
|
||||
flag_debug_mode = false;
|
||||
flag_loop_mode = false;
|
||||
|
||||
// check arguments
|
||||
program_mode = MODE_ARGV;
|
||||
if (ac > 3)
|
||||
if (ac == 1)
|
||||
{
|
||||
stop_errors("too many arguments");
|
||||
program_mode = MODE_STDIN;
|
||||
}
|
||||
else if (ac == 3)
|
||||
else if (ac > 1)
|
||||
{
|
||||
if ((ft_strcmp(av[1], "-d") == 0))
|
||||
// get flags
|
||||
input = NULL;
|
||||
i = 1;
|
||||
while (i < ac)
|
||||
{
|
||||
debug_mode = true;
|
||||
input = av[2];
|
||||
if ((ft_strcmp(av[i], "-d") == 0))
|
||||
{
|
||||
flag_debug_mode = true;
|
||||
}
|
||||
else if ((ft_strcmp(av[2], "-d") == 0))
|
||||
else if ((ft_strcmp(av[i], "-l") == 0))
|
||||
{
|
||||
debug_mode = true;
|
||||
input = av[1];
|
||||
flag_loop_mode = true;
|
||||
}
|
||||
else if (ft_strlen(av[i]) == 2 && av[i][0] == '-')
|
||||
{
|
||||
stop_errors("unknwon flag '%s'", av[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
stop_errors("3rd argument is not a valid flag: '%s'", av[2]);
|
||||
input = av[i];
|
||||
}
|
||||
i++;
|
||||
}
|
||||
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)
|
||||
// if input was not set, it means interactiv mode
|
||||
if (input == NULL)
|
||||
{
|
||||
program_mode = MODE_STDIN;
|
||||
}
|
||||
}
|
||||
|
||||
// launch calculator
|
||||
if (program_mode == MODE_ARGV)
|
||||
|
||||
@@ -131,7 +131,7 @@ static int get_exponent(s_token *tokens, int i, int *token_count)
|
||||
}
|
||||
else
|
||||
{
|
||||
stop_errors("at exponent place, after a '*' we should have a 'var', but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
stop_errors("at exponent place, after a '*' we should have a 'var', but instead got : '%s' (token number %i)", token_type_to_str(tokens[i].type), i);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -160,7 +160,7 @@ static int get_exponent(s_token *tokens, int i, int *token_count)
|
||||
}
|
||||
else
|
||||
{
|
||||
stop_errors("at exponent place, we should have an int, but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
stop_errors("at exponent place, we should have an int, but instead got : '%s' (token number %i)", token_type_to_str(tokens[i].type), i);
|
||||
}
|
||||
|
||||
return tokens[i].value_int;
|
||||
|
||||
@@ -108,7 +108,7 @@ static void print_context_polynom()
|
||||
|
||||
void print_state()
|
||||
{
|
||||
if (!debug_mode)
|
||||
if (!flag_debug_mode)
|
||||
return;
|
||||
ft_dprintf(STDERR_FILENO, "\nSTATE :\n");
|
||||
if (input_g_err)
|
||||
|
||||
Reference in New Issue
Block a user