lang: lex: move structure definitions to a separate header

This commit is contained in:
2024-11-19 15:22:16 +00:00
parent 0f3328565e
commit 881f345bbe
2 changed files with 56 additions and 71 deletions

View File

@@ -9,6 +9,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;

55
lang/lex.h Normal file
View File

@@ -0,0 +1,55 @@
#ifndef _LEX_H_
#define _LEX_H_
#include <blue/core/queue.h>
#include <blue/object/string.h>
#include <blue/object/dict.h>
#include <stdint.h>
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