-
Notifications
You must be signed in to change notification settings - Fork 3
/
variable_ops.c
331 lines (298 loc) · 7.64 KB
/
variable_ops.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "noheap/ht.c"
ht *global_hash_table;
////////////
// Locals //
////////////
// Function to swap two cells
void swap(DCLANG_PTR *a, DCLANG_PTR *b) {
DCLANG_PTR temp = *a;
*a = *b;
*b = temp;
}
// function to reverse an array
void reverse_array(DCLANG_PTR arr[], int n) {
// Initialize left to the beginning and right to the end
int left = 0, right = n;
// Iterate till left is less than right
while (left < right) {
// Swap the elements at left and right position
swap(&arr[left], &arr[right]);
// Increment the left pointer
left++;
// Decrement the right pointer
right--;
}
}
void _set_locals_var(DCLANG_FLT flt_idx)
{
int idx = (int) flt_idx;
locals_vals[locals_base_idx + idx] = dclang_pop();
}
void _processlocals()
{
char *token;
int locals_idx;
while (strcmp(token = get_token(), "}")) {
// Add key to ongoing array of keys
//printf("Token is: %s\n", token);
locals_keys[locals_base_idx + locals_idx] = token;
locals_idx += 1;
}
reverse_array(locals_keys + locals_base_idx, locals_idx - 1);
// For each key up to the count, pop the stack
// onto the locals_vals array:
for (int i=0; i<locals_idx; i++) {
prog[iptr].function.with_param = _set_locals_var;
prog[iptr++].param = (DCLANG_FLT) i;
}
}
void _get_locals_var(DCLANG_FLT flt_idx)
{
int idx = (int) flt_idx;
push(locals_vals[locals_base_idx + idx]);
}
/////////////
// Globals //
/////////////
void pokefunc()
{
if (data_stack_ptr < 2)
{
printf("! -- stack underflow! ");
return;
}
DCLANG_PTR idx = (DCLANG_PTR) dclang_pop();
if (idx < 0 || idx > NUMVARS)
{
printf("! -- variable slot number out-of-range!\n");
return;
}
DCLANG_FLT val = dclang_pop();
vars[idx] = val;
}
void peekfunc()
{
if (data_stack_ptr < 1)
{
printf("@ -- stack underflow! ");
return;
}
DCLANG_PTR idx = (DCLANG_PTR) dclang_pop();
if (idx < 0 || idx > NUMVARS)
{
printf("@ -- variable slot number %lu is out-of-range!\n", idx);
return;
}
push(vars[idx]);
}
/* implement constants */
void constantfunc()
{
const_keys[const_idx] = get_token();
const_vals[const_idx++] = dclang_pop();
}
void variablefunc()
{
DCLANG_PTR next_var = vars_idx++;
var_keys[var_map_idx] = get_token();
var_vals[var_map_idx++] = next_var;
}
void allotfunc()
{
if (data_stack_ptr < 1)
{
printf("allot -- stack underflow! ");
return;
}
vars_idx += (DCLANG_PTR) dclang_pop() - 1;
}
void createfunc()
{
variablefunc();
--vars_idx;
}
void commafunc()
{
if (data_stack_ptr < 1)
{
printf(", -- stack underflow! ");
return;
}
DCLANG_FLT val = dclang_pop();
vars[vars_idx++] = val;
}
void herefunc()
{
push((DCLANG_PTR) vars_idx);
}
// some helpers to show stuff
void showvars()
{
for (int x=0; x < var_map_idx; x++) {
printf("Variable %i: %s @ %li\n", x, var_keys[x], var_vals[x]);
}
}
void showconsts()
{
for (int x=0; x < const_idx; x++) {
printf("Constant %i: %s ==> %.19g\n", x, const_keys[x], const_vals[x]);
}
}
//////////////////////////////////////////////////////////
// global hash space, a la 'redis', but in local memory //
//////////////////////////////////////////////////////////
void _add_key(char *key){
if (hashwords_cnt > hashwords_size)
{
hashwords_size *= 2;
hashwords = dclang_realloc(hashwords, hashwords_size * sizeof(*hashwords));
}
hashwords[hashwords_cnt] = key;
++hashwords_cnt;
}
void hashsetfunc()
{
if (data_stack_ptr < 2)
{
printf("h! -- stack underflow! ");
return;
}
/* grab the key */
char *key = (const char *)(DCLANG_PTR) dclang_pop();
DCLANG_PTR key_addr = (DCLANG_PTR) key;
if (key_addr < MIN_STR || key_addr > MAX_STR)
{
perror("h! -- String address for hash key out-of-range.");
return;
}
/* grab the value */
void *value = (void *)(DCLANG_PTR) dclang_pop();
void *confirm = hset(global_hash_table, key, value);
if (confirm != NULL)
{
_add_key(key);
}
}
void hashgetfunc()
{
if (data_stack_ptr < 1)
{
printf("h@ -- stack underflow! ");
return;
}
/* grab the key */
char *key = (char *)(DCLANG_PTR) dclang_pop();
DCLANG_PTR key_addr = (DCLANG_PTR) key;
if (key_addr < MIN_STR || key_addr > MAX_STR)
{
perror("h@ -- String address for hash key out-of-range.");
return;
}
/* grab the value */
void *value = hget(global_hash_table, key);
if (value == NULL)
{
push(0);
} else {
push((DCLANG_PTR)(char *)value);
}
}
void hashkeysfunc()
{
if (data_stack_ptr < 1)
{
printf("hkeys -- need an integer index on stack. Stack underflow! ");
return;
}
/* grab the key index */
DCLANG_PTR keyidx = (DCLANG_PTR) dclang_pop();
push((DCLANG_PTR) hashwords[keyidx]);
}
// helper functions for sorting
int compare_doubles (const void *a, const void *b)
{
const double *da = (const double *) a;
const double *db = (const double *) b;
return (*da > *db) - (*da < *db);
}
int compare_strings (const void *a, const void *b)
{
const char *sa = (const char *) (DCLANG_PTR) * (DCLANG_FLT *) a;
const char *sb = (const char *) (DCLANG_PTR) * (DCLANG_FLT *) b;
return strcmp(sa, sb);
}
// end helper functions
void sortnumsfunc()
{
if (data_stack_ptr < 2)
{
printf("sortnums -- need <arrstart_index> <size> on the stack.\n");
return;
}
int size = (DCLANG_PTR) dclang_pop();
int arrstart = (DCLANG_PTR) dclang_pop();
qsort (vars+arrstart, size, sizeof(DCLANG_FLT), compare_doubles);
}
void sortstrsfunc()
{
if (data_stack_ptr < 2)
{
printf("sortstrs -- need <arrstart_index> <size> on the stack.\n");
}
int size = (DCLANG_PTR) dclang_pop();
int arrstart = (DCLANG_PTR) dclang_pop();
qsort (vars+arrstart, size, sizeof(DCLANG_FLT), compare_strings);
}
// environment variables:
void envgetfunc()
{
if (data_stack_ptr < 1)
{
printf("envget -- need <env_key> string on the stack.\n");
}
/* grab the key */
char *env_key = (char *)(DCLANG_PTR) dclang_pop();
DCLANG_PTR env_key_addr = (DCLANG_PTR) env_key;
if (env_key_addr < MIN_STR || env_key_addr > MAX_STR)
{
perror("envget -- String address for hash key out-of-range.");
return;
}
char *val = getenv(env_key);
DCLANG_PTR val_addr = (DCLANG_PTR) val;
if (val_addr > MAX_STR || MAX_STR == 0)
{
MAX_STR = val_addr;
}
if ((val_addr != 0) && (val_addr < MIN_STR || MIN_STR == 0))
{
MIN_STR = val_addr;
}
push(val_addr);
}
void envsetfunc()
{
if (data_stack_ptr < 2)
{
printf("envset -- need <env_val> <env_key> strings on the stack.\n");
}
// grab the key from the stack
char *env_key = (char *)(DCLANG_PTR) dclang_pop();
DCLANG_PTR env_key_addr = (DCLANG_PTR) env_key;
if (env_key_addr < MIN_STR || env_key_addr > MAX_STR)
{
perror("envset -- String address for environment key out-of-range.");
return;
}
// grab the value from the stack
char *env_val = (char *)(DCLANG_PTR) dclang_pop();
DCLANG_PTR env_val_addr = (DCLANG_PTR) env_val;
if (env_val_addr < MIN_STR || env_val_addr > MAX_STR)
{
perror("envset -- String address for environment value out-of-range.");
return;
}
// set the key's value
setenv(env_key, env_val, 1);
// no value put on stack -- only side effect
}