wip parsing and error
This commit is contained in:
120
src/parser.c
120
src/parser.c
@@ -1,4 +1,8 @@
|
||||
#include "parser.h"
|
||||
#include "libft.h"
|
||||
#include "lexer.h"
|
||||
#include "errors.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
TOKEN_VARIABLE, // x, y, etc.
|
||||
@@ -33,15 +37,15 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||
// forbidden tokens
|
||||
if (tokens[i].type == TOKEN_POWER)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'power' : %c", tokens[i].value_char);
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'power' : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
if (tokens[i].tag == TOKEN_FACTOR)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'factor' : %c", tokens[i].value_char);
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'factor' : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
if (tokens[i].type == TOKEN_EQUAL)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'equal' : %c", tokens[i].value_char);
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'equal' : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
|
||||
// sign
|
||||
@@ -61,7 +65,7 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||
return '+';
|
||||
}
|
||||
|
||||
return stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should have a token 'sign' : %c", tokens[i].value_char);
|
||||
return stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should have a token 'sign', not : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
|
||||
static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
@@ -73,19 +77,19 @@ static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
// forbidden tokens
|
||||
if (tokens[i].type == TOKEN_POWER)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'power' : %c", tokens[i].value_char);
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'power' : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
if (tokens[i].tag == TOKEN_FACTOR)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'factor' : %c", tokens[i].value_char);
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'factor' : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
if (tokens[i].type == TOKEN_EQUAL)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'equal' : %c", tokens[i].value_char);
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'equal' : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
if (tokens[i].tag == TOKEN_SIGN)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'sign' : %c", tokens[i].value_char);
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'sign' : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
|
||||
// if not coefficient token
|
||||
@@ -127,59 +131,65 @@ static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
return coefficient;
|
||||
}
|
||||
|
||||
static int get_exponent(token *tokens, int i, int *token_count)
|
||||
static double get_exponent(token *tokens, int i, int *token_count)
|
||||
{
|
||||
/**
|
||||
* power
|
||||
* number
|
||||
* sign
|
||||
* equal
|
||||
* factor_div
|
||||
*/
|
||||
// forbidden tokens
|
||||
// first reach VARIABLE
|
||||
if (tokens[i].type == TOKEN_VARIABLE)
|
||||
{
|
||||
i++;
|
||||
(*token_count)++;
|
||||
}
|
||||
else if (tokens[i].type == TOKEN_FACTOR_MULT)
|
||||
{
|
||||
i++;
|
||||
if (tokens[i].type == TOKEN_VARIABLE)
|
||||
{
|
||||
i++;
|
||||
(*token_count) += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, after a '*' we should have a 'var', but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, the first tokens should be 'x' or '*x', but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
|
||||
// then get power sign '^'
|
||||
if (tokens[i].type == TOKEN_POWER)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'power' : %c", tokens[i].value_char);
|
||||
i++;
|
||||
(*token_count)++;
|
||||
}
|
||||
if (tokens[i].tag == TOKEN_NUMBER)
|
||||
else
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'number' : %c", tokens[i].value_char);
|
||||
}
|
||||
if (tokens[i].tag == TOKEN_SIGN)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'sign' : %c", tokens[i].value_char);
|
||||
}
|
||||
if (tokens[i].type == TOKEN_EQUAL)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'equal' : %c", tokens[i].value_char);
|
||||
}
|
||||
if (tokens[i].type == TOKEN_FACTOR_DIV)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'division' : %c", tokens[i].value_char);
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, after variable we should have '^', but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
|
||||
// if 'var' -> token_count + 1
|
||||
// else if '*' + 'var' -> token_count + 2
|
||||
// then get exponent
|
||||
if (tokens[i].type == TOKEN_NUMBER_INT)
|
||||
{
|
||||
i++;
|
||||
(*token_count)++;
|
||||
}
|
||||
else
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should have an int, but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
|
||||
token_count += 0; // placeholder
|
||||
|
||||
return 1; // placeholder
|
||||
return tokens[i].value_double;
|
||||
}
|
||||
|
||||
int parse(token *tokens, term *terms, int terms_count_max)
|
||||
static void check_variables(token *tokens)
|
||||
{
|
||||
int i;
|
||||
int terms_count;
|
||||
int token_count;
|
||||
term_position term_position;
|
||||
char var;
|
||||
|
||||
terms_count = 0;
|
||||
token_count = 0;
|
||||
i = 0;
|
||||
term_position = TERM_LEFT;
|
||||
var = 0;
|
||||
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
|
||||
while (tokens[i].type != TOKEN_END)
|
||||
{
|
||||
// variable -> all variables must be the same
|
||||
if (tokens[i].type == TOKEN_VARIABLE)
|
||||
@@ -190,10 +200,28 @@ int parse(token *tokens, term *terms, int terms_count_max)
|
||||
}
|
||||
else if (var != tokens[i].value_char)
|
||||
{
|
||||
stop_errors(ERROR_VAR_DIFF, "old var : '%c' - new var : '%c'", var, tokens[i].value_char);
|
||||
stop_errors(ERROR_VAR_DIFF, "old var : '%c' - new var : '%c' (token number %i)", var, tokens[i].value_char, i);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int parse(token *tokens, term *terms, int terms_count_max)
|
||||
{
|
||||
int i;
|
||||
int terms_count;
|
||||
int token_count;
|
||||
term_position term_position;
|
||||
|
||||
check_variables(tokens);
|
||||
|
||||
terms_count = 0;
|
||||
token_count = 0;
|
||||
i = 0;
|
||||
term_position = TERM_LEFT;
|
||||
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
|
||||
{
|
||||
// equal
|
||||
if (tokens[i].type == TOKEN_EQUAL)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user