annotate src/paranormal/libcalc/dict.c @ 1181:2f3397e53b05 trunk

[svn] - memory cleanups, purify-assisted.
author nenolod
date Sat, 09 Jun 2007 06:16:03 -0700
parents e9b2d21cf078
children 6d749f573b3b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
1 /* dict.c -- symbol dictionary structures
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
2 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
3 * Copyright (C) 2001 Janusz Gregorczyk <jgregor@kki.net.pl>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
4 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
5 * This file is part of xvs.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
6 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
10 * (at your option) any later version.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
11 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful,
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
15 * GNU General Public License for more details.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
16 *
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
20 */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
21
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
22 #define V_SPACE_INIT 8
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
23 #define V_SPACE_INCR 8
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
24
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
25 #include <glib.h>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
26 #include <string.h>
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
27
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
28 #include "dict.h"
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
29
1175
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
30 static int global_dict_initialized = 0;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
31 static symbol_dict_t global_dict;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
32
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
33 static void more_variables (symbol_dict_t *dict) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
34 var_t *new_var;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
35
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
36 dict->v_space += V_SPACE_INCR;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
37
1181
2f3397e53b05 [svn] - memory cleanups, purify-assisted.
nenolod
parents: 1178
diff changeset
38 new_var = g_new(var_t, dict->v_space + 1);
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
39 memcpy (new_var, dict->variables, dict->v_count * sizeof(var_t));
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
40 g_free (dict->variables);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
41
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
42 dict->variables = new_var;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
43 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
44
1175
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
45 static int dict_define_variable (symbol_dict_t *dict, const char *name) {
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
46 var_t *var;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
47
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
48 if (dict->v_count >= dict->v_space)
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
49 more_variables (dict);
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
50
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
51 var = &dict->variables[dict->v_count];
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
52
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
53 var->value = 0.0;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
54 var->name = g_strdup (name);
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
55
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
56 return dict->v_count++;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
57 }
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
58
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
59 symbol_dict_t *dict_new (void) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
60 symbol_dict_t *dict;
1175
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
61
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
62 if (global_dict_initialized != 1) {
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
63 int i;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
64
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
65 global_dict.v_count = 0;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
66 global_dict.v_space = V_SPACE_INIT;
1181
2f3397e53b05 [svn] - memory cleanups, purify-assisted.
nenolod
parents: 1178
diff changeset
67 global_dict.variables = (var_t *) g_new(var_t, global_dict.v_space + 1);
1175
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
68 global_dict_initialized = 1;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
69
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
70 for (i = 0; i < 100; i++) {
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
71 gchar tmpbuf[40];
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
72 snprintf(tmpbuf, 40, "global_reg%d", i);
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
73 dict_define_variable(&global_dict, tmpbuf);
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
74 }
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
75 }
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
76
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
77 dict = g_new(symbol_dict_t, 1);
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
78
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
79 /* Allocate space for variables. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
80 dict->v_count = 0;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
81 dict->v_space = V_SPACE_INIT;
1181
2f3397e53b05 [svn] - memory cleanups, purify-assisted.
nenolod
parents: 1178
diff changeset
82 dict->variables = (var_t *) g_new (var_t, dict->v_space + 1);
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
83
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
84 return dict;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
85 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
86
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
87 void dict_free (symbol_dict_t *dict) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
88 int i;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
89
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
90 if (!dict)
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
91 return;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
92
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
93 /* Free memory used by variables. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
94 for (i = 0; i < dict->v_count; i++)
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
95 g_free (dict->variables[i].name);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
96 g_free (dict->variables);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
97
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
98 g_free (dict);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
99 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
100
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
101 int dict_lookup (symbol_dict_t *dict, const char *name) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
102 int i;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
103
1175
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
104 for (i = 0; i < global_dict.v_count; i++) {
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
105 if (strcmp (global_dict.variables[i].name, name) == 0)
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
106 return -i;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
107 }
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
108
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
109 for (i = 0; i < dict->v_count; i++) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
110 if (strcmp (dict->variables[i].name, name) == 0)
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
111 return i;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
112 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
113
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
114 /* Not defined -- define a new variable. */
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
115 return dict_define_variable (dict, name);
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
116 }
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
117
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
118 double *dict_variable (symbol_dict_t *dict, const char *var_name) {
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
119 int id = dict_lookup (dict, var_name);
1175
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
120
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
121 /* global variables are presented as negative offset. negating
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
122 * a negative number results in a positive offset. --nenolod
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
123 */
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
124 if (id < 0)
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
125 return &global_dict.variables[-id].value;
7ae024f5d91b [svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
nenolod
parents: 282
diff changeset
126
282
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
127 return &dict->variables[id].value;
3e160f6c04d2 [svn] - add libcalc from xvs and link to build
nenolod
parents:
diff changeset
128 }