From 4718780d9d3e06f7e40db563a091a8b0965d44f6 Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sun, 28 Jul 2024 23:37:43 +0100 Subject: [PATCH] Make writer actually print something This also fixes some of the GcRoot bugs --- src/arena.hpp | 7 +++---- src/common.cpp | 12 +++++++++++- src/common.hpp | 3 +++ src/result.hpp | 28 ++++++++++++++++++++++++++-- src/vli.cpp | 3 +++ 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/arena.hpp b/src/arena.hpp index 555d6fc..7f0b0f8 100644 --- a/src/arena.hpp +++ b/src/arena.hpp @@ -55,11 +55,10 @@ class GcRootList { GcRootList(GcRootBase* root) : _prev(0), _next(0), _root(root) {} void insert(GcRootList* node) { - GcRootList* next = _next; - - _next = node; - node->_next = next; + node->_next = _next; node->_prev = this; + if (_next) _next->_prev = node; + _next = node; } void remove() { diff --git a/src/common.cpp b/src/common.cpp index 2cd7772..e1c3b3e 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -52,6 +52,7 @@ Result ByteArray::create(Arena& arena, String& str) { size += utf8_codepoint_size(TRY(str[i])); } auto pod = TRY(arena.alloc(size * sizeof(char))); + pod->size = size; char* res = pod->data; for (uint64_t i = 0; i < str.size(); i++) { @@ -59,7 +60,6 @@ Result ByteArray::create(Arena& arena, String& str) { size = utf8_codepoint_size(codepoint); res = utf8_write_codepoint(res, codepoint); } - pod->size = size; return ByteArray(TRY(MkGcRoot(pod, arena))); } @@ -137,3 +137,13 @@ Result reverse(Arena& arena, Value& val) { return res; } + +Result debug_print(Arena& arena, String& val) { + auto ba = TRY(ByteArray::create(arena, val)); + + for (uint64_t i = 0; i < ba.size(); i++) { + std::cout << TRY(ba[i]); + } + std::cout << "\n"; + return Result(); +} diff --git a/src/common.hpp b/src/common.hpp index d21ec9e..40c0df4 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -423,6 +423,7 @@ class Value { } Value& operator=(Value&& val) { + ((Object*)buf)->~Object(); memcpy(buf, val.buf, 24); new (val.buf) Nil(); return *this; @@ -455,3 +456,5 @@ class Value { Result syntax_unwrap(Value& val); Result reverse(Arena& arena, Value& val); + +Result debug_print(Arena& arena, String& val); diff --git a/src/result.hpp b/src/result.hpp index 3a0e32f..4e4f120 100644 --- a/src/result.hpp +++ b/src/result.hpp @@ -7,8 +7,8 @@ template class Result { public: - Result(const T& res) : _value(res) {} - Result(T&& res) : _value(std::move(res)) {} + Result(const T& res) : _value(res), _error(ErrorCode::Success) {} + Result(T&& res) : _value(std::move(res)), _error(ErrorCode::Success) {} Result(ErrorCode err) : _error(err) {} bool has_error() { return _error != ErrorCode::Success; } @@ -24,6 +24,30 @@ class Result { ErrorCode _error; }; +template <> +class Result { + public: + using ResultType = void; + + Result(Result&& res) = default; + + Result(ErrorCode err) : err(err) {} + Result() : err(ErrorCode::Success) {} + + Result& operator=(Result&& res) = default; + + bool has_error() { return err != ErrorCode::Success; } + bool has_value() { return !has_error(); } + + void value() {} + void release_value() {} + + ErrorCode error() { return err; } + + private: + ErrorCode err; +}; + #define TRY(m) \ (({ \ auto ___res = (m); \ diff --git a/src/vli.cpp b/src/vli.cpp index 2cb020e..662bcde 100644 --- a/src/vli.cpp +++ b/src/vli.cpp @@ -36,5 +36,8 @@ int main() { auto writer = Writer(arena); auto s2 = DIEIF(writer.write_one(r)); + std::cout << "size: " << s.size() << "\n"; + std::cout << "char: " << (char)DIEIF(s[0]) << "\n"; + DIEIF(debug_print(arena, s2)); return 0; }