This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_variables.cpp
203 lines (144 loc) · 3.98 KB
/
user_variables.cpp
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
200
201
202
203
#include <iostream>
#include <string.h>
#include "calc_value.hpp"
#include "user_variables.hpp"
namespace vars {
// return the last variable on the list
UserVar* lastVar(UserVar* first) {
while (first->next != NULL)
first = first->next;
return first;
}
// delete all variables
void wipeAll(UserVar* first) {
UserVar *var1, *var2;
var1 = first->next;
// leap-frog and delete
while (var1) {
var2 = var1->next;
delete var1;
var1 = NULL;
if (var2 != NULL) {
var1 = var2->next;
delete var2;
}
}
first->next = NULL;
}
// add a variable to the linked list
UserVar* assignVar(UserVar* first, const char* name, CalcValue value) {
UserVar* var;
if (*name == ' ')
goto make_new_var;
var = findVar(first, name);
// making a new variable
if (var == NULL) {
make_new_var:
var = new UserVar(first, name, value);
lastVar(first)->next = var;
// changing the variable's value
} else
var->setValue(value);
return var;
}
UserVar* assignNewVar(UserVar* first, const char name[USERVAR_NAME_MAXLENGTH], CalcValue value){
// making a new variable
UserVar* var = new UserVar(first, name, value);
lastVar(first)->next = var;
return var;
}
// remove a variable from the linked list
void removeVar(UserVar* first, const char* name) {
if (!name || !first)
return;
// search the linked list for the object
while (first->next && first) {
if (strcmp(first->next->name, name) == 0) {
// get object address so it doesn't become unaccessable
UserVar *toBeDeleted = first->next;
// replace link
first->next = first->next->next;
// remove the node
delete toBeDeleted;
// we done
return;
} else // keep looking for it
first = first->next;
}
}
// returns pointer to given variable
UserVar* findVar(UserVar* first, const char* name) {
if (!name || !first)
return NULL;
first = first->next;
// search the linked list for the object
while (first != NULL)
if (strcmp(first->name, name) == 0)
return first;
else
first = first->next;
return NULL;
}
UserVar* findVar(std::vector<UserVar>& vars, const char* name) {
for (int i = vars.size() - 1; i >= 0; i--) {
UserVar *ret = findVar(&vars[i], name);
if (ret)
return ret;
}
return NULL;
}
// returns whether variable is in list or not
bool varExists(UserVar* first, const char* name) {
first = first->next;
while (first)
if (strcmp(first->name, name) == 0)
return true;
else
first = first->next;
return false;
}
bool varExists(std::vector<UserVar>& vars, const char* name) {
for (ssize_t i = vars.size() - 1; i >= 0; i--)
if (varExists(&vars[i], name))
return true;
return false;
}
// de-references the variable's
CalcValue* valueAtVar(UserVar* first, const char name[USERVAR_NAME_MAXLENGTH]) {
UserVar* var = findVar(first, name);
return var ?
var->val.type == CalcValue::REF ?
valueAtVar(first, var->val.string)
: &var->val
: NULL;
}
CalcValue* valueAtVar(std::vector<UserVar>& vars, const char name[USERVAR_NAME_MAXLENGTH]) {
UserVar* var = findVar(vars, name);
return var ?
var->val.type == CalcValue::REF ?
valueAtVar(vars, var->val.string)
: &var->val
: NULL;
}
// finds the last var in a reference chain
UserVar* lastVarInRefChain(UserVar* first, const char name[USERVAR_NAME_MAXLENGTH]) {
UserVar* var = findVar(first, name);
return var ? // var defined ?
var->val.type == CalcValue::REF ? // is it a ref ?
lastVarInRefChain(first, var->val.string)
: var
: NULL;
}
UserVar* lastVarInRefChain(std::vector<UserVar>& vars, const char name[USERVAR_NAME_MAXLENGTH]) {
UserVar* var = findVar(vars, name);
return var ?
var->val.type == CalcValue::REF ?
lastVarInRefChain(vars, var->val.string) // recursion to find ref-dest
: var // var found and isn't a ref
: NULL; // not defined
}
void clearScope(std::vector<UserVar>& scopes){
wipeAll(&scopes[scopes.size() - 1]);
scopes.pop_back();
}
}