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 <string_view>
22#include <memory>
23
24// Inc Pure
25#include "dbc.h"
26#include "helper.h"
27#include "adapter128.h"
28#include "conversion.h"
29#include "registry.h"
30
31// Inc Rich
32#include "so.h"
33
34
35/* ******************************************************************* */
36/* Simple Objects */
37/* SO's that store their data without indirect referencing */
38/* ******************************************************************* */
39
40
43class SOM : public SO {
44
45public:
46
48 SOM() = default;
49
50
52 ~SOM() override = default;
53
54
55 SOM( const SOM & ) = delete;
56 SOM( SOM && ) = delete;
57 SOM & operator=( const SOM & ) = delete;
58 SOM & operator=( SOM && ) = delete;
59
60public: /* virtual */
61
62 [[nodiscard]] SOM * dup() const override { return new SOM; }
63
64
65 std::string opequal() const override { return "--nostringval--"; }
66
67
68 std::string opequalequal() const override { return "mark"; }
69
70
71 OTCode ot() const override { return OTCode::M; }
72
73
74 std::string_view type() const override { return "marktype"; }
75
76
79 bool equal( const SO * p_other ) const override {
80 DBC_PRE( p_other != nullptr );
81
82 return p_other->ot() == OTCode::M; // All SOM are equal
83 }
84
85};
86
87
90class SOL : public SO {
91
92public:
93
95 SOL() = default;
96
97
99 ~SOL() override = default;
100
101
102 SOL( const SOL & ) = delete;
103 SOL( SOL && ) = delete;
104 SOL & operator=( const SOL & ) = delete;
105 SOL & operator=( SOL && ) = delete;
106
107public: /* virtual */
108
109 SOL * dup() const override { return new SOL; }
110
111
112 std::string opequal() const override { return "--nostringval--"; }
113
114
115 std::string opequalequal() const override { return "null"; }
116
117
118 OTCode ot() const override { return OTCode::L; }
119
120
121 std::string_view type() const override { return "nulltype"; }
122
123
126 bool equal( const SO * p_other ) const override {
127 DBC_PRE( p_other != nullptr );
128
129 return p_other->ot() == OTCode::L; // All SOL are equal
130 }
131
132};
133
134
139class SOI : public SO {
140
141 __int128 i_;
143public:
144
150 explicit SOI( const std::string & p_s ) : i_(conversion::string_to___int128(p_s)) {}
151
152
155 explicit SOI( __int128 p_i ) : i_(p_i) {}
156
157
159 ~SOI() override = default;
160
161
162 SOI() = delete;
163 SOI( const SOI & ) = delete;
164 SOI( SOI && ) = delete;
165 SOI & operator=( const SOI & ) = delete;
166 SOI & operator=( SOI && ) = delete;
167
168public: /* accessor */
169
171 __int128 getInteger() const { return i_; }
172
173
175 void setInteger( __int128 p_i ) { i_=p_i; }
176
177public: /* virtual */
178
179 [[nodiscard]] SOI * dup() const override { return new SOI( i_ ); }
180
181
182 std::string opequal() const override { return conversion::__int128_to_string( i_ ); }
183
184
185 OTCode ot() const override { return OTCode::I; }
186
187
188 std::string_view type() const override { return "integertype"; }
189
190
193 bool equal( const SO * p_other ) const override {
194 DBC_PRE( p_other != nullptr );
195
196 if( p_other->ot() != ot() )
197 return false;
198 const auto o = static_cast<const SOI *>( p_other );
199 return i_ == o->i_;
200 }
201
202
206 bool gt( OpError & p_ec, const SO * p_other ) const override {
207 DBC_PRE( p_other != nullptr );
208
209 if( p_other->ot() != OTCode::I ) {
210 p_ec = typecheck;
211 return false;
212 }
213 const auto o = static_cast<const SOI *>( p_other );
214 p_ec = success;
215 return i_ > o->i_;
216 }
217
218
222 bool ge( OpError & p_ec, const SO * p_other ) const override {
223 DBC_PRE( p_other != nullptr );
224
225 if( p_other->ot() != OTCode::I ) {
226 p_ec = typecheck;
227 return false;
228 }
229 const auto o = static_cast<const SOI *>( p_other );
230 p_ec = success;
231 return i_ >= o->i_;
232 }
233
234public: /* other */
235
237 void decrement__1() { i_--; }
238
239
241 void increment__1() { i_++; }
242
243
245 void sqr__1() { i_ *= i_; }
246
247
249 void half__1() { i_ /= 2; }
250
251};
252
253
256class SOB : public SO {
257
258 bool b_;
260public:
261
266 explicit SOB( const std::string & p_s ) : b_(p_s == "true") {}
267
268
271 explicit SOB( bool p_b ) : b_(p_b) {}
272
273
275 ~SOB() override = default;
276
277
278 SOB() = delete;
279 SOB( const SOB & ) = delete;
280 SOB( SOB && ) = delete;
281 SOB & operator=( const SOB & ) = delete;
282 SOB & operator=( SOB && ) = delete;
283
284public: /* accessor */
285
287 bool getB() const { return b_; }
288
289
291 void setB( bool p_b ) { b_ = p_b; }
292
293public: /* virtual */
294
295 [[nodiscard]] SOB * dup() const override { return new SOB( b_ ); }
296
297
298 std::string opequal() const override {
299 return b_ ? "true" : "false";
300 }
301
302
303 OTCode ot() const override { return OTCode::B; }
304
305
306 std::string_view type() const override { return "booleantype"; }
307
308
311 bool equal( const SO * p_other ) const override {
312 DBC_PRE( p_other != nullptr );
313
314 if( p_other->ot() != ot() )
315 return false;
316 const auto o = static_cast<const SOB *>( p_other);
317 return b_ == o->b_;
318 }
319
320public: /* other */
321
323 void negation__1() { b_ = !b_; }
324
325};
326
327
334class SOR : public SO {
335
336 __float128 r_;
338protected:
339
340#ifndef DBC_IS_VOID
344 bool invariant() const noexcept override { /* LCOV_EXCL_START */
345 return adapter128::isfinite( r_ );
346 } /* LCOV_EXCL_STOP */
347#endif
348
349public:
350
355 explicit SOR( const std::string & p_s ) : r_( conversion::string_to___float128(p_s) ) {
356 DBC_INV_CTOR(SOR); // This also checks the precondition.
357 }
358
359
362 explicit SOR( __float128 p_r ) : r_(p_r) {
364 }
365
366
368 ~SOR() override = default;
369
370
371 SOR() = delete;
372 SOR( const SOR & ) = delete;
373 SOR( SOR && ) = delete;
374 SOR & operator=( const SOR & ) = delete;
375 SOR & operator=( SOR && ) = delete;
376
377public: /* accessor */
378
380 __float128 getReal() const { return r_; }
381
382
387 bool setReal( __float128 p_r ) {
389
390 if( ! adapter128::isfinite(p_r) )
391 return false;
392 r_ = p_r;
393 return true;
394 }
395
396public: /* virtual */
397
398 [[nodiscard]] SOR * dup() const override { return new SOR( r_ ); }
399
400
406 std::string opequal() const override;
407
408
409 OTCode ot() const override { return OTCode::R; }
410
411
412 std::string_view type() const override { return "realtype"; }
413
414
417 bool equal( const SO * p_other ) const override {
418 DBC_PRE( p_other != nullptr );
419
420 if( p_other->ot() != ot() )
421 return false;
422 const auto o = static_cast<const SOR *>( p_other );
423 return r_ == o->r_;
424 }
425
426
430 bool gt( OpError & p_ec, const SO * p_other ) const override {
431 DBC_PRE( p_other != nullptr );
432
433 if( p_other->ot() != OTCode::R ) {
434 p_ec = typecheck;
435 return false;
436 }
437 const auto o = static_cast<const SOR *>( p_other );
438 p_ec = success;
439 return r_ > o->r_;
440 }
441
442
446 bool ge( OpError & p_ec, const SO * p_other ) const override {
447 DBC_PRE( p_other != nullptr );
448
449 if( p_other->ot() != OTCode::R ) {
450 p_ec = typecheck;
451 return false;
452 }
453 const auto o = static_cast<const SOR *>( p_other );
454 p_ec = success;
455 return r_ >= o->r_;
456 }
457
458public: /* other */
459
461 void half__1() {
462 r_ /= 2;
463
464 DBC_INV;
465 }
466
467
473
474 if( std::abs(r_) > SOR_MAX_SQR )
475 return undefinedresult;
476 r_ *= r_;
477 return success;
478 }
479
480
482 void trunc__1() {
484
485 DBC_INV;
486 }
487
488
490 void ceil__1() {
492
493 DBC_INV;
494 }
495
496
498 void floor__1() {
500
501 DBC_INV;
502 }
503
504
512
513 if( std::abs(r_) > std::numeric_limits<__float128>::min() ) {
514 r_ = 1/r_;
515 return success;
516 }
517 return undefinedresult;
518 }
519};
520
521
525class SON : public SO {
526
527 std::string name_;
529protected:
530
531#ifndef DBC_IS_VOID
536 bool invariant() const noexcept override { /* LCOV_EXCL_START */
537 return !name_.empty();
538 } /* LCOV_EXCL_STOP */
539#endif
540
541public:
542
547 explicit SON( std::string p_name, bool p_exec = false )
548 : SO( p_exec ),
549 name_( std::move(p_name) ) {
551 }
552
553
555 SON( const SON & p_other )
556 : SO(p_other),
557 name_(p_other.name_) {
559 }
560
561
563 ~SON() override = default;
564
565
566 SON() = delete;
567 SON( SON && ) = delete;
568 SON & operator=( const SON & ) = delete;
569 SON & operator=( SON && ) = delete;
570
571public: /* accessor */
572
574 std::string getName() const { return name_; }
575
576public: /* virtual */
577
578 [[nodiscard]] SON * dup() const override { return new SON( *this ); }
579
580
581 std::string opequalequal() const override {
582 if( getExec() )
583 return name_;
584 return "/"+name_;
585 }
586
587
588 std::string opequal() const override {
589 return name_;
590 }
591
592
593 OTCode ot() const override {
594 return getExec() ? OTCode::Nx : OTCode::N;
595 }
596
597
598 std::string_view type() const override { return "nametype"; }
599
600
603 bool equal( const SO * p_other ) const override {
604 DBC_PRE( p_other != nullptr );
605
606 if( !otcodeIsName( p_other->ot() ) )
607 return false;
608 const auto o = static_cast<const SON *>( p_other );
609 return name_ == o->name_;
610 }
611
612
613 size_t getSize() const override { return name_.size(); }
614
615public: /* other */
616
621 static bool otcodeIsName( OTCode p_code ) {
622 return p_code <= OTCode::Nx;
623 }
624
625};
626
627
634class SOO : public SO {
635
636 core_t * const core_;
638protected:
639
640#ifndef DBC_IS_VOID
644 bool invariant() const noexcept override { /* LCOV_EXCL_START */
645 if( core_ == nullptr )
646 return false;
647 return Registry::lookUp( core_ );
648 } /* LCOV_EXCL_STOP */
649#endif
650
651public:
652
657 explicit SOO( core_t * p_fun )
658 : core_(p_fun) {
659 DBC_PRE( Registry::lookUp( p_fun ) );
660
662 }
663
664
666 ~SOO() override {
668 }
669
670
671 SOO() = delete;
672 SOO( const SOO & ) = delete;
673 SOO( SOO && ) = delete;
674 SOO & operator=( const SOO & ) = delete;
675 SOO & operator=( SOO && ) = delete;
676
677public: /* virtual */
678
679 [[nodiscard]] SOO * dup() const override { return new SOO( core_ ); }
680
681
682 std::string opequal() const override {
683 return std::string( Registry::lookUpName( core_) );
684 }
685
686
687 std::string opequalequal() const override {
688 return "--" + opequal() + "--";
689 }
690
691
692 OTCode ot() const override { return OTCode::O; }
693
694
695 std::string_view type() const override { return "operatortype"; }
696
697
700 bool equal( const SO * p_other ) const override {
701 DBC_PRE( p_other != nullptr );
702
703 if( !otcodeIsCoreCode( p_other->ot() ) )
704 return false;
705 const auto o = static_cast<const SOO *>( p_other );
706 return core_ == o->core_;
707 }
708
709public: /* other */
710
713 void exec( Context & k ) const { core_(k); }
714
715
720 static bool otcodeIsCoreCode( OTCode p_code ) {
721 return p_code == OTCode::O || p_code == OTCode::o;
722 }
723
724};
725
726
733class SOo : public SOO {
734
736 size_t stackusage_;
737
738public:
739
744 explicit SOo( core_t * p_fun )
745 : SOO( p_fun ), // This checks the precondition
746 stackusage_( Registry::lookUpStackusage( p_fun ) ) {
747 }
748
749
751 ~SOo() override = default;
752
753
754 SOo() = delete;
755 SOo( const SOo & ) = delete;
756 SOo( SOo && ) = delete;
757 SOo & operator=( const SOo & ) = delete;
758 SOo & operator=( SOo && ) = delete;
759
760public: /* accessor */
761
763 auto getStackusage() const { return stackusage_; }
764
765public: /* virtual */
766
767 OTCode ot() const override { return OTCode::o; }
768
769};
770
Adapters for 128 bit versions of standard functions.
The context of execution.
Definition context.h:46
Central registry for all core code entries.
Definition registry.h:36
static std::string_view lookUpName(core_t *p_core)
Finds the name of a core code.
Definition registry.cpp:46
static bool lookUp(core_t *p_core)
Checks if core code is in the static table.
Definition registry.h:69
Semantic Object Boolean.
Definition sosimp.h:256
std::string_view type() const override
Returns a type name.
Definition sosimp.h:306
SOB(bool p_b)
Ctor from a boolean.
Definition sosimp.h:271
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:303
void negation__1()
In place negation.
Definition sosimp.h:323
bool getB() const
Getter for boolean value.
Definition sosimp.h:287
~SOB() override=default
Dtor.
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:298
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:311
void setB(bool p_b)
Setter for boolean value.
Definition sosimp.h:291
bool b_
The boolean value.
Definition sosimp.h:258
SOB(const std::string &p_s)
Ctor from a string representation.
Definition sosimp.h:266
SOB * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:295
Semantic Object Integer.
Definition sosimp.h:139
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:193
SOI * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:179
~SOI() override=default
Dtor.
bool ge(OpError &p_ec, const SO *p_other) const override
Greater or equal.
Definition sosimp.h:222
void half__1()
In place half value.
Definition sosimp.h:249
bool gt(OpError &p_ec, const SO *p_other) const override
Greater than.
Definition sosimp.h:206
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:185
std::string_view type() const override
Returns a type name.
Definition sosimp.h:188
void sqr__1()
In place sqr.
Definition sosimp.h:245
SOI(const std::string &p_s)
Ctor from a string representation.
Definition sosimp.h:150
void setInteger(__int128 p_i)
Setter for the integer value.
Definition sosimp.h:175
__int128 i_
The 128 bit integer.
Definition sosimp.h:141
void increment__1()
In place increment.
Definition sosimp.h:241
SOI(__int128 p_i)
Ctor from an 128 bit integer.
Definition sosimp.h:155
void decrement__1()
In place decrement.
Definition sosimp.h:237
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:182
__int128 getInteger() const
Getter for the integer value.
Definition sosimp.h:171
Semantic Object nuLl.
Definition sosimp.h:90
SOL()=default
Ctor.
SOL * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:109
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:118
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:112
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sosimp.h:115
~SOL() override=default
Dtor.
std::string_view type() const override
Returns a type name.
Definition sosimp.h:121
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:126
Semantic Object Mark.
Definition sosimp.h:43
SOM * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:62
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sosimp.h:68
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:65
~SOM() override=default
Dtor.
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:79
SOM()=default
Ctor.
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:71
std::string_view type() const override
Returns a type name.
Definition sosimp.h:74
Semantic Object Name.
Definition sosimp.h:525
SON(std::string p_name, bool p_exec=false)
Ctor from a string as name.
Definition sosimp.h:547
SON * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:578
std::string getName() const
Getter for name_.
Definition sosimp.h:574
SON(const SON &p_other)
Copy ctor.
Definition sosimp.h:555
bool invariant() const noexcept override
Checks class invariants.
Definition sosimp.h:536
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sosimp.h:581
size_t getSize() const override
Returns the size of the SO.
Definition sosimp.h:613
static bool otcodeIsName(OTCode p_code)
Checks if an OTCode represents a name type (N or Nx).
Definition sosimp.h:621
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:588
~SON() override=default
Dtor.
std::string_view type() const override
Returns a type name.
Definition sosimp.h:598
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:593
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:603
std::string name_
The name.
Definition sosimp.h:527
Semantic Object core code Operator.
Definition sosimp.h:634
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:692
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'.
Definition sosimp.h:682
SOO * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:679
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sosimp.h:687
bool invariant() const noexcept override
Checks class invariants.
Definition sosimp.h:644
~SOO() override
Dtor.
Definition sosimp.h:666
SOO(core_t *p_fun)
Ctor.
Definition sosimp.h:657
core_t *const core_
The core code, a C++ implementation of the operator.
Definition sosimp.h:636
static bool otcodeIsCoreCode(OTCode p_code)
Checks if an OTCode represents a core code operator type (O or o).
Definition sosimp.h:720
std::string_view type() const override
Returns a type name.
Definition sosimp.h:695
void exec(Context &k) const
Call the core code.
Definition sosimp.h:713
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:700
Semantic Object Real.
Definition sosimp.h:334
bool setReal(__float128 p_r)
Setter for real value.
Definition sosimp.h:387
void half__1()
In place half value.
Definition sosimp.h:461
SOR(const std::string &p_s)
Ctor from a string representation.
Definition sosimp.h:355
void ceil__1()
In place ceil().
Definition sosimp.h:490
~SOR() override=default
Dtor.
std::string opequal() const override
String representation of object for operators '=', 'cvs' and 'stack'. Reals are formated for output...
Definition sosimp.cpp:28
void floor__1()
In place floor().
Definition sosimp.h:498
__float128 r_
The real value as decimal number.
Definition sosimp.h:336
bool gt(OpError &p_ec, const SO *p_other) const override
Greater than.
Definition sosimp.h:430
bool ge(OpError &p_ec, const SO *p_other) const override
Greater or equal.
Definition sosimp.h:446
__float128 getReal() const
Getter for real value.
Definition sosimp.h:380
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sosimp.h:417
SOR(__float128 p_r)
Ctor from a float.
Definition sosimp.h:362
void trunc__1()
In place trunc().
Definition sosimp.h:482
OpError sqr__1()
In place square.
Definition sosimp.h:471
OpError reciprocal__1()
In place reciprocal.
Definition sosimp.h:510
SOR * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:398
bool invariant() const noexcept override
Checks class invariants.
Definition sosimp.h:344
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:409
std::string_view type() const override
Returns a type name.
Definition sosimp.h:412
Semantic Object.
Definition so.h:67
virtual OTCode ot() const =0
Returns an OTCode.
bool getExec() const
Getter for exec_.
Definition so.h:100
Semantic Object core code Operator - loop auxillary operators These unregistered operator objects are...
Definition sosimp.h:733
auto getStackusage() const
Getter for stackusage_.
Definition sosimp.h:763
~SOo() override=default
Dtor.
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:767
size_t stackusage_
The number of objects needed on the execution stack for execution.
Definition sosimp.h:736
SOo(core_t *p_fun)
Ctor.
Definition sosimp.h:744
Conversion functions.
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:122
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:114
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:104
#define DBC_INV_RAII(TT)
Defines an instance of the class DbCRAIIassert<>, which calls the invariant()-function at the return ...
Definition dbc.h:94
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:82
constexpr __float128 SOR_MAX_SQR
__float128 maximum that can be squared.
Definition helper.h:38
__float128 floor(__float128 p_r)
__float128 overload of floor().
Definition adapter128.cpp:35
__float128 ceil(__float128 p_r)
__float128 overload of ceil().
Definition adapter128.cpp:30
__float128 trunc(__float128 p_r)
__float128 overload of trunc().
Definition adapter128.cpp:40
bool isfinite(__float128 p_r)
__float128 overload of isfinite().
Definition adapter128.cpp:25
Collection of conversion functions.
Definition conversion.h:26
std::string __int128_to_string(__int128 p_z)
Converts an __int128 to its string representation.
Definition conversion.cpp:37
The class Registry.
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:38
@ M
SOM.
@ N
SON literal.
@ B
SOB.
@ Nx
SON executable.
@ L
SOL.
@ o
SOo.
@ I
SOI.
@ R
SOR.
@ O
SOO.