summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authornasr <nsrddyn@gmail.com>2026-03-02 22:44:17 +0000
committernasr <nsrddyn@gmail.com>2026-03-02 22:44:17 +0000
commit4e77bc7164070d7ffafdee1ba6ce3bb1aaf10746 (patch)
treee878f009730e934dfe5e295d4b24dfd906f0eb73 /source
parent56ebfa3f4b0d7a80090b344b294252d2be152bb0 (diff)
feature(main): loading file + bug fixes
structure improvement
Diffstat (limited to 'source')
-rwxr-xr-xsource/base/base_include.h3
-rw-r--r--source/base/base_mem.c17
-rw-r--r--source/base/base_mem.h18
-rw-r--r--source/base/base_os.h41
-rw-r--r--source/base/base_string.c63
-rw-r--r--source/base/base_string.h45
-rw-r--r--source/engine/#engine_repl.c#7
-rw-r--r--source/engine/engine.c34
-rw-r--r--source/engine/engine_entry.c16
-rw-r--r--source/engine/engine_repl.c8
-rw-r--r--source/lexer/lexer.c8
-rw-r--r--source/lexer/lexer.h23
-rw-r--r--source/parser/parser.c (renamed from source/engine/engine_repl.h)1
-rw-r--r--source/parser/parser.h16
-rw-r--r--source/repl/repl.c16
-rw-r--r--source/repl/repl.h16
-rw-r--r--source/storage/csv_reader.c (renamed from source/base/base_test.c)0
-rw-r--r--source/storage/csv_reader.h0
18 files changed, 209 insertions, 123 deletions
diff --git a/source/base/base_include.h b/source/base/base_include.h
index 0b0f256..40ae5ea 100755
--- a/source/base/base_include.h
+++ b/source/base/base_include.h
@@ -20,11 +20,8 @@
20 20
21#ifdef BASE_UNITY 21#ifdef BASE_UNITY
22 22
23#include "base_mem.c"
24#include "base_arena.c" 23#include "base_arena.c"
25#include "base_stack.c" 24#include "base_stack.c"
26#include "base_test.c"
27#include "base_string.c"
28 25
29#endif 26#endif
30#endif 27#endif
diff --git a/source/base/base_mem.c b/source/base/base_mem.c
index f20ba39..e69de29 100644
--- a/source/base/base_mem.c
+++ b/source/base/base_mem.c
@@ -1,17 +0,0 @@
1internal inline b8
2is_pow(umm x)
3{
4 return (x & (x - 1)) == 0;
5}
6
7internal inline u64
8align(u64 pointer, umm alignment)
9{
10 if ((alignment & (alignment - 1)) == 0)
11 {
12 return pointer;
13 }
14
15 return (pointer + alignment - 1) & ~(alignment - 1);
16}
17
diff --git a/source/base/base_mem.h b/source/base/base_mem.h
index 945f0ce..2778fce 100644
--- a/source/base/base_mem.h
+++ b/source/base/base_mem.h
@@ -5,4 +5,22 @@
5#define MIN(a, b) (((a) < (b)) ? (a) : (b)) 5#define MIN(a, b) (((a) < (b)) ? (a) : (b))
6#define MAX(a, b) (((a) > (b)) ? (a) : (b)) 6#define MAX(a, b) (((a) > (b)) ? (a) : (b))
7 7
8internal inline b8
9is_pow(umm x)
10{
11 return (x & (x - 1)) == 0;
12}
13
14internal inline u64
15align(u64 pointer, umm alignment)
16{
17 if ((alignment & (alignment - 1)) == 0)
18 {
19 return pointer;
20 }
21
22 return (pointer + alignment - 1) & ~(alignment - 1);
23}
24
25
8#endif 26#endif
diff --git a/source/base/base_os.h b/source/base/base_os.h
index ce9acae..ec0a3de 100644
--- a/source/base/base_os.h
+++ b/source/base/base_os.h
@@ -9,6 +9,45 @@ print(const char *str)
9 write(STDOUT_FILENO, str, len); 9 write(STDOUT_FILENO, str, len);
10} 10}
11 11
12#define print(Format) print(Format) 12internal string8
13load_file(const char *path)
14{
15 string8 result = {0};
16 struct stat sbuf = {0};
17 int err = 0;
18 i32 file = open(path, O_RDONLY);
19
20 if(file)
21 {
22
23 if(file != -1)
24 {
25 err = fstat(file, &sbuf);
26 check(err != -1);
27
28 result.size = (u64)sbuf.st_size;
29
30 if(result.size != 0)
31 {
32 result.data = (u8 *)mmap(0,
33 result.size,
34 PROT_READ,
35 MAP_PRIVATE,
36 file,
37 0);
38
39 check(result.data != MAP_FAILED);
40 }
41
42 close(file);
43 }
44 else
45 {
46 // TODO(nasr): logging
47 }
48
49 }
50 return result;
51}
13 52
14#endif /* BASE_OS_H */ 53#endif /* BASE_OS_H */
diff --git a/source/base/base_string.c b/source/base/base_string.c
deleted file mode 100644
index 9d09914..0000000
--- a/source/base/base_string.c
+++ /dev/null
@@ -1,63 +0,0 @@
1internal b8
2compare_string(string c1, string c2)
3{
4 if (c1.size != c2.size)
5 {
6 return 0;
7 }
8
9 for (u64 index = 0; index < c1.size; ++index)
10 {
11 if (c1.data[index] != c2.data[index])
12 {
13 return 0;
14 }
15 }
16
17 return 1;
18}
19
20internal b8
21compare_u8(u8 *c1, u8 *c2, u64 len)
22{
23 if (sizeof(c1) != len || sizeof(c2) != len)
24 {
25 return 0;
26 }
27
28 if (sizeof(*c1) != sizeof(*c2))
29 {
30 return 0;
31 }
32
33 for (u64 word_idx = 0; word_idx <= sizeof(*c1); ++word_idx)
34 {
35 if (*c1 != *c2)
36 {
37 return 0;
38 }
39
40 ++c1;
41 ++c2;
42 }
43
44 return 1;
45}
46
47internal u64
48to_u64(u8 *buf, umm len)
49{
50 u64 value = 0;
51
52 for (umm buffer_idx = 0; buffer_idx < len; ++buffer_idx)
53 {
54 u8 c = buf[buffer_idx];
55 if (c < '0' || c > '9')
56 {
57 break;
58 }
59 value = value * 10 + (c - '0');
60 }
61
62 return value;
63}
diff --git a/source/base/base_string.h b/source/base/base_string.h
index 5f875e0..941dc53 100644
--- a/source/base/base_string.h
+++ b/source/base/base_string.h
@@ -1,15 +1,46 @@
1#ifndef BASE_STRING_H 1#ifndef BASE_STRING_H
2#define BASE_STRING_H 2#define BASE_STRING_H
3 3
4#define _StringCast (string) 4#include <string.h>
5#define StringCast { .data (u8 *)(string), .size = (sizeof((string)) - 1)}
6#define PushString (Arena, Size) StringCast{.data = PushArray((arena), PushStruct(u8), (Size)), .size = (u64)(Size)}
7 5
8typedef struct string string; 6#define StringLit(string) \
9struct string 7 (string8){ .data = (u8 *)(string), .size = (sizeof(string) - 1) }
8
9 #define PushString(arena, size) \
10 (string8){ (u8 *)PushArray((arena), u8, (size)), (u64)(size) }
11
12typedef struct string8 string8;
13struct string8
10{ 14{
11 u8 *data; 15 u8 *data;
12 u64 size; 16 u64 size;
13}; 17};
14 18
19internal b8
20string8_cmp(string8 a, string8 b)
21{
22 if (a.size != b.size) return 0;
23 return (b8)(memcmp(a.data, b.data, a.size) == 0);
24}
25
26internal u64
27string8_to_u64(u8 *buf, umm len)
28{
29 u64 value = 0;
30 for (umm i = 0; i < len; ++i)
31 {
32 u8 c = buf[i];
33 if (c < '0' || c > '9') break;
34 value = value * 10 + (c - '0');
35 }
36 return value;
37}
38
39internal void
40string8_append_char(string8 *buf, u8 c)
41{
42 buf->data[buf->size] = c;
43 buf->size += 1;
44}
45
15#endif /* BASE_STRING_H */ 46#endif /* BASE_STRING_H */
diff --git a/source/engine/#engine_repl.c# b/source/engine/#engine_repl.c#
deleted file mode 100644
index be43645..0000000
--- a/source/engine/#engine_repl.c#
+++ /dev/null
@@ -1,7 +0,0 @@
1
2internal void
3tokenize(str8 )
4{
5
6
7}
diff --git a/source/engine/engine.c b/source/engine/engine.c
new file mode 100644
index 0000000..609101e
--- /dev/null
+++ b/source/engine/engine.c
@@ -0,0 +1,34 @@
1#define BASE_UNITY
2#include "../base/base_include.h"
3
4#include "../lexer/lexer.h"
5#include "../lexer/lexer.c"
6
7#include "../parser/parser.h"
8#include "../parser/parser.c"
9
10#include "../repl/repl.h"
11#include "../repl/repl.c"
12
13int main(int c, char **v)
14{
15 mem_arena *global_arena = arena_create(MiB(1));
16
17 unused(c);
18 unused(v);
19
20 string8 buffer = PushString(global_arena, 5);
21 unused(buffer);
22
23
24 for (;;)
25 {
26 print("reading user input...");
27 // TODO(nasr): design a repl system
28
29 sleep(1);
30 }
31
32}
33
34
diff --git a/source/engine/engine_entry.c b/source/engine/engine_entry.c
deleted file mode 100644
index 8973dee..0000000
--- a/source/engine/engine_entry.c
+++ /dev/null
@@ -1,16 +0,0 @@
1#define BASE_UNITY
2#include "../base/base_include.h"
3
4#include "engine_repl.h"
5#include "engine_repl.c"
6
7int main(int c, char **v)
8{
9 unused(c);
10 unused(v);
11
12 for (;;)
13 {
14
15 }
16}
diff --git a/source/engine/engine_repl.c b/source/engine/engine_repl.c
deleted file mode 100644
index ead6c7b..0000000
--- a/source/engine/engine_repl.c
+++ /dev/null
@@ -1,8 +0,0 @@
1
2internal tokens *
3tokenize(string buffer)
4{
5
6
7
8}
diff --git a/source/lexer/lexer.c b/source/lexer/lexer.c
new file mode 100644
index 0000000..60a5cda
--- /dev/null
+++ b/source/lexer/lexer.c
@@ -0,0 +1,8 @@
1internal token *
2tokenize(string8 buffer)
3{
4
5 // TODO(nasr): tokenize the user input
6
7 return NULL;
8}
diff --git a/source/lexer/lexer.h b/source/lexer/lexer.h
new file mode 100644
index 0000000..86f8427
--- /dev/null
+++ b/source/lexer/lexer.h
@@ -0,0 +1,23 @@
1#ifndef ENGINE_LEXER_H
2#define ENGINE_LEXER_H
3
4typedef enum token_type token_type;
5enum token_type
6{
7 // first 255 tokens for ascii characters
8 TOKEN_UNDEFINED = 255,
9 TOKEN_IDENTIFIER,
10 TOKEN_VALUE,
11
12};
13
14typedef struct token token;
15struct token
16{
17 string8 lexeme;
18 token_type type;
19
20};
21
22
23#endif /* ENGINE_LEXER_H */
diff --git a/source/engine/engine_repl.h b/source/parser/parser.c
index eb20524..4c57345 100644
--- a/source/engine/engine_repl.h
+++ b/source/parser/parser.c
@@ -13,5 +13,4 @@ struct btree
13 13
14}; 14};
15 15
16
17#endif /* ENGINE_H */ 16#endif /* ENGINE_H */
diff --git a/source/parser/parser.h b/source/parser/parser.h
new file mode 100644
index 0000000..4c57345
--- /dev/null
+++ b/source/parser/parser.h
@@ -0,0 +1,16 @@
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/repl/repl.c b/source/repl/repl.c
new file mode 100644
index 0000000..4c57345
--- /dev/null
+++ b/source/repl/repl.c
@@ -0,0 +1,16 @@
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/repl/repl.h b/source/repl/repl.h
new file mode 100644
index 0000000..4c57345
--- /dev/null
+++ b/source/repl/repl.h
@@ -0,0 +1,16 @@
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/base/base_test.c b/source/storage/csv_reader.c
index e69de29..e69de29 100644
--- a/source/base/base_test.c
+++ b/source/storage/csv_reader.c
diff --git a/source/storage/csv_reader.h b/source/storage/csv_reader.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/source/storage/csv_reader.h