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