From 9229d6c86e3504feb2c6566fe6e8743b4f3b40ba Mon Sep 17 00:00:00 2001 From: Andre Wagner Date: Wed, 29 Apr 2026 11:23:53 -0500 Subject: [PATCH] . --- CMakeLists.txt | 2 +- TODO.md | 2 +- src/bytecode/bytearray.cc | 20 ++++++++++++++++++++ src/bytecode/bytearray.hh | 2 ++ src/bytecode/bytecodeprototype.hh | 2 +- src/bytecode/tests.cc | 19 +++++++++---------- 6 files changed, 34 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5f5f06..3dbb8bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,7 +58,7 @@ FetchContent_MakeAvailable(googletest) # library # -add_library(lib${PROJECT_NAME} STATIC +add_library(lib${PROJECT_NAME} SHARED src/bytecode/bytearray.hh src/bytecode/bytearray.cc src/bytecode/bytecode.cc diff --git a/TODO.md b/TODO.md index 85d8a2c..5b92b3f 100644 --- a/TODO.md +++ b/TODO.md @@ -10,7 +10,7 @@ - [x] Refactor bytecode code Improvements: -- [ ] Fixed int type (based on opcode) +- [x] Fixed int type (based on opcode) - [ ] Constant type (only floats and strings for now) - [ ] Debug output bytecode format diff --git a/src/bytecode/bytearray.cc b/src/bytecode/bytearray.cc index 4bfcfdd..317218a 100644 --- a/src/bytecode/bytearray.cc +++ b/src/bytecode/bytearray.cc @@ -1,6 +1,7 @@ #include "bytearray.hh" #include +#include namespace tyche { @@ -132,4 +133,23 @@ void ByteArray::append_bytearray(ByteArray const& bytearray) data_.insert(data_.end(), bytearray.data().begin(), bytearray.data().end()); } +std::string ByteArray::hexdump() const +{ + auto to_hex = [](uint32_t value, size_t n_chars) -> std::string { + char buf[15]; + snprintf(buf, sizeof buf, (std::string("%0") + std::to_string(n_chars) + "X").c_str(), value); + return { buf }; + }; + + std::string out; + for (size_t i = 0; i < data_.size(); ++i) { + if (i % 16 == 0) + out += to_hex(i, 4) + " | "; + out += to_hex(data_.at(i), 2) + " "; + if (i % 16 == 15) + out += "\n"; + } + return out + "\n"; +} + } \ No newline at end of file diff --git a/src/bytecode/bytearray.hh b/src/bytecode/bytearray.hh index fa45591..3cf6ef6 100644 --- a/src/bytecode/bytearray.hh +++ b/src/bytecode/bytearray.hh @@ -45,6 +45,8 @@ public: [[nodiscard]] std::vector const& data() const { return data_; } [[nodiscard]] size_t size() const { return data_.size(); } + [[nodiscard]] std::string hexdump() const; + private: std::vector data_ {}; }; diff --git a/src/bytecode/bytecodeprototype.hh b/src/bytecode/bytecodeprototype.hh index 0a4a6bf..880027b 100644 --- a/src/bytecode/bytecodeprototype.hh +++ b/src/bytecode/bytecodeprototype.hh @@ -9,7 +9,7 @@ namespace tyche { struct BytecodePrototype { - using ConstantValue = std::variant; + using ConstantValue = std::variant; struct Function { uint16_t n_pars; diff --git a/src/bytecode/tests.cc b/src/bytecode/tests.cc index 5838123..7c39152 100644 --- a/src/bytecode/tests.cc +++ b/src/bytecode/tests.cc @@ -44,7 +44,7 @@ TEST(ByteArray, ByteArray) TEST(Bytecode, Constants) { BytecodePrototype bp; - bp.constants.emplace_back(42); + bp.constants.emplace_back(42.3f); bp.constants.emplace_back("HELLO"); std::vector expected = { @@ -57,7 +57,7 @@ TEST(Bytecode, Constants) // index 0x50, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // constant index 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // function undex - 0x58, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, // raw constants + 0x58, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, // raw constants 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // raw code 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -66,14 +66,14 @@ TEST(Bytecode, Constants) // constant indexes 0x00, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, // constant values - 0x54, 'H', 'E', 'L', 'L', 'O', 0x00 + 0x33, 0x33, 0x29, 0x42, // float: 42.3f + 'H', 'E', 'L', 'L', 'O', 0x00 }; ByteArray ba = Bytecode::generate(bp); - // print(ba.data()); print(expected); ASSERT_EQ(ba.data(), expected); } @@ -82,7 +82,7 @@ TEST(Bytecode, Code) BytecodePrototype bp; auto& f = bp.functions.emplace_back(0, 0); f.code.append_byte(0x68); - f.code.append_int32(42); + f.code.append_int8(42); auto& f2 = bp.functions.emplace_back(2, 1); f2.code.append_byte(0x42); @@ -109,7 +109,7 @@ TEST(Bytecode, Code) 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, // code - 0x68, 0x54, 0x42, + 0x68, 42, 0x42, }; ByteArray ba = Bytecode::generate(bp); @@ -122,7 +122,6 @@ TEST(Bytecode, Parsing) BytecodePrototype bp; - bp.constants.emplace_back(42); bp.constants.emplace_back(3.14f); bp.constants.emplace_back("HELLO"); @@ -143,8 +142,8 @@ TEST(Bytecode, Parsing) ASSERT_EQ(bc.n_constants(), 2); ASSERT_EQ(bc.n_functions(), 2); - ASSERT_FLOAT_EQ(bc.get_constant_float(1), 3.14f); - ASSERT_EQ(bc.get_constant_string(2), "HELLO"); + ASSERT_FLOAT_EQ(bc.get_constant_float(0), 3.14f); + ASSERT_EQ(bc.get_constant_string(1), "HELLO"); Bytecode::FunctionDef f1 = bc.get_function_def(0); ASSERT_EQ(f1.n_params, 0);