diff options
| author | nasr <nsrddyn@gmail.com> | 2026-03-02 22:43:16 +0000 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-03-02 22:43:16 +0000 |
| commit | 56ebfa3f4b0d7a80090b344b294252d2be152bb0 (patch) | |
| tree | 9d12900a2870dde05e7ce6581104dc2b3070dbe1 /source | |
| parent | 9053e4a0a20f0c0338aa4c5d9f9c0520184bcad7 (diff) | |
feature(main): base library impelmentation
Diffstat (limited to 'source')
| -rwxr-xr-x | source/base/base.h | 73 | ||||
| -rwxr-xr-x | source/base/base_arena.c | 138 | ||||
| -rwxr-xr-x | source/base/base_arena.h | 24 | ||||
| -rw-r--r-- | source/base/base_hash.c | 12 | ||||
| -rwxr-xr-x | source/base/base_include.h | 30 | ||||
| -rw-r--r-- | source/base/base_io.h | 11 | ||||
| -rw-r--r-- | source/base/base_mem.c | 17 | ||||
| -rw-r--r-- | source/base/base_mem.h | 8 | ||||
| -rw-r--r-- | source/base/base_os.h | 14 | ||||
| -rwxr-xr-x | source/base/base_stack.c | 187 | ||||
| -rwxr-xr-x | source/base/base_stack.h | 22 | ||||
| -rw-r--r-- | source/base/base_string.c | 63 | ||||
| -rw-r--r-- | source/base/base_string.h | 15 | ||||
| -rw-r--r-- | source/base/base_test.c | 0 | ||||
| -rw-r--r-- | source/base/base_test.h | 74 | ||||
| -rw-r--r-- | source/base/bash_hash.h | 15 | ||||
| -rw-r--r-- | source/engine/#engine_repl.c# | 7 | ||||
| -rw-r--r-- | source/engine/engine_entry.c | 16 | ||||
| -rw-r--r-- | source/engine/engine_repl.c | 8 | ||||
| -rw-r--r-- | source/engine/engine_repl.h | 17 |
20 files changed, 751 insertions, 0 deletions
diff --git a/source/base/base.h b/source/base/base.h new file mode 100755 index 0000000..ef23391 --- /dev/null +++ b/source/base/base.h | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | #ifndef BASE_H | ||
| 2 | #define BASE_H | ||
| 3 | |||
| 4 | /* assert an expression and output the file and the line */ | ||
| 5 | |||
| 6 | #define internal static | ||
| 7 | #define global_variable static | ||
| 8 | #define local_persist static | ||
| 9 | |||
| 10 | #define ERR_OK 0 | ||
| 11 | #define ERR_IO 1 | ||
| 12 | #define ERR_PARSE 2 | ||
| 13 | #define ERR_PERM 3 | ||
| 14 | #define ERR_INVALID 4 | ||
| 15 | |||
| 16 | #define KiB(n) (((u64)(n)) << 10) | ||
| 17 | #define MiB(n) (((u64)(n)) << 20) | ||
| 18 | #define GiB(n) (((u64)(n)) << 30) | ||
| 19 | |||
| 20 | #define unused(x) (void)(x) | ||
| 21 | |||
| 22 | #define PATH_MAX_LEN 128 | ||
| 23 | #define BUFF_SMALL 128 | ||
| 24 | #define BUFF_DEFAULT 256 | ||
| 25 | #define BUFF_LARGE 512 | ||
| 26 | |||
| 27 | #define NIL 0 | ||
| 28 | |||
| 29 | #define DEPRECATED __attribute__((__deprecated__)) | ||
| 30 | |||
| 31 | #if defined(__arm__) || defined(__aarch64__) | ||
| 32 | #define breakpoint __asm__ volatile("brk #0"); | ||
| 33 | #define temp_breakpoint __asm__ volatile("udf #0"); | ||
| 34 | #elif defined(__i386__) || defined(__x86_64__) | ||
| 35 | #define breakpoint __asm__ volatile("int3"); | ||
| 36 | #endif | ||
| 37 | |||
| 38 | #define MemCpy(dest, src, len) memcpy((dest), (src), (len)) | ||
| 39 | #define MemSet(dest, len) memset((dest), (0), (len)) | ||
| 40 | |||
| 41 | #if COMPILER_MSVC || (COMPILER_CLANG && OS_WINDOWS) | ||
| 42 | #pragma section(".rdata$", read) | ||
| 43 | #define read_only __declspec(allocate(".rdata$")) | ||
| 44 | #elif (COMPILER_CLANG && OS_LINUX) | ||
| 45 | #define read_only __attribute__((section(".rodata"))) | ||
| 46 | #else | ||
| 47 | #define read_only | ||
| 48 | #endif | ||
| 49 | |||
| 50 | typedef uint64_t u64; | ||
| 51 | typedef uint32_t u32; | ||
| 52 | typedef uint16_t u16; | ||
| 53 | typedef uint8_t u8; | ||
| 54 | |||
| 55 | typedef int8_t i8; | ||
| 56 | typedef int16_t i16; | ||
| 57 | typedef int32_t i32; | ||
| 58 | typedef int64_t i64; | ||
| 59 | |||
| 60 | typedef float f32; | ||
| 61 | typedef double f64; | ||
| 62 | |||
| 63 | typedef i32 b32; | ||
| 64 | typedef i16 b16; | ||
| 65 | typedef u8 b8; | ||
| 66 | |||
| 67 | typedef uintptr_t umm; | ||
| 68 | typedef intptr_t smm; | ||
| 69 | |||
| 70 | #define TRUE (0 == 0) | ||
| 71 | #define FALSE (0 != 0) | ||
| 72 | |||
| 73 | #endif | ||
diff --git a/source/base/base_arena.c b/source/base/base_arena.c new file mode 100755 index 0000000..3e9a6df --- /dev/null +++ b/source/base/base_arena.c | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | internal mem_arena * | ||
| 2 | arena_create(u64 capacity) | ||
| 3 | { | ||
| 4 | mem_arena *arena = (mem_arena *)mmap( | ||
| 5 | /* kernel decides where to throw the arena */ | ||
| 6 | NULL, | ||
| 7 | capacity + sizeof(mem_arena), | ||
| 8 | PROT_READ | PROT_WRITE, | ||
| 9 | MAP_SHARED | MAP_ANONYMOUS, | ||
| 10 | -1, | ||
| 11 | 0); | ||
| 12 | |||
| 13 | if (arena == MAP_FAILED) | ||
| 14 | { | ||
| 15 | return NULL; | ||
| 16 | } | ||
| 17 | |||
| 18 | arena->capacity = capacity; | ||
| 19 | arena->base_position = (u8 *)arena + sizeof(mem_arena); | ||
| 20 | arena->current_position = 0; | ||
| 21 | arena->previous_position = 0; | ||
| 22 | |||
| 23 | return arena; | ||
| 24 | } | ||
| 25 | |||
| 26 | internal void | ||
| 27 | arena_destroy(mem_arena *arena) | ||
| 28 | { | ||
| 29 | if (!arena) | ||
| 30 | { | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | munmap(arena, arena->capacity + sizeof(mem_arena)); | ||
| 34 | } | ||
| 35 | internal void * | ||
| 36 | arena_alloc(mem_arena *arena, u64 size) | ||
| 37 | { | ||
| 38 | if (!arena) | ||
| 39 | { | ||
| 40 | return NULL; | ||
| 41 | } | ||
| 42 | u64 aligned = Align(arena->current_position, ARENA_ALIGN); | ||
| 43 | u64 new_pos = aligned + size; | ||
| 44 | if (new_pos > arena->capacity) | ||
| 45 | { | ||
| 46 | return NULL; | ||
| 47 | } | ||
| 48 | |||
| 49 | void *out = arena->base_position + aligned; | ||
| 50 | |||
| 51 | arena->previous_position = arena->current_position; | ||
| 52 | arena->current_position = aligned + size; | ||
| 53 | |||
| 54 | MemSet(out, size); | ||
| 55 | |||
| 56 | return out; | ||
| 57 | } | ||
| 58 | |||
| 59 | internal void | ||
| 60 | arena_pop(mem_arena *arena, u64 size) | ||
| 61 | { | ||
| 62 | size = MIN(size, arena->current_position); | ||
| 63 | arena->current_position -= size; | ||
| 64 | } | ||
| 65 | |||
| 66 | internal void | ||
| 67 | arena_pop_to(mem_arena *arena, u64 pos) | ||
| 68 | { | ||
| 69 | u64 size = pos < arena->current_position ? arena->current_position - pos : 0; | ||
| 70 | arena_pop(arena, size); | ||
| 71 | } | ||
| 72 | |||
| 73 | internal void | ||
| 74 | arena_clear(mem_arena *arena) | ||
| 75 | { | ||
| 76 | arena->current_position = 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | internal mem_arena * | ||
| 80 | arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size, umm alignment) | ||
| 81 | { | ||
| 82 | u8 *old_mem = (u8 *)old_memory; | ||
| 83 | |||
| 84 | if (!is_pow(alignment)) | ||
| 85 | { | ||
| 86 | Align(arena->current_position, alignment); | ||
| 87 | } | ||
| 88 | |||
| 89 | if (old_memory == NULL || old_size == 0) | ||
| 90 | { | ||
| 91 | return (mem_arena *)arena_alloc(arena, new_size); | ||
| 92 | } | ||
| 93 | else if ((old_mem >= arena->base_position && old_mem < arena->base_position + arena->capacity)) | ||
| 94 | { | ||
| 95 | if ((arena->base_position + arena->previous_position) == old_memory) | ||
| 96 | { | ||
| 97 | arena->current_position = arena->previous_position + new_size; | ||
| 98 | if (new_size > old_size) | ||
| 99 | { | ||
| 100 | MemSet(&arena->current_position, new_size - old_size); | ||
| 101 | } | ||
| 102 | return (mem_arena *)old_memory; | ||
| 103 | } | ||
| 104 | else | ||
| 105 | { | ||
| 106 | void *new_memory = arena_alloc(arena, new_size); | ||
| 107 | umm copy_size = old_size < new_size ? old_size : new_size; | ||
| 108 | memmove(new_memory, old_mem, copy_size); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | else | ||
| 112 | { | ||
| 113 | check(0); | ||
| 114 | } | ||
| 115 | return NULL; | ||
| 116 | } | ||
| 117 | |||
| 118 | internal mem_arena * | ||
| 119 | arena_resize(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size) | ||
| 120 | { | ||
| 121 | return arena_resize_align(arena, old_memory, new_size, old_size, ARENA_ALIGN); | ||
| 122 | } | ||
| 123 | |||
| 124 | internal temp_arena | ||
| 125 | temp_arena_begin(mem_arena *arena) | ||
| 126 | { | ||
| 127 | temp_arena t; | ||
| 128 | t.arena = arena; | ||
| 129 | t.start_position = arena->current_position; | ||
| 130 | |||
| 131 | return t; | ||
| 132 | } | ||
| 133 | |||
| 134 | internal void | ||
| 135 | temp_arena_end(temp_arena temp) | ||
| 136 | { | ||
| 137 | temp.arena->current_position = temp.start_position; | ||
| 138 | } | ||
diff --git a/source/base/base_arena.h b/source/base/base_arena.h new file mode 100755 index 0000000..fe71c24 --- /dev/null +++ b/source/base/base_arena.h | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | #ifndef BASE_ARENA_H | ||
| 2 | #define BASE_ARENA_H | ||
| 3 | |||
| 4 | #define Align(pointer, alignment) align((u64)(pointer), (umm)(alignment)) | ||
| 5 | #define PushStruct(arena, type) (type *)arena_alloc((arena), sizeof(type)) | ||
| 6 | #define PushArray(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len)) | ||
| 7 | |||
| 8 | typedef struct mem_arena mem_arena; | ||
| 9 | struct mem_arena | ||
| 10 | { | ||
| 11 | u64 current_position; | ||
| 12 | u64 previous_position; | ||
| 13 | u64 capacity; | ||
| 14 | u8 *base_position; | ||
| 15 | }; | ||
| 16 | |||
| 17 | typedef struct temp_arena temp_arena; | ||
| 18 | struct temp_arena | ||
| 19 | { | ||
| 20 | mem_arena *arena; | ||
| 21 | u64 start_position; | ||
| 22 | }; | ||
| 23 | |||
| 24 | #endif /* BASE_ARENA_H */ | ||
diff --git a/source/base/base_hash.c b/source/base/base_hash.c new file mode 100644 index 0000000..1964441 --- /dev/null +++ b/source/base/base_hash.c | |||
| @@ -0,0 +1,12 @@ | |||
| 1 | |||
| 2 | internal u64 | ||
| 3 | generate_hash() | ||
| 4 | { | ||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | } | ||
| 9 | |||
| 10 | internal hash_map | ||
| 11 | make_hash_map | ||
| 12 | |||
diff --git a/source/base/base_include.h b/source/base/base_include.h new file mode 100755 index 0000000..0b0f256 --- /dev/null +++ b/source/base/base_include.h | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #ifndef BASE_INCLUDE_H | ||
| 2 | #define BASE_INCLUDE_H | ||
| 3 | |||
| 4 | #include <dirent.h> | ||
| 5 | #include <sys/mman.h> | ||
| 6 | #include <sys/stat.h> | ||
| 7 | #include <fcntl.h> | ||
| 8 | #include <stdint.h> | ||
| 9 | #include <stddef.h> | ||
| 10 | #include <string.h> | ||
| 11 | #include <unistd.h> | ||
| 12 | |||
| 13 | #include "base.h" | ||
| 14 | #include "base_mem.h" | ||
| 15 | #include "base_arena.h" | ||
| 16 | #include "base_stack.h" | ||
| 17 | #include "base_test.h" | ||
| 18 | #include "base_string.h" | ||
| 19 | #include "base_os.h" | ||
| 20 | |||
| 21 | #ifdef BASE_UNITY | ||
| 22 | |||
| 23 | #include "base_mem.c" | ||
| 24 | #include "base_arena.c" | ||
| 25 | #include "base_stack.c" | ||
| 26 | #include "base_test.c" | ||
| 27 | #include "base_string.c" | ||
| 28 | |||
| 29 | #endif | ||
| 30 | #endif | ||
diff --git a/source/base/base_io.h b/source/base/base_io.h new file mode 100644 index 0000000..ece4d7c --- /dev/null +++ b/source/base/base_io.h | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | #ifndef BASE_IO_H | ||
| 2 | #define BASE_IO_H | ||
| 3 | |||
| 4 | internal void | ||
| 5 | input_read() | ||
| 6 | { | ||
| 7 | |||
| 8 | |||
| 9 | } | ||
| 10 | |||
| 11 | #endif /* BASE_IO_H */ | ||
diff --git a/source/base/base_mem.c b/source/base/base_mem.c new file mode 100644 index 0000000..f20ba39 --- /dev/null +++ b/source/base/base_mem.c | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | internal inline b8 | ||
| 2 | is_pow(umm x) | ||
| 3 | { | ||
| 4 | return (x & (x - 1)) == 0; | ||
| 5 | } | ||
| 6 | |||
| 7 | internal inline u64 | ||
| 8 | align(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 new file mode 100644 index 0000000..945f0ce --- /dev/null +++ b/source/base/base_mem.h | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | #ifndef BASE_MEM_H | ||
| 2 | #define BASE_MEM_H | ||
| 3 | |||
| 4 | #define ARENA_ALIGN (2 * sizeof(void *)) | ||
| 5 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ||
| 6 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ||
| 7 | |||
| 8 | #endif | ||
diff --git a/source/base/base_os.h b/source/base/base_os.h new file mode 100644 index 0000000..ce9acae --- /dev/null +++ b/source/base/base_os.h | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | #ifndef BASE_OS_H | ||
| 2 | #define BASE_OS_H | ||
| 3 | |||
| 4 | internal void | ||
| 5 | print(const char *str) | ||
| 6 | { | ||
| 7 | i32 len = 0; | ||
| 8 | while (str[len]) len++; | ||
| 9 | write(STDOUT_FILENO, str, len); | ||
| 10 | } | ||
| 11 | |||
| 12 | #define print(Format) print(Format) | ||
| 13 | |||
| 14 | #endif /* BASE_OS_H */ | ||
diff --git a/source/base/base_stack.c b/source/base/base_stack.c new file mode 100755 index 0000000..9c1218a --- /dev/null +++ b/source/base/base_stack.c | |||
| @@ -0,0 +1,187 @@ | |||
| 1 | internal mem_stack * | ||
| 2 | stack_create(u64 capacity) | ||
| 3 | { | ||
| 4 | mem_stack *stack = (mem_stack *)mmap( | ||
| 5 | 0, | ||
| 6 | capacity + sizeof(mem_stack), | ||
| 7 | PROT_READ | PROT_WRITE, | ||
| 8 | MAP_SHARED | MAP_ANONYMOUS, | ||
| 9 | -1, | ||
| 10 | 0); | ||
| 11 | |||
| 12 | if (stack == MAP_FAILED) | ||
| 13 | { | ||
| 14 | return NULL; | ||
| 15 | } | ||
| 16 | |||
| 17 | stack->capacity = capacity; | ||
| 18 | stack->base_position = (u8 *)stack + sizeof(mem_stack); | ||
| 19 | stack->current_offset = 0; | ||
| 20 | |||
| 21 | return stack; | ||
| 22 | } | ||
| 23 | |||
| 24 | internal u8 | ||
| 25 | calculate_padding(u64 pointer, u8 alignment, u64 header_size) | ||
| 26 | { | ||
| 27 | u8 modulo, padding; | ||
| 28 | |||
| 29 | if (!is_pow(alignment)) | ||
| 30 | { | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | modulo = pointer & (u8)(alignment - 1); | ||
| 35 | |||
| 36 | padding = 0; | ||
| 37 | |||
| 38 | if (0 == modulo) | ||
| 39 | { | ||
| 40 | padding = alignment - modulo; | ||
| 41 | } | ||
| 42 | |||
| 43 | if (padding < header_size) | ||
| 44 | { | ||
| 45 | header_size -= padding; | ||
| 46 | |||
| 47 | if ((header_size & (alignment - 1)) != 0) | ||
| 48 | { | ||
| 49 | padding += alignment * (1 + (header_size / alignment)); | ||
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | padding += alignment * (header_size / alignment); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | return padding; | ||
| 58 | } | ||
| 59 | |||
| 60 | internal mem_stack * | ||
| 61 | stack_push_align(mem_stack *stack, u64 size, u8 alignment) | ||
| 62 | { | ||
| 63 | u8 padding = 0; | ||
| 64 | |||
| 65 | if (!is_pow(alignment)) | ||
| 66 | { | ||
| 67 | return (0); | ||
| 68 | } | ||
| 69 | |||
| 70 | if (alignment > 128) | ||
| 71 | { | ||
| 72 | alignment = 128; | ||
| 73 | } | ||
| 74 | |||
| 75 | u64 current_address = (u64)stack->base_position + stack->current_offset; | ||
| 76 | padding = calculate_padding(current_address, alignment, sizeof(mem_stack_header)); | ||
| 77 | |||
| 78 | if (stack->current_offset + padding + size > stack->capacity) | ||
| 79 | { | ||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | stack->current_offset += padding; | ||
| 84 | |||
| 85 | u64 next_address = current_address + (u64)padding; | ||
| 86 | mem_stack_header *header = (mem_stack_header *)(next_address - sizeof(mem_stack_header)); | ||
| 87 | header->padding = padding; | ||
| 88 | |||
| 89 | stack->current_offset += size; | ||
| 90 | |||
| 91 | return MemSet((void *)next_address, size); | ||
| 92 | } | ||
| 93 | internal void * | ||
| 94 | stack_push(mem_stack *stack, u64 size) | ||
| 95 | { | ||
| 96 | return stack_push_align(stack, size, ARENA_ALIGN); | ||
| 97 | } | ||
| 98 | |||
| 99 | internal void | ||
| 100 | stack_pop(mem_stack *stack, void *pointer) | ||
| 101 | { | ||
| 102 | if (pointer != NULL) | ||
| 103 | { | ||
| 104 | u64 start, end, current_address; | ||
| 105 | mem_stack_header *header; | ||
| 106 | u64 prev_offset; | ||
| 107 | |||
| 108 | start = (u64)stack->base_position; | ||
| 109 | end = start + (u64)stack->capacity; | ||
| 110 | current_address = (u64)pointer; | ||
| 111 | |||
| 112 | if (!(start <= current_address && current_address < end)) | ||
| 113 | { | ||
| 114 | if (0 && "Out of bounds memory address passed to stack allocator (free)") | ||
| 115 | { | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | if (current_address >= start + (u64)stack->base_position) | ||
| 122 | { | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | header = (mem_stack_header *)(current_address - sizeof(mem_stack_header)); | ||
| 127 | prev_offset = (size_t)(current_address - (u64)header->padding - start); | ||
| 128 | stack->current_offset = prev_offset; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | internal mem_stack * | ||
| 133 | stack_resize_align(mem_stack *stack, void *pointer, u64 old_size, u64 new_size, u8 alignment) | ||
| 134 | { | ||
| 135 | if (pointer == NULL) | ||
| 136 | { | ||
| 137 | return stack_push_align(stack, new_size, alignment); | ||
| 138 | } | ||
| 139 | else if (new_size == 0) | ||
| 140 | { | ||
| 141 | stack_pop(stack, pointer); | ||
| 142 | return NULL; | ||
| 143 | } | ||
| 144 | |||
| 145 | u64 start, end, current_address; | ||
| 146 | u64 min_size = old_size < new_size ? old_size : new_size; | ||
| 147 | void *new_pointer; | ||
| 148 | |||
| 149 | start = (u64)stack->base_position; | ||
| 150 | end = start + (u64)stack->capacity; | ||
| 151 | current_address = (u64)pointer; | ||
| 152 | if (!(start <= current_address && current_address < end)) | ||
| 153 | { | ||
| 154 | return NULL; | ||
| 155 | } | ||
| 156 | |||
| 157 | if (current_address >= start + (u64)stack->current_offset) | ||
| 158 | { | ||
| 159 | return NULL; | ||
| 160 | } | ||
| 161 | |||
| 162 | if (old_size == new_size) | ||
| 163 | { | ||
| 164 | return pointer; | ||
| 165 | } | ||
| 166 | |||
| 167 | new_pointer = stack_push_align(stack, new_size, alignment); | ||
| 168 | memmove(new_pointer, pointer, min_size); | ||
| 169 | return new_pointer; | ||
| 170 | } | ||
| 171 | |||
| 172 | internal void | ||
| 173 | stack_pop_all(mem_stack *stack) | ||
| 174 | { | ||
| 175 | stack->current_offset = 0; | ||
| 176 | } | ||
| 177 | |||
| 178 | internal void | ||
| 179 | stack_destroy(mem_stack *stack) | ||
| 180 | { | ||
| 181 | if (!stack) | ||
| 182 | { | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | munmap(stack, stack->capacity + sizeof(mem_stack)); | ||
| 187 | } | ||
diff --git a/source/base/base_stack.h b/source/base/base_stack.h new file mode 100755 index 0000000..54d61d3 --- /dev/null +++ b/source/base/base_stack.h | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | #ifndef STACK_H | ||
| 2 | #define STACK_H | ||
| 3 | |||
| 4 | typedef struct mem_stack_header mem_stack_header; | ||
| 5 | struct mem_stack_header | ||
| 6 | { | ||
| 7 | u8 padding; | ||
| 8 | u8 previous_offset; | ||
| 9 | }; | ||
| 10 | |||
| 11 | |||
| 12 | typedef struct mem_stack mem_stack; | ||
| 13 | struct mem_stack | ||
| 14 | { | ||
| 15 | mem_stack_header *header; | ||
| 16 | |||
| 17 | u64 current_offset; | ||
| 18 | u64 capacity; | ||
| 19 | u8 *base_position; | ||
| 20 | }; | ||
| 21 | |||
| 22 | #endif | ||
diff --git a/source/base/base_string.c b/source/base/base_string.c new file mode 100644 index 0000000..9d09914 --- /dev/null +++ b/source/base/base_string.c | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | internal b8 | ||
| 2 | compare_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 | |||
| 20 | internal b8 | ||
| 21 | compare_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 | |||
| 47 | internal u64 | ||
| 48 | to_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 new file mode 100644 index 0000000..5f875e0 --- /dev/null +++ b/source/base/base_string.h | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | #ifndef BASE_STRING_H | ||
| 2 | #define BASE_STRING_H | ||
| 3 | |||
| 4 | #define _StringCast (string) | ||
| 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 | |||
| 8 | typedef struct string string; | ||
| 9 | struct string | ||
| 10 | { | ||
| 11 | u8 *data; | ||
| 12 | u64 size; | ||
| 13 | }; | ||
| 14 | |||
| 15 | #endif /* BASE_STRING_H */ | ||
diff --git a/source/base/base_test.c b/source/base/base_test.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/source/base/base_test.c | |||
diff --git a/source/base/base_test.h b/source/base/base_test.h new file mode 100644 index 0000000..412797b --- /dev/null +++ b/source/base/base_test.h | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | #ifndef BASE_TEST_H | ||
| 2 | #define BASE_TEST_H | ||
| 3 | |||
| 4 | #define RED "\x1b[31m" | ||
| 5 | #define GREEN "\x1b[32m" | ||
| 6 | #define RESET "\x1b[0m" | ||
| 7 | #define BLUE "\x1b[34m" | ||
| 8 | |||
| 9 | #define LEN(s) (sizeof(s) - 1) | ||
| 10 | |||
| 11 | internal void | ||
| 12 | write_int(i32 num) | ||
| 13 | { | ||
| 14 | |||
| 15 | if (num < 0) | ||
| 16 | { | ||
| 17 | write(STDERR_FILENO, "-", 1); | ||
| 18 | num = -num; | ||
| 19 | } | ||
| 20 | if (num >= 10) | ||
| 21 | write_int(num / 10); | ||
| 22 | char digit = '0' + (num % 10); | ||
| 23 | |||
| 24 | write(STDERR_FILENO, &digit, 1); | ||
| 25 | } | ||
| 26 | |||
| 27 | #define show \ | ||
| 28 | do \ | ||
| 29 | { \ | ||
| 30 | write(STDOUT_FILENO, __FILE__, sizeof(__FILE__) - 1); \ | ||
| 31 | write(STDOUT_FILENO, ":", 1); \ | ||
| 32 | write(STDOUT_FILENO, __func__, sizeof(__func__) - 1); \ | ||
| 33 | write(STDOUT_FILENO, ":", 1); \ | ||
| 34 | write_int(__LINE__); \ | ||
| 35 | write(STDOUT_FILENO, "\n", 1); \ | ||
| 36 | } while (0) | ||
| 37 | |||
| 38 | #define test(expr) \ | ||
| 39 | { \ | ||
| 40 | if ((expr) != 0) \ | ||
| 41 | { \ | ||
| 42 | write(STDERR_FILENO, "[FAILED] ", LEN("[FAILED] ")); \ | ||
| 43 | show; \ | ||
| 44 | _exit(1); \ | ||
| 45 | } \ | ||
| 46 | } | ||
| 47 | |||
| 48 | #define check(expr) \ | ||
| 49 | { \ | ||
| 50 | if ((expr) != 0) \ | ||
| 51 | { \ | ||
| 52 | write(STDERR_FILENO, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ | ||
| 53 | show; \ | ||
| 54 | write(STDERR_FILENO, RESET, LEN(RESET)); \ | ||
| 55 | _exit(1); \ | ||
| 56 | } \ | ||
| 57 | else \ | ||
| 58 | { \ | ||
| 59 | write(STDERR_FILENO, GREEN "[SUCCESS] ", LEN(GREEN "[SUCCESS] ")); \ | ||
| 60 | show; \ | ||
| 61 | write(STDERR_FILENO, RESET, LEN(RESET)); \ | ||
| 62 | } \ | ||
| 63 | } | ||
| 64 | |||
| 65 | #define checkpoint_output "<<CHECKPOINT>>\n" | ||
| 66 | #define checkpoint_end_output "^^^^^^^^^^^^^^\n\n\n" | ||
| 67 | #define checkpoint \ | ||
| 68 | { \ | ||
| 69 | write(STDERR_FILENO, BLUE checkpoint_output, LEN(BLUE checkpoint_output)); \ | ||
| 70 | show; \ | ||
| 71 | write(STDERR_FILENO, BLUE checkpoint_end_output, LEN(BLUE checkpoint_end_output)); \ | ||
| 72 | } | ||
| 73 | |||
| 74 | #endif /* BASE_TEST_H */ | ||
diff --git a/source/base/bash_hash.h b/source/base/bash_hash.h new file mode 100644 index 0000000..2c286a2 --- /dev/null +++ b/source/base/bash_hash.h | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | ##ifndef HEADER_H | ||
| 2 | #define HEADER_H | ||
| 3 | |||
| 4 | typedef struct hash_map hash_map; | ||
| 5 | typedef struct hash hash; | ||
| 6 | |||
| 7 | |||
| 8 | struct hash_map | ||
| 9 | { | ||
| 10 | |||
| 11 | |||
| 12 | }; | ||
| 13 | |||
| 14 | |||
| 15 | #endif /* HEADER_H */ | ||
diff --git a/source/engine/#engine_repl.c# b/source/engine/#engine_repl.c# new file mode 100644 index 0000000..be43645 --- /dev/null +++ b/source/engine/#engine_repl.c# | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | |||
| 2 | internal void | ||
| 3 | tokenize(str8 ) | ||
| 4 | { | ||
| 5 | |||
| 6 | |||
| 7 | } | ||
diff --git a/source/engine/engine_entry.c b/source/engine/engine_entry.c new file mode 100644 index 0000000..8973dee --- /dev/null +++ b/source/engine/engine_entry.c | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | #define BASE_UNITY | ||
| 2 | #include "../base/base_include.h" | ||
| 3 | |||
| 4 | #include "engine_repl.h" | ||
| 5 | #include "engine_repl.c" | ||
| 6 | |||
| 7 | int 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 new file mode 100644 index 0000000..ead6c7b --- /dev/null +++ b/source/engine/engine_repl.c | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | |||
| 2 | internal tokens * | ||
| 3 | tokenize(string buffer) | ||
| 4 | { | ||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | } | ||
diff --git a/source/engine/engine_repl.h b/source/engine/engine_repl.h new file mode 100644 index 0000000..eb20524 --- /dev/null +++ b/source/engine/engine_repl.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #ifndef ENGINE_REPL_H | ||
| 2 | #define ENGINE_REPL_H | ||
| 3 | |||
| 4 | typedef struct node node; | ||
| 5 | struct node | ||
| 6 | { | ||
| 7 | |||
| 8 | }; | ||
| 9 | |||
| 10 | typedef struct btree btree; | ||
| 11 | struct btree | ||
| 12 | { | ||
| 13 | |||
| 14 | }; | ||
| 15 | |||
| 16 | |||
| 17 | #endif /* ENGINE_H */ | ||
