parse working

This commit is contained in:
hugogogo
2026-05-03 22:28:26 +02:00
parent 07144c53c6
commit 6648161d9a
4 changed files with 26 additions and 20 deletions

View File

@@ -93,6 +93,7 @@ typedef enum t_program_error
ERROR_SENTINEL, // last token not used, only for enum count ERROR_SENTINEL, // last token not used, only for enum count
} program_error; } program_error;
void print_state();
int stop_errors(program_error err, const char *format, ...); int stop_errors(program_error err, const char *format, ...);
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

View File

@@ -97,10 +97,8 @@ int main(int ac, char **av)
remove_spaces(input); remove_spaces(input);
// lexerize // 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 ft_printf("-> tokens[%i]\n", arg_len); // debug
token tokens[arg_len]; token tokens[arg_len];
tokens_g_err = tokens; tokens_g_err = tokens;
tokens_fill_null(tokens, arg_len); tokens_fill_null(tokens, arg_len);
@@ -112,9 +110,7 @@ int main(int ac, char **av)
// parse // parse
terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL 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]; term terms[terms_count_prediction];
terms_g_err = terms; terms_g_err = terms;
terms_fill_null(terms, terms_count_prediction); 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"); stop_errors(ERROR_TERM_COUNT, "parser returned 0 term");
} }
// debug
print_state();
return (0); return (0);
} }

View File

@@ -237,14 +237,14 @@ int parse(token *tokens, term *terms, int terms_count_max)
term_position = TERM_LEFT; term_position = TERM_LEFT;
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max) 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 // equal
if (tokens[i].type == TOKEN_EQUAL) if (tokens[i].type == TOKEN_EQUAL)
{ {
term_position = TERM_RIGHT; term_position = TERM_RIGHT;
i++; i++;
break; continue;
} }
// position // 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); term_sign ret_sign = get_sign(tokens, i, &token_count);
terms[terms_count].sign = ret_sign; terms[terms_count].sign = ret_sign;
i += token_count; 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 // coefficient
double ret_coefficient = get_coefficient(tokens, i, &token_count); double ret_coefficient = get_coefficient(tokens, i, &token_count);
terms[terms_count].coefficient = ret_coefficient; terms[terms_count].coefficient = ret_coefficient;
i += token_count; 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 // exponent
int ret_exponent = get_exponent(tokens, i, &token_count); int ret_exponent = get_exponent(tokens, i, &token_count);
terms[terms_count].exponent = ret_exponent; terms[terms_count].exponent = ret_exponent;
i += token_count; 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++; terms_count++;
} }
@@ -281,7 +281,7 @@ int parse(token *tokens, term *terms, int terms_count_max)
} }
else 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; return terms_count;

View File

@@ -15,7 +15,7 @@ static void print_context_tokens()
i = 0; i = 0;
while (tokens_g_err[i].type != TOKEN_END) 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) if (tokens_g_err[i].type == TOKEN_VARIABLE)
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_VARIABLE"); ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_VARIABLE");
@@ -65,7 +65,7 @@ static void print_context_terms()
i = 0; i = 0;
while (terms_g_err[i].position != TERM_END) while (terms_g_err[i].position != TERM_END)
{ {
ft_dprintf(STDERR_FILENO, "term %2i - ", i); ft_dprintf(STDERR_FILENO, "term[%2i] - ", i);
// position // position
ft_dprintf(STDERR_FILENO, "%8s : ", "position"); ft_dprintf(STDERR_FILENO, "%8s : ", "position");
@@ -95,6 +95,17 @@ static void print_context_terms()
ft_putchar('\n'); 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, ...) int stop_errors(program_error err, const char *details, ...)
{ {
// the base error message // the base error message
@@ -119,12 +130,7 @@ int stop_errors(program_error err, const char *details, ...)
} }
// print context // print context
if (input_g_err) print_state();
print_context_input();
if (tokens_g_err)
print_context_tokens();
if (terms_g_err)
print_context_terms();
// print the base message // print the base message
ft_dprintf(STDERR_FILENO, "error: (%i) %s - details: ", err, msg); ft_dprintf(STDERR_FILENO, "error: (%i) %s - details: ", err, msg);