This commit is contained in:
2026-04-29 11:23:53 -05:00
parent 766032e839
commit 9229d6c86e
6 changed files with 34 additions and 13 deletions

View File

@@ -58,7 +58,7 @@ FetchContent_MakeAvailable(googletest)
# library # library
# #
add_library(lib${PROJECT_NAME} STATIC add_library(lib${PROJECT_NAME} SHARED
src/bytecode/bytearray.hh src/bytecode/bytearray.hh
src/bytecode/bytearray.cc src/bytecode/bytearray.cc
src/bytecode/bytecode.cc src/bytecode/bytecode.cc

View File

@@ -10,7 +10,7 @@
- [x] Refactor bytecode code - [x] Refactor bytecode code
Improvements: Improvements:
- [ ] Fixed int type (based on opcode) - [x] Fixed int type (based on opcode)
- [ ] Constant type (only floats and strings for now) - [ ] Constant type (only floats and strings for now)
- [ ] Debug output bytecode format - [ ] Debug output bytecode format

View File

@@ -1,6 +1,7 @@
#include "bytearray.hh" #include "bytearray.hh"
#include <cstring> #include <cstring>
#include <cstdio>
namespace tyche { namespace tyche {
@@ -132,4 +133,23 @@ void ByteArray::append_bytearray(ByteArray const& bytearray)
data_.insert(data_.end(), bytearray.data().begin(), bytearray.data().end()); 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";
}
} }

View File

@@ -45,6 +45,8 @@ public:
[[nodiscard]] std::vector<uint8_t> const& data() const { return data_; } [[nodiscard]] std::vector<uint8_t> const& data() const { return data_; }
[[nodiscard]] size_t size() const { return data_.size(); } [[nodiscard]] size_t size() const { return data_.size(); }
[[nodiscard]] std::string hexdump() const;
private: private:
std::vector<uint8_t> data_ {}; std::vector<uint8_t> data_ {};
}; };

View File

@@ -9,7 +9,7 @@
namespace tyche { namespace tyche {
struct BytecodePrototype { struct BytecodePrototype {
using ConstantValue = std::variant<int32_t, float, std::string>; using ConstantValue = std::variant<float, std::string>;
struct Function { struct Function {
uint16_t n_pars; uint16_t n_pars;

View File

@@ -44,7 +44,7 @@ TEST(ByteArray, ByteArray)
TEST(Bytecode, Constants) TEST(Bytecode, Constants)
{ {
BytecodePrototype bp; BytecodePrototype bp;
bp.constants.emplace_back(42); bp.constants.emplace_back(42.3f);
bp.constants.emplace_back("HELLO"); bp.constants.emplace_back("HELLO");
std::vector<uint8_t> expected = { std::vector<uint8_t> expected = {
@@ -57,7 +57,7 @@ TEST(Bytecode, Constants)
// index // index
0x50, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // constant index 0x50, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // constant index
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // function undex 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, // raw code
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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 // constant indexes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
// constant values // 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); ByteArray ba = Bytecode::generate(bp);
// print(ba.data()); print(expected);
ASSERT_EQ(ba.data(), expected); ASSERT_EQ(ba.data(), expected);
} }
@@ -82,7 +82,7 @@ TEST(Bytecode, Code)
BytecodePrototype bp; BytecodePrototype bp;
auto& f = bp.functions.emplace_back(0, 0); auto& f = bp.functions.emplace_back(0, 0);
f.code.append_byte(0x68); f.code.append_byte(0x68);
f.code.append_int32(42); f.code.append_int8(42);
auto& f2 = bp.functions.emplace_back(2, 1); auto& f2 = bp.functions.emplace_back(2, 1);
f2.code.append_byte(0x42); f2.code.append_byte(0x42);
@@ -109,7 +109,7 @@ TEST(Bytecode, Code)
0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
// code // code
0x68, 0x54, 0x42, 0x68, 42, 0x42,
}; };
ByteArray ba = Bytecode::generate(bp); ByteArray ba = Bytecode::generate(bp);
@@ -122,7 +122,6 @@ TEST(Bytecode, Parsing)
BytecodePrototype bp; BytecodePrototype bp;
bp.constants.emplace_back(42);
bp.constants.emplace_back(3.14f); bp.constants.emplace_back(3.14f);
bp.constants.emplace_back("HELLO"); bp.constants.emplace_back("HELLO");
@@ -143,8 +142,8 @@ TEST(Bytecode, Parsing)
ASSERT_EQ(bc.n_constants(), 2); ASSERT_EQ(bc.n_constants(), 2);
ASSERT_EQ(bc.n_functions(), 2); ASSERT_EQ(bc.n_functions(), 2);
ASSERT_FLOAT_EQ(bc.get_constant_float(1), 3.14f); ASSERT_FLOAT_EQ(bc.get_constant_float(0), 3.14f);
ASSERT_EQ(bc.get_constant_string(2), "HELLO"); ASSERT_EQ(bc.get_constant_string(1), "HELLO");
Bytecode::FunctionDef f1 = bc.get_function_def(0); Bytecode::FunctionDef f1 = bc.get_function_def(0);
ASSERT_EQ(f1.n_params, 0); ASSERT_EQ(f1.n_params, 0);