Leonardus
Loading...
Searching...
No Matches
sok.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 <deque>
23#include <initializer_list>
24#include <algorithm>
25#include <memory>
26#include "determinations.h"
27
28// Inc Pure
29#include "dbc.h"
30
31// Inc Medium
32#include "watermark.h"
33
34// Inc Rich
35#include "so.h"
36
37
49class SOK : public SOcomposite {
50
57 struct SOKDeque {
58
59 std::deque<SOp> stldeque_{};
65 for( const auto & ptr : stldeque_ )
66 delete ptr;
67 }
68
69 };
70
71
73 std::shared_ptr<SOKDeque> deque_;
74
75
79
80protected:
81
82 void * shptr() const override { return static_cast<void *>( deque_.get() ); }
83
84
85#ifndef DBC_IS_VOID
89 bool invariant() const noexcept override { /* LCOV_EXCL_START */
91 return false;
92 if( getSize() > deque_->watermark_.getWatermark() )
93 return false;
94 if( std::any_of( deque_->stldeque_.begin(), deque_->stldeque_.end(),
95 []( const SO * p ) {
96 return p == nullptr;
97 } ) )
98 return false;
99 if( restricted_ != OTCode::X )
100 if( std::any_of( deque_->stldeque_.begin(), deque_->stldeque_.end(),
101 [this]( const SO * p ) {
102 return p->ot() != restricted_;
103 } ) )
104 return false;
105 return true;
106 } /* LCOV_EXCL_STOP */
107#endif
108
109public:
110
113 : SOcomposite(),
114 deque_(std::make_shared<SOKDeque>()),
117 }
118
119
121 explicit SOK( OTCode p_restr )
122 : SOcomposite(),
123 deque_(std::make_shared<SOKDeque>()),
124 restricted_(p_restr) {
126 }
127
128
132 explicit SOK( const SOcomposite & p_other )
133 : SOcomposite(p_other),
134 deque_(std::make_shared<SOKDeque>()),
137 }
138
139
141 SOK( const SOK & p_other )
142 : SOcomposite(p_other),
143 deque_(p_other.deque_),
144 restricted_(p_other.restricted_) {
146 }
147
148
150 ~SOK() override {
152 }
153
154
155 SOK( SOK && ) = delete;
156 SOK & operator=( const SOK & ) = delete;
157 SOK & operator=( SOK && ) = delete;
158
159public: /* virtual */
160
161 [[nodiscard]] SOK * dup() const override { return new SOK( *this ); }
162
163
167 [[nodiscard]] SOK * clone() const override;
168
169
170 std::string opequalequal() const override;
171
172
173 OTCode ot() const override { return OTCode::K; }
174
175
176 std::string_view type() const override { return "stacktype"; }
177
178
179 size_t getSize() const override { return deque_->stldeque_.size(); }
180
181
182 size_t dagheight( size_t p_myheight ) const override;
183
184public: /* accessor */
185
188 OTCode getRestricted() const { return restricted_; }
189
190
193 size_t getWatermark() const { return deque_->watermark_.getWatermark(); }
194
195
203 SOp ncat( size_t p_pos ) const {
204 DBC_PRE( p_pos < getSize() );
205
206 return deque_->stldeque_[ p_pos ];
207 }
208
209
216 SOp peek() const {
217 DBC_PRE( getSize() > 0 );
218
219 return deque_->stldeque_.back();
220 }
221
222public: /* other */
223
232 void putSO( size_t p_index, SOp p_sop ) {
233 DBC_PRE( p_index < getSize() and p_sop != nullptr );
234 DBC_PRE( restricted_ == OTCode::X || restricted_ == p_sop->ot() );
235
236 delete deque_->stldeque_[p_index];
237 deque_->stldeque_[p_index] = p_sop;
238
239 if( SOcomposite::otcodeIsComposite( p_sop->ot() ) &&
240 static_cast<SOcomposite *>(p_sop)->dagheight( 0 ) >= DET_MAXDAGHEIGHT )
241 inErrExit( semantics, "composite object DAG height exceeds maximum" );
242
243 DBC_INV;
244 }
245
246
252 [[nodiscard]] SOp pop() {
253 DBC_PRE( getSize() > 0 );
254
255 SOp retval = deque_->stldeque_.back();
256 deque_->stldeque_.pop_back();
257
258 DBC_INV;
259 return retval;
260 }
261
262
265 void pop_delete() {
266 DBC_PRE( getSize() > 0 );
267
268 delete deque_->stldeque_.back();
269 deque_->stldeque_.pop_back();
270
271 DBC_INV;
272 }
273
274
281 void push( SOp p_o1 );
282
283
289 void push( std::initializer_list<SOp> p_list );
290
291
299 void push_front( SOp p_obj );
300
301
306 bool underflowcheck( size_t p_size ) const { return getSize() >= p_size; }
307
308
316 bool otchecker( std::initializer_list<OTCode> p_list ) const;
317
318
325 bool otchecker( OTCode p_code ) const {
326 DBC_PRE( !deque_->stldeque_.empty() );
327 DBC_PRE( p_code != OTCode::X );
328
329 return peek()->ot() == p_code;
330 }
331
332
341 bool otcheckerOr( OTCode p_code1, OTCode p_code2 ) const {
342 DBC_PRE( !deque_->stldeque_.empty() );
343 DBC_PRE( p_code1 != OTCode::X );
344 DBC_PRE( p_code2 != OTCode::X );
345
346 return peek()->ot() == p_code1 || peek()->ot() == p_code2;
347 }
348
349
356 bool countto( size_t & p_retval, OTCode p_code ) const;
357
358
366 SOp findValue( SO * p_key ) const;
367
368
378 SOp findDict( SO * p_key ) const;
379
380
388 [[nodiscard]] __int128 getI();
389
390
398 [[nodiscard]] __float128 getR();
399
400
409 [[nodiscard]] __float128 getIorRasR();
410
411
421 [[nodiscard]] size_t getISize_t( OpError & p_ec );
422
423};
virtual bool invariant() const noexcept
Checks the invariants of the class in which it is defined.
Definition dbc.h:40
Semantic Object stacK.
Definition sok.h:49
SOp findValue(SO *p_key) const
Finds the given key within the whole stack, assuming a stack with dictionaries only.
Definition sok.cpp:112
SOp ncat(size_t p_pos) const
Returns a copy of the SOp at the given position.
Definition sok.h:203
bool invariant() const noexcept override
Checks class invariants.
Definition sok.h:89
size_t getISize_t(OpError &p_ec)
I from SOK as size_t.
Definition sok.cpp:163
SOp findDict(SO *p_key) const
Finds the dictionary storing the given key within the whole stack, assuming a stack with dictionaries...
Definition sok.cpp:126
void * shptr() const override
Pointer to the shared object.
Definition sok.h:82
SOp peek() const
Returns a copy of the SOp of the top most position.
Definition sok.h:216
OTCode getRestricted() const
Returns the restriction.
Definition sok.h:188
void putSO(size_t p_index, SOp p_sop)
Put a single SO into a stack object.
Definition sok.h:232
void pop_delete()
Removes the top most object from SOK, and the referenced SO will be deleted.
Definition sok.h:265
__int128 getI()
I from SOK.
Definition sok.cpp:139
OTCode ot() const override
Returns an OTCode.
Definition sok.h:173
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:93
std::string opequalequal() const override
String representation for operators '==' and 'pstack'.
Definition sok.cpp:52
size_t dagheight(size_t p_myheight) const override
DAG Height.
Definition sok.cpp:67
~SOK() override
Dtor.
Definition sok.h:150
SOK * clone() const override
Creates a new instance as copy with deep cloning.
Definition sok.cpp:39
__float128 getR()
R from SOK.
Definition sok.cpp:151
SOK(const SOK &p_other)
Copy ctor.
Definition sok.h:141
SOK()
Ctor.
Definition sok.h:112
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:228
bool otchecker(OTCode p_code) const
Checks SO classes on the operand stack against an OTCode.
Definition sok.h:325
size_t getWatermark() const
Returns the maximum size the stack has reached.
Definition sok.h:193
__float128 getIorRasR()
I or R from SOK as R Deletes the top object after reading its value.
Definition sok.cpp:181
bool underflowcheck(size_t p_size) const
Checks SOK against given size.
Definition sok.h:306
std::shared_ptr< SOKDeque > deque_
The shared deque.
Definition sok.h:73
std::string_view type() const override
Returns a type name.
Definition sok.h:176
SOp pop()
Returns a copy of the top SOp, removes the SOp from the SOK.
Definition sok.h:252
void push(SOp p_o1)
Pushes the object onto the SOK and transfers ownership to the SOK.
Definition sok.cpp:197
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:82
size_t getSize() const override
Returns the size of the SO.
Definition sok.h:179
OTCode restricted_
Restrict container entries to this type.
Definition sok.h:78
SOK * dup() const override
Creates a new instance as copy following the red book definition.
Definition sok.h:161
SOK(const SOcomposite &p_other)
Ctor from parent class.
Definition sok.h:132
SOK(OTCode p_restr)
Ctor.
Definition sok.h:121
bool otcheckerOr(OTCode p_code1, OTCode p_code2) const
Checks SO class on the operand stack against two possible OTCodes.
Definition sok.h:341
Semantic Object.
Definition so.h:67
virtual OTCode ot() const =0
Returns an OTCode.
Composite Semantic Object.
Definition so.h:261
virtual size_t dagheight(size_t p_myheigth) const =0
DAG Height.
static bool otcodeIsComposite(OTCode p_code)
Checks if an OTCode represents a composite type (A, Ax, D, K).
Definition so.h:290
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: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
Determinations of the compile time constants, which we can not calculate (so far).
constexpr size_t DET_MAXDAGHEIGHT
The maximum number of nesting levels of composite semantic objects.
Definition determinations.h:33
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
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:38
@ X
any SO. Is used by checks to represent any object.
@ K
SOK.
Semantic Object Stack - deque member class.
Definition sok.h:57
Watermark watermark_
The watermark for the size of the stack.
Definition sok.h:60
std::deque< SOp > stldeque_
The deque storing SO pointers.
Definition sok.h:59
~SOKDeque()
Dtor.
Definition sok.h:64
The class Watermark.