60 lines
1.1 KiB
C
60 lines
1.1 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_VARIABLE,
|
|
TOK_FUNCTION,
|
|
|
|
TOK_LEFT_PAREN,
|
|
TOK_RIGHT_PAREN,
|
|
TOK_COMMA
|
|
};
|
|
|
|
struct token {
|
|
enum token_type type;
|
|
union token_val {
|
|
double num;
|
|
size_t fn_idx;
|
|
} val;
|
|
};
|
|
|
|
struct lexer {
|
|
const char *start;
|
|
const char *prev_p;
|
|
const char *p;
|
|
struct token tok;
|
|
struct error_buffer eb;
|
|
const char *variable_name;
|
|
};
|
|
|
|
void lex_init(struct lexer *lex, const char *str, const char *variable_name);
|
|
|
|
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);
|
|
|
|
const char *lex_token_str(enum token_type token);
|
|
|
|
#ifdef LEX_DEBUG
|
|
void lex_debug_print_token(const struct token *tok);
|
|
#endif /* LEX_DEBUG */
|
|
|
|
#endif /* LEX_H */ |