Kryptostack
sox.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <string>
8#include <map>
9#include <numeric>
10#include <vector>
11#include <memory>
12#include "helper.h"
13#include "so.h"
14#include "context.h"
15
16using namespace std;
17
18
19/* ******************************************************************* */
20/* Simple Objects */
21/* SO's that store their data without indirect referencing */
22/* ******************************************************************* */
23
24
28class SOM : public SO {
30 SOM * dup() const override { return new SOM; }
31 string opequal() const override { return "--nostringval--"; }
32 string opequalequal( Context & ) const override { return "-mark-"; }
33 OTCode ot() const override { return 'M'; }
34 bool equal( const SO* p_other ) const override { // We can't use SOp here. Doxygen fails to identify the override with this typedef.
35 return dynamic_cast<const SOM*>( p_other) != nullptr; // All SOM are equal
36 }
37};
38
39
43class SO0 : public SO {
45 SO0 * dup() const override { return new SO0; }
46 string opequal() const override { return "--nostringval--"; }
47 string opequalequal( Context & ) const override { return "null"; }
48 OTCode ot() const override { return '0'; }
49 bool equal( const SO* p_other ) const override {
50 return dynamic_cast<const SO0*>( p_other) != nullptr; // All SO0 are equal
51 }
52};
53
54
58class SOI : public SO {
59 __int128 i_;
61public:
63 explicit SOI( const string& p_s ) : i_(stoint128(p_s)) {}
65 explicit SOI( __int128 p_i ) : i_(p_i) {}
66
69 __int128 getInteger() const { return i_; }
71 void setInteger( __int128 p_i ) { i_=p_i; }
72
74 SOI * dup() const override { return new SOI( i_ ); }
75 string opequal() const override { return to_string( i_ ); }
76 OTCode ot() const override { return 'I'; }
77 bool equal( const SO* p_other ) const override {
78 const SOI* o = dynamic_cast<const SOI*>( p_other);
79 if( o ) return i_ == o->i_;
80 return false;
81 }
82};
83
84
87class SOB : public SO {
88 bool b_;
90public:
92 explicit SOB( const string& p_s ) { b_ = p_s == "true"; }
94 explicit SOB( bool p_b ) : b_(p_b) {}
95
98 bool getB() const { return b_; }
100 void setB( bool p_b ) { b_ = p_b; }
101
103 SOB * dup() const override { return new SOB( b_ ); }
104 string opequal() const override { return b_ ? "true" : "false"; }
105 OTCode ot() const override { return 'B'; }
106 bool equal( const SO* p_other ) const override {
107 const SOB* o = dynamic_cast<const SOB*>( p_other);
108 if( o ) return b_ == o->b_;
109 return false;
110 }
111
114 void negation() { b_ = !b_; }
115};
116
117
122class SOR : public SO {
123 __float128 r_;
125public:
127 explicit SOR( const string& p_s ) : r_(stofloat128(p_s)) {}
129 explicit SOR( __float128 p_r ) : r_(p_r) {}
130
133 __float128 getReal() const { return r_; }
135 void setReal( __float128 p_r ) { r_ = p_r; }
136
138 SOR * dup() const override { return new SOR( r_ ); }
139 string opequal() const override { return to_string( r_ ); }
140 OTCode ot() const override { return 'R'; }
141 bool equal( const SO* p_other ) const override {
142 const SOR* o = dynamic_cast<const SOR*>( p_other);
143 if( o ) return r_ == o->r_;
144 return false;
145 }
146};
147
148
151class SON : public SO {
152 string n_;
154public:
156 explicit SON( const string& p_n, bool p_exec = false ) : SO(p_exec), n_(p_n) { }
157
159 SON * dup() const override { return new SON( n_, getExec() ); }
160 string opequal() const override {
161 if( exec_ ) return n_;
162 return "/"+n_;
163 }
164 OTCode ot() const override { return exec_ ? 'n' : 'N'; }
165 bool equal( const SO* p_other ) const override {
166 const SON* o = dynamic_cast<const SON*>( p_other);
167 if( o ) return n_ == o->n_;
168 return false;
169 }
170 size_t getSize() const override { return n_.size(); }
171
174 void load( Context & k ) const;
175};
176
177
182class SOO : public SO {
183 void (*f_)( Context & );
184 string s_;
186public:
188 explicit SOO( void (*p_f)( Context & ), const string& p_s ) : f_(p_f), s_(p_s) { }
189
191 SOO * dup() const override { return new SOO( f_, s_ ); }
192 string opequal() const override { return "--"+s_+"--"; }
193 OTCode ot() const override { return 'O'; }
194 bool equal( const SO* p_other ) const override {
195 const SOO* o = dynamic_cast<const SOO*>( p_other);
196 if( o ) return f_ == o->f_;
197 return false;
198 }
199
202 void exec( Context & k ) { f_(k); }
203};
204
205
210class SOo : public SOO {
211 size_t stackusage_;
213public:
215 SOo( void (*p_f)( Context & ), const string & p_s, size_t p_stackusage )
216 : SOO( p_f, p_s ), stackusage_(p_stackusage) { }
217
220 size_t getStackusage() const { return stackusage_; }
221
223 OTCode ot() const override { return 'o'; }
224};
225
226
227/* ************************************************************ */
228/* Composite Objects */
229/* SO's that reference their data with shared_ptr<> */
230/* ************************************************************ */
231
232
235class SOS : public SO {
236 shared_ptr<string> s_;
239 explicit SOS( shared_ptr<string> p_s ) : s_(p_s) {}
240
241public:
243 explicit SOS( string p_s ) : s_(make_shared<string>(p_s)) { }
244
246 ~SOS() = default;
247
250 string getString() const { return *s_; }
251
253 SOS* dup() const override { return new SOS( s_ ); }
254 SOS* clone() const override { return new SOS( *s_ ); }
255 string opequal() const override { return *s_; }
256 string opequalequal( Context & ) const override { return "("+*s_+")"; }
257 OTCode ot() const override { return 'S'; }
258 bool equal( const SO* p_other ) const override {
259 const SOS* o = dynamic_cast<const SOS*>( p_other);
260 if( o ) return *s_ == *(o->s_); // SOS are equal if their value is equal, even with non-shared pointers
261 return false;
262 }
263 size_t getSize() const override { return s_->size(); }
264
269 void setch( size_t index, int ch ) { (*s_)[index] = ch; }
270
272 void erase0() { s_->erase(0,1); }
273};
274
275
280class SOA : public SO {
285 struct SOAArray {
286 vector<SOp> a_;
288 ~SOAArray() { for( auto ptr : a_ ) delete ptr; }
289 };
290 shared_ptr<SOAArray> v_;
293 SOA( shared_ptr<SOAArray> p_v, bool p_exec ) : SO(p_exec), v_(p_v) {}
294
295public:
297 explicit SOA( size_t p_l = 0, bool p_exec = false ) : SO(p_exec) {
298 v_ = make_shared<SOAArray>();
299 for( size_t i = 1; i <= p_l; i++ )
300 v_->a_.push_back( new SO0 );
301 }
302
307 void setSO( size_t p_index, SOp p_o ) {
308 delete v_->a_[p_index];
309 v_->a_[p_index] = p_o;
310 }
311
315 SOp at( size_t p_pos ) const { return v_->a_.at( p_pos ); }
316
318 SOA* dup() const override { return new SOA( v_, getExec() ); }
319 SOA* clone() const override { // IDEA: work around the temporary construction of all the SO0 objects.
320 SOA* rv = new SOA( getSize(), exec_ );
321 for( size_t i = 0; i < getSize(); i++ )
322 rv->setSO( i, at(i)->clone() );
323 return rv;
324 }
325 string opequal() const override { return "--nostringval--"; }
326 string opequalequal( Context & ) const override;
327 OTCode ot() const override { return exec_ ? 'a' : 'A'; }
328 bool equal( const SO* p_other ) const override {
329 const SOA* o = dynamic_cast<const SOA*>( p_other);
330 if( o ) {
331 if( getSize() == 0 && o->getSize() == 0 ) return true; // Exception: arrays of size zero are always equal.
332 return v_ == o->v_;
333 }
334 return false;
335 }
336 size_t getSize() const override { return v_->a_.size(); }
337
344 void reduce() {
345 const size_t s = getSize();
346 if( !s ) return;
347 SOp last = v_->a_.at( s-1 );
348 v_->a_.pop_back();
349 delete last;
350 }
351
354 void unfold2exec( Context & k ) const {
355 for( size_t s = getSize(); s>0; s-- )
356 k.exst().push( at(s-1)->dup() );
357 }
358
362 SOp retval = v_->a_.at(0);
363 v_->a_.erase( v_->a_.begin() );
364 return retval;
365 }
366};
367
368
371class SOD : public SO {
375 struct SODMap {
376 map<SOp,SOp> a_;
378 ~SODMap() { for( const auto& ptr : a_ ) { delete ptr.first; delete ptr.second; } }
379 };
380 shared_ptr<SODMap> d_;
383 explicit SOD( shared_ptr<SODMap> p_d ) : d_(p_d) {}
384
385public:
387 SOD() { d_ = make_shared<SODMap>(); }
388
390 SOD* dup() const override { return new SOD( d_ ); }
391 SOD* clone() const override {
392 SOD* rv = new SOD();
393 for( auto& ptr : d_->a_ ) rv->insert( ptr.first->clone(), ptr.second->clone() );
394 return rv;
395 }
396 string opequal() const override { return "--nostringval--"; }
397 string opequalequal( Context & k ) const override;
398 OTCode ot() const override { return 'D'; }
399 bool equal( const SO* p_other ) const override {
400 const SOD* o = dynamic_cast<const SOD*>( p_other);
401 if( o ) return d_ == o->d_;
402 return false;
403 }
404 size_t getSize() const override { return d_->a_.size(); }
405
411 void insert( SOp p_key,
412 SOp p_value,
413 bool p_forcebegin = false
414 );
415
420 SOp find( const SO * p_key ) const;
421
425 pair<SOp,SOp> any_pop() {
426 auto it = d_->a_.begin();
427 pair<SOp,SOp> retval( *it );
428 d_->a_.erase( it );
429 return retval;
430 }
431};
The context of execution.
Definition: context.h:17
Stack & exst()
Access to the execution stack.
Definition: context.h:47
Semantic Object Null.
Definition: sox.h:43
public_virtual SO0 * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:45
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:49
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:46
string opequalequal(Context &) const override
For operators '==' and 'pstack'.
Definition: sox.h:47
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:48
Semantic Object Array.
Definition: sox.h:280
shared_ptr< SOAArray > v_
The shared array.
Definition: sox.h:290
public_virtual SOA * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:318
SOA(size_t p_l=0, bool p_exec=false)
ctor.
Definition: sox.h:297
SOp front_pop()
Returns a copy of the SOp at position 0 and removes this first position from the array.
Definition: sox.h:361
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition: sox.h:336
void unfold2exec(Context &k) const
Unfolds duplicates of the array-content to the execution stack.
Definition: sox.h:354
SOA * clone() const override
Creates a new instance as copy with deep cloning.
Definition: sox.h:319
SOp at(size_t p_pos) const
Returns a copy of the SOp at the given position.
Definition: sox.h:315
SOA(shared_ptr< SOAArray > p_v, bool p_exec)
Private ctor for dup().
Definition: sox.h:293
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:328
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:327
public_other void reduce()
Reduces the array by one SO at the end.
Definition: sox.h:344
public_accessor void setSO(size_t p_index, SOp p_o)
Setter for an array object.
Definition: sox.h:307
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:325
string opequalequal(Context &) const override
For operators '==' and 'pstack'.
Definition: sox.cpp:31
Semantic Object Boolean.
Definition: sox.h:87
SOB(bool p_b)
ctor.
Definition: sox.h:94
public_accessor bool getB() const
Getter for boolean value.
Definition: sox.h:98
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:105
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:104
SOB(const string &p_s)
ctor.
Definition: sox.h:92
public_other void negation()
Negation.
Definition: sox.h:114
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:106
void setB(bool p_b)
Setter for boolean value.
Definition: sox.h:100
public_virtual SOB * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:103
bool b_
The boolean value.
Definition: sox.h:88
Semantic Object Dictionary.
Definition: sox.h:371
string opequalequal(Context &k) const override
For operators '==' and 'pstack'.
Definition: sox.cpp:52
public_other void insert(SOp p_key, SOp p_value, bool p_forcebegin=false)
Insert with PostScript definition of equal.
Definition: sox.cpp:73
SOD(shared_ptr< SODMap > p_d)
Private ctor for dup().
Definition: sox.h:383
pair< SOp, SOp > any_pop()
Returns a pair from the dictionary.
Definition: sox.h:425
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition: sox.h:404
SOD * clone() const override
Creates a new instance as copy with deep cloning.
Definition: sox.h:391
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:398
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:396
SOp find(const SO *p_key) const
Searches for p_key in the dictionary.
Definition: sox.cpp:92
shared_ptr< SODMap > d_
The shared dictionary.
Definition: sox.h:380
SOD()
Ctor.
Definition: sox.h:387
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:399
public_virtual SOD * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:390
Semantic Object Integer.
Definition: sox.h:58
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:77
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:76
SOI(const string &p_s)
ctor.
Definition: sox.h:63
public_virtual SOI * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:74
void setInteger(__int128 p_i)
Setter for the integer value.
Definition: sox.h:71
__int128 i_
The 128 bit integer.
Definition: sox.h:59
SOI(__int128 p_i)
ctor.
Definition: sox.h:65
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:75
public_accessor __int128 getInteger() const
Getter for the integer value.
Definition: sox.h:69
Semantic Object Mark.
Definition: sox.h:28
public_virtual SOM * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:30
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:34
string opequalequal(Context &) const override
For operators '==' and 'pstack'.
Definition: sox.h:32
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:33
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:31
Semantic Object Name.
Definition: sox.h:151
string n_
The name.
Definition: sox.h:152
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:160
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition: sox.h:170
public_virtual SON * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:159
SON(const string &p_n, bool p_exec=false)
ctor.
Definition: sox.h:156
public_other void load(Context &k) const
Search for the name on the dictionary stack and push the value onto the execution stack.
Definition: sox.cpp:11
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:164
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:165
Semantic Object Operator.
Definition: sox.h:182
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:193
SOO(void(*p_f)(Context &), const string &p_s)
Ctor.
Definition: sox.h:188
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:192
string s_
A name just for representation.
Definition: sox.h:184
public_virtual SOO * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:191
public_other void exec(Context &k)
Call the implementation of the operator.
Definition: sox.h:202
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:194
void(* f_)(Context &)
The C++ implementation of the operator.
Definition: sox.h:183
Semantic Object Real.
Definition: sox.h:122
public_accessor __float128 getReal() const
Getter for real value.
Definition: sox.h:133
void setReal(__float128 p_r)
Setter for real value.
Definition: sox.h:135
public_virtual SOR * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:138
__float128 r_
The real value as decimal number.
Definition: sox.h:123
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:141
SOR(const string &p_s)
ctor.
Definition: sox.h:127
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:139
SOR(__float128 p_r)
ctor.
Definition: sox.h:129
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:140
Semantic Object String.
Definition: sox.h:235
public_accessor string getString() const
Getter for the string value.
Definition: sox.h:250
SOS(string p_s)
ctor.
Definition: sox.h:243
string opequalequal(Context &) const override
For operators '==' and 'pstack'.
Definition: sox.h:256
SOS * clone() const override
Creates a new instance as copy with deep cloning.
Definition: sox.h:254
~SOS()=default
dtor.
SOS(shared_ptr< string > p_s)
Private ctor for dup()
Definition: sox.h:239
bool equal(const SO *p_other) const override
Equality.
Definition: sox.h:258
void erase0()
Erases the fist character of the String.
Definition: sox.h:272
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition: sox.h:263
public_virtual SOS * dup() const override
Creates a new instance as copy following the PostScript definition.
Definition: sox.h:253
public_other void setch(size_t index, int ch)
Sets a single character within the string.
Definition: sox.h:269
string opequal() const override
For operators '=' and 'stack'.
Definition: sox.h:255
OTCode ot() const override
Returns an OTCode.
Definition: sox.h:257
shared_ptr< string > s_
the string value
Definition: sox.h:236
Semantic Object.
Definition: so.h:17
public_accessor bool getExec() const
Getter for exec_.
Definition: so.h:31
virtual SO * clone() const
Creates a new instance as copy with deep cloning.
Definition: so.h:44
bool exec_
All SOs have executive vs.
Definition: so.h:19
Semantic Object Operator unregistered section.
Definition: sox.h:210
public_accessor size_t getStackusage() const
Getter for stackusage_.
Definition: sox.h:220
public_virtual OTCode ot() const override
Returns an OTCode.
Definition: sox.h:223
size_t stackusage_
The number of objects needed on stack for execution.
Definition: sox.h:211
SOo(void(*p_f)(Context &), const string &p_s, size_t p_stackusage)
ctor.
Definition: sox.h:215
void push(SOp p_s)
Pushes the object onto the Stack and transfers ownership to the Stack.
Definition: stack.h:67
The class Context.
#define public_virtual
virtual member functions
Definition: helper.h:15
#define public_other
other member functions
Definition: helper.h:16
#define public_accessor
accessor functions
Definition: helper.h:14
std::string to_string(__int128 p_z)
We need a dedicated function, because __int128 isn't supported by to_string().
Definition: helper.cpp:18
__int128 stoint128(const std::string &p_s)
We need a dedicated function, because __float128 isn't supported by the standard library.
Definition: helper.cpp:52
__float128 stofloat128(const std::string &p_s)
We need a dedicated function, because __float128 isn't supported by the standard library.
Definition: helper.cpp:59
Miscellaneous definitions and functions.
char OTCode
OTCode - the Object Type Code.
Definition: helper.h:22
Class semantic object.
Semantic Object Array - array member class.
Definition: sox.h:285
~SOAArray()
Dtor.
Definition: sox.h:288
vector< SOp > a_
The inner array.
Definition: sox.h:286
Semantic Object Dictionary - map member class.
Definition: sox.h:375
map< SOp, SOp > a_
The map.
Definition: sox.h:376
~SODMap()
Dtor.
Definition: sox.h:378