Leonardus
sosimp.h
Go to the documentation of this file.
1
17#pragma once
18
19#include <string>
20#include <map>
21#include <numeric>
22#include <vector>
23#include <memory>
24
25#include "dbc.h"
26#include "helper.h"
27#include "adapter128.h"
28#include "so.h"
29
30
31/* ******************************************************************* */
32/* Simple Objects */
33/* SO's that store their data without indirect referencing */
34/* ******************************************************************* */
35
36
40class SOM : public SO {
41public: /* virtual */
42 [[nodiscard]] SOM * dup() const override { return new SOM; }
43
44 std::string opequal() const override { return "--nostringval--"; }
45
46 std::string opequalequal() const override { return "mark"; }
47
48 OTCode ot() const override { return OTCode::M; }
49
50 std::string type() const override { return "marktype"; }
51
52 bool equal( const SO * p_other ) const override {
53 return dynamic_cast<const SOM *>( p_other) != nullptr; // All SOM are equal
54 }
55};
56
57
61class SOL : public SO {
62public: /* virtual */
63 SOL * dup() const override { return new SOL; }
64
65 std::string opequal() const override { return "--nostringval--"; }
66
67 std::string opequalequal() const override { return "null"; }
68
69 OTCode ot() const override { return OTCode::L; }
70
71 std::string type() const override { return "nulltype"; }
72
73 bool equal( const SO * p_other ) const override {
74 return dynamic_cast<const SOL *>( p_other) != nullptr; // All SOL are equal
75 }
76};
77
78
83class SOI : public SO {
84 __int128 i_;
86public:
88 explicit SOI( const std::string & p_s ) : i_(stoint128(p_s)) {}
89
91 explicit SOI( __int128 p_i ) : i_(p_i) {}
92
93public: /* accessor */
95 __int128 getInteger() const { return i_; }
96
98 void setInteger( __int128 p_i ) { i_=p_i; }
99
100public: /* virtual */
101 [[nodiscard]] SOI * dup() const override { return new SOI( i_ ); }
102
103 std::string opequal() const override { return to_string( i_ ); }
104
105 OTCode ot() const override { return OTCode::I; }
106
107 std::string type() const override { return "integertype"; }
108
109 bool equal( const SO * p_other ) const override {
110 auto o = dynamic_cast<const SOI*>( p_other );
111 return o ? i_ == o->i_ : false;
112 }
113
114 bool gt( const SO * p_other ) const override {
115 auto o = dynamic_cast<const SOI*>( p_other );
116 if( o == nullptr )
118 return i_ > o->i_;
119 }
120
121 bool ge( const SO * p_other ) const override {
122 auto o = dynamic_cast<const SOI*>( p_other );
123 if( o == nullptr )
125 return i_ >= o->i_;
126 }
127
128public: /* other */
130 void decrement() { i_--; }
131
133 void increment() { i_++; }
134};
135
136
139class SOB : public SO {
140 bool b_;
142public:
144 explicit SOB( const std::string & p_s ) : b_(p_s == "true") {}
145
147 explicit SOB( bool p_b ) : b_(p_b) {}
148
149public: /* accessor */
151 bool getB() const { return b_; }
152
154 void setB( bool p_b ) { b_ = p_b; }
155
156public: /* virtual */
157 [[nodiscard]] SOB * dup() const override { return new SOB( b_ ); }
158
159 std::string opequal() const override {
160 return b_ ? "true" : "false";
161 }
162
163 OTCode ot() const override { return OTCode::B; }
164
165 std::string type() const override { return "booleantype"; }
166
167 bool equal( const SO * p_other ) const override {
168 auto o = dynamic_cast<const SOB *>( p_other);
169 return o ? b_ == o->b_ : false;
170 }
171
172public: /* other */
174 void negation() { b_ = !b_; }
175};
176
177
184class SOR : public SO {
185 __float128 r_;
187public:
189 explicit SOR( const std::string & p_s ) : r_( stofloat128(p_s) ) {}
190
192 explicit SOR( __float128 p_r ) : r_(p_r) {}
193
194public: /* accessor */
196 __float128 getReal() const { return r_; }
197
199 void setReal( __float128 p_r ) { r_ = p_r; }
200
201public: /* virtual */
202 [[nodiscard]] SOR * dup() const override { return new SOR( r_ ); }
203
209 std::string opequal() const override;
210
211 OTCode ot() const override { return OTCode::R; }
212
213 std::string type() const override { return "realtype"; }
214
215 bool equal( const SO * p_other ) const override {
216 auto o = dynamic_cast<const SOR* >( p_other );
217 return o ? r_ == o->r_ : false;
218 }
219
220 bool gt( const SO * p_other ) const override {
221 auto o = dynamic_cast<const SOR *>( p_other );
222 if( o == nullptr )
224 return r_ > o->r_;
225 }
226
227 bool ge( const SO * p_other ) const override {
228 auto o = dynamic_cast<const SOR *>( p_other );
229 if( o == nullptr )
231 return r_ >= o->r_;
232 }
233
234public: /* other */
236 void trunc() { r_ = ::trunc( r_ ); }
237
239 void ceil() { r_ = ::ceil( r_ ); }
240
242 void floor() { r_ = ::floor( r_ ); }
243};
244
245
249class SON : public SO {
250 std::string name_;
252protected:
253#ifndef DBC_IS_VOID
258 bool invariant() const noexcept override { /* LCOV_EXCL_START */
259 return !name_.empty();
260 } /* LCOV_EXCL_STOP */
261#endif
262
263public:
265 explicit SON( const std::string& p_name, bool p_exec = false ) : SO(p_exec), name_(p_name) {
267 }
268
269public: /* virtual */
270 [[nodiscard]] SON * dup() const override { return new SON( name_, getExec() ); }
271
272 std::string opequal() const override {
273 if( getExec() )
274 return name_;
275 return "/"+name_;
276 }
277
278 OTCode ot() const override {
279 return getExec() ? OTCode::Nx : OTCode::N;
280 }
281
282 std::string type() const override { return "nametype"; }
283
284 bool equal( const SO * p_other ) const override {
285 auto o = dynamic_cast<const SON *>( p_other);
286 return o ? name_ == o->name_ : false;
287 }
288
289 size_t getSize() const override { return name_.size(); }
290
291public: /* other */
297 void load_exec( Context & k ) const;
298};
299
300
307class SOO : public SO {
308 core_t * const core_;
309 const char * const srep_;
311protected:
312#ifndef DBC_IS_VOID
316 bool invariant() const noexcept override { /* LCOV_EXCL_START */
317 if( core_ == nullptr or srep_ == nullptr )
318 return false;
319 return *srep_ != 0;
320 } /* LCOV_EXCL_STOP */
321#endif
322
323public:
325 SOO( core_t * p_fun, const char * p_str ) : core_(p_fun), srep_(p_str) {
327 }
328
329public: /* virtual */
330 [[nodiscard]] SOO * dup() const override { return new SOO( core_, srep_ ); }
331
332 std::string opequal() const override { return "--"+std::string(srep_)+"--"; }
333
334 OTCode ot() const override { return OTCode::O; }
335
336 std::string type() const override { return "operatortype"; }
337
338 bool equal( const SO * p_other ) const override {
339 const SOO* o = dynamic_cast<const SOO*>( p_other);
340 return o ? core_ == o->core_ : false;
341 }
342
343public: /* other */
345 void exec( Context & k ) const { core_(k); }
346};
347
348
353class SOo : public SOO {
354 size_t stackusage_;
356public:
358 SOo( core_t * p_fun, const char * p_str, size_t p_stackusage )
359 : SOO( p_fun, p_str ), stackusage_(p_stackusage) {}
360
361public: /* accessor */
363 auto getStackusage() const { return stackusage_; }
364
365public: /* virtual */
366 OTCode ot() const override { return OTCode::o; }
367};
string to_string(const __int128 p_z)
We need an adapter function, because __int128 isn't supported by std::to_string().
Definition: adapter128.cpp:33
__int128 stoint128(const string &p_s)
We need an adapter function, because __float128 isn't supported by the standard library.
Definition: adapter128.cpp:64
__float128 stofloat128(const string &p_s)
We need an adapter function, because __float128 isn't supported by the standard library.
Definition: adapter128.cpp:75
Adapters for 128 bit versions of standard functions.
The context of execution.
Definition: context.h:37
Semantic Object Boolean.
Definition: sosimp.h:139
SOB(bool p_b)
Ctor.
Definition: sosimp.h:147
void negation()
Negation.
Definition: sosimp.h:174
OTCode ot() const override
Returns an OTCode.
Definition: sosimp.h:163
bool getB() const
Getter for boolean value.
Definition: sosimp.h:151
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition: sosimp.h:159
bool equal(const SO *p_other) const override
Equality.
Definition: sosimp.h:167
void setB(bool p_b)
Setter for boolean value.
Definition: sosimp.h:154
bool b_
The boolean value.
Definition: sosimp.h:140
SOB(const std::string &p_s)
Ctor.
Definition: sosimp.h:144
std::string type() const override
Returns a type name.
Definition: sosimp.h:165
SOB * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sosimp.h:157
Semantic Object Integer.
Definition: sosimp.h:83
bool equal(const SO *p_other) const override
Equality.
Definition: sosimp.h:109
SOI * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sosimp.h:101
OTCode ot() const override
Returns an OTCode.
Definition: sosimp.h:105
SOI(const std::string &p_s)
Ctor.
Definition: sosimp.h:88
void increment()
Increment.
Definition: sosimp.h:133
void setInteger(__int128 p_i)
Setter for the integer value.
Definition: sosimp.h:98
void decrement()
Decrement.
Definition: sosimp.h:130
__int128 i_
The 128 bit integer.
Definition: sosimp.h:84
SOI(__int128 p_i)
Ctor.
Definition: sosimp.h:91
std::string type() const override
Returns a type name.
Definition: sosimp.h:107
bool gt(const SO *p_other) const override
Greater than.
Definition: sosimp.h:114
bool ge(const SO *p_other) const override
Greater or equal.
Definition: sosimp.h:121
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition: sosimp.h:103
__int128 getInteger() const
Getter for the integer value.
Definition: sosimp.h:95
Semantic Object nuLl.
Definition: sosimp.h:61
SOL * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sosimp.h:63
OTCode ot() const override
Returns an OTCode.
Definition: sosimp.h:69
std::string type() const override
Returns a type name.
Definition: sosimp.h:71
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition: sosimp.h:65
std::string opequalequal() const override
For operators '==' and 'pstack'.
Definition: sosimp.h:67
bool equal(const SO *p_other) const override
Equality.
Definition: sosimp.h:73
Semantic Object Mark.
Definition: sosimp.h:40
std::string type() const override
Returns a type name.
Definition: sosimp.h:50
SOM * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sosimp.h:42
std::string opequalequal() const override
For operators '==' and 'pstack'.
Definition: sosimp.h:46
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition: sosimp.h:44
bool equal(const SO *p_other) const override
Equality.
Definition: sosimp.h:52
OTCode ot() const override
Returns an OTCode.
Definition: sosimp.h:48
Semantic Object Name.
Definition: sosimp.h:249
SON * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sosimp.h:270
bool invariant() const noexcept override
Checks class invariants.
Definition: sosimp.h:258
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition: sosimp.h:289
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition: sosimp.h:272
OTCode ot() const override
Returns an OTCode.
Definition: sosimp.h:278
bool equal(const SO *p_other) const override
Equality.
Definition: sosimp.h:284
SON(const std::string &p_name, bool p_exec=false)
Ctor.
Definition: sosimp.h:265
std::string type() const override
Returns a type name.
Definition: sosimp.h:282
std::string name_
The name.
Definition: sosimp.h:250
void load_exec(Context &k) const
Look up a name and executes it.
Definition: sosimp.cpp:34
Semantic Object core code Operator.
Definition: sosimp.h:307
OTCode ot() const override
Returns an OTCode.
Definition: sosimp.h:334
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition: sosimp.h:332
const char *const srep_
A name just for representation.
Definition: sosimp.h:309
SOO * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sosimp.h:330
bool invariant() const noexcept override
Checks class invariants.
Definition: sosimp.h:316
core_t *const core_
The core code, a C++ implementation of the operator.
Definition: sosimp.h:308
SOO(core_t *p_fun, const char *p_str)
Ctor.
Definition: sosimp.h:325
std::string type() const override
Returns a type name.
Definition: sosimp.h:336
void exec(Context &k) const
Call the core code.
Definition: sosimp.h:345
bool equal(const SO *p_other) const override
Equality.
Definition: sosimp.h:338
Semantic Object Real.
Definition: sosimp.h:184
void setReal(__float128 p_r)
Setter for real value.
Definition: sosimp.h:199
SOR(const std::string &p_s)
Ctor.
Definition: sosimp.h:189
bool ge(const SO *p_other) const override
Greater or equal.
Definition: sosimp.h:227
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition: sosimp.cpp:30
__float128 r_
The real value as decimal number.
Definition: sosimp.h:185
__float128 getReal() const
Getter for real value.
Definition: sosimp.h:196
bool equal(const SO *p_other) const override
Equality.
Definition: sosimp.h:215
void trunc()
Direct trunc().
Definition: sosimp.h:236
SOR(__float128 p_r)
Ctor.
Definition: sosimp.h:192
void floor()
Direct floor().
Definition: sosimp.h:242
bool gt(const SO *p_other) const override
Greater than.
Definition: sosimp.h:220
void ceil()
Direct ceil().
Definition: sosimp.h:239
std::string type() const override
Returns a type name.
Definition: sosimp.h:213
SOR * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sosimp.h:202
OTCode ot() const override
Returns an OTCode.
Definition: sosimp.h:211
Semantic Object.
Definition: so.h:54
bool getExec() const
Getter for exec_.
Definition: so.h:70
Semantic Object core code Operator unregistered section.
Definition: sosimp.h:353
auto getStackusage() const
Getter for stackusage_.
Definition: sosimp.h:363
OTCode ot() const override
Returns an OTCode.
Definition: sosimp.h:366
size_t stackusage_
The number of objects needed on the execution stack for execution.
Definition: sosimp.h:354
SOo(core_t *p_fun, const char *p_str, size_t p_stackusage)
Ctor.
Definition: sosimp.h:358
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition: dbc.h:88
void opErrExit(OpError p_err, const std::string &p_details, const std::source_location p_location)
Operator error message to interpreter cout_ and exit( EC_OPERATOR ).
Definition: error.cpp:28
@ typecheck
PS operator error typecheck.
Definition: error.h:33
Miscellaneous definitions and functions.
void(Context &) core_t
A shortcut for the core code functions, the C++ implementations of the operators.
Definition: helper.h:88
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition: so.h:29
@ M
SOM.
@ N
SON literal.
@ B
SOB.
@ Nx
SON executable.
@ L
SOL.
@ o
SOo.
@ I
SOI.
@ R
SOR.
@ O
SOO.