-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.h
199 lines (167 loc) · 5.18 KB
/
context.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifndef CONTEXT_H
#define CONTEXT_H
#include "stack.h"
#include "types.h"
namespace amps
{
class context
{
user_map environment_;
gstack stack_;
size_t counter_;
public:
context() :
counter_(0)
{
}
~context() = default;
context(const context&) = delete;
context(context&&) = delete;
context &operator=(context&) = delete;
context &operator=(context&&) = delete;
void reset();
// --------------------------
// handles the prog counter
// --------------------------
void jump_to(size_t n);
const size_t &get_counter() const;
// --------------------------
// handles the context stack
// --------------------------
vobject_types stack_top_type() const;
std::string stack_pop_string_or(const std::string &opt);
number_t stack_pop_number_or(number_t opt);
object stack_top() const;
object stack_pop();
bool stack_empty() const;
bool stack_pop_bool_or(bool opt);
bool stack_pop_resolve_bool();
void stack_push(const object_t &obj);
void stack_clear();
bool stack_push_from_environment(const std::string &key);
bool stack_push_from_environment(const std::string &key,
size_t index);
bool stack_push_from_environment(const std::string &key,
const std::string &user_key);
// --------------------------
// handles the env. table
// --------------------------
bool environment_is_key_defined(const std::string &key) const;
void environment_setup(const user_map &data);
void environment_add_or_update(const std::string &key,
const user_var &data);
void environment_add_or_update(const std::string &key,
const std::string &dest_key,
size_t index);
void environment_erase(const std::string &key);
size_t environment_get_size(const std::string &key) const;
size_t environment_add_or_update(const std::string &key,
const std::string &dest_key,
const std::string &value,
size_t index);
bool environment_check_value(const std::string &key,
const user_var &data) const;
void environment_increment_value(const std::string &key);
};
inline void context::reset()
{
counter_ = 0;
environment_.clear();
stack_.clear();
}
inline void context::jump_to(size_t n)
{
counter_ = n;
}
inline const size_t &context::get_counter() const
{
return counter_;
}
inline vobject_types context::stack_top_type() const
{
return stack_.look_back().value().get_type();
}
inline object context::stack_top() const
{
if (stack_.empty()) {
return std::nullopt;
}
return stack_.look_back();
}
inline bool context::stack_empty() const
{
return stack_.empty();
}
inline object context::stack_pop()
{
return stack_.pop();
}
inline void context::stack_push(const object_t &obj)
{
stack_.push(obj);
}
inline void context::stack_clear()
{
stack_.clear();
}
inline std::string context::stack_pop_string_or(const std::string &opt)
{
object result = stack_.pop();
if (result == std::nullopt) {
return opt;
}
return result.value().get_string_or(opt);
}
inline number_t context::stack_pop_number_or(number_t opt)
{
object result = stack_.pop();
if (result == std::nullopt) {
return opt;
}
return result.value().get_number_or(opt);
}
inline bool context::stack_pop_bool_or(bool opt)
{
object result = stack_.pop();
if (result == std::nullopt) {
return opt;
}
return result.value().get_bool_or(opt);
}
inline bool context::stack_pop_resolve_bool()
{
object result = stack_.pop();
if (result == std::nullopt) {
return false;
}
if (result.value().get_type() == vobject_types::BOOL) {
return result.value().get_bool_or(false);
}
else if (result.value().get_type() == vobject_types::NUMBER) {
if (result.value().get_number_or(0) == 0) {
return false;
}
}
else if (result.value().get_type() == vobject_types::STRING) {
if (result.value().get_string_or("").size() == 0) {
return false;
}
}
return true;
}
inline void context::environment_setup(const user_map &data)
{
environment_ = data;
}
inline bool context::environment_is_key_defined(const std::string &key) const
{
return environment_.find(key) != environment_.end();
}
inline void context::environment_erase(const std::string &key)
{
if (environment_is_key_defined(key)) {
environment_.erase(environment_.find(key));
}
}
}
#endif // CONTEXT_H