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 <map>
22#include <numeric>
23#include <vector>
24#include <memory>
25
26// Inc HAA
27#include "dbc.h"
28#include "helper.h"
29#include "adapter128.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
44class SOM : public SO {
45public: /* virtual */
46 [[nodiscard]] SOM * dup() const override { return new SOM; }
47
48 std::string opequal() const override { return "--nostringval--"; }
49
50 std::string opequalequal() const override { return "mark"; }
51
52 OTCode ot() const override { return OTCode::M; }
53
54 std::string type() const override { return "marktype"; }
55
56 bool equal( const SO * p_other ) const override {
57 return dynamic_cast<const SOM *>( p_other) != nullptr; // All SOM are equal
58 }
59};
60
61
65class SOL : public SO {
66public: /* virtual */
67 SOL * dup() const override { return new SOL; }
68
69 std::string opequal() const override { return "--nostringval--"; }
70
71 std::string opequalequal() const override { return "null"; }
72
73 OTCode ot() const override { return OTCode::L; }
74
75 std::string type() const override { return "nulltype"; }
76
77 bool equal( const SO * p_other ) const override {
78 return dynamic_cast<const SOL *>( p_other) != nullptr; // All SOL are equal
79 }
80};
81
82
87class SOI : public SO {
88 __int128 i_;
90public:
92 explicit SOI( const std::string & p_s ) : i_(stoint128(p_s)) {}
93
95 explicit SOI( __int128 p_i ) : i_(p_i) {}
96
97public: /* accessor */
99 __int128 getInteger() const { return i_; }
100
102 void setInteger( __int128 p_i ) { i_=p_i; }
103
104public: /* virtual */
105 [[nodiscard]] SOI * dup() const override { return new SOI( i_ ); }
106
107 std::string opequal() const override { return to_string( i_ ); }
108
109 OTCode ot() const override { return OTCode::I; }
110
111 std::string type() const override { return "integertype"; }
112
113 bool equal( const SO * p_other ) const override {
114 auto o = dynamic_cast<const SOI*>( p_other );
115 return o ? i_ == o->i_ : false;
116 }
117
118 bool gt( const SO * p_other ) const override {
119 auto o = dynamic_cast<const SOI*>( p_other );
120 if( o == nullptr )
122 return i_ > o->i_;
123 }
124
125 bool ge( const SO * p_other ) const override {
126 auto o = dynamic_cast<const SOI*>( p_other );
127 if( o == nullptr )
129 return i_ >= o->i_;
130 }
131
132public: /* other */
134 void decrement() { i_--; }
135
137 void increment() { i_++; }
138};
139
140
143class SOB : public SO {
144 bool b_;
146public:
148 explicit SOB( const std::string & p_s ) : b_(p_s == "true") {}
149
151 explicit SOB( bool p_b ) : b_(p_b) {}
152
153public: /* accessor */
155 bool getB() const { return b_; }
156
158 void setB( bool p_b ) { b_ = p_b; }
159
160public: /* virtual */
161 [[nodiscard]] SOB * dup() const override { return new SOB( b_ ); }
162
163 std::string opequal() const override {
164 return b_ ? "true" : "false";
165 }
166
167 OTCode ot() const override { return OTCode::B; }
168
169 std::string type() const override { return "booleantype"; }
170
171 bool equal( const SO * p_other ) const override {
172 auto o = dynamic_cast<const SOB *>( p_other);
173 return o ? b_ == o->b_ : false;
174 }
175
176public: /* other */
178 void negation() { b_ = !b_; }
179};
180
181
188class SOR : public SO {
189 __float128 r_;
191public:
193 explicit SOR( const std::string & p_s ) : r_( stofloat128(p_s) ) {}
194
196 explicit SOR( __float128 p_r ) : r_(p_r) {}
197
198public: /* accessor */
200 __float128 getReal() const { return r_; }
201
203 void setReal( __float128 p_r ) { r_ = p_r; }
204
205public: /* virtual */
206 [[nodiscard]] SOR * dup() const override { return new SOR( r_ ); }
207
213 std::string opequal() const override;
214
215 OTCode ot() const override { return OTCode::R; }
216
217 std::string type() const override { return "realtype"; }
218
219 bool equal( const SO * p_other ) const override {
220 auto o = dynamic_cast<const SOR* >( p_other );
221 return o ? r_ == o->r_ : false;
222 }
223
224 bool gt( const SO * p_other ) const override {
225 auto o = dynamic_cast<const SOR *>( p_other );
226 if( o == nullptr )
228 return r_ > o->r_;
229 }
230
231 bool ge( const SO * p_other ) const override {
232 auto o = dynamic_cast<const SOR *>( p_other );
233 if( o == nullptr )
235 return r_ >= o->r_;
236 }
237
238public: /* other */
240 void trunc() { r_ = ::trunc( r_ ); }
241
243 void ceil() { r_ = ::ceil( r_ ); }
244
246 void floor() { r_ = ::floor( r_ ); }
247};
248
249
253class SON : public SO {
254 std::string name_;
256protected:
257#ifndef DBC_IS_VOID
262 bool invariant() const noexcept override { /* LCOV_EXCL_START */
263 return !name_.empty();
264 } /* LCOV_EXCL_STOP */
265#endif
266
267public:
269 explicit SON( const std::string& p_name, bool p_exec = false ) : SO(p_exec), name_(p_name) {
271 }
272
273public: /* virtual */
274 [[nodiscard]] SON * dup() const override { return new SON( name_, getExec() ); }
275
276 std::string opequal() const override {
277 if( getExec() )
278 return name_;
279 return "/"+name_;
280 }
281
282 OTCode ot() const override {
283 return getExec() ? OTCode::Nx : OTCode::N;
284 }
285
286 std::string type() const override { return "nametype"; }
287
288 bool equal( const SO * p_other ) const override {
289 auto o = dynamic_cast<const SON *>( p_other);
290 return o ? name_ == o->name_ : false;
291 }
292
293 size_t getSize() const override { return name_.size(); }
294
295public: /* other */
301 void load_exec( Context & k ) const;
302};
303
304
311class SOO : public SO {
312 core_t * const core_;
313 const char * const srep_;
315protected:
316#ifndef DBC_IS_VOID
320 bool invariant() const noexcept override { /* LCOV_EXCL_START */
321 if( core_ == nullptr or srep_ == nullptr )
322 return false;
323 return *srep_ != 0;
324 } /* LCOV_EXCL_STOP */
325#endif
326
327public:
329 SOO( core_t * p_fun, const char * p_str ) : core_(p_fun), srep_(p_str) {
331 }
332
333public: /* virtual */
334 [[nodiscard]] SOO * dup() const override { return new SOO( core_, srep_ ); }
335
336 std::string opequal() const override { return "--"+std::string(srep_)+"--"; }
337
338 OTCode ot() const override { return OTCode::O; }
339
340 std::string type() const override { return "operatortype"; }
341
342 bool equal( const SO * p_other ) const override {
343 const SOO* o = dynamic_cast<const SOO*>( p_other);
344 return o ? core_ == o->core_ : false;
345 }
346
347public: /* other */
349 void exec( Context & k ) const { core_(k); }
350};
351
352
357class SOo : public SOO {
358 size_t stackusage_;
360public:
362 SOo( core_t * p_fun, const char * p_str, size_t p_stackusage )
363 : SOO( p_fun, p_str ), stackusage_(p_stackusage) {}
364
365public: /* accessor */
367 auto getStackusage() const { return stackusage_; }
368
369public: /* virtual */
370 OTCode ot() const override { return OTCode::o; }
371};
string to_string(const __int128 p_z)
We need an adapter function, because __int128 isn't supported by std::to_string().
Definition adapter128.cpp:34
__int128 stoint128(const string &p_s)
We need an adapter function, because __float128 isn't supported by the standard library.
Definition adapter128.cpp:65
__float128 stofloat128(const string &p_s)
We need an adapter function, because __float128 isn't supported by the standard library.
Definition adapter128.cpp:76
Adapters for 128 bit versions of standard functions.
The context of execution.
Definition context.h:42
Semantic Object Boolean.
Definition sosimp.h:143
SOB(bool p_b)
Ctor.
Definition sosimp.h:151
void negation()
Negation.
Definition sosimp.h:178
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:167
bool getB() const
Getter for boolean value.
Definition sosimp.h:155
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition sosimp.h:163
bool equal(const SO *p_other) const override
Equality.
Definition sosimp.h:171
void setB(bool p_b)
Setter for boolean value.
Definition sosimp.h:158
bool b_
The boolean value.
Definition sosimp.h:144
SOB(const std::string &p_s)
Ctor.
Definition sosimp.h:148
std::string type() const override
Returns a type name.
Definition sosimp.h:169
SOB * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:161
Semantic Object Integer.
Definition sosimp.h:87
bool equal(const SO *p_other) const override
Equality.
Definition sosimp.h:113
SOI * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:105
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:109
SOI(const std::string &p_s)
Ctor.
Definition sosimp.h:92
void increment()
Increment.
Definition sosimp.h:137
void setInteger(__int128 p_i)
Setter for the integer value.
Definition sosimp.h:102
void decrement()
Decrement.
Definition sosimp.h:134
__int128 i_
The 128 bit integer.
Definition sosimp.h:88
SOI(__int128 p_i)
Ctor.
Definition sosimp.h:95
std::string type() const override
Returns a type name.
Definition sosimp.h:111
bool gt(const SO *p_other) const override
Greater than.
Definition sosimp.h:118
bool ge(const SO *p_other) const override
Greater or equal.
Definition sosimp.h:125
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition sosimp.h:107
__int128 getInteger() const
Getter for the integer value.
Definition sosimp.h:99
Semantic Object nuLl.
Definition sosimp.h:65
SOL * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:67
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:73
std::string type() const override
Returns a type name.
Definition sosimp.h:75
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition sosimp.h:69
std::string opequalequal() const override
For operators '==' and 'pstack'.
Definition sosimp.h:71
bool equal(const SO *p_other) const override
Equality.
Definition sosimp.h:77
Semantic Object Mark.
Definition sosimp.h:44
std::string type() const override
Returns a type name.
Definition sosimp.h:54
SOM * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:46
std::string opequalequal() const override
For operators '==' and 'pstack'.
Definition sosimp.h:50
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition sosimp.h:48
bool equal(const SO *p_other) const override
Equality.
Definition sosimp.h:56
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:52
Semantic Object Name.
Definition sosimp.h:253
SON * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:274
bool invariant() const noexcept override
Checks class invariants.
Definition sosimp.h:262
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition sosimp.h:293
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition sosimp.h:276
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:282
bool equal(const SO *p_other) const override
Equality.
Definition sosimp.h:288
SON(const std::string &p_name, bool p_exec=false)
Ctor.
Definition sosimp.h:269
std::string type() const override
Returns a type name.
Definition sosimp.h:286
std::string name_
The name.
Definition sosimp.h:254
void load_exec(Context &k) const
Look up a name and executes it.
Definition sosimp.cpp:37
Semantic Object core code Operator.
Definition sosimp.h:311
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:338
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition sosimp.h:336
const char *const srep_
A name just for representation.
Definition sosimp.h:313
SOO * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:334
bool invariant() const noexcept override
Checks class invariants.
Definition sosimp.h:320
core_t *const core_
The core code, a C++ implementation of the operator.
Definition sosimp.h:312
SOO(core_t *p_fun, const char *p_str)
Ctor.
Definition sosimp.h:329
std::string type() const override
Returns a type name.
Definition sosimp.h:340
void exec(Context &k) const
Call the core code.
Definition sosimp.h:349
bool equal(const SO *p_other) const override
Equality.
Definition sosimp.h:342
Semantic Object Real.
Definition sosimp.h:188
void setReal(__float128 p_r)
Setter for real value.
Definition sosimp.h:203
SOR(const std::string &p_s)
Ctor.
Definition sosimp.h:193
bool ge(const SO *p_other) const override
Greater or equal.
Definition sosimp.h:231
std::string opequal() const override
For operators '=', 'cvs' and 'stack'.
Definition sosimp.cpp:33
__float128 r_
The real value as decimal number.
Definition sosimp.h:189
__float128 getReal() const
Getter for real value.
Definition sosimp.h:200
bool equal(const SO *p_other) const override
Equality.
Definition sosimp.h:219
void trunc()
Direct trunc().
Definition sosimp.h:240
SOR(__float128 p_r)
Ctor.
Definition sosimp.h:196
void floor()
Direct floor().
Definition sosimp.h:246
bool gt(const SO *p_other) const override
Greater than.
Definition sosimp.h:224
void ceil()
Direct ceil().
Definition sosimp.h:243
std::string type() const override
Returns a type name.
Definition sosimp.h:217
SOR * dup() const override
Creates a new instance as copy following the red book definition.
Definition sosimp.h:206
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:215
Semantic Object.
Definition so.h:58
bool getExec() const
Getter for exec_.
Definition so.h:74
Semantic Object core code Operator unregistered section.
Definition sosimp.h:357
auto getStackusage() const
Getter for stackusage_.
Definition sosimp.h:367
OTCode ot() const override
Returns an OTCode.
Definition sosimp.h:370
size_t stackusage_
The number of objects needed on the execution stack for execution.
Definition sosimp.h:358
SOo(core_t *p_fun, const char *p_str, size_t p_stackusage)
Ctor.
Definition sosimp.h:362
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:89
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:31
@ typecheck
PS operator error typecheck.
Definition error.h:34
Miscellaneous definitions and functions.
void(Context &) core_t
A shortcut for the core code functions, the C++ implementations of the operators.
Definition helper.h:89
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:33
@ M
SOM.
@ N
SON literal.
@ B
SOB.
@ Nx
SON executable.
@ L
SOL.
@ o
SOo.
@ I
SOI.
@ R
SOR.
@ O
SOO.