184 lines
4.9 KiB
C
184 lines
4.9 KiB
C
#include "computorv1.h"
|
|
|
|
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';
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
int main(int ac, char **av)
|
|
{
|
|
int i;
|
|
int arg_len;
|
|
int terms_count_prediction;
|
|
char *input;
|
|
|
|
if (ac < 2)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
// tmp debug output
|
|
ft_putstr("-> received args :\n"); // debug
|
|
i = 0;
|
|
while (i < ac)
|
|
{
|
|
ft_putstr(" ");
|
|
ft_putnbr(i);
|
|
ft_putstr(" : ");
|
|
ft_putstr(av[i]);
|
|
ft_putchar('\n');
|
|
i++;
|
|
}
|
|
|
|
input = av[1];
|
|
remove_spaces(input);
|
|
arg_len = ft_strlen(input);
|
|
|
|
ft_putstr("-> input without space : "); // debug
|
|
ft_putstr(input); // debug
|
|
ft_putchar('\n'); // debug
|
|
ft_putstr("-> arg_len : "); // debug
|
|
ft_putnbr(arg_len); // debug
|
|
ft_putchar('\n'); // debug
|
|
|
|
token tokens[arg_len];
|
|
int tokens_count = lexerize(input, tokens);
|
|
|
|
ft_putstr("-> tokens_count : "); // debug
|
|
ft_putnbr(tokens_count); // debug
|
|
ft_putchar('\n'); // debug
|
|
|
|
// tmp debug output
|
|
ft_putchar('\n'); // debug
|
|
i = 0;
|
|
while (tokens[i].type != TOKEN_END)
|
|
{
|
|
ft_printf("token %2i - type : ", i);
|
|
|
|
if (tokens[i].type == TOKEN_VARIABLE)
|
|
ft_printf("%14s%30s", "TOKEN_VARIABLE", "");
|
|
else if (tokens[i].type == TOKEN_NUMBER)
|
|
{
|
|
ft_printf("%14s", "TOKEN_NUMBER");
|
|
if (tokens[i].subtype == TOKEN_NUMBER_INT)
|
|
ft_printf("%30s", "TOKEN_NUMBER_INT");
|
|
else if (tokens[i].subtype == TOKEN_NUMBER_DOUBLE)
|
|
ft_printf("%30s", "TOKEN_NUMBER_DOUBLE");
|
|
}
|
|
else if (tokens[i].type == TOKEN_POWER)
|
|
ft_printf("%14s%30s", "TOKEN_POWER", "");
|
|
else if (tokens[i].type == TOKEN_SIGN)
|
|
{
|
|
ft_printf("%14s", "TOKEN_SIGN");
|
|
if (tokens[i].subtype == TOKEN_SIGN_PLUS)
|
|
ft_printf("%30s", "TOKEN_SIGN_PLUS");
|
|
else if (tokens[i].subtype == TOKEN_SIGN_MINUS)
|
|
ft_printf("%30s", "TOKEN_SIGN_MINUS");
|
|
}
|
|
else if (tokens[i].type == TOKEN_FACTOR)
|
|
{
|
|
ft_printf("%14s", "TOKEN_FACTOR");
|
|
if (tokens[i].subtype == TOKEN_FACTOR_MULTIPLICATION)
|
|
ft_printf("%30s", "TOKEN_FACTOR_MULTIPLICATION");
|
|
else if (tokens[i].subtype == TOKEN_FACTOR_DIVISION)
|
|
ft_printf("%30s", "TOKEN_FACTOR_DIVISION");
|
|
}
|
|
else if (tokens[i].type == TOKEN_EQUAL)
|
|
ft_printf("%14s%30s", "TOKEN_EQUAL", "");
|
|
else if (tokens[i].type == TOKEN_END)
|
|
ft_printf("%14s%30s", "TOKEN_END", "");
|
|
|
|
ft_putstr(" - value : ");
|
|
|
|
if (tokens[i].type == TOKEN_NUMBER)
|
|
{
|
|
printf("%g\n", tokens[i].value_double);
|
|
}
|
|
else
|
|
{
|
|
ft_printf("%c\n", tokens[i].value_char);
|
|
}
|
|
i++;
|
|
}
|
|
ft_putchar('\n'); // debug
|
|
// END tmp debug output
|
|
|
|
terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL
|
|
|
|
ft_putstr("-> terms_count_prediction : "); // debug
|
|
ft_putnbr(terms_count_prediction); // debug
|
|
ft_putchar('\n'); // debug
|
|
|
|
term terms[terms_count_prediction];
|
|
int term_count = parse(tokens, terms, terms_count_prediction);
|
|
|
|
ft_putstr("-> terms_count : "); // debug
|
|
ft_putnbr(term_count); // debug
|
|
ft_putchar('\n'); // debug
|
|
|
|
// tmp debug output
|
|
ft_putchar('\n'); // debug
|
|
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'); // debug
|
|
// END tmp debug output
|
|
|
|
return (0);
|
|
}
|