/* computorv1.c */ #include "computorv1.h" /** * GLOBALS */ char *input_g_err; token *tokens_g_err; /** * PROGRAM */ static void remove_spaces(char *s) { char *read = s; char *write = s; // copy all non-space chars while (*read) { if (!ft_isspace(*read)) { *write++ = *read; } read++; } *write = '\0'; // zero the rest of the buffer while (write != read) { *write++ = '\0'; } } static int count_any_of(const char *s, const char *set) { int count = 0; for (; *s != '\0'; s++) { if (ft_strchr(set, *s) != NULL) { count++; } } return count; } static void token_fill_null(token *tokens, size_t arg_len) { size_t i; i = 0; while (i < arg_len) { tokens[i].type = TOKEN_END; tokens[i].tag = TOKEN_NO_TAG; tokens[i].value_char = '\0'; i++; } } int main(int ac, char **av) { int i; size_t arg_len; int terms_count_prediction; char *input; if (ac < 2) { return 0; } // init input = av[1]; input_g_err = input; remove_spaces(input); arg_len = ft_strlen(input) + 1; // +1 for last END token // lexerize token tokens[arg_len]; tokens_g_err = tokens; token_fill_null(tokens, arg_len); int tokens_count = lexerize(input, tokens); if (tokens_count == 0) { stop_errors(ERROR_TOKEN_COUNT, "lexer returned 0 token"); } // parse terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL term terms[terms_count_prediction]; int term_count = parse(tokens, terms, terms_count_prediction); { stop_errors(ERROR_TERM_COUNT, "parser returned 0 term"); } // DEBUG ft_putstr("-> terms_count_prediction : "); ft_putnbr(terms_count_prediction); ft_putchar('\n'); ft_putstr("-> terms_count : "); ft_putnbr(term_count); ft_putchar('\n'); ft_putchar('\n'); i = 0; while (terms[i].position != TERM_END) { ft_printf("term %2i :\n", i); // position ft_printf(" position : "); if (terms[i].position == TERM_LEFT) ft_printf("%s\n", "TERM_LEFT"); if (terms[i].position == TERM_RIGHT) ft_printf("%s\n", "TERM_RIGHT"); // sign ft_printf(" sign : "); if (terms[i].sign == TERM_PLUS) ft_printf("%s\n", "TERM_PLUS"); if (terms[i].sign == TERM_MINUS) ft_printf("%s\n", "TERM_MINUS"); // coefficient printf(" coefficient : %g\n", terms[i].coefficient); // exponent ft_printf(" exponent : %d\n", terms[i].exponent); i++; } ft_putchar('\n'); return (0); }