diff --git a/Makefile b/Makefile index 373c1e8..c89ba8d 100644 --- a/Makefile +++ b/Makefile @@ -97,7 +97,7 @@ $(NAME): $(OBJS) run: $(NAME) @echo $(YELLOW)"run"$(RESET) - @./$(NAME) "3.4 * x^2 + 1 * x^1 - 2 * x^0 = 5.123 * x^1" + @./$(NAME) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" clean: $(RM_OBJS) diff --git a/headers/parser.h b/headers/parser.h index bc0c36c..a4e2c31 100644 --- a/headers/parser.h +++ b/headers/parser.h @@ -4,22 +4,37 @@ #include "libft.h" #include "lexer.h" #include "errors.h" +#include typedef enum { TERM_LEFT, // a in "a = b" TERM_RIGHT, // b in "a = b" -} term_type; +} term_position; + +typedef enum +{ + TERM_PLUS, // + + TERM_MINUS, // - +} term_sign; + +typedef enum +{ + TYPE_INT, // "123" + TYPE_DOUBLE, // "123.456" +} coefficient_type; typedef struct { - term_type type; + term_position position; + term_sign sign; + coefficient_type coefficient_type; union { - char value_char; - int value_int; - double value_double; + int coefficient_int; + double coefficient_double; }; + int exponent; } term; int parse(token *tokens, term *terms); diff --git a/src/computorv1.c b/src/computorv1.c index fd59c9e..78074b1 100644 --- a/src/computorv1.c +++ b/src/computorv1.c @@ -97,7 +97,7 @@ int main(int ac, char **av) } else if (tokens[i].type == TOKEN_NUMBER_DOUBLE) { - printf("%f\n", tokens[i].value_double); + printf("%g\n", tokens[i].value_double); } else { diff --git a/src/parser.c b/src/parser.c index 62eebbb..fcb60ea 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,10 +1,109 @@ #include "parser.h" +/** + TOKEN_VARIABLE, // x, y, etc. + TOKEN_NUMBER_INT, // int + TOKEN_NUMBER_DOUBLE, // double + TOKEN_POWER, // ^ or ** + TOKEN_SIGN, // + or - + TOKEN_FACTOR, // * or / + TOKEN_EQUAL, // = + TOKEN_END // null (end of input) + + 1. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END + 2. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END + 3. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END + + term_position position; + term_sign sign; + coefficient_type coefficient_type; + union + { + int coefficient_int; + double coefficient_double; + }; + int exponent; + */ + +static term_sign get_sign(token *tokens, int *token_count) +{ + if (tokens) // placeholder + *token_count = 1; // placeholder + return '+'; // placeholder +} + +static void get_coefficient(token *tokens, int *token_count, coefficient_type *coefficient_type, int *coefficient_int, double *coefficient_double) +{ + if (tokens && coefficient_type && coefficient_double && coefficient_int) // placeholder + *token_count = 1; // placeholder +} + +static int get_exponent(token *tokens, int *token_count) +{ + if (tokens) // placeholder + *token_count = 1; // placeholder + return 1; // placeholder +} + int parse(token *tokens, term *terms) { + /* if (tokens) { - terms[0].type = 0; + terms[0].exponent = 0; } return 1; + */ + + int i; + int term_count; + int token_count; + int coefficient_int; + double coefficient_double; + term_position term_position; + coefficient_type coefficient_type; + + term_count = 0; + token_count = 0; + i = 0; + term_position = TERM_LEFT; + while (tokens[i].type != TOKEN_END) + { + coefficient_int = 0; + coefficient_double = 0.0; + + if (tokens[i].type == TOKEN_EQUAL) + { + term_position = TERM_RIGHT; + i++; + break; + } + + // position + terms[term_count].position = term_position; + + // sign + terms[term_count].sign = get_sign(&tokens[i], &token_count); + i += token_count; + + // coefficient + get_coefficient(&tokens[i], &token_count, &coefficient_type, &coefficient_int, &coefficient_double); + if (coefficient_type == TYPE_INT) + { + terms[term_count].coefficient_int = coefficient_int; + } + else if (coefficient_type == TYPE_DOUBLE) + { + terms[term_count].coefficient_double = coefficient_double; + } + i += token_count; + + // exponent + terms[term_count].exponent = get_exponent(&tokens[i], &token_count); + i += token_count; + + i++; + } + + return term_count; } \ No newline at end of file