diff --git a/CMakeLists.txt b/CMakeLists.txt index d43741d..17af31e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,9 @@ add_library(lib${PROJECT_NAME} SHARED src/vm/vm_exceptions.hh src/vm/vm.cc src/vm/vm.hh + src/vm/expr.cc + src/vm/expr.hh + src/vm/location.hh ) target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings}) @@ -85,8 +88,7 @@ target_compile_options(lib${PROJECT_NAME} PRIVATE ${warnings}) # tests # -add_executable(${PROJECT_NAME}-bytecode-test src/bytecode/tests.cc - src/vm/location.hh) +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) diff --git a/TODO.md b/TODO.md index a00258d..842b83a 100644 --- a/TODO.md +++ b/TODO.md @@ -25,9 +25,10 @@ After some additional development: - [x] Output bytecode format - [x] Value object - [x] Stack object - - [ ] External interface - - [ ] Code execution (except functions) - - [ ] Functions + - [x] External interface + - [x] Code execution (except functions) + - [x] Functions + - [ ] Print stack After some additional development: - [ ] Bytecode loader diff --git a/src/vm/expr.cc b/src/vm/expr.cc new file mode 100644 index 0000000..6475798 --- /dev/null +++ b/src/vm/expr.cc @@ -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()); + } +} + +} \ No newline at end of file diff --git a/src/vm/expr.hh b/src/vm/expr.hh new file mode 100644 index 0000000..1641534 --- /dev/null +++ b/src/vm/expr.hh @@ -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 diff --git a/src/vm/vm.cc b/src/vm/vm.cc index 18c4ed9..a6b0e5b 100644 --- a/src/vm/vm.cc +++ b/src/vm/vm.cc @@ -1,6 +1,7 @@ #include "vm.hh" #include "vm_exceptions.hh" +#include "expr.hh" namespace tyche { @@ -21,7 +22,7 @@ void VM::call(size_t n_params) loc_.emplace(f.as_function_id(), 0); stack_.push_fp(); run_until_return(); - stack_.pop_fp(); + // stack_.pop_fp(); loc_.pop(); } @@ -54,6 +55,12 @@ void VM::step() case Instruction::PushInt32: push_integer(op.operator_); break; + case Instruction::Sum: + stack_.push(binary_operation(stack_.pop(), stack_.pop(), BinaryOperationType::Sum)); + break; + case Instruction::Return: + stack_.pop_fp(); + return; default: throw VMInvalidOpcode((uint8_t) op.instruction); } diff --git a/src/vm/vm_exceptions.hh b/src/vm/vm_exceptions.hh index d20eac5..49152dc 100644 --- a/src/vm/vm_exceptions.hh +++ b/src/vm/vm_exceptions.hh @@ -4,6 +4,8 @@ #include #include +#include "expr.hh" + namespace tyche { class VMRuntimeError : public std::runtime_error @@ -36,6 +38,12 @@ public: 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