improve term count prediction
This commit is contained in:
@@ -23,10 +23,24 @@ void remove_spaces(char *s)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -59,10 +73,10 @@ int main(int ac, char **av)
|
||||
ft_putchar('\n'); // debug
|
||||
|
||||
token tokens[arg_len];
|
||||
int token_count = lexerize(input, tokens);
|
||||
int tokens_count = lexerize(input, tokens);
|
||||
|
||||
ft_putstr("-> token_count : "); // debug
|
||||
ft_putnbr(token_count); // debug
|
||||
ft_putstr("-> tokens_count : "); // debug
|
||||
ft_putnbr(tokens_count); // debug
|
||||
ft_putchar('\n'); // debug
|
||||
|
||||
// tmp debug output
|
||||
@@ -108,10 +122,16 @@ int main(int ac, char **av)
|
||||
ft_putchar('\n'); // debug
|
||||
// END tmp debug output
|
||||
|
||||
term terms[token_count / 2];
|
||||
terms_count_prediction = count_any_of(input, "-+=") + 1; // +1 for first term that can have no leading '+'
|
||||
|
||||
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);
|
||||
|
||||
ft_putstr("-> term_count : "); // debug
|
||||
ft_putstr("-> terms_count : "); // debug
|
||||
ft_putnbr(term_count); // debug
|
||||
ft_putchar('\n'); // debug
|
||||
|
||||
|
||||
40
src/lexer.c
40
src/lexer.c
@@ -152,11 +152,11 @@ static bool token_is_equal(const char *input, int input_pos, int *token_size)
|
||||
*/
|
||||
int lexerize(const char *input, token *tokens)
|
||||
{
|
||||
int token_count;
|
||||
int tokens_count;
|
||||
int input_pos;
|
||||
int token_size;
|
||||
|
||||
token_count = 0;
|
||||
tokens_count = 0;
|
||||
input_pos = 0;
|
||||
while (input[input_pos])
|
||||
{
|
||||
@@ -169,38 +169,38 @@ int lexerize(const char *input, token *tokens)
|
||||
|
||||
if (token_is_variable(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[token_count].type = TOKEN_VARIABLE;
|
||||
tokens[token_count].value_char = 'x';
|
||||
tokens[tokens_count].type = TOKEN_VARIABLE;
|
||||
tokens[tokens_count].value_char = 'x';
|
||||
}
|
||||
else if (token_is_number_int(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[token_count].type = TOKEN_NUMBER_INT;
|
||||
tokens[token_count].value_int = ft_atoi(&input[input_pos]);
|
||||
tokens[tokens_count].type = TOKEN_NUMBER_INT;
|
||||
tokens[tokens_count].value_int = ft_atoi(&input[input_pos]);
|
||||
}
|
||||
else if (token_is_number_double(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[token_count].type = TOKEN_NUMBER_DOUBLE;
|
||||
tokens[token_count].value_double = ft_atof(&input[input_pos]);
|
||||
tokens[tokens_count].type = TOKEN_NUMBER_DOUBLE;
|
||||
tokens[tokens_count].value_double = ft_atof(&input[input_pos]);
|
||||
}
|
||||
else if (token_is_power(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[token_count].type = TOKEN_POWER;
|
||||
tokens[token_count].value_char = '^';
|
||||
tokens[tokens_count].type = TOKEN_POWER;
|
||||
tokens[tokens_count].value_char = '^';
|
||||
}
|
||||
else if (token_is_sign(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[token_count].type = TOKEN_SIGN;
|
||||
tokens[token_count].value_char = input[input_pos];
|
||||
tokens[tokens_count].type = TOKEN_SIGN;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_factor(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[token_count].type = TOKEN_FACTOR;
|
||||
tokens[token_count].value_char = input[input_pos];
|
||||
tokens[tokens_count].type = TOKEN_FACTOR;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_equal(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[token_count].type = TOKEN_EQUAL;
|
||||
tokens[token_count].value_char = '=';
|
||||
tokens[tokens_count].type = TOKEN_EQUAL;
|
||||
tokens[tokens_count].value_char = '=';
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -209,7 +209,7 @@ int lexerize(const char *input, token *tokens)
|
||||
// stop_errors(ERROR_UNKNOWN_TOKEN);
|
||||
}
|
||||
|
||||
token_count++;
|
||||
tokens_count++;
|
||||
if (token_size == 0)
|
||||
{
|
||||
stop_errors(ERROR_UNKNOWN_TOKEN);
|
||||
@@ -217,8 +217,8 @@ int lexerize(const char *input, token *tokens)
|
||||
input_pos += token_size;
|
||||
}
|
||||
|
||||
tokens[token_count].type = TOKEN_END;
|
||||
tokens[token_count].value_char = '\0';
|
||||
tokens[tokens_count].type = TOKEN_END;
|
||||
tokens[tokens_count].value_char = '\0';
|
||||
|
||||
return token_count;
|
||||
return tokens_count;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user