1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-31 01:15:34 +00:00
openmw/components/interpreter/mathopcodes.hpp

89 lines
1.8 KiB
C++
Raw Normal View History

2010-06-29 14:11:19 +00:00
#ifndef INTERPRETER_MATHOPCODES_H_INCLUDED
#define INTERPRETER_MATHOPCODES_H_INCLUDED
#include <cmath>
2022-09-22 18:26:05 +00:00
#include <stdexcept>
2010-06-29 14:11:19 +00:00
#include "opcodes.hpp"
#include "runtime.hpp"
namespace Interpreter
{
2022-09-22 18:26:05 +00:00
template <typename T>
2010-06-29 14:11:19 +00:00
class OpAddInt : public Opcode0
{
2022-09-22 18:26:05 +00:00
public:
void execute(Runtime& runtime) override
{
T result = getData<T>(runtime[1]) + getData<T>(runtime[0]);
runtime.pop();
getData<T>(runtime[0]) = result;
}
2010-06-29 14:11:19 +00:00
};
2022-09-22 18:26:05 +00:00
template <typename T>
2010-06-29 14:11:19 +00:00
class OpSubInt : public Opcode0
{
2022-09-22 18:26:05 +00:00
public:
void execute(Runtime& runtime) override
{
T result = getData<T>(runtime[1]) - getData<T>(runtime[0]);
runtime.pop();
getData<T>(runtime[0]) = result;
}
2010-06-29 14:11:19 +00:00
};
2022-09-22 18:26:05 +00:00
template <typename T>
2010-06-29 14:11:19 +00:00
class OpMulInt : public Opcode0
{
2022-09-22 18:26:05 +00:00
public:
void execute(Runtime& runtime) override
{
T result = getData<T>(runtime[1]) * getData<T>(runtime[0]);
runtime.pop();
getData<T>(runtime[0]) = result;
}
2010-06-29 14:11:19 +00:00
};
2022-09-22 18:26:05 +00:00
template <typename T>
2010-06-29 14:11:19 +00:00
class OpDivInt : public Opcode0
{
2022-09-22 18:26:05 +00:00
public:
void execute(Runtime& runtime) override
{
T left = getData<T>(runtime[0]);
if (left == 0)
throw std::runtime_error("division by zero");
T result = getData<T>(runtime[1]) / left;
runtime.pop();
getData<T>(runtime[0]) = result;
}
2010-06-29 14:11:19 +00:00
};
2022-09-22 18:26:05 +00:00
template <typename T, typename C>
2010-07-01 10:19:52 +00:00
class OpCompare : public Opcode0
{
2022-09-22 18:26:05 +00:00
public:
void execute(Runtime& runtime) override
{
int result = C()(getData<T>(runtime[1]), getData<T>(runtime[0]));
runtime.pop();
runtime[0].mInteger = result;
}
};
2010-06-29 14:11:19 +00:00
}
#endif