Vita
bool.h
Go to the documentation of this file.
1
13#if !defined(VITA_BOOL_PRIMITIVE_H)
14#define VITA_BOOL_PRIMITIVE_H
15
16#include <cstdlib>
17
18#include "kernel/gp/function.h"
20#include "kernel/gp/terminal.h"
21
22namespace vita::boolean
23{
24
25class zero : public terminal
26{
27public:
28 explicit zero(const cvect &c) : terminal("0", c[0])
29 { Expects(c.size() == 1); }
30
31 value_t eval(symbol_params &) const final { return false; }
32
33 std::string display(terminal_param_t, format f) const final
34 {
35 switch (f)
36 {
37 case cpp_format: return "false";
38 case python_format: return "False";
39 default: return "0";
40 }
41 }
42};
43
44class one : public terminal
45{
46public:
47 explicit one(const cvect &c) : terminal("1", c[0])
48 { Expects(c.size() == 1); }
49
50 value_t eval(symbol_params &) const final { return true; }
51
52 std::string display(terminal_param_t, format f) const final
53 {
54 switch (f)
55 {
56 case cpp_format: return "true";
57 case python_format: return "True";
58 default: return "1";
59 }
60 }
61};
62
63class l_and : public function
64{
65public:
66 explicit l_and(const cvect &c) : function("AND", c[0], {c[0], c[0]})
67 { Expects(c.size() == 1); }
68
69 bool associative() const final { return true; }
70
71 value_t eval(symbol_params &args) const final
72 {
73 return std::get<D_INT>(args[0]) && std::get<D_INT>(args[1]);
74 }
75
76 std::string display(format f) const final
77 {
78 switch (f)
79 {
80 case python_format: return "(%%1%% and %%2%%)";
81 default: return "(%%1%% && %%2%%)";
82 }
83 }
84};
85
86class l_not : public function
87{
88public:
89 explicit l_not(const cvect &c) : function("NOT", c[0], {c[0]})
90 { Expects(c.size() == 1); }
91
92 value_t eval(symbol_params &args) const final
93 {
94 return !std::get<D_INT>(args[0]);
95 }
96
97 std::string display(format f) const final
98 {
99 switch (f)
100 {
101 case python_format: return "not(%%1%%)";
102 default: return "!%%1%%";
103 }
104 }
105};
106
107class l_or : public function
108{
109public:
110 explicit l_or(const cvect &c) : function("OR", c[0], {c[0], c[0]})
111 { Expects(c.size() == 1); }
112
113 bool associative() const final { return true; }
114
115 value_t eval(symbol_params &args) const final
116 {
117 return std::get<D_INT>(args[0]) || std::get<D_INT>(args[1]);
118 }
119
120 std::string display(format f) const final
121 {
122 switch (f)
123 {
124 case python_format: return "(%%1%% or %%2%%)";
125 default: return "(%%1%% || %%2%%)";
126 }
127 }
128};
129
130} // namespace vita::boolean
131
132#endif // include guard
bool associative() const final
Is the symbol subject to the associative law of arithmetic?
Definition: bool.h:69
std::string display(format f) const final
Definition: bool.h:76
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
Definition: bool.h:71
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
Definition: bool.h:92
std::string display(format f) const final
Definition: bool.h:97
bool associative() const final
Is the symbol subject to the associative law of arithmetic?
Definition: bool.h:113
std::string display(format f) const final
Definition: bool.h:120
value_t eval(symbol_params &args) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
Definition: bool.h:115
std::string display(terminal_param_t, format f) const final
Definition: bool.h:52
value_t eval(symbol_params &) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
Definition: bool.h:50
std::string display(terminal_param_t, format f) const final
Definition: bool.h:33
value_t eval(symbol_params &) const final
Calculates the value of / performs the action associated with the symbol (it's implementation specifi...
Definition: bool.h:31
A symbol with arity() > 0.
Definition: function.h:35
function(const std::string &, category_t, cvect)
Definition: function.cc:25
An interface for parameter passing to functions / terminals.
format
Symbol rendering format.
Definition: symbol.h:39
bool terminal() const
Definition: symbol.h:153
A symbol with zero-arity.
Definition: terminal.h:27
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 ...
Definition: value.h:45