diff --git a/lang/lex.c b/lang/lex.c index b16447c..2e6fa3b 100644 --- a/lang/lex.c +++ b/lang/lex.c @@ -9,6 +9,7 @@ #include #include #include +#include "lex.h" #define LINEBUF_DEFAULT_CAPACITY 1024 @@ -17,52 +18,6 @@ .id = (i), .name = (n) \ } -struct ivy_lexer { - struct ivy_lexer_symbol_node *lex_sym_tree; - struct ivy_line_source *lex_source; - b_dict *lex_keywords; - enum ivy_status lex_status; - - struct ivy_token *lex_queue; - enum ivy_token_type lex_prev_token; - - b_string *lex_temp; - b_queue lex_state; - unsigned int lex_brace_depth; - - char *lex_linebuf; - size_t lex_linebuf_len; - size_t lex_linebuf_cap; - size_t lex_linebuf_ptr; -}; - -enum lexer_state_type { - STATE_NORMAL, - STATE_STRING, - STATE_FSTRING, - STATE_INTERPOLATION, -}; - -struct lexer_state { - enum lexer_state_type s_type; - unsigned int s_brace_depth; - b_queue_entry s_entry; -}; - -struct ivy_lexer_symbol_node { - char s_char; - enum ivy_symbol s_id; - - b_queue_entry s_entry; - b_queue s_children; -}; - -struct lex_token_def { - int id; - const char *name; - uint64_t name_hash; -}; - static struct lex_token_def keywords[] = { LEX_TOKEN_DEF(IVY_KW_PACKAGE, "package"), LEX_TOKEN_DEF(IVY_KW_USE, "use"), @@ -282,29 +237,6 @@ static struct ivy_lexer_symbol_node *build_symbol_tree(void) return root; } -static void print_symbol_node(struct ivy_lexer_symbol_node *node, int depth) -{ - for (int i = 0; i < depth; i++) { - fputs(" ", stdout); - } - - printf("%c", node->s_char); - - if (node->s_id != IVY_SYM_NONE) { - printf(" (%s)", ivy_symbol_to_string(node->s_id)); - } - - printf("\n"); - - b_queue_iterator it; - b_queue_foreach (&it, &node->s_children) { - struct ivy_lexer_symbol_node *child = b_unbox( - struct ivy_lexer_symbol_node, it.entry, s_entry); - - print_symbol_node(child, depth + 1); - } -} - static void init_keywords(b_dict *keyword_dict) { for (size_t i = 0; i < nr_keywords; i++) { @@ -349,8 +281,6 @@ enum ivy_status ivy_lexer_create(struct ivy_lexer **lexp) return IVY_ERR_NO_MEMORY; } - print_symbol_node(lex->lex_sym_tree, 0); - lex->lex_keywords = b_dict_create(); init_keywords(lex->lex_keywords); *lexp = lex; diff --git a/lang/lex.h b/lang/lex.h new file mode 100644 index 0000000..c48d717 --- /dev/null +++ b/lang/lex.h @@ -0,0 +1,55 @@ +#ifndef _LEX_H_ +#define _LEX_H_ + +#include +#include +#include +#include + +struct ivy_lexer { + struct ivy_lexer_symbol_node *lex_sym_tree; + struct ivy_line_source *lex_source; + b_dict *lex_keywords; + enum ivy_status lex_status; + + struct ivy_token *lex_queue; + enum ivy_token_type lex_prev_token; + + b_string *lex_temp; + b_queue lex_state; + unsigned int lex_brace_depth; + + char *lex_linebuf; + size_t lex_linebuf_len; + size_t lex_linebuf_cap; + size_t lex_linebuf_ptr; +}; + +enum lexer_state_type { + STATE_NORMAL, + STATE_STRING, + STATE_FSTRING, + STATE_INTERPOLATION, +}; + +struct lexer_state { + enum lexer_state_type s_type; + unsigned int s_brace_depth; + b_queue_entry s_entry; +}; + +struct ivy_lexer_symbol_node { + char s_char; + enum ivy_symbol s_id; + + b_queue_entry s_entry; + b_queue s_children; +}; + +struct lex_token_def { + int id; + const char *name; + uint64_t name_hash; +}; + +#endif \ No newline at end of file