Leonardus
Loading...
Searching...
No Matches
sok.h
Go to the documentation of this file.
1
17#pragma once
18
19// Inc Library
20#include <deque>
21#include <initializer_list>
22#include <algorithm>
23#include <memory>
24
25// Inc HAA
26#include "dbc.h"
27#include "helper.h"
28
29// Inc Medium
30#include "watermark.h"
31
32// Inc Rich
33#include "so.h"
34
35
45class SOK : public SOcomposite {
46
52 struct SOKDeque {
53 std::deque<SOp> stldeque_{};
58 for( const auto & ptr : stldeque_ )
59 delete ptr;
60 }
61 };
62
63
65 std::shared_ptr<SOKDeque> deque_;
66
67
68protected:
69#ifndef DBC_IS_VOID
73 bool invariant() const noexcept override { /* LCOV_EXCL_START */
74 if( deque_ == nullptr )
75 return false;
76 if( getSize() > deque_->watermark_.getWatermark() )
77 return false;
78 return std::none_of( deque_->stldeque_.begin(), deque_->stldeque_.end(),
79 []( const SO * p ) {
80 return p == nullptr;
81 } );
82 } /* LCOV_EXCL_STOP */
83#endif
84
85
86public:
89 : SOcomposite(),
90 deque_(std::make_shared<SOKDeque>()) {
92 }
93
94
98 explicit SOK( const SOcomposite & p_other )
99 : SOcomposite(p_other),
100 deque_(std::make_shared<SOKDeque>()) {
102 }
103
104
106 SOK( const SOK & p_other )
107 : SOcomposite(p_other),
108 deque_(p_other.deque_) {
110 }
111
112
114 ~SOK() override {
116 }
117
118
119 SOK( SOK && ) = delete;
120 SOK & operator=( const SOK & ) = delete;
121 SOK & operator=( SOK && ) = delete;
122
123
124public: /* virtual */
125 [[nodiscard]] SOK * dup() const override { return new SOK( *this ); }
126
127
131 [[nodiscard]] SOK * clone() const override;
132
133
134 std::string opequalequal() const override;
135
136
137 OTCode ot() const override { return OTCode::K; }
138
139
140 std::string type() const override { return "stacktype"; }
141
142
145 bool equal( const SO * p_other ) const override {
146 auto o = dynamic_cast<const SOK *>( p_other );
147 return o ? deque_ == o->deque_ : false;
148 }
149
150
151 size_t getSize() const override { return deque_->stldeque_.size(); }
152
153
154 size_t treeheight( size_t p_myheight ) const override;
155
156
157public: /* accessor */
160 size_t getWatermark() const { return deque_->watermark_.getWatermark(); }
161
162
170 SOp ncat( size_t p_pos ) const {
171 DBC_PRE( p_pos < getSize() );
172
173 return deque_->stldeque_[ p_pos ];
174 }
175
176
183 SOp peek() const {
184 DBC_PRE( getSize() > 0 );
185
186 return deque_->stldeque_.back();
187 }
188
189
190public: /* other */
198 void putSO( size_t p_index, SOp p_sop ) {
199 DBC_PRE( p_index < getSize() and p_sop != nullptr );
200
201 delete deque_->stldeque_[p_index];
202 deque_->stldeque_[p_index] = p_sop;
203
204 if( treeheight( 0 ) >= maxtreeheight )
205 inErrExit( semantics, "composite object tree height exceeds maximum" );
206
207 DBC_INV;
208 }
209
210
216 [[nodiscard]] SOp pop() {
217 DBC_PRE( getSize() > 0 );
218
219 SOp retval = deque_->stldeque_.back();
220 deque_->stldeque_.pop_back();
221
222 DBC_INV;
223 return retval;
224 }
225
226
229 void pop_delete() {
230 DBC_PRE( getSize() > 0 );
231
232 delete deque_->stldeque_.back();
233 deque_->stldeque_.pop_back();
234
235 DBC_INV;
236 }
237
238
244 void push( SOp p_o1 );
245
246
251 void push( std::initializer_list<SOp> p_list );
252
253
260 void push_front( SOp p_obj );
261
262
267 bool underflowcheck( size_t p_size ) const { return getSize() >= p_size; }
268
269
277 bool otchecker( std::initializer_list<OTCode> p_list ) const;
278
279
286 bool otchecker( OTCode p_code ) const {
287 DBC_PRE( !deque_->stldeque_.empty() );
288 DBC_PRE( p_code != OTCode::X );
289
290 return peek()->ot() == p_code;
291 }
292
293
302 bool otcheckerOr( OTCode p_code1, OTCode p_code2 ) const {
303 DBC_PRE( !deque_->stldeque_.empty() );
304 DBC_PRE( p_code1 != OTCode::X );
305 DBC_PRE( p_code2 != OTCode::X );
306
307 return peek()->ot() == p_code1 || peek()->ot() == p_code2;
308 }
309
310
317 bool countto( size_t & p_retval, OTCode p_code ) const;
318
319
326 SOp findValue( const SO * p_key ) const;
327
328
336 SOp findDict( const SO * p_key ) const;
337
338
347 [[nodiscard]] __int128 getI( OpError & p_ec );
348
349
358 [[nodiscard]] __float128 getR( OpError & p_ec );
359
360
369 [[nodiscard]] __float128 getIorRasR();
370
371
381 [[nodiscard]] size_t getISize_t( OpError & p_ec );
382};
Semantic Object stacK.
Definition sok.h:45
SOp ncat(size_t p_pos) const
Returns a copy of the SOp at the given position.
Definition sok.h:170
bool invariant() const noexcept override
Checks class invariants.
Definition sok.h:73
size_t getISize_t(OpError &p_ec)
I from SOK as size_t.
Definition sok.cpp:171
SOp peek() const
Returns a copy of the SOp of the top most position.
Definition sok.h:183
void putSO(size_t p_index, SOp p_sop)
Put a single SO into a stack object.
Definition sok.h:198
void pop_delete()
Removes the top most object from SOK, and the referenced SO will be deleted.
Definition sok.h:229
size_t treeheight(size_t p_myheight) const override
Tree Height.
Definition sok.cpp:64
OTCode ot() const override
Returns an OTCode.
Definition sok.h:137
bool otchecker(std::initializer_list< OTCode > p_list) const
Checks SO classes on the operand stack against a list of given OTCodes.
Definition sok.cpp:91
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sok.cpp:49
~SOK() override
Dtor.
Definition sok.h:114
__int128 getI(OpError &p_ec)
I from SOK.
Definition sok.cpp:139
SOK * clone() const override
Creates a new instance as copy with deep cloning.
Definition sok.cpp:37
__float128 getR(OpError &p_ec)
R from SOK.
Definition sok.cpp:155
SOK(const SOK &p_other)
Copy ctor.
Definition sok.h:106
SOK()
Ctor.
Definition sok.h:88
void push_front(SOp p_obj)
Pushes the object onto the front side of the SOK and transfers ownership to the SOK.
Definition sok.cpp:234
bool otchecker(OTCode p_code) const
Checks SO classes on the operand stack against an OTCode.
Definition sok.h:286
size_t getWatermark() const
Returns the maximum size the stack has reached.
Definition sok.h:160
SOp findValue(const SO *p_key) const
Finds the given key within the whole stack, assuming a stack with dictionaries only.
Definition sok.cpp:110
__float128 getIorRasR()
I or R from SOK as R Deletes the top object after reading its value.
Definition sok.cpp:189
bool underflowcheck(size_t p_size) const
Checks SOK against given size.
Definition sok.h:267
std::shared_ptr< SOKDeque > deque_
The shared deque.
Definition sok.h:65
SOp findDict(const SO *p_key) const
Finds the dictionary storing the given key within the whole stack, assuming a stack with dictionaries...
Definition sok.cpp:125
SOp pop()
Returns a copy of the top SOp, removes the SOp from the SOK.
Definition sok.h:216
void push(SOp p_o1)
Pushes the object onto the SOK and transfers ownership to the SOK.
Definition sok.cpp:207
bool countto(size_t &p_retval, OTCode p_code) const
Count the objects on the SOK down to but excluding the object from the given type.
Definition sok.cpp:80
size_t getSize() const override
Returns the size of the SO.
Definition sok.h:151
std::string type() const override
Returns a type name.
Definition sok.h:140
SOK * dup() const override
Creates a new instance as copy following the red book definition.
Definition sok.h:125
SOK(const SOcomposite &p_other)
Ctor from parent class.
Definition sok.h:98
bool equal(const SO *p_other) const override
Checks if this SO is equal to another SO.
Definition sok.h:145
bool otcheckerOr(OTCode p_code1, OTCode p_code2) const
Checks SO class on the operand stack against two possible OTCodes.
Definition sok.h:302
Semantic Object.
Definition so.h:60
virtual OTCode ot() const =0
Returns an OTCode.
Composite Semantic Object.
Definition so.h:198
Watermark class.
Definition watermark.h:28
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition dbc.h:121
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:113
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:103
void inErrExit(InError p_err, const std::string &p_details, const std::source_location p_location)
Write interpreter error message to interpreter out file and exit( EC_INTERPRETER / EC_CMDLINE ).
Definition error.cpp:86
OpError
Operator error code enum.
Definition error.h:30
@ semantics
infinite nesting of arrays; infinite loop loading values from dictionary;
Definition error.h:51
Miscellaneous definitions and functions.
constexpr size_t maxtreeheight
The maximum number of nesting levels of composite semantic objects.
Definition helper.h:93
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:35
@ X
any SO. Is used by checks to represent any object.
@ K
SOK.
@ o
SOo.
Semantic Object Stack - deque member class.
Definition sok.h:52
Watermark watermark_
The watermark for the size of the stack.
Definition sok.h:54
std::deque< SOp > stldeque_
The deque storing SO pointers.
Definition sok.h:53
~SOKDeque()
Dtor.
Definition sok.h:57
The class Watermark.