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 <memory>
23#include <algorithm>
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
44class SOK : public SOcomposite {
45
49 struct SOKDeque {
50 std::deque<SOp> stldeque_;
55 for( const auto & ptr : stldeque_ )
56 delete ptr;
57 }
58 };
59
60 std::shared_ptr<SOKDeque> deque_;
63protected:
64#ifndef DBC_IS_VOID
68 bool invariant() const noexcept override { /* LCOV_EXCL_START */
69 if( deque_ == nullptr )
70 return false;
71 if( getSize() > deque_->watermark_.getWatermark() )
72 return false;
73 return std::none_of( deque_->stldeque_.begin(), deque_->stldeque_.end(),
74 []( const SO * p ) {
75 return p == nullptr;
76 } );
77 } /* LCOV_EXCL_STOP */
78#endif
79
80
81public:
83 SOK() : deque_(std::make_shared<SOKDeque>()) {
85 }
86
87
88public: /* virtual */
89 [[nodiscard]] SOK * dup() const override { return new SOK( *this ); }
90
91
95 [[nodiscard]] SOK * clone() const override;
96
97
98 std::string opequalequal() const override;
99
100
101 OTCode ot() const override { return OTCode::K; }
102
103
104 std::string type() const override { return "stacktype"; }
105
106
107 bool equal( const SO * p_other ) const override {
108 auto o = dynamic_cast<const SOK *>( p_other );
109 return o ? deque_ == o->deque_ : false;
110 }
111
112
113 size_t getSize() const override { return deque_->stldeque_.size(); }
114
115
116 size_t treeheight( size_t p_myheight ) const override;
117
118
119public: /* accessor */
121 size_t getWatermark() const { return deque_->watermark_.getWatermark(); }
122
123
130 void setSO( size_t p_index, SOp p_sop ) {
131 DBC_PRE( p_index < getSize() and p_sop != nullptr );
132
133 delete deque_->stldeque_[p_index];
134 deque_->stldeque_[p_index] = p_sop;
135
136 if( treeheight( 0 ) >= maxtreeheight )
137 inErrExit( semantics, "composite object tree height exceeds maximum" );
138
139 DBC_INV;
140 }
141
142
147 SOp at( size_t p_pos ) const {
148 DBC_PRE( p_pos < getSize() );
149
150 return deque_->stldeque_[ p_pos ];
151 }
152
153
158 SOp peek() const {
159 DBC_PRE( getSize() > 0 );
160
161 return deque_->stldeque_.back();
162 }
163
164
165public: /* other */
169 [[nodiscard]] SOp pop() {
170 DBC_PRE( getSize() > 0 );
171
172 SOp retval = deque_->stldeque_.back();
173 deque_->stldeque_.pop_back();
174
175 DBC_INV;
176 return retval;
177 }
178
179
182 void pop_delete() {
183 DBC_PRE( getSize() > 0 );
184
185 delete deque_->stldeque_.back();
186 deque_->stldeque_.pop_back();
187
188 DBC_INV;
189 }
190
191
195 void push( SOp p_o1 );
196
197
200 void push( std::initializer_list<SOp> p_list );
201
202
207 void push_front( SOp p_obj );
208
209
211 bool underflowcheck( size_t p_size ) const { return getSize() >= p_size; }
212
213
218 bool otchecker( std::initializer_list<OTCode> p_list ) const;
219
220
223 bool otchecker( OTCode p_code ) const {
224 DBC_PRE( !deque_->stldeque_.empty() );
225
226 return peek()->ot() == p_code;
227 }
228
229
235 bool countto( size_t & p_retval, OTCode p_code ) const;
236
237
242 SOp findValue( const SO * p_key ) const;
243
244
250 SOp findDict( const SO * p_key ) const;
251
252
258 [[nodiscard]] __int128 getI();
259
260
266 [[nodiscard]] __float128 getR();
267
268
274 [[nodiscard]] __float128 getIorRasR();
275
276
283 [[nodiscard]] size_t getISize_t();
284};
Semantic Object stacK.
Definition sok.h:44
bool invariant() const noexcept override
Checks class invariants.
Definition sok.h:68
SOp peek() const
Returns a copy of the SOp of the top most position.
Definition sok.h:158
void pop_delete()
Removes the top most object from SOK, and the referenced SO will be deleted.
Definition sok.h:182
size_t treeheight(size_t p_myheight) const override
Tree Height.
Definition sok.cpp:64
__int128 getI()
I from SOK.
Definition sok.cpp:139
OTCode ot() const override
Returns an OTCode.
Definition sok.h:101
void setSO(size_t p_index, SOp p_sop)
Setter for a stack object.
Definition sok.h:130
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
For operators '==' and 'pstack'.
Definition sok.cpp:49
SOK * clone() const override
Creates a new instance as copy with deep cloning.
Definition sok.cpp:37
__float128 getR()
R from SOK.
Definition sok.cpp:152
SOK()
Ctor.
Definition sok.h:83
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:222
bool otchecker(OTCode p_code) const
Checks SO classes on the operand stack against an OTCode.
Definition sok.h:223
size_t getWatermark() const
Returns the watermark.
Definition sok.h:121
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:176
bool underflowcheck(size_t p_size) const
Checks SOK against given size.
Definition sok.h:211
size_t getISize_t()
I from SOK as size_t.
Definition sok.cpp:165
std::shared_ptr< SOKDeque > deque_
The shared deque.
Definition sok.h:60
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:169
SOp at(size_t p_pos) const
Returns a copy of the SOp at the given position.
Definition sok.h:147
void push(SOp p_o1)
Pushes the object onto the SOK and transfers ownership to the SOK.
Definition sok.cpp:195
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
Getter for the number of characters or number of objects.
Definition sok.h:113
std::string type() const override
Returns a type name.
Definition sok.h:104
SOK * dup() const override
Creates a new instance as copy following the red book definition.
Definition sok.h:89
bool equal(const SO *p_other) const override
Equality.
Definition sok.h:107
Semantic Object.
Definition so.h:58
virtual OTCode ot() const =0
Returns an OTCode.
Composite Semantic Object.
Definition so.h:165
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:89
#define DBC_INV
Assert for invariant checks in member functions.
Definition dbc.h:84
#define DBC_PRE(XXX)
Assert for preconditions.
Definition dbc.h:78
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:74
@ semantics
infinite nesting of arrays; infinite loop loading values from dictionary;
Definition error.h:45
Miscellaneous definitions and functions.
constexpr size_t maxtreeheight
The maximum number of nesting levels of composite semantic objects.
Definition helper.h:78
The class SO - semantic object.
OTCode
OTCode - the Object Type Code.
Definition so.h:33
@ K
SOK.
@ o
SOo.
Semantic Object Stack - deque member class.
Definition sok.h:49
Watermark watermark_
The watermark for the size of the stack.
Definition sok.h:51
std::deque< SOp > stldeque_
The deque.
Definition sok.h:50
~SOKDeque()
Dtor.
Definition sok.h:54
The class Watermark.