parse working
This commit is contained in:
@@ -97,10 +97,8 @@ int main(int ac, char **av)
|
||||
remove_spaces(input);
|
||||
|
||||
// lexerize
|
||||
arg_len = ft_strlen(input) + 1; // +1 for last END token
|
||||
|
||||
arg_len = ft_strlen(input) + 1; // +1 for last END token
|
||||
ft_printf("-> tokens[%i]\n", arg_len); // debug
|
||||
|
||||
token tokens[arg_len];
|
||||
tokens_g_err = tokens;
|
||||
tokens_fill_null(tokens, arg_len);
|
||||
@@ -112,9 +110,7 @@ int main(int ac, char **av)
|
||||
|
||||
// parse
|
||||
terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL
|
||||
|
||||
ft_printf("-> terms[%i]\n", terms_count_prediction); // debug
|
||||
|
||||
ft_printf("-> terms[%i]\n", terms_count_prediction); // debug
|
||||
term terms[terms_count_prediction];
|
||||
terms_g_err = terms;
|
||||
terms_fill_null(terms, terms_count_prediction);
|
||||
@@ -124,5 +120,8 @@ int main(int ac, char **av)
|
||||
stop_errors(ERROR_TERM_COUNT, "parser returned 0 term");
|
||||
}
|
||||
|
||||
// debug
|
||||
print_state();
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
12
src/parser.c
12
src/parser.c
@@ -237,14 +237,14 @@ int parse(token *tokens, term *terms, int terms_count_max)
|
||||
term_position = TERM_LEFT;
|
||||
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
|
||||
{
|
||||
ft_printf("- token[%i]\n", i); // debug
|
||||
// ft_printf("- token[%i]\n", i); // debug
|
||||
|
||||
// equal
|
||||
if (tokens[i].type == TOKEN_EQUAL)
|
||||
{
|
||||
term_position = TERM_RIGHT;
|
||||
i++;
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
// position
|
||||
@@ -254,19 +254,19 @@ int parse(token *tokens, term *terms, int terms_count_max)
|
||||
term_sign ret_sign = get_sign(tokens, i, &token_count);
|
||||
terms[terms_count].sign = ret_sign;
|
||||
i += token_count;
|
||||
ft_printf("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // debug
|
||||
// ft_printf("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // debug
|
||||
|
||||
// coefficient
|
||||
double ret_coefficient = get_coefficient(tokens, i, &token_count);
|
||||
terms[terms_count].coefficient = ret_coefficient;
|
||||
i += token_count;
|
||||
printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug
|
||||
// printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug
|
||||
|
||||
// exponent
|
||||
int ret_exponent = get_exponent(tokens, i, &token_count);
|
||||
terms[terms_count].exponent = ret_exponent;
|
||||
i += token_count;
|
||||
ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug
|
||||
// ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug
|
||||
|
||||
terms_count++;
|
||||
}
|
||||
@@ -281,7 +281,7 @@ int parse(token *tokens, term *terms, int terms_count_max)
|
||||
}
|
||||
else
|
||||
{
|
||||
stop_errors(ERROR_PARSING, "deal with that later");
|
||||
stop_errors(ERROR_PARSING, "terms_count: %i, terms_count_max: %i, tokens[%i].type: %s", terms_count, terms_count_max, i, token_type_to_str(tokens[i].type));
|
||||
}
|
||||
|
||||
return terms_count;
|
||||
|
||||
@@ -15,7 +15,7 @@ static void print_context_tokens()
|
||||
i = 0;
|
||||
while (tokens_g_err[i].type != TOKEN_END)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "token %2i - type : ", i);
|
||||
ft_dprintf(STDERR_FILENO, "token[%2i] - type : ", i);
|
||||
|
||||
if (tokens_g_err[i].type == TOKEN_VARIABLE)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_VARIABLE");
|
||||
@@ -65,7 +65,7 @@ static void print_context_terms()
|
||||
i = 0;
|
||||
while (terms_g_err[i].position != TERM_END)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "term %2i - ", i);
|
||||
ft_dprintf(STDERR_FILENO, "term[%2i] - ", i);
|
||||
|
||||
// position
|
||||
ft_dprintf(STDERR_FILENO, "%8s : ", "position");
|
||||
@@ -95,6 +95,17 @@ static void print_context_terms()
|
||||
ft_putchar('\n');
|
||||
}
|
||||
|
||||
void print_state()
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "\nSTATE :\n");
|
||||
if (input_g_err)
|
||||
print_context_input();
|
||||
if (tokens_g_err)
|
||||
print_context_tokens();
|
||||
if (terms_g_err)
|
||||
print_context_terms();
|
||||
}
|
||||
|
||||
int stop_errors(program_error err, const char *details, ...)
|
||||
{
|
||||
// the base error message
|
||||
@@ -119,12 +130,7 @@ int stop_errors(program_error err, const char *details, ...)
|
||||
}
|
||||
|
||||
// print context
|
||||
if (input_g_err)
|
||||
print_context_input();
|
||||
if (tokens_g_err)
|
||||
print_context_tokens();
|
||||
if (terms_g_err)
|
||||
print_context_terms();
|
||||
print_state();
|
||||
|
||||
// print the base message
|
||||
ft_dprintf(STDERR_FILENO, "error: (%i) %s - details: ", err, msg);
|
||||
|
||||
Reference in New Issue
Block a user