home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 018.lha / sources / number.c < prev    next >
C/C++ Source or Header  |  1986-10-19  |  3KB  |  145 lines

  1. /*
  2.     Little Smalltalk
  3.  
  4.         number definitions
  5.         timothy a. budd, 10/84
  6. */
  7. /*
  8.     The source code for the Little Smalltalk System may be freely
  9.     copied provided that the source of all files is acknowledged
  10.     and that this condition is copied with each file.
  11.  
  12.     The Little Smalltalk System is distributed without responsibility
  13.     for the performance of the program and without any guarantee of
  14.     maintenance.
  15.  
  16.     All questions concerning Little Smalltalk should be addressed to:
  17.  
  18.         Professor Tim Budd
  19.         Department of Computer Science
  20.         The University of Arizona
  21.         Tucson, Arizona
  22.         85721
  23.         USA
  24. */
  25. # include <stdio.h>
  26. # include "object.h"
  27. # include "number.h"
  28.  
  29. # define MAXLOW 100    /* maximum low numbers kept */
  30.  
  31. static integer *low_nums[MAXLOW];  /* better be initialized to zero ! */
  32.  
  33. static mstruct *fr_integer = 0;
  34. static mstruct *fr_float = 0;
  35.  
  36. static integer init_itable[INTINITMAX];
  37.  
  38. int_init() {
  39.     integer *p;
  40.     mstruct *new;
  41.     int i;
  42.  
  43.     for (p = init_itable, i = 0; i < INTINITMAX; i++, p++) {
  44.         new = (mstruct *) p;
  45.         new->mlink = fr_integer;
  46.         /*fprintf(stderr,"init int %d %d\n", new, new->mlink);*/
  47.         fr_integer = new;
  48.         }
  49. }
  50.  
  51. int ca_int = 0;    /* count the number of integer allocations */
  52.  
  53. extern object *o_magnitude;
  54. extern object *o_number;
  55.  
  56. /* new_cori - new character or integer */
  57. object *new_cori(val, type)
  58. int val, type;
  59. {    register integer *new;
  60.  
  61.     if ((type == 1) && (val >= 0 && val < MAXLOW) && low_nums[val])
  62.         return( (struct obj_struct *) low_nums[val]);
  63.  
  64.     if (fr_integer) {
  65.         new = (integer *) fr_integer;
  66.         /*fprintf(stderr,"int off list %d %d\n", fr_integer,
  67.         fr_integer->mlink);*/
  68.         fr_integer = fr_integer->mlink;
  69.         }
  70.     else {
  71.         new = structalloc(integer);
  72.         /*fprintf(stderr,"allocating new int %d\n", new);*/
  73.         ca_int++;
  74.         }
  75.  
  76.     new->i_ref_count = 0;
  77.     new->i_value = val;
  78.     switch(type) {
  79.         case 0: /* chars */
  80.             new->i_size = CHARSIZE;
  81.               break;
  82.  
  83.         case 1: /* integers */
  84.             new->i_size = INTEGERSIZE;
  85.             if (val >= 0 && val < MAXLOW)
  86.                 sassign(low_nums[val], new);
  87.               break;
  88.  
  89.         default: cant_happen(5);
  90.         }
  91.     return ((object *) new);
  92. }
  93.  
  94. free_integer(i)
  95. integer *i;
  96. {
  97.     if ((! is_integer(i)) && (! is_character(i)))
  98.         cant_happen(8);
  99.     ((mstruct *) i)->mlink = fr_integer;
  100.     fr_integer = (mstruct *) i;
  101.     /*fprintf(stderr,"freeing integer %d %d\n", fr_integer,
  102.     fr_integer->mlink);*/
  103. }
  104.  
  105. free_low_nums()
  106. {    int i;
  107.  
  108.     for (i = 0; i < MAXLOW; i++)
  109.         if (low_nums[i]) {
  110.             obj_dec((object *) low_nums[i]);
  111.             low_nums[i] = NULL;
  112.             }
  113. }
  114.  
  115. int ca_float = 0;
  116.  
  117. /* new_float - produce a new floating point number */
  118. object *new_float(val)
  119. double val;
  120. {    register sfloat *new;
  121.  
  122.     if (fr_float) {
  123.         new = (sfloat *) fr_float;
  124.         fr_float = fr_float->mlink;
  125.         }
  126.     else {
  127.         new = structalloc(sfloat);
  128.         ca_float++;
  129.         }
  130.  
  131.     new->f_ref_count = 0;
  132.     new->f_size = FLOATSIZE;
  133.     new->f_value = val;
  134.     return( (object *) new);
  135. }
  136.  
  137. free_float(f)
  138. sfloat *f;
  139. {
  140.     if (! is_float(f))
  141.         cant_happen(8);
  142.     ((mstruct *) f)->mlink = fr_float;
  143.     fr_float = (mstruct *) f;
  144. }
  145.