summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-03-01 22:03:55 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-03-01 22:03:55 +0100
commitffa09db5138dea853c910a0307fe5063511abb9f (patch)
treef3bfc548c126fb8280fc44262d265d44eb0fb3c4 /src
parentc638003e8aa684bae60e8b3736c6d9db8571e727 (diff)
fixing type errors working on strings
Diffstat (limited to 'src')
-rw-r--r--src/common.cc30
-rw-r--r--src/memory.cc63
-rw-r--r--src/source.cc8
-rw-r--r--src/token.cc8
-rw-r--r--src/voidc.cc19
5 files changed, 105 insertions, 23 deletions
diff --git a/src/common.cc b/src/common.cc
index aa734b3..ef790b2 100644
--- a/src/common.cc
+++ b/src/common.cc
@@ -2,6 +2,7 @@
#define COMMON_CC
#include <cstdint>
+#include <cstdio>
/* typedefs for common types */
@@ -25,32 +26,25 @@ struct Slice {
T* ptr;
usize length;
+ Slice() : ptr(nullptr), length(0) {}
Slice(T* ptr, usize length) : ptr(ptr), length(length) {}
T* operator[](usize index) {
return ptr + index;
}
-};
-
-typedef Slice<u8> String;
-
-/* allocator handling */
-typedef u8* (*Allocator_Allocate)(u8* self, usize length, usize align);
-typedef void (*Allocator_Deallocate)(u8* self, u8* ptr);
-
-struct Allocator {
- u8* self;
- Allocator_Allocate allocate;
- Allocator_Deallocate deallocate;
+ inline usize size() const {
+ return this->length * sizeof(T);
+ }
};
-u8* allocate(Allocator* allocator, usize size, usize align) {
- return allocator->allocate(allocator->self, size, align);
-}
+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;
+ return true;
+}
-void deallocate(Allocator* allocator, u8* ptr) {
- allocator->deallocate(allocator->self, ptr);
-}
+typedef Slice<const u8> String;
#endif
diff --git a/src/memory.cc b/src/memory.cc
new file mode 100644
index 0000000..27504f0
--- /dev/null
+++ b/src/memory.cc
@@ -0,0 +1,63 @@
+#ifndef MEMORY_CC
+#define MEMORY_CC
+
+#include "common.cc"
+#include <cstdlib>
+#include <cstring>
+
+typedef u8* (*Allocator_Allocate)(u8* self, usize length, usize align);
+typedef void (*Allocator_Deallocate)(u8* self, u8* ptr);
+
+struct Allocator {
+ u8* self;
+ Allocator_Allocate allocate;
+ Allocator_Deallocate deallocate;
+};
+
+u8* allocate(Allocator* allocator, usize size, usize align) {
+ return allocator->allocate(allocator->self, size, align);
+}
+
+void deallocate(Allocator* allocator, u8* ptr) {
+ allocator->deallocate(allocator->self, ptr);
+}
+
+static inline u8* heap_allocate(u8* self, usize size, usize align) {
+ (void)self;
+ return (u8*)aligned_alloc(size, align);
+}
+
+static inline void heap_deallocate(u8* self, u8* ptr) {
+ (void)self;
+ free(ptr);
+}
+
+Allocator* heap_allocator() {
+ 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) {
+ T* new_ptr = allocate(allocator, source->size(), alignof(T));
+ if(new_ptr == nullptr) return false;
+
+ memcpy((u8*)new_ptr, (u8*)source->ptr, source->length);
+
+ out->length = source->length;
+ out->ptr = new_ptr;
+ return true;
+}
+
+template<typename T>
+void slice_deallocate(Allocator* allocator, Slice<T> *slice) {
+ if(slice->ptr == nullptr) return;
+
+ deallocate(allocator, (u8*)slice->ptr);
+
+ slice->ptr = nullptr;
+ slice->length = 0;
+}
+
+#endif
+
diff --git a/src/source.cc b/src/source.cc
new file mode 100644
index 0000000..df3a90e
--- /dev/null
+++ b/src/source.cc
@@ -0,0 +1,8 @@
+#ifndef SOURCE_CC
+#define SOURCE_CC
+
+struct Source {
+
+};
+
+#endif
diff --git a/src/token.cc b/src/token.cc
index 1f6f6f0..f5cf1de 100644
--- a/src/token.cc
+++ b/src/token.cc
@@ -4,12 +4,12 @@
#include "common.cc"
#define TOKEN_KINDS_NOLEX \
- X(eof) \
- X(invalid_char) \
- X(invalid_literal)
+ X(Eof) \
+ X(Invalid_Char) \
+ X(Invalid_Literal)
enum Token_Kind {
-#define X(name) token_kind_##name,
+#define X(name) Token_Kind_##name,
TOKEN_KINDS_NOLEX
#undef X
};
diff --git a/src/voidc.cc b/src/voidc.cc
index a957815..9e9c38f 100644
--- a/src/voidc.cc
+++ b/src/voidc.cc
@@ -1,5 +1,22 @@
-#include "token.cc"
+#include "memory.cc"
+#include "common.cc"
+
+static const char* SOURCE = R"(
+#include <stdlib.h>
+
+int main() {
+ return EXIT_FAILURE;
+}
+
+)";
+static const String SOURCE_STRING = { (u8*)SOURCE, sizeof(SOURCE) - 1 };
int main() {
+ String source;
+ slice_copy(heap_allocator(), &SOURCE_STRING, &source);
+
+ slice_write(&source, stdout);
+ fflush(stdout);
+ slice_deallocate(heap_allocator(), &source);
}