wip parser structure with placeholders

This commit is contained in:
hugogogo
2026-05-01 01:58:04 +02:00
parent 512ba9b5f4
commit 241cbdaff1
4 changed files with 122 additions and 8 deletions

View File

@@ -97,7 +97,7 @@ $(NAME): $(OBJS)
run: $(NAME) run: $(NAME)
@echo $(YELLOW)"run"$(RESET) @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: clean:
$(RM_OBJS) $(RM_OBJS)

View File

@@ -4,22 +4,37 @@
#include "libft.h" #include "libft.h"
#include "lexer.h" #include "lexer.h"
#include "errors.h" #include "errors.h"
#include <stdbool.h>
typedef enum 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_type; } term_position;
typedef enum
{
TERM_PLUS, // +
TERM_MINUS, // -
} term_sign;
typedef enum
{
TYPE_INT, // "123"
TYPE_DOUBLE, // "123.456"
} coefficient_type;
typedef struct typedef struct
{ {
term_type type; term_position position;
term_sign sign;
coefficient_type coefficient_type;
union union
{ {
char value_char; int coefficient_int;
int value_int; double coefficient_double;
double value_double;
}; };
int exponent;
} term; } term;
int parse(token *tokens, term *terms); int parse(token *tokens, term *terms);

View File

@@ -97,7 +97,7 @@ int main(int ac, char **av)
} }
else if (tokens[i].type == TOKEN_NUMBER_DOUBLE) else if (tokens[i].type == TOKEN_NUMBER_DOUBLE)
{ {
printf("%f\n", tokens[i].value_double); printf("%g\n", tokens[i].value_double);
} }
else else
{ {

View File

@@ -1,10 +1,109 @@
#include "parser.h" #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) int parse(token *tokens, term *terms)
{ {
/*
if (tokens) if (tokens)
{ {
terms[0].type = 0; terms[0].exponent = 0;
} }
return 1; 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;
} }