adding subtypes
This commit is contained in:
94
src/parser.c
94
src/parser.c
@@ -1,18 +1,24 @@
|
||||
#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)
|
||||
TOKEN_VARIABLE, // x, y, etc.
|
||||
TOKEN_NUMBER_INT, // int
|
||||
TOKEN_NUMBER_DOUBLE, // double
|
||||
TOKEN_POWER, // ^ or **
|
||||
TOKEN_SIGN, // + or -
|
||||
TOKEN_MULTIPLICATION, // *
|
||||
TOKEN_DIVISION, // /
|
||||
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
|
||||
1. VAR | NUM | ! POW | SIGN | ! FACTOR | ! EQUAL | END
|
||||
NS | NUM_I | NUM_D | NS | SIGN_P | SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | NS | NS
|
||||
|
||||
2. VAR | NUM | ! POW | ! SIGN | ! FACTOR | ! EQUAL | END
|
||||
NS | NUM_I | NUM_D | NS | ! SIGN_P | ! SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | NS | NS
|
||||
|
||||
3. VAR | NUM | ! POW | SIGN | FACTOR | ! EQUAL | END
|
||||
NS | NUM_I | NUM_D | NS | SIGN_P | SIGN_M | FACTOR_MUL | ! FACTOR_DIV | NS | NS
|
||||
|
||||
term_position position;
|
||||
term_sign sign;
|
||||
@@ -45,12 +51,12 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||
}
|
||||
else if (i == 0) // if most left term, the sign can be ommited for a '+' sign in front of a number or variable
|
||||
{
|
||||
*token_count = 1;
|
||||
*token_count = 0;
|
||||
return '+';
|
||||
}
|
||||
else if (tokens[i - 1].type == TOKEN_EQUAL) // if first token after 'equal', the sign can be ommited for a '+' sign in front of a number or variable
|
||||
{
|
||||
*token_count = 1;
|
||||
*token_count = 0;
|
||||
return '+';
|
||||
}
|
||||
|
||||
@@ -59,9 +65,65 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||
|
||||
static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
{
|
||||
if (tokens[i].type) // placeholder
|
||||
*token_count = 1; // placeholder
|
||||
return 1.0; // placeholder
|
||||
double coefficient;
|
||||
|
||||
coefficient = 1.0;
|
||||
|
||||
// forbidden tokens
|
||||
if (tokens[i].type == TOKEN_POWER)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a power token : " + tokens[i].value_char);
|
||||
}
|
||||
if (tokens[i].type == TOKEN_FACTOR)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a factor token : " + tokens[i].value_char);
|
||||
}
|
||||
if (tokens[i].type == TOKEN_EQUAL)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have an equal token : " + tokens[i].value_char);
|
||||
}
|
||||
if (tokens[i].type == TOKEN_SIGN)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a sign token : " + tokens[i].value_char);
|
||||
}
|
||||
|
||||
// if not coefficient token
|
||||
if (tokens[i].type == TOKEN_VARIABLE)
|
||||
{
|
||||
*token_count = 0;
|
||||
return coefficient;
|
||||
}
|
||||
|
||||
// if coefficient tokens
|
||||
if (tokens[i].type == TOKEN_NUMBER)
|
||||
{
|
||||
(*token_count)++;
|
||||
coefficient = tokens[i].value_double;
|
||||
}
|
||||
// detect more coefficients, like "3 * 2 / 5" etc
|
||||
i++;
|
||||
while (tokens[i].type == TOKEN_FACTOR)
|
||||
{
|
||||
i++;
|
||||
if (tokens[i].type == TOKEN_NUMBER)
|
||||
{
|
||||
*token_count += 2;
|
||||
if (tokens[i - 1].subtype == TOKEN_FACTOR_MULTIPLICATION)
|
||||
{
|
||||
coefficient *= tokens[i].value_double;
|
||||
}
|
||||
else if (tokens[i - 1].subtype == TOKEN_FACTOR_DIVISION)
|
||||
{
|
||||
coefficient /= tokens[i].value_double;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return coefficient;
|
||||
}
|
||||
}
|
||||
|
||||
return coefficient;
|
||||
}
|
||||
|
||||
static int get_exponent(token *tokens, int i, int *token_count)
|
||||
|
||||
Reference in New Issue
Block a user