#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: virtual void execute (Runtime& runtime) { T result = *reinterpret_cast (&runtime[1]) + *reinterpret_cast (&runtime[0]); runtime.pop(); runtime[0] = *reinterpret_cast (&result); } }; template class OpSubInt : public Opcode0 { public: virtual void execute (Runtime& runtime) { T result = *reinterpret_cast (&runtime[1]) - *reinterpret_cast (&runtime[0]); runtime.pop(); runtime[0] = *reinterpret_cast (&result); } }; template class OpMulInt : public Opcode0 { public: virtual void execute (Runtime& runtime) { T result = *reinterpret_cast (&runtime[1]) * *reinterpret_cast (&runtime[0]); runtime.pop(); runtime[0] = *reinterpret_cast (&result); } }; template class OpDivInt : public Opcode0 { public: virtual void execute (Runtime& runtime) { T left = *reinterpret_cast (&runtime[0]); if (left==0) throw std::runtime_error ("division by zero"); T result = *reinterpret_cast (&runtime[1]) / left; runtime.pop(); runtime[0] = *reinterpret_cast (&result); } }; class OpSquareRoot : public Opcode0 { public: virtual void execute (Runtime& runtime) { Type_Float value = *reinterpret_cast (&runtime[0]); if (value<0) throw std::runtime_error ( "square root of negative number (we aren't that imaginary)"); value = std::sqrt (value); runtime[0] = *reinterpret_cast (&value); } }; } #endif