Leonardus
sok.h
Go to the documentation of this file.
1
17#pragma once
18
19#include <deque>
20#include <initializer_list>
21#include <memory>
22#include <algorithm>
23
24#include "dbc.h"
25#include "watermark.h"
26#include "counter.h"
27#include "helper.h"
28#include "so.h"
29
30
39class SOK : public SOcomposite {
40
44 struct SOKDeque {
45 std::deque<SOp> stldeque_;
50 for( const auto & ptr : stldeque_ )
51 delete ptr;
52 }
53 };
54
55 std::shared_ptr<SOKDeque> deque_;
58protected:
59#ifndef DBC_IS_VOID
63 bool invariant() const noexcept override { /* LCOV_EXCL_START */
64 if( deque_ == nullptr )
65 return false;
66 if( getSize() > deque_->watermark_.getWatermark() )
67 return false;
68 return std::none_of( deque_->stldeque_.begin(), deque_->stldeque_.end(),
69 []( const SO * p ) {
70 return p == nullptr;
71 } );
72 } /* LCOV_EXCL_STOP */
73#endif
74
75
76public:
78 SOK() : deque_(std::make_shared<SOKDeque>()) {
80 }
81
82
83public: /* virtual */
84 [[nodiscard]] SOK * dup() const override { return new SOK( *this ); }
85
86
90 [[nodiscard]] SOK * clone() const override;
91
92
93 std::string opequalequal() const override;
94
95
96 OTCode ot() const override { return OTCode::K; }
97
98
99 std::string type() const override { return "stacktype"; }
100
101
102 bool equal( const SO * p_other ) const override {
103 auto o = dynamic_cast<const SOK *>( p_other );
104 return o ? deque_ == o->deque_ : false;
105 }
106
107
108 size_t getSize() const override { return deque_->stldeque_.size(); }
109
110
111 size_t treeheight( size_t p_myheight ) const override;
112
113
114public: /* accessor */
116 size_t getWatermark() const { return deque_->watermark_.getWatermark(); }
117
118
125 void setSO( size_t p_index, SOp p_sop ) {
126 DBC_PRE( p_index < getSize() and p_sop != nullptr );
127
128 delete deque_->stldeque_[p_index];
129 deque_->stldeque_[p_index] = p_sop;
130
131 if( treeheight( 0 ) >= maxtreeheight )
132 inErrExit( semantics, "composite object tree height exceeds maximum" );
133
134 DBC_INV;
135 }
136
137
142 SOp at( size_t p_pos ) const {
143 DBC_PRE( p_pos < getSize() );
144
145 return deque_->stldeque_[ p_pos ];
146 }
147
148
153 SOp peek() const {
154 DBC_PRE( getSize() > 0 );
155
156 return deque_->stldeque_.back();
157 }
158
159
160public: /* other */
164 [[nodiscard]] SOp pop() {
165 DBC_PRE( getSize() > 0 );
166
167 SOp retval = deque_->stldeque_.back();
168 deque_->stldeque_.pop_back();
169
170 DBC_INV;
171 return retval;
172 }
173
174
177 void pop_delete() {
178 DBC_PRE( getSize() > 0 );
179
180 delete deque_->stldeque_.back();
181 deque_->stldeque_.pop_back();
182
183 DBC_INV;
184 }
185
186
190 void push( SOp p_o1 );
191
192
195 void push( std::initializer_list<SOp> p_list );
196
197
202 void push_front( SOp p_obj );
203
204
206 bool underflowcheck( size_t p_size ) const { return getSize() >= p_size; }
207
208
213 bool otchecker( std::initializer_list<OTCode> p_list ) const;
214
215
218 bool otchecker( OTCode p_code ) const {
219 DBC_PRE( !deque_->stldeque_.empty() );
220
221 return peek()->ot() == p_code;
222 }
223
224
230 bool countto( size_t & p_retval, OTCode p_code ) const;
231
232
237 SOp findValue( const SO * p_key ) const;
238
239
245 SOp findDict( const SO * p_key ) const;
246
247
253 [[nodiscard]] __int128 getI();
254
255
261 [[nodiscard]] __float128 getR();
262
263
269 [[nodiscard]] __float128 getIorRasR();
270
271
278 [[nodiscard]] size_t getISize_t();
279};
Semantic Object stacK.
Definition: sok.h:39
bool invariant() const noexcept override
Checks class invariants.
Definition: sok.h:63
SOp peek() const
Returns a copy of the SOp of the top most position.
Definition: sok.h:153
void pop_delete()
Removes the top most object from SOK, and the referenced SO will be deleted.
Definition: sok.h:177
size_t treeheight(size_t p_myheight) const override
Tree Height.
Definition: sok.cpp:61
__int128 getI()
I from SOK.
Definition: sok.cpp:136
OTCode ot() const override
Returns an OTCode.
Definition: sok.h:96
void setSO(size_t p_index, SOp p_sop)
Setter for a stack object.
Definition: sok.h:125
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:88
std::string opequalequal() const override
For operators '==' and 'pstack'.
Definition: sok.cpp:46
SOK * clone() const override
Creates a new instance as copy with deep cloning.
Definition: sok.cpp:34
__float128 getR()
R from SOK.
Definition: sok.cpp:149
SOK()
Ctor.
Definition: sok.h:78
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:219
bool otchecker(OTCode p_code) const
Checks SO classes on the operand stack against an OTCode.
Definition: sok.h:218
size_t getWatermark() const
Returns the watermark.
Definition: sok.h:116
SOp findValue(const SO *p_key) const
Finds the given key within the whole stack, assuming a stack with dictionaries only.
Definition: sok.cpp:107
__float128 getIorRasR()
I or R from SOK as R Deletes the top object after reading its value.
Definition: sok.cpp:173
bool underflowcheck(size_t p_size) const
Checks SOK against given size.
Definition: sok.h:206
size_t getISize_t()
I from SOK as size_t.
Definition: sok.cpp:162
std::shared_ptr< SOKDeque > deque_
The shared deque.
Definition: sok.h:55
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:122
SOp pop()
Returns a copy of the top SOp, removes the SOp from the SOK.
Definition: sok.h:164
SOp at(size_t p_pos) const
Returns a copy of the SOp at the given position.
Definition: sok.h:142
void push(SOp p_o1)
Pushes the object onto the SOK and transfers ownership to the SOK.
Definition: sok.cpp:192
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:77
size_t getSize() const override
Getter for the number of characters or number of objects.
Definition: sok.h:108
std::string type() const override
Returns a type name.
Definition: sok.h:99
SOK * dup() const override
Creates a new instance as copy following the red book definition.
Definition: sok.h:84
bool equal(const SO *p_other) const override
Equality.
Definition: sok.h:102
Semantic Object.
Definition: so.h:54
virtual OTCode ot() const =0
Returns an OTCode.
Composite Semantic Object.
Definition: so.h:161
Watermark class.
Definition: watermark.h:26
The class Counter.
Helpers for design by contract idioms.
#define DBC_INV_CTOR(T)
Assert for invariant checks in ctors and dtors.
Definition: dbc.h:88
#define DBC_INV
Assert for invariant checks in member functions.
Definition: dbc.h:83
#define DBC_PRE(XXX)
Assert for preconditions.
Definition: dbc.h:77
void inErrExit(InError p_err, const std::string &p_details, const std::source_location p_location)
Interpreter error message to interpreter cout_ and exit( EC_INTERPRETER / EC_CMDLINE ).
Definition: error.cpp:71
@ semantics
infinite nesting of arrays; infinite loop loading values from dictionary;
Definition: error.h:44
Miscellaneous definitions and functions.
constexpr size_t maxtreeheight
The maximum number of nesting levels of composite semantic objects.
Definition: helper.h:77
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition: so.h:29
@ K
SOK.
@ o
SOo.
Semantic Object Stack - deque member class.
Definition: sok.h:44
Watermark watermark_
The watermark for the size of the stack.
Definition: sok.h:46
std::deque< SOp > stldeque_
The deque.
Definition: sok.h:45
~SOKDeque()
Dtor.
Definition: sok.h:49
The class Watermark.