changed subtypes for tags
This commit is contained in:
@@ -139,35 +139,72 @@
|
||||
* PROPOSITION 3
|
||||
*/
|
||||
|
||||
// typedef enum
|
||||
// {
|
||||
// TOKEN_VARIABLE, // x, y, etc.
|
||||
// TOKEN_NUMBER, // int or double
|
||||
// TOKEN_POWER, // ^ or **
|
||||
// TOKEN_SIGN, // + or -
|
||||
// TOKEN_FACTOR, // * or / or :
|
||||
// TOKEN_EQUAL, // =
|
||||
// TOKEN_END // null (end of input)
|
||||
// } token_type;
|
||||
//
|
||||
// typedef enum
|
||||
// {
|
||||
// TOKEN_NO_SUBTYPE,
|
||||
// // NUMBER
|
||||
// TOKEN_NUMBER_INT,
|
||||
// TOKEN_NUMBER_DOUBLE,
|
||||
// // SIGN
|
||||
// TOKEN_SIGN_PLUS,
|
||||
// TOKEN_SIGN_MINUS,
|
||||
// // FACTOR
|
||||
// TOKEN_FACTOR_MULTIPLICATION,
|
||||
// TOKEN_FACTOR_DIVISION,
|
||||
// } token_subtype;
|
||||
//
|
||||
// typedef struct
|
||||
// {
|
||||
// token_type type;
|
||||
// token_subtype subtype;
|
||||
// union
|
||||
// {
|
||||
// char value_char;
|
||||
// double value_double;
|
||||
// };
|
||||
// } token;
|
||||
|
||||
/**
|
||||
* PROPOSITION 4
|
||||
*/
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TOKEN_VARIABLE, // x, y, etc.
|
||||
TOKEN_NUMBER, // int or double
|
||||
TOKEN_POWER, // ^ or **
|
||||
TOKEN_SIGN, // + or -
|
||||
TOKEN_FACTOR, // * or / 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_PLUS, // +
|
||||
TOKEN_SIGN_MINUS, // -
|
||||
TOKEN_FACTOR_MULT, // *
|
||||
TOKEN_FACTOR_DIV, // / or :
|
||||
TOKEN_EQUAL, // =
|
||||
TOKEN_END // null (end of input)
|
||||
} token_type;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TOKEN_NO_SUBTYPE,
|
||||
// NUMBER
|
||||
TOKEN_NUMBER_INT,
|
||||
TOKEN_NUMBER_DOUBLE,
|
||||
// SIGN
|
||||
TOKEN_SIGN_PLUS,
|
||||
TOKEN_SIGN_MINUS,
|
||||
// FACTOR
|
||||
TOKEN_FACTOR_MULTIPLICATION,
|
||||
TOKEN_FACTOR_DIVISION,
|
||||
} token_subtype;
|
||||
TOKEN_NO_TAG,
|
||||
TOKEN_NUMBER,
|
||||
TOKEN_SIGN,
|
||||
TOKEN_FACTOR,
|
||||
} token_tag;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
token_type type;
|
||||
token_subtype subtype;
|
||||
token_tag tag;
|
||||
union
|
||||
{
|
||||
char value_char;
|
||||
|
||||
@@ -87,41 +87,29 @@ int main(int ac, char **av)
|
||||
ft_printf("token %2i - type : ", i);
|
||||
|
||||
if (tokens[i].type == TOKEN_VARIABLE)
|
||||
ft_printf("%14s%30s", "TOKEN_VARIABLE", "");
|
||||
else if (tokens[i].type == TOKEN_NUMBER)
|
||||
{
|
||||
ft_printf("%14s", "TOKEN_NUMBER");
|
||||
if (tokens[i].subtype == TOKEN_NUMBER_INT)
|
||||
ft_printf("%30s", "TOKEN_NUMBER_INT");
|
||||
else if (tokens[i].subtype == TOKEN_NUMBER_DOUBLE)
|
||||
ft_printf("%30s", "TOKEN_NUMBER_DOUBLE");
|
||||
}
|
||||
ft_printf("%20s", "TOKEN_VARIABLE");
|
||||
else if (tokens[i].type == TOKEN_NUMBER_INT)
|
||||
ft_printf("%20s", "TOKEN_NUMBER_INT");
|
||||
else if (tokens[i].type == TOKEN_NUMBER_DOUBLE)
|
||||
ft_printf("%20s", "TOKEN_NUMBER_DOUBLE");
|
||||
else if (tokens[i].type == TOKEN_POWER)
|
||||
ft_printf("%14s%30s", "TOKEN_POWER", "");
|
||||
else if (tokens[i].type == TOKEN_SIGN)
|
||||
{
|
||||
ft_printf("%14s", "TOKEN_SIGN");
|
||||
if (tokens[i].subtype == TOKEN_SIGN_PLUS)
|
||||
ft_printf("%30s", "TOKEN_SIGN_PLUS");
|
||||
else if (tokens[i].subtype == TOKEN_SIGN_MINUS)
|
||||
ft_printf("%30s", "TOKEN_SIGN_MINUS");
|
||||
}
|
||||
else if (tokens[i].type == TOKEN_FACTOR)
|
||||
{
|
||||
ft_printf("%14s", "TOKEN_FACTOR");
|
||||
if (tokens[i].subtype == TOKEN_FACTOR_MULTIPLICATION)
|
||||
ft_printf("%30s", "TOKEN_FACTOR_MULTIPLICATION");
|
||||
else if (tokens[i].subtype == TOKEN_FACTOR_DIVISION)
|
||||
ft_printf("%30s", "TOKEN_FACTOR_DIVISION");
|
||||
}
|
||||
ft_printf("%20s", "TOKEN_POWER");
|
||||
else if (tokens[i].type == TOKEN_SIGN_PLUS)
|
||||
ft_printf("%20s", "TOKEN_SIGN_PLUS");
|
||||
else if (tokens[i].type == TOKEN_SIGN_MINUS)
|
||||
ft_printf("%20s", "TOKEN_SIGN_MINUS");
|
||||
else if (tokens[i].type == TOKEN_FACTOR_MULT)
|
||||
ft_printf("%20s", "TOKEN_FACTOR_MULT");
|
||||
else if (tokens[i].type == TOKEN_FACTOR_DIV)
|
||||
ft_printf("%20s", "TOKEN_FACTOR_DIV");
|
||||
else if (tokens[i].type == TOKEN_EQUAL)
|
||||
ft_printf("%14s%30s", "TOKEN_EQUAL", "");
|
||||
ft_printf("%20s", "TOKEN_EQUAL");
|
||||
else if (tokens[i].type == TOKEN_END)
|
||||
ft_printf("%14s%30s", "TOKEN_END", "");
|
||||
ft_printf("%20s", "TOKEN_END");
|
||||
|
||||
ft_putstr(" - value : ");
|
||||
|
||||
if (tokens[i].type == TOKEN_NUMBER)
|
||||
if (tokens[i].tag == TOKEN_NUMBER)
|
||||
{
|
||||
printf("%g\n", tokens[i].value_double);
|
||||
}
|
||||
|
||||
32
src/lexer.c
32
src/lexer.c
@@ -197,55 +197,55 @@ int lexerize(const char *input, token *tokens)
|
||||
if (token_is_variable(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_VARIABLE;
|
||||
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
|
||||
tokens[tokens_count].tag = TOKEN_NO_TAG;
|
||||
tokens[tokens_count].value_char = 'x';
|
||||
}
|
||||
else if (token_is_number_int(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_NUMBER;
|
||||
tokens[tokens_count].subtype = TOKEN_NUMBER_INT;
|
||||
tokens[tokens_count].type = TOKEN_NUMBER_INT;
|
||||
tokens[tokens_count].tag = TOKEN_NUMBER;
|
||||
tokens[tokens_count].value_double = ft_atof(&input[input_pos]); // we keep info it's an int, but treat it as a double
|
||||
}
|
||||
else if (token_is_number_double(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_NUMBER;
|
||||
tokens[tokens_count].subtype = TOKEN_NUMBER_DOUBLE;
|
||||
tokens[tokens_count].type = TOKEN_NUMBER_DOUBLE;
|
||||
tokens[tokens_count].tag = TOKEN_NUMBER;
|
||||
tokens[tokens_count].value_double = ft_atof(&input[input_pos]);
|
||||
}
|
||||
else if (token_is_power(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_POWER;
|
||||
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
|
||||
tokens[tokens_count].tag = TOKEN_NO_TAG;
|
||||
tokens[tokens_count].value_char = '^';
|
||||
}
|
||||
else if (token_is_sign_plus(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_SIGN;
|
||||
tokens[tokens_count].subtype = TOKEN_SIGN_PLUS;
|
||||
tokens[tokens_count].type = TOKEN_SIGN_PLUS;
|
||||
tokens[tokens_count].tag = TOKEN_SIGN;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_sign_minus(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_SIGN;
|
||||
tokens[tokens_count].subtype = TOKEN_SIGN_MINUS;
|
||||
tokens[tokens_count].type = TOKEN_SIGN_MINUS;
|
||||
tokens[tokens_count].tag = TOKEN_SIGN;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_factor_multiplication(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_FACTOR;
|
||||
tokens[tokens_count].subtype = TOKEN_FACTOR_MULTIPLICATION;
|
||||
tokens[tokens_count].type = TOKEN_FACTOR_MULT;
|
||||
tokens[tokens_count].tag = TOKEN_FACTOR;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_factor_division(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_FACTOR;
|
||||
tokens[tokens_count].subtype = TOKEN_FACTOR_DIVISION;
|
||||
tokens[tokens_count].type = TOKEN_FACTOR_DIV;
|
||||
tokens[tokens_count].tag = TOKEN_FACTOR;
|
||||
tokens[tokens_count].value_char = input[input_pos];
|
||||
}
|
||||
else if (token_is_equal(input, input_pos, &token_size))
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_EQUAL;
|
||||
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
|
||||
tokens[tokens_count].tag = TOKEN_NO_TAG;
|
||||
tokens[tokens_count].value_char = '=';
|
||||
}
|
||||
else
|
||||
@@ -262,7 +262,7 @@ int lexerize(const char *input, token *tokens)
|
||||
}
|
||||
|
||||
tokens[tokens_count].type = TOKEN_END;
|
||||
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
|
||||
tokens[tokens_count].tag = TOKEN_NO_TAG;
|
||||
tokens[tokens_count].value_char = '\0';
|
||||
|
||||
return tokens_count;
|
||||
|
||||
30
src/parser.c
30
src/parser.c
@@ -11,14 +11,14 @@
|
||||
TOKEN_EQUAL, // =
|
||||
TOKEN_END // null (end of input)
|
||||
|
||||
1. VAR | NUM | ! POW | SIGN | ! FACTOR | ! EQUAL | END
|
||||
NS | NUM_I | NUM_D | NS | SIGN_P | SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | NS | NS
|
||||
1. VAR | NUMBER_I | NUMBER_D | ! POW | SIGN_P | SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | ! EQUAL | END
|
||||
NT | NUMBER | NT | SIGN | ! FACTOR | NT | NT
|
||||
|
||||
2. 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 | NUMBER_I | NUMBER_D | ! POW | ! SIGN_P | ! SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | ! EQUAL | END
|
||||
NT | NUMBER | NT | ! SIGN | ! FACTOR | NT | NT
|
||||
|
||||
3. 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 | NUMBER_I | NUMBER_D | ! POW | SIGN_P | SIGN_M | FACTOR_MUL | FACTOR_DIV | EQUAL | END
|
||||
NT | NUMBER | NT | SIGN | FACTOR | NT | NT
|
||||
|
||||
term_position position;
|
||||
term_sign sign;
|
||||
@@ -34,7 +34,7 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should not have a power token : " + tokens[i].value_char);
|
||||
}
|
||||
if (tokens[i].type == TOKEN_FACTOR)
|
||||
if (tokens[i].tag == TOKEN_FACTOR)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should not have a factor token : " + tokens[i].value_char);
|
||||
}
|
||||
@@ -44,7 +44,7 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||
}
|
||||
|
||||
// sign
|
||||
if (tokens[i].type == TOKEN_SIGN)
|
||||
if (tokens[i].tag == TOKEN_SIGN)
|
||||
{
|
||||
*token_count = 1;
|
||||
return tokens[i].value_char;
|
||||
@@ -74,7 +74,7 @@ static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
{
|
||||
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)
|
||||
if (tokens[i].tag == TOKEN_FACTOR)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a factor token : " + tokens[i].value_char);
|
||||
}
|
||||
@@ -82,7 +82,7 @@ static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
{
|
||||
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)
|
||||
if (tokens[i].tag == TOKEN_SIGN)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a sign token : " + tokens[i].value_char);
|
||||
}
|
||||
@@ -95,24 +95,24 @@ static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
}
|
||||
|
||||
// if coefficient tokens
|
||||
if (tokens[i].type == TOKEN_NUMBER)
|
||||
if (tokens[i].tag == TOKEN_NUMBER)
|
||||
{
|
||||
(*token_count)++;
|
||||
coefficient = tokens[i].value_double;
|
||||
}
|
||||
// detect more coefficients, like "3 * 2 / 5" etc
|
||||
i++;
|
||||
while (tokens[i].type == TOKEN_FACTOR)
|
||||
while (tokens[i].tag == TOKEN_FACTOR)
|
||||
{
|
||||
i++;
|
||||
if (tokens[i].type == TOKEN_NUMBER)
|
||||
if (tokens[i].tag == TOKEN_NUMBER)
|
||||
{
|
||||
*token_count += 2;
|
||||
if (tokens[i - 1].subtype == TOKEN_FACTOR_MULTIPLICATION)
|
||||
if (tokens[i - 1].type == TOKEN_FACTOR_MULT)
|
||||
{
|
||||
coefficient *= tokens[i].value_double;
|
||||
}
|
||||
else if (tokens[i - 1].subtype == TOKEN_FACTOR_DIVISION)
|
||||
else if (tokens[i - 1].type == TOKEN_FACTOR_DIV)
|
||||
{
|
||||
coefficient /= tokens[i].value_double;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user