File: dict.c

package info (click to toggle)
genius 1.0.24-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 27,952 kB
  • sloc: ansic: 105,597; xml: 67,672; sh: 4,537; makefile: 2,089; lex: 499; yacc: 298; perl: 54; python: 22
file content (1111 lines) | stat: -rw-r--r-- 23,507 bytes parent folder | download | duplicates (4)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
/* GENIUS Calculator
 * Copyright (C) 1997-2011 Jiri (George) Lebl
 *
 * Author: Jiri (George) Lebl
 *
 * This file is part of Genius.
 *
 * Genius is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "eval.h"
#include "dict.h"
#include "util.h"
#include "funclib.h"
#include "compil.h"

/* Note: this won't be completely mem-debug friendly, but only mostly */
/* #define MEM_DEBUG_FRIENDLY 1 */

/*the context stack structure*/
typedef struct _GelDictContext {
	GelContextFrame *stack;
	GelContextFrame *global_frame;
	int top;
} GelDictContext;

static GelDictContext context = {NULL, NULL, -1};

static GHashTable *dictionary;

extern const char *genius_toplevels[];
extern const char *genius_operators[];

/*return current context number (0 is global, -1 is uninitialized)*/
int
d_curcontext (void) /* PURE! no side effects*/
{
	return context.top;
}

/*make builtin function and return it*/
GelEFunc *
d_makebifunc (GelToken *id, GelBIFunction f, int nargs)
{
	GelEFunc *n;

	n = g_slice_new0 (GelEFunc);
	n->id = id;
	n->data.func = f;
	n->nargs = nargs;
	n->context = context.top;
	n->type = GEL_BUILTIN_FUNC;
	/*
	n->vararg = 0;
	n->on_subst_list = 0;
	n->named_args = NULL;
	n->local_idents = NULL;
	n->extra_dict = NULL;
	n->no_mod_all_args = FALSE;
	n->propagate_mod = FALSE;
	*/

	return n;
}

/*make a user function and return it*/
GelEFunc *
d_makeufunc (GelToken *id, GelETree *value, GSList *argnames, int nargs,
	     const GSList *extra_dict)
{
	GelEFunc *n;

	n = g_slice_new0 (GelEFunc);

	n->id = id;
	n->data.user = value;
	n->nargs = nargs;
	n->named_args = argnames;
	n->context = context.top;
	n->type = GEL_USER_FUNC;

	/* look at the new0
	n->vararg = 0;
	n->on_subst_list = 0;
	n->no_mod_all_args = FALSE;
	n->propagate_mod = FALSE;
	n->extra_dict = NULL;
	n->local_all = 0;
	n->local_idents = NULL;
	*/

	if (extra_dict != NULL) {
		GSList *li;
		n->extra_dict = g_slist_copy ((GSList *)extra_dict);
		for (li = n->extra_dict; li != NULL; li = li->next)
			li->data = d_copyfunc (li->data);
	}

	return n;
}

/*make a variable function and return it*/
GelEFunc *
d_makevfunc(GelToken *id, GelETree *value)
{
	GelEFunc *n;

	n = g_slice_new0 (GelEFunc);

	n->id = id;
	n->data.user = value;
	n->context = context.top;
	n->type = GEL_VARIABLE_FUNC;

	/* look at the new0
	n->nargs = 0;
	n->vararg = 0;
	n->on_subst_list = 0;
	n->named_args = NULL;
	n->extra_dict = NULL;
	n->no_mod_all_args = FALSE;
	n->propagate_mod = FALSE;
	n->local_all = 0;
	n->local_idents = NULL;
	*/

	return n;
}

/*make a user function and return it*/
GelEFunc *
d_makereffunc(GelToken *id, GelEFunc *ref)
{
	GelEFunc *n;

	n = g_slice_new0 (GelEFunc);

	n->id = id;
	n->data.ref = ref;
	n->context = context.top;
	n->type = GEL_REFERENCE_FUNC;

	/* look at the new0
	n->nargs = 0;
	n->vararg = 0;
	n->on_subst_list = 0;
	n->named_args = NULL;
	n->local_idents = NULL;
	n->extra_dict = NULL;
	n->no_mod_all_args = FALSE;
	n->propagate_mod = FALSE;
	*/

	return n;
}

/*copy a function*/
GelEFunc *
d_copyfunc(GelEFunc *o)
{
	GelEFunc *n;
	GSList *li;

	n = g_slice_dup (GelEFunc, o);

	if(n->type == GEL_USER_FUNC ||
	   n->type == GEL_VARIABLE_FUNC) {
		D_ENSURE_USER_BODY (o);
		n->data.user = gel_copynode(o->data.user);
	}
	n->named_args = g_slist_copy (o->named_args);
	n->local_idents = g_slist_copy (o->local_idents);
	n->subst_dict = g_slist_copy (o->subst_dict);

	n->extra_dict = g_slist_copy (o->extra_dict);
	for (li = n->extra_dict; li != NULL; li = li->next)
		li->data = d_copyfunc (li->data);

	if (n->on_subst_list) {
		n->on_subst_list = 0;
		d_put_on_subst_list (n);
	}
	
	return n;
}

/*make a real function from a fake*/
GelEFunc *
d_makerealfunc(GelEFunc *o,GelToken *id, gboolean use)
{
	GelEFunc *n;

	n = g_slice_dup (GelEFunc, o);

	/* never copy is_local! */
	n->is_local = 0;

	n->id = id;
	if (o->symbolic_id == NULL)
		n->symbolic_id = o->id;
	else
		n->symbolic_id = o->symbolic_id;
	n->context = context.top;

	if(n->type == GEL_USER_FUNC ||
	   n->type == GEL_VARIABLE_FUNC) {
		D_ENSURE_USER_BODY (o);
		if(use) {
			n->data.user = o->data.user;
			o->data.user = NULL;
		} else
			n->data.user = gel_copynode(o->data.user);
	}
	if(use) {
		o->extra_dict = NULL;
		o->named_args = NULL;
		o->local_idents = NULL;
		o->subst_dict = NULL;
		o->nargs = 0;
	} else {
		GSList *li;
		n->extra_dict = g_slist_copy (o->extra_dict);
		for (li = n->extra_dict; li != NULL; li = li->next)
			li->data = d_copyfunc (li->data);
		n->named_args = g_slist_copy (o->named_args);
		n->local_idents = g_slist_copy (o->local_idents);
		n->subst_dict = g_slist_copy (o->subst_dict);
	}

	if (n->on_subst_list) {
		n->on_subst_list = 0;
		d_put_on_subst_list (n);
	}
	
	return n;
}

/*make real func and replace n with it, without changing n's context or id*/
/*if use is set, we USE the original function, NULLing approriately*/
void
d_setrealfunc(GelEFunc *n,GelEFunc *fake, gboolean use)
{
	if(n->type == GEL_USER_FUNC ||
	   n->type == GEL_VARIABLE_FUNC)
		gel_freetree(n->data.user);
	
	n->type = fake->type;
	n->data = fake->data;
	n->symbolic_id = fake->symbolic_id;
	if (fake->symbolic_id == NULL)
		n->symbolic_id = fake->id;
	if(n->type == GEL_USER_FUNC ||
	   n->type == GEL_VARIABLE_FUNC) {
		D_ENSURE_USER_BODY (fake);
		if(use) {
			n->data.user = fake->data.user;
			fake->data.user = NULL;
		} else
			n->data.user = gel_copynode(fake->data.user);
	}

	n->never_on_subst_list = fake->never_on_subst_list;
	n->vararg = fake->vararg;
	n->propagate_mod = fake->propagate_mod;
	n->no_mod_all_args = fake->no_mod_all_args;
	n->local_all = fake->local_all;
	/* Never copy is_local */
	n->built_subst_dict = fake->built_subst_dict;

	if(use) {
		n->named_args = fake->named_args;
		n->local_idents = fake->local_idents;
		n->subst_dict = fake->subst_dict;
		n->nargs = fake->nargs;
		fake->named_args = NULL;
		fake->local_idents = NULL;
		fake->nargs = 0;
		fake->subst_dict = NULL;

		n->extra_dict = fake->extra_dict;
		fake->extra_dict = NULL;
	} else {
		GSList *li;

		n->named_args = g_slist_copy (fake->named_args);
		n->local_idents = g_slist_copy (fake->local_idents);
		n->subst_dict = g_slist_copy (fake->subst_dict);
		n->nargs = fake->nargs;

		n->extra_dict = g_slist_copy (fake->extra_dict);
		for (li = n->extra_dict; li != NULL; li = li->next)
			li->data = d_copyfunc (li->data);
	}

	if (fake->on_subst_list) {
		d_put_on_subst_list (n);
	}
}

void
d_initcontext(void)
{
	GelToken *tok;

	context.top = 0; /*0 means that element 0 exists!*/
	/*add an empty dictionary*/
	context.stack = g_slice_new0 (GelContextFrame);
	context.global_frame = context.stack;

	dictionary = g_hash_table_new (g_str_hash, g_str_equal);

	/*add Ans and ans as the same token*/
	tok = g_new0 (GelToken, 1);
	tok->token = g_strdup ("Ans");
	g_hash_table_insert (dictionary, tok->token, tok);
	g_hash_table_insert (dictionary, g_strdup("ans"), tok);

	/*this is where the built in functions register into the global
	  dictionary*/
	gel_funclib_addall ();
}

/*compare two GelEFunc's by their context numbers*/
/*static int
compare_func_bycontext(gconstpointer p1, gconstpointer p2)
{
	GelEFunc *func1 = (GelEFunc *)p1;
	GelEFunc *func2 = (GelEFunc *)p2;
	
	return func1->context < func2->context;
}*/

/*add a function struct to the dict (in current context)*/
GelEFunc *
d_addfunc (GelEFunc *func)
{
	GelEFunc *n;
	
	g_return_val_if_fail (func->context == context.top, func);

	if (context.stack->local_all)
		func->is_local = 1;
	
	/*we already found it (in current context)*/
	n = d_lookup_local(func->id);
	if(n) {
		int is_local = n->is_local;
		d_replacefunc (n, func);
		if (is_local)
			n->is_local = 1;
		return n;
	}

	context.stack->functions =
		g_slist_prepend (context.stack->functions, func);
	
	func->id->refs = g_slist_prepend(func->id->refs,func);

	return func;
}

/*add a function struct to the dict (in global context)*/
GelEFunc *
d_addfunc_global (GelEFunc *func)
{
	GelEFunc *n;
	GSList *last;
	
	g_return_val_if_fail (func->context == 0, func);

	/* get the function in the lowest context */
	last = g_slist_last (func->id->refs);
	n = last != NULL ? last->data : NULL;
	/* if this function is global */
	if (n != NULL && n->context == 0) {
		d_replacefunc (n, func);
		return n;
	}

	context.global_frame->functions =
		g_slist_prepend (context.global_frame->functions, func);
	
	func->id->refs = g_slist_append (func->id->refs, func);

	return func;
}

/*set value of an existing function (in local context), used for arguments
  WARNING, does not free the memory allocated by previous value!*/
gboolean
d_setvalue (GelToken *id, GelETree *value)
{
	GelEFunc *f;
	f=d_lookup_local(id);
	if(!f || (f->type!=GEL_USER_FUNC &&
		  f->type!=GEL_VARIABLE_FUNC))
		return FALSE;
	f->data.user=value;
	return TRUE;
}

/*dictionary functions*/

/*lookup a function in the dictionary in the current context*/
GelEFunc *
d_lookup_local(GelToken *id) /* PURE! no side effects*/
{
	GelEFunc *func;

	if G_UNLIKELY (id == NULL || id->refs == NULL)
		return NULL;
	
	/*the first one must be the lowest context*/
	func = id->refs->data;
	
	/*not in currect context and we only want the currect context ones*/
	if (func->context < context.top)
		return NULL;

	return func;
}

/*lookup a function in the dictionary NOT in the current context*/
GelEFunc *
d_lookup_global_up1(GelToken *id)/* PURE! no side effects*/
{
	GSList *li;

	if G_UNLIKELY (id == NULL || id->refs == NULL)
		return NULL;

	/*the first one must be the lowest context*/
	li = id->refs;
	do {
		GelEFunc *f = li->data;
		if ( ! f->is_local && f->context < context.top) {
			return f;
		}
		li = li->next;
	} while (li != NULL);

	return NULL;
}

/*lookup a function in the dictionary but only in the toplevel context */
GelEFunc *
d_lookup_only_global (GelToken *id)/* PURE! no side effects*/
{
	GSList *li;
	GelEFunc *func;

	if G_UNLIKELY (id == NULL || id->refs == NULL)
		return NULL;

	li = id->refs;
	while (li->next != NULL)
		li = li->next;

	/* this must be our function */
	func = li->data;
	
	if(func->context == 0)
		return func;
	else
		return NULL;
}

/*lookup a function in the dictionary*/
GelEFunc *
d_lookup_global (GelToken *id)/* PURE! no side effects*/
{
	GSList *li;
	
	if G_UNLIKELY (id == NULL || id->refs == NULL)
		return NULL;

	/*the first one must be the lowest context*/
	li = id->refs;
	do {
		GelEFunc *f = li->data;
		if ( ! f->is_local || f->context == context.top) {
			return f;
		}
		li = li->next;
	} while (li != NULL);

	return NULL;
}

GelToken *
d_intern (const char *id)
{
	GelToken * tok;

	if G_UNLIKELY (id == NULL)
		return NULL;

        tok = g_hash_table_lookup (dictionary, id);
	if G_UNLIKELY (tok == NULL) {
		tok = g_new0 (GelToken, 1);
		tok->token = g_strdup (id);
		g_hash_table_insert (dictionary, tok->token, tok);
	}

	return tok;
}

/* this may be inefficient as it also goes through global,
 * but we don't assume we do this kind of thing often.  Only 
 * done in d_delete and d_delete_global which are only done
 * on Undefine and UndefineAll. */
static void
whack_from_all_contexts (GelEFunc *func)
{
	GelContextFrame *li;
	for (li = context.stack; li != NULL; li = li->next) {
		li->functions = g_slist_remove (li->functions, func);
	}
}

gboolean
d_delete(GelToken *id)
{
	GSList *li, *list;

	g_return_val_if_fail (id != NULL && id->token != NULL, FALSE);

	id->protected_ = 0;
	id->parameter = 0;
	id->built_in_parameter = 0;

	list = id->refs;
	id->refs = NULL;
	for (li = list; li != NULL; li = li->next) {
		GelEFunc *f = li->data;
		f->id = NULL;
		whack_from_all_contexts (f);
		d_freefunc (f);
	}
	g_slist_free (list);

	return TRUE;
}

gboolean
d_delete_global(GelToken *id)
{
	GSList *list;
	GelEFunc *f;

	g_return_val_if_fail (id != NULL && id->token != NULL, FALSE);

	id->protected_ = 0;
	id->parameter = 0;
	id->built_in_parameter = 0;

	list = g_slist_last (id->refs);
	f = list->data;

	if (f->context == 0) {
		/* inefficient, but it need not be */
		id->refs = g_slist_remove_link (id->refs, list);

		f->id = NULL;
		whack_from_all_contexts (f);
		d_freefunc (f);
	}

	return TRUE;
}

/*clear all context dictionaries and pop out all the contexts except
  the global one
  also init the context stack if it hasn't been done*/
void
d_singlecontext(void)
{
	if (context.stack == NULL)
		d_initcontext();
	else
		while (context.top > 0)
			d_popcontext ();
}

/*free all memory allocated by a dictionary*/
void
d_freedict (GSList *n)
{
	GSList *li;
	for (li = n; li != NULL; li = li->next) {
		d_freefunc (li->data);
		li->data = NULL;
	}
	g_slist_free (n);
}

static void
whack_from_subst_lists (GelEFunc *func)
{
	GelContextFrame *li;
	for (li = context.stack; li != NULL; li = li->next) {
		GSList *fl = g_slist_find (li->substlist, func);
		if (fl != NULL) {
			li->substlist = g_slist_delete_link (li->substlist, fl);
			return;
		}
	}
}

/* Put on subst local var list for this current stack frame*/
void
d_put_on_subst_list (GelEFunc *func)
{
	if (func->on_subst_list) {
		/* On a lower stackframe? */
		/* weird but true.  So whack it and put it here,
		 * it will get to the lower one eventually */
		whack_from_subst_lists (func);
	}

	if (func->context == -1)
		func->context = context.top;

	context.stack->substlist = 
		g_slist_prepend (context.stack->substlist, func);
	func->on_subst_list = 1;
}

void
d_freefunc (GelEFunc *n)
{
	GSList *li;
	if(!n)
		return;
	/*
	g_assert(!n->id || g_slist_find(n->id->refs,n)==NULL);
	*/

	if (n->on_subst_list) {
		whack_from_subst_lists (n);
		n->on_subst_list = 0;
	}

	/*if(n->id) {
		n->id->refs = g_slist_remove(n->id->refs,n);
	} */
	if((n->type == GEL_USER_FUNC ||
	    n->type == GEL_VARIABLE_FUNC) &&
	   n->data.user)
		gel_freetree(n->data.user);

	for (li = n->extra_dict; li != NULL; li = li->next) {
		d_freefunc (li->data);
		li->data = NULL;
	}

	g_slist_free (n->extra_dict);

	g_slist_free (n->named_args);
	g_slist_free (n->local_idents);
	g_slist_free (n->subst_dict);

	g_slice_free (GelEFunc, n);
}

/*replace old with stuff from new and free new,
  new has to have the same id, also new->id should
  not hold a reference to new*/
void
d_replacefunc(GelEFunc *old,GelEFunc *_new)
{
	GSList *li;
	gboolean put_on_subst = FALSE;

	g_return_if_fail(old && _new);
	g_return_if_fail(old->id == _new->id);

	if (old->on_subst_list) {
		whack_from_subst_lists (old);
		old->on_subst_list = 0;
	}

	if (_new->on_subst_list) {
		whack_from_subst_lists (_new);
		_new->on_subst_list = 0;
		put_on_subst = TRUE;
	}

	if(old->type == GEL_USER_FUNC ||
	   old->type == GEL_VARIABLE_FUNC)
		gel_freetree(old->data.user);

	g_slist_free (old->named_args);
	g_slist_free (old->local_idents);
	g_slist_free (old->subst_dict);

	for (li = old->extra_dict; li != NULL; li = li->next) {
		d_freefunc (li->data);
		li->data = NULL;
	}
	g_slist_free (old->extra_dict);

	memcpy(old,_new,sizeof(GelEFunc));

	/* FIXME: this is inefficient */
	if (put_on_subst) {
		d_put_on_subst_list (old);
	}

	g_slice_free (GelEFunc, _new);
}

/*set_ref*/
void
d_set_ref(GelEFunc *n,GelEFunc *ref)
{
	if(!n || !ref)
		return;
	if(n->type == GEL_USER_FUNC ||
	   n->type == GEL_VARIABLE_FUNC)
		gel_freetree(n->data.user);
	n->type = GEL_REFERENCE_FUNC;
	g_slist_free (n->named_args);
	g_slist_free (n->local_idents);
	g_slist_free (n->subst_dict);
	n->nargs = 0;
	n->named_args = NULL;
	n->subst_dict = NULL;

	n->never_on_subst_list = 0;
	n->vararg = 0;
	n->propagate_mod = 0;
	n->no_mod_all_args = 0;
	n->local_all = 0;
	/* don't touch is_local */
	n->built_subst_dict = 0;
	
	n->data.ref = ref;
}

/*set_value*/
void
d_set_value(GelEFunc *n,GelETree *value)
{
	if(!n || !value)
		return;
	if(n->type == GEL_USER_FUNC ||
	   n->type == GEL_VARIABLE_FUNC)
		gel_freetree(n->data.user);
	n->type = GEL_VARIABLE_FUNC;
	g_slist_free (n->named_args);
	g_slist_free (n->local_idents);
	g_slist_free (n->subst_dict);
	n->nargs = 0;
	n->named_args = NULL;
	n->local_idents = NULL;
	n->subst_dict = NULL;

	n->never_on_subst_list = 0;
	n->vararg = 0;
	n->propagate_mod = 0;
	n->no_mod_all_args = 0;
	n->local_all = 0;
	/* don't touch is_local */
	n->built_subst_dict = 0;

	n->data.user = value;
}

gboolean
d_addcontext (GelEFunc *func)
{
	GelContextFrame *old = context.stack;
	context.stack = g_slice_new0 (GelContextFrame);
	context.stack->next = old;

	if (func != NULL) {
		context.stack->name = func->id;
		context.stack->local_all = func->local_all;
	}

	context.top++;

	return TRUE;
}

/*pop the context stack*/
void
d_popcontext (void)
{
	if (context.top != -1) {
		GSList *li;
		GSList *dict = context.stack->functions;
		GSList *subst = context.stack->substlist;
		GSList *substlast = NULL;
		GelContextFrame *of;

		if (context.top != 0) {
			li = subst;
			while (li != NULL) {
				GelEFunc *func = li->data;
				if (context.top == 1)
					func->on_subst_list = 0;

				if ((func->type == GEL_USER_FUNC ||
				     func->type == GEL_VARIABLE_FUNC) &&
				    func->context >= context.top) {
					D_ENSURE_SUBST_DICT (func);
					func->extra_dict =
						gel_subst_local_vars (func->extra_dict, &(func->subst_dict));
					/* With substitution, context of the function is
					   lowered */
					if (func->context >= context.top)
						func->context = context.top - 1;

					/* If subst_dict goes to NULL, we no
					 * longer need to keep this around on
					 * the subst_lists */
					if (func->subst_dict == NULL) {
						if (substlast != NULL) {
							substlast = g_slist_remove_link (substlast, li);
							li = substlast->next;
						} else {
							subst = g_slist_remove_link (subst, subst);
							li = subst;
						}
						continue;
					}

				}

				substlast = li;
				li = li->next;
			}
		}

		for (li = dict; li != NULL; li = li->next) {
			GelEFunc *func = li->data;
			func->id->refs = g_slist_remove(func->id->refs,func);
		}
		context.top--;

		of = context.stack;
		context.stack = of->next;

		g_slice_free (GelContextFrame, of);

		/* substitute lower variables unless we are on the toplevel */
		if (substlast != NULL && context.top > 0) {
			substlast->next = context.stack->substlist;
			context.stack->substlist = subst;
			subst = NULL;
		}

		d_freedict (dict);
		g_slist_free (subst);
	}
}

/*gimme the current dictinary*/
GSList *
d_getcontext (void)/* PURE! no side effects*/
{
	if (context.top == -1)
		return NULL;
	else {
		g_assert (context.stack != NULL);
		return context.stack->functions;
	}
}

GelContextFrame *
d_get_all_contexts (void)/* PURE! no side effects*/
{
	if (context.top == -1)
		return NULL;
	else
		return context.stack;
}

/*gimme the current global dictinary*/
GSList *
d_getcontext_global (void)/* PURE! no side effects*/
{
	if (context.top == -1) {
		return NULL;
	} else {
		g_assert (context.stack != NULL);
		return context.global_frame->functions;
	}
}

static int lowercase_ascii_sum_square (const char *id) G_GNUC_PURE;
static int
lowercase_ascii_sum_square (const char *id) 
{
	int sum = 0;
	int i;
	for (i = 0; id[i] != '\0'; i++) {
		int n = g_ascii_tolower (id[i]) - 'a';
		sum += n*n;
	}
	return sum;
}

static int lowercase_ascii_sum (const char *id) G_GNUC_PURE;
static int
lowercase_ascii_sum (const char *id) 
{
	int sum = 0;
	int i;
	for (i = 0; id[i] != '\0'; i++) {
		sum += g_ascii_tolower (id[i]) - 'a';
	}
	return sum;
}

static int lowercase_kronecker_difference (const char *id1, const char *id2) G_GNUC_PURE;
static int
lowercase_kronecker_difference (const char *id1, const char *id2) 
{
	int sum = 0;
	int i;
	for (i = 0; id1[i] != '\0' && id2[i] != '\0'; i++) {
		if (g_ascii_tolower (id1[i]) != g_ascii_tolower (id2[i]))
			sum += 1;
	}
	/* plus the ends */
	sum += strlen (&id1[i]);
	sum += strlen (&id2[i]);
	return sum;
}

static char *
construct_deleted (const char *id, int d, int len)
{
	int i, y;
	char *out = g_new0 (char, len);

	y = 0;
	for (i = 0; i < len; i++) {
		if (i != d)
			out[y++] = id[i];
	}

	return out;
}

static gboolean
try_deletions (const char *id1, int len1, const char *id2)
{
	int i;
	for (i = 0; i < len1; i++) {
		char *d = construct_deleted (id1, i, len1);
		int allowed = len1 * 0.2;
		if (lowercase_kronecker_difference (id2, d) <= allowed) {
			g_free (d);
			return TRUE;
		}
		g_free (d);
	}

	return FALSE;
}

static gboolean
are_ids_similar (const char *id1, const char *id2)
{
	int len1 = strlen (id1), len2 = strlen (id2);
	int dif1, dif2, dif3;
	int allowed;

	if (ABS (len1 - len2) > 1)
		return FALSE;

	if (g_ascii_strcasecmp (id1, id2) == 0)
		return TRUE;

	if (len1 > 6 && len1 == len2) {
		int sum1, sum2;
		int sum1s, sum2s;

		sum1 = lowercase_ascii_sum (id1);
		sum2 = lowercase_ascii_sum (id2);

		sum1s = lowercase_ascii_sum_square (id1);
		sum2s = lowercase_ascii_sum_square (id2);

		/* just a reordering (possibly) */
		if (sum1 == sum2 && sum1s == sum2s) {
			return TRUE;
		}
	}

	/* simple cases for length 2 and 3 */
	if (len1 == 2 &&
	    len2 == len1 &&
	    id1[0] == id2[1] &&
	    id1[1] == id2[0]) {
		return TRUE;
	}

	if (len1 == 3 &&
	    len2 == len1 &&
	    ( (id1[0] == id2[1] && id1[1] == id2[0] && id1[2] == id2[2]) ||
	      (id1[0] == id2[0] && id1[1] == id2[2] && id1[2] == id2[1]) )) {
		return TRUE;
	}

	if (len1 <= 2 && len2 <= 2) {
		return FALSE;
	}

	if (try_deletions (id1, len1, id2))
		return TRUE;
	if (try_deletions (id2, len2, id1))
		return TRUE;

	dif1 = lowercase_kronecker_difference (id1, id2);
	dif2 = lowercase_kronecker_difference (id1, &id2[1]);
	dif3 = lowercase_kronecker_difference (&id1[1], id2);

	allowed = 0.2 * len1;

	if (dif1 > allowed+1 &&
	    dif2 > allowed &&
	    dif3 > allowed)
		return FALSE;

	return TRUE;
}

GSList *
d_find_similar_globals (const char *id)
{
	GSList *ret = NULL;
	GSList *li;
	int i;

	for (li = d_getcontext_global (); li != NULL; li = li->next) {
		GelEFunc *n = li->data;
		if (n->id != NULL &&
		    n->id->token != NULL &&
		    are_ids_similar (n->id->token, id)) {
			ret = g_slist_prepend (ret, g_strdup (n->id->token));
		}
	}

	for (i = 0; genius_toplevels[i] != NULL; i++) {
		if (are_ids_similar (genius_toplevels[i], id)) {
			ret = g_slist_prepend (ret,
					       g_strdup (genius_toplevels[i]));
		}
	}

	for (i = 0; genius_operators[i] != NULL; i++) {
		if (are_ids_similar (genius_operators[i], id)) {
			ret = g_slist_prepend (ret,
					       g_strdup (genius_operators[i]));
		}
	}

	return ret;
}

void
d_protect_all(void)
{
	GSList *funcs;
	GSList *li;
	funcs = d_getcontext();
	if(!funcs) return;
	for(li=funcs;li;li=g_slist_next(li)) {
		GelEFunc *func = li->data;
		if(!func->id || strcmp(func->id->token,"Ans")==0)
			continue;
		func->id->protected_ = 1;
	}
}

void
d_add_named_args (GelEFunc *f, const char *args)
{
	int i;
	char **s;

	if (args == NULL || args[0] == '\0')
		return;
       
	s = g_strsplit (args, ",", -1);
	for (i = 0; s[i] != NULL; i++) {
		f->named_args = g_slist_append (f->named_args, d_intern (s[i]));
	}
	g_strfreev (s);
}