summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile2
-rw-r--r--source/b_tree.c (renamed from source/storage/b_tree.c)27
-rw-r--r--source/b_tree.h (renamed from source/storage/b_tree.h)4
-rwxr-xr-xsource/base/base.h16
-rwxr-xr-xsource/base/base_include.h2
-rw-r--r--source/base/base_io.h29
-rw-r--r--source/base/base_os.h10
-rw-r--r--source/base/base_string.h15
-rw-r--r--source/base/base_test.h2
-rw-r--r--source/csv_parser.c32
-rw-r--r--source/csv_parser.h (renamed from source/storage/csv_reader.h)15
-rw-r--r--source/engine.c46
-rw-r--r--source/engine/engine.c37
-rw-r--r--source/lexer.c (renamed from source/lexer/lexer.c)56
-rw-r--r--source/lexer.h (renamed from source/lexer/lexer.h)11
-rw-r--r--source/parser/parser.c16
-rw-r--r--source/parser/parser.h16
-rw-r--r--source/query.c (renamed from source/query/query.c)0
-rw-r--r--source/query.h (renamed from source/query/query.h)0
-rw-r--r--source/repl/repl.c12
-rw-r--r--source/repl/repl.h16
-rw-r--r--source/storage/csv_reader.c7
-rw-r--r--source/storage/table.c0
-rw-r--r--source/storage/table.h0
25 files changed, 196 insertions, 176 deletions
diff --git a/.gitignore b/.gitignore
index 00fe5ef..05a7b85 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,3 +9,4 @@ idea/*
9/helper.sh 9/helper.sh
10/tests 10/tests
11/tags 11/tags
12/.cache/clangd/index
diff --git a/Makefile b/Makefile
index f9c5906..0224a42 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
1BIN = build/engine 1BIN = build/engine
2SRC = source/engine/engine.c 2SRC = source/engine.c
3CC = clang 3CC = clang
4CFLAGS = -Wall -Wextra -Wfloat-equal -Wswitch-default -Wswitch-enum \ 4CFLAGS = -Wall -Wextra -Wfloat-equal -Wswitch-default -Wswitch-enum \
5 -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-unused-function -g -Werror 5 -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-unused-function -g -Werror
diff --git a/source/storage/b_tree.c b/source/b_tree.c
index 6a0e76d..e42f709 100644
--- a/source/storage/b_tree.c
+++ b/source/b_tree.c
@@ -10,10 +10,10 @@ node_alloc(mem_arena *arena)
10} 10}
11 11
12// NOTE(nasr): @return the index of of the found element 12// NOTE(nasr): @return the index of of the found element
13internal i32 13internal s32
14node_find_pos(mem_arena *arena, string8 value) 14node_find_pos(mem_arena *arena, string8 value)
15{ 15{
16 i32 i = 0; 16 s32 i = 0;
17 while (i < n->key_count && str8_cmp(n->keys[i], k) < 0) 17 while (i < n->key_count && str8_cmp(n->keys[i], k) < 0)
18 { 18 {
19 ++i; 19 ++i;
@@ -22,10 +22,31 @@ node_find_pos(mem_arena *arena, string8 value)
22 return i; 22 return i;
23} 23}
24 24
25interal void
26b_tree_create(mem_arena *arena, b_tree *tree)
27{
28 tree->root = node_alloc(arena);
29 tree->root->leaf = 1;
30 tree->root->key_count = 0;
31}
32
25// NOTE(nasr): nodes that get passed as parameters should've already been loaded into memory 33// NOTE(nasr): nodes that get passed as parameters should've already been loaded into memory
26internal void 34internal void
27b_tree_search(node *node) 35b_tree_search(b_tree_node *node, string8 key)
28{ 36{
37 s32 found_index = node_find_pos(node, key);
38
39 if (found_index < n->key_count && string_compare(n->keys[i], key) == 0)
40 {
41 return n->rows[i];
42 }
43 if (n->leaf)
44 {
45 return NULL;
46
47 }
48
49 return b_tree_search(n->children[i], key);
29 50
30 51
31} 52}
diff --git a/source/storage/b_tree.h b/source/b_tree.h
index 10ad00d..0fc4d3c 100644
--- a/source/storage/b_tree.h
+++ b/source/b_tree.h
@@ -21,9 +21,9 @@ struct b_tree_node
21 21
22 // NOTE(nasr): reference count ::: check how many leaves are using this node 22 // NOTE(nasr): reference count ::: check how many leaves are using this node
23 // also not needed for now because we don't free individual node because of arena allocator 23 // also not needed for now because we don't free individual node because of arena allocator
24 // i32 *refc; 24 // s32 *refc;
25 25
26 i32 key_count; 26 s32 key_count;
27 27
28 b32 leaf; 28 b32 leaf;
29 29
diff --git a/source/base/base.h b/source/base/base.h
index ef23391..cf2a15f 100755
--- a/source/base/base.h
+++ b/source/base/base.h
@@ -52,22 +52,22 @@ typedef uint32_t u32;
52typedef uint16_t u16; 52typedef uint16_t u16;
53typedef uint8_t u8; 53typedef uint8_t u8;
54 54
55typedef int8_t i8; 55typedef int8_t s8;
56typedef int16_t i16; 56typedef int16_t s16;
57typedef int32_t i32; 57typedef int32_t s32;
58typedef int64_t i64; 58typedef int64_t s64;
59 59
60typedef float f32; 60typedef float f32;
61typedef double f64; 61typedef double f64;
62 62
63typedef i32 b32; 63typedef s32 b32;
64typedef i16 b16; 64typedef s16 b16;
65typedef u8 b8; 65typedef u8 b8;
66 66
67typedef uintptr_t umm; 67typedef uintptr_t umm;
68typedef intptr_t smm; 68typedef intptr_t smm;
69 69
70#define TRUE (0 == 0) 70#define TRUE (1 == 1)
71#define FALSE (0 != 0) 71#define FALSE (1 != 1)
72 72
73#endif 73#endif
diff --git a/source/base/base_include.h b/source/base/base_include.h
index 40ae5ea..07856d0 100755
--- a/source/base/base_include.h
+++ b/source/base/base_include.h
@@ -4,6 +4,7 @@
4#include <dirent.h> 4#include <dirent.h>
5#include <sys/mman.h> 5#include <sys/mman.h>
6#include <sys/stat.h> 6#include <sys/stat.h>
7#include <sys/syscall.h>
7#include <fcntl.h> 8#include <fcntl.h>
8#include <stdint.h> 9#include <stdint.h>
9#include <stddef.h> 10#include <stddef.h>
@@ -16,6 +17,7 @@
16#include "base_stack.h" 17#include "base_stack.h"
17#include "base_test.h" 18#include "base_test.h"
18#include "base_string.h" 19#include "base_string.h"
20#include "base_io.h"
19#include "base_os.h" 21#include "base_os.h"
20 22
21#ifdef BASE_UNITY 23#ifdef BASE_UNITY
diff --git a/source/base/base_io.h b/source/base/base_io.h
index ece4d7c..85fedc7 100644
--- a/source/base/base_io.h
+++ b/source/base/base_io.h
@@ -1,11 +1,38 @@
1#ifndef BASE_IO_H 1#ifndef BASE_IO_H
2#define BASE_IO_H 2#define BASE_IO_H
3 3
4#define STDIN_FD 0
5#define STDOUT_FD 1
6#define STDERR_FD 2
7
8internal s64
9os_write(s32 fd, void const *buf, u64 count)
10{
11 return syscall(SYS_write, fd, buf, count);
12}
13
14internal s64
15os_read(s32 fd, void *buf, u64 count)
16{
17 return syscall(SYS_read, fd, buf, count);
18}
19
4internal void 20internal void
5input_read() 21print_s8(string8 s)
6{ 22{
23 os_write(STDOUT_FILENO, s.data, s.size);
24}
7 25
26internal void
27print(const char *str)
28{
29 s32 len = 0;
30 while (str[len]) len++;
31 os_write(STDOUT_FILENO, str, len);
8 32
9} 33}
10 34
35#define Os_read(buffer, buffer_count)
36
37
11#endif /* BASE_IO_H */ 38#endif /* BASE_IO_H */
diff --git a/source/base/base_os.h b/source/base/base_os.h
index 23587c6..abe8628 100644
--- a/source/base/base_os.h
+++ b/source/base/base_os.h
@@ -1,21 +1,13 @@
1#ifndef BASE_OS_H 1#ifndef BASE_OS_H
2#define BASE_OS_H 2#define BASE_OS_H
3 3
4internal void
5print(const char *str)
6{
7 i32 len = 0;
8 while (str[len]) len++;
9 write(STDOUT_FILENO, str, len);
10}
11
12internal string8 4internal string8
13load_file(const char *path) 5load_file(const char *path)
14{ 6{
15 string8 result = {0}; 7 string8 result = {0};
16 struct stat sbuf = {0}; 8 struct stat sbuf = {0};
17 9
18 i32 file = open(path, O_RDONLY); 10 s32 file = open(path, O_RDONLY);
19 if(file == -1) return result; 11 if(file == -1) return result;
20 12
21 if(fstat(file, &sbuf) == -1) 13 if(fstat(file, &sbuf) == -1)
diff --git a/source/base/base_string.h b/source/base/base_string.h
index 189b38a..4e0d923 100644
--- a/source/base/base_string.h
+++ b/source/base/base_string.h
@@ -1,13 +1,18 @@
1#ifndef BASE_STRING_H 1#ifndef BASE_STRING_H
2#define BASE_STRING_H 2#define BASE_STRING_H
3 3
4#include <string.h>
5
6#define StringLit(string) \ 4#define StringLit(string) \
7 (string8){ .data = (u8 *)(string), .size = (sizeof(string) - 1) } 5 (string8){ .data = (u8 *)(string), .size = (sizeof(string) - 1) }
8 6
9 #define PushString(arena, size) \ 7#define PushString(arena, size) \
10 (string8){ (u8 *)PushArray((arena), u8, (size)), (u64)(size) } 8 ({ \
9 string8 *result = PushStruct((arena), string8); \
10 result->data = PushArray((arena), u8, (size)); \
11 result->size = (u64)(size); \
12 result; \
13 })
14
15#define String8Cast(literal, literal_count) ( string8 ){( u8 * )( literal ),( u64 )( literal_count ) }
11 16
12#define StringFmt "%.*s" 17#define StringFmt "%.*s"
13#define ULongFmt "%lu" 18#define ULongFmt "%lu"
@@ -50,10 +55,8 @@ string8_append_char(string8 *buf, u8 c)
50read_only global_variable 55read_only global_variable
51string8 nil_string = 56string8 nil_string =
52{ 57{
53
54 .data = NULL, 58 .data = NULL,
55 .size = 0, 59 .size = 0,
56
57}; 60};
58 61
59#endif /* BASE_STRING_H */ 62#endif /* BASE_STRING_H */
diff --git a/source/base/base_test.h b/source/base/base_test.h
index 412797b..7619cfb 100644
--- a/source/base/base_test.h
+++ b/source/base/base_test.h
@@ -9,7 +9,7 @@
9#define LEN(s) (sizeof(s) - 1) 9#define LEN(s) (sizeof(s) - 1)
10 10
11internal void 11internal void
12write_int(i32 num) 12write_int(s32 num)
13{ 13{
14 14
15 if (num < 0) 15 if (num < 0)
diff --git a/source/csv_parser.c b/source/csv_parser.c
new file mode 100644
index 0000000..048e089
--- /dev/null
+++ b/source/csv_parser.c
@@ -0,0 +1,32 @@
1internal void
2strip_new_line(string8 buffer)
3{
4
5 for (u64 index = 0; index < buffer.size; index++)
6 {
7
8 }
9
10 return;
11
12}
13
14internal void
15read_csv(string8 buffer)
16{
17 printf("\nsize:%lu\ndata %s\n", buffer.size, buffer.data);
18
19}
20
21internal csv_table *
22parse_csv(token *tokens, csv_table *table)
23{
24
25
26
27
28
29
30
31 return NULL;
32}
diff --git a/source/storage/csv_reader.h b/source/csv_parser.h
index 36e07a4..1aa96b9 100644
--- a/source/storage/csv_reader.h
+++ b/source/csv_parser.h
@@ -1,12 +1,12 @@
1#ifndef CSV_READER_H 1#ifndef CSV_PARSER_H
2#define CSV_READER_H 2#define CSV_PARSER_H
3 3
4typedef struct csv_row csv_row; 4typedef struct csv_row csv_row;
5struct csv_row 5struct csv_row
6{ 6{
7 // array of size col_count, points into mmap buffer 7 // array of size col_count, points into mmap buffer
8 string8 *fields; 8 string8 *fields;
9 i32 count; 9 s32 count;
10}; 10};
11 11
12typedef struct csv_table csv_table; 12typedef struct csv_table csv_table;
@@ -16,8 +16,8 @@ struct csv_table
16 // all data rows 16 // all data rows
17 string8 *headers; 17 string8 *headers;
18 csv_row *rows; 18 csv_row *rows;
19 i32 col_count; 19 s32 col_count;
20 i32 row_count; 20 s32 row_count;
21}; 21};
22 22
23read_only global_variable 23read_only global_variable
@@ -27,8 +27,6 @@ csv_row nil_csv_row =
27 .count = 0, 27 .count = 0,
28}; 28};
29 29
30
31
32read_only global_variable 30read_only global_variable
33csv_table nil_csv_table = 31csv_table nil_csv_table =
34{ 32{
@@ -39,5 +37,4 @@ csv_table nil_csv_table =
39}; 37};
40 38
41 39
42 40#endif /* CSV_PARSER_H */
43#endif /* CSV_READER_H */
diff --git a/source/engine.c b/source/engine.c
new file mode 100644
index 0000000..8f701e3
--- /dev/null
+++ b/source/engine.c
@@ -0,0 +1,46 @@
1#define BASE_UNITY
2#include "base/base_include.h"
3
4#include <stdio.h>
5
6#include "csv_parser.h"
7#include "lexer.h"
8
9#include "lexer.c"
10#include "csv_parser.c"
11
12int main(int c, char **v)
13{
14 if(c < 2) return -999;
15
16 b32 running = 0;
17
18 mem_arena *global_arena = arena_create(MiB(30));
19 csv_table *global_table = PushStruct(global_arena, csv_table);
20
21 string8 buffer = load_file(v[1]);
22
23 print("database engine in nasr");
24
25 for(;;)
26 {
27 if (running)
28 {
29 // TODO(nasr): check for return key
30 {
31 u8 line_buffer[256] = {};
32
33 s64 codepoint = os_read(STDIN_FD, line_buffer, 256);
34 unused(codepoint);
35
36 read_csv(buffer);
37 token *tokens = tokenize_csv(buffer, global_arena);
38 global_table = parse_csv(tokens, global_table);
39 }
40
41 sleep(1);
42 }
43 }
44
45 return 0;
46}
diff --git a/source/engine/engine.c b/source/engine/engine.c
deleted file mode 100644
index 18b52be..0000000
--- a/source/engine/engine.c
+++ /dev/null
@@ -1,37 +0,0 @@
1#define BASE_UNITY
2#include "../base/base_include.h"
3
4#include <stdio.h>
5
6
7
8#include "../parser/parser.h"
9#include "../parser/parser.c"
10
11#include "../repl/repl.h"
12#include "../repl/repl.c"
13
14#include "../storage/csv_reader.h"
15#include "../storage/csv_reader.c"
16
17#include "../lexer/lexer.h"
18#include "../lexer/lexer.c"
19
20
21
22int main(int c, char **v)
23{
24 if(c < 2) return -999;
25
26 mem_arena *global_arena = arena_create(MiB(30));
27 csv_table *global_table = PushStruct(global_arena, csv_table);
28
29 string8 buffer = load_file(v[1]);
30 // read_csv(buffer);
31 tokenize_csv(buffer, global_table, global_arena);
32
33
34 return 0;
35}
36
37
diff --git a/source/lexer/lexer.c b/source/lexer.c
index 948afd0..c84a831 100644
--- a/source/lexer/lexer.c
+++ b/source/lexer.c
@@ -32,19 +32,20 @@ is_delimiter(u8 point)
32} 32}
33 33
34internal token * 34internal token *
35tokenize_csv(string8 buffer, csv_table *global_table, mem_arena *arena) 35tokenize_csv(string8 buffer, mem_arena *arena)
36{ 36{
37 i32 count = 0; 37 s32 count = 0;
38 string8 **tokens = PushArray(arena, string8 *, buffer.size / 10); 38 string8 **tokens = PushString(arena, buffer.size);
39 b32 first_line = 1; 39
40 b32 FL = TRUE;
40 41
41 if(buffer.size < 0) return NULL; 42 if(buffer.size < 0) return NULL;
42 for(i32 index = 0; 43 for(s32 index = 0;
43 buffer.data[index] != '\0'; 44 buffer.data[index] != '\0';
44 ++index) 45 ++index)
45 { 46 {
46 csv_row *row = PushStruct(arena, csv_row); 47 csv_row *row = PushStruct(arena, csv_row);
47 string8 token = {0}; 48 token *tok = PushStruct(arena, token);
48 49
49 u8 point = buffer.data[index]; 50 u8 point = buffer.data[index];
50 51
@@ -53,28 +54,25 @@ tokenize_csv(string8 buffer, csv_table *global_table, mem_arena *arena)
53 54
54 unused(row); 55 unused(row);
55 56
56 switch (point) 57 switch(point)
57 { 58 {
58 case '\n': 59 case('\n'):
59 {
60 first_line = -1;
61 break;
62 }
63 case ',':
64 { 60 {
65 end = start - 1;
66 61
67 if (first_line) 62 if(FL)
68 { 63 {
69 global_table->headers = &token; 64 FL = FALSE;
70 ++global_table->headers; 65 tok->flags |= END_FL;
71 break;
72 } 66 }
73 else
74 {
75 67
76 break; 68 break;
77 } 69
70 }
71
72 case(','):
73 {
74 end = start - 1;
75 break;
78 } 76 }
79 77
80 default: 78 default:
@@ -85,16 +83,12 @@ tokenize_csv(string8 buffer, csv_table *global_table, mem_arena *arena)
85 } 83 }
86 } 84 }
87 85
88 token = (string8){ 86 token->lexeme = String8Cast(start, end - start);
89 .data = start,
90 .size = end - start,
91 };
92 87
93 **tokens = token; 88 *tokens = token;
94 ++*tokens; 89 ++tokens;
95 }
96 90
97 printf("%d", count);
98 91
99 return NULL; 92 return NULL;
93 }
100} 94}
diff --git a/source/lexer/lexer.h b/source/lexer.h
index 86f8427..7bafc0d 100644
--- a/source/lexer/lexer.h
+++ b/source/lexer.h
@@ -1,6 +1,14 @@
1#ifndef ENGINE_LEXER_H 1#ifndef ENGINE_LEXER_H
2#define ENGINE_LEXER_H 2#define ENGINE_LEXER_H
3 3
4typedef enum token_flags token_flags;
5enum token_flags
6{
7 START_FL = 1 << 1,
8 END_FL = 1 << 2,
9};
10
11
4typedef enum token_type token_type; 12typedef enum token_type token_type;
5enum token_type 13enum token_type
6{ 14{
@@ -16,8 +24,9 @@ struct token
16{ 24{
17 string8 lexeme; 25 string8 lexeme;
18 token_type type; 26 token_type type;
19 27 token_flags flags;
20}; 28};
21 29
22 30
31
23#endif /* ENGINE_LEXER_H */ 32#endif /* ENGINE_LEXER_H */
diff --git a/source/parser/parser.c b/source/parser/parser.c
deleted file mode 100644
index 4c57345..0000000
--- a/source/parser/parser.c
+++ /dev/null
@@ -1,16 +0,0 @@
1#ifndef ENGINE_REPL_H
2#define ENGINE_REPL_H
3
4typedef struct node node;
5struct node
6{
7
8};
9
10typedef struct btree btree;
11struct btree
12{
13
14};
15
16#endif /* ENGINE_H */
diff --git a/source/parser/parser.h b/source/parser/parser.h
deleted file mode 100644
index 4c57345..0000000
--- a/source/parser/parser.h
+++ /dev/null
@@ -1,16 +0,0 @@
1#ifndef ENGINE_REPL_H
2#define ENGINE_REPL_H
3
4typedef struct node node;
5struct node
6{
7
8};
9
10typedef struct btree btree;
11struct btree
12{
13
14};
15
16#endif /* ENGINE_H */
diff --git a/source/query/query.c b/source/query.c
index e69de29..e69de29 100644
--- a/source/query/query.c
+++ b/source/query.c
diff --git a/source/query/query.h b/source/query.h
index e69de29..e69de29 100644
--- a/source/query/query.h
+++ b/source/query.h
diff --git a/source/repl/repl.c b/source/repl/repl.c
deleted file mode 100644
index dd289d8..0000000
--- a/source/repl/repl.c
+++ /dev/null
@@ -1,12 +0,0 @@
1internal void
2init_repl()
3{
4 for(;;)
5 {
6 print("reading user input...");
7 // TODO(nasr): design a repl system
8
9 sleep(1);
10 }
11
12}
diff --git a/source/repl/repl.h b/source/repl/repl.h
deleted file mode 100644
index 4c57345..0000000
--- a/source/repl/repl.h
+++ /dev/null
@@ -1,16 +0,0 @@
1#ifndef ENGINE_REPL_H
2#define ENGINE_REPL_H
3
4typedef struct node node;
5struct node
6{
7
8};
9
10typedef struct btree btree;
11struct btree
12{
13
14};
15
16#endif /* ENGINE_H */
diff --git a/source/storage/csv_reader.c b/source/storage/csv_reader.c
deleted file mode 100644
index 2fcbe04..0000000
--- a/source/storage/csv_reader.c
+++ /dev/null
@@ -1,7 +0,0 @@
1internal void
2read_csv(string8 buffer)
3{
4 printf("\nsize:%lu\ndata %s\n", buffer.size, buffer.data);
5
6}
7
diff --git a/source/storage/table.c b/source/storage/table.c
deleted file mode 100644
index e69de29..0000000
--- a/source/storage/table.c
+++ /dev/null
diff --git a/source/storage/table.h b/source/storage/table.h
deleted file mode 100644
index e69de29..0000000
--- a/source/storage/table.h
+++ /dev/null