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 @@ 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
{

View File

@@ -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;
}