This commit is contained in:
Andre Wagner
2026-04-30 09:47:02 -05:00
parent 50b55eaeef
commit 29d2b0bba1
6 changed files with 54 additions and 6 deletions

View File

@@ -77,6 +77,9 @@ add_library(lib${PROJECT_NAME} SHARED
src/vm/vm_exceptions.hh src/vm/vm_exceptions.hh
src/vm/vm.cc src/vm/vm.cc
src/vm/vm.hh src/vm/vm.hh
src/vm/expr.cc
src/vm/expr.hh
src/vm/location.hh
) )
target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings}) target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings})
@@ -85,8 +88,7 @@ target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings})
# tests # tests
# #
add_executable(${PROJECT_NAME}-bytecode-test src/bytecode/tests.cc add_executable(${PROJECT_NAME}-bytecode-test src/bytecode/tests.cc)
src/vm/location.hh)
target_link_libraries(${PROJECT_NAME}-bytecode-test lib${PROJECT_NAME} gtest_main) target_link_libraries(${PROJECT_NAME}-bytecode-test lib${PROJECT_NAME} gtest_main)
add_test(NAME tyche_bytecode_test COMMAND ${PROJECT_NAME}-bytecode-test) add_test(NAME tyche_bytecode_test COMMAND ${PROJECT_NAME}-bytecode-test)

View File

@@ -25,9 +25,10 @@ After some additional development:
- [x] Output bytecode format - [x] Output bytecode format
- [x] Value object - [x] Value object
- [x] Stack object - [x] Stack object
- [ ] External interface - [x] External interface
- [ ] Code execution (except functions) - [x] Code execution (except functions)
- [ ] Functions - [x] Functions
- [ ] Print stack
After some additional development: After some additional development:
- [ ] Bytecode loader - [ ] Bytecode loader

18
src/vm/expr.cc Normal file
View File

@@ -0,0 +1,18 @@
#include "expr.hh"
#include "vm_exceptions.hh"
namespace tyche {
Value binary_operation(Value const& a, Value const& b, BinaryOperationType op)
{
// TODO - this is temporary code
if (a.type() == Type::Integer && b.type() == Type::Integer && op == BinaryOperationType::Sum) {
return Value::CreateInteger(a.as_integer() + b.as_integer());
} else {
throw VMInvalidOperation(op, a.type(), b.type());
}
}
}

12
src/vm/expr.hh Normal file
View File

@@ -0,0 +1,12 @@
#ifndef TYCHE_EXPR_HH
#define TYCHE_EXPR_HH
#include "value.hh"
namespace tyche {
enum class BinaryOperationType { Sum };
Value binary_operation(Value const& a, Value const& b, BinaryOperationType op);
}
#endif //TYCHE_EXPR_HH

View File

@@ -1,6 +1,7 @@
#include "vm.hh" #include "vm.hh"
#include "vm_exceptions.hh" #include "vm_exceptions.hh"
#include "expr.hh"
namespace tyche { namespace tyche {
@@ -21,7 +22,7 @@ void VM::call(size_t n_params)
loc_.emplace(f.as_function_id(), 0); loc_.emplace(f.as_function_id(), 0);
stack_.push_fp(); stack_.push_fp();
run_until_return(); run_until_return();
stack_.pop_fp(); // stack_.pop_fp();
loc_.pop(); loc_.pop();
} }
@@ -54,6 +55,12 @@ void VM::step()
case Instruction::PushInt32: case Instruction::PushInt32:
push_integer(op.operator_); push_integer(op.operator_);
break; break;
case Instruction::Sum:
stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Sum));
break;
case Instruction::Return:
stack_.pop_fp();
return;
default: default:
throw VMInvalidOpcode((uint8_t) op.instruction); throw VMInvalidOpcode((uint8_t) op.instruction);
} }

View File

@@ -4,6 +4,8 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include "expr.hh"
namespace tyche { namespace tyche {
class VMRuntimeError : public std::runtime_error class VMRuntimeError : public std::runtime_error
@@ -36,6 +38,12 @@ public:
explicit VMInvalidOpcode(uint8_t opcode) : VMRuntimeError("Invalid opcode " + std::to_string(opcode)) {} explicit VMInvalidOpcode(uint8_t opcode) : VMRuntimeError("Invalid opcode " + std::to_string(opcode)) {}
}; };
class VMInvalidOperation : public VMRuntimeError
{
public:
explicit VMInvalidOperation(BinaryOperationType op, Type type1, Type type2) : VMRuntimeError("Invalid binary operation") {} // TODO - print types
};
} }
#endif //TYCHE_VM_EXCEPTIONS_HH #endif //TYCHE_VM_EXCEPTIONS_HH