Files
42_EXT_05_computorv1/headers/computorv1.h
2026-05-03 20:42:18 +02:00

115 lines
2.5 KiB
C

/* computorv1.h */
#ifndef COMPUTORV1_H
#define COMPUTORV1_H
#include "libft.h"
#include <stdio.h> // tmp for printf, for float debug
#include <stdarg.h> // for va_list
#include <stdbool.h>
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* LEXER.C
*/
typedef enum t_token_type
{
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 t_token_tag
{
TOKEN_NO_TAG,
TOKEN_NUMBER,
TOKEN_SIGN,
TOKEN_FACTOR,
} token_tag;
typedef struct
{
token_type type;
token_tag tag;
union
{
char value_char;
int value_int;
double value_double;
};
} token;
int lexerize(const char *input, token *tokens);
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* PARSER.C
*/
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 t_term_sign
{
TERM_PLUS, // +
TERM_MINUS, // -
TERM_NULL, // null -> for the last term
} term_sign;
typedef struct t_term
{
term_position position;
term_sign sign;
double coefficient;
int exponent;
} term;
int parse(token *tokens, term *terms, int terms_count_max);
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* UTILS/ERRORS.C
*/
typedef enum t_program_error
{
ERROR_BASE = 1, // start at 1 to avoid exit(0) for errors
ERROR_TOKEN_COUNT,
ERROR_UNKNOWN_TOKEN,
ERROR_NUMBER_TOO_BIG,
ERROR_PARSING,
ERROR_TERM_COUNT,
ERROR_TOKEN_POSITION,
ERROR_VAR_DIFF,
ERROR_SENTINEL, // last token not used, only for enum count
} program_error;
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
*/
extern char *input_g_err;
extern token *tokens_g_err;
extern term *terms_g_err;
#endif