summaryrefslogtreecommitdiff
path: root/source/engine.c
blob: 1cfbab01f921b0e93749227c47abd3fa65edb17c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#define B_TREE_IMPLEMENTATION
#define BASE_UNITY
#include "base/base_include.h"

internal b32
is_alpha(u8 point)
{
    return ((point >= 'a' && point <= 'z') || (point >= 'A' && point <= 'Z') || (point == '_'));
}

internal b32
is_digit(u8 point)
{
    return (point >= '0' && point <= '9');
}

internal b32
is_alpha_num(u8 point)
{
    return (is_alpha(point) || is_digit(point));
}

internal b32
is_whitespace(u8 point)
{
    return (point == '\n' || point == '\r' || point == ' ' || point == '\t');
}

internal b32
is_delimiter(u8 point)
{
    return (point == ',');

}


#include "b_tree.h"
#include "csv_reader.h"

typedef struct query_token query_token;
struct query_token
{
    string8      *lexeme;
    query_token  *next;
};


// takes on line of the repl input
internal query_token *
query_tokenizer(mem_arena *arena, string8 *buffer)
{
    query_token *tok = PushStruct(arena, query_token);
    unused(tok);

    for (u64 index = 0; index < buffer->size; ++index)
    {            
        u8 codepoint = buffer->data[index];

        if(codepoint == '\n' || codepoint == '\r') break;

        s32 start   = 0; 
        s32 end     = 0; 

        if(is_whitespace(codepoint))
        {
            end = index; 
        }

        // save the token
        // TODO(nasr): work on the string macros cuz no work
        {

            s32 new_token_size = end - start;

            tok->lexeme->data = &buffer->data[index]; 
            tok->lexeme->size = new_token_size;

            tok->next = tok;
            start = index + 1;
        }
    }

    return tok;
}

int main(int c, char **v)
{

    if(c < 2)
    { 
        print("bad file, setting default file\n");
    }
    else v[1] =  "./test/customers-10000.csv";

    local_persist b32 running = 1;

    mem_arena *global_arena = arena_create(MiB(30));
    csv_table *global_table = PushStruct(global_arena, csv_table);

    string8 buffer = load_file(v[1]);

    print("\nDatabase Engine\n");

    for(;;)
    {
        if (running)
        {
            {
                u8 lbuf[256] = {};
                s32 err = os_read(STDIN_FD, lbuf, 256);
                if(err < 0)
                {
                    print("error reading from stdin");
                }

                query_tokenizer(global_arena, &StringLit(lbuf));

            }

            {
                read_csv(buffer);
                token *tokens = tokenize_csv(buffer, global_arena);
                global_table = parse_csv(tokens, global_table);
            }

            sleep(1);
        }
    }

    return 0;
}