home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1997: The Complete Utilities Toolkit / macworld-complete-utilities-1997.iso / Programming / Little Smalltalk v3.1.5 / C Source / Sources / news.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-21  |  3.8 KB  |  186 lines  |  [TEXT/KAHL]

  1. /*
  2.     little smalltalk, version 3.1
  3.     written by tim budd, July 1988
  4.  
  5.     new object creation routines
  6.     built on top of memory allocation, these routines
  7.     handle the creation of various kinds of objects
  8. */
  9.  
  10. # include <stdio.h>
  11. # include "env.h"
  12. # include "memory.h"
  13. # include "names.h"
  14.  
  15. #ifdef THINKC
  16. #include "memory.proto.h"
  17. #include "news.proto.h"
  18. #include "names.proto.h"
  19. #endif
  20.  
  21. static object arrayClass = nilobj;    /* the class Array */
  22. static object intClass = nilobj;    /* the class Integer */
  23. static object stringClass = nilobj;    /* the class String */
  24. static object symbolClass = nilobj;    /* the class Symbol */
  25.  
  26. void ncopy(char *p, char *q, int n)
  27. {    for (; n>0; n--)
  28.         *p++ = *q++;
  29. }
  30.  
  31. object getClass(object obj)
  32. {
  33.     if (isInteger(obj)) {
  34.         if (intClass == nilobj)
  35.             intClass = globalSymbol("Integer");
  36.         return(intClass);
  37.         }
  38.     return classField (obj);
  39. }
  40.  
  41. object newArray(int size)
  42. {    object newObj;
  43.  
  44.     newObj = allocObject(size);
  45.     if (arrayClass == nilobj)
  46.         arrayClass = globalSymbol("Array");
  47.     setClass(newObj, arrayClass);
  48.     return newObj;
  49. }
  50.  
  51. object newBlock(void)
  52. {    object newObj;
  53.  
  54.     newObj = allocObject(blockSize);
  55.     setClass(newObj, globalSymbol("Block"));
  56.     return newObj;
  57. }
  58.  
  59. object newByteArray(int size)
  60. {    object newobj;
  61.  
  62.     newobj = allocByte(size);
  63.     setClass(newobj, globalSymbol("ByteArray"));
  64.     return newobj;
  65. }
  66.  
  67. object newChar(int value)
  68. {    object newobj;
  69.  
  70.     newobj = allocObject(1);
  71.     basicAtPut(newobj, 1, newInteger(value));
  72.     setClass(newobj, globalSymbol("Char"));
  73.     return(newobj);
  74. }
  75.  
  76. object newClass(char *name)
  77. {    object newObj, nameObj;
  78.  
  79.     newObj = allocObject(classSize);
  80.     setClass(newObj, globalSymbol("Class"));
  81.  
  82.     /* now make name */
  83.     nameObj = newSymbol(name);
  84.     basicAtPut(newObj, nameInClass, nameObj);
  85.  
  86.     /* now put in global symbols table */
  87.     nameTableInsert(symbols, strHash(name), nameObj, newObj);
  88.  
  89.     return newObj;
  90. }
  91.  
  92. object copyFrom(object obj, int start, int size)
  93. {    object newObj;
  94.     int i;
  95.  
  96.     newObj = newArray(size);
  97.     for (i = 1; i <= size; i++) {
  98.         basicAtPut(newObj, i, basicAt(obj, start));
  99.         start++;
  100.         }
  101.     return newObj;
  102. }
  103.  
  104. object newContext(int link, object method, object args, object temp)
  105. {    object newObj;
  106.  
  107.     newObj = allocObject(contextSize);
  108.     setClass(newObj, globalSymbol("Context"));
  109.     basicAtPut(newObj, linkPtrInContext, newInteger(link));
  110.     basicAtPut(newObj, methodInContext, method);
  111.     basicAtPut(newObj, argumentsInContext, args);
  112.     basicAtPut(newObj, temporariesInContext, temp);
  113.     return newObj;
  114. }
  115.  
  116. object newDictionary(int size)
  117. {    object newObj;
  118.  
  119.     newObj = allocObject(1);
  120.     setClass(newObj, globalSymbol("Dictionary"));
  121.     basicAtPut(newObj, 1, newArray(size));
  122.     return newObj;
  123. }
  124.  
  125. object newFloat(double d)
  126. {    object newObj;
  127.  
  128.     newObj = allocByte((int) sizeof (double));
  129.     ncopy(charPtr(newObj), (char *) &d, (int) sizeof (double));
  130.     setClass(newObj, globalSymbol("Float"));
  131.     return newObj;
  132. }
  133.  
  134. double floatValue(object o)
  135. {    double d;
  136.  
  137.     ncopy((char *) &d, charPtr(o), (int) sizeof(double));
  138.     return d;
  139. }
  140.  
  141. object newLink(object key, object value)
  142. {    object newObj;
  143.  
  144.     newObj = allocObject(3);
  145.     setClass(newObj, globalSymbol("Link"));
  146.     basicAtPut(newObj, 1, key);
  147.     basicAtPut(newObj, 2, value);
  148.     return newObj;
  149. }
  150.  
  151. object newMethod(void)
  152. {    object newObj;
  153.  
  154.     newObj = allocObject(methodSize);
  155.     setClass(newObj, globalSymbol("Method"));
  156.     return newObj;
  157. }
  158.  
  159. object newStString(char *value)
  160. {    object newObj;
  161.  
  162.     newObj = allocStr(value);
  163.     if (stringClass == nilobj)
  164.         stringClass = globalSymbol("String");
  165.     setClass(newObj, stringClass);
  166.     return(newObj);
  167. }
  168.  
  169. object newSymbol(char *str)
  170. {     object newObj;
  171.  
  172.     /* first see if it is already there */
  173.     newObj = globalKey(str);
  174.     if (newObj) 
  175.         return newObj;
  176.  
  177.     /* not found, must make */
  178.     newObj = allocStr(str);
  179.     if (symbolClass == nilobj)
  180.         symbolClass = globalSymbol("Symbol");
  181.     setClass(newObj, symbolClass);
  182.     nameTableInsert(symbols, strHash(str), newObj, nilobj);
  183.     return newObj;
  184. }
  185.  
  186.