summaryrefslogtreecommitdiff
path: root/source/tb_db/tb_db.c
diff options
context:
space:
mode:
authornasr <nsrddyn@gmail.com>2026-04-13 14:58:49 +0200
committernasr <nsrddyn@gmail.com>2026-04-13 14:59:10 +0200
commita9cb228861a6b0fad4d508c05c0614757a7f0a34 (patch)
tree281ae48c7248413cae727b403a1cd802741b061d /source/tb_db/tb_db.c
parent65907835d9835d85cff31269db19e18045cb3392 (diff)
refactor(main): refactor directory structuremain
Diffstat (limited to 'source/tb_db/tb_db.c')
-rw-r--r--source/tb_db/tb_db.c198
1 files changed, 198 insertions, 0 deletions
diff --git a/source/tb_db/tb_db.c b/source/tb_db/tb_db.c
new file mode 100644
index 0000000..4ae247d
--- /dev/null
+++ b/source/tb_db/tb_db.c
@@ -0,0 +1,198 @@
1#define BTREE_IMPLEMENTATION
2#define BASE_UNITY
3#include "../base/base_include.h"
4
5internal b32
6is_alpha(u8 point)
7{
8 return ((point >= 'a' && point <= 'z') || (point >= 'A' && point <= 'Z') || (point == '_'));
9}
10
11internal b32
12is_digit(u8 point)
13{
14 return (point >= '0' && point <= '9');
15}
16
17internal b32
18is_alpha_num(u8 point)
19{
20 return (is_alpha(point) || is_digit(point));
21}
22
23internal b32
24is_whitespace(u8 point)
25{
26 return (point == '\n' || point == '\r' || point == ' ' || point == '\t');
27}
28
29internal b32
30is_delimiter(u8 point)
31{
32 return (point == ',');
33}
34
35#include "btree_impl.h"
36#include "csv_decoder.h"
37
38typedef struct query_token query_token;
39struct query_token
40{
41 string8 lexeme;
42 query_token *next;
43};
44
45typedef struct query_token_list query_token_list;
46struct query_token_list
47{
48 query_token *start_token;
49 query_token *current_token;
50};
51
52read_only global_variable
53query_token nil_query_token =
54{
55 .lexeme = {.data = NULL, .size = 0},
56 .next = &nil_query_token
57};
58
59
60read_only global_variable
61query_token_list nil_query_token_list =
62{
63 .start_token = &nil_query_token,
64 .current_token = &nil_query_token,
65};
66
67internal b32
68is_nil_query_token(query_token *token)
69{
70 return (token == &nil_query_token) || (token == NULL);
71}
72
73internal b32
74is_nil_query_token_list(query_token *token)
75{
76 return (token == &nil_query_token) || (token == NULL);
77}
78
79// takes on line of the repl input
80// return a reference to the passed list
81internal query_token_list *
82query_tokenizer(mem_arena *arena, string8 *buffer, query_token_list *list)
83{
84 b32 initialized = 0;
85 unused(initialized);
86
87 for (u64 index = 0; index < buffer->size; ++index)
88 {
89 u8 codepoint = buffer->data[index];
90
91 if(codepoint == '\n' || codepoint == '\r') break;
92
93 s32 start = 0;
94 s32 end = 0;
95
96 if(is_whitespace(codepoint)) end = index;
97
98 // save the token
99 // TODO(nasr): work on the string macros cuz no work
100 {
101 query_token *new_token = PushStruct(arena, query_token);
102
103 //- initialize list
104 {
105 if(is_nil_query_token(list->start_token))
106 {
107 list->start_token = new_token;
108 list->current_token = new_token;
109 }
110 else
111 {
112 //- all we need to do - we dont track parents or what ever. this is a token stream not a tree
113 list->current_token->next = new_token;
114 }
115 }
116
117 s32 new_token_size = end - start;
118
119 new_token->lexeme = PushString(arena, new_token_size);
120 new_token->lexeme.data = &buffer->data[index];
121 new_token->lexeme.size = new_token_size;
122
123 list->current_token->next = new_token;
124
125 start = index + 1;
126 }
127 }
128
129 return list;
130}
131
132int main(int count, char **value)
133{
134#if 1
135 unused(nil_query_token_list);
136#endif
137
138 if(count < 2) value[1] = "./test/data.csv";
139
140 local_persist b32 running = 1;
141
142 mem_arena *global_arena = arena_create(GiB(1));
143
144 string8 buffer = load_file(global_arena, value[1]);
145
146 // NOTE(nasr): the use of tables is required for tracking headers etc.
147 // i think we can optimize this away in the future but for now its fine
148 csv_table *table = PushStructZero(global_arena, csv_table);
149 csv_token_list *token_list = PushStructZero(global_arena, csv_token_list);
150
151 token_list->start_token = &nil_csv_token;
152 token_list->end_token = &nil_csv_token;
153
154 csv_token *tokens = tokenize_csv(buffer, global_arena, table, token_list);
155 assert_msg(tokens != NULL, "tokens are null");
156
157 // NOTE(nasr): token_list is now populated — pass it directly, not a fresh empty list
158 btree *bt = parse_csv(global_arena, token_list, table);
159
160 print("\nDatabase Engine\n");
161
162 for(;;)
163 {
164 if(running)
165 {
166 u8 *lbuf = PushArray(global_arena, u8, 256);
167 s32 err = os_read(STDIN_FD, lbuf, 256);
168
169 if(err < 0)
170 {
171 print("error reading from stdin");
172 }
173
174 // TODO(nasr): extract this later in the future and make a string copy function/macro
175 // @params (s32 lbuf_size, string8 lbuf_stringified)
176 // NOTE(nasr): use err (bytes read) not sizeof(lbuf*) — sizeof a pointer is always 8
177 s32 lbuf_size = err;
178 string8 lbuf_stringified = PushString(global_arena, lbuf_size);
179 {
180 memcpy(lbuf_stringified.data, lbuf, lbuf_size);
181 lbuf_stringified.size = lbuf_size;
182 }
183
184 query_token_list *qtl = PushStructZero(global_arena, query_token_list);
185
186
187 query_tokenizer(global_arena, &lbuf_stringified, qtl);
188
189 // TODO(nasr): dispatch qtl against bt here
190
191 unused(bt);
192
193 sleep(1);
194 }
195 }
196
197 return 0;
198}