code #4

Merged
andre merged 5 commits from code into master 2026-04-29 14:44:34 -05:00
8 changed files with 51 additions and 12 deletions
Showing only changes of commit 046d5cb609 - Show all commits

View File

@@ -82,16 +82,20 @@ add_executable(${PROJECT_NAME}-bytecode-test src/bytecode/tests.cc)
target_link_libraries(${PROJECT_NAME}-bytecode-test lib${PROJECT_NAME} gtest_main)
add_test(NAME tyche_bytecode_test COMMAND ${PROJECT_NAME}-bytecode-test)
add_executable(${PROJECT_NAME}-vm-test src/vm/tests.cc)
target_link_libraries(${PROJECT_NAME}-vm-test lib${PROJECT_NAME} gtest_main)
add_test(NAME tyche_vm_test COMMAND ${PROJECT_NAME}-vm-test)
#
# check for leaks
#
add_custom_target(leaks)
add_custom_command(TARGET leaks
add_custom_target(leaks-vm-test)
add_custom_command(TARGET leaks-vm-test
POST_BUILD
COMMENT "Check for leaks using valgrind."
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --suppressions=${CMAKE_SOURCE_DIR}/valgrind.supp ./${PROJECT_NAME}
COMMAND valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --suppressions=${CMAKE_SOURCE_DIR}/valgrind.supp ./${PROJECT_NAME}-vm-test
)
#

View File

@@ -7,13 +7,10 @@ namespace tyche {
void ByteArray::set_byte(uint32_t addr, uint8_t byte)
{
try {
data_.at(addr) = byte;
} catch (std::out_of_range&) {
if (data_.size() < (addr + 1))
data_.resize(addr + 1, 0);
data_.at(addr) = byte;
}
}
void ByteArray::set_int8(uint32_t addr, int8_t value)
{

View File

@@ -8,6 +8,7 @@ namespace tyche {
class Bytecode {
public:
Bytecode() = default;
explicit Bytecode(ByteArray ba);
[[nodiscard]] uint32_t n_constants() const;

View File

@@ -6,6 +6,7 @@
#include <variant>
#include <vector>
#include "constant.hh"
#include "bytearray.hh"
namespace tyche {

View File

@@ -1,5 +1,4 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <cstring>
#include <functional>

View File

@@ -4,10 +4,12 @@
namespace tyche {
void Code::import_bytecode(Bytecode const& incoming)
void Code::import_bytecode(ByteArray incoming)
{
Bytecode bc(std::move(incoming));
// TODO - adjust function calls, constants
bytecode_ = incoming;
bytecode_ = std::move(bc);
}
std::string Code::disassemble() const

View File

@@ -7,7 +7,7 @@ namespace tyche {
class Code {
public:
void import_bytecode(Bytecode const& incoming);
void import_bytecode(ByteArray incoming);
[[nodiscard]] std::string disassemble() const;

35
src/vm/tests.cc Normal file
View File

@@ -0,0 +1,35 @@
#include "gtest/gtest.h"
#include "../bytecode/bytecodeprototype.hh"
#include "../bytecode/bytearray.hh"
#include "../bytecode/bytecode.hh"
#include "code.hh"
using namespace tyche;
TEST(Code, ImportSingleAndDebug)
{
BytecodePrototype bp;
bp.constants.emplace_back(3.14f);
bp.constants.emplace_back("HELLO");
bp.functions.emplace_back(0, 0);
bp.functions.at(0).code.append_byte(0xa0); // pushi
bp.functions.at(0).code.append_int8(42);
bp.functions.emplace_back(2, 1);
bp.functions.at(1).code.append_byte(0x1a); // appnd
ByteArray ba = Bytecode::generate(bp);
Code code;
code.import_bytecode(std::move(ba));
printf("%s\n", code.disassemble().c_str());
}
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}