Debian Codegraph @

common/core_variables.cc

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "core_globals.h"
22 #include "core_helpers.h"
23 #include "core_display.h"
24 #include "core_variables.h"
25
26
27 // We cache vartype_real, vartype_complex, and vartype_string instances, to
28 // cut down on the malloc/free overhead.
29
30 #define POOLSIZE 10
31 static vartype_real *realpool[POOLSIZE];
32 static vartype_complex *complexpool[POOLSIZE];
33 static vartype_string *stringpool[POOLSIZE];
34 static int realpool_size = 0;
35 static int complexpool_size = 0;
36 static int stringpool_size = 0;
37
38 vartype *new_real(phloat value) {
39 vartype_real *r;
40 if (realpool_size > 0) {
41 r = realpool[--realpool_size];
42 } else {
43 r = (vartype_real *) malloc(sizeof(vartype_real));
44 if (r == NULL)
45 return NULL;
46 r->type = TYPE_REAL;
47 }
48 r->x = value;
49 return (vartype *) r;
50 }
51
52 vartype *new_complex(phloat re, phloat im) {
53 vartype_complex *c;
54 if (complexpool_size > 0) {
55 c = complexpool[--complexpool_size];
56 } else {
57 c = (vartype_complex *) malloc(sizeof(vartype_complex));
58 if (c == NULL)
59 return NULL;
60 c->type = TYPE_COMPLEX;
61 }
62 c->re = re;
63 c->im = im;
64 return (vartype *) c;
65 }
66
67 vartype *new_string(const char *text, int length) {
68 char *dbuf;
69 if (length > SSLENV) {
70 dbuf = (char *) malloc(length);
71 if (dbuf == NULL)
72 return NULL;
73 }
74 vartype_string *s;
75 if (stringpool_size > 0) {
76 s = stringpool[--stringpool_size];
77 } else {
78 s = (vartype_string *) malloc(sizeof(vartype_string));
79 if (s == NULL) {
80 if (length > SSLENV)
81 free(dbuf);
82 return NULL;
83 }
84 s->type = TYPE_STRING;
85 }
86 s->length = length;
87 if (length > SSLENV)
88 s->t.ptr = dbuf;
89 if (text != NULL)
90 memcpy(length > SSLENV ? s->t.ptr : s->t.buf, text, length);
91 return (vartype *) s;
92 }
93
94 vartype *new_realmatrix(int4 rows, int4 columns) {
95 double d_bytes = ((double) rows) * ((double) columns) * sizeof(phloat);
96 if (((double) (int4) d_bytes) != d_bytes)
97 return NULL;
98
99 vartype_realmatrix *rm = (vartype_realmatrix *)
100 malloc(sizeof(vartype_realmatrix));
101 if (rm == NULL)
102 return NULL;
103 int4 i, sz;
104 rm->type = TYPE_REALMATRIX;
105 rm->rows = rows;
106 rm->columns = columns;
107 sz = rows * columns;
108 rm->array = (realmatrix_data *) malloc(sizeof(realmatrix_data));
109 if (rm->array == NULL) {
110 free(rm);
111 return NULL;
112 }
113 rm->array->data = (phloat *) malloc(sz * sizeof(phloat));
114 if (rm->array->data == NULL) {
115 free(rm->array);
116 free(rm);
117 return NULL;
118 }
119 rm->array->is_string = (char *) malloc(sz);
120 if (rm->array->is_string == NULL) {
121 free(rm->array->data);
122 free(rm->array);
123 free(rm);
124 return NULL;
125 }
126 for (i = 0; i < sz; i++)
127 rm->array->data[i] = 0;
128 memset(rm->array->is_string, 0, sz);
129 rm->array->refcount = 1;
130 return (vartype *) rm;
131 }
132
133 vartype *new_complexmatrix(int4 rows, int4 columns) {
134 double d_bytes = ((double) rows) * ((double) columns) * sizeof(phloat) * 2;
135 if (((double) (int4) d_bytes) != d_bytes)
136 return NULL;
137
138 vartype_complexmatrix *cm = (vartype_complexmatrix *)
139 malloc(sizeof(vartype_complexmatrix));
140 if (cm == NULL)
141 return NULL;
142 int4 i, sz;
143 cm->type = TYPE_COMPLEXMATRIX;
144 cm->rows = rows;
145 cm->columns = columns;
146 sz = rows * columns * 2;
147 cm->array = (complexmatrix_data *) malloc(sizeof(complexmatrix_data));
148 if (cm->array == NULL) {
149 free(cm);
150 return NULL;
151 }
152 cm->array->data = (phloat *) malloc(sz * sizeof(phloat));
153 if (cm->array->data == NULL) {
154 free(cm->array);
155 free(cm);
156 return NULL;
157 }
158 for (i = 0; i < sz; i++)
159 cm->array->data[i] = 0;
160 cm->array->refcount = 1;
161 return (vartype *) cm;
162 }
163
164 vartype *new_list(int4 size) {
165 vartype_list *list = (vartype_list *) malloc(sizeof(vartype_list));
166 if (list == NULL)
167 return NULL;
168 list->type = TYPE_LIST;
169 list->size = size;
170 list->array = (list_data *) malloc(sizeof(list_data));
171 if (list->array == NULL) {
172 free(list);
173 return NULL;
174 }
175 list->array->data = (vartype **) malloc(size * sizeof(vartype *));
176 if (list->array->data == NULL && size != 0) {
177 free(list->array);
178 free(list);
179 return NULL;
180 }
181 memset(list->array->data, 0, size * sizeof(vartype *));
182 list->array->refcount = 1;
183 return (vartype *) list;
184 }
185
186 void free_vartype(vartype *v) {
187 if (v == NULL)
188 return;
189 switch (v->type) {
190 case TYPE_REAL: {
191 if (realpool_size < POOLSIZE)
192 realpool[realpool_size++] = (vartype_real *) v;
193 else
194 free(v);
195 break;
196 }
197 case TYPE_COMPLEX: {
198 if (complexpool_size < POOLSIZE)
199 complexpool[complexpool_size++] = (vartype_complex *) v;
200 else
201 free(v);
202 break;
203 }
204 case TYPE_STRING: {
205 vartype_string *s = (vartype_string *) v;
206 if (s->length > SSLENV)
207 free(s->t.ptr);
208 if (stringpool_size < POOLSIZE)
209 stringpool[stringpool_size++] = s;
210 else
211 free(s);
212 break;
213 }
214 case TYPE_REALMATRIX: {
215 vartype_realmatrix *rm = (vartype_realmatrix *) v;
216 if (--(rm->array->refcount) == 0) {
217 int4 sz = rm->rows * rm->columns;
218 free_long_strings(rm->array->is_string, rm->array->data, sz);
219 free(rm->array->data);
220 free(rm->array->is_string);
221 free(rm->array);
222 }
223 free(rm);
224 break;
225 }
226 case TYPE_COMPLEXMATRIX: {
227 vartype_complexmatrix *cm = (vartype_complexmatrix *) v;
228 if (--(cm->array->refcount) == 0) {
229 free(cm->array->data);
230 free(cm->array);
231 }
232 free(cm);
233 break;
234 }
235 case TYPE_LIST: {
236 vartype_list *list = (vartype_list *) v;
237 if (--(list->array->refcount) == 0) {
238 for (int4 i = 0; i < list->size; i++)
239 free_vartype(list->array->data[i]);
240 free(list->array->data);
241 free(list->array);
242 }
243 free(list);
244 break;
245 }
246 }
247 }
248
249 void clean_vartype_pools() {
250 while (realpool_size > 0)
251 free(realpool[--realpool_size]);
252 while (complexpool_size > 0)
253 free(complexpool[--complexpool_size]);
254 while (stringpool_size > 0)
255 free(stringpool[--stringpool_size]);
256 }
257
258 void free_long_strings(char *is_string, phloat *data, int4 n) {
259 for (int4 i = 0; i < n; i++)
260 if (is_string[i] == 2)
261 free(*(void **) &data[i]);
262 }
263
264 void get_matrix_string(vartype_realmatrix *rm, int i, char **text, int4 *length) {
265 if (rm->array->is_string[i] == 1) {
266 char *t = (char *) &rm->array->data[i];
267 *text = t + 1;
268 *length = *t;
269 } else {
270 int4 *p = *(int4 **) &rm->array->data[i];
271 *text = (char *) (p + 1);
272 *length = *p;
273 }
274 }
275
276 void get_matrix_string(const vartype_realmatrix *rm, int i, const char **text, int4 *length) {
277 get_matrix_string((vartype_realmatrix *) rm, i, (char **) text, length);
278 }
279
280 bool put_matrix_string(vartype_realmatrix *rm, int i, const char *text, int4 length) {
281 char *ptext;
282 int4 plength;
283 if (rm->array->is_string[i] != 0) {
284 get_matrix_string(rm, i, &ptext, &plength);
285 if (plength == length) {
286 memcpy(ptext, text, length);
287 return true;
288 }
289 }
290 if (length > SSLENM) {
291 int4 *p = (int4 *) malloc(length + 4);
292 if (p == NULL)
293 return false;
294 *p = length;
295 memcpy(p + 1, text, length);
296 if (rm->array->is_string[i] == 2)
297 free(*(void **) &rm->array->data[i]);
298 *(int4 **) &rm->array->data[i] = p;
299 rm->array->is_string[i] = 2;
300 } else {
301 void *oldptr = rm->array->is_string[i] == 2 ? *(void **) &rm->array->data[i] : NULL;
302 char *t = (char *) &rm->array->data[i];
303 t[0] = length;
304 memmove(t + 1, text, length);
305 rm->array->is_string[i] = 1;
306 if (oldptr != NULL)
307 free(oldptr);
308 }
309 return true;
310 }
311
312 vartype *dup_vartype(const vartype *v) {
313 if (v == NULL)
314 return NULL;
315 switch (v->type) {
316 case TYPE_REAL: {
317 vartype_real *r = (vartype_real *) v;
318 return new_real(r->x);
319 }
320 case TYPE_COMPLEX: {
321 vartype_complex *c = (vartype_complex *) v;
322 return new_complex(c->re, c->im);
323 }
324 case TYPE_REALMATRIX: {
325 vartype_realmatrix *rm = (vartype_realmatrix *) v;
326 vartype_realmatrix *rm2 = (vartype_realmatrix *)
327 malloc(sizeof(vartype_realmatrix));
328 if (rm2 == NULL)
329 return NULL;
330 *rm2 = *rm;
331 rm->array->refcount++;
332 return (vartype *) rm2;
333 }
334 case TYPE_COMPLEXMATRIX: {
335 vartype_complexmatrix *cm = (vartype_complexmatrix *) v;
336 vartype_complexmatrix *cm2 = (vartype_complexmatrix *)
337 malloc(sizeof(vartype_complexmatrix));
338 if (cm2 == NULL)
339 return NULL;
340 *cm2 = *cm;
341 cm->array->refcount++;
342 return (vartype *) cm2;
343 }
344 case TYPE_STRING: {
345 vartype_string *s = (vartype_string *) v;
346 return new_string(s->txt(), s->length);
347 }
348 case TYPE_LIST: {
349 vartype_list *list = (vartype_list *) v;
350 vartype_list *list2 = (vartype_list *) malloc(sizeof(vartype_list));
351 if (list2 == NULL)
352 return NULL;
353 *list2 = *list;
354 list->array->refcount++;
355 return (vartype *) list2;
356 }
357 default:
358 return NULL;
359 }
360 }
361
362 bool disentangle(vartype *v) {
363 switch (v->type) {
364 case TYPE_REALMATRIX: {
365 vartype_realmatrix *rm = (vartype_realmatrix *) v;
366 if (rm->array->refcount == 1)
367 return true;
368 else {
369 realmatrix_data *md = (realmatrix_data *)
370 malloc(sizeof(realmatrix_data));
371 if (md == NULL)
372 return false;
373 int4 sz = rm->rows * rm->columns;
374 int4 i;
375 md->data = (phloat *) malloc(sz * sizeof(phloat));
376 if (md->data == NULL) {
377 free(md);
378 return false;
379 }
380 md->is_string = (char *) malloc(sz);
381 if (md->is_string == NULL) {
382 free(md->data);
383 free(md);
384 return false;
385 }
386 for (i = 0; i < sz; i++) {
387 md->is_string[i] = rm->array->is_string[i];
388 if (md->is_string[i] == 2) {
389 int4 *sp = *(int4 **) &rm->array->data[i];
390 int4 len = *sp + 4;
391 int4 *dp = (int4 *) malloc(len);
392 if (dp == NULL) {
393 free_long_strings(md->is_string, md->data, i);
394 free(md->is_string);
395 free(md->data);
396 free(md);
397 return false;
398 }
399 memcpy(dp, sp, len);
400 *(int4 **) &md->data[i] = dp;
401 } else {
402 md->data[i] = rm->array->data[i];
403 }
404 }
405 md->refcount = 1;
406 rm->array->refcount--;
407 rm->array = md;
408 return true;
409 }
410 }
411 case TYPE_COMPLEXMATRIX: {
412 vartype_complexmatrix *cm = (vartype_complexmatrix *) v;
413 if (cm->array->refcount == 1)
414 return true;
415 else {
416 complexmatrix_data *md = (complexmatrix_data *)
417 malloc(sizeof(complexmatrix_data));
418 if (md == NULL)
419 return false;
420 int4 sz = cm->rows * cm->columns * 2;
421 int4 i;
422 md->data = (phloat *) malloc(sz * sizeof(phloat));
423 if (md->data == NULL) {
424 free(md);
425 return false;
426 }
427 for (i = 0; i < sz; i++)
428 md->data[i] = cm->array->data[i];
429 md->refcount = 1;
430 cm->array->refcount--;
431 cm->array = md;
432 return true;
433 }
434 }
435 case TYPE_LIST: {
436 vartype_list *list = (vartype_list *) v;
437 if (list->array->refcount == 1)
438 return true;
439 else {
440 list_data *ld = (list_data *) malloc(sizeof(list_data));
441 if (ld == NULL)
442 return false;
443 ld->data = (vartype **) malloc(list->size * sizeof(vartype *));
444 if (ld->data == NULL && list->size != 0) {
445 free(ld);
446 return false;
447 }
448 for (int4 i = 0; i < list->size; i++) {
449 vartype *vv = list->array->data[i];
450 if (vv != NULL) {
451 vv = dup_vartype(vv);
452 if (vv == NULL) {
453 for (int4 j = 0; j < i; j++)
454 free_vartype(ld->data[j]);
455 free(ld->data);
456 free(ld);
457 return false;
458 }
459 }
460 ld->data[i] = vv;
461 }
462 ld->refcount = 1;
463 list->array->refcount--;
464 list->array = ld;
465 return true;
466 }
467 }
468 case TYPE_REAL:
469 case TYPE_COMPLEX:
470 case TYPE_STRING:
471 default:
472 return true;
473 }
474 }
475
476 int lookup_var(const char *name, int namelength) {
477 int i, j;
478 for (i = vars_count - 1; i >= 0; i--) {
479 if ((vars[i].flags & (VAR_HIDDEN | VAR_PRIVATE)) != 0)
480 continue;
481 if (vars[i].length == namelength) {
482 for (j = 0; j < namelength; j++)
483 if (vars[i].name[j] != name[j])
484 goto nomatch;
485 return i;
486 }
487 nomatch:;
488 }
489 return -1;
490 }
491
492 vartype *recall_var(const char *name, int namelength) {
493 int varindex = lookup_var(name, namelength);
494 if (varindex == -1)
495 return NULL;
496 else
497 return vars[varindex].value;
498 }
499
500 bool ensure_var_space(int n) {
501 int nc = vars_count + n;
502 if (nc > vars_capacity) {
503 var_struct *nv = (var_struct *) realloc(vars, nc * sizeof(var_struct));
504 if (nv == NULL)
505 return false;
506 vars_capacity = nc;
507 vars = nv;
508 }
509 return true;
510 }
511
512 int store_var(const char *name, int namelength, vartype *value, bool local) {
513 int varindex = lookup_var(name, namelength);
514 int i;
515 if (varindex == -1) {
516 /* Name not found; create new global or local */
517 if (vars_count == vars_capacity) {
518 int nc = vars_capacity + 25;
519 var_struct *nv = (var_struct *) realloc(vars, nc * sizeof(var_struct));
520 if (nv == NULL)
521 return ERR_INSUFFICIENT_MEMORY;
522 vars_capacity = nc;
523 vars = nv;
524 }
525 varindex = vars_count++;
526 vars[varindex].length = namelength;
527 for (i = 0; i < namelength; i++)
528 vars[varindex].name[i] = name[i];
529 vars[varindex].level = local ? get_rtn_level() : -1;
530 vars[varindex].flags = 0;
531 } else if (local && vars[varindex].level < get_rtn_level()) {
532 /* Create local that hides an existing variable */
533 if (vars_count == vars_capacity) {
534 int nc = vars_capacity + 25;
535 var_struct *nv = (var_struct *) realloc(vars, nc * sizeof(var_struct));
536 if (nv == NULL)
537 return ERR_INSUFFICIENT_MEMORY;
538 vars_capacity = nc;
539 vars = nv;
540 }
541 vars[varindex].flags |= VAR_HIDDEN;
542 varindex = vars_count++;
543 vars[varindex].length = namelength;
544 for (i = 0; i < namelength; i++)
545 vars[varindex].name[i] = name[i];
546 vars[varindex].level = get_rtn_level();
547 vars[varindex].flags = VAR_HIDING;
548 } else {
549 /* Update existing variable */
550 if (matedit_mode == 1 &&
551 matedit_level == vars[varindex].level &&
552 string_equals(name, namelength, matedit_name, matedit_length)) {
553 if (value->type == TYPE_REALMATRIX
554 || value->type == TYPE_COMPLEXMATRIX
555 || value->type == TYPE_LIST) {
556 matedit_i = matedit_j = 0;
557 } else {
558 matedit_mode = 0;
559 free(matedit_stack);
560 matedit_stack = NULL;
561 matedit_stack_depth = 0;
562 }
563 }
564 free_vartype(vars[varindex].value);
565 }
566 vars[varindex].value = value;
567 update_catalog();
568 return ERR_NONE;
569 }
570
571 bool purge_var(const char *name, int namelength, bool global, bool local) {
572 int varindex = lookup_var(name, namelength);
573 if (varindex == -1)
574 return true;
575 if (vars[varindex].level == -1) {
576 if (!global)
577 // Asked to delete a local, but found a global;
578 // not an error, but don't delete.
579 return true;
580 } else {
581 if (!local)
582 // Asked to delete a global, but found a local;
583 // that's an error.
584 return false;
585 if (vars[varindex].level != get_rtn_level())
586 // Found a local at a lower level;
587 // not an error, but don't delete.
588 return true;
589 }
590 if (matedit_mode == 1
591 && matedit_level == vars[varindex].level
592 && string_equals(matedit_name, matedit_length, name, namelength)) {
593 matedit_mode = 0;
594 free(matedit_stack);
595 matedit_stack = NULL;
596 matedit_stack_depth = 0;
597 }
598 free_vartype(vars[varindex].value);
599 if ((vars[varindex].flags & VAR_HIDING) != 0) {
600 for (int i = varindex - 1; i >= 0; i--)
601 if ((vars[i].flags & VAR_HIDDEN) != 0 && string_equals(vars[i].name, vars[i].length, name, namelength)) {
602 vars[i].flags &= ~VAR_HIDDEN;
603 break;
604 }
605 }
606 for (int i = varindex; i < vars_count - 1; i++)
607 vars[i] = vars[i + 1];
608 vars_count--;
609 update_catalog();
610 return true;
611 }
612
613 void purge_all_vars() {
614 int i;
615 for (i = 0; i < vars_count; i++)
616 free_vartype(vars[i].value);
617 vars_count = 0;
618 }
619
620 bool vars_exist(int section) {
621 int i;
622 for (i = 0; i < vars_count; i++) {
623 if ((vars[i].flags & (VAR_HIDDEN | VAR_PRIVATE)) != 0)
624 continue;
625 if (section == -1)
626 return true;
627 switch (vars[i].value->type) {
628 case TYPE_REAL:
629 if (section == CATSECT_REAL)
630 return true;
631 else
632 break;
633 case TYPE_STRING:
634 if (section == CATSECT_REAL || section == CATSECT_LIST_STR_ONLY)
635 return true;
636 else
637 break;
638 case TYPE_COMPLEX:
639 if (section == CATSECT_CPX)
640 return true;
641 else
642 break;
643 case TYPE_REALMATRIX:
644 case TYPE_COMPLEXMATRIX:
645 if (section == CATSECT_MAT || section == CATSECT_MAT_LIST)
646 return true;
647 else
648 break;
649 case TYPE_LIST:
650 if (section == CATSECT_LIST_STR_ONLY || section == CATSECT_MAT_LIST || section == CATSECT_LIST)
651 return true;
652 else
653 break;
654 }
655 }
656 return false;
657 }
658
659 bool contains_strings(const vartype_realmatrix *rm) {
660 int4 size = rm->rows * rm->columns;
661 for (int4 i = 0; i < size; i++)
662 if (rm->array->is_string[i] != 0)
663 return true;
664 return false;
665 }
666
667 /* This is only used by core_linalg1, and does not deal with strings,
668 * even when copying a real matrix to a real matrix. It returns an
669 * error if any are encountered.
670 */
671 int matrix_copy(vartype *dst, const vartype *src) {
672 if (src->type == TYPE_REALMATRIX) {
673 vartype_realmatrix *s = (vartype_realmatrix *) src;
674 if (dst->type == TYPE_REALMATRIX) {
675 vartype_realmatrix *d = (vartype_realmatrix *) dst;
676 if (s->rows != d->rows || s->columns != d->columns)
677 return ERR_DIMENSION_ERROR;
678 if (contains_strings(s))
679 return ERR_ALPHA_DATA_IS_INVALID;
680 int4 size = s->rows * s->columns;
681 free_long_strings(d->array->is_string, d->array->data, size);
682 memset(d->array->is_string, 0, size);
683 memcpy((void *) d->array->data, (const void *) s->array->data, size * sizeof(phloat));
684 return ERR_NONE;
685 } else if (dst->type == TYPE_COMPLEXMATRIX) {
686 vartype_complexmatrix *d = (vartype_complexmatrix *) dst;
687 if (s->rows != d->rows || s->columns != d->columns)
688 return ERR_DIMENSION_ERROR;
689 if (contains_strings(s))
690 return ERR_ALPHA_DATA_IS_INVALID;
691 int4 size = s->rows * s->columns;
692 for (int4 i = 0; i < size; i++) {
693 d->array->data[2 * i] = s->array->data[i];
694 d->array->data[2 * i + 1] = 0;
695 }
696 return ERR_NONE;
697 } else
698 return ERR_INVALID_TYPE;
699 } else if (src->type == TYPE_COMPLEXMATRIX
700 && dst->type == TYPE_COMPLEXMATRIX) {
701 vartype_complexmatrix *s = (vartype_complexmatrix *) src;
702 vartype_complexmatrix *d = (vartype_complexmatrix *) dst;
703 if (s->rows != d->rows || s->columns != d->columns)
704 return ERR_DIMENSION_ERROR;
705 int4 size = s->rows * s->columns * 2;
706 memcpy((void *) d->array->data, (const void *) s->array->data, size * sizeof(phloat));
707 return ERR_NONE;
708 } else
709 return ERR_INVALID_TYPE;
710 }
711
712 static int lookup_private_var(const char *name, int namelength) {
713 int level = get_rtn_level();
714 int i, j;
715 for (i = vars_count - 1; i >= 0; i--) {
716 int vlevel = vars[i].level;
717 if (vlevel == -1)
718 continue;
719 if (vlevel < level)
720 break;
721 if ((vars[i].flags & VAR_PRIVATE) == 0)
722 continue;
723 if (vars[i].length == namelength) {
724 for (j = 0; j < namelength; j++)
725 if (vars[i].name[j] != name[j])
726 goto nomatch;
727 return i;
728 }
729 nomatch:;
730 }
731 return -1;
732 }
733
734 vartype *recall_private_var(const char *name, int namelength) {
735 int varindex = lookup_private_var(name, namelength);
736 if (varindex == -1)
737 return NULL;
738 else
739 return vars[varindex].value;
740 }
741
742 vartype *recall_and_purge_private_var(const char *name, int namelength) {
743 int varindex = lookup_private_var(name, namelength);
744 if (varindex == -1)
745 return NULL;
746 vartype *ret = vars[varindex].value;
747 for (int i = varindex; i < vars_count - 1; i++)
748 vars[i] = vars[i + 1];
749 vars_count--;
750 return ret;
751 }
752
753 int store_private_var(const char *name, int namelength, vartype *value) {
754 int varindex = lookup_private_var(name, namelength);
755 int i;
756 if (varindex == -1) {
757 if (vars_count == vars_capacity) {
758 int nc = vars_capacity + 25;
759 var_struct *nv = (var_struct *) realloc(vars, nc * sizeof(var_struct));
760 if (nv == NULL)
761 return ERR_INSUFFICIENT_MEMORY;
762 vars_capacity = nc;
763 vars = nv;
764 }
765 varindex = vars_count++;
766 vars[varindex].length = namelength;
767 for (i = 0; i < namelength; i++)
768 vars[varindex].name[i] = name[i];
769 vars[varindex].level = get_rtn_level();
770 vars[varindex].flags = VAR_PRIVATE;
771 } else {
772 free_vartype(vars[varindex].value);
773 }
774 vars[varindex].value = value;
775 return ERR_NONE;
776 }