#ifndef INTERPRETER_MATHOPCODES_H_INCLUDED #define INTERPRETER_MATHOPCODES_H_INCLUDED #include #include #include "opcodes.hpp" #include "runtime.hpp" namespace Interpreter { template class OpAddInt : public Opcode0 { public: void execute(Runtime& runtime) override { T result = getData(runtime[1]) + getData(runtime[0]); runtime.pop(); getData(runtime[0]) = result; } }; template class OpSubInt : public Opcode0 { public: void execute(Runtime& runtime) override { T result = getData(runtime[1]) - getData(runtime[0]); runtime.pop(); getData(runtime[0]) = result; } }; template class OpMulInt : public Opcode0 { public: void execute(Runtime& runtime) override { T result = getData(runtime[1]) * getData(runtime[0]); runtime.pop(); getData(runtime[0]) = result; } }; template class OpDivInt : public Opcode0 { public: void execute(Runtime& runtime) override { T left = getData(runtime[0]); if (left == 0) throw std::runtime_error("division by zero"); T result = getData(runtime[1]) / left; runtime.pop(); getData(runtime[0]) = result; } }; template class OpCompare : public Opcode0 { public: void execute(Runtime& runtime) override { int result = C()(getData(runtime[1]), getData(runtime[0])); runtime.pop(); runtime[0].mInteger = result; } }; } #endif