PC_graph/lex.h
2024-11-28 14:49:19 +01:00

70 lines
1.2 KiB
C

#ifndef LEX_H
#define LEX_H
#include "error_buffer.h"
#define LEX_DEBUG
enum token_type {
TOK_EOF,
TOK_ERROR,
TOK_NUMBER,
TOK_PLUS,
TOK_MINUS,
TOK_MULTIPLY,
TOK_DIVIDE,
TOK_POWER,
TOK_X,
TOK_FUNCTION,
TOK_LEFT_PAREN,
TOK_RIGHT_PAREN
};
enum math_fn {
FN_ABS,
FN_EXP,
FN_LN,
FN_LOG,
FN_SIN,
FN_COS,
FN_TAN,
FN_ASIN,
FN_ACOS,
FN_ATAN,
FN_SINH,
FN_COSH,
FN_TANH
};
struct token {
enum token_type type;
union token_val {
double num;
enum math_fn fn;
} val;
};
struct lexer {
const char *start;
const char *prev_p;
const char *p;
struct token tok;
struct error_buffer eb;
};
void lex_init(struct lexer *lex, const char *str);
void lex_next(struct lexer *lex);
struct token *lex_token(struct lexer *lex);
void lex_print_position(const struct lexer *lex, struct error_buffer *eb);
enum error_code lex_get_error(const struct lexer *lex);
const char *lex_get_error_text(const struct lexer *lex);
#ifdef LEX_DEBUG
void lex_debug_print_token(const struct token *tok);
#endif /* LEX_DEBUG */
#endif /* LEX_H */