renamed enum and struct
This commit is contained in:
@@ -12,7 +12,7 @@
|
|||||||
* LEXER.C
|
* LEXER.C
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef enum t_token_type
|
typedef enum
|
||||||
{
|
{
|
||||||
TOKEN_VARIABLE, // x, y, etc.
|
TOKEN_VARIABLE, // x, y, etc.
|
||||||
TOKEN_NUMBER_INT, // int
|
TOKEN_NUMBER_INT, // int
|
||||||
@@ -24,63 +24,76 @@ typedef enum t_token_type
|
|||||||
TOKEN_FACTOR_DIV, // / or :
|
TOKEN_FACTOR_DIV, // / or :
|
||||||
TOKEN_EQUAL, // =
|
TOKEN_EQUAL, // =
|
||||||
TOKEN_END // null (end of input)
|
TOKEN_END // null (end of input)
|
||||||
} token_type;
|
} e_token_type;
|
||||||
|
|
||||||
typedef enum t_token_tag
|
typedef enum
|
||||||
{
|
{
|
||||||
TOKEN_NO_TAG,
|
TOKEN_NO_TAG,
|
||||||
TOKEN_NUMBER,
|
TOKEN_NUMBER,
|
||||||
TOKEN_SIGN,
|
TOKEN_SIGN,
|
||||||
TOKEN_FACTOR,
|
TOKEN_FACTOR,
|
||||||
} token_tag;
|
} e_token_tag;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
token_type type;
|
e_token_type type;
|
||||||
token_tag tag;
|
e_token_tag tag;
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
char value_char;
|
char value_char;
|
||||||
int value_int;
|
int value_int;
|
||||||
double value_double;
|
double value_double;
|
||||||
};
|
};
|
||||||
} token;
|
} s_token;
|
||||||
|
|
||||||
int lexerize(const char *input, token *tokens);
|
int lexerize(const char *input, s_token *tokens);
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* PARSER.C
|
* PARSER.C
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef enum t_term_position
|
typedef enum
|
||||||
{
|
{
|
||||||
TERM_LEFT, // a in "a = b"
|
TERM_LEFT, // a in "a = b"
|
||||||
TERM_RIGHT, // b in "a = b"
|
TERM_RIGHT, // b in "a = b"
|
||||||
TERM_END, // last term
|
TERM_END, // last term
|
||||||
} term_position;
|
} e_term_position;
|
||||||
|
|
||||||
typedef enum t_term_sign
|
typedef enum
|
||||||
{
|
{
|
||||||
TERM_PLUS, // +
|
TERM_PLUS, // +
|
||||||
TERM_MINUS, // -
|
TERM_MINUS, // -
|
||||||
TERM_NULL, // null -> for the last term
|
TERM_NULL, // null -> for the last term
|
||||||
} term_sign;
|
} e_term_sign;
|
||||||
|
|
||||||
typedef struct t_term
|
typedef struct
|
||||||
{
|
{
|
||||||
term_position position;
|
e_term_position position;
|
||||||
term_sign sign;
|
e_term_sign sign;
|
||||||
double coefficient;
|
double coefficient;
|
||||||
int exponent;
|
int exponent;
|
||||||
} term;
|
} s_term;
|
||||||
|
|
||||||
int parse(token *tokens, term *terms, int terms_count_max);
|
int parse(s_token *tokens, s_term *terms, int terms_count_max);
|
||||||
|
|
||||||
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* REDUCE.C
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
double a;
|
||||||
|
double b;
|
||||||
|
double c;
|
||||||
|
} s_polynom;
|
||||||
|
|
||||||
|
void reduce(s_term *terms, s_polynom *polynom);
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* UTILS/ERRORS.C
|
* UTILS/ERRORS.C
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef enum t_program_error
|
typedef enum
|
||||||
{
|
{
|
||||||
ERROR_BASE = 1, // start at 1 to avoid exit(0) for errors
|
ERROR_BASE = 1, // start at 1 to avoid exit(0) for errors
|
||||||
ERROR_TOKEN_COUNT,
|
ERROR_TOKEN_COUNT,
|
||||||
@@ -91,26 +104,27 @@ typedef enum t_program_error
|
|||||||
ERROR_TOKEN_POSITION,
|
ERROR_TOKEN_POSITION,
|
||||||
ERROR_VAR_DIFF,
|
ERROR_VAR_DIFF,
|
||||||
ERROR_SENTINEL, // last token not used, only for enum count
|
ERROR_SENTINEL, // last token not used, only for enum count
|
||||||
} program_error;
|
} e_program_error;
|
||||||
|
|
||||||
void print_state();
|
void print_state();
|
||||||
int stop_errors(program_error err, const char *format, ...);
|
int stop_errors(e_program_error err, const char *format, ...);
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* UTILS/PRINT_ENUMS.C
|
* UTILS/PRINT_ENUMS.C
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const char *token_type_to_str(token_type type);
|
const char *token_type_to_str(e_token_type type);
|
||||||
const char *token_tag_to_str(token_tag tag);
|
const char *token_tag_to_str(e_token_tag tag);
|
||||||
const char *term_position_to_str(term_position pos);
|
const char *term_position_to_str(e_term_position pos);
|
||||||
const char *term_sign_to_str(term_sign sign);
|
const char *term_sign_to_str(e_term_sign sign);
|
||||||
|
|
||||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
* GLOBALS
|
* GLOBALS
|
||||||
*/
|
*/
|
||||||
|
|
||||||
extern char *input_g_err;
|
extern char *input_g_err;
|
||||||
extern token *tokens_g_err;
|
extern s_token *tokens_g_err;
|
||||||
extern term *terms_g_err;
|
extern s_term *terms_g_err;
|
||||||
|
extern s_term *polynom_g_err;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -7,8 +7,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
char *input_g_err;
|
char *input_g_err;
|
||||||
token *tokens_g_err;
|
s_token *tokens_g_err;
|
||||||
term *terms_g_err;
|
s_term *terms_g_err;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PROGRAM
|
* PROGRAM
|
||||||
@@ -50,7 +50,7 @@ static size_t count_any_of(const char *s, const char *set)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tokens_fill_null(token *tokens, size_t arg_len)
|
static void tokens_fill_null(s_token *tokens, size_t arg_len)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ static void tokens_fill_null(token *tokens, size_t arg_len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void terms_fill_null(term *terms, size_t terms_count_prediction)
|
static void terms_fill_null(s_term *terms, size_t terms_count_prediction)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ int main(int ac, char **av)
|
|||||||
// lexerize
|
// lexerize
|
||||||
arg_len = ft_strlen(input) + 1; // +1 for last END token
|
arg_len = ft_strlen(input) + 1; // +1 for last END token
|
||||||
ft_printf("-> tokens[%i]\n", arg_len); // debug
|
ft_printf("-> tokens[%i]\n", arg_len); // debug
|
||||||
token tokens[arg_len];
|
s_token tokens[arg_len];
|
||||||
tokens_g_err = tokens;
|
tokens_g_err = tokens;
|
||||||
tokens_fill_null(tokens, arg_len);
|
tokens_fill_null(tokens, arg_len);
|
||||||
ret = lexerize(input, tokens);
|
ret = lexerize(input, tokens);
|
||||||
@@ -111,7 +111,7 @@ int main(int ac, char **av)
|
|||||||
// parse
|
// parse
|
||||||
terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL
|
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
|
ft_printf("-> terms[%i]\n", terms_count_prediction); // debug
|
||||||
term terms[terms_count_prediction];
|
s_term terms[terms_count_prediction];
|
||||||
terms_g_err = terms;
|
terms_g_err = terms;
|
||||||
terms_fill_null(terms, terms_count_prediction);
|
terms_fill_null(terms, terms_count_prediction);
|
||||||
ret = parse(tokens, terms, terms_count_prediction);
|
ret = parse(tokens, terms, terms_count_prediction);
|
||||||
@@ -120,6 +120,10 @@ int main(int ac, char **av)
|
|||||||
stop_errors(ERROR_TERM_COUNT, "parser returned 0 term");
|
stop_errors(ERROR_TERM_COUNT, "parser returned 0 term");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reduce
|
||||||
|
s_polynom *polynom;
|
||||||
|
reduce(terms, polynom);
|
||||||
|
|
||||||
// debug
|
// debug
|
||||||
print_state();
|
print_state();
|
||||||
|
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ static bool token_is_equal(const char *input, int input_pos, int *token_size)
|
|||||||
/**
|
/**
|
||||||
* LEXER
|
* LEXER
|
||||||
*/
|
*/
|
||||||
int lexerize(const char *input, token *tokens)
|
int lexerize(const char *input, s_token *tokens)
|
||||||
{
|
{
|
||||||
int tokens_count;
|
int tokens_count;
|
||||||
int input_pos;
|
int input_pos;
|
||||||
|
|||||||
16
src/parser.c
16
src/parser.c
@@ -13,7 +13,7 @@
|
|||||||
NT | ! NUMBER | NT | ! SIGN | FACTOR | NT | NT
|
NT | ! NUMBER | NT | ! SIGN | FACTOR | NT | NT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static term_sign get_sign(token *tokens, int i, int *token_count)
|
static e_term_sign get_sign(s_token *tokens, int i, int *token_count)
|
||||||
{
|
{
|
||||||
// // forbidden tokens
|
// // forbidden tokens
|
||||||
// if (tokens[i].type == TOKEN_POWER)
|
// if (tokens[i].type == TOKEN_POWER)
|
||||||
@@ -52,7 +52,7 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
|
|||||||
return tokens[i].type == TOKEN_SIGN_PLUS ? TERM_PLUS : TERM_MINUS;
|
return tokens[i].type == TOKEN_SIGN_PLUS ? TERM_PLUS : TERM_MINUS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static double get_double_value(token token)
|
static double get_double_value(s_token token)
|
||||||
{
|
{
|
||||||
if (token.tag != TOKEN_NUMBER)
|
if (token.tag != TOKEN_NUMBER)
|
||||||
{
|
{
|
||||||
@@ -66,7 +66,7 @@ static double get_double_value(token token)
|
|||||||
return token.value_int;
|
return token.value_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
static double get_coefficient(token *tokens, int i, int *token_count)
|
static double get_coefficient(s_token *tokens, int i, int *token_count)
|
||||||
{
|
{
|
||||||
double coefficient;
|
double coefficient;
|
||||||
|
|
||||||
@@ -129,7 +129,7 @@ static double get_coefficient(token *tokens, int i, int *token_count)
|
|||||||
return coefficient;
|
return coefficient;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_exponent(token *tokens, int i, int *token_count)
|
static int get_exponent(s_token *tokens, int i, int *token_count)
|
||||||
{
|
{
|
||||||
// // forbidden tokens
|
// // forbidden tokens
|
||||||
// if (tokens[i].type == TOKEN_POWER)
|
// if (tokens[i].type == TOKEN_POWER)
|
||||||
@@ -197,7 +197,7 @@ static int get_exponent(token *tokens, int i, int *token_count)
|
|||||||
return tokens[i].value_int;
|
return tokens[i].value_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void check_variables(token *tokens)
|
static void check_variables(s_token *tokens)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char var;
|
char var;
|
||||||
@@ -222,12 +222,12 @@ static void check_variables(token *tokens)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse(token *tokens, term *terms, int terms_count_max)
|
int parse(s_token *tokens, s_term *terms, int terms_count_max)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int terms_count;
|
int terms_count;
|
||||||
int token_count;
|
int token_count;
|
||||||
term_position term_position;
|
e_term_position term_position;
|
||||||
|
|
||||||
check_variables(tokens);
|
check_variables(tokens);
|
||||||
|
|
||||||
@@ -251,7 +251,7 @@ int parse(token *tokens, term *terms, int terms_count_max)
|
|||||||
terms[terms_count].position = term_position;
|
terms[terms_count].position = term_position;
|
||||||
|
|
||||||
// sign
|
// sign
|
||||||
term_sign ret_sign = get_sign(tokens, i, &token_count);
|
e_term_sign ret_sign = get_sign(tokens, i, &token_count);
|
||||||
terms[terms_count].sign = ret_sign;
|
terms[terms_count].sign = ret_sign;
|
||||||
i += token_count;
|
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); // debug
|
// ft_printf("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // debug
|
||||||
|
|||||||
7
src/reduce.c
Normal file
7
src/reduce.c
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
/* reduce.c */
|
||||||
|
|
||||||
|
#include "computorv1.h"
|
||||||
|
|
||||||
|
void reduce(s_term *terms, s_polynom *polynom)
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -106,7 +106,7 @@ void print_state()
|
|||||||
print_context_terms();
|
print_context_terms();
|
||||||
}
|
}
|
||||||
|
|
||||||
int stop_errors(program_error err, const char *details, ...)
|
int stop_errors(e_program_error err, const char *details, ...)
|
||||||
{
|
{
|
||||||
// the base error message
|
// the base error message
|
||||||
const char *msg = "error: error type is out of range";
|
const char *msg = "error: error type is out of range";
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "computorv1.h"
|
#include "computorv1.h"
|
||||||
|
|
||||||
const char *token_type_to_str(token_type enum_value)
|
const char *token_type_to_str(e_token_type enum_value)
|
||||||
{
|
{
|
||||||
if (enum_value > TOKEN_END || enum_value < TOKEN_VARIABLE)
|
if (enum_value > TOKEN_END || enum_value < TOKEN_VARIABLE)
|
||||||
return "invalid enum value";
|
return "invalid enum value";
|
||||||
@@ -21,7 +21,7 @@ const char *token_type_to_str(token_type enum_value)
|
|||||||
return token_type_str[enum_value];
|
return token_type_str[enum_value];
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *token_tag_to_str(token_tag enum_value)
|
const char *token_tag_to_str(e_token_tag enum_value)
|
||||||
{
|
{
|
||||||
if (enum_value > TOKEN_FACTOR || enum_value < TOKEN_NO_TAG)
|
if (enum_value > TOKEN_FACTOR || enum_value < TOKEN_NO_TAG)
|
||||||
return "invalid enum value";
|
return "invalid enum value";
|
||||||
@@ -34,7 +34,7 @@ const char *token_tag_to_str(token_tag enum_value)
|
|||||||
return token_tag_str[enum_value];
|
return token_tag_str[enum_value];
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *term_position_to_str(term_position enum_value)
|
const char *term_position_to_str(e_term_position enum_value)
|
||||||
{
|
{
|
||||||
if (enum_value > TERM_END || enum_value < TERM_LEFT)
|
if (enum_value > TERM_END || enum_value < TERM_LEFT)
|
||||||
return "invalid enum value";
|
return "invalid enum value";
|
||||||
@@ -46,7 +46,7 @@ const char *term_position_to_str(term_position enum_value)
|
|||||||
return term_position_str[enum_value];
|
return term_position_str[enum_value];
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *term_sign_to_str(term_sign enum_value)
|
const char *term_sign_to_str(e_term_sign enum_value)
|
||||||
{
|
{
|
||||||
if (enum_value > TERM_NULL || enum_value < TERM_PLUS)
|
if (enum_value > TERM_NULL || enum_value < TERM_PLUS)
|
||||||
return "invalid enum value";
|
return "invalid enum value";
|
||||||
|
|||||||
Reference in New Issue
Block a user