diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common.cc | 75 | ||||
| -rw-r--r-- | src/memory.cc | 22 | ||||
| -rw-r--r-- | src/source.cc | 8 | ||||
| -rw-r--r-- | src/token.cc | 14 | ||||
| -rw-r--r-- | src/voidc.cc | 9 |
5 files changed, 102 insertions, 26 deletions
diff --git a/src/common.cc b/src/common.cc index ef790b2..141b079 100644 --- a/src/common.cc +++ b/src/common.cc @@ -1,8 +1,10 @@ #ifndef COMMON_CC #define COMMON_CC +#include <cstdarg> #include <cstdint> #include <cstdio> +#include <cstdlib> /* typedefs for common types */ @@ -19,9 +21,36 @@ typedef int64_t i64; typedef uintptr_t usize; typedef intptr_t isize; +/* intrinsics */ +#define likely(cond) __builtin_expect(!!(cond), 1) +#define unlikely(cond) __builtin_expect(!!(cond), 0) + +/* error handling sort of */ +[[noreturn]] void panic_impl(const char* file, i32 line, const char* fmt, ...) { + fprintf(stderr, "PANIC at %s:%d: ", file, line); + + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + + fputc('\n', stderr); + fflush(stderr); + abort(); +} + +#define panic(fmt, ...) panic_impl(__FILE__, __LINE__, fmt, __VA_ARGS__) +#define spanic(msg) panic("%s", msg) + +#undef assert +#define assert(cond) \ + do { \ + if (unlikely(!(cond))) panic("assertion failed: %s", #cond); \ + } while (0) + /* slice and string handling */ -template<typename T> +template <typename T> struct Slice { T* ptr; usize length; @@ -30,21 +59,53 @@ struct Slice { Slice(T* ptr, usize length) : ptr(ptr), length(length) {} T* operator[](usize index) { - return ptr + index; + assert(index < this->length); + return this->ptr + index; } - inline usize size() const { - return this->length * sizeof(T); + const T* operator[](usize index) const { + assert(index < this->size()); + return this->ptr + index; } + + inline usize size() const { return this->length * sizeof(T); } }; -template<typename T> +template <typename T> bool slice_write(const Slice<T>* slice, FILE* stream) { usize rc = fwrite(slice->ptr, sizeof(T), slice->length, stream); - if(rc == 0 || slice->size() > rc) return false; + if (rc == 0 || slice->size() > rc) return false; return true; -} +} typedef Slice<const u8> String; +/* linked list */ + +struct Link { + Link* next; + Link* prev; + + Link(Link* next, Link* prev) : next(next), prev(prev) {} +}; + +struct List { + Link head; + Link tail; +}; + +static inline void link_after(Link* prev, Link* nlink) { + assert(prev != nullptr); + assert(nlink != nullptr); + + Link* next = prev->next; + + nlink->prev = prev; + nlink->next = next; + prev->next = nlink; + + if(next == nullptr) return; + next->prev = nlink; +} + #endif diff --git a/src/memory.cc b/src/memory.cc index 518b761..2d83413 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -1,10 +1,12 @@ #ifndef MEMORY_CC #define MEMORY_CC -#include "common.cc" -#include <cstring> #include <mimalloc.h> +#include <cstring> + +#include "common.cc" + typedef u8* (*Allocator_Allocate)(u8* self, usize length, usize align); typedef void (*Allocator_Deallocate)(u8* self, u8* ptr); @@ -33,14 +35,15 @@ static inline void heap_deallocate(u8* self, u8* ptr) { } Allocator* heap_allocator() { - thread_local Allocator HEAP_ALLOCATOR = {nullptr, heap_allocate, heap_deallocate}; + thread_local Allocator HEAP_ALLOCATOR = {nullptr, heap_allocate, + heap_deallocate}; return &HEAP_ALLOCATOR; } -template<typename T> -bool slice_copy(Allocator* allocator, const Slice<T> *source, Slice<T> *out) { +template <typename T> +bool slice_copy(Allocator* allocator, const Slice<T>* source, Slice<T>* out) { T* new_ptr = allocate(allocator, source->size(), alignof(T)); - if(new_ptr == nullptr) return false; + if (new_ptr == nullptr) return false; memcpy((u8*)new_ptr, (u8*)source->ptr, source->length); @@ -49,9 +52,9 @@ bool slice_copy(Allocator* allocator, const Slice<T> *source, Slice<T> *out) { return true; } -template<typename T> -void slice_deallocate(Allocator* allocator, Slice<T> *slice) { - if(slice->ptr == nullptr) return; +template <typename T> +void slice_deallocate(Allocator* allocator, Slice<T>* slice) { + if (slice->ptr == nullptr) return; deallocate(allocator, (u8*)slice->ptr); @@ -60,4 +63,3 @@ void slice_deallocate(Allocator* allocator, Slice<T> *slice) { } #endif - diff --git a/src/source.cc b/src/source.cc index df3a90e..9234ea9 100644 --- a/src/source.cc +++ b/src/source.cc @@ -1,8 +1,12 @@ #ifndef SOURCE_CC #define SOURCE_CC -struct Source { - +#include "common.cc" +#include "memory.cc" + +struct Buffer { + String content; + String file; }; #endif diff --git a/src/token.cc b/src/token.cc index f5cf1de..4a218ef 100644 --- a/src/token.cc +++ b/src/token.cc @@ -4,14 +4,22 @@ #include "common.cc" #define TOKEN_KINDS_NOLEX \ - X(Eof) \ - X(Invalid_Char) \ + X(Eof) \ + X(Invalid_Char) \ X(Invalid_Literal) +#define TOKEN_KINDS_SLEX X(Hash, '#') + +#define TOKEN_KIND(name) Token_Kind_##name + enum Token_Kind { -#define X(name) Token_Kind_##name, +#define X(name) TOKEN_KIND(name), TOKEN_KINDS_NOLEX #undef X + +#define X(name, _) TOKEN_KIND(name), + TOKEN_KINDS_SLEX +#undef X }; struct Token { diff --git a/src/voidc.cc b/src/voidc.cc index e69b8a6..8110184 100644 --- a/src/voidc.cc +++ b/src/voidc.cc @@ -1,7 +1,8 @@ -#include "memory.cc" -#include "common.cc" #include <cstring> +#include "common.cc" +#include "memory.cc" + static const char* SOURCE = R"( #include <stdlib.h> @@ -10,13 +11,13 @@ int main() { } )"; -static const String SOURCE_STRING = { (u8*)SOURCE, strlen(SOURCE) }; +static const String SOURCE_STRING = {(u8*)SOURCE, strlen(SOURCE)}; int main() { String source; slice_copy(heap_allocator(), &SOURCE_STRING, &source); slice_write(&source, stdout); - + slice_deallocate(heap_allocator(), &source); } |
