13#if !defined(VITA_INT_PRIMITIVE_H)
14#define VITA_INT_PRIMITIVE_H
46 return std::get<base_t>(v);
57 explicit number(
const cvect &c,
int m = -128,
int u = 127)
58 :
terminal(
"INT", c[0]), min(m), upp(u)
60 Expects(c.size() == 1);
66 terminal_param_t
init() const final {
return random::between(min, upp); }
69 {
return std::to_string(v); }
73 return static_cast<base_t
>(p.fetch_param());
84 explicit add(
const cvect &c) :
function(
"ADD", c[0], {c[0], c[0]})
86 Expects(c.size() == 1);
96 if (v0 > 0 && v1 > 0 && (v0 > std::numeric_limits<base_t>::max() - v1))
97 return std::numeric_limits<base_t>::max();
98 if (v0 < 0 && v1 < 0 && (v0 < std::numeric_limits<base_t>::min() - v1))
99 return std::numeric_limits<base_t>::min();
109 explicit div(
const cvect &c) :
function(
"DIV", c[0], {c[0], c[0]})
110 { Expects(c.size() == 1); }
117 if (v1 == 0 || (v0 == std::numeric_limits<base_t>::min() && (v1 == -1)))
127 explicit ife(
const cvect &c)
128 :
function(
"IFE", c[1], {c[0], c[0], c[1], c[1]})
129 { Expects(c.size() == 2); }
151 explicit ifl(
const cvect &c)
152 :
function(
"IFL", c[1], {c[0], c[0], c[1], c[1]})
153 { Expects(c.size() == 2); }
175 explicit ifz(
const cvect &c) :
function(
"IFZ", c[0], {c[0], c[0], c[0]})
176 { Expects(c.size() == 1); }
198 explicit mod(
const cvect &c) :
function(
"MOD", c[0], {c[0], c[0]})
199 { Expects(c.size() == 1); }
206 if (v1 == 0 || (v0 == std::numeric_limits<base_t>::min() && (v1 == -1)))
217 explicit mul(
const cvect &c) :
function(
"MUL", c[0], {c[0], c[0]})
218 { Expects(c.size() == 1); }
224 static_assert(
sizeof(std::intmax_t) >= 2 *
sizeof(base_t),
225 "Unable to detect overflow after multiplication");
230 const auto tmp(v0 * v1);
231 if (tmp > std::numeric_limits<base_t>::max())
232 return std::numeric_limits<base_t>::max();
233 if (tmp < std::numeric_limits<base_t>::min())
234 return std::numeric_limits<base_t>::min();
236 return static_cast<base_t
>(tmp);
278 explicit shl(
const cvect &c) :
function(
"SHL", c[0], {c[0], c[0]})
279 { Expects(c.size() == 1); }
286 if (v0 < 0 || v1 < 0 ||
287 v1 >=
static_cast<base_t
>(
sizeof(base_t) * CHAR_BIT) ||
288 v0 > std::numeric_limits<base_t>::max() >> v1)
299 explicit sub(
const cvect &c) :
function(
"SUB", c[0], {c[0], c[0]})
300 { Expects(c.size() == 1); }
307 if (v0 < 0 && v1 > 0 && (v0 < std::numeric_limits<base_t>::min() + v1))
308 return std::numeric_limits<base_t>::min();
309 if (v0 > 0 && v1 < 0 && (v0 > std::numeric_limits<base_t>::max() + v1))
310 return std::numeric_limits<base_t>::max();
Minimum interface of an interpreter.
A symbol with arity() > 0.
function(const std::string &, category_t, cvect)
bool associative() const final
Is the symbol subject to the associative law of arithmetic?
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
double penalty_nvi(core_interpreter *ci) const final
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
double penalty_nvi(core_interpreter *ci) const final
double penalty_nvi(core_interpreter *ci) const final
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
bool associative() const final
Is the symbol subject to the associative law of arithmetic?
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
Integer ephemeral random constant.
value_t eval(symbol_params &p) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
bool parametric() const final
A parametric terminal needs an additional parameter to be evaluated.
terminal_param_t init() const final
Used to initialize the internal parameter of the terminal.
std::string display(terminal_param_t v, format) const final
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
An interface for parameter passing to functions / terminals.
format
Symbol rendering format.
A symbol with zero-arity.
Integer overflow is undefined behaviour.
base_t cast(const value_t &v)
Just a simple shortcut.
double comparison_function_penalty(core_interpreter *ci)
A simple, convenient function for the penalty score of the typical four-terms comparison.
std::variant< D_VOID, D_INT, D_DOUBLE, D_STRING > value_t
A variant containing the data types used by the interpreter for internal calculations / output value ...