home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 09 / bob / bobmem.c < prev    next >
Text File  |  1991-07-11  |  4KB  |  188 lines

  1. /* bobmem.c - memory manager */
  2. /*
  3.     Copyright (c) 1991, by David Michael Betz
  4.     All rights reserved
  5. */
  6.  
  7. #include <setjmp.h>
  8. #include "bob.h"
  9.  
  10. /* external variables */
  11. extern jmp_buf error_trap;    /* the error trap target */
  12.  
  13. /* global variables */
  14. DICTIONARY *symbols;    /* the symbol table */
  15. DICTIONARY *classes;    /* the class table */
  16.  
  17. /* initialize - initialize the virtual machine */
  18. initialize(smax,cmax)
  19.   int smax,cmax;
  20. {
  21.     int obj_class(),cls_new();
  22.     VALUE *allocvector();
  23.     
  24.     /* setup an error trap handler */
  25.     if (setjmp(error_trap) != 0)
  26.     return;
  27.  
  28.     /* initialize the compiler */
  29.     init_compiler(cmax);
  30.  
  31.     /* allocate the stack */
  32.     stkbase = (VALUE *)getmemory(smax * sizeof(VALUE));
  33.     stktop = sp = stkbase + smax;
  34.  
  35.     /* create the symbol and class tables */
  36.     symbols = newdictionary(NULL);
  37.     classes = newdictionary(NULL);
  38.  
  39.     /* enter the built-in functions */
  40.     init_functions();
  41. }
  42.  
  43. /* newclass - create a new class */
  44. CLASS *newclass(name,base)
  45.   char *name; CLASS *base;
  46. {
  47.     CLASS *theclass;
  48.  
  49.     /* allocate the memory for the new class */
  50.     theclass = (CLASS *)getmemory(sizeof(CLASS));
  51.  
  52.     /* initialize */
  53.     theclass->cl_name = copystring(name);
  54.     theclass->cl_base = base;
  55.     theclass->cl_members = newdictionary(theclass);
  56.     theclass->cl_functions = newdictionary(theclass);
  57.     theclass->cl_size = 0;
  58.  
  59.     /* return the new class */
  60.     return (theclass);
  61. }
  62.  
  63. /* newdictionary - create a new dictionary */
  64. DICTIONARY *newdictionary(class)
  65.   CLASS *class;
  66. {
  67.     DICTIONARY *dict;
  68.     dict = (DICTIONARY *)getmemory(sizeof(DICTIONARY));
  69.     dict->di_class = class;
  70.     dict->di_contents = NULL;
  71.     return (dict);
  72. }
  73.  
  74. /* addentry - add an entry to a dictionary */
  75. DICT_ENTRY *addentry(dict,key,type)
  76.   DICTIONARY *dict; char *key; int type;
  77. {
  78.     DICT_ENTRY *entry;
  79.     if ((entry = findentry(dict,key)) == NULL) {
  80.     entry = (DICT_ENTRY *)getmemory(sizeof(DICT_ENTRY));
  81.     entry->de_dictionary = dict;
  82.     entry->de_key = copystring(key);
  83.     entry->de_type = type;
  84.     set_nil(&entry->de_value);
  85.     entry->de_next = dict->di_contents;
  86.     dict->di_contents = entry;
  87.     }
  88.     return (entry);
  89. }
  90.  
  91. /* findentry - find an entry in a dictionary */
  92. DICT_ENTRY *findentry(dict,key)
  93.   DICTIONARY *dict; char *key;
  94. {
  95.     DICT_ENTRY *entry;
  96.     for (entry = dict->di_contents; entry != NULL; entry = entry->de_next)
  97.     if (strcmp(key,entry->de_key) == 0)
  98.         return (entry);
  99.     return (NULL);
  100. }
  101.  
  102. /* copystring - make a copy of a string */
  103. char *copystring(str)
  104.   char *str;
  105. {
  106.     char *val;
  107.     val = getmemory(strlen(str)+1);
  108.     strcpy(val,str);
  109.     return (val);
  110. }
  111.  
  112. /* makestring - make an initialized string */
  113. STRING *makestring(str)
  114.   char *str;
  115. {
  116.     STRING *val;
  117.     int len;
  118.     len = strlen(str);
  119.     val = newstring(len);
  120.     strncpy(val->s_data,str,len);
  121.     return (val);
  122. }
  123.  
  124. /* getcstring - get a C-style version of a string */
  125. getcstring(buf,max,str)
  126.   char *buf; int max; STRING *str;
  127. {
  128.     int len;
  129.     if ((len = str->s_length) >= max)
  130.     len = max - 1;
  131.     strncpy(buf,str->s_data,len);
  132.     buf[len] = '\0';
  133. }
  134.  
  135. /* newstring - allocate a new string object */
  136. STRING *newstring(n)
  137.   int n;
  138. {
  139.     STRING *val;
  140.     char *p;
  141.     val = (STRING *)getmemory(sizeof(STRING)+n-1);
  142.     val->s_length = n;
  143.     for (p = val->s_data; --n >= 0; )
  144.     *p++ = '\0';
  145.     return (val);
  146. }
  147.  
  148. /* newobject - allocate a new object */
  149. VALUE *newobject(class)
  150.   CLASS *class;
  151. {
  152.     VALUE *allocvector(),*val;
  153.     val = allocvector(class->cl_size);
  154.     set_class(&val[OB_CLASS],class);
  155.     return (val);
  156. }
  157.  
  158. /* newvector - allocate a new vector */
  159. VALUE *newvector(n)
  160.   int n;
  161. {
  162.     VALUE *allocvector(),*val;
  163.     val = allocvector(n+1);
  164.     set_integer(&val[0],n);
  165.     return (val);
  166. }
  167.  
  168. /* allocvector - allocate a new vector */
  169. static VALUE *allocvector(n)
  170.   int n;
  171. {
  172.     VALUE *val,*p;
  173.     val = (VALUE *)getmemory(n * sizeof(VALUE));
  174.     for (p = val; --n >= 0; ++p)
  175.     set_nil(p);
  176.     return (val);
  177. }
  178.  
  179. /* getmemory - allocate memory and complain if there isn't enough */
  180. char *getmemory(size)
  181.   int size;
  182. {
  183.     char *malloc(),*val;
  184.     if ((val = malloc(size)) == NULL)
  185.     error("Insufficient memory");
  186.     return (val);
  187. }
  188.