Leonardus
Loading...
Searching...
No Matches
sosimp.h
Go to the documentation of this file.
1
17#pragma once
18
19// Inc Library
20#include <string>
21#include <memory>
22
23// Inc HAA
24#include "dbc.h"
25#include "helper.h"
26#include "adapter128.h"
27
28// Inc Rich
29#include "so.h"
30
31
32/* ******************************************************************* */
33/* Simple Objects */
34/* SO's that store their data without indirect referencing */
35/* ******************************************************************* */
36
37
40class SOM : public SO {
41
42public:
44 SOM() {}
45
46
48 ~SOM() override {}
49
50
51 SOM( const SOM & ) = delete;
52 SOM( SOM && ) = delete;
53 SOM & operator=( const SOM & ) = delete;
54 SOM & operator=( SOM && ) = delete;
55
56
57public: /* virtual */
58 [[nodiscard]] SOM * dup() const override { return new SOM; }
59
60
61 std::string opequal() const override { return "--nostringval--"; }
62
63
64 std::string opequalequal() const override { return "mark"; }
65
66
67 OTCode ot() const override { return OTCode::M; }
68
69
70 std::string type() const override { return "marktype"; }
71
72
75 bool equal( const SO * p_other ) const override {
76 return dynamic_cast<const SOM *>( p_other) != nullptr; // All SOM are equal
77 }
78
79};
80
81
84class SOL : public SO {
85
86public:
88 SOL() {}
89
90
92 ~SOL() override {}
93
94
95 SOL( const SOL & ) = delete;
96 SOL( SOL && ) = delete;
97 SOL & operator=( const SOL & ) = delete;
98 SOL & operator=( SOL && ) = delete;
99
100
101public: /* virtual */
102 SOL * dup() const override { return new SOL; }
103
104
105 std::string opequal() const override { return "--nostringval--"; }
106
107
108 std::string opequalequal() const override { return "null"; }
109
110
111 OTCode ot() const override { return OTCode::L; }
112
113
114 std::string type() const override { return "nulltype"; }
115
116
119 bool equal( const SO * p_other ) const override {
120 return dynamic_cast<const SOL *>( p_other) != nullptr; // All SOL are equal
121 }
122
123};
124
125
130class SOI : public SO {
131
132 __int128 i_;
134public:
140 explicit SOI( const std::string & p_s ) : i_(string_to___int128(p_s)) {}
141
142
145 explicit SOI( __int128 p_i ) : i_(p_i) {}
146
147
149 ~SOI() override {}
150
151
152 SOI() = delete;
153 SOI( const SOI & ) = delete;
154 SOI( SOI && ) = delete;
155 SOI & operator=( const SOI & ) = delete;
156 SOI & operator=( SOI && ) = delete;
157
158
159public: /* accessor */
161 __int128 getInteger() const { return i_; }
162
163
165 void setInteger( __int128 p_i ) { i_=p_i; }
166
167
168public: /* virtual */
169 [[nodiscard]] SOI * dup() const override { return new SOI( i_ ); }
170
171
172 std::string opequal() const override { return __int128_to_string( i_ ); }
173
174
175 OTCode ot() const override { return OTCode::I; }
176
177
178 std::string type() const override { return "integertype"; }
179
180
183 bool equal( const SO * p_other ) const override {
184 auto o = dynamic_cast<const SOI*>( p_other );
185 return o ? i_ == o->i_ : false;
186 }
187
188
191 bool gt( OpError & p_ec, const SO * p_other ) const override {
192 auto o = dynamic_cast<const SOI*>( p_other );
193 if( o == nullptr ) {
194 p_ec = typecheck;
195 return false;
196 }
197 p_ec = success;
198 return i_ > o->i_;
199 }
200
201
204 bool ge( OpError & p_ec, const SO * p_other ) const override {
205 auto o = dynamic_cast<const SOI*>( p_other );
206 if( o == nullptr ) {
207 p_ec = typecheck;
208 return false;
209 }
210 p_ec = success;
211 return i_ >= o->i_;
212 }
213
214
215public: /* other */
217 void decrement() { i_--; }
218
219
221 void increment() { i_++; }
222
223};
224
225
228class SOB : public SO {
229
230 bool b_;
232public:
237 explicit SOB( const std::string & p_s ) : b_(p_s == "true") {}
238
239
242 explicit SOB( bool p_b ) : b_(p_b) {}
243
244
246 ~SOB() override {}
247
248
249 SOB() = delete;
250 SOB( const SOB & ) = delete;
251 SOB( SOB && ) = delete;
252 SOB & operator=( const SOB & ) = delete;
253 SOB & operator=( SOB && ) = delete;
254
255
256public: /* accessor */
258 bool getB() const { return b_; }
259
260
262 void setB( bool p_b ) { b_ = p_b; }
263
264
265public: /* virtual */
266 [[nodiscard]] SOB * dup() const override { return new SOB( b_ ); }
267
268
269 std::string opequal() const override {
270 return b_ ? "true" : "false";
271 }
272
273
274 OTCode ot() const override { return OTCode::B; }
275
276
277 std::string type() const override { return "booleantype"; }
278
279
282 bool equal( const SO * p_other ) const override {
283 auto o = dynamic_cast<const SOB *>( p_other);
284 return o ? b_ == o->b_ : false;
285 }
286
287
288public: /* other */
290 void negation() { b_ = !b_; }
291
292};
293
294
301class SOR : public SO {
302
303 __float128 r_;
306public:
311 explicit SOR( const std::string & p_s ) : r_( string_to___float128(p_s) ) {}
312
313
316 explicit SOR( __float128 p_r ) : r_(p_r) {}
317
318
320 ~SOR() override {}
321
322
323 SOR() = delete;
324 SOR( const SOR & ) = delete;
325 SOR( SOR && ) = delete;
326 SOR & operator=( const SOR & ) = delete;
327 SOR & operator=( SOR && ) = delete;
328
329
330public: /* accessor */
332 __float128 getReal() const { return r_; }
333
334
336 void setReal( __float128 p_r ) { r_ = p_r; }
337
338
339public: /* virtual */
340 [[nodiscard]] SOR * dup() const override { return new SOR( r_ ); }
341
342
348 std::string opequal() const override;
349
350
351 OTCode ot() const override { return OTCode::R; }
352
353
354 std::string type() const override { return "realtype"; }
355
356
359 bool equal( const SO * p_other ) const override {
360 auto o = dynamic_cast<const SOR* >( p_other );
361 return o ? r_ == o->r_ : false;
362 }
363
364
367 bool gt( OpError & p_ec, const SO * p_other ) const override {
368 auto o = dynamic_cast<const SOR *>( p_other );
369 if( o == nullptr ) {
370 p_ec = typecheck;
371 return false;
372 }
373 p_ec = success;
374 return r_ > o->r_;
375 }
376
377
380 bool ge( OpError & p_ec, const SO * p_other ) const override {
381 auto o = dynamic_cast<const SOR *>( p_other );
382 if( o == nullptr ) {
383 p_ec = typecheck;
384 return false;
385 }
386 p_ec = success;
387 return r_ >= o->r_;
388 }
389
390
391public: /* other */
393 void trunc() { r_ = adapter128::trunc( r_ ); }
394
395
397 void ceil() { r_ = adapter128::ceil( r_ ); }
398
399
401 void floor() { r_ = adapter128::floor( r_ ); }
402
403
409 if( std::abs(r_) > std::numeric_limits<__float128>::min() ) {
410 r_ = 1/r_;
411 return success;
412 }
413 return undefinedresult;
414 }
415};
416
417
421class SON : public SO {
422
423 std::string name_;
426protected:
427#ifndef DBC_IS_VOID
432 bool invariant() const noexcept override { /* LCOV_EXCL_START */
433 return !name_.empty();
434 } /* LCOV_EXCL_STOP */
435#endif
436
437
438public:
443 explicit SON( std::string p_name, bool p_exec = false )
444 : SO( p_exec ),
445 name_( std::move(p_name) ) {
447 }
448
449
451 SON( const SON & p_other )
452 : SO(p_other),
453 name_(p_other.name_) {
455 }
456
457
459 ~SON() override {}
460
461
462 SON() = delete;
463 SON( SON && ) = delete;
464 SON & operator=( const SON & ) = delete;
465 SON & operator=( SON && ) = delete;
466
467
468public: /* accessor */
470 std::string getName() const { return name_; }
471
472
473public: /* virtual */
474 [[nodiscard]] SON * dup() const override { return new SON( *this ); }
475
476
477 std::string opequal() const override {
478 if( getExec() )
479 return name_;
480 return "/"+name_;
481 }
482
483
484 OTCode ot() const override {
485 return getExec() ? OTCode::Nx : OTCode::N;
486 }
487
488
489 std::string type() const override { return "nametype"; }
490
491
494 bool equal( const SO * p_other ) const override {
495 auto o = dynamic_cast<const SON *>( p_other);
496 return o ? name_ == o->name_ : false;
497 }
498
499
500 size_t getSize() const override { return name_.size(); }
501
502};
503
504
512class SOO : public SO {
513
514 core_t * const core_;
515 const char * const srep_;
518protected:
519#ifndef DBC_IS_VOID
523 bool invariant() const noexcept override { /* LCOV_EXCL_START */
524 if( core_ == nullptr or srep_ == nullptr )
525 return false;
526 return *srep_ != 0;
527 } /* LCOV_EXCL_STOP */
528#endif
529
530
531public:
536 SOO( core_t * p_fun, const char * p_str )
537 : core_(p_fun),
538 srep_(p_str) {
540 }
541
542
544 ~SOO() override {
546 }
547
548
549 SOO() = delete;
550 SOO( const SOO & ) = delete;
551 SOO( SOO && ) = delete;
552 SOO & operator=( const SOO & ) = delete;
553 SOO & operator=( SOO && ) = delete;
554
555
556public: /* virtual */
557 [[nodiscard]] SOO * dup() const override { return new SOO( core_, srep_ ); }
558
559
560 std::string opequal() const override { return "--"+std::string(srep_)+"--"; }
561
562
563 OTCode ot() const override { return OTCode::O; }
564
565
566 std::string type() const override { return "operatortype"; }
567
568
571 bool equal( const SO * p_other ) const override {
572 const SOO* o = dynamic_cast<const SOO*>( p_other);
573 return o ? core_ == o->core_ : false;
574 }
575
576
577public: /* other */
580 void exec( Context & k ) const { core_(k); }
581
582};
583
584
590class SOo : public SOO {
591
593 size_t stackusage_;
594
595
596public:
602 SOo( core_t * p_fun, const char * p_str, size_t p_stackusage )
603 : SOO( p_fun, p_str ),
604 stackusage_(p_stackusage) {
605 }
606
607
609 ~SOo() override {}
610
611
612 SOo() = delete;
613 SOo( const SOo & ) = delete;
614 SOo( SOo && ) = delete;
615 SOo & operator=( const SOo & ) = delete;
616 SOo & operator=( SOo && ) = delete;
617
618
619public: /* accessor */
621 auto getStackusage() const { return stackusage_; }
622
623
624public: /* virtual */
625 OTCode ot() const override { return OTCode::o; }
626
627};
628
__float128 string_to___float128(const string &p_s)
We need an adapter function, because __float128 isn't supported by the standard library.
Definition adapter128.cpp:81
string __int128_to_string(const __int128 p_z)
We need an adapter function, because __int128 isn't supported by std::to_string().
Definition adapter128.cpp:35
__int128 string_to___int128(const string &p_s)
We need an adapter function, because __int128 isn't supported by the standard library.
Definition adapter128.cpp:70
Adapters for 128 bit versions of standard functions.
The context of execution.
Definition context.h:45
Semantic Object Boolean.
Definition sosimp.h:228
~SOB() override
Dtor.
Definition sosimp.h:246
SOB(bool p_b)
Ctor from a boolean.
Definition sosimp.h:242
void negation()
In place negation.
Definition sosimp.h:290
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:274
bool getB() const
Getter for boolean value.
Definition sosimp.h:258
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:269
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:282
void setB(bool p_b)
Setter for boolean value.
Definition sosimp.h:262
bool b_
The boolean value.
Definition sosimp.h:230
SOB(const std::string &p_s)
Ctor from a string representation.
Definition sosimp.h:237
std::string type() const override
Returns a type name.
Definition sosimp.h:277
SOB * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:266
Semantic Object Integer.
Definition sosimp.h:130
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:183
SOI * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:169
bool ge(OpError &p_ec, const SO *p_other) const override
Greater or equal.
Definition sosimp.h:204
bool gt(OpError &p_ec, const SO *p_other) const override
Greater than.
Definition sosimp.h:191
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:175
SOI(const std::string &p_s)
Ctor from a string representation.
Definition sosimp.h:140
void increment()
In place increment.
Definition sosimp.h:221
void setInteger(__int128 p_i)
Setter for the integer value.
Definition sosimp.h:165
void decrement()
In place decrement.
Definition sosimp.h:217
__int128 i_
The 128 bit integer.
Definition sosimp.h:132
SOI(__int128 p_i)
Ctor from an 128 bit integer.
Definition sosimp.h:145
~SOI() override
Dtor.
Definition sosimp.h:149
std::string type() const override
Returns a type name.
Definition sosimp.h:178
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:172
__int128 getInteger() const
Getter for the integer value.
Definition sosimp.h:161
Semantic Object nuLl.
Definition sosimp.h:84
SOL * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:102
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:111
~SOL() override
Dtor.
Definition sosimp.h:92
std::string type() const override
Returns a type name.
Definition sosimp.h:114
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:105
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sosimp.h:108
SOL()
Ctor.
Definition sosimp.h:88
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:119
Semantic Object Mark.
Definition sosimp.h:40
std::string type() const override
Returns a type name.
Definition sosimp.h:70
~SOM() override
Dtor.
Definition sosimp.h:48
SOM * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:58
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sosimp.h:64
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:61
SOM()
Ctor.
Definition sosimp.h:44
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:75
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:67
Semantic Object Name.
Definition sosimp.h:421
SON(std::string p_name, bool p_exec=false)
Ctor from a string as name.
Definition sosimp.h:443
SON * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:474
std::string getName() const
Getter for name_.
Definition sosimp.h:470
SON(const SON &p_other)
Copy ctor.
Definition sosimp.h:451
bool invariant() const noexcept override
Checks class invariants.
Definition sosimp.h:432
size_t getSize() const override
Returns the size of the SO.
Definition sosimp.h:500
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:477
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:484
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:494
std::string type() const override
Returns a type name.
Definition sosimp.h:489
std::string name_
The name.
Definition sosimp.h:423
~SON() override
Dtor.
Definition sosimp.h:459
Semantic Object core code Operator.
Definition sosimp.h:512
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:563
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:560
const char *const srep_
A name just for representation.
Definition sosimp.h:515
SOO * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:557
bool invariant() const noexcept override
Checks class invariants.
Definition sosimp.h:523
~SOO() override
Dtor.
Definition sosimp.h:544
core_t *const core_
The core code, a C++ implementation of the operator.
Definition sosimp.h:514
SOO(core_t *p_fun, const char *p_str)
Ctor.
Definition sosimp.h:536
std::string type() const override
Returns a type name.
Definition sosimp.h:566
void exec(Context &k) const
Call the core code.
Definition sosimp.h:580
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:571
Semantic Object Real.
Definition sosimp.h:301
void setReal(__float128 p_r)
Setter for real value.
Definition sosimp.h:336
SOR(const std::string &p_s)
Ctor from a string representation.
Definition sosimp.h:311
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'. Reals are formated for output...
Definition sosimp.cpp:28
OpError reciprocal()
In place reciprocal.
Definition sosimp.h:408
~SOR() override
Dtor.
Definition sosimp.h:320
__float128 r_
The real value as decimal number.
Definition sosimp.h:303
bool gt(OpError &p_ec, const SO *p_other) const override
Greater than.
Definition sosimp.h:367
bool ge(OpError &p_ec, const SO *p_other) const override
Greater or equal.
Definition sosimp.h:380
__float128 getReal() const
Getter for real value.
Definition sosimp.h:332
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:359
void trunc()
In place trunc().
Definition sosimp.h:393
SOR(__float128 p_r)
Ctor from a float.
Definition sosimp.h:316
void floor()
In place floor().
Definition sosimp.h:401
void ceil()
In place ceil().
Definition sosimp.h:397
std::string type() const override
Returns a type name.
Definition sosimp.h:354
SOR * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:340
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:351
Semantic Object.
Definition so.h:60
bool getExec() const
Getter for exec_.
Definition so.h:93
Semantic Object core code Operator unregistered section.
Definition sosimp.h:590
auto getStackusage() const
Getter for stackusage_.
Definition sosimp.h:621
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:625
size_t stackusage_
The number of objects needed on the execution stack for execution.
Definition sosimp.h:593
~SOo() override
Dtor.
Definition sosimp.h:609
SOo(core_t *p_fun, const char *p_str, size_t p_stackusage)
Ctor.
Definition sosimp.h:602
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:121
OpError
Operator error code enum.
Definition error.h:30
@ undefinedresult
operator error undefinedresult
Definition error.h:39
@ success
No operator error.
Definition error.h:32
@ typecheck
operator error typecheck
Definition error.h:36
Miscellaneous definitions and functions.
void(Context &) core_t
A shortcut for the core code function type.
Definition helper.h:112
__float128 floor(__float128 p_r)
__float128 overload of floor().
Definition adapter128.cpp:180
__float128 ceil(__float128 p_r)
__float128 overload of ceil().
Definition adapter128.cpp:175
__float128 trunc(__float128 p_r)
__float128 overload of trunc().
Definition adapter128.cpp:185
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:35
@ M
SOM.
@ N
SON literal.
@ B
SOB.
@ Nx
SON executable.
@ L
SOL.
@ o
SOo.
@ I
SOI.
@ R
SOR.
@ O
SOO.