adding utils with enum_print
This commit is contained in:
8
Makefile
8
Makefile
@@ -35,11 +35,13 @@ NAME = computorv1
|
||||
D_LIB = ./libft
|
||||
D_HEADERS = ./headers
|
||||
HEADERS = computorv1.h
|
||||
D_SRCS = ./src
|
||||
D_SRCS = ./src \
|
||||
./src/utils
|
||||
SRCS = computorv1.c \
|
||||
errors.c \
|
||||
lexer.c \
|
||||
parser.c
|
||||
parser.c \
|
||||
errors.c \
|
||||
print_enums.c
|
||||
|
||||
# COMPILATION CONFIG :
|
||||
CC = gcc
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
* LEXER.C
|
||||
*/
|
||||
|
||||
typedef enum
|
||||
typedef enum t_token_type
|
||||
{
|
||||
TOKEN_VARIABLE, // x, y, etc.
|
||||
TOKEN_NUMBER_INT, // int
|
||||
@@ -26,7 +26,7 @@ typedef enum
|
||||
TOKEN_END // null (end of input)
|
||||
} token_type;
|
||||
|
||||
typedef enum
|
||||
typedef enum t_token_tag
|
||||
{
|
||||
TOKEN_NO_TAG,
|
||||
TOKEN_NUMBER,
|
||||
@@ -41,6 +41,7 @@ typedef struct
|
||||
union
|
||||
{
|
||||
char value_char;
|
||||
int value_int;
|
||||
double value_double;
|
||||
};
|
||||
} token;
|
||||
@@ -51,35 +52,35 @@ int lexerize(const char *input, token *tokens);
|
||||
* PARSER.C
|
||||
*/
|
||||
|
||||
typedef enum
|
||||
typedef enum t_term_position
|
||||
{
|
||||
TERM_LEFT, // a in "a = b"
|
||||
TERM_RIGHT, // b in "a = b"
|
||||
TERM_END, // last term
|
||||
} term_position;
|
||||
|
||||
typedef enum
|
||||
typedef enum t_term_sign
|
||||
{
|
||||
TERM_PLUS, // +
|
||||
TERM_MINUS, // -
|
||||
TERM_NULL, // null -> for the last term
|
||||
} term_sign;
|
||||
|
||||
typedef struct
|
||||
typedef struct t_term
|
||||
{
|
||||
term_position position;
|
||||
term_sign sign;
|
||||
double coefficient;
|
||||
double exponent;
|
||||
int exponent;
|
||||
} term;
|
||||
|
||||
int parse(token *tokens, term *terms, int terms_count_max);
|
||||
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* ERRORS.C
|
||||
* UTILS/ERRORS.C
|
||||
*/
|
||||
|
||||
typedef enum
|
||||
typedef enum t_program_error
|
||||
{
|
||||
ERROR_BASE = 1, // start at 1 to avoid exit(0) for errors
|
||||
ERROR_TOKEN_COUNT,
|
||||
@@ -94,6 +95,15 @@ typedef enum
|
||||
|
||||
int stop_errors(program_error err, const char *format, ...);
|
||||
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* UTILS/PRINT_ENUMS.C
|
||||
*/
|
||||
|
||||
const char *token_type_to_str(token_type type);
|
||||
const char *token_tag_to_str(token_tag tag);
|
||||
const char *term_position_to_str(term_position pos);
|
||||
const char *term_sign_to_str(term_sign sign);
|
||||
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* GLOBALS
|
||||
*/
|
||||
|
||||
@@ -37,9 +37,9 @@ static void remove_spaces(char *s)
|
||||
}
|
||||
}
|
||||
|
||||
static int count_any_of(const char *s, const char *set)
|
||||
static size_t count_any_of(const char *s, const char *set)
|
||||
{
|
||||
int count = 0;
|
||||
size_t count = 0;
|
||||
for (; *s != '\0'; s++)
|
||||
{
|
||||
if (ft_strchr(set, *s) != NULL)
|
||||
@@ -50,7 +50,7 @@ static int count_any_of(const char *s, const char *set)
|
||||
return count;
|
||||
}
|
||||
|
||||
static void token_fill_null(token *tokens, size_t arg_len)
|
||||
static void tokens_fill_null(token *tokens, size_t arg_len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
@@ -64,11 +64,26 @@ static void token_fill_null(token *tokens, size_t arg_len)
|
||||
}
|
||||
}
|
||||
|
||||
static void terms_fill_null(term *terms, size_t terms_count_prediction)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < terms_count_prediction)
|
||||
{
|
||||
terms[i].coefficient = 0.0;
|
||||
terms[i].exponent = 0;
|
||||
terms[i].position = TERM_END;
|
||||
terms[i].sign = TERM_NULL;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int ret;
|
||||
size_t arg_len;
|
||||
int terms_count_prediction;
|
||||
size_t terms_count_prediction;
|
||||
char *input;
|
||||
|
||||
if (ac < 2)
|
||||
@@ -80,12 +95,15 @@ int main(int ac, char **av)
|
||||
input = av[1];
|
||||
input_g_err = input;
|
||||
remove_spaces(input);
|
||||
arg_len = ft_strlen(input) + 1; // +1 for last END token
|
||||
|
||||
// lexerize
|
||||
arg_len = ft_strlen(input) + 1; // +1 for last END token
|
||||
|
||||
ft_printf("-> tokens[%i]\n", arg_len); // debug
|
||||
|
||||
token tokens[arg_len];
|
||||
tokens_g_err = tokens;
|
||||
token_fill_null(tokens, arg_len);
|
||||
tokens_fill_null(tokens, arg_len);
|
||||
ret = lexerize(input, tokens);
|
||||
if (ret == 0)
|
||||
{
|
||||
@@ -94,8 +112,12 @@ int main(int ac, char **av)
|
||||
|
||||
// parse
|
||||
terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL
|
||||
|
||||
ft_printf("-> terms[%i]\n", terms_count_prediction); // debug
|
||||
|
||||
term terms[terms_count_prediction];
|
||||
terms_g_err = terms;
|
||||
terms_fill_null(terms, terms_count_prediction);
|
||||
ret = parse(tokens, terms, terms_count_prediction);
|
||||
if (ret == 0)
|
||||
{
|
||||
|
||||
@@ -206,7 +206,7 @@ int lexerize(const char *input, token *tokens)
|
||||
{
|
||||
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
|
||||
tokens[tokens_count].value_double = ft_atoi(&input[input_pos]);
|
||||
}
|
||||
else if (token_is_number_double(input, input_pos, &token_size))
|
||||
{
|
||||
|
||||
16
src/parser.c
16
src/parser.c
@@ -129,7 +129,7 @@ static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
return coefficient;
|
||||
}
|
||||
|
||||
static double get_exponent(token *tokens, int i, int *token_count)
|
||||
static int get_exponent(token *tokens, int i, int *token_count)
|
||||
{
|
||||
// first reach VARIABLE
|
||||
if (tokens[i].type == TOKEN_VARIABLE)
|
||||
@@ -177,7 +177,7 @@ static double get_exponent(token *tokens, int i, int *token_count)
|
||||
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);
|
||||
}
|
||||
|
||||
return tokens[i].value_double;
|
||||
return tokens[i].value_int;
|
||||
}
|
||||
|
||||
static void check_variables(token *tokens)
|
||||
@@ -232,16 +232,22 @@ int parse(token *tokens, term *terms, int terms_count_max)
|
||||
terms[terms_count].position = term_position;
|
||||
|
||||
// sign
|
||||
terms[terms_count].sign = get_sign(tokens, i, &token_count);
|
||||
term_sign ret_sign = get_sign(tokens, i, &token_count);
|
||||
terms[terms_count].sign = ret_sign;
|
||||
i += token_count;
|
||||
ft_printf("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count);
|
||||
|
||||
// coefficient
|
||||
terms[terms_count].coefficient = get_coefficient(tokens, i, &token_count);
|
||||
double ret_coefficient = get_coefficient(tokens, i, &token_count);
|
||||
terms[terms_count].coefficient = ret_coefficient;
|
||||
i += token_count;
|
||||
printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count);
|
||||
|
||||
// exponent
|
||||
terms[terms_count].exponent = get_exponent(tokens, i, &token_count);
|
||||
int ret_exponent = get_exponent(tokens, i, &token_count);
|
||||
terms[terms_count].exponent = ret_exponent;
|
||||
i += token_count;
|
||||
ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count);
|
||||
|
||||
i++;
|
||||
terms_count++;
|
||||
|
||||
59
src/utils/print_enums.c
Normal file
59
src/utils/print_enums.c
Normal file
@@ -0,0 +1,59 @@
|
||||
/* print_enums.c */
|
||||
|
||||
#include "computorv1.h"
|
||||
|
||||
const char *token_type_to_str(token_type enum_value)
|
||||
{
|
||||
if (enum_value > TOKEN_END || enum_value < TOKEN_VARIABLE)
|
||||
return "invalid enum value";
|
||||
|
||||
static const char *const token_type_str[TOKEN_END + 1] = {
|
||||
[TOKEN_VARIABLE] = "TOKEN_VARIABLE",
|
||||
[TOKEN_NUMBER_INT] = "TOKEN_NUMBER_INT",
|
||||
[TOKEN_NUMBER_DOUBLE] = "TOKEN_NUMBER_DOUBLE",
|
||||
[TOKEN_POWER] = "TOKEN_POWER",
|
||||
[TOKEN_SIGN_PLUS] = "TOKEN_SIGN_PLUS",
|
||||
[TOKEN_SIGN_MINUS] = "TOKEN_SIGN_MINUS",
|
||||
[TOKEN_FACTOR_MULT] = "TOKEN_FACTOR_MULT",
|
||||
[TOKEN_FACTOR_DIV] = "TOKEN_FACTOR_DIV",
|
||||
[TOKEN_EQUAL] = "TOKEN_EQUAL",
|
||||
[TOKEN_END] = "TOKEN_END"};
|
||||
return token_type_str[enum_value];
|
||||
}
|
||||
|
||||
const char *token_tag_to_str(token_tag enum_value)
|
||||
{
|
||||
if (enum_value > TOKEN_FACTOR || enum_value < TOKEN_NO_TAG)
|
||||
return "invalid enum value";
|
||||
|
||||
static const char *const token_tag_str[TOKEN_FACTOR + 1] = {
|
||||
[TOKEN_NO_TAG] = "TOKEN_NO_TAG",
|
||||
[TOKEN_NUMBER] = "TOKEN_NUMBER",
|
||||
[TOKEN_SIGN] = "TOKEN_SIGN",
|
||||
[TOKEN_FACTOR] = "TOKEN_FACTOR"};
|
||||
return token_tag_str[enum_value];
|
||||
}
|
||||
|
||||
const char *term_position_to_str(term_position enum_value)
|
||||
{
|
||||
if (enum_value > TERM_END || enum_value < TERM_LEFT)
|
||||
return "invalid enum value";
|
||||
|
||||
static const char *const term_position_str[TERM_END + 1] = {
|
||||
[TERM_LEFT] = "TERM_LEFT",
|
||||
[TERM_RIGHT] = "TERM_RIGHT",
|
||||
[TERM_END] = "TERM_END"};
|
||||
return term_position_str[enum_value];
|
||||
}
|
||||
|
||||
const char *term_sign_to_str(term_sign enum_value)
|
||||
{
|
||||
if (enum_value > TERM_NULL || enum_value < TERM_PLUS)
|
||||
return "invalid enum value";
|
||||
|
||||
static const char *const term_sign_str[TERM_NULL + 1] = {
|
||||
[TERM_PLUS] = "TERM_PLUS",
|
||||
[TERM_MINUS] = "TERM_MINUS",
|
||||
[TERM_NULL] = "TERM_NULL"};
|
||||
return term_sign_str[enum_value];
|
||||
}
|
||||
Reference in New Issue
Block a user