Clang format

This commit is contained in:
Krzosa Karol
2023-03-28 21:16:10 +02:00
parent f53d5dd1f2
commit 5495f96b3b
25 changed files with 9886 additions and 9989 deletions

188
base.cpp
View File

@@ -1,53 +1,53 @@
#define CORE_BASE
#if defined(__clang__)
# define COMPILER_CLANG 1
# if defined(_WIN32)
# define OS_WINDOWS 1
# elif defined(__linux__)
# define OS_LINUX 1
# else
# error Couldnt figure out the platform automatically
# endif
#define COMPILER_CLANG 1
#if defined(_WIN32)
#define OS_WINDOWS 1
#elif defined(__linux__)
#define OS_LINUX 1
#else
#error Couldnt figure out the platform automatically
#endif
#elif defined(_MSC_VER)
# define COMPILER_MSVC 1
# define OS_WINDOWS 1
#define COMPILER_MSVC 1
#define OS_WINDOWS 1
#elif defined(__GNUC__)
# define COMPILER_GCC 1
# if defined(__linux__)
# define OS_LINUX 1
# endif
#define COMPILER_GCC 1
#if defined(__linux__)
#define OS_LINUX 1
#endif
#else
# error Couldnt figure out the compiler
#error Couldnt figure out the compiler
#endif
#if defined(OS_MAC)
#define OS_UNIX 1
#define OS_UNIX 1
#endif
#if defined(OS_LINUX)
#define OS_UNIX 1
#define OS_UNIX 1
#endif
#if !defined(COMPILER_MSVC)
# define COMPILER_MSVC 0
#define COMPILER_MSVC 0
#endif
#if !defined(COMPILER_GCC)
# define COMPILER_GCC 0
#define COMPILER_GCC 0
#endif
#if !defined(COMPILER_CLANG)
# define COMPILER_CLANG 0
#define COMPILER_CLANG 0
#endif
#if !defined(OS_WINDOWS)
# define OS_WINDOWS 0
#define OS_WINDOWS 0
#endif
#if !defined(OS_LINUX)
# define OS_LINUX 0
#define OS_LINUX 0
#endif
#if !defined(OS_MAC)
# define OS_MAC 0
#define OS_MAC 0
#endif
#if !defined(OS_UNIX)
# define OS_UNIX 0
#define OS_UNIX 0
#endif
#if OS_WINDOWS
@@ -120,34 +120,46 @@ typedef double F64;
#define api
#define CORE_Static static
#define global static
#define assert(x) do{if(!(x))Breakpoint;}while(0)
#define assert_message(x,...) assert(x)
#define assert(x) \
do { \
if (!(x)) Breakpoint; \
} while (0)
#define assert_message(x, ...) assert(x)
#define invalid_codepath assert_message(0, "Invalid codepath")
#define invalid_return do{assert_message(0, "Invalid codepath"); return {};}while(0)
#define invalid_default_case default: invalid_codepath
#define invalid_return \
do { \
assert_message(0, "Invalid codepath"); \
return {}; \
} while (0)
#define invalid_default_case \
default: \
invalid_codepath
#define not_implemented assert_message(0, "Not implemented")
#define unused(x) ((void)x)
#define buff_cap(x) (sizeof(x)/sizeof((x)[0]))
#define is_flag_set(val,flag) ((val) & (flag))
#define set_flag(val,flag) ((val) |= (flag))
#define unset_flag(val,flag) ((val) &= (~(flag)))
#define buff_cap(x) (sizeof(x) / sizeof((x)[0]))
#define is_flag_set(val, flag) ((val) & (flag))
#define set_flag(val, flag) ((val) |= (flag))
#define unset_flag(val, flag) ((val) &= (~(flag)))
#define bit_flag(x) (1ull << (x))
#define kib(x) ((x)*1024llu)
#define mib(x) (kib(x)*1024llu)
#define gib(x) (mib(x)*1024llu)
#define JOIN1(X,Y) X##Y
#define JOIN(X,Y) JOIN1(X,Y)
#define mib(x) (kib(x) * 1024llu)
#define gib(x) (mib(x) * 1024llu)
#define JOIN1(X, Y) X##Y
#define JOIN(X, Y) JOIN1(X, Y)
#define string_expand(x) (int)x.len, x.str
struct String{
struct String {
U8 *str;
S64 len;
};
global String string_null = {(U8 *)"null", 4};
union Intern_String{ // Basically just String
union Intern_String { // Basically just String
String s;
struct{ U8 *str; S64 len; };
struct {
U8 *str;
S64 len;
};
};
struct Allocator {
@@ -161,8 +173,8 @@ struct Allocator {
CORE_Static void memory_zero(void *p, size_t size);
CORE_Static void deallocate_stub(Allocator *, void *) {}
#define allocate_array(a, T, size,...) (T *)allocate_size(a, sizeof(T)*(size),##__VA_ARGS__)
#define allocate_struct(a, T, ...) allocate_array(a, T, 1,##__VA_ARGS__)
#define allocate_array(a, T, size, ...) (T *)allocate_size(a, sizeof(T) * (size), ##__VA_ARGS__)
#define allocate_struct(a, T, ...) allocate_array(a, T, 1, ##__VA_ARGS__)
CORE_Static void *allocate_size(Allocator *allocator, size_t size, bool zero_memory = true) {
void *result = allocator->allocate(allocator, size);
if (zero_memory) {
@@ -176,84 +188,83 @@ CORE_Static void deallocate(Allocator *allocator, void *p) {
allocator->deallocate(allocator, p);
}
//-----------------------------------------------------------------------------
// Utilities
//-----------------------------------------------------------------------------
CORE_Static size_t
get_align_offset(size_t size, size_t align){
get_align_offset(size_t size, size_t align) {
size_t mask = align - 1;
size_t val = size & mask;
if(val){
if (val) {
val = align - val;
}
return val;
}
CORE_Static size_t
align_up(size_t size, size_t align){
align_up(size_t size, size_t align) {
size_t result = size + get_align_offset(size, align);
return result;
}
CORE_Static size_t
align_down(size_t size, size_t align){
align_down(size_t size, size_t align) {
size += 1; // Make sure 8 when align is 8 doesn't get rounded down to 0
size_t result = size - (align - get_align_offset(size, align));
return result;
}
CORE_Static void
memory_copy(void *dst, const void *src, size_t size){
U8 *d = (U8*)dst;
U8 *s = (U8*)src;
for(size_t i = 0; i < size; i++){
memory_copy(void *dst, const void *src, size_t size) {
U8 *d = (U8 *)dst;
U8 *s = (U8 *)src;
for (size_t i = 0; i < size; i++) {
d[i] = s[i];
}
}
CORE_Static void
memory_zero(void *p, size_t size){
memory_zero(void *p, size_t size) {
U8 *pp = (U8 *)p;
for(size_t i = 0; i < size; i++)
for (size_t i = 0; i < size; i++)
pp[i] = 0;
}
template<class T>
void swap(T &a, T &b){
template <class T>
void swap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
template<class T>
T max(T a, T b){
if(a > b) return a;
template <class T>
T max(T a, T b) {
if (a > b) return a;
return b;
}
template<class T>
T min(T a, T b){
if(a > b) return b;
template <class T>
T min(T a, T b) {
if (a > b) return b;
return a;
}
template<class T>
T clamp_top(T val, T max){
if(val > max) val = max;
template <class T>
T clamp_top(T val, T max) {
if (val > max) val = max;
return val;
}
template<class T>
T clamp_bot(T bot, T val){
if(val < bot) val = bot;
template <class T>
T clamp_bot(T bot, T val) {
if (val < bot) val = bot;
return val;
}
template<class T>
T clamp(T min, T val, T max){
if(val > max) val = max;
if(val < min) val = min;
template <class T>
T clamp(T min, T val, T max) {
if (val > max) val = max;
if (val < min) val = min;
return val;
}
@@ -302,22 +313,22 @@ is_pow2(U64 x) {
CORE_Static U64
wrap_around_pow2(U64 x, U64 power_of_2) {
assert(is_pow2(power_of_2));
U64 r = (((x)&((power_of_2)-1llu)));
U64 r = (((x) & ((power_of_2)-1llu)));
return r;
}
force_inline String
operator""_s(const char *str, size_t size){
operator""_s(const char *str, size_t size) {
return String{(U8 *)str, (S64)size};
}
force_inline B32
operator==(Intern_String a, Intern_String b){
operator==(Intern_String a, Intern_String b) {
return a.str == b.str;
}
force_inline B32
operator!=(Intern_String a, Intern_String b){
operator!=(Intern_String a, Intern_String b) {
B32 result = a.str == b.str;
return !result;
}
@@ -330,7 +341,8 @@ operator!=(Intern_String a, Intern_String b){
do { \
if ((f) == 0) { \
(f) = (l) = (n); \
} else { \
} \
else { \
(l) = (l)->next = (n); \
} \
} while (0)
@@ -340,7 +352,8 @@ operator!=(Intern_String a, Intern_String b){
do { \
if ((f) == (l)) { \
(f) = (l) = 0; \
} else { \
} \
else { \
(f) = (f)->next; \
} \
} while (0)
@@ -368,7 +381,8 @@ operator!=(Intern_String a, Intern_String b){
(f) = (l) = (node); \
(node)->prev = 0; \
(node)->next = 0; \
} else { \
} \
else { \
(l)->next = (node); \
(node)->prev = (l); \
(node)->next = 0; \
@@ -382,13 +396,16 @@ operator!=(Intern_String a, Intern_String b){
if ((first) == (last)) { \
assert_message((node) == (first), "Macro assert failed"); \
(first) = (last) = 0; \
} else if ((last) == (node)) { \
} \
else if ((last) == (node)) { \
(last) = (last)->prev; \
(last)->next = 0; \
} else if ((first) == (node)) { \
} \
else if ((first) == (node)) { \
(first) = (first)->next; \
(first)->prev = 0; \
} else { \
} \
else { \
(node)->prev->next = (node)->next; \
(node)->next->prev = (node)->prev; \
} \
@@ -410,7 +427,8 @@ operator!=(Intern_String a, Intern_String b){
(first) = (first)->next; \
if ((first)) \
(first)->prev = 0; \
} else { \
} \
else { \
(node)->prev->next = (node)->next; \
if ((node)->next) \
(node)->next->prev = (node)->prev; \
@@ -418,7 +436,7 @@ operator!=(Intern_String a, Intern_String b){
} while (0)
#define DLL_STACK_REMOVE(first, node) DLL_STACK_REMOVE_MOD(first, node, next, prev)
#define For_Linked_List_Named(a,it) for(auto *it = (a); it; it=it->next) // @todo: reference?
#define For_Linked_List(a) For_Linked_List_Named(a,it)
#define For_Named(a,it) for(auto &it : (a))
#define For(a) For_Named((a),it)
#define For_Linked_List_Named(a, it) for (auto *it = (a); it; it = it->next) // @todo: reference?
#define For_Linked_List(a) For_Linked_List_Named(a, it)
#define For_Named(a, it) for (auto &it : (a))
#define For(a) For_Named((a), it)

View File

@@ -14,7 +14,7 @@ static CRT_Heap make_crt_heap() {
//-----------------------------------------------------------------------------
// Memory OS
//-----------------------------------------------------------------------------
struct OS_Memory{
struct OS_Memory {
size_t commit, reserve;
U8 *data;
};
@@ -37,47 +37,47 @@ struct Arena : Allocator {
};
CORE_Static void
arena_pop_pos(Arena *arena, size_t pos){
arena_pop_pos(Arena *arena, size_t pos) {
pos = clamp_top(pos, arena->len);
arena->len = pos;
}
CORE_Static void *
arena_pop(Arena *arena, size_t size){
arena_pop(Arena *arena, size_t size) {
size = clamp_top(size, arena->len);
arena->len -= size;
return arena->memory.data + arena->len;
}
CORE_Static void
arena_release(Arena *arena){
arena_release(Arena *arena) {
os_release(&arena->memory);
}
CORE_Static void
arena_clear(Arena *arena){
arena_clear(Arena *arena) {
arena_pop_pos(arena, 0);
}
CORE_Static void *
arena_push_size(Arena *a, size_t size){
arena_push_size(Arena *a, size_t size) {
size_t generous_size = size + a->alignment;
if(a->len+generous_size>a->memory.commit){
if (a->len + generous_size > a->memory.commit) {
assert(a->memory.reserve > 0);
B32 result = os_commit(&a->memory, generous_size+additional_commit_size);
B32 result = os_commit(&a->memory, generous_size + additional_commit_size);
assert(result);
}
a->len = align_up(a->len, a->alignment);
assert(a->memory.reserve > a->len + size);
void *result = (U8*)a->memory.data + a->len;
void *result = (U8 *)a->memory.data + a->len;
a->len += size;
return result;
}
CORE_Static Arena
push_arena(Allocator *allocator, size_t size, String debug_name) {
push_arena(Allocator *allocator, size_t size, String debug_name) {
Arena result = {};
result.memory.data = (U8 *)allocate_size(allocator, size);
result.memory.reserve = size;
@@ -89,7 +89,7 @@ CORE_Static Arena
}
CORE_Static void
arena_init(Arena *a, String debug_name){
arena_init(Arena *a, String debug_name) {
a->memory = os_reserve(default_reserve_size);
a->alignment = default_alignment;
a->debug_string = debug_name;
@@ -98,14 +98,14 @@ CORE_Static void
}
CORE_Static Arena
arena_sub(Arena *base, size_t size, String debug_name) {
arena_sub(Arena *base, size_t size, String debug_name) {
Arena result = {};
result.memory.data = (U8 *)arena_push_size(base, size);
result.memory.commit = size;
result.memory.reserve = size;
result.alignment = default_alignment;
result.allocate = (Allocator::Allocate *)arena_push_size;
result.deallocate= (Allocator::Deallocate *)deallocate_stub;
result.deallocate = (Allocator::Deallocate *)deallocate_stub;
return result;
}
@@ -117,13 +117,16 @@ arena_from_buffer(void *buffer, size_t size) {
result.memory.reserve = size;
result.alignment = default_alignment;
result.allocate = (Allocator::Allocate *)arena_push_size;
result.deallocate= (Allocator::Deallocate *)deallocate_stub;
result.deallocate = (Allocator::Deallocate *)deallocate_stub;
return result;
}
struct Scratch_Scope {
Arena *arena;
int pos;
Scratch_Scope(Arena *arena) { this->arena = arena; this->pos = arena->len; }
Scratch_Scope(Arena *arena) {
this->arena = arena;
this->pos = arena->len;
}
~Scratch_Scope() { this->arena->len = this->pos; }
};

View File

@@ -2,112 +2,114 @@
//-----------------------------------------------------------------------------
// Array
//-----------------------------------------------------------------------------
template<class T>
struct Array{
template <class T>
struct Array {
Allocator *allocator;
T *data;
S64 cap;
S64 len;
T *push_empty(S64 count = 1){
T *push_empty(S64 count = 1) {
grow(count);
T *result = data + len;
len += count;
return result;
}
T *push_empty_zero(S64 count = 1){
T *push_empty_zero(S64 count = 1) {
T *result = push_empty(count);
memory_zero(result, count*sizeof(T));
memory_zero(result, count * sizeof(T));
return result;
}
void grow(S64 required_size){
if(cap == 0){
S64 new_cap = max(required_size*2, (S64)16);
void grow(S64 required_size) {
if (cap == 0) {
S64 new_cap = max(required_size * 2, (S64)16);
data = allocate_array(allocator, T, new_cap);
cap = new_cap;
}
else if(len + required_size > cap){
U64 new_cap = max(cap * 2, len+required_size+1);
else if (len + required_size > cap) {
U64 new_cap = max(cap * 2, len + required_size + 1);
T *new_data = allocate_array(allocator, T, new_cap);
memory_copy(new_data, data, cap*sizeof(T));
memory_copy(new_data, data, cap * sizeof(T));
deallocate(allocator, data);
data = new_data;
cap = new_cap;
}
}
S64 get_index(T *item){
S64 get_index(T *item) {
assert((data <= item) && ((data + len) > item));
size_t offset = item - data;
return (S64)offset;
}
void add(Array<T> items){
For(items){
void add(Array<T> items) {
For(items) {
add(it);
}
}
void add(T item){
void add(T item) {
grow(1);
data[len++] = item;
}
S64 addi(T item){
S64 addi(T item) {
S64 result = len;
grow(1);
data[len++] = item;
return result;
}
void unordered_remove(T *item){
void unordered_remove(T *item) {
assert(len > 0);
assert((data <= item) && ((data + len) > item));
*item = data[--len];
}
void init(Allocator *a, S64 size = 16){
void init(Allocator *a, S64 size = 16) {
allocator = a;
data = allocate_array(a, T, size);
cap = size;
}
Array<T> copy(Allocator *a){
Array<T> copy(Allocator *a) {
Array<T> result = {};
result.len = len;
result.cap = len*2;
result.cap = len * 2;
result.allocator = a;
result.data = allocate_array(a, T, result.cap);
memory_copy(result.data, data, sizeof(T)*result.len);
memory_copy(result.data, data, sizeof(T) * result.len);
return result;
}
Array<T> tight_copy(Allocator *a){
Array<T> tight_copy(Allocator *a) {
Array<T> result = {};
result.len = len;
result.cap = len;
result.allocator = 0;
result.data = allocate_array(a, T, len);
memory_copy(result.data, data, sizeof(T)*len);
memory_copy(result.data, data, sizeof(T) * len);
return result;
}
force_inline B32 is_last(T *item){ return item == last(); }
force_inline B32 is_first(T *item){ return item == begin(); }
force_inline void clear(){ len = 0; }
force_inline B32 is_last(T *item) { return item == last(); }
force_inline B32 is_first(T *item) { return item == begin(); }
force_inline void clear() { len = 0; }
force_inline T pop() { return data[--len]; }
force_inline T *last() { return data + len - 1; }
force_inline T *begin() { return data; }
force_inline T *end () { return data + len; }
force_inline T &operator[](S64 i){ assert(i >= 0 && i < cap); return data[i]; }
force_inline T *end() { return data + len; }
force_inline T &operator[](S64 i) {
assert(i >= 0 && i < cap);
return data[i];
}
};
template<class T>
template <class T>
CORE_Static Array<T>
array_make(Allocator *a, S64 size = 16){
array_make(Allocator *a, S64 size = 16) {
Array<T> result = {};
result.init(a, size);
return result;
@@ -116,13 +118,13 @@ array_make(Allocator *a, S64 size = 16){
//-----------------------------------------------------------------------------
// Map
//-----------------------------------------------------------------------------
struct Map_Key_Value{
struct Map_Key_Value {
int occupied;
U64 key;
void *value;
};
struct Map{
struct Map {
Allocator *allocator;
Map_Key_Value *data;
S64 len;
@@ -131,7 +133,7 @@ struct Map{
CORE_Static void map_insert(Map *map, U64 key, void *val);
CORE_Static void
map_grow(Map *map, S64 new_size){
map_grow(Map *map, S64 new_size) {
new_size = max((S64)16, new_size);
assert(new_size > map->cap);
assert(is_pow2(new_size));
@@ -142,89 +144,89 @@ CORE_Static void
new_map.cap = new_size;
new_map.allocator = map->allocator;
for(S64 i = 0; i < map->cap; i++){
if(map->data[i].occupied){
for (S64 i = 0; i < map->cap; i++) {
if (map->data[i].occupied) {
map_insert(&new_map, map->data[i].key, map->data[i].value);
}
}
if(map->data) deallocate(map->allocator, map->data);
if (map->data) deallocate(map->allocator, map->data);
*map = new_map;
}
CORE_Static Map
map_make(Allocator *a, S64 size){
map_make(Allocator *a, S64 size) {
Map result = {a};
map_grow(&result, size);
return result;
}
CORE_Static void
map_insert(Map *map, U64 key, void *val){
map_insert(Map *map, U64 key, void *val) {
assert(val);
assert(key);
// if(key == 0) key+=1;
if((2*map->len) + 1 > map->cap){
map_grow(map, 2*map->cap);
if ((2 * map->len) + 1 > map->cap) {
map_grow(map, 2 * map->cap);
}
U64 hash = hash_u64(key);
U64 index = wrap_around_pow2(hash, map->cap);
U64 i = index;
for(;;){
if(map->data[i].occupied == false){
for (;;) {
if (map->data[i].occupied == false) {
map->len++;
map->data[i].occupied = true;
map->data[i].key = key;
map->data[i].value = val;
return;
}
else if(map->data[i].key == key){
else if (map->data[i].key == key) {
map->data[i].value = val;
return;
}
i = wrap_around_pow2(i+1, map->cap);
if(i == map->cap){
i = wrap_around_pow2(i + 1, map->cap);
if (i == map->cap) {
return;
}
}
}
CORE_Static Map_Key_Value *
map_base_get(Map *map, U64 key){
if(map->len == 0) return 0;
map_base_get(Map *map, U64 key) {
if (map->len == 0) return 0;
assert(key);
U64 hash = hash_u64(key);
U64 index = wrap_around_pow2(hash, map->cap);
U64 i = index;
for(;;){
if(map->data[i].key == key){
for (;;) {
if (map->data[i].key == key) {
return map->data + i;
}
else if(map->data[i].key == 0){
else if (map->data[i].key == 0) {
return 0;
}
i = wrap_around_pow2(i+1, map->cap);
if(i == map->cap){
i = wrap_around_pow2(i + 1, map->cap);
if (i == map->cap) {
return 0;
}
}
}
CORE_Static void *
map_get(Map *map, U64 key){
map_get(Map *map, U64 key) {
Map_Key_Value *result = map_base_get(map, key);
if(result && result->occupied) return result->value;
if (result && result->occupied) return result->value;
return 0;
}
CORE_Static void *
map_remove(Map *map, U64 key){
map_remove(Map *map, U64 key) {
Map_Key_Value *kv = map_base_get(map, key);
if(kv){
if (kv) {
kv->occupied = false;
return kv->value;
}
@@ -232,29 +234,29 @@ CORE_Static void *
}
CORE_Static void *
map_get(Map *map, void *pointer){
map_get(Map *map, void *pointer) {
return map_get(map, (U64)pointer);
}
CORE_Static void *
map_get(Map *map, Intern_String string){
map_get(Map *map, Intern_String string) {
return map_get(map, hash_string(string.s));
}
CORE_Static void
map_insert(Map *map, void *key, void *value){
map_insert(Map *map, void *key, void *value) {
map_insert(map, (U64)key, value);
}
CORE_Static void
map_insert(Map *map, Intern_String key, void *value){
map_insert(Map *map, Intern_String key, void *value) {
map_insert(map, hash_string(key.s), value);
}
//-----------------------------------------------------------------------------
// String intern
//-----------------------------------------------------------------------------
struct Intern_Table{
struct Intern_Table {
Allocator *string_allocator;
Map map;
U8 *first_keyword;
@@ -262,7 +264,7 @@ struct Intern_Table{
};
CORE_Static Intern_Table
intern_table_make(Allocator *string_allocator, Allocator *map_allocator, S64 initial_size = 32){
intern_table_make(Allocator *string_allocator, Allocator *map_allocator, S64 initial_size = 32) {
Intern_Table result = {};
result.map = map_make(map_allocator, initial_size);
result.string_allocator = string_allocator;
@@ -270,17 +272,19 @@ CORE_Static Intern_Table
}
CORE_Static Intern_String
intern_string(Intern_Table *t, String string){
intern_string(Intern_Table *t, String string) {
assert(t->string_allocator);
U64 hash = hash_string(string);
U8 *slot = (U8 *)map_get(&t->map, hash);
if(slot){
if (slot) {
// @todo: Is this a cast bug: *(slot-sizeof(S64))? slot is u8 so truncates?
Intern_String result = {{slot, *(slot-sizeof(S64))}};
Intern_String result = {
{slot, *(slot - sizeof(S64))}
};
return result;
}
S64 *len_address = (S64 *)allocate_size(t->string_allocator, string.len+1+sizeof(S64), false);
S64 *len_address = (S64 *)allocate_size(t->string_allocator, string.len + 1 + sizeof(S64), false);
*len_address = string.len;
U8 *string_address = (U8 *)(len_address + 1);
@@ -288,7 +292,9 @@ CORE_Static Intern_String
string_address[string.len] = 0;
map_insert(&t->map, hash, string_address);
Intern_String result = {{string_address, *len_address}};
Intern_String result = {
{string_address, *len_address}
};
return result;
}
@@ -302,8 +308,8 @@ CORE_Static Intern_String
const int LIST_DEFAULT_BLOCK_SIZE = 32;
const int LIST_DEFAULT_ALLOCATION_MUL = 2;
template<class T>
struct List_Node{
template <class T>
struct List_Node {
List_Node<T> *next;
List_Node<T> *prev;
int cap;
@@ -311,15 +317,15 @@ struct List_Node{
T data[];
};
template<class T>
struct List{
template <class T>
struct List {
int block_size = 0;
int allocation_multiplier = 0;
List_Node<T> *first = 0;
List_Node<T> *last = 0;
List_Node<T> *first_free = 0;
struct Iter{
struct Iter {
T *item;
List_Node<T> *node;
int node_index;
@@ -349,13 +355,13 @@ struct List{
result.node_index = -1;
return ++result;
}
Iter end() { return{}; }
Iter end() { return {}; }
friend bool operator!=(Iter &a, Iter &b) { return a.item != b.item; }
};
template<class T>
List_Node<T> *list_allocate_node(Allocator *arena, int size){
auto node = (List_Node<T> *)allocate_size(arena, sizeof(List_Node<T>) + size*sizeof(T), false);
template <class T>
List_Node<T> *list_allocate_node(Allocator *arena, int size) {
auto node = (List_Node<T> *)allocate_size(arena, sizeof(List_Node<T>) + size * sizeof(T), false);
node->cap = size;
node->len = 0;
node->next = 0;
@@ -363,21 +369,21 @@ List_Node<T> *list_allocate_node(Allocator *arena, int size){
return node;
}
template<class T>
void list_allocate_free_node(Allocator *arena, List<T> *list, int size){
template <class T>
void list_allocate_free_node(Allocator *arena, List<T> *list, int size) {
List_Node<T> *node = list_allocate_node<T>(arena, size);
DLL_STACK_ADD(list->first_free, node);
}
template<class T>
void list_make_sure_there_is_room_for_item_count(Allocator *arena, List<T> *list, int item_count){
if(list->last == 0 || list->last->len + item_count > list->last->cap){
template <class T>
void list_make_sure_there_is_room_for_item_count(Allocator *arena, List<T> *list, int item_count) {
if (list->last == 0 || list->last->len + item_count > list->last->cap) {
// Not enough space we need to get a new block
List_Node<T> *node = 0;
// Iterate the free list to check if we have a block of required size there
For_Linked_List(list->first_free){
if(it->cap >= item_count){
For_Linked_List(list->first_free) {
if (it->cap >= item_count) {
DLL_STACK_REMOVE(list->first_free, it);
node = it;
node->len = 0;
@@ -386,13 +392,13 @@ void list_make_sure_there_is_room_for_item_count(Allocator *arena, List<T> *list
}
// We don't have a block on the free list need to allocate
if(!node){
if (!node) {
// Set default values if not initialized
if(!list->allocation_multiplier) list->allocation_multiplier = LIST_DEFAULT_ALLOCATION_MUL;
if(!list->block_size) list->block_size = LIST_DEFAULT_BLOCK_SIZE;
if (!list->allocation_multiplier) list->allocation_multiplier = LIST_DEFAULT_ALLOCATION_MUL;
if (!list->block_size) list->block_size = LIST_DEFAULT_BLOCK_SIZE;
if(item_count > list->block_size)
list->block_size = item_count*2;
if (item_count > list->block_size)
list->block_size = item_count * 2;
node = list_allocate_node<T>(arena, list->block_size);
list->block_size *= list->allocation_multiplier;
}
@@ -402,15 +408,15 @@ void list_make_sure_there_is_room_for_item_count(Allocator *arena, List<T> *list
}
}
template<class T>
T *list_get(List<T> *list, int index, List_Node<T> **node = 0, int *in_block_index = 0){
if(list){
template <class T>
T *list_get(List<T> *list, int index, List_Node<T> **node = 0, int *in_block_index = 0) {
if (list) {
int i = 0;
For_Linked_List(list->first){
For_Linked_List(list->first) {
int lookup_i = index - i;
if(lookup_i < it->len) {
if(node) *node = it;
if(in_block_index) *in_block_index = lookup_i;
if (lookup_i < it->len) {
if (node) *node = it;
if (in_block_index) *in_block_index = lookup_i;
return it->data + lookup_i;
}
i += it->len;
@@ -419,42 +425,42 @@ T *list_get(List<T> *list, int index, List_Node<T> **node = 0, int *in_block_ind
return 0;
}
template<class T>
T *getp(List<T> *list, int index){
template <class T>
T *getp(List<T> *list, int index) {
return list_get(list, index);
}
template<class T>
T get(List<T> *list, int index){
template <class T>
T get(List<T> *list, int index) {
return *list_get(list, index);
}
template<class T>
void add(Allocator *arena, List<T> *list, T item){
template <class T>
void add(Allocator *arena, List<T> *list, T item) {
list_make_sure_there_is_room_for_item_count(arena, list, 1);
list->last->data[list->last->len++] = item;
}
template<class T>
T *add_size(Allocator *arena, List<T> *list, int count = 1, int zero_memory = 0){
template <class T>
T *add_size(Allocator *arena, List<T> *list, int count = 1, int zero_memory = 0) {
list_make_sure_there_is_room_for_item_count(arena, list, count);
T *result = list->last->data + list->last->len;
list->last->len += count;
if(zero_memory){
memory_zero(result, sizeof(T)*count);
if (zero_memory) {
memory_zero(result, sizeof(T) * count);
}
return result;
}
template<class T>
void list_free_node(List<T> *list, List_Node<T> *node){
template <class T>
void list_free_node(List<T> *list, List_Node<T> *node) {
#if 1
// Make sure it's actually in list list
bool found = false;
For_Linked_List(list->first){
if(it == node){
For_Linked_List(list->first) {
if (it == node) {
found = true;
break;
}
@@ -466,60 +472,61 @@ void list_free_node(List<T> *list, List_Node<T> *node){
DLL_STACK_ADD(list->first_free, node);
}
template<class T>
void clear(List<T> *list){
template <class T>
void clear(List<T> *list) {
memory_zero(list, sizeof(List<T>));
}
template<class T>
int length(List<T> *list){
template <class T>
int length(List<T> *list) {
int result = 0;
For_Linked_List(list->first){
For_Linked_List(list->first) {
result += it->len;
}
return result;
}
template<class T>
void free_all_nodes(List<T> *list){
template <class T>
void free_all_nodes(List<T> *list) {
assert(!list->last->next);
assert(!list->first->prev);
list->last->next = list->first_free;
if(list->first_free) list->first_free->prev = list->last;
if (list->first_free) list->first_free->prev = list->last;
list->first_free = list->first;
list->last = list->first = 0;
}
template<class T>
T ordered_remove(List<T> *list, int index){
List_Node<T> *node; int in_block_index;
template <class T>
T ordered_remove(List<T> *list, int index) {
List_Node<T> *node;
int in_block_index;
T *data = list_get(list, index, &node, &in_block_index);
assert_message(data, "Trying to unordered_remove element that's outside of the List");
if(!data)
if (!data)
return {};
T result = *data;
// Check if we need to deallocate the block
if(node->len == 1) {
if (node->len == 1) {
list_free_node(list, node);
return result;
}
// We need to move part of the block to fill the new empty spot
int right_count = (--node->len) - in_block_index;
memory_copy(data, data+1, sizeof(T)*right_count);
memory_copy(data, data + 1, sizeof(T) * right_count);
return result;
}
template<class T>
T unordered_remove(List<T> *list, int index){
template <class T>
T unordered_remove(List<T> *list, int index) {
List_Node<T> *node;
T *data = list_get(list, index, &node);
assert_message(data, "Trying to unordered_remove element that's outside of the List");
if(!data)
if (!data)
return {};
assert(node->len);
@@ -530,32 +537,32 @@ T unordered_remove(List<T> *list, int index){
*data = node->data[node->len - 1];
node->len -= 1;
if(node->len == 0){
if (node->len == 0) {
list_free_node(list, node);
}
return result;
}
template<class T>
T pop(List<T> *list){
template <class T>
T pop(List<T> *list) {
assert(list->last != 0);
assert(list->last->len > 0);
T result = list->last->data[--list->last->len];
if(list->last->len == 0){
if (list->last->len == 0) {
list_free_node(list, list->last);
}
return result;
}
template<class T>
T *merge(Allocator *arena, List<T> *list){
template <class T>
T *merge(Allocator *arena, List<T> *list) {
int len = length(list);
T *result = allocate_size(arena, T, len, false);
int i = 0;
For_Linked_List(list->first){
memory_copy(result + i, it->data, it->len*sizeof(T));
For_Linked_List(list->first) {
memory_copy(result + i, it->data, it->len * sizeof(T));
i += it->len;
}

View File

@@ -14,15 +14,15 @@ to_upper_case(U8 a) {
}
CORE_Static U8
char_to_lower(U8 c){
if(c >= 'A' && c <= 'Z')
char_to_lower(U8 c) {
if (c >= 'A' && c <= 'Z')
c += 32;
return c;
}
CORE_Static U8
char_to_upper(U8 c){
if(c >= 'a' && c <= 'z')
char_to_upper(U8 c) {
if (c >= 'a' && c <= 'z')
c -= 32;
return c;
}
@@ -71,13 +71,13 @@ string_compare(String a, String b, B32 ignore_case = false) {
}
CORE_Static B32
operator==(String a, String b){
return string_compare(a,b);
operator==(String a, String b) {
return string_compare(a, b);
}
CORE_Static String
string_copy(Allocator *a, String string){
U8 *copy = allocate_array(a, U8, string.len+1);
string_copy(Allocator *a, String string) {
U8 *copy = allocate_array(a, U8, string.len + 1);
memory_copy(copy, string.str, string.len);
copy[string.len] = 0;
return String{copy, string.len};
@@ -98,10 +98,10 @@ string_fmtv(Allocator *a, const char *str, va_list args1) {
}
#define STRING_FMT(alloc, str, result) \
va_list args1; \
va_start(args1, str); \
String result = string_fmtv(alloc, str, args1); \
va_end(args1)
va_list args1; \
va_start(args1, str); \
String result = string_fmtv(alloc, str, args1); \
va_end(args1)
CORE_Static String
string_fmt(Allocator *a, const char *str, ...) {
@@ -112,61 +112,62 @@ string_fmt(Allocator *a, const char *str, ...) {
//-----------------------------------------------------------------------------
// String builder
//-----------------------------------------------------------------------------
struct String_Builder_Block{
struct String_Builder_Block {
String_Builder_Block *next;
S64 cap;
S64 len;
U8 data[0];
};
struct String_Builder{
struct String_Builder {
Allocator *allocator;
String_Builder_Block *first_free;
String_Builder_Block *first;
String_Builder_Block *last;
U64 di;
void reset(){
for(;;){
void reset() {
for (;;) {
auto *block = first;
first = first->next;
block->next = first_free;
first_free = block;
if(!first) break;
if (!first) break;
}
last = 0;
assert(!last && !first);
}
void push_block(size_t size){
void push_block(size_t size) {
String_Builder_Block *block = 0;
if(first_free){
if (first_free) {
block = first_free;
first_free = first_free->next;
} else{
}
else {
block = (String_Builder_Block *)allocate_size(allocator, sizeof(String_Builder_Block) + size, false);
}
memory_zero(block, sizeof(String_Builder_Block)+1); // Also clear first byte of character data
memory_zero(block, sizeof(String_Builder_Block) + 1); // Also clear first byte of character data
block->cap = size;
SLL_QUEUE_ADD(first, last, block);
}
void init(S64 size = 4096){
void init(S64 size = 4096) {
assert(allocator);
push_block(size);
}
void append_data(void *data, S64 size){
if(first == 0){
void append_data(void *data, S64 size) {
if (first == 0) {
init();
}
S64 remaining_cap = last->cap - last->len;
if(size > remaining_cap){
S64 new_block_size = max(last->cap*2, size*2);
if (size > remaining_cap) {
S64 new_block_size = max(last->cap * 2, size * 2);
push_block(new_block_size);
}
@@ -175,13 +176,13 @@ struct String_Builder{
memory_copy(write_address, data, size);
}
void addf(const char *str, ...){
if(first == 0){
void addf(const char *str, ...) {
if (first == 0) {
init();
}
va_list args, args2;
va_start(args, str);
retry:{
retry : {
String_Builder_Block *block = last;
S64 block_size = block->cap - block->len;
char *write_address = (char *)block->data + block->len;
@@ -190,8 +191,8 @@ struct String_Builder{
int written = stbsp_vsnprintf(write_address, (int)block_size, str, args2);
va_end(args2);
if(written > (block_size-1)){
S64 new_block_size = max(4096, (written+1)*2);
if (written > (block_size - 1)) {
S64 new_block_size = max(4096, (written + 1) * 2);
push_block(new_block_size);
goto retry;
}
@@ -203,17 +204,17 @@ struct String_Builder{
};
CORE_Static String_Builder
string_builder_make(Allocator *a, S64 first_block_size = 4096){
string_builder_make(Allocator *a, S64 first_block_size = 4096) {
String_Builder sb = {a};
sb.init(first_block_size);
return sb;
}
// @! Make string_flatten a method
static String string_flatten(Allocator *a, String_Builder *b){
static String string_flatten(Allocator *a, String_Builder *b) {
// @Note(Krzosa): Compute size to allocate
S64 size = 1;
For_Linked_List(b->first){
For_Linked_List(b->first) {
size += it->len;
}
@@ -221,7 +222,7 @@ static String string_flatten(Allocator *a, String_Builder *b){
result.str = (U8 *)allocate_size(a, size, false);
// @Note(Krzosa): Copy the content of each block into the string
For_Linked_List(b->first){
For_Linked_List(b->first) {
memory_copy(result.str + result.len, it->data, it->len);
result.len += it->len;
}
@@ -251,7 +252,7 @@ string_make(char *str, S64 len) {
CORE_Static String
string_make(U8 *str, S64 len) {
return string_make((char*)str, len);
return string_make((char *)str, len);
}
CORE_Static String
@@ -307,7 +308,6 @@ CORE_Static String
string_trim(String string) {
if (string.len == 0) return string;
S64 whitespace_begin = 0;
for (; whitespace_begin < string.len; whitespace_begin++) {
if (!is_whitespace(string.str[whitespace_begin])) {
@@ -365,9 +365,9 @@ string_to_upper_case(Allocator *arena, String s) {
typedef U32 MatchFlag;
enum {
MatchFlag_None=0,
MatchFlag_FindLast=1,
MatchFlag_IgnoreCase=2,
MatchFlag_None = 0,
MatchFlag_FindLast = 1,
MatchFlag_IgnoreCase = 2,
};
CORE_Static B32
@@ -435,14 +435,14 @@ string_skip_to_last_period(String s) {
}
CORE_Static S64
string_len(char *string){
string_len(char *string) {
S64 len = 0;
while(*string++!=0)len++;
while (*string++ != 0) len++;
return len;
}
CORE_Static String
string_from_cstring(char *string){
string_from_cstring(char *string) {
String result;
result.str = (U8 *)string;
result.len = string_len(string);
@@ -455,14 +455,14 @@ struct String_Replace {
};
CORE_Static String
string_replace(Arena *scratch, Allocator *allocator, String string, Array<String_Replace> pairs){
string_replace(Arena *scratch, Allocator *allocator, String string, Array<String_Replace> pairs) {
Scratch_Scope _scope(scratch);
String_Builder builder = {scratch};
for(S64 i = 0; i < string.len; i++){
For(pairs){
for (S64 i = 0; i < string.len; i++) {
For(pairs) {
String current = string_skip(string, i);
current = string_get_prefix(current, it.find.len);
if(current == it.find){
if (current == it.find) {
builder.append_data(it.replace.str, it.replace.len);
i += it.find.len;
continue;

View File

@@ -2,12 +2,12 @@ global U32 question_mark32 = '?';
global U16 question_mark16 = 0x003f;
global U8 question_mark8 = '?';
struct String32{
struct String32 {
U32 *str;
S64 len;
};
struct UTF32_Result{
struct UTF32_Result {
U32 out_str;
S64 advance;
B32 error;
@@ -18,7 +18,7 @@ utf8_to_utf32(U8 *c, S64 max_advance) {
UTF32_Result result = {};
if ((c[0] & 0b10000000) == 0) { // Check if leftmost zero of first byte is unset
if(max_advance >= 1){
if (max_advance >= 1) {
result.out_str = c[0];
result.advance = 1;
}
@@ -27,7 +27,7 @@ utf8_to_utf32(U8 *c, S64 max_advance) {
else if ((c[0] & 0b11100000) == 0b11000000) {
if ((c[1] & 0b11000000) == 0b10000000) { // Continuation byte required
if(max_advance >= 2){
if (max_advance >= 2) {
result.out_str = (U32)(c[0] & 0b00011111) << 6u | (c[1] & 0b00111111);
result.advance = 2;
}
@@ -38,7 +38,7 @@ utf8_to_utf32(U8 *c, S64 max_advance) {
else if ((c[0] & 0b11110000) == 0b11100000) {
if ((c[1] & 0b11000000) == 0b10000000 && (c[2] & 0b11000000) == 0b10000000) { // Two continuation bytes required
if(max_advance >= 3){
if (max_advance >= 3) {
result.out_str = (U32)(c[0] & 0b00001111) << 12u | (U32)(c[1] & 0b00111111) << 6u | (c[2] & 0b00111111);
result.advance = 3;
}
@@ -49,7 +49,7 @@ utf8_to_utf32(U8 *c, S64 max_advance) {
else if ((c[0] & 0b11111000) == 0b11110000) {
if ((c[1] & 0b11000000) == 0b10000000 && (c[2] & 0b11000000) == 0b10000000 && (c[3] & 0b11000000) == 0b10000000) { // Three continuation bytes required
if(max_advance >= 4){
if (max_advance >= 4) {
result.out_str = (U32)(c[0] & 0b00001111) << 18u | (U32)(c[1] & 0b00111111) << 12u | (U32)(c[2] & 0b00111111) << 6u | (U32)(c[3] & 0b00111111);
result.advance = 4;
}
@@ -62,19 +62,19 @@ utf8_to_utf32(U8 *c, S64 max_advance) {
return result;
}
struct String16{
struct String16 {
U16 *str;
S64 len;
};
struct UTF16_Result{
struct UTF16_Result {
U16 out_str[2];
S32 len;
B32 error;
};
CORE_Static UTF16_Result
utf32_to_utf16(U32 codepoint){
utf32_to_utf16(U32 codepoint) {
UTF16_Result result = {};
if (codepoint < 0x10000) {
result.out_str[0] = (U16)codepoint;
@@ -87,14 +87,14 @@ utf32_to_utf16(U32 codepoint){
result.out_str[1] = (U16)(0xDC00 | (code & 0x3FF));
result.len = 2;
}
else{
else {
result.error = 1;
}
return result;
}
struct UTF8_Result{
struct UTF8_Result {
U8 out_str[4];
S32 len;
B32 error;
@@ -108,24 +108,24 @@ utf32_to_utf8(U32 codepoint) {
result.out_str[0] = (U8)codepoint;
}
else if (codepoint <= 0x7FF) {
result.len= 2;
result.len = 2;
result.out_str[0] = 0b11000000 | (0b00011111 & (codepoint >> 6));
result.out_str[1] = 0b10000000 | (0b00111111 & codepoint);
}
else if (codepoint <= 0xFFFF) { // 16 bit word
result.len= 3;
result.len = 3;
result.out_str[0] = 0b11100000 | (0b00001111 & (codepoint >> 12)); // 4 bits
result.out_str[1] = 0b10000000 | (0b00111111 & (codepoint >> 6)); // 6 bits
result.out_str[2] = 0b10000000 | (0b00111111 & codepoint); // 6 bits
}
else if (codepoint <= 0x10FFFF) { // 21 bit word
result.len= 4;
result.len = 4;
result.out_str[0] = 0b11110000 | (0b00000111 & (codepoint >> 18)); // 3 bits
result.out_str[1] = 0b10000000 | (0b00111111 & (codepoint >> 12)); // 6 bits
result.out_str[2] = 0b10000000 | (0b00111111 & (codepoint >> 6)); // 6 bits
result.out_str[3] = 0b10000000 | (0b00111111 & codepoint); // 6 bits
}
else{
else {
result.error = true;
}
@@ -135,11 +135,11 @@ utf32_to_utf8(U32 codepoint) {
CORE_Static UTF32_Result
utf16_to_utf32(U16 *c, S32 max_advance) {
UTF32_Result result = {};
if(max_advance >= 1){
if (max_advance >= 1) {
result.advance = 1;
result.out_str = c[0];
if (c[0] >= 0xD800 && c[0] <= 0xDBFF && c[1] >= 0xDC00 && c[1] <= 0xDFFF) {
if(max_advance >= 2){
if (max_advance >= 2) {
result.out_str = 0x10000;
result.out_str += (U32)(c[0] & 0x03FF) << 10u | (c[1] & 0x03FF);
result.advance = 2;
@@ -159,11 +159,11 @@ utf16_to_utf32(U16 *c, S32 max_advance) {
}
CORE_Static String32
string16_to_string32(Allocator *allocator, String16 string){
String32 result = {allocate_array(allocator, U32, string.len+1)};
for(S64 i = 0; i < string.len;){
string16_to_string32(Allocator *allocator, String16 string) {
String32 result = {allocate_array(allocator, U32, string.len + 1)};
for (S64 i = 0; i < string.len;) {
UTF32_Result decode = utf16_to_utf32(string.str + i, (S32)(string.len - i));
if(!decode.error){
if (!decode.error) {
i += decode.advance;
result.str[result.len++] = decode.out_str;
}
@@ -175,11 +175,11 @@ string16_to_string32(Allocator *allocator, String16 string){
}
CORE_Static String32
string8_to_string32(Allocator *allocator, String string){
String32 result = {allocate_array(allocator, U32, string.len+1)};
for(S64 i = 0; i < string.len;){
string8_to_string32(Allocator *allocator, String string) {
String32 result = {allocate_array(allocator, U32, string.len + 1)};
for (S64 i = 0; i < string.len;) {
UTF32_Result decode = utf8_to_utf32(string.str + i, string.len - i);
if(!decode.error){
if (!decode.error) {
i += decode.advance;
result.str[result.len++] = decode.out_str;
}
@@ -190,15 +190,15 @@ string8_to_string32(Allocator *allocator, String string){
}
CORE_Static String16
string8_to_string16(Allocator *allocator, String in){
String16 result = {allocate_array(allocator, U16, (in.len*2)+1)}; // @Note(Krzosa): Should be more then enough space
for(S64 i = 0; i < in.len;){
string8_to_string16(Allocator *allocator, String in) {
String16 result = {allocate_array(allocator, U16, (in.len * 2) + 1)}; // @Note(Krzosa): Should be more then enough space
for (S64 i = 0; i < in.len;) {
UTF32_Result decode = utf8_to_utf32(in.str + i, in.len - i);
if(!decode.error){
if (!decode.error) {
i += decode.advance;
UTF16_Result encode = utf32_to_utf16(decode.out_str);
if(!encode.error){
for(S32 j = 0; j < encode.len; j++){
if (!encode.error) {
for (S32 j = 0; j < encode.len; j++) {
result.str[result.len++] = encode.out_str[j];
}
}
@@ -212,15 +212,15 @@ string8_to_string16(Allocator *allocator, String in){
}
CORE_Static String
string16_to_string8(Allocator *allocator, String16 in){
String result = {allocate_array(allocator, U8, in.len*4+1)};
for(S64 i = 0; i < in.len;){
string16_to_string8(Allocator *allocator, String16 in) {
String result = {allocate_array(allocator, U8, in.len * 4 + 1)};
for (S64 i = 0; i < in.len;) {
UTF32_Result decode = utf16_to_utf32(in.str + i, (S32)(in.len - i));
if(!decode.error){
if (!decode.error) {
i += decode.advance;
UTF8_Result encode = utf32_to_utf8(decode.out_str);
if(!encode.error){
for(S32 j = 0; j < encode.len; j++)
if (!encode.error) {
for (S32 j = 0; j < encode.len; j++)
result.str[result.len++] = encode.out_str[j];
}
else unicode_error(question_mark8);
@@ -233,32 +233,32 @@ string16_to_string8(Allocator *allocator, String16 in){
}
CORE_Static B32
string_compare(String16 a, String16 b){
if(a.len != b.len) return false;
for(S64 i = 0; i < a.len; i++){
if(a.str[i] != b.str[i]) return false;
string_compare(String16 a, String16 b) {
if (a.len != b.len) return false;
for (S64 i = 0; i < a.len; i++) {
if (a.str[i] != b.str[i]) return false;
}
return true;
}
CORE_Static B32
string_compare(String32 a, String32 b){
if(a.len != b.len) return false;
for(S64 i = 0; i < a.len; i++){
if(a.str[i] != b.str[i]) return false;
string_compare(String32 a, String32 b) {
if (a.len != b.len) return false;
for (S64 i = 0; i < a.len; i++) {
if (a.str[i] != b.str[i]) return false;
}
return true;
}
CORE_Static S64
widechar_len(wchar_t *string){
widechar_len(wchar_t *string) {
S64 len = 0;
while(*string++!=0)len++;
while (*string++ != 0) len++;
return len;
}
CORE_Static String16
string16_from_widechar(wchar_t *string){
string16_from_widechar(wchar_t *string) {
String16 result;
result.str = (U16 *)string;
result.len = widechar_len(string);
@@ -266,8 +266,8 @@ string16_from_widechar(wchar_t *string){
}
CORE_Static String
string16_copy(Allocator *a, String string){
U8 *copy = allocate_array(a, U8, string.len+1);
string16_copy(Allocator *a, String string) {
U8 *copy = allocate_array(a, U8, string.len + 1);
memory_copy(copy, string.str, string.len);
copy[string.len] = 0;
return String{copy, string.len};

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,7 @@
struct Token;
#include <inttypes.h>
enum CmpRes
{
enum CmpRes {
CMP_LT,
CMP_GT,
CMP_EQ,

View File

@@ -1,6 +1,6 @@
#define AST_NEW(T,ikind,ipos,iflags) \
#define AST_NEW(T, ikind, ipos, iflags) \
Ast_##T *result = allocate_struct(pctx->perm, Ast_##T); \
result->flags = iflags; \
result->kind = AST_##ikind; \
@@ -8,9 +8,9 @@
result->pos = ipos; \
result->di = ++pctx->unique_ids
#define ast_new(T,kind,pos,flags) (T *)_ast_new(sizeof(T), kind, pos, flags)
#define ast_new(T, kind, pos, flags) (T *)_ast_new(sizeof(T), kind, pos, flags)
CORE_Static Ast *
_ast_new(size_t size, Ast_Kind kind, Token *pos, Ast_Flag flags = 0){
_ast_new(size_t size, Ast_Kind kind, Token *pos, Ast_Flag flags = 0) {
Ast *result = (Ast *)allocate_size(pctx->perm, size);
result->flags = flags;
result->kind = kind;
@@ -21,7 +21,7 @@ _ast_new(size_t size, Ast_Kind kind, Token *pos, Ast_Flag flags = 0){
}
CORE_Static Ast_Atom *
ast_str(Token *pos, Intern_String string){
ast_str(Token *pos, Intern_String string) {
AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM);
result->type = pctx->untyped_string;
result->intern_val = string;
@@ -29,14 +29,14 @@ ast_str(Token *pos, Intern_String string){
}
CORE_Static Ast_Atom *
ast_ident(Token *pos, Intern_String string){
ast_ident(Token *pos, Intern_String string) {
AST_NEW(Atom, IDENT, pos, AST_EXPR | AST_ATOM);
result->intern_val = string;
return result;
}
CORE_Static Ast_Atom *
ast_bool(Token *pos, B32 bool_val){
ast_bool(Token *pos, B32 bool_val) {
AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM);
result->bool_val = bool_val;
result->type = pctx->untyped_bool;
@@ -44,7 +44,7 @@ ast_bool(Token *pos, B32 bool_val){
}
CORE_Static Ast_Atom *
ast_float(Token *pos, F64 value){
ast_float(Token *pos, F64 value) {
AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM);
result->type = pctx->untyped_float;
result->f64_val = value;
@@ -52,7 +52,7 @@ ast_float(Token *pos, F64 value){
}
CORE_Static Ast_Atom *
ast_int(Token *pos, BigInt val){
ast_int(Token *pos, BigInt val) {
AST_NEW(Atom, VALUE, pos, AST_EXPR | AST_ATOM);
result->type = pctx->untyped_int;
result->big_int_val = bigint_copy(pctx->perm, &val);
@@ -60,12 +60,12 @@ ast_int(Token *pos, BigInt val){
}
CORE_Static Ast_Atom *
ast_int(Token *pos, U64 value){
ast_int(Token *pos, U64 value) {
return ast_int(pos, bigint_u64(value));
}
CORE_Static Ast_Expr *
ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op) {
AST_NEW(Binary, BINARY, op, AST_EXPR);
result->op = op->kind;
result->left = left;
@@ -74,7 +74,7 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
}
CORE_Static Ast_Call *
ast_call(Token *pos, Ast_Expr *name, Array<Ast_Call_Item *> exprs){
ast_call(Token *pos, Ast_Expr *name, Array<Ast_Call_Item *> exprs) {
// name here specifies also typespec for compound expressions !
AST_NEW(Call, CALL, pos, AST_EXPR);
result->name = name;
@@ -83,7 +83,7 @@ ast_call(Token *pos, Ast_Expr *name, Array<Ast_Call_Item *> exprs){
}
CORE_Static Ast_Call_Item *
ast_call_item(Token *pos, Ast_Atom *name, Ast_Expr *index, Ast_Expr *item){
ast_call_item(Token *pos, Ast_Atom *name, Ast_Expr *index, Ast_Expr *item) {
AST_NEW(Call_Item, CALL_ITEM, pos, AST_EXPR);
result->name = name;
result->item = item;
@@ -92,7 +92,7 @@ ast_call_item(Token *pos, Ast_Atom *name, Ast_Expr *index, Ast_Expr *item){
}
CORE_Static Ast_Expr *
ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){
ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr) {
AST_NEW(Unary, UNARY, pos, AST_EXPR);
result->flags = AST_EXPR;
result->expr = expr;
@@ -101,7 +101,7 @@ ast_expr_unary(Token *pos, Token_Kind op, Ast_Expr *expr){
}
CORE_Static Ast_Expr *
ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index) {
AST_NEW(Index, INDEX, pos, AST_EXPR);
result->flags = AST_EXPR;
result->expr = expr;
@@ -110,7 +110,7 @@ ast_expr_index(Token *pos, Ast_Expr *expr, Ast_Expr *index){
}
CORE_Static Ast_Lambda *
ast_lambda(Token *pos, Array<Ast_Decl *> params, Array<Ast_Expr *> ret, Ast_Scope *scope){
ast_lambda(Token *pos, Array<Ast_Decl *> params, Array<Ast_Expr *> ret, Ast_Scope *scope) {
AST_NEW(Lambda, LAMBDA_EXPR, pos, AST_EXPR);
result->flags = AST_EXPR;
result->args = params.tight_copy(pctx->perm);
@@ -120,14 +120,14 @@ ast_lambda(Token *pos, Array<Ast_Decl *> params, Array<Ast_Expr *> ret, Ast_Scop
}
CORE_Static Ast_If *
ast_if(Token *pos, Array<Ast_If_Node *> ifs){
ast_if(Token *pos, Array<Ast_If_Node *> ifs) {
AST_NEW(If, IF, pos, AST_STMT);
result->ifs = ifs.tight_copy(pctx->perm);
return result;
}
CORE_Static Ast_For *
ast_for(Token *pos, Ast_Expr *init, Ast_Expr *cond, Ast_Expr *iter, Ast_Scope *scope){
ast_for(Token *pos, Ast_Expr *init, Ast_Expr *cond, Ast_Expr *iter, Ast_Scope *scope) {
AST_NEW(For, FOR, pos, AST_STMT);
result->init = init;
result->cond = cond;
@@ -137,21 +137,21 @@ ast_for(Token *pos, Ast_Expr *init, Ast_Expr *cond, Ast_Expr *iter, Ast_Scope *s
}
CORE_Static Ast_Pass *
ast_pass(Token *pos){
ast_pass(Token *pos) {
AST_NEW(Pass, PASS, pos, AST_STMT);
return result;
}
CORE_Static Ast_Break *
ast_break(Token *pos){
ast_break(Token *pos) {
AST_NEW(Break, BREAK, pos, AST_STMT);
return result;
}
CORE_Static Ast_Return *
ast_return(Token *pos, Array<Ast_Expr *> expr){
ast_return(Token *pos, Array<Ast_Expr *> expr) {
AST_NEW(Return, RETURN, pos, AST_STMT);
if(expr.len){
if (expr.len) {
For(expr) assert(is_flag_set(it->flags, AST_EXPR));
result->expr = expr.tight_copy(pctx->perm);
}
@@ -159,26 +159,26 @@ ast_return(Token *pos, Array<Ast_Expr *> expr){
}
CORE_Static Ast_If_Node *
ast_if_node(Token *pos, Ast_Expr *init, Ast_Expr *expr, Ast_Scope *scope){
ast_if_node(Token *pos, Ast_Expr *init, Ast_Expr *expr, Ast_Scope *scope) {
AST_NEW(If_Node, IF_NODE, pos, AST_STMT);
result->scope = scope;
result->expr = expr;
result->init = (Ast_Binary *)init;
if(result->init) {
if (result->init) {
assert(init->kind == AST_VAR);
}
return result;
}
CORE_Static Ast_Array *
ast_array(Token *pos, Ast_Expr *expr){
ast_array(Token *pos, Ast_Expr *expr) {
AST_NEW(Array, ARRAY, pos, AST_EXPR);
result->expr = expr;
return result;
}
CORE_Static Ast_Scope *
begin_decl_scope(Allocator *scratch, Token *pos){
begin_decl_scope(Allocator *scratch, Token *pos) {
AST_NEW(Scope, SCOPE, pos, AST_DECL);
result->file = pctx->currently_parsed_file;
result->module = pctx->currently_parsed_file->module;
@@ -190,12 +190,12 @@ begin_decl_scope(Allocator *scratch, Token *pos){
}
CORE_Static void
finalize_decl_scope(Ast_Scope *scope){
finalize_decl_scope(Ast_Scope *scope) {
pctx->currently_parsed_scope = scope->parent_scope;
}
CORE_Static Ast_Scope *
begin_stmt_scope(Allocator *scratch, Token *pos){
begin_stmt_scope(Allocator *scratch, Token *pos) {
AST_NEW(Scope, SCOPE, pos, AST_STMT);
result->stmts = {scratch};
result->file = pctx->currently_parsed_file;
@@ -208,20 +208,20 @@ begin_stmt_scope(Allocator *scratch, Token *pos){
}
CORE_Static void
finalize_stmt_scope(Ast_Scope *scope){
finalize_stmt_scope(Ast_Scope *scope) {
scope->stmts = scope->stmts.tight_copy(pctx->perm);
pctx->currently_parsed_scope = scope->parent_scope;
}
CORE_Static Ast_Decl *
ast_struct(Token *pos, Ast_Scope *scope){
ast_struct(Token *pos, Ast_Scope *scope) {
AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE);
result->scope = scope;
return result;
}
CORE_Static Ast_Decl *
ast_enum(Token *pos, Ast_Expr *typespec, Ast_Scope *scope){
ast_enum(Token *pos, Ast_Expr *typespec, Ast_Scope *scope) {
AST_NEW(Decl, ENUM, pos, AST_DECL | AST_AGGREGATE);
result->scope = scope;
result->typespec = typespec;
@@ -229,7 +229,7 @@ ast_enum(Token *pos, Ast_Expr *typespec, Ast_Scope *scope){
}
CORE_Static Ast_Decl *
ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){
ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr) {
AST_NEW(Decl, VAR, pos, AST_DECL);
result->name = name;
result->typespec = typespec;
@@ -238,7 +238,7 @@ ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr){
}
CORE_Static Ast_Decl *
ast_const(Token *pos, Intern_String name, Value value){
ast_const(Token *pos, Intern_String name, Value value) {
AST_NEW(Decl, CONST, pos, AST_DECL);
result->value = value;
result->name = name;
@@ -246,7 +246,7 @@ ast_const(Token *pos, Intern_String name, Value value){
}
CORE_Static Ast_Decl *
ast_const(Token *pos, Intern_String name, Ast_Expr *expr){
ast_const(Token *pos, Intern_String name, Ast_Expr *expr) {
AST_NEW(Decl, CONST, pos, AST_DECL);
result->expr = expr;
result->name = name;
@@ -254,7 +254,7 @@ ast_const(Token *pos, Intern_String name, Ast_Expr *expr){
}
CORE_Static Ast_Decl *
ast_type(Token *pos, Intern_String name, Ast_Type *type){
ast_type(Token *pos, Intern_String name, Ast_Type *type) {
AST_NEW(Decl, TYPE, pos, AST_DECL);
result->type = pctx->type_type;
result->type_val = type;
@@ -263,7 +263,7 @@ ast_type(Token *pos, Intern_String name, Ast_Type *type){
}
CORE_Static Ast_Scope *
ast_decl_scope(Token *pos, Allocator *allocator, Ast_File *file){
ast_decl_scope(Token *pos, Allocator *allocator, Ast_File *file) {
AST_NEW(Scope, SCOPE, pos, AST_DECL);
result->file = file;
@@ -273,7 +273,7 @@ ast_decl_scope(Token *pos, Allocator *allocator, Ast_File *file){
}
CORE_Static Ast_Decl *
ast_namespace(Token *pos, Ast_Scope *module, Intern_String name){
ast_namespace(Token *pos, Ast_Scope *module, Intern_String name) {
AST_NEW(Decl, NAMESPACE, pos, AST_DECL);
result->scope = module;
result->name = name;
@@ -281,7 +281,7 @@ ast_namespace(Token *pos, Ast_Scope *module, Intern_String name){
}
CORE_Static Ast_Builtin *
ast_runtime_assert(Token *pos, Ast_Expr *expr, Intern_String message){
ast_runtime_assert(Token *pos, Ast_Expr *expr, Intern_String message) {
AST_NEW(Builtin, RUNTIME_ASSERT, pos, AST_EXPR);
result->expr = expr;
result->assert_message = message;
@@ -289,7 +289,7 @@ ast_runtime_assert(Token *pos, Ast_Expr *expr, Intern_String message){
}
CORE_Static Ast_Builtin *
ast_constant_assert(Token *pos, Ast_Expr *expr, Intern_String message){
ast_constant_assert(Token *pos, Ast_Expr *expr, Intern_String message) {
AST_NEW(Builtin, CONSTANT_ASSERT, pos, AST_EXPR);
result->expr = expr;
result->assert_message = message;
@@ -297,28 +297,28 @@ ast_constant_assert(Token *pos, Ast_Expr *expr, Intern_String message){
}
CORE_Static Ast_Builtin *
ast_sizeof(Token *pos, Ast_Expr *expr){
ast_sizeof(Token *pos, Ast_Expr *expr) {
AST_NEW(Builtin, SIZE_OF, pos, AST_EXPR);
result->expr = expr;
return result;
}
CORE_Static Ast_Builtin *
ast_len(Token *pos, Ast_Expr *expr){
ast_len(Token *pos, Ast_Expr *expr) {
AST_NEW(Builtin, LENGTH_OF, pos, AST_EXPR);
result->expr = expr;
return result;
}
CORE_Static Ast_Builtin *
ast_alignof(Token *pos, Ast_Expr *expr){
ast_alignof(Token *pos, Ast_Expr *expr) {
AST_NEW(Builtin, ALIGN_OF, pos, AST_EXPR);
result->expr = expr;
return result;
}
CORE_Static Ast_Var_Unpack *
ast_var_unpack(Token *pos, Array<Ast_Decl *> vars, Ast_Expr *expr){
ast_var_unpack(Token *pos, Array<Ast_Decl *> vars, Ast_Expr *expr) {
AST_NEW(Var_Unpack, VAR_UNPACK, pos, AST_STMT);
result->vars = vars.tight_copy(pctx->perm);
result->expr = expr;
@@ -329,7 +329,7 @@ ast_var_unpack(Token *pos, Array<Ast_Decl *> vars, Ast_Expr *expr){
// Value
//-----------------------------------------------------------------------------
CORE_Static Value
value_bool(B32 v){
value_bool(B32 v) {
Value value;
value.bool_val = v;
value.type = pctx->untyped_bool;
@@ -337,7 +337,7 @@ value_bool(B32 v){
}
CORE_Static Value
value_int(BigInt b){
value_int(BigInt b) {
Value value;
value.big_int_val = b;
value.type = pctx->untyped_int;
@@ -345,7 +345,7 @@ value_int(BigInt b){
}
CORE_Static Value
value_int(S64 s64){
value_int(S64 s64) {
Value value;
value.type = pctx->untyped_int;
bigint_init_signed(&value.big_int_val, s64);
@@ -353,7 +353,7 @@ value_int(S64 s64){
}
CORE_Static Value
value_float(F64 b){
value_float(F64 b) {
Value value;
value.f64_val = b;
value.type = pctx->untyped_float;
@@ -361,7 +361,7 @@ value_float(F64 b){
}
CORE_Static Value
value_float(BigInt a){
value_float(BigInt a) {
Value value;
value.f64_val = bigint_as_float(&a);
value.type = pctx->untyped_float;
@@ -369,19 +369,19 @@ value_float(BigInt a){
}
CORE_Static B32
is_ident(Ast *ast){
is_ident(Ast *ast) {
B32 result = ast->kind == AST_IDENT;
return result;
}
CORE_Static B32
is_binary(Ast *ast){
is_binary(Ast *ast) {
B32 result = ast->kind == AST_BINARY;
return result;
}
CORE_Static B32
is_atom(Ast *ast){
is_atom(Ast *ast) {
B32 result = is_flag_set(ast->flags, AST_ATOM);
return result;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
//apifn
// apifn
static void core_free_compiler() {
deallocate(pctx->heap, pctx->type_map.data);
deallocate(pctx->heap, pctx->interns.map.data);
@@ -8,7 +8,7 @@ static void core_free_compiler() {
arena_release(pctx->perm);
}
//apifn
// apifn
static void core_init_compiler(Core_Ctx *ctx, Allocator *allocator) {
ctx->time.init_context = os_time();
pctx = ctx;
@@ -17,7 +17,7 @@ static void core_init_compiler(Core_Ctx *ctx, Allocator *allocator) {
ctx->emit_line_directives = true;
ctx->debugger_break_on_compiler_error = true;
ctx->color_codes_enabled = true;
ctx->same_scope_token = { SAME_SCOPE };
ctx->same_scope_token = {SAME_SCOPE};
arena_init(&ctx->perm_push_only, "Perm Push Only"_s);
arena_init(&ctx->scratch_, "Scratch"_s);
@@ -34,72 +34,72 @@ static void core_init_compiler(Core_Ctx *ctx, Allocator *allocator) {
ctx->interns = intern_table_make(ctx->perm, ctx->heap, 2048);
/*#import meta
for i in meta.keywords:
/*#import meta
for i in meta.keywords:
print(f'pctx->keyword_{i.lower()} = pctx->intern("{i}"_s);')
print(f'pctx->interns.first_keyword = pctx->keyword_{meta.keywords[0].lower()}.str;')
print(f'pctx->interns.last_keyword = pctx->keyword_{meta.keywords[-1].lower()}.str;')
print(f'pctx->interns.first_keyword = pctx->keyword_{meta.keywords[0].lower()}.str;')
print(f'pctx->interns.last_keyword = pctx->keyword_{meta.keywords[-1].lower()}.str;')
for i in meta.interns:
for i in meta.interns:
print(f'pctx->intern_{i.lower()} = pctx->intern("{i}"_s);')
index = 0
for i in meta.token_simple_expr:
index = 0
for i in meta.token_simple_expr:
if i[1] != "SPECIAL":
print(f'pctx->op_info_table[{index}] = {{pctx->intern("{i[1]}"_s), "{i[0].upper()}"_s, TK_{i[0]}, {int(i[2]&meta.BINARY_EXPR>0)}, {int(i[2]&meta.UNARY_EXPR>0)}}};')
index += 1
*/
pctx->keyword_struct = pctx->intern("struct"_s);
pctx->keyword_union = pctx->intern("union"_s);
pctx->keyword_true = pctx->intern("true"_s);
pctx->keyword_default = pctx->intern("default"_s);
pctx->keyword_break = pctx->intern("break"_s);
pctx->keyword_false = pctx->intern("false"_s);
pctx->keyword_return = pctx->intern("return"_s);
pctx->keyword_switch = pctx->intern("switch"_s);
pctx->keyword_assert = pctx->intern("Assert"_s);
pctx->keyword_if = pctx->intern("if"_s);
pctx->keyword_elif = pctx->intern("elif"_s);
pctx->keyword_pass = pctx->intern("pass"_s);
pctx->keyword_else = pctx->intern("else"_s);
pctx->keyword_for = pctx->intern("for"_s);
pctx->keyword_enum = pctx->intern("enum"_s);
pctx->interns.first_keyword = pctx->keyword_struct.str;
pctx->interns.last_keyword = pctx->keyword_enum.str;
pctx->intern_typeof = pctx->intern("TypeOf"_s);
pctx->intern_sizeof = pctx->intern("SizeOf"_s);
pctx->intern_len = pctx->intern("Len"_s);
pctx->intern_alignof = pctx->intern("AlignOf"_s);
pctx->intern_foreign = pctx->intern("foreign"_s);
pctx->intern_strict = pctx->intern("strict"_s);
pctx->intern_void = pctx->intern("void"_s);
pctx->intern_flag = pctx->intern("flag"_s);
pctx->intern_it = pctx->intern("it"_s);
pctx->intern_load = pctx->intern("load"_s);
pctx->intern_import = pctx->intern("import"_s);
pctx->intern_link = pctx->intern("link"_s);
pctx->op_info_table[0] = {pctx->intern("*"_s), "MUL"_s, TK_Mul, 1, 0};
pctx->op_info_table[1] = {pctx->intern("/"_s), "DIV"_s, TK_Div, 1, 0};
pctx->op_info_table[2] = {pctx->intern("%"_s), "MOD"_s, TK_Mod, 1, 0};
pctx->op_info_table[3] = {pctx->intern("<<"_s), "LEFTSHIFT"_s, TK_LeftShift, 1, 0};
pctx->op_info_table[4] = {pctx->intern(">>"_s), "RIGHTSHIFT"_s, TK_RightShift, 1, 0};
pctx->op_info_table[5] = {pctx->intern("+"_s), "ADD"_s, TK_Add, 1, 1};
pctx->op_info_table[6] = {pctx->intern("-"_s), "SUB"_s, TK_Sub, 1, 1};
pctx->op_info_table[7] = {pctx->intern("=="_s), "EQUALS"_s, TK_Equals, 1, 0};
pctx->op_info_table[8] = {pctx->intern("<="_s), "LESSERTHENOREQUAL"_s, TK_LesserThenOrEqual, 1, 0};
pctx->op_info_table[9] = {pctx->intern(">="_s), "GREATERTHENOREQUAL"_s, TK_GreaterThenOrEqual, 1, 0};
pctx->op_info_table[10] = {pctx->intern("<"_s), "LESSERTHEN"_s, TK_LesserThen, 1, 0};
pctx->op_info_table[11] = {pctx->intern(">"_s), "GREATERTHEN"_s, TK_GreaterThen, 1, 0};
pctx->op_info_table[12] = {pctx->intern("!="_s), "NOTEQUALS"_s, TK_NotEquals, 1, 0};
pctx->op_info_table[13] = {pctx->intern("&"_s), "BITAND"_s, TK_BitAnd, 1, 0};
pctx->op_info_table[14] = {pctx->intern("|"_s), "BITOR"_s, TK_BitOr, 1, 0};
pctx->op_info_table[15] = {pctx->intern("^"_s), "BITXOR"_s, TK_BitXor, 1, 0};
pctx->op_info_table[16] = {pctx->intern("&&"_s), "AND"_s, TK_And, 1, 0};
pctx->op_info_table[17] = {pctx->intern("||"_s), "OR"_s, TK_Or, 1, 0};
pctx->op_info_table[18] = {pctx->intern("~"_s), "NEG"_s, TK_Neg, 0, 1};
pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1};
/*END*/
*/
pctx->keyword_struct = pctx->intern("struct"_s);
pctx->keyword_union = pctx->intern("union"_s);
pctx->keyword_true = pctx->intern("true"_s);
pctx->keyword_default = pctx->intern("default"_s);
pctx->keyword_break = pctx->intern("break"_s);
pctx->keyword_false = pctx->intern("false"_s);
pctx->keyword_return = pctx->intern("return"_s);
pctx->keyword_switch = pctx->intern("switch"_s);
pctx->keyword_assert = pctx->intern("Assert"_s);
pctx->keyword_if = pctx->intern("if"_s);
pctx->keyword_elif = pctx->intern("elif"_s);
pctx->keyword_pass = pctx->intern("pass"_s);
pctx->keyword_else = pctx->intern("else"_s);
pctx->keyword_for = pctx->intern("for"_s);
pctx->keyword_enum = pctx->intern("enum"_s);
pctx->interns.first_keyword = pctx->keyword_struct.str;
pctx->interns.last_keyword = pctx->keyword_enum.str;
pctx->intern_typeof = pctx->intern("TypeOf"_s);
pctx->intern_sizeof = pctx->intern("SizeOf"_s);
pctx->intern_len = pctx->intern("Len"_s);
pctx->intern_alignof = pctx->intern("AlignOf"_s);
pctx->intern_foreign = pctx->intern("foreign"_s);
pctx->intern_strict = pctx->intern("strict"_s);
pctx->intern_void = pctx->intern("void"_s);
pctx->intern_flag = pctx->intern("flag"_s);
pctx->intern_it = pctx->intern("it"_s);
pctx->intern_load = pctx->intern("load"_s);
pctx->intern_import = pctx->intern("import"_s);
pctx->intern_link = pctx->intern("link"_s);
pctx->op_info_table[0] = {pctx->intern("*"_s), "MUL"_s, TK_Mul, 1, 0};
pctx->op_info_table[1] = {pctx->intern("/"_s), "DIV"_s, TK_Div, 1, 0};
pctx->op_info_table[2] = {pctx->intern("%"_s), "MOD"_s, TK_Mod, 1, 0};
pctx->op_info_table[3] = {pctx->intern("<<"_s), "LEFTSHIFT"_s, TK_LeftShift, 1, 0};
pctx->op_info_table[4] = {pctx->intern(">>"_s), "RIGHTSHIFT"_s, TK_RightShift, 1, 0};
pctx->op_info_table[5] = {pctx->intern("+"_s), "ADD"_s, TK_Add, 1, 1};
pctx->op_info_table[6] = {pctx->intern("-"_s), "SUB"_s, TK_Sub, 1, 1};
pctx->op_info_table[7] = {pctx->intern("=="_s), "EQUALS"_s, TK_Equals, 1, 0};
pctx->op_info_table[8] = {pctx->intern("<="_s), "LESSERTHENOREQUAL"_s, TK_LesserThenOrEqual, 1, 0};
pctx->op_info_table[9] = {pctx->intern(">="_s), "GREATERTHENOREQUAL"_s, TK_GreaterThenOrEqual, 1, 0};
pctx->op_info_table[10] = {pctx->intern("<"_s), "LESSERTHEN"_s, TK_LesserThen, 1, 0};
pctx->op_info_table[11] = {pctx->intern(">"_s), "GREATERTHEN"_s, TK_GreaterThen, 1, 0};
pctx->op_info_table[12] = {pctx->intern("!="_s), "NOTEQUALS"_s, TK_NotEquals, 1, 0};
pctx->op_info_table[13] = {pctx->intern("&"_s), "BITAND"_s, TK_BitAnd, 1, 0};
pctx->op_info_table[14] = {pctx->intern("|"_s), "BITOR"_s, TK_BitOr, 1, 0};
pctx->op_info_table[15] = {pctx->intern("^"_s), "BITXOR"_s, TK_BitXor, 1, 0};
pctx->op_info_table[16] = {pctx->intern("&&"_s), "AND"_s, TK_And, 1, 0};
pctx->op_info_table[17] = {pctx->intern("||"_s), "OR"_s, TK_Or, 1, 0};
pctx->op_info_table[18] = {pctx->intern("~"_s), "NEG"_s, TK_Neg, 0, 1};
pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1};
/*END*/
// Init types
pctx->type__void = {TYPE_VOID};
@@ -132,7 +132,7 @@ pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1};
pctx->type_int = &pctx->type__int;
pctx->type_void = &pctx->type__void;
//pctx->type_any; // Needs to be inited at runtime
// pctx->type_any; // Needs to be inited at runtime
pctx->type_type = &pctx->type__type;
// pctx->type_string = &pctx->type__string;
@@ -141,12 +141,12 @@ pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1};
pctx->type_f32 = &pctx->type__f32;
pctx->type_f64 = &pctx->type__f64;
pctx->type_s8 = &pctx->type__s8 ;
pctx->type_s8 = &pctx->type__s8;
pctx->type_s16 = &pctx->type__s16;
pctx->type_s32 = &pctx->type__s32;
pctx->type_s64 = &pctx->type__s64;
pctx->type_u8 = &pctx->type__u8 ;
pctx->type_u8 = &pctx->type__u8;
pctx->type_u16 = &pctx->type__u16;
pctx->type_u32 = &pctx->type__u32;
pctx->type_u64 = &pctx->type__u64;
@@ -168,7 +168,7 @@ pctx->op_info_table[19] = {pctx->intern("!"_s), "NOT"_s, TK_Not, 0, 1};
ctx->time.init_context = os_time() - ctx->time.init_context;
}
//apifn
// apifn
CORE_Static void
core_bootstrap_compiler(Allocator *allocator) {
Core_Ctx *ctx = allocate_struct(allocator, Core_Ctx);
@@ -178,7 +178,7 @@ core_bootstrap_compiler(Allocator *allocator) {
CORE_Static void
insert_builtin_type_into_scope(Ast_Scope *p, String name, Ast_Type *type) {
Intern_String string = pctx->intern(name);
Ast_Decl * decl = ast_type(0, string, type);
Ast_Decl *decl = ast_type(0, string, type);
type->type_id = pctx->type_ids++;
decl->parent_scope = p;
decl->state = DECL_RESOLVED;
@@ -392,7 +392,7 @@ GetTypeInfo :: (type: Type): *Type_Info
{
Ast_Scope *scope = ast_decl_scope(&pctx->null_token, pctx->perm, get(&module->all_loaded_files, 0));
Ast_Decl * decl = ast_namespace(&pctx->null_token, scope, pctx->intern("Const"_s));
Ast_Decl *decl = ast_namespace(&pctx->null_token, scope, pctx->intern("Const"_s));
decl->state = DECL_RESOLVED;
Value v1 = {};

View File

@@ -1,5 +1,5 @@
struct Lex_Stream{
struct Lex_Stream {
String stream;
S64 iter;
@@ -10,7 +10,7 @@ struct Lex_Stream{
Array<Token *> indent_stack; // @scratch_allocated
};
struct Core_Ctx{
struct Core_Ctx {
Allocator *heap;
Arena perm_push_only;
@@ -87,48 +87,48 @@ struct Core_Ctx{
Token same_scope_token;
Token null_token;
/*#import meta
for i in meta.keywords: print(f'Intern_String keyword_{i.lower()};')
for i in meta.interns: print(f'Intern_String intern_{i.lower()};')
*/
Intern_String keyword_struct;
Intern_String keyword_union;
Intern_String keyword_true;
Intern_String keyword_default;
Intern_String keyword_break;
Intern_String keyword_false;
Intern_String keyword_return;
Intern_String keyword_switch;
Intern_String keyword_assert;
Intern_String keyword_if;
Intern_String keyword_elif;
Intern_String keyword_pass;
Intern_String keyword_else;
Intern_String keyword_for;
Intern_String keyword_enum;
Intern_String intern_typeof;
Intern_String intern_sizeof;
Intern_String intern_len;
Intern_String intern_alignof;
Intern_String intern_foreign;
Intern_String intern_strict;
Intern_String intern_void;
Intern_String intern_flag;
Intern_String intern_it;
Intern_String intern_load;
Intern_String intern_import;
Intern_String intern_link;
/*END*/
/*#import meta
for i in meta.keywords: print(f'Intern_String keyword_{i.lower()};')
for i in meta.interns: print(f'Intern_String intern_{i.lower()};')
*/
Intern_String keyword_struct;
Intern_String keyword_union;
Intern_String keyword_true;
Intern_String keyword_default;
Intern_String keyword_break;
Intern_String keyword_false;
Intern_String keyword_return;
Intern_String keyword_switch;
Intern_String keyword_assert;
Intern_String keyword_if;
Intern_String keyword_elif;
Intern_String keyword_pass;
Intern_String keyword_else;
Intern_String keyword_for;
Intern_String keyword_enum;
Intern_String intern_typeof;
Intern_String intern_sizeof;
Intern_String intern_len;
Intern_String intern_alignof;
Intern_String intern_foreign;
Intern_String intern_strict;
Intern_String intern_void;
Intern_String intern_flag;
Intern_String intern_it;
Intern_String intern_load;
Intern_String intern_import;
Intern_String intern_link;
/*END*/
/*#import meta
size = 0
for i in meta.token_simple_expr:
size = 0
for i in meta.token_simple_expr:
if i[1] != "SPECIAL":
size += 1
print(f"Ast_Operator_Info op_info_table[{size}];")
*/
Ast_Operator_Info op_info_table[20];
/*END*/
print(f"Ast_Operator_Info op_info_table[{size}];")
*/
Ast_Operator_Info op_info_table[20];
/*END*/
Ast_Type type__void;
Ast_Type type__string;
@@ -138,12 +138,12 @@ Ast_Operator_Info op_info_table[20];
Ast_Type type__f32;
Ast_Type type__f64;
Ast_Type type__s8 ;
Ast_Type type__s8;
Ast_Type type__s16;
Ast_Type type__s32;
Ast_Type type__s64;
Ast_Type type__u8 ;
Ast_Type type__u8;
Ast_Type type__u16;
Ast_Type type__u32;
Ast_Type type__u64;
@@ -171,12 +171,12 @@ Ast_Operator_Info op_info_table[20];
Ast_Type *type_f32 = &type__f32;
Ast_Type *type_f64 = &type__f64;
Ast_Type *type_s8 = &type__s8 ;
Ast_Type *type_s8 = &type__s8;
Ast_Type *type_s16 = &type__s16;
Ast_Type *type_s32 = &type__s32;
Ast_Type *type_s64 = &type__s64;
Ast_Type *type_u8 = &type__u8 ;
Ast_Type *type_u8 = &type__u8;
Ast_Type *type_u16 = &type__u16;
Ast_Type *type_u32 = &type__u32;
Ast_Type *type_u64 = &type__u64;
@@ -186,7 +186,7 @@ Ast_Operator_Info op_info_table[20];
Ast_Type *untyped_int = &type__untyped_int;
Ast_Type *untyped_float = &type__untyped_float;
Intern_String intern(String string){
Intern_String intern(String string) {
assert(string.len > 0);
return intern_string(&interns, string);
}

View File

@@ -8,11 +8,11 @@
#include "os.h"
#if OS_WINDOWS
#include "os_windows.cpp"
#include "os_windows.cpp"
#elif OS_LINUX
#include "os_linux.cpp"
#include "os_linux.cpp"
#else
#error Couldnt figure out OS using macros
#error Couldnt figure out OS using macros
#endif
#include "core_compiler_interface.hpp"

View File

@@ -26,8 +26,8 @@ print(" return 0;\n}")
*/
CORE_Static Ast_Operator_Info *
get_operator_info(Token_Kind op){
switch(op){
get_operator_info(Token_Kind op) {
switch (op) {
case TK_Mul: return pctx->op_info_table + 0;
case TK_Div: return pctx->op_info_table + 1;
case TK_Mod: return pctx->op_info_table + 2;
@@ -48,33 +48,35 @@ get_operator_info(Token_Kind op){
case TK_Or: return pctx->op_info_table + 17;
case TK_Neg: return pctx->op_info_table + 18;
case TK_Not: return pctx->op_info_table + 19;
default: {}
default: {
}
}
return 0;
}
CORE_Static Ast_Operator_Info *
get_operator_info(Intern_String op){
if(0){}
else if(pctx->op_info_table[0].op.str == op.str) return pctx->op_info_table + 0;
else if(pctx->op_info_table[1].op.str == op.str) return pctx->op_info_table + 1;
else if(pctx->op_info_table[2].op.str == op.str) return pctx->op_info_table + 2;
else if(pctx->op_info_table[3].op.str == op.str) return pctx->op_info_table + 3;
else if(pctx->op_info_table[4].op.str == op.str) return pctx->op_info_table + 4;
else if(pctx->op_info_table[5].op.str == op.str) return pctx->op_info_table + 5;
else if(pctx->op_info_table[6].op.str == op.str) return pctx->op_info_table + 6;
else if(pctx->op_info_table[7].op.str == op.str) return pctx->op_info_table + 7;
else if(pctx->op_info_table[8].op.str == op.str) return pctx->op_info_table + 8;
else if(pctx->op_info_table[9].op.str == op.str) return pctx->op_info_table + 9;
else if(pctx->op_info_table[10].op.str == op.str) return pctx->op_info_table + 10;
else if(pctx->op_info_table[11].op.str == op.str) return pctx->op_info_table + 11;
else if(pctx->op_info_table[12].op.str == op.str) return pctx->op_info_table + 12;
else if(pctx->op_info_table[13].op.str == op.str) return pctx->op_info_table + 13;
else if(pctx->op_info_table[14].op.str == op.str) return pctx->op_info_table + 14;
else if(pctx->op_info_table[15].op.str == op.str) return pctx->op_info_table + 15;
else if(pctx->op_info_table[16].op.str == op.str) return pctx->op_info_table + 16;
else if(pctx->op_info_table[17].op.str == op.str) return pctx->op_info_table + 17;
else if(pctx->op_info_table[18].op.str == op.str) return pctx->op_info_table + 18;
else if(pctx->op_info_table[19].op.str == op.str) return pctx->op_info_table + 19;
get_operator_info(Intern_String op) {
if (0) {
}
else if (pctx->op_info_table[0].op.str == op.str) return pctx->op_info_table + 0;
else if (pctx->op_info_table[1].op.str == op.str) return pctx->op_info_table + 1;
else if (pctx->op_info_table[2].op.str == op.str) return pctx->op_info_table + 2;
else if (pctx->op_info_table[3].op.str == op.str) return pctx->op_info_table + 3;
else if (pctx->op_info_table[4].op.str == op.str) return pctx->op_info_table + 4;
else if (pctx->op_info_table[5].op.str == op.str) return pctx->op_info_table + 5;
else if (pctx->op_info_table[6].op.str == op.str) return pctx->op_info_table + 6;
else if (pctx->op_info_table[7].op.str == op.str) return pctx->op_info_table + 7;
else if (pctx->op_info_table[8].op.str == op.str) return pctx->op_info_table + 8;
else if (pctx->op_info_table[9].op.str == op.str) return pctx->op_info_table + 9;
else if (pctx->op_info_table[10].op.str == op.str) return pctx->op_info_table + 10;
else if (pctx->op_info_table[11].op.str == op.str) return pctx->op_info_table + 11;
else if (pctx->op_info_table[12].op.str == op.str) return pctx->op_info_table + 12;
else if (pctx->op_info_table[13].op.str == op.str) return pctx->op_info_table + 13;
else if (pctx->op_info_table[14].op.str == op.str) return pctx->op_info_table + 14;
else if (pctx->op_info_table[15].op.str == op.str) return pctx->op_info_table + 15;
else if (pctx->op_info_table[16].op.str == op.str) return pctx->op_info_table + 16;
else if (pctx->op_info_table[17].op.str == op.str) return pctx->op_info_table + 17;
else if (pctx->op_info_table[18].op.str == op.str) return pctx->op_info_table + 18;
else if (pctx->op_info_table[19].op.str == op.str) return pctx->op_info_table + 19;
return 0;
}
/*END*/

View File

@@ -5,6 +5,3 @@ global S64 bigint_allocation_count;
const uintptr_t pointer_size = sizeof(uintptr_t);
const uintptr_t pointer_align = __alignof(uintptr_t);

View File

@@ -1,85 +1,85 @@
force_inline B32 token_is_assign(Token_Kind token){return token >= TK_FirstAssign && token <= TK_LastAssign;}
force_inline B32 token_is_assign(Token *token){return token_is_assign(token->kind);}
force_inline B32 token_is_compare(Token_Kind token){return token >= TK_FirstCompare && token <= TK_LastCompare;}
force_inline B32 token_is_compare(Token *token){return token_is_compare(token->kind);}
force_inline B32 token_is_assign(Token_Kind token) { return token >= TK_FirstAssign && token <= TK_LastAssign; }
force_inline B32 token_is_assign(Token *token) { return token_is_assign(token->kind); }
force_inline B32 token_is_compare(Token_Kind token) { return token >= TK_FirstCompare && token <= TK_LastCompare; }
force_inline B32 token_is_compare(Token *token) { return token_is_compare(token->kind); }
CORE_Static U8
lexc(Lex_Stream *s){
lexc(Lex_Stream *s) {
return s->stream.str[s->iter];
}
CORE_Static U8
lexci(Lex_Stream *s, S32 i){
return s->stream.str[s->iter+i];
lexci(Lex_Stream *s, S32 i) {
return s->stream.str[s->iter + i];
}
CORE_Static U8 *
lexcp(Lex_Stream *s){
lexcp(Lex_Stream *s) {
return s->stream.str + s->iter;
}
CORE_Static B32
lex_is_whitespace(U8 c){
lex_is_whitespace(U8 c) {
B32 result = c == ' ' || c == '\r';
return result;
}
CORE_Static B32
lex_is_alphabetic(U8 c){
lex_is_alphabetic(U8 c) {
B32 result = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
return result;
}
CORE_Static B32
lex_is_numeric(U8 c){
lex_is_numeric(U8 c) {
B32 result = c >= '0' && c <= '9';
return result;
}
CORE_Static B32
lex_is_numeric_base16(U8 c){
lex_is_numeric_base16(U8 c) {
B32 result = (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') ||
(c >= 'a' && c <= 'f');
return result;
}
CORE_Static B32
lex_is_alphanumeric(U8 c){
lex_is_alphanumeric(U8 c) {
B32 result = lex_is_numeric(c) || lex_is_alphabetic(c);
return result;
}
CORE_Static void
lex_set_len(Lex_Stream *s, Token *token){
lex_set_len(Lex_Stream *s, Token *token) {
assert(lexcp(s) >= token->str);
token->len = lexcp(s) - token->str;
}
CORE_Static void
lex_set_keywords(Core_Ctx *lexer, Array<String> keywords){
lex_set_keywords(Core_Ctx *lexer, Array<String> keywords) {
Intern_String keyword = {};
For(keywords){
For(keywords) {
keyword = intern_string(&lexer->interns, it);
if(&it == keywords.begin())
if (&it == keywords.begin())
lexer->interns.first_keyword = keyword.str;
}
lexer->interns.last_keyword = keyword.str;
}
CORE_Static B32
lex_is_keyword(Intern_Table *lexer, Intern_String keyword){
lex_is_keyword(Intern_Table *lexer, Intern_String keyword) {
B32 result = keyword.str >= lexer->first_keyword && keyword.str <= lexer->last_keyword;
return result;
}
CORE_Static void
token_error(Token *t, String error_val){
token_error(Token *t, String error_val) {
t->kind = TK_Error;
t->error_val = error_val;
}
CORE_Static void
lex_parse_u64(Core_Ctx *lexer, Token *t, S64 base){
lex_parse_u64(Core_Ctx *lexer, Token *t, S64 base) {
Scratch_Scope _scope(lexer->scratch);
Set_BigInt_Arena(lexer->scratch);
@@ -88,10 +88,10 @@ lex_parse_u64(Core_Ctx *lexer, Token *t, S64 base){
BigInt base_mul = bigint_u64(base);
BigInt result = bigint_u64(0);
for(S64 i = t->len - 1; i >= 0; --i){
for (S64 i = t->len - 1; i >= 0; --i) {
U64 value = t->str[i];
if(t->str[i] >= 'a') value = value - 'a' + 10;
else if(t->str[i] >= 'A') value = value - 'A' + 10;
if (t->str[i] >= 'a') value = value - 'a' + 10;
else if (t->str[i] >= 'A') value = value - 'A' + 10;
else value -= '0';
BigInt val = bigint_u64(value);
@@ -104,7 +104,7 @@ lex_parse_u64(Core_Ctx *lexer, Token *t, S64 base){
}
CORE_Static void
lex_parse_f64(Token *t){
lex_parse_f64(Token *t) {
t->kind = TK_Float;
char buffer[128];
S64 len = clamp_top((int)t->len, 126);
@@ -114,42 +114,42 @@ lex_parse_f64(Token *t){
}
CORE_Static void
lex_advance(Lex_Stream *s){
if(s->iter >= s->stream.len){
lex_advance(Lex_Stream *s) {
if (s->iter >= s->stream.len) {
return;
}
else if(lexc(s) == '\n'){
else if (lexc(s) == '\n') {
s->iter++;
s->line++;
s->line_begin = lexcp(s);
}
else{
else {
s->iter++;
}
}
CORE_Static void
lex_parse_string(Lex_Stream *s, Token *t, U8 c){
for(;;){
if(lexc(s) == '\\') lex_advance(s);
else if(lexc(s) == c) break;
else if(lexc(s) == 0){
lex_parse_string(Lex_Stream *s, Token *t, U8 c) {
for (;;) {
if (lexc(s) == '\\') lex_advance(s);
else if (lexc(s) == c) break;
else if (lexc(s) == 0) {
token_error(t, "Unterminated string, reached end of file"_s);
break;
}
lex_advance(s);
}
if(t->kind != TK_Error){
if (t->kind != TK_Error) {
lex_advance(s);
lex_set_len(s,t);
lex_set_len(s, t);
}
}
CORE_Static void
lex_parse_ident(Intern_Table *table, Lex_Stream *s, Token *t){
while(lex_is_alphanumeric(lexc(s)) || lexc(s) == '_')
lex_parse_ident(Intern_Table *table, Lex_Stream *s, Token *t) {
while (lex_is_alphanumeric(lexc(s)) || lexc(s) == '_')
lex_advance(s);
lex_set_len(s,t);
lex_set_len(s, t);
}
#define CASE2(op, OpName, Assign) \
@@ -157,7 +157,8 @@ lex_parse_ident(Intern_Table *table, Lex_Stream *s, Token *t){
if (lexc(s) == '=') { \
lex_advance(s); \
t.kind = Assign; \
} else { \
} \
else { \
t.kind = OpName; \
} \
break
@@ -166,16 +167,18 @@ lex_parse_ident(Intern_Table *table, Lex_Stream *s, Token *t){
if (lexc(s) == '=') { \
lex_advance(s); \
t.kind = Assign; \
} else if (lexc(s) == op) { \
} \
else if (lexc(s) == op) { \
lex_advance(s); \
t.kind = Incr; \
} else { \
} \
else { \
t.kind = OpName; \
} \
break
CORE_Static Token
token_make(Core_Ctx *lexer, U8 *str, Intern_String file, int line, U8 *line_begin){
token_make(Core_Ctx *lexer, U8 *str, Intern_String file, int line, U8 *line_begin) {
Token t = {};
t.str = str;
t.file = file;
@@ -186,12 +189,12 @@ token_make(Core_Ctx *lexer, U8 *str, Intern_String file, int line, U8 *line_begi
}
CORE_Static Token
token_make(Core_Ctx *lexer){
token_make(Core_Ctx *lexer) {
return token_make(lexer, lexcp(&lexer->stream), lexer->stream.file, lexer->stream.line, lexer->stream.line_begin);
}
CORE_Static Token *
lex_last_indent_token(Lex_Stream *s){
lex_last_indent_token(Lex_Stream *s) {
if (s->indent_stack.len > 0) {
return *s->indent_stack.last();
}
@@ -199,7 +202,7 @@ lex_last_indent_token(Lex_Stream *s){
}
CORE_Static B32
lex_is_scope(Token *t){
lex_is_scope(Token *t) {
B32 result = t->kind == OPEN_SCOPE || t->kind == CLOSE_SCOPE || t->kind == SAME_SCOPE;
return result;
}
@@ -214,21 +217,21 @@ lex_add_token(Core_Ctx *ctx, Token *token) {
}
CORE_Static void
lex_unwind_indent_stack(Core_Ctx *ctx, Token *t, Lex_Stream *s){
for(S64 i = s->indent_stack.len-1; i >= 0; i-=1){
lex_unwind_indent_stack(Core_Ctx *ctx, Token *t, Lex_Stream *s) {
for (S64 i = s->indent_stack.len - 1; i >= 0; i -= 1) {
auto it = s->indent_stack.data[i];
assert(lex_is_scope(it));
if(it->indent == t->indent){
if (it->indent == t->indent) {
t->kind = SAME_SCOPE;
lex_add_token(ctx, t);
break;
}
else if(it->indent < t->indent){
else if (it->indent < t->indent) {
token_error(t, "Bad indentation"_s);
lex_add_token(ctx, t);
break;
}
else{
else {
s->indent_stack.pop();
t->kind = CLOSE_SCOPE;
lex_add_token(ctx, t);
@@ -237,13 +240,13 @@ lex_unwind_indent_stack(Core_Ctx *ctx, Token *t, Lex_Stream *s){
}
CORE_Static void
lex__stream(Core_Ctx *lexer){
lex__stream(Core_Ctx *lexer) {
Intern_Table *table = &lexer->interns;
Lex_Stream *s = &lexer->stream;
B32 beginning = true;
for(;;){
if(lexc(s) == 0 || s->iter >= s->stream.len){
for (;;) {
if (lexc(s) == 0 || s->iter >= s->stream.len) {
end_of_stream:
Token t = token_make(lexer);
lex_unwind_indent_stack(lexer, &t, s);
@@ -269,29 +272,36 @@ lex__stream(Core_Ctx *lexer){
// from a block parsing, second to allow continuation of surrounding scope
Token t = token_make(lexer);
B32 should_emit = beginning;
for(;;){
switch(lexc(s)){
case 0 : goto end_of_stream; break;
case '\t': case ' ': lex_advance(s); t.indent++; break;
for (;;) {
switch (lexc(s)) {
case 0: goto end_of_stream; break;
case '\t':
case ' ':
lex_advance(s);
t.indent++;
break;
case '\r': lex_advance(s); break;
case '/': {
if(lexci(s,1) == '/'){
lex_advance(s); lex_advance(s);
if (lexci(s, 1) == '/') {
lex_advance(s);
lex_advance(s);
t.kind = TK_Comment;
for(;;){
if(lexc(s) == '\n' || lexc(s) == 0) break;
for (;;) {
if (lexc(s) == '\n' || lexc(s) == 0) break;
lex_advance(s);
}
}
else if(lexci(s,1) == '*'){
lex_advance(s); lex_advance(s);
else if (lexci(s, 1) == '*') {
lex_advance(s);
lex_advance(s);
t.kind = TK_Comment;
for(;;){
if(lexc(s) == '*' && lexci(s,1) == '/'){
lex_advance(s); lex_advance(s);
for (;;) {
if (lexc(s) == '*' && lexci(s, 1) == '/') {
lex_advance(s);
lex_advance(s);
break;
}
else if(lexc(s) == 0){
else if (lexc(s) == 0) {
token_error(&t, "Unterminated block comment"_s);
break;
}
@@ -306,40 +316,41 @@ lex__stream(Core_Ctx *lexer){
// first of all we can check for consistency and second of
// all because we would know by how much to indent
// @todo: after detecting indentation 2 spaces would become 1 indent value
case ';' : {
case ';': {
Token semi = token_make(lexer);
Token *last = lex_last_indent_token(s);
semi.indent = last->indent;
lex_advance(s);
if(lexc(s) == ';'){
if (lexc(s) == ';') {
lex_advance(s);
semi.kind = OPEN_SCOPE;
semi.indent = last->indent + 2; // @todo: proper detection of indentation
lex_add_token(lexer, &semi);
s->indent_stack.add(lexer->tokens.last());
} else{
}
else {
semi.kind = SAME_SCOPE;
lex_add_token(lexer, &semi);
}
} break;
case '\n':{
case '\n': {
lex_advance(s);
should_emit = true;
t = token_make(lexer);
} break;
default:{
if(s->inside_brace_paren) should_emit = false;
if(should_emit){
default: {
if (s->inside_brace_paren) should_emit = false;
if (should_emit) {
Token *last = lex_last_indent_token(s);
if(t.indent > last->indent){
if (t.indent > last->indent) {
t.kind = OPEN_SCOPE;
lex_add_token(lexer, &t);
s->indent_stack.add(lexer->tokens.last());
}
else if(t.indent < last->indent){
else if (t.indent < last->indent) {
lex_unwind_indent_stack(lexer, &t, s);
}
else {
@@ -351,25 +362,46 @@ lex__stream(Core_Ctx *lexer){
goto indent_loop_break;
}
}
} indent_loop_break:
}
indent_loop_break:
beginning = false;
// @note: handle the indented token
t = token_make(lexer);
lex_advance(s);
switch(*t.str){
case 0 : goto end_of_stream; break;
switch (*t.str) {
case 0: goto end_of_stream; break;
case '@': t.kind = TK_At; break;
case '(': s->inside_brace_paren++; t.kind = TK_OpenParen; break;
case ')': s->inside_brace_paren--; t.kind = TK_CloseParen; break;
case '{': s->inside_brace_paren++; t.kind = TK_OpenBrace; break;
case '}': s->inside_brace_paren--; t.kind = TK_CloseBrace; break;
case '[': s->inside_brace_paren++; t.kind = TK_OpenBracket; break;
case ']': s->inside_brace_paren--; t.kind = TK_CloseBracket; break;
case '(':
s->inside_brace_paren++;
t.kind = TK_OpenParen;
break;
case ')':
s->inside_brace_paren--;
t.kind = TK_CloseParen;
break;
case '{':
s->inside_brace_paren++;
t.kind = TK_OpenBrace;
break;
case '}':
s->inside_brace_paren--;
t.kind = TK_CloseBrace;
break;
case '[':
s->inside_brace_paren++;
t.kind = TK_OpenBracket;
break;
case ']':
s->inside_brace_paren--;
t.kind = TK_CloseBracket;
break;
case ',': t.kind = TK_Comma; break;
case '~': t.kind = TK_Neg; break;
case '?': t.kind = TK_Question; break;
case '^': t.kind = TK_BitXor; break;
case '^':
t.kind = TK_BitXor;
break;
CASE2('!', TK_Not, TK_NotEquals);
CASE2('=', TK_Assign, TK_Equals);
CASE2('*', TK_Mul, TK_MulAssign);
@@ -384,13 +416,13 @@ lex__stream(Core_Ctx *lexer){
t.str += 1;
t.len -= 1;
t.intern_val = intern_string(table, t.string);
if(t.len == 0) token_error(&t, "Macro token without content"_s);
}break;
if (t.len == 0) token_error(&t, "Macro token without content"_s);
} break;
case '.': {
if(lexc(s) == '.'){
if (lexc(s) == '.') {
lex_advance(s);
if(lexci(s,1) == '.') {
if (lexci(s, 1) == '.') {
lex_advance(s);
t.kind = TK_ThreeDots;
}
@@ -399,21 +431,21 @@ lex__stream(Core_Ctx *lexer){
else t.kind = TK_Dot;
} break;
case '\'':{
case '\'': {
assert(s->stream.len >= s->iter);
UTF32_Result decode = utf8_to_utf32(lexcp(s), s->stream.len - s->iter);
if(!decode.error){
for(S32 i = 0; i < decode.advance; i++) lex_advance(s);
if (!decode.error) {
for (S32 i = 0; i < decode.advance; i++) lex_advance(s);
t.unicode = decode.out_str;
t.kind = TK_UnicodeLit;
if(lexc(s) == '\''){
if (lexc(s) == '\'') {
lex_advance(s);
}
else{
else {
token_error(&t, "Unclosed unicode literal"_s);
}
}
else{
else {
token_error(&t, "Invalid UTF8 sequence in unicode literal"_s);
}
} break;
@@ -463,7 +495,7 @@ lex__stream(Core_Ctx *lexer){
lex_advance(s);
t.kind = TK_DoubleColon;
}
else if(lexc(s) == '='){
else if (lexc(s) == '=') {
lex_advance(s);
t.kind = TK_ColonAssign;
}
@@ -472,7 +504,7 @@ lex__stream(Core_Ctx *lexer){
}
} break;
case '-':{
case '-': {
if (lexc(s) == '=') {
lex_advance(s);
t.kind = TK_SubAssign;
@@ -492,8 +524,8 @@ lex__stream(Core_Ctx *lexer){
case '"': {
t.kind = TK_StringLit;
lex_parse_string(s,&t,'"');
if(t.kind != TK_Error){
lex_parse_string(s, &t, '"');
if (t.kind != TK_Error) {
t.str += 1;
t.len -= 2;
}
@@ -501,7 +533,7 @@ lex__stream(Core_Ctx *lexer){
} break;
case '/': {
if(lexc(s) == '='){
if (lexc(s) == '=') {
t.kind = TK_DivAssign;
lex_advance(s);
}
@@ -510,43 +542,49 @@ lex__stream(Core_Ctx *lexer){
}
} break;
case '0':{
if(lexc(s) == 'x'){
case '0': {
if (lexc(s) == 'x') {
lex_advance(s);
while(lex_is_numeric_base16(lexc(s)))
while (lex_is_numeric_base16(lexc(s)))
lex_advance(s);
lex_set_len(s, &t);
t.str += 2;
t.len -= 2;
if(t.len == 0)
if (t.len == 0)
token_error(&t, "Hex constant doesn't have value"_s);
else
lex_parse_u64(lexer, &t, 16);
break;
}
else if(lexc(s) == 'b'){
else if (lexc(s) == 'b') {
lex_advance(s);
while(lexc(s) == '0' || lexc(s) == '1')
while (lexc(s) == '0' || lexc(s) == '1')
lex_advance(s);
lex_set_len(s, &t);
t.str += 2;
t.len -= 2;
if(t.len == 0)
if (t.len == 0)
token_error(&t, "Hex constant doesn't have value"_s);
else
lex_parse_u64(lexer, &t, 2);
break;
}
}
case '1':case '2':case '3':case '4':
case '5':case '6':case '7':case '8':case '9':{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
B32 found_dot = false;
for(;;){
if(lex_is_numeric(lexc(s)))
for (;;) {
if (lex_is_numeric(lexc(s)))
;
else if(lexc(s) == '.'){
if(found_dot){
else if (lexc(s) == '.') {
if (found_dot) {
token_error(&t, "Multiple '.' in float literal"_s);
goto end_of_switch;
}
@@ -557,24 +595,68 @@ lex__stream(Core_Ctx *lexer){
lex_advance(s);
}
lex_set_len(s, &t);
if(found_dot) lex_parse_f64(&t);
if (found_dot) lex_parse_f64(&t);
else lex_parse_u64(lexer, &t, 10);
} break;
case 'A':case 'a':case 'M':case 'm':case 'B':
case 'b':case 'N':case 'n':case 'C':case 'c':case 'O':
case 'o':case 'D':case 'd':case 'P':case 'p':case 'E':
case 'e':case 'Q':case 'q':case 'F':case 'f':case 'R':
case 'r':case 'G':case 'g':case 'S':case 's':case 'H':
case 'h':case 'T':case 't':case 'I':case 'i':case 'U':
case 'u':case 'J':case 'j':case 'V':case 'v':case 'K':
case 'k':case 'W':case 'w':case 'L':case 'X':case 'l':
case 'x':case 'Z':case 'z':case 'Y':case 'y':case '_': {
case 'A':
case 'a':
case 'M':
case 'm':
case 'B':
case 'b':
case 'N':
case 'n':
case 'C':
case 'c':
case 'O':
case 'o':
case 'D':
case 'd':
case 'P':
case 'p':
case 'E':
case 'e':
case 'Q':
case 'q':
case 'F':
case 'f':
case 'R':
case 'r':
case 'G':
case 'g':
case 'S':
case 's':
case 'H':
case 'h':
case 'T':
case 't':
case 'I':
case 'i':
case 'U':
case 'u':
case 'J':
case 'j':
case 'V':
case 'v':
case 'K':
case 'k':
case 'W':
case 'w':
case 'L':
case 'X':
case 'l':
case 'x':
case 'Z':
case 'z':
case 'Y':
case 'y':
case '_': {
t.kind = TK_Identifier;
lex_parse_ident(table, s, &t);
t.intern_val = intern_string(table, t.string);
if(lex_is_keyword(table, t.intern_val)){
if (lex_is_keyword(table, t.intern_val)) {
t.kind = TK_Keyword;
}
} break;
@@ -582,10 +664,11 @@ lex__stream(Core_Ctx *lexer){
default: {
token_error(&t, "Unknown token"_s);
}
}end_of_switch:
}
end_of_switch:
if(t.len==0)
lex_set_len(s,&t);
if (t.len == 0)
lex_set_len(s, &t);
lex_add_token(lexer, &t);
}
@@ -594,7 +677,7 @@ lex__stream(Core_Ctx *lexer){
}
CORE_Static void
lex_restream(Core_Ctx *lexer, String istream, String file){
lex_restream(Core_Ctx *lexer, String istream, String file) {
lexer->stream = {};
lexer->stream.stream = istream;
lexer->stream.line_begin = istream.str;
@@ -611,80 +694,81 @@ lex_restream(Core_Ctx *lexer, String istream, String file){
//-----------------------------------------------------------------------------
CORE_Static const char *
name(Token_Kind kind){
switch(kind){
name(Token_Kind kind) {
switch (kind) {
case TK_End: return "End of stream";
/*#
import meta
for i in meta.token_kinds:
/*#
import meta
for i in meta.token_kinds:
if i[1] != "SPECIAL":
print("case TK_" + i[0] + f": return \"{i[1]}\";")
*/
case TK_Mul: return "*";
case TK_Div: return "/";
case TK_Mod: return "%";
case TK_LeftShift: return "<<";
case TK_RightShift: return ">>";
case TK_Add: return "+";
case TK_Sub: return "-";
case TK_Equals: return "==";
case TK_LesserThenOrEqual: return "<=";
case TK_GreaterThenOrEqual: return ">=";
case TK_LesserThen: return "<";
case TK_GreaterThen: return ">";
case TK_NotEquals: return "!=";
case TK_BitAnd: return "&";
case TK_BitOr: return "|";
case TK_BitXor: return "^";
case TK_And: return "&&";
case TK_Or: return "||";
case TK_Neg: return "~";
case TK_Not: return "!";
case TK_Decrement: return "--";
case TK_Increment: return "++";
case TK_PostDecrement: return "--";
case TK_PostIncrement: return "++";
case TK_Assign: return "=";
case TK_ColonAssign: return ":=";
case TK_DivAssign: return "/=";
case TK_MulAssign: return "*=";
case TK_ModAssign: return "%=";
case TK_SubAssign: return "-=";
case TK_AddAssign: return "+=";
case TK_AndAssign: return "&=";
case TK_OrAssign: return "|=";
case TK_XorAssign: return "^=";
case TK_LeftShiftAssign: return "<<=";
case TK_RightShiftAssign: return ">>=";
case TK_OpenParen: return "(";
case TK_CloseParen: return ")";
case TK_OpenBrace: return "{";
case TK_CloseBrace: return "}";
case TK_OpenBracket: return "[";
case TK_CloseBracket: return "]";
case TK_Comma: return ",";
case TK_Pound: return "#";
case TK_Question: return "?";
case TK_ThreeDots: return "...";
case TK_Semicolon: return ";";
case TK_Dot: return ".";
case TK_TwoDots: return "..";
case TK_NewLine: return "[NewLine]";
case TK_Colon: return ":";
case TK_DoubleColon: return "::";
case TK_At: return "@";
case TK_Arrow: return "->";
case TK_ExprSizeof: return "[SizeOf]";
case TK_DocComment: return "[///]";
case TK_Comment: return "//";
case TK_Identifier: return "[Ident]";
case TK_UnicodeLit: return "[Unicode]";
case TK_StringLit: return "[String]";
case TK_Error: return "[Error]";
case TK_Float: return "[Float]";
case TK_Integer: return "[Int]";
case TK_Keyword: return "[Keyword]";
/*END*/
*/
case TK_Mul: return "*";
case TK_Div: return "/";
case TK_Mod: return "%";
case TK_LeftShift: return "<<";
case TK_RightShift: return ">>";
case TK_Add: return "+";
case TK_Sub: return "-";
case TK_Equals: return "==";
case TK_LesserThenOrEqual: return "<=";
case TK_GreaterThenOrEqual: return ">=";
case TK_LesserThen: return "<";
case TK_GreaterThen: return ">";
case TK_NotEquals: return "!=";
case TK_BitAnd: return "&";
case TK_BitOr: return "|";
case TK_BitXor: return "^";
case TK_And: return "&&";
case TK_Or: return "||";
case TK_Neg: return "~";
case TK_Not: return "!";
case TK_Decrement: return "--";
case TK_Increment: return "++";
case TK_PostDecrement: return "--";
case TK_PostIncrement: return "++";
case TK_Assign: return "=";
case TK_ColonAssign: return ":=";
case TK_DivAssign: return "/=";
case TK_MulAssign: return "*=";
case TK_ModAssign: return "%=";
case TK_SubAssign: return "-=";
case TK_AddAssign: return "+=";
case TK_AndAssign: return "&=";
case TK_OrAssign: return "|=";
case TK_XorAssign: return "^=";
case TK_LeftShiftAssign: return "<<=";
case TK_RightShiftAssign: return ">>=";
case TK_OpenParen: return "(";
case TK_CloseParen: return ")";
case TK_OpenBrace: return "{";
case TK_CloseBrace: return "}";
case TK_OpenBracket: return "[";
case TK_CloseBracket: return "]";
case TK_Comma: return ",";
case TK_Pound: return "#";
case TK_Question: return "?";
case TK_ThreeDots: return "...";
case TK_Semicolon: return ";";
case TK_Dot: return ".";
case TK_TwoDots: return "..";
case TK_NewLine: return "[NewLine]";
case TK_Colon: return ":";
case TK_DoubleColon: return "::";
case TK_At: return "@";
case TK_Arrow: return "->";
case TK_ExprSizeof: return "[SizeOf]";
case TK_DocComment: return "[///]";
case TK_Comment: return "//";
case TK_Identifier: return "[Ident]";
case TK_UnicodeLit: return "[Unicode]";
case TK_StringLit: return "[String]";
case TK_Error: return "[Error]";
case TK_Float: return "[Float]";
case TK_Integer: return "[Int]";
case TK_Keyword:
return "[Keyword]";
/*END*/
case CLOSE_SCOPE: return "Close_Scope";
case OPEN_SCOPE: return "Open_Scope";
case SAME_SCOPE: return "Same_Scope";

View File

@@ -156,19 +156,19 @@ static void compile_file(Allocator *allocator, String filename, U32 compile_flag
}
}
int main(int argument_count, char **arguments){
int main(int argument_count, char **arguments) {
Arena arena = {};
Arena scratch = {};
arena_init(&arena, "Pernament arena"_s);
arena_init(&scratch, "Pernament scratch arena"_s);
Array<String> args = {&scratch};
for(int i = 1; i < argument_count; i+=1){
for (int i = 1; i < argument_count; i += 1) {
String arg = string_from_cstring(arguments[i]);
args.add(arg);
}
if(!args.len){
if (!args.len) {
printf("Please specify a file to compile!");
return 0;
}
@@ -187,11 +187,11 @@ int main(int argument_count, char **arguments){
if (SetConsoleMode(hOut, dwMode)) {
enable_color_codes = true;
}
else{
else {
printf("Failed to enable colored terminal output C\n");
}
}
else{
else {
printf("Failed to enable colored terminal output B\n");
}
}
@@ -203,18 +203,18 @@ int main(int argument_count, char **arguments){
#endif
(void)enable_color_codes;
For(args){
For(args) {
if(it == "-testing"_s){
if (it == "-testing"_s) {
Scratch_Scope _scope(&scratch);
Array<OS_File_Info> examples = os_list_dir(&scratch, &scratch, "examples"_s);
Array<OS_File_Info> tests = os_list_dir(&scratch, &scratch, "tests"_s);
For(examples){
if(it.is_directory) continue;
For(examples) {
if (it.is_directory) continue;
compile_file(&arena, it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING);
}
For(tests){
if(it.is_directory) continue;
For(tests) {
if (it.is_directory) continue;
compile_file(&arena, it.absolute_path, COMPILE_AND_RUN | COMPILE_TESTING);
}
}
@@ -223,7 +223,6 @@ int main(int argument_count, char **arguments){
String program_name = string_from_cstring(arguments[1]);
compile_file(&arena, program_name, COMPILE_PRINT_STATS);
}
}
printf("End of program\n");
return 0;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,29 +1,29 @@
struct Operand{
/*#import meta
meta.inline_value_fields()
*/
union {
struct Operand {
/*#import meta
meta.inline_value_fields()
*/
union {
Value value;
struct {
Ast_Type *type;
Ast_Decl *resolved_decl;
union {
Ast_Decl *resolved_decl;
union {
bool bool_val;
double f64_val;
Intern_String intern_val;
BigInt big_int_val;
Ast_Type *type_val;
};
};
};
/*END*/
};
};
/*END*/
// is_const is used to rewrite the tree and bound
// something to the const name at the end
U8 is_const : 1;
U8 is_lvalue : 1;
U8 pound_strict: 1;
U8 pound_strict : 1;
};
struct Scope_Search {
@@ -44,7 +44,7 @@ enum {
};
typedef U32 Resolve_Flag;
enum{
enum {
AST_CANT_BE_NULL = bit_flag(0),
AST_CAN_BE_NULL = bit_flag(1),
RESOLVE_TYPESPEC_COMPLETE = bit_flag(2),
@@ -52,7 +52,7 @@ enum{
};
typedef U32 Search_Flag;
enum{
enum {
SEARCH_ONLY_CURRENT_SCOPE = bit_flag(1),
RESOLVE_NAME_MAKE_SURE_OPERATOR_OVERLOAD_IS_NOT_EVER_CALLED = bit_flag(2),
};
@@ -60,5 +60,9 @@ enum{
CORE_Static Operand resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context, Ast_Scope *field_access_scope);
CORE_Static void resolve_decl(Ast_Decl *ast);
CORE_Static Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name, Search_Flag search_flags = 0);
#define CASE(kind,type) case AST_##kind: { Ast_##type *node = (Ast_##type *)ast;
#define BREAK() } break
#define CASE(kind, type) \
case AST_##kind: { \
Ast_##type *node = (Ast_##type *)ast;
#define BREAK() \
} \
break

View File

@@ -1,7 +1,7 @@
CORE_Static const char *
get_name_of_type(Ast_Type *type){
switch(type->kind){
get_name_of_type(Ast_Type *type) {
switch (type->kind) {
case TYPE_VOID: return "void";
case TYPE_BOOL: return "Bool";
case TYPE_CHAR: return "char";
@@ -17,7 +17,8 @@ get_name_of_type(Ast_Type *type){
case TYPE_U32: return "U32";
case TYPE_U64: return "U64";
case TYPE_TUPLE: return "Tuple";
case TYPE_TYPE: return "Type";
case TYPE_TYPE:
return "Type";
invalid_default_case;
}
return "<unknown_type>";
@@ -26,29 +27,29 @@ get_name_of_type(Ast_Type *type){
//-----------------------------------------------------------------------------
// Type constructors and utillities
//-----------------------------------------------------------------------------
force_inline B32 is_any(Ast_Type *a){return a == pctx->type_any;}
force_inline B32 is_struct(Ast_Type *a){return a->kind == TYPE_STRUCT;}
force_inline B32 is_lambda(Ast_Type *a){return a->kind == TYPE_LAMBDA;}
force_inline B32 is_array(Ast_Type *a){return a->kind == TYPE_ARRAY;}
force_inline B32 is_slice(Ast_Type *a){return a->kind == TYPE_SLICE;}
force_inline B32 is_tuple(Ast_Type *a){return a->kind == TYPE_TUPLE;}
force_inline B32 is_enum(Ast_Type *a){return a->kind == TYPE_ENUM;}
force_inline B32 is_pointer(Ast_Type *a){return a->kind == TYPE_POINTER;}
force_inline B32 is_void(Ast_Type *a){return a->kind == TYPE_VOID;}
force_inline B32 is_void_pointer(Ast_Type *a){return a == pctx->type_pointer_to_void;}
force_inline B32 is_string(Ast_Type *a){return a == pctx->type_string || a->kind == TYPE_UNTYPED_STRING || a == pctx->type_pointer_to_char;}
force_inline B32 is_untyped_int(Ast_Type *a){return a->kind == TYPE_UNTYPED_INT;}
force_inline B32 is_typed_int(Ast_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8);}
force_inline B32 is_int(Ast_Type *a){return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8) || a->kind == TYPE_UNTYPED_INT;}
force_inline B32 is_signed_int(Ast_Type *a){return !a->is_unsigned;}
force_inline B32 is_unsigned_int(Ast_Type *a){return a->is_unsigned;}
force_inline B32 is_float(Ast_Type *a){return a->kind == TYPE_F32 || a->kind == TYPE_F64 || a->kind == TYPE_UNTYPED_FLOAT;}
force_inline B32 is_f32(Ast_Type *a){return a->kind == TYPE_F32;}
force_inline B32 is_f64(Ast_Type *a){return a->kind == TYPE_F64;}
force_inline B32 is_bool(Ast_Type *a){return a->kind == TYPE_BOOL || a->kind == TYPE_UNTYPED_BOOL;}
force_inline B32 is_untyped(Ast_Type *a){return a->kind >= TYPE_UNTYPED_FIRST && a->kind <= TYPE_UNTYPED_LAST;}
force_inline B32 is_typed(Ast_Type *a){return !is_untyped(a);}
force_inline B32 is_numeric(Ast_Type *type){
force_inline B32 is_any(Ast_Type *a) { return a == pctx->type_any; }
force_inline B32 is_struct(Ast_Type *a) { return a->kind == TYPE_STRUCT; }
force_inline B32 is_lambda(Ast_Type *a) { return a->kind == TYPE_LAMBDA; }
force_inline B32 is_array(Ast_Type *a) { return a->kind == TYPE_ARRAY; }
force_inline B32 is_slice(Ast_Type *a) { return a->kind == TYPE_SLICE; }
force_inline B32 is_tuple(Ast_Type *a) { return a->kind == TYPE_TUPLE; }
force_inline B32 is_enum(Ast_Type *a) { return a->kind == TYPE_ENUM; }
force_inline B32 is_pointer(Ast_Type *a) { return a->kind == TYPE_POINTER; }
force_inline B32 is_void(Ast_Type *a) { return a->kind == TYPE_VOID; }
force_inline B32 is_void_pointer(Ast_Type *a) { return a == pctx->type_pointer_to_void; }
force_inline B32 is_string(Ast_Type *a) { return a == pctx->type_string || a->kind == TYPE_UNTYPED_STRING || a == pctx->type_pointer_to_char; }
force_inline B32 is_untyped_int(Ast_Type *a) { return a->kind == TYPE_UNTYPED_INT; }
force_inline B32 is_typed_int(Ast_Type *a) { return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8); }
force_inline B32 is_int(Ast_Type *a) { return (a->kind >= TYPE_S64 && a->kind <= TYPE_U8) || a->kind == TYPE_UNTYPED_INT; }
force_inline B32 is_signed_int(Ast_Type *a) { return !a->is_unsigned; }
force_inline B32 is_unsigned_int(Ast_Type *a) { return a->is_unsigned; }
force_inline B32 is_float(Ast_Type *a) { return a->kind == TYPE_F32 || a->kind == TYPE_F64 || a->kind == TYPE_UNTYPED_FLOAT; }
force_inline B32 is_f32(Ast_Type *a) { return a->kind == TYPE_F32; }
force_inline B32 is_f64(Ast_Type *a) { return a->kind == TYPE_F64; }
force_inline B32 is_bool(Ast_Type *a) { return a->kind == TYPE_BOOL || a->kind == TYPE_UNTYPED_BOOL; }
force_inline B32 is_untyped(Ast_Type *a) { return a->kind >= TYPE_UNTYPED_FIRST && a->kind <= TYPE_UNTYPED_LAST; }
force_inline B32 is_typed(Ast_Type *a) { return !is_untyped(a); }
force_inline B32 is_numeric(Ast_Type *type) {
return (type->kind >= TYPE_UNTYPED_FIRST_NUMERIC && type->kind <= TYPE_UNTYPED_LAST_NUMERIC) ||
(type->kind >= TYPE_FIRST_NUMERIC && type->kind <= TYPE_LAST_NUMERIC);
}
@@ -57,7 +58,7 @@ force_inline B32 is_numeric(Ast_Type *type){
// Hash consed types
//-----------------------------------------------------------------------------
CORE_Static Ast_Type *
type_new(Allocator *allocator, Ast_Type_Kind kind, size_t size, size_t align){
type_new(Allocator *allocator, Ast_Type_Kind kind, size_t size, size_t align) {
Ast_Type *result = allocate_struct(allocator, Ast_Type, true);
result->kind = kind;
result->size = size;
@@ -68,7 +69,7 @@ type_new(Allocator *allocator, Ast_Type_Kind kind, size_t size, size_t align){
}
CORE_Static Ast_Type *
type_copy(Allocator *a, Ast_Type *type){
type_copy(Allocator *a, Ast_Type *type) {
// @warning: This changes type id !!!!
Ast_Type *result = allocate_struct(a, Ast_Type);
memory_copy(result, type, sizeof(Ast_Type));
@@ -78,9 +79,9 @@ type_copy(Allocator *a, Ast_Type *type){
}
CORE_Static Ast_Type *
type_pointer(Ast_Type *base){
type_pointer(Ast_Type *base) {
Ast_Type *result = (Ast_Type *)map_get(&pctx->type_map, (void *)base);
if(!result){
if (!result) {
result = type_new(pctx->perm, TYPE_POINTER, pointer_size, pointer_align);
result->base = base;
result->is_unsigned = true;
@@ -91,17 +92,20 @@ type_pointer(Ast_Type *base){
}
CORE_Static Ast_Type *
type_slice(Ast_Type *base, Ast *ast){
type_slice(Ast_Type *base, Ast *ast) {
U64 hash_base = hash_ptr(base);
U64 hash = hash_mix(hash_base, hash_u64(ARRAY_SIZE_SLICE));
Ast_Type *result = (Ast_Type *)map_get(&pctx->type_map, hash);
if(result){
if (result) {
assert(result->kind == TYPE_SLICE);
assert(result->arr.base == base);
return result;
}
struct Slice{void *p; S64 len;};
struct Slice {
void *p;
S64 len;
};
result = type_new(pctx->perm, TYPE_SLICE, sizeof(Slice), alignof(Slice));
result->arr.base = base;
result->arr.slice_hash = hash;
@@ -111,14 +115,14 @@ type_slice(Ast_Type *base, Ast *ast){
}
CORE_Static Ast_Type *
type_try_tupling(Array<Ast_Type *> types, Ast *ast){
if(types.len == 0) return pctx->type_void;
if(types.len == 1) return types[0];
type_try_tupling(Array<Ast_Type *> types, Ast *ast) {
if (types.len == 0) return pctx->type_void;
if (types.len == 1) return types[0];
U64 hash = 13;
For(types) hash = hash_mix(hash, hash_ptr(it));
Ast_Type *result = (Ast_Type *)map_get(&pctx->type_map, hash);
if(result){
if (result) {
assert(result->kind == TYPE_TUPLE);
assert(result->agg.members.len == types.len);
assert(result->agg.members.len > 1);
@@ -128,7 +132,7 @@ type_try_tupling(Array<Ast_Type *> types, Ast *ast){
// @todo alignment, offsets
result = type_new(pctx->perm, TYPE_TUPLE, 0, pointer_align);
result->agg.members = array_make<Ast_Resolved_Member>(pctx->perm, types.len);
For(types){
For(types) {
Ast_Resolved_Member m = {};
m.type = it;
m.offset = 0; // @todo
@@ -142,18 +146,18 @@ type_try_tupling(Array<Ast_Type *> types, Ast *ast){
}
CORE_Static Ast_Type *
type_array(Ast_Type *base, S64 size){
type_array(Ast_Type *base, S64 size) {
U64 hash_base = hash_ptr(base);
U64 hash = hash_mix(hash_base, hash_u64(size));
Ast_Type *result = (Ast_Type *)map_get(&pctx->type_map, hash);
if(result){
if (result) {
assert(result->kind == TYPE_ARRAY);
assert(result->arr.size == size);
assert(result->arr.base == base);
return result;
}
result = type_new(pctx->perm, TYPE_ARRAY, size*base->size, pointer_align);
result = type_new(pctx->perm, TYPE_ARRAY, size * base->size, pointer_align);
result->arr.base = base;
result->arr.size = size;
result->arr.slice_hash = hash_mix(hash_base, hash_u64(ARRAY_SIZE_SLICE));
@@ -162,7 +166,7 @@ type_array(Ast_Type *base, S64 size){
}
inline U64
calculate_hash_for_arguments(Ast_Type *a, Ast_Type *b){
calculate_hash_for_arguments(Ast_Type *a, Ast_Type *b) {
U64 result = 13;
result = hash_mix(result, hash_ptr(a));
result = hash_mix(result, hash_ptr(b));
@@ -170,21 +174,21 @@ calculate_hash_for_arguments(Ast_Type *a, Ast_Type *b){
}
inline U64
calculate_hash_for_arguments(Ast_Type *a){
calculate_hash_for_arguments(Ast_Type *a) {
U64 result = 13;
result = hash_mix(result, hash_ptr(a));
return result;
}
CORE_Static Ast_Type *
type_lambda(Ast *ast, Array<Ast_Type *> return_vals, Array<Ast_Type *> args){
type_lambda(Ast *ast, Array<Ast_Type *> return_vals, Array<Ast_Type *> args) {
Ast_Type *ret = type_try_tupling(return_vals, ast);
U64 hash_without_ret = 13;
For(args) hash_without_ret = hash_mix(hash_without_ret, hash_ptr(it));
U64 hash = hash_mix(hash_ptr(ret), hash_without_ret);
Ast_Type *result = (Ast_Type *)map_get(&pctx->type_map, hash);
if(result){
if (result) {
assert(result->kind == TYPE_LAMBDA);
assert(result->func.args.len == args.len);
return result;
@@ -201,8 +205,8 @@ type_lambda(Ast *ast, Array<Ast_Type *> return_vals, Array<Ast_Type *> args){
}
CORE_Static Ast_Type *
type_enum(Ast_Decl *ast, Ast_Type *type){
if(!type){
type_enum(Ast_Decl *ast, Ast_Type *type) {
if (!type) {
type = pctx->type_s64;
}
@@ -213,7 +217,7 @@ type_enum(Ast_Decl *ast, Ast_Type *type){
}
CORE_Static Ast_Type *
type_incomplete(Ast *ast){
type_incomplete(Ast *ast) {
Ast_Type *result = type_new(pctx->perm, TYPE_INCOMPLETE, 0, 0);
result->ast = ast;
return result;
@@ -221,7 +225,7 @@ type_incomplete(Ast *ast){
CORE_Static void type_complete(Ast_Type *type);
CORE_Static void
type_struct_complete(Ast_Type *type, Ast_Decl *node){
type_struct_complete(Ast_Type *type, Ast_Decl *node) {
assert(node->kind == AST_STRUCT);
// @todo: compute size, alignement, offset !!!
// @note: resolve all the struct members first
@@ -231,7 +235,7 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node){
Array<Ast_Resolved_Member> members = {scratch};
type->kind = TYPE_COMPLETING;
size_t members_size = 0;
For_Named(node->scope->decls, decl){
For_Named(node->scope->decls, decl) {
resolve_decl(decl);
assert(decl->type->kind != TYPE_INCOMPLETE);
assert(is_pow2(decl->type->align));
@@ -254,14 +258,14 @@ type_struct_complete(Ast_Type *type, Ast_Decl *node){
}
CORE_Static void
type_complete(Ast_Type *type){
if(!type) {
type_complete(Ast_Type *type) {
if (!type) {
return;
}
if(type->kind == TYPE_COMPLETING){
if (type->kind == TYPE_COMPLETING) {
compiler_error(type->ast->pos, "Cyclic type dependency");
}
else if(type->kind != TYPE_INCOMPLETE){
else if (type->kind != TYPE_INCOMPLETE) {
return;
}
@@ -270,8 +274,8 @@ type_complete(Ast_Type *type){
}
CORE_Static void
typename_base(String_Builder *sb, Ast_Type *type){
switch(type->kind){
typename_base(String_Builder *sb, Ast_Type *type) {
switch (type->kind) {
case TYPE_INCOMPLETE: sb->addf("INCOMPLETE"); break;
case TYPE_COMPLETING: sb->addf("COMPLETING"); break;
case TYPE_TYPE: sb->addf("TYPE"); break;
@@ -283,7 +287,7 @@ typename_base(String_Builder *sb, Ast_Type *type){
sb->addf("(");
For(type->func.args) {
typename_base(sb, it);
if(!type->func.args.is_last(&it)) sb->addf(", ");
if (!type->func.args.is_last(&it)) sb->addf(", ");
}
sb->addf("):");
@@ -298,7 +302,7 @@ typename_base(String_Builder *sb, Ast_Type *type){
typename_base(sb, type->base);
break;
case TYPE_STRUCT:
case TYPE_ENUM:{
case TYPE_ENUM: {
// @todo direct access
auto constant = (Ast_Decl *)type->ast;
auto name = constant->name;
@@ -316,7 +320,7 @@ typename_base(String_Builder *sb, Ast_Type *type){
}
CORE_Static String
get_typename(Allocator *a, Ast_Type *type){
get_typename(Allocator *a, Ast_Type *type) {
pctx->helper_builder.addf("[");
typename_base(&pctx->helper_builder, type);
pctx->helper_builder.addf("]");
@@ -326,6 +330,6 @@ get_typename(Allocator *a, Ast_Type *type){
}
CORE_Static String
typestring(Ast_Type *type){
typestring(Ast_Type *type) {
return get_typename(pctx->stage_arena, type);
}

View File

@@ -2,12 +2,36 @@
// Resolved Types
//-----------------------------------------------------------------------------
#define CASE_SINT case TYPE_S8:case TYPE_S16:case TYPE_S32:case TYPE_S64: case TYPE_CHAR: case TYPE_INT
#define CASE_UINT case TYPE_U8:case TYPE_U16:case TYPE_U32:case TYPE_U64
#define CASE_INT case TYPE_UNTYPED_INT: CASE_SINT: CASE_UINT
#define CASE_BOOL case TYPE_UNTYPED_BOOL: case TYPE_BOOL
#define CASE_FLOAT case TYPE_UNTYPED_FLOAT: case TYPE_F32: case TYPE_F64
#define CASE_STRING case TYPE_UNTYPED_STRING: case TYPE_STRUCT: case TYPE_POINTER
#define CASE_UNTYPED case TYPE_UNTYPED_INT: case TYPE_UNTYPED_BOOL: case TYPE_UNTYPED_FLOAT: case TYPE_UNTYPED_STRING
#define CASE_SINT \
case TYPE_S8: \
case TYPE_S16: \
case TYPE_S32: \
case TYPE_S64: \
case TYPE_CHAR: \
case TYPE_INT
#define CASE_UINT \
case TYPE_U8: \
case TYPE_U16: \
case TYPE_U32: \
case TYPE_U64
#define CASE_INT \
case TYPE_UNTYPED_INT: \
CASE_SINT: \
CASE_UINT
#define CASE_BOOL \
case TYPE_UNTYPED_BOOL: \
case TYPE_BOOL
#define CASE_FLOAT \
case TYPE_UNTYPED_FLOAT: \
case TYPE_F32: \
case TYPE_F64
#define CASE_STRING \
case TYPE_UNTYPED_STRING: \
case TYPE_STRUCT: \
case TYPE_POINTER
#define CASE_UNTYPED \
case TYPE_UNTYPED_INT: \
case TYPE_UNTYPED_BOOL: \
case TYPE_UNTYPED_FLOAT: \
case TYPE_UNTYPED_STRING
#define ARRAY_SIZE_SLICE (-1)

2
os.h
View File

@@ -2,7 +2,7 @@
const U32 LIST_NO_FLAGS = 0;
const U32 LIST_RECURSE_INTO_DIRS = 1;
struct OS_File_Info{
struct OS_File_Info {
String relative_path;
String absolute_path;
B32 is_directory;

View File

@@ -11,9 +11,9 @@
#define POSIX_PAGE_SIZE 4096
CORE_Static B32
os_write_file(String filename, String filecontent){
os_write_file(String filename, String filecontent) {
FILE *f = fopen((const char *)filename.str, "w");
if(f){
if (f) {
fwrite(filecontent.str, 1, filecontent.len, f);
fclose(f);
return true;
@@ -22,10 +22,10 @@ os_write_file(String filename, String filecontent){
}
CORE_Static String
os_read_file(Alloator *a, String name){
os_read_file(Alloator *a, String name) {
String result = {0};
FILE *f = fopen((char *)name.str, "rb");
if(f){
if (f) {
fseek(f, 0, SEEK_END);
result.len = ftell(f);
fseek(f, 0, SEEK_SET);
@@ -39,7 +39,7 @@ os_read_file(Alloator *a, String name){
}
CORE_Static String
os_get_exe_dir(Allocator *a){
os_get_exe_dir(Allocator *a) {
char buffer[PATH_MAX] = {};
if (readlink("/proc/self/exe", buffer, PATH_MAX) == -1) {
log_info("Failed to retrieve the path of the executable, the method used is fetching /proc/self/exe, very likely you are using an OS that doesn't follow this convention and as such it is currently not supported");
@@ -53,7 +53,7 @@ os_get_exe_dir(Allocator *a){
}
CORE_Static String
os_get_absolute_path(Allocator *a, String path){
os_get_absolute_path(Allocator *a, String path) {
assert(path.str[path.len] == 0);
char buffer[PATH_MAX] = {};
realpath((char *)path.str, buffer);
@@ -63,7 +63,7 @@ os_get_absolute_path(Allocator *a, String path){
}
CORE_Static B32
os_does_file_exist(String path){
os_does_file_exist(String path) {
B32 result = false;
assert(path.str[path.len] == 0);
if (access((char *)path.str, F_OK) == 0) {
@@ -74,32 +74,32 @@ os_does_file_exist(String path){
}
CORE_Static String
os_get_working_dir(Allocator *allocator){
os_get_working_dir(Allocator *allocator) {
char *buffer = allocate_array(allocator, char, PATH_MAX, false);
char *result = getcwd(buffer, PATH_MAX);
return string_from_cstring(result);
}
CORE_Static Array<OS_File_Info>
os_list_dir(Scratch_Arena *scratch, Allocator *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS){
os_list_dir(Scratch_Arena *scratch, Allocator *a, String dir, U32 flags = LIST_RECURSE_INTO_DIRS) {
Scratch_Scope _scope(scratch);
Array<String> dirs_to_read = {scratch};
dirs_to_read.add(dir);
Array<OS_File_Info> result = {a};
for(auto it = dirs_to_read.begin(); it != dirs_to_read.end(); it++){
for (auto it = dirs_to_read.begin(); it != dirs_to_read.end(); it++) {
assert(it->str[it->len] == 0);
dirent *dir;
DIR *d = opendir((char *)it->str);
if(d){
if (d) {
while ((dir = readdir(d)) != NULL) {
if(dir->d_name[0] == '.'){
if(dir->d_name[1] == '.'){
if(dir->d_name[2] == 0)
if (dir->d_name[0] == '.') {
if (dir->d_name[1] == '.') {
if (dir->d_name[2] == 0)
continue;
}
if(dir->d_name[1] == 0)
if (dir->d_name[1] == 0)
continue;
}
@@ -107,10 +107,10 @@ os_list_dir(Scratch_Arena *scratch, Allocator *a, String dir, U32 flags = LIST_R
entry.relative_path = string_fmt(a, "%Q/%s", *it, dir->d_name);
entry.absolute_path = os_get_absolute_path(a, entry.relative_path);
if(dir->d_type == DT_DIR){
if (dir->d_type == DT_DIR) {
entry.is_directory = true;
if(flags & LIST_RECURSE_INTO_DIRS){
if (flags & LIST_RECURSE_INTO_DIRS) {
dirs_to_read.add(entry.absolute_path);
}
}
@@ -120,7 +120,7 @@ os_list_dir(Scratch_Arena *scratch, Allocator *a, String dir, U32 flags = LIST_R
}
closedir(d);
}
else{
else {
log_info("ERROR Failed to read dir: %Q, errno: %s", *it, strerror(errno));
}
}
@@ -157,7 +157,7 @@ os_reserve(size_t size) {
CORE_Static B32
os_commit(OS_Memory *m, size_t commit) {
B32 result = false;
U8 * pointer = os_advance_commit(m, &commit, POSIX_PAGE_SIZE);
U8 *pointer = os_advance_commit(m, &commit, POSIX_PAGE_SIZE);
if (pointer) {
int mprotect_result = mprotect(pointer, commit, PROT_READ | PROT_WRITE);
assert_message(mprotect_result == 0, "OS1 POSIX Debug Error: Failed to commit more memory using mmap");
@@ -187,7 +187,7 @@ os_get_microseconds() {
}
CORE_Static F64
os_time(){
os_time() {
F64 time = (F64)os_get_microseconds();
F64 result = time / 1000000.0; // Microseconds to seconds
return result;

View File

@@ -6,23 +6,23 @@
const size_t os_page_size = 4096;
CORE_Static OS_Memory
os_reserve(size_t size){
os_reserve(size_t size) {
OS_Memory result = {};
size_t adjusted_size = align_up(size, os_page_size);
result.data = (U8*)VirtualAlloc(0, adjusted_size, MEM_RESERVE, PAGE_READWRITE);
result.data = (U8 *)VirtualAlloc(0, adjusted_size, MEM_RESERVE, PAGE_READWRITE);
assert_message(result.data, "Failed to reserve virtual memory");
result.reserve = adjusted_size;
return result;
}
CORE_Static B32
os_commit(OS_Memory *m, size_t size){
os_commit(OS_Memory *m, size_t size) {
size_t commit = align_up(size, os_page_size);
size_t total_commit = m->commit + commit;
total_commit = clamp_top(total_commit, m->reserve);
size_t adjusted_commit = total_commit - m->commit;
if(adjusted_commit != 0){
void *result = VirtualAlloc((U8*)m->data + m->commit, adjusted_commit, MEM_COMMIT, PAGE_READWRITE);
if (adjusted_commit != 0) {
void *result = VirtualAlloc((U8 *)m->data + m->commit, adjusted_commit, MEM_COMMIT, PAGE_READWRITE);
assert_message(result, "Failed to commit more memory");
m->commit += adjusted_commit;
return true;
@@ -31,10 +31,10 @@ os_commit(OS_Memory *m, size_t size){
}
CORE_Static void
os_release(OS_Memory *m){
os_release(OS_Memory *m) {
BOOL result = VirtualFree(m->data, 0, MEM_RELEASE);
assert_message(result != 0, "Failed to release OS_Memory");
if(result){
if (result) {
m->data = 0;
m->commit = 0;
m->reserve = 0;
@@ -42,14 +42,14 @@ os_release(OS_Memory *m){
}
CORE_Static B32
os_decommit_pos(OS_Memory *m, size_t pos){
os_decommit_pos(OS_Memory *m, size_t pos) {
size_t aligned = align_down(pos, os_page_size);
size_t adjusted_pos = clamp_top(aligned, m->commit);
size_t size_to_decommit = m->commit - adjusted_pos;
if(size_to_decommit){
if (size_to_decommit) {
U8 *base_address = m->data + adjusted_pos;
BOOL result = VirtualFree(base_address, size_to_decommit, MEM_DECOMMIT);
if(result){
if (result) {
m->commit -= size_to_decommit;
return true;
}
@@ -58,20 +58,20 @@ os_decommit_pos(OS_Memory *m, size_t pos){
}
CORE_Static void
test_os_memory(){
test_os_memory() {
assert(align_down(4096, 4096) == 4096);
assert(align_down(4095, 4096) == 0);
OS_Memory memory = os_reserve(9000);
assert(memory.reserve == 4096*3 && memory.data && memory.commit == 0);
assert(memory.reserve == 4096 * 3 && memory.data && memory.commit == 0);
os_commit(&memory, 100);
assert(memory.commit == 4096);
os_commit(&memory, 100);
assert(memory.commit == 4096*2);
assert(memory.commit == 4096 * 2);
os_commit(&memory, 9000);
assert(memory.commit == 4096*3);
assert(memory.commit == 4096 * 3);
os_commit(&memory, 9000);
assert(memory.commit == 4096*3);
assert(memory.commit == 4096 * 3);
os_decommit_pos(&memory, 4096);
assert(memory.commit == 4096);
@@ -105,9 +105,9 @@ api F64 os_time() {
// Filesystem
//-----------------------------------------------------------------------------
CORE_Static B32
os_write_file(String filename, String filecontent){
os_write_file(String filename, String filecontent) {
FILE *f = fopen((const char *)filename.str, "w");
if(f){
if (f) {
fwrite(filecontent.str, 1, filecontent.len, f);
fclose(f);
return true;
@@ -116,10 +116,10 @@ os_write_file(String filename, String filecontent){
}
CORE_Static String
os_read_file(Allocator *a, String name){
os_read_file(Allocator *a, String name) {
String result = {0};
FILE *f = fopen((char *)name.str, "rb");
if(f){
if (f) {
fseek(f, 0, SEEK_END);
result.len = ftell(f);
fseek(f, 0, SEEK_SET);
@@ -133,7 +133,7 @@ os_read_file(Allocator *a, String name){
}
CORE_Static String
os_get_working_dir(Allocator *a){
os_get_working_dir(Allocator *a) {
wchar_t buffer[2048];
DWORD written = GetCurrentDirectoryW(2048, buffer);
assert(written != 0);
@@ -144,7 +144,7 @@ os_get_working_dir(Allocator *a){
}
CORE_Static String
os_get_exe_dir(Allocator *a){
os_get_exe_dir(Allocator *a) {
wchar_t buffer[2048];
DWORD written = GetModuleFileNameW(0, buffer, 2048);
assert(written != 0);
@@ -153,20 +153,20 @@ os_get_exe_dir(Allocator *a){
string_path_normalize(result);
result = string_chop_last_slash(result);
if(string16.len > result.len) result.str[result.len] = 0;
if (string16.len > result.len) result.str[result.len] = 0;
string_path_normalize(result);
return result;
}
CORE_Static String
os_get_absolute_path(Allocator *a, String path){
os_get_absolute_path(Allocator *a, String path) {
char buff[2048];
Arena scratch = arena_from_buffer(buff, 2048);
String16 path16 = string8_to_string16(&scratch, path);
wchar_t *buffer = allocate_array(&scratch, wchar_t, 512);
DWORD written = GetFullPathNameW((wchar_t *)path16.str, 512, buffer, 0);
if(written == 0) return {};
if (written == 0) return {};
String16 absolute16 = string16_from_widechar(buffer);
String absolute = string16_to_string8(a, absolute16);
@@ -175,7 +175,7 @@ os_get_absolute_path(Allocator *a, String path){
}
CORE_Static B32
os_does_file_exist(String path){
os_does_file_exist(String path) {
char buff[2048];
Arena scratch = arena_from_buffer(buff, buff_cap(buff));
String16 path16 = string8_to_string16(&scratch, path);
@@ -185,32 +185,32 @@ os_does_file_exist(String path){
}
CORE_Static Array<OS_File_Info>
os_list_dir(Arena *scratch, Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){
os_list_dir(Arena *scratch, Allocator *a, String dir, U32 flags = LIST_NO_FLAGS) {
Scratch_Scope _scope(scratch);
Array<String> dirs_to_read = {scratch};
dirs_to_read.add(dir);
Array<OS_File_Info> result = {a};
for(auto it = dirs_to_read.begin(); it != dirs_to_read.end(); it++){
for (auto it = dirs_to_read.begin(); it != dirs_to_read.end(); it++) {
String modified_path = string_fmt(scratch, "%Q\\*", it);
String16 path16 = string8_to_string16(scratch, modified_path);
WIN32_FIND_DATAW ffd;
HANDLE handle = FindFirstFileW((wchar_t *)path16.str, &ffd);
if(handle == INVALID_HANDLE_VALUE) continue;
if (handle == INVALID_HANDLE_VALUE) continue;
do{
do {
//
// Skip '.' and '..'
//
if(ffd.cFileName[0] == '.'){
if(ffd.cFileName[1] == '.'){
if(ffd.cFileName[2] == 0)
if (ffd.cFileName[0] == '.') {
if (ffd.cFileName[1] == '.') {
if (ffd.cFileName[2] == 0)
continue;
}
if(ffd.cFileName[1] == 0)
if (ffd.cFileName[1] == 0)
continue;
}
@@ -220,21 +220,20 @@ os_list_dir(Arena *scratch, Allocator *a, String dir, U32 flags = LIST_NO_FLAGS)
String full_file_path = string_fmt(a, "%Q/%Q", dir, filename);
OS_File_Info listing = {full_file_path, os_get_absolute_path(a, full_file_path)};
if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
listing.is_directory = true;
if(flags & LIST_RECURSE_INTO_DIRS){
if (flags & LIST_RECURSE_INTO_DIRS) {
dirs_to_read.add(full_file_path);
}
}
result.add(listing);
}while(FindNextFileW(handle, &ffd) != 0);
} while (FindNextFileW(handle, &ffd) != 0);
DWORD error = GetLastError();
if(error != ERROR_NO_MORE_FILES){
if (error != ERROR_NO_MORE_FILES) {
// Not sure what to do here hmmm
}
FindClose(handle);

File diff suppressed because it is too large Load Diff