summaryrefslogtreecommitdiff
path: root/bc/storage.c
blob: c79db8263c6bb44124366c4bf902deb90889eaab (plain)
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
/*  This file is part of GNU bc.

    Copyright (C) 1991-1994, 1997, 2006, 2008, 2012-2017 Free Software Foundation, Inc.

    This program 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; see the file COPYING.  If not, see
    <http://www.gnu.org/licenses>.

    You may contact the author by:
       e-mail:  philnelson@acm.org
      us-mail:  Philip A. Nelson
                Computer Science Department, 9062
                Western Washington University
                Bellingham, WA 98226-9062
       
*************************************************************************/

/* storage.c:  Code and data storage manipulations.  This includes labels. */

#include "bcdefs.h"
#include "proto.h"

/* Local prototypes */
static bc_array_node *copy_tree (bc_array_node *ary_node, int depth);
static bc_array *copy_array (bc_array *ary);


/* Initialize the storage at the beginning of the run. */

void
init_storage (void)
{

  /* Functions: we start with none and ask for more. */
  f_count = 0;
  more_functions ();
  f_names[0] = strdup("(main)");

  /* Variables. */
  v_count = 0;
  more_variables ();
  
  /* Arrays. */
  a_count = 0;
  more_arrays ();

  /* Other things... */
  ex_stack = NULL;
  fn_stack = NULL;
  i_base = 10;
  o_base = 10;
  scale  = 0;
#if defined(READLINE) || defined(LIBEDIT)
  n_history = -1;	
#endif
  c_code = FALSE;
  bc_init_numbers();
}

/* Three functions for increasing the number of functions, variables, or
   arrays that are needed.  This adds another 32 of the requested object. */

void
more_functions (void)
{
  int old_count;
  int indx;
  bc_function *old_f;
  bc_function *f;
  char **old_names;

  /* Save old information. */
  old_count = f_count;
  old_f = functions;
  old_names = f_names;

  /* Add a fixed amount and allocate new space. */
  f_count += STORE_INCR;
  functions = bc_malloc (f_count*sizeof (bc_function));
  f_names = bc_malloc (f_count*sizeof (char *));

  /* Copy old ones. */
  for (indx = 0; indx < old_count; indx++)
    {
      functions[indx] = old_f[indx];
      f_names[indx] = old_names[indx];
    }

  /* Initialize the new ones. */
  for (; indx < f_count; indx++)
    {
      f = &functions[indx];
      f->f_defined = FALSE;
      f->f_void = FALSE;
      f->f_body = bc_malloc (BC_START_SIZE);
      f->f_body_size = BC_START_SIZE;
      f->f_code_size = 0;
      f->f_label = NULL;
      f->f_autos = NULL;
      f->f_params = NULL;
    }

  /* Free the old elements. */
  if (old_count != 0)
    {
      free (old_f);
      free (old_names);
    }
}

void
more_variables (void)
{
  int indx;
  int old_count;
  bc_var **old_var;
  char **old_names;

  /* Save the old values. */
  old_count = v_count;
  old_var = variables;
  old_names = v_names;

  /* Increment by a fixed amount and allocate. */
  v_count += STORE_INCR;
  variables = bc_malloc (v_count*sizeof(bc_var *));
  v_names = bc_malloc (v_count*sizeof(char *));

  /* Copy the old variables. */
  for (indx = 3; indx < old_count; indx++)
    {
      variables[indx] = old_var[indx];
      v_names[indx] = old_names[indx];
    }

  /* Initialize the new elements. */
  for (; indx < v_count; indx++)
    variables[indx] = NULL;

  /* Free the old elements. */
  if (old_count != 0)
    {
      free (old_var);
      free (old_names);
    }
}

void
more_arrays (void)
{
  int indx;
  int old_count;
  bc_var_array **old_ary;
  char **old_names;

  /* Save the old values. */
  old_count = a_count;
  old_ary = arrays;
  old_names = a_names;

  /* Increment by a fixed amount and allocate. */
  a_count += STORE_INCR;
  arrays = bc_malloc (a_count*sizeof(bc_var_array *));
  a_names = bc_malloc (a_count*sizeof(char *));

  /* Copy the old arrays. */
  for (indx = 1; indx < old_count; indx++)
    {
      arrays[indx] = old_ary[indx];
      a_names[indx] = old_names[indx];
    }


  /* Initialize the new elements. */
  for (; indx < a_count; indx++)
    arrays[indx] = NULL;

  /* Free the old elements. */
  if (old_count != 0)
    {
      free (old_ary);
      free (old_names);
    }
}


/* clear_func clears out function FUNC and makes it ready to redefine. */

void
clear_func (int func)
{
  bc_function *f;
  bc_label_group *lg;

  /* Set the pointer to the function. */
  f = &functions[func];
  f->f_defined = FALSE;
  /* XXX restore f_body to initial size??? */
  f->f_code_size = 0;
  if (f->f_autos != NULL)
    {
      free_args (f->f_autos);
      f->f_autos = NULL;
    }
  if (f->f_params != NULL)
    {
      free_args (f->f_params);
      f->f_params = NULL;
    }
  while (f->f_label != NULL)
    {
      lg = f->f_label->l_next;
      free (f->f_label);
      f->f_label = lg;
    }
}


/*  Pop the function execution stack and return the top. */

int
fpop(void)
{
  fstack_rec *temp;
  int retval;
  
  if (fn_stack != NULL)
    {
      temp = fn_stack;
      fn_stack = temp->s_next;
      retval = temp->s_val;
      free (temp);
    }
  else
    {
      retval = 0;
      rt_error ("function stack underflow, contact maintainer.");
    }
  return (retval);
}


/* Push VAL on to the function stack. */

void
fpush (int val)
{
  fstack_rec *temp;
  
  temp = bc_malloc (sizeof (fstack_rec));
  temp->s_next = fn_stack;
  temp->s_val = val;
  fn_stack = temp;
}


/* Pop and discard the top element of the regular execution stack. */

void
pop (void)
{
  estack_rec *temp;
  
  if (ex_stack != NULL)
    {
      temp = ex_stack;
      ex_stack = temp->s_next;
      bc_free_num (&temp->s_num);
      free (temp);
    }
}


/* Push a copy of NUM on to the regular execution stack. */

void
push_copy (bc_num num)
{
  estack_rec *temp;

  temp = bc_malloc (sizeof (estack_rec));
  temp->s_num = bc_copy_num (num);
  temp->s_next = ex_stack;
  ex_stack = temp;
}


/* Push NUM on to the regular execution stack.  Do NOT push a copy. */

void
push_num (bc_num num)
{
  estack_rec *temp;

  temp = bc_malloc (sizeof (estack_rec));
  temp->s_num = num;
  temp->s_next = ex_stack;
  ex_stack = temp;
}


/* Make sure the ex_stack has at least DEPTH elements on it.
   Return TRUE if it has at least DEPTH elements, otherwise
   return FALSE. */

char
check_stack (int depth)
{
  estack_rec *temp;

  temp = ex_stack;
  while ((temp != NULL) && (depth > 0))
    {
      temp = temp->s_next;
      depth--;
    }
  if (depth > 0)
    {
      rt_error ("Stack error.");
      return FALSE;
    }
  return TRUE;
}


/* The following routines manipulate simple variables and
   array variables. */

/* get_var returns a pointer to the variable VAR_NAME.  If one does not
   exist, one is created. */

bc_var *
get_var (int var_name)
{
  bc_var *var_ptr;

  var_ptr = variables[var_name];
  if (var_ptr == NULL)
    {
      var_ptr = variables[var_name] = bc_malloc (sizeof (bc_var));
      bc_init_num (&var_ptr->v_value);
    }
  return var_ptr;
}


/* get_array_num returns the address of the bc_num in the array
   structure.  If more structure is requried to get to the index,
   this routine does the work to create that structure. VAR_INDEX
   is a zero based index into the arrays storage array. INDEX is
   the index into the bc array. */

bc_num *
get_array_num (int var_index, unsigned long idx)
{
  bc_var_array *ary_ptr;
  bc_array *a_var;
  bc_array_node *temp;
  int log;
  unsigned int ix, ix1;
  int sub [NODE_DEPTH];

  /* Get the array entry. */
  ary_ptr = arrays[var_index];
  if (ary_ptr == NULL)
    {
      ary_ptr = arrays[var_index] = bc_malloc (sizeof (bc_var_array));
      ary_ptr->a_value = NULL;
      ary_ptr->a_next = NULL;
      ary_ptr->a_param = FALSE;
    }

  a_var = ary_ptr->a_value;
  if (a_var == NULL) {
    a_var = ary_ptr->a_value = bc_malloc (sizeof (bc_array));
    a_var->a_tree = NULL;
    a_var->a_depth = 0;
  }

  /* Get the index variable. */
  sub[0] = idx & NODE_MASK;
  ix = idx >> NODE_SHIFT;
  log = 1;
  while (ix > 0 || log < a_var->a_depth)
    {
      sub[log] = ix & NODE_MASK;
      ix >>= NODE_SHIFT;
      log++;
    }
  
  /* Build any tree that is necessary. */
  while (log > a_var->a_depth)
    {
      temp = bc_malloc (sizeof(bc_array_node));
      if (a_var->a_depth != 0)
	{
	  temp->n_items.n_down[0] = a_var->a_tree;
	  for (ix=1; ix < NODE_SIZE; ix++)
	    temp->n_items.n_down[ix] = NULL;
	}
      else
	{
	  for (ix=0; ix < NODE_SIZE; ix++)
	    temp->n_items.n_num[ix] = bc_copy_num(_zero_);
	}
      a_var->a_tree = temp;
      a_var->a_depth++;
    }
  
  /* Find the indexed variable. */
  temp = a_var->a_tree;
  while ( log-- > 1)
    {
      ix1 = sub[log];
      if (temp->n_items.n_down[ix1] == NULL)
	{
	  temp->n_items.n_down[ix1] = bc_malloc (sizeof(bc_array_node));
	  temp = temp->n_items.n_down[ix1];
	  if (log > 1)
	    for (ix=0; ix < NODE_SIZE; ix++)
	      temp->n_items.n_down[ix] = NULL;
	  else
	    for (ix=0; ix < NODE_SIZE; ix++)
	      temp->n_items.n_num[ix] = bc_copy_num(_zero_);
	}
      else
	temp = temp->n_items.n_down[ix1];
    }
  
  /* Return the address of the indexed variable. */
  return &(temp->n_items.n_num[sub[0]]);
}


/* Store the top of the execution stack into VAR_NAME.  
   This includes the special variables ibase, obase, and scale. */

void
store_var (int var_name)
{
  bc_var *var_ptr;
  long temp;
  char toobig;

  if (var_name > 3)
    {
      /* It is a simple variable. */
      var_ptr = get_var (var_name);
      if (var_ptr != NULL)
	{
	  bc_free_num(&var_ptr->v_value);
	  var_ptr->v_value = bc_copy_num (ex_stack->s_num);
	}
    }
  else
    {
      /* It is a special variable... */
      toobig = FALSE;
      temp = 0;
      if (bc_is_neg (ex_stack->s_num))
	{
	  switch (var_name)
	    {
	    case 0:
	      rt_warn ("negative ibase, set to 2");
	      temp = 2;
	      break;
	    case 1:
	      rt_warn ("negative obase, set to 2");
	      temp = 2;
	      break;
	    case 2:
	      rt_warn ("negative scale, set to 0");
	      temp = 0;
	      break;
#if defined(READLINE) || defined(LIBEDIT)
	    case 3:
	      temp = -1;
	      break;
#endif
	    }
	}
      else
	{
	  temp = bc_num2long (ex_stack->s_num);
	  if (!bc_is_zero (ex_stack->s_num) && temp == 0)
	    toobig = TRUE;
	}
      switch (var_name)
	{
	case 0:
	  if (temp < 2 && !toobig)
	    {
	      i_base = 2;
	      rt_warn ("ibase too small, set to 2");
	    }
	  else
	    if (temp > 16 || toobig)
	      {
	        if (std_only)
                  {
		    i_base = 16;  
		    rt_warn ("ibase too large, set to 16");
                  } 
                else if (temp > 36 || toobig) 
                  {
		    i_base = 36;
		    rt_warn ("ibase too large, set to 36");
                  }
                else
                  { 
                     if (temp >= 16 && warn_not_std)
                       rt_warn ("ibase larger than 16 is non-standard");
		     i_base = temp;
                  }
	      }
	    else
	      i_base = (int) temp;
	  break;

	case 1:
	  if (temp < 2 && !toobig)
	    {
	      o_base = 2;
	      rt_warn ("obase too small, set to 2");
	    }
	  else
	    if (temp > BC_BASE_MAX || toobig)
	      {
		o_base = BC_BASE_MAX;
		rt_warn ("obase too large, set to %d", BC_BASE_MAX);
	      }
	    else
	      o_base = (int) temp;
	  break;

	case 2:
	  /*  WARNING:  The following if statement may generate a compiler
	      warning if INT_MAX == LONG_MAX.  This is NOT a problem. */
	  if (temp > BC_SCALE_MAX || toobig )
	    {
	      scale = BC_SCALE_MAX;
	      rt_warn ("scale too large, set to %d", BC_SCALE_MAX);
	    }
	  else
	    scale = (int) temp;
	  break;

#if defined(READLINE) || defined(LIBEDIT)
	case 3:
	  if (toobig)
	    {
	      temp = -1;
	      rt_warn ("history too large, set to unlimited");
	      UNLIMIT_HISTORY;
	    }
	  else
	    {
	      n_history = temp;
	      if (temp < 0)
		UNLIMIT_HISTORY;
	      else
		HISTORY_SIZE(n_history);
	    }
#endif
	}
    }
}


/* Store the top of the execution stack into array VAR_NAME. 
   VAR_NAME is the name of an array, and the next to the top
   of stack for the index into the array. */

void
store_array (int var_name)
{
  bc_num *num_ptr;
  long idx;

  if (!check_stack(2)) return;
  idx = bc_num2long (ex_stack->s_next->s_num);
  if (idx < 0 || idx > BC_DIM_MAX ||
      (idx == 0 && !bc_is_zero(ex_stack->s_next->s_num))) 
    rt_error ("Array %s subscript out of bounds.", a_names[var_name]);
  else
    {
      num_ptr = get_array_num (var_name, idx);
      if (num_ptr != NULL)
	{
	  bc_free_num (num_ptr);
	  *num_ptr = bc_copy_num (ex_stack->s_num);
	  bc_free_num (&ex_stack->s_next->s_num);
	  ex_stack->s_next->s_num = ex_stack->s_num;
	  bc_init_num (&ex_stack->s_num);
	  pop();
	}
    }
}


/*  Load a copy of VAR_NAME on to the execution stack.  This includes
    the special variables ibase, obase and scale.  */

void
load_var (int var_name)
{
  bc_var *var_ptr;

  switch (var_name)
    {

    case 0:
      /* Special variable ibase. */
      push_copy (_zero_);
      bc_int2num (&ex_stack->s_num, i_base);
      break;

    case 1:
      /* Special variable obase. */
      push_copy (_zero_);
      bc_int2num (&ex_stack->s_num, o_base);
      break;

    case 2:
      /* Special variable scale. */
      push_copy (_zero_);
      bc_int2num (&ex_stack->s_num, scale);
      break;

#if defined(READLINE) || defined(LIBEDIT)
    case 3:
      /* Special variable history. */
      push_copy (_zero_);
      bc_int2num (&ex_stack->s_num, n_history);
      break;
#endif

    default:
      /* It is a simple variable. */
      var_ptr = variables[var_name];
      if (var_ptr != NULL)
	push_copy (var_ptr->v_value);
      else
	push_copy (_zero_);
    }
}


/*  Load a copy of VAR_NAME on to the execution stack.  This includes
    the special variables ibase, obase and scale.  */

void
load_array (int var_name)
{
  bc_num *num_ptr;
  long   idx;

  if (!check_stack(1)) return;
  idx = bc_num2long (ex_stack->s_num);
  if (idx < 0 || idx > BC_DIM_MAX ||
     (idx == 0 && !bc_is_zero(ex_stack->s_num))) 
    rt_error ("Array %s subscript out of bounds.", a_names[var_name]);
  else
    {
      num_ptr = get_array_num (var_name, idx);
      if (num_ptr != NULL)
	{
	  pop();
	  push_copy (*num_ptr);
	}
    }
}


/* Decrement VAR_NAME by one.  This includes the special variables
   ibase, obase, and scale. */

void
decr_var (int var_name)
{
  bc_var *var_ptr;

  switch (var_name)
    {

    case 0: /* ibase */
      if (i_base > 2)
	i_base--;
      else
	rt_warn ("ibase too small in --");
      break;
      
    case 1: /* obase */
      if (o_base > 2)
	o_base--;
      else
	rt_warn ("obase too small in --");
      break;

    case 2: /* scale */
      if (scale > 0)
	scale--;
      else
	rt_warn ("scale can not be negative in -- ");
      break;

#if defined(READLINE) || defined(LIBEDIT)
    case 3: /* history */
      n_history--;
      if (n_history >= 0)
	HISTORY_SIZE(n_history);
      else
	{
	  n_history = -1;
	  rt_warn ("history is negative, set to unlimited");
	  UNLIMIT_HISTORY;
	}
      break;
#endif

    default: /* It is a simple variable. */
      var_ptr = get_var (var_name);
      if (var_ptr != NULL)
	bc_sub (var_ptr->v_value,_one_,&var_ptr->v_value, 0);
    }
}


/* Decrement VAR_NAME by one.  VAR_NAME is an array, and the top of
   the execution stack is the index and it is popped off the stack. */

void
decr_array (int var_name)
{
  bc_num *num_ptr;
  long   idx;

  /* It is an array variable. */
  if (!check_stack (1)) return;
  idx = bc_num2long (ex_stack->s_num);
  if (idx < 0 || idx > BC_DIM_MAX ||
     (idx == 0 && !bc_is_zero (ex_stack->s_num))) 
    rt_error ("Array %s subscript out of bounds.", a_names[var_name]);
  else
    {
      num_ptr = get_array_num (var_name, idx);
      if (num_ptr != NULL)
	{
	  pop ();
	  bc_sub (*num_ptr, _one_, num_ptr, 0);
	}
    }
}


/* Increment VAR_NAME by one.  This includes the special variables
   ibase, obase, and scale. */

void
incr_var (int var_name)
{
  bc_var *var_ptr;

  switch (var_name)
    {

    case 0: /* ibase */
      if (i_base < 16)
	i_base++;
      else
	rt_warn ("ibase too big in ++");
      break;

    case 1: /* obase */
      if (o_base < BC_BASE_MAX)
	o_base++;
      else
	rt_warn ("obase too big in ++");
      break;

    case 2:
      if (scale < BC_SCALE_MAX)
	scale++;
      else
	rt_warn ("Scale too big in ++");
      break;

#if defined(READLINE) || defined(LIBEDIT)
    case 3: /* history */
      n_history++;
      if (n_history > 0)
	HISTORY_SIZE(n_history);
      else
	{
	  n_history = -1;
	  rt_warn ("history set to unlimited");
	  UNLIMIT_HISTORY;
	}
      break;	
#endif

    default:  /* It is a simple variable. */
      var_ptr = get_var (var_name);
      if (var_ptr != NULL)
	bc_add (var_ptr->v_value, _one_, &var_ptr->v_value, 0);

    }
}


/* Increment VAR_NAME by one.  VAR_NAME is an array and top of
   execution stack is the index and is popped off the stack. */

void
incr_array (int var_name)
{
  bc_num *num_ptr;
  long   idx;

  if (!check_stack (1)) return;
  idx = bc_num2long (ex_stack->s_num);
  if (idx < 0 || idx > BC_DIM_MAX ||
      (idx == 0 && !bc_is_zero (ex_stack->s_num))) 
    rt_error ("Array %s subscript out of bounds.", a_names[var_name]);
  else
    {
      num_ptr = get_array_num (var_name, idx);
      if (num_ptr != NULL)
	{
	  pop ();
	  bc_add (*num_ptr, _one_, num_ptr, 0);
	}
    }
}


/* Routines for processing autos variables and parameters. */

/* NAME is an auto variable that needs to be pushed on its stack. */

void
auto_var (int name)
{
  bc_var *v_temp;
  bc_var_array *a_temp;
  int ix;

  if (name > 0)
    {
      /* A simple variable. */
      ix = name;
      v_temp = bc_malloc (sizeof (bc_var));
      v_temp->v_next = variables[ix];
      bc_init_num (&v_temp->v_value);
      variables[ix] = v_temp;
    }
  else
    {
      /* An array variable. */
      ix = -name;
      a_temp = bc_malloc (sizeof (bc_var_array));
      a_temp->a_next = arrays[ix];
      a_temp->a_value = NULL;
      a_temp->a_param = FALSE;
      arrays[ix] = a_temp;
    } 
}


/* Free_a_tree frees everything associated with an array variable tree.
   This is used when popping an array variable off its auto stack.  */

void
free_a_tree (bc_array_node *root, int depth)
{
  int ix;

  if (root != NULL)
    {
      if (depth > 1)
	for (ix = 0; ix < NODE_SIZE; ix++)
	  free_a_tree (root->n_items.n_down[ix], depth-1);
      else
	for (ix = 0; ix < NODE_SIZE; ix++)
	  bc_free_num ( &(root->n_items.n_num[ix]));
      free (root);
    }
}


/* LIST is an NULL terminated list of varible names that need to be
   popped off their auto stacks. */

void
pop_vars (arg_list *list)
{
  bc_var *v_temp;
  bc_var_array *a_temp;
  int    ix;

  while (list != NULL)
    {
      ix = list->av_name;
      if (ix > 0)
	{
	  /* A simple variable. */
	  v_temp = variables[ix];
	  if (v_temp != NULL)
	    {
	      variables[ix] = v_temp->v_next;
	      bc_free_num (&v_temp->v_value);
	      free (v_temp);
	    }
	}
      else
	{
	  /* An array variable. */
	  ix = -ix;
	  a_temp = arrays[ix];
	  if (a_temp != NULL)
	    {
	      arrays[ix] = a_temp->a_next;
	      if (!a_temp->a_param && a_temp->a_value != NULL)
		{
		  free_a_tree (a_temp->a_value->a_tree,
			       a_temp->a_value->a_depth);
		  free (a_temp->a_value);
		}
	      free (a_temp);
	    }
	} 
      list = list->next;
    }
}

/* COPY_NODE: Copies an array node for a call by value parameter. */
static bc_array_node *
copy_tree (bc_array_node *ary_node, int depth)
{
  bc_array_node *res = bc_malloc (sizeof(bc_array_node));
  int i;

  if (depth > 1)
    for (i=0; i<NODE_SIZE; i++)
      if (ary_node->n_items.n_down[i] != NULL)
	res->n_items.n_down[i] =
	  copy_tree (ary_node->n_items.n_down[i], depth - 1);
      else
	res->n_items.n_down[i] = NULL;
  else
    for (i=0; i<NODE_SIZE; i++)
      if (ary_node->n_items.n_num[i] != NULL)
	res->n_items.n_num[i] = bc_copy_num (ary_node->n_items.n_num[i]);
      else
	res->n_items.n_num[i] = NULL;
  return res;
}

/* COPY_ARRAY: Copies an array for a call by value array parameter. 
   ARY is the pointer to the bc_array structure. */

static bc_array *
copy_array (bc_array *ary)
{
  bc_array *res = bc_malloc (sizeof(bc_array));
  res->a_depth = ary->a_depth;
  res->a_tree = copy_tree (ary->a_tree, ary->a_depth);
  return (res);
}


/* A call is being made to FUNC.  The call types are at PC.  Process
   the parameters by doing an auto on the parameter variable and then
   store the value at the new variable or put a pointer the the array
   variable. */

void
process_params (program_counter *progctr, int func)
{
  char ch;
  arg_list *params;
  int ix, ix1;
  bc_var *v_temp;
  bc_var_array *a_src, *a_dest;
  
  /* Get the parameter names from the function. */
  params = functions[func].f_params;

  while ((ch = byte(progctr)) != ':')
    {
      if (params != NULL)
	{
	  if ((ch == '0') && params->av_name > 0)
	    {
	      /* A simple variable. */
	      ix = params->av_name;
	      v_temp = bc_malloc (sizeof(bc_var));
	      v_temp->v_next = variables[ix];
	      v_temp->v_value = ex_stack->s_num;
	      bc_init_num (&ex_stack->s_num);
	      variables[ix] = v_temp;
	    }
	  else
	    if ((ch == '1') && (params->av_name < 0))
	      {
		/* The variables is an array variable. */
	
		/* Compute source index and make sure some structure exists. */
		ix = (int) bc_num2long (ex_stack->s_num);
		(void) get_array_num (ix, 0);    
	
		/* Push a new array and Compute Destination index */
		auto_var (params->av_name);  
		ix1 = -params->av_name;

		/* Set up the correct pointers in the structure. */
		if (ix == ix1) 
		  a_src = arrays[ix]->a_next;
		else
		  a_src = arrays[ix];
		a_dest = arrays[ix1];
		if (params->arg_is_var)
		  {
		    a_dest->a_param = TRUE;
		    a_dest->a_value = a_src->a_value;
		  }
		else
		  {
		    a_dest->a_param = FALSE;
		    a_dest->a_value = copy_array (a_src->a_value);
		  }
	      }
	    else
	      {
		if (params->av_name < 0)
		  rt_error ("Parameter type mismatch parameter %s.",
			    a_names[-params->av_name]);
		else
		  rt_error ("Parameter type mismatch, parameter %s.",
			    v_names[params->av_name]);
		params++;
	      }
	  pop ();
	}
      else
	{
	    rt_error ("Parameter number mismatch");
	    return;
	}
      params = params->next;
    }
  if (params != NULL) 
    rt_error ("Parameter number mismatch");
}