Skip to content

Commit 24cb149

Browse files
committed
Wrap all flisp global states
1 parent 00589c8 commit 24cb149

19 files changed

+3014
-2908
lines changed

src/ast.c

Lines changed: 161 additions & 145 deletions
Large diffs are not rendered by default.

src/flisp/builtins.c

Lines changed: 117 additions & 121 deletions
Large diffs are not rendered by default.

src/flisp/cvalues.c

Lines changed: 395 additions & 418 deletions
Large diffs are not rendered by default.

src/flisp/equal.c

Lines changed: 83 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,40 @@
44
// comparable tag
55
#define cmptag(v) (isfixnum(v) ? TAG_NUM : tag(v))
66

7-
static value_t eq_class(htable_t *table, value_t key)
7+
static value_t eq_class(fl_context_t *fl_ctx, htable_t *table, value_t key)
88
{
99
value_t c = (value_t)ptrhash_get(table, (void*)key);
1010
if (c == (value_t)HT_NOTFOUND)
11-
return NIL;
11+
return fl_ctx->NIL;
1212
if (c == key)
1313
return c;
14-
return eq_class(table, c);
14+
return eq_class(fl_ctx, table, c);
1515
}
1616

17-
static void eq_union(htable_t *table, value_t a, value_t b,
18-
value_t c, value_t cb)
17+
static void eq_union(fl_context_t *fl_ctx, htable_t *table, value_t a,
18+
value_t b, value_t c, value_t cb)
1919
{
20-
value_t ca = (c==NIL ? a : c);
21-
if (cb != NIL)
20+
value_t ca = (c==fl_ctx->NIL ? a : c);
21+
if (cb != fl_ctx->NIL)
2222
ptrhash_put(table, (void*)cb, (void*)ca);
2323
ptrhash_put(table, (void*)a, (void*)ca);
2424
ptrhash_put(table, (void*)b, (void*)ca);
2525
}
2626

27-
static value_t bounded_compare(value_t a, value_t b, int bound, int eq);
28-
static value_t cyc_compare(value_t a, value_t b, htable_t *table, int eq);
27+
static value_t bounded_compare(fl_context_t *fl_ctx, value_t a, value_t b, int bound, int eq);
28+
static value_t cyc_compare(fl_context_t *fl_ctx, value_t a, value_t b, htable_t *table, int eq);
2929

30-
static value_t bounded_vector_compare(value_t a, value_t b, int bound, int eq)
30+
static value_t bounded_vector_compare(fl_context_t *fl_ctx, value_t a, value_t b, int bound, int eq)
3131
{
3232
size_t la = vector_size(a);
3333
size_t lb = vector_size(b);
3434
size_t m, i;
3535
if (eq && (la!=lb)) return fixnum(1);
3636
m = la < lb ? la : lb;
3737
for (i = 0; i < m; i++) {
38-
value_t d = bounded_compare(vector_elt(a,i), vector_elt(b,i),
38+
value_t d = bounded_compare(fl_ctx, vector_elt(a,i), vector_elt(b,i),
3939
bound-1, eq);
40-
if (d==NIL || numval(d)!=0) return d;
40+
if (d==fl_ctx->NIL || numval(d)!=0) return d;
4141
}
4242
if (la < lb) return fixnum(-1);
4343
if (la > lb) return fixnum(1);
@@ -46,14 +46,14 @@ static value_t bounded_vector_compare(value_t a, value_t b, int bound, int eq)
4646

4747
// strange comparisons are resolved arbitrarily but consistently.
4848
// ordering: number < cprim < function < vector < cvalue < symbol < cons
49-
static value_t bounded_compare(value_t a, value_t b, int bound, int eq)
49+
static value_t bounded_compare(fl_context_t *fl_ctx, value_t a, value_t b, int bound, int eq)
5050
{
5151
value_t d;
5252

5353
compare_top:
5454
if (a == b) return fixnum(0);
5555
if (bound <= 0)
56-
return NIL;
56+
return fl_ctx->NIL;
5757
int taga = tag(a);
5858
int tagb = cmptag(b);
5959
int c;
@@ -64,29 +64,29 @@ static value_t bounded_compare(value_t a, value_t b, int bound, int eq)
6464
return (numval(a) < numval(b)) ? fixnum(-1) : fixnum(1);
6565
}
6666
if (iscprim(b)) {
67-
if (cp_class((cprim_t*)ptr(b)) == wchartype)
67+
if (cp_class((cprim_t*)ptr(b)) == fl_ctx->wchartype)
6868
return fixnum(1);
69-
return fixnum(numeric_compare(a, b, eq, 1, NULL));
69+
return fixnum(numeric_compare(fl_ctx, a, b, eq, 1, NULL));
7070
}
7171
return fixnum(-1);
7272
case TAG_SYM:
7373
if (eq) return fixnum(1);
7474
if (tagb < TAG_SYM) return fixnum(1);
7575
if (tagb > TAG_SYM) return fixnum(-1);
76-
return fixnum(strcmp(symbol_name(a), symbol_name(b)));
76+
return fixnum(strcmp(symbol_name(fl_ctx, a), symbol_name(fl_ctx, b)));
7777
case TAG_VECTOR:
7878
if (isvector(b))
79-
return bounded_vector_compare(a, b, bound, eq);
79+
return bounded_vector_compare(fl_ctx, a, b, bound, eq);
8080
break;
8181
case TAG_CPRIM:
82-
if (cp_class((cprim_t*)ptr(a)) == wchartype) {
83-
if (!iscprim(b) || cp_class((cprim_t*)ptr(b)) != wchartype)
82+
if (cp_class((cprim_t*)ptr(a)) == fl_ctx->wchartype) {
83+
if (!iscprim(b) || cp_class((cprim_t*)ptr(b)) != fl_ctx->wchartype)
8484
return fixnum(-1);
8585
}
86-
else if (iscprim(b) && cp_class((cprim_t*)ptr(b)) == wchartype) {
86+
else if (iscprim(b) && cp_class((cprim_t*)ptr(b)) == fl_ctx->wchartype) {
8787
return fixnum(1);
8888
}
89-
c = numeric_compare(a, b, eq, 1, NULL);
89+
c = numeric_compare(fl_ctx, a, b, eq, 1, NULL);
9090
if (c != 2)
9191
return fixnum(c);
9292
break;
@@ -102,30 +102,30 @@ static value_t bounded_compare(value_t a, value_t b, int bound, int eq)
102102
if (uintval(a) > N_BUILTINS && uintval(b) > N_BUILTINS) {
103103
function_t *fa = (function_t*)ptr(a);
104104
function_t *fb = (function_t*)ptr(b);
105-
d = bounded_compare(fa->bcode, fb->bcode, bound-1, eq);
106-
if (d==NIL || numval(d) != 0) return d;
107-
d = bounded_compare(fa->vals, fb->vals, bound-1, eq);
108-
if (d==NIL || numval(d) != 0) return d;
109-
d = bounded_compare(fa->env, fb->env, bound-1, eq);
110-
if (d==NIL || numval(d) != 0) return d;
105+
d = bounded_compare(fl_ctx, fa->bcode, fb->bcode, bound-1, eq);
106+
if (d==fl_ctx->NIL || numval(d) != 0) return d;
107+
d = bounded_compare(fl_ctx, fa->vals, fb->vals, bound-1, eq);
108+
if (d==fl_ctx->NIL || numval(d) != 0) return d;
109+
d = bounded_compare(fl_ctx, fa->env, fb->env, bound-1, eq);
110+
if (d==fl_ctx->NIL || numval(d) != 0) return d;
111111
return fixnum(0);
112112
}
113113
return (uintval(a) < uintval(b)) ? fixnum(-1) : fixnum(1);
114114
}
115115
break;
116116
case TAG_CONS:
117117
if (tagb < TAG_CONS) return fixnum(1);
118-
d = bounded_compare(car_(a), car_(b), bound-1, eq);
119-
if (d==NIL || numval(d) != 0) return d;
118+
d = bounded_compare(fl_ctx, car_(a), car_(b), bound-1, eq);
119+
if (d==fl_ctx->NIL || numval(d) != 0) return d;
120120
a = cdr_(a); b = cdr_(b);
121121
bound--;
122122
goto compare_top;
123123
}
124124
return (taga < tagb) ? fixnum(-1) : fixnum(1);
125125
}
126126

127-
static value_t cyc_vector_compare(value_t a, value_t b, htable_t *table,
128-
int eq)
127+
static value_t cyc_vector_compare(fl_context_t *fl_ctx, value_t a,
128+
value_t b, htable_t *table, int eq)
129129
{
130130
size_t la = vector_size(a);
131131
size_t lb = vector_size(b);
@@ -139,8 +139,8 @@ static value_t cyc_vector_compare(value_t a, value_t b, htable_t *table,
139139
xa = vector_elt(a,i);
140140
xb = vector_elt(b,i);
141141
if (leafp(xa) || leafp(xb)) {
142-
d = bounded_compare(xa, xb, 1, eq);
143-
if (d!=NIL && numval(d)!=0) return d;
142+
d = bounded_compare(fl_ctx, xa, xb, 1, eq);
143+
if (d!=fl_ctx->NIL && numval(d)!=0) return d;
144144
}
145145
else if (tag(xa) < tag(xb)) {
146146
return fixnum(-1);
@@ -150,18 +150,18 @@ static value_t cyc_vector_compare(value_t a, value_t b, htable_t *table,
150150
}
151151
}
152152

153-
ca = eq_class(table, a);
154-
cb = eq_class(table, b);
155-
if (ca!=NIL && ca==cb)
153+
ca = eq_class(fl_ctx, table, a);
154+
cb = eq_class(fl_ctx, table, b);
155+
if (ca!=fl_ctx->NIL && ca==cb)
156156
return fixnum(0);
157157

158-
eq_union(table, a, b, ca, cb);
158+
eq_union(fl_ctx, table, a, b, ca, cb);
159159

160160
for (i = 0; i < m; i++) {
161161
xa = vector_elt(a,i);
162162
xb = vector_elt(b,i);
163163
if (!leafp(xa) || tag(xa)==TAG_FUNCTION) {
164-
d = cyc_compare(xa, xb, table, eq);
164+
d = cyc_compare(fl_ctx, xa, xb, table, eq);
165165
if (numval(d)!=0)
166166
return d;
167167
}
@@ -172,7 +172,7 @@ static value_t cyc_vector_compare(value_t a, value_t b, htable_t *table,
172172
return fixnum(0);
173173
}
174174

175-
static value_t cyc_compare(value_t a, value_t b, htable_t *table, int eq)
175+
static value_t cyc_compare(fl_context_t *fl_ctx, value_t a, value_t b, htable_t *table, int eq)
176176
{
177177
value_t d, ca, cb;
178178
cyc_compare_top:
@@ -185,29 +185,29 @@ static value_t cyc_compare(value_t a, value_t b, htable_t *table, int eq)
185185
int tagaa = tag(aa); int tagda = tag(da);
186186
int tagab = tag(ab); int tagdb = tag(db);
187187
if (leafp(aa) || leafp(ab)) {
188-
d = bounded_compare(aa, ab, 1, eq);
189-
if (d!=NIL && numval(d)!=0) return d;
188+
d = bounded_compare(fl_ctx, aa, ab, 1, eq);
189+
if (d!=fl_ctx->NIL && numval(d)!=0) return d;
190190
}
191191
else if (tagaa < tagab)
192192
return fixnum(-1);
193193
else if (tagaa > tagab)
194194
return fixnum(1);
195195
if (leafp(da) || leafp(db)) {
196-
d = bounded_compare(da, db, 1, eq);
197-
if (d!=NIL && numval(d)!=0) return d;
196+
d = bounded_compare(fl_ctx, da, db, 1, eq);
197+
if (d!=fl_ctx->NIL && numval(d)!=0) return d;
198198
}
199199
else if (tagda < tagdb)
200200
return fixnum(-1);
201201
else if (tagda > tagdb)
202202
return fixnum(1);
203203

204-
ca = eq_class(table, a);
205-
cb = eq_class(table, b);
206-
if (ca!=NIL && ca==cb)
204+
ca = eq_class(fl_ctx, table, a);
205+
cb = eq_class(fl_ctx, table, b);
206+
if (ca!=fl_ctx->NIL && ca==cb)
207207
return fixnum(0);
208208

209-
eq_union(table, a, b, ca, cb);
210-
d = cyc_compare(aa, ab, table, eq);
209+
eq_union(fl_ctx, table, a, b, ca, cb);
210+
d = cyc_compare(fl_ctx, aa, ab, table, eq);
211211
if (numval(d)!=0) return d;
212212
a = da;
213213
b = db;
@@ -218,56 +218,55 @@ static value_t cyc_compare(value_t a, value_t b, htable_t *table, int eq)
218218
}
219219
}
220220
else if (isvector(a) && isvector(b)) {
221-
return cyc_vector_compare(a, b, table, eq);
221+
return cyc_vector_compare(fl_ctx, a, b, table, eq);
222222
}
223223
else if (isclosure(a) && isclosure(b)) {
224224
function_t *fa = (function_t*)ptr(a);
225225
function_t *fb = (function_t*)ptr(b);
226-
d = bounded_compare(fa->bcode, fb->bcode, 1, eq);
226+
d = bounded_compare(fl_ctx, fa->bcode, fb->bcode, 1, eq);
227227
if (numval(d) != 0) return d;
228228

229-
ca = eq_class(table, a);
230-
cb = eq_class(table, b);
231-
if (ca!=NIL && ca==cb)
229+
ca = eq_class(fl_ctx, table, a);
230+
cb = eq_class(fl_ctx, table, b);
231+
if (ca!=fl_ctx->NIL && ca==cb)
232232
return fixnum(0);
233233

234-
eq_union(table, a, b, ca, cb);
235-
d = cyc_compare(fa->vals, fb->vals, table, eq);
234+
eq_union(fl_ctx, table, a, b, ca, cb);
235+
d = cyc_compare(fl_ctx, fa->vals, fb->vals, table, eq);
236236
if (numval(d) != 0) return d;
237237
a = fa->env;
238238
b = fb->env;
239239
goto cyc_compare_top;
240240
}
241-
return bounded_compare(a, b, 1, eq);
241+
return bounded_compare(fl_ctx, a, b, 1, eq);
242242
}
243243

244-
static htable_t equal_eq_hashtable;
245-
void comparehash_init(void)
244+
void comparehash_init(fl_context_t *fl_ctx)
246245
{
247-
htable_new(&equal_eq_hashtable, 512);
246+
htable_new(&fl_ctx->equal_eq_hashtable, 512);
248247
}
249248

250249
// 'eq' means unordered comparison is sufficient
251-
static value_t compare_(value_t a, value_t b, int eq)
250+
static value_t compare_(fl_context_t *fl_ctx, value_t a, value_t b, int eq)
252251
{
253-
value_t guess = bounded_compare(a, b, BOUNDED_COMPARE_BOUND, eq);
254-
if (guess == NIL) {
255-
guess = cyc_compare(a, b, &equal_eq_hashtable, eq);
256-
htable_reset(&equal_eq_hashtable, 512);
252+
value_t guess = bounded_compare(fl_ctx, a, b, BOUNDED_COMPARE_BOUND, eq);
253+
if (guess == fl_ctx->NIL) {
254+
guess = cyc_compare(fl_ctx, a, b, &fl_ctx->equal_eq_hashtable, eq);
255+
htable_reset(&fl_ctx->equal_eq_hashtable, 512);
257256
}
258257
return guess;
259258
}
260259

261-
value_t fl_compare(value_t a, value_t b)
260+
value_t fl_compare(fl_context_t *fl_ctx, value_t a, value_t b)
262261
{
263-
return compare_(a, b, 0);
262+
return compare_(fl_ctx, a, b, 0);
264263
}
265264

266-
value_t fl_equal(value_t a, value_t b)
265+
value_t fl_equal(fl_context_t *fl_ctx, value_t a, value_t b)
267266
{
268267
if (eq_comparable(a, b))
269-
return (a == b) ? FL_T : FL_F;
270-
return (numval(compare_(a,b,1))==0 ? FL_T : FL_F);
268+
return (a == b) ? fl_ctx->T : fl_ctx->F;
269+
return (numval(compare_(fl_ctx, a,b,1))==0 ? fl_ctx->T : fl_ctx->F);
271270
}
272271

273272
/*
@@ -287,7 +286,7 @@ value_t fl_equal(value_t a, value_t b)
287286
#endif
288287

289288
// *oob: output argument, means we hit the limit specified by 'bound'
290-
static uptrint_t bounded_hash(value_t a, int bound, int *oob)
289+
static uptrint_t bounded_hash(fl_context_t *fl_ctx, value_t a, int bound, int *oob)
291290
{
292291
*oob = 0;
293292
union {
@@ -308,14 +307,14 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
308307
return doublehash(u.i64);
309308
case TAG_FUNCTION:
310309
if (uintval(a) > N_BUILTINS)
311-
return bounded_hash(((function_t*)ptr(a))->bcode, bound, oob);
310+
return bounded_hash(fl_ctx, ((function_t*)ptr(a))->bcode, bound, oob);
312311
return inthash(a);
313312
case TAG_SYM:
314313
return ((symbol_t*)ptr(a))->hash;
315314
case TAG_CPRIM:
316315
cp = (cprim_t*)ptr(a);
317316
data = cp_data(cp);
318-
if (cp_class(cp) == wchartype)
317+
if (cp_class(cp) == fl_ctx->wchartype)
319318
return inthash(*(int32_t*)data);
320319
nt = cp_numtype(cp);
321320
u.d = conv_to_double(data, nt);
@@ -332,7 +331,7 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
332331
}
333332
len = vector_size(a);
334333
for(i=0; i < len; i++) {
335-
h = MIX(h, bounded_hash(vector_elt(a,i), bound/2, &oob2)^1);
334+
h = MIX(h, bounded_hash(fl_ctx, vector_elt(a,i), bound/2, &oob2)^1);
336335
if (oob2)
337336
bound/=2;
338337
*oob = *oob || oob2;
@@ -345,7 +344,7 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
345344
*oob = 1;
346345
return h;
347346
}
348-
h = MIX(h, bounded_hash(car_(a), bound/2, &oob2));
347+
h = MIX(h, bounded_hash(fl_ctx, car_(a), bound/2, &oob2));
349348
// bounds balancing: try to share the bounds efficiently
350349
// so we can hash better when a list is cdr-deep (a common case)
351350
if (oob2)
@@ -357,29 +356,29 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
357356
*oob = *oob || oob2;
358357
a = cdr_(a);
359358
} while (iscons(a));
360-
h = MIX(h, bounded_hash(a, bound-1, &oob2)^2);
359+
h = MIX(h, bounded_hash(fl_ctx, a, bound-1, &oob2)^2);
361360
*oob = *oob || oob2;
362361
return h;
363362
}
364363
return 0;
365364
}
366365

367-
int equal_lispvalue(value_t a, value_t b)
366+
int equal_lispvalue(fl_context_t *fl_ctx, value_t a, value_t b)
368367
{
369368
if (eq_comparable(a, b))
370369
return (a==b);
371-
return (numval(compare_(a,b,1))==0);
370+
return (numval(compare_(fl_ctx, a, b, 1))==0);
372371
}
373372

374-
uptrint_t hash_lispvalue(value_t a)
373+
uptrint_t hash_lispvalue(fl_context_t *fl_ctx, value_t a)
375374
{
376-
int oob=0;
377-
uptrint_t n = bounded_hash(a, BOUNDED_HASH_BOUND, &oob);
375+
int oob = 0;
376+
uptrint_t n = bounded_hash(fl_ctx, a, BOUNDED_HASH_BOUND, &oob);
378377
return n;
379378
}
380379

381-
value_t fl_hash(value_t *args, u_int32_t nargs)
380+
value_t fl_hash(fl_context_t *fl_ctx, value_t *args, u_int32_t nargs)
382381
{
383-
argcount("hash", nargs, 1);
384-
return fixnum(hash_lispvalue(args[0]));
382+
argcount(fl_ctx, "hash", nargs, 1);
383+
return fixnum(hash_lispvalue(fl_ctx, args[0]));
385384
}

0 commit comments

Comments
 (0)