-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.c
More file actions
218 lines (173 loc) · 4.42 KB
/
env.c
File metadata and controls
218 lines (173 loc) · 4.42 KB
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
/*
*
* env.c
* (Environment)
* Core of global object manipulation.
*
*/
/* SMPL headers */
#include "env.h"
#include "mem.h"
#include "state.h"
#include "limits.h"
/* C headers */
#include <stdlib.h>
#include <string.h>
static uint hash_name (char *name, uint len)
{
uint hash = 5381;
char c;
while(c = *name++)
hash = ((hash << 5) + hash) + c; // hash * 33 + c
// printf("hash: %d (i=%d)\n", hash, hash % SMPL_MAX_VARIABLES);
return hash % SMPL_MAX_VARIABLES;
}
/*
** Free the environment
*/
void spE_free (sp_State *S)
{
for(uint i = 0; i < SMPL_MAX_VARIABLES; i++)
{
if(S->vars[i] != NULL)
{
spO_free(S->vars[i]);
}
}
}
/*
** Push an new global with the given id of length 'len'
** and return it's object.
*/
sp_Object *spE_global_new (sp_State *S, char *id, uint len)
{
sp_Object *o = spO_new(S);
uint hash = hash_name(id, len);
if(S->vars[hash] != NULL)
{
// collision
}
S->vars[hash] = o;
return o;
}
/*
** Get an global with the given id of length 'len'.
** If it doesn't exist then create an new one
** and return its object.
*/
sp_Object *spE_global_ensure (sp_State *S, sp_ObjectType m, char *id, uint len)
{
sp_Object *o = spE_global_get(S, m, id, len);
if(o == NULL)
return spE_global_new(S, id, len);
return o;
}
/*
*
* Getting global object of basic type
*
*/
/*
** Get an global with the given id of length 'len'
** and object type matching mask 'm'.
** Return it's object.
*/
sp_Object *spE_global_get (sp_State *S, sp_ObjectType m, char *id, uint len)
{
uint hash = hash_name(id, len);
sp_Object *o = S->vars[hash];
if(o)
if(obj_type(o) & m)
return o;
return NULL;
}
/*
** Get an global of string type with id 'vs' of length 'vl'
** and return its object.
*/
sp_Object *spE_global_get_string (sp_State *S, char *vs, uint vl)
{
return spE_global_get(S, OT_STR, vs, vl);
}
/*
** Get an global of number type with id 'vs' of length 'vl'
** and return its object.
*/
sp_Object *spE_global_get_number (sp_State *S, char *vs, uint vl)
{
return spE_global_get(S, OT_NUM, vs, vl);
}
/*
** Get an global of function type with id 'vs' of length 'vl'
** and return its object.
*/
sp_Object *spE_global_get_function (sp_State *S, char *vs, uint vl)
{
return spE_global_get(S, OT_FUNC, vs, vl);
}
/*
** Get an global of C function type with id 'vs' of length 'vl'
** and return its object.
*/
sp_Object *spE_global_get_CFunction (sp_State *S, char *vs, uint vl)
{
return spE_global_get(S, OT_CFUNC, vs, vl);
}
sp_Object *spE_global_get_dim (sp_State *S, char *vs, uint vl)
{
return spE_global_get(S, OT_DIM, vs, vl);
}
/*
*
* Setting global objects to basic types
*
*/
/*
** Set an global with id 'vs' of length 'vl'
** and object type matching to the given mask 'm'
** to the given object 'o'.
*/
void spE_global_set (sp_State *S, sp_ObjectType m, char *vs, uint vl, sp_Object *o)
{
spO_copy(S, spE_global_ensure(S, m, vs, vl), o);
}
/*
** Set an global with id 'vs' of length 'vl'
** and object type matching to the given mask 'm'
** to the given C string of source 'ss' and length 'sl'.
*/
void spE_global_set_string (sp_State *S, sp_ObjectType m, char *vs, uint vl, char *ss, uint sl)
{
spO_set_string(S, spE_global_ensure(S, m, vs, vl), ss, sl);
}
/*
** Set an global with id 'vs' of length 'vl'
** and object type matching to the given mask 'm'
** to the given number 'n'.
*/
void spE_global_set_number (sp_State *S, sp_ObjectType m, char *vs, uint vl, sp_Number n)
{
spO_set_number(spE_global_ensure(S, m, vs, vl), n);
}
/*
** Set an global with id 'vs' of length 'vl'
** and object type matching to the given mask 'm'
** to the given function with source 'i' and argument 'arg'.
*/
void spE_global_set_function (sp_State *S, sp_ObjectType m, char *vs, uint vl, uint i, sp_Object *arg)
{
spO_set_function(spE_global_ensure(S, m, vs, vl), i, arg);
}
/*
** Set an global with id 'vs' of length 'vl'
** and object type matching to the given mask 'm'
** to the given C function 'cf'.
*/
void spE_global_set_CFunction (sp_State *S, sp_ObjectType m, char *vs, uint vl, sp_CFunction cf)
{
spO_set_CFunction(spE_global_ensure(S, m, vs, vl), cf);
}
void spE_global_set_dim (sp_State *S, sp_ObjectType m, char *vs, uint vl, sp_Object **v, uint32_t len)
{
spO_set_dim(S, spE_global_ensure(S, m, vs, vl), v, len);
}