fix error in stop_errors and add debug term output
This commit is contained in:
@@ -11,6 +11,6 @@ typedef enum
|
|||||||
ERROR_PARSING = -3,
|
ERROR_PARSING = -3,
|
||||||
} program_error;
|
} program_error;
|
||||||
|
|
||||||
int stop_errors(int err, char *details);
|
int stop_errors(int err, const char *details);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -10,12 +10,14 @@ typedef enum
|
|||||||
{
|
{
|
||||||
TERM_LEFT, // a in "a = b"
|
TERM_LEFT, // a in "a = b"
|
||||||
TERM_RIGHT, // b in "a = b"
|
TERM_RIGHT, // b in "a = b"
|
||||||
|
TERM_END, // last term
|
||||||
} term_position;
|
} term_position;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
TERM_PLUS, // +
|
TERM_PLUS, // +
|
||||||
TERM_MINUS, // -
|
TERM_MINUS, // -
|
||||||
|
TERM_NULL, // null -> for the last term
|
||||||
} term_sign;
|
} term_sign;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ int main(int ac, char **av)
|
|||||||
ft_putchar('\n'); // debug
|
ft_putchar('\n'); // debug
|
||||||
// END tmp debug output
|
// END tmp debug output
|
||||||
|
|
||||||
terms_count_prediction = count_any_of(input, "-+=") + 1; // +1 for first term that can have no leading '+'
|
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_putstr("-> terms_count_prediction : "); // debug
|
||||||
ft_putnbr(terms_count_prediction); // debug
|
ft_putnbr(terms_count_prediction); // debug
|
||||||
@@ -129,5 +129,37 @@ int main(int ac, char **av)
|
|||||||
ft_putnbr(term_count); // debug
|
ft_putnbr(term_count); // debug
|
||||||
ft_putchar('\n'); // 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);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
|
|
||||||
int stop_errors(int err, char *details)
|
int stop_errors(int err, const char *details)
|
||||||
{
|
{
|
||||||
switch (err)
|
switch (err)
|
||||||
{
|
{
|
||||||
|
|||||||
18
src/parser.c
18
src/parser.c
@@ -44,14 +44,6 @@ static int get_exponent(token *tokens, int *token_count)
|
|||||||
|
|
||||||
int parse(token *tokens, term *terms, int terms_count_max)
|
int parse(token *tokens, term *terms, int terms_count_max)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
if (tokens)
|
|
||||||
{
|
|
||||||
terms[0].exponent = 0;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
*/
|
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
int terms_count;
|
int terms_count;
|
||||||
int token_count;
|
int token_count;
|
||||||
@@ -89,7 +81,15 @@ int parse(token *tokens, term *terms, int terms_count_max)
|
|||||||
terms_count++;
|
terms_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tokens[i].type != TOKEN_END && terms_count >= terms_count_max)
|
// last token is TOKEN_END, and terms[] should have at least one more spot for a null
|
||||||
|
if (tokens[i].type == TOKEN_END && terms_count < terms_count_max)
|
||||||
|
{
|
||||||
|
terms[terms_count].position = TERM_END;
|
||||||
|
terms[terms_count].sign = TERM_NULL;
|
||||||
|
terms[terms_count].coefficient = 0;
|
||||||
|
terms[terms_count].exponent = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
stop_errors(ERROR_PARSING, "deal with that later");
|
stop_errors(ERROR_PARSING, "deal with that later");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user