wip parser structure with placeholders
This commit is contained in:
2
Makefile
2
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)
|
||||
|
||||
@@ -4,22 +4,37 @@
|
||||
#include "libft.h"
|
||||
#include "lexer.h"
|
||||
#include "errors.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
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);
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
101
src/parser.c
101
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;
|
||||
}
|
||||
Reference in New Issue
Block a user