1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-30 12:32:36 +00:00
OpenMW/components/interpreter/mathopcodes.hpp

89 lines
1.8 KiB
C++
Raw Normal View History

2010-06-29 16:11:19 +02:00
#ifndef INTERPRETER_MATHOPCODES_H_INCLUDED
#define INTERPRETER_MATHOPCODES_H_INCLUDED
#include <cmath>
2022-09-22 21:26:05 +03:00
#include <stdexcept>
2010-06-29 16:11:19 +02:00
#include "opcodes.hpp"
#include "runtime.hpp"
namespace Interpreter
{
2022-09-22 21:26:05 +03:00
template <typename T>
2010-06-29 16:11:19 +02:00
class OpAddInt : public Opcode0
{
2022-09-22 21:26:05 +03: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 16:11:19 +02:00
};
2022-09-22 21:26:05 +03:00
template <typename T>
2010-06-29 16:11:19 +02:00
class OpSubInt : public Opcode0
{
2022-09-22 21:26:05 +03: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 16:11:19 +02:00
};
2022-09-22 21:26:05 +03:00
template <typename T>
2010-06-29 16:11:19 +02:00
class OpMulInt : public Opcode0
{
2022-09-22 21:26:05 +03: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 16:11:19 +02:00
};
2022-09-22 21:26:05 +03:00
template <typename T>
2010-06-29 16:11:19 +02:00
class OpDivInt : public Opcode0
{
2022-09-22 21:26:05 +03: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 16:11:19 +02:00
};
2022-09-22 21:26:05 +03:00
template <typename T, typename C>
2010-07-01 12:19:52 +02:00
class OpCompare : public Opcode0
{
2022-09-22 21:26:05 +03: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 16:11:19 +02:00
}
#endif