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