home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / smalltk / src / news.c < prev    next >
C/C++ Source or Header  |  1991-10-12  |  4KB  |  196 lines

  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. static object arrayClass = nilobj;    /* the class Array */
  16. static object intClass = nilobj;    /* the class Integer */
  17. static object stringClass = nilobj;    /* the class String */
  18. static object symbolClass = nilobj;    /* the class Symbol */
  19.  
  20. ncopy(p, q, n)        /* ncopy - copy exactly n bytes from place to place */
  21. register char *p, *q;
  22. register int n;
  23. {    for (; n>0; n--)
  24.         *p++ = *q++;
  25. }
  26.  
  27. object getClass(obj)    /* getClass - get the class of an object */
  28. register object obj;
  29. {
  30.     if (isInteger(obj)) {
  31.         if (intClass == nilobj)
  32.             intClass = globalSymbol("Integer");
  33.         return(intClass);
  34.         }
  35.     return (classField(obj));
  36. }
  37.  
  38. object newArray(size)
  39. int size;
  40. {    object newObj;
  41.  
  42.     newObj = allocObject(size);
  43.     if (arrayClass == nilobj)
  44.         arrayClass = globalSymbol("Array");
  45.     setClass(newObj, arrayClass);
  46.     return newObj;
  47. }
  48.  
  49. object newBlock()
  50. {    object newObj;
  51.  
  52.     newObj = allocObject(blockSize);
  53.     setClass(newObj, globalSymbol("Block"));
  54.     return newObj;
  55. }
  56.  
  57. object newByteArray(size)
  58. int size;
  59. {    object newobj;
  60.  
  61.     newobj = allocByte(size);
  62.     setClass(newobj, globalSymbol("ByteArray"));
  63.     return newobj;
  64. }
  65.  
  66. object newChar(value)
  67. 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(name)
  77. char *name;
  78. {    object newObj, nameObj;
  79.  
  80.     newObj = allocObject(classSize);
  81.     setClass(newObj, globalSymbol("Class"));
  82.  
  83.     /* now make name */
  84.     nameObj = newSymbol(name);
  85.     basicAtPut(newObj, nameInClass, nameObj);
  86.  
  87.     /* now put in global symbols table */
  88.     nameTableInsert(symbols, strHash(name), nameObj, newObj);
  89.  
  90.     return newObj;
  91. }
  92.  
  93. object copyFrom(obj, start, size)
  94. object obj;
  95. int start, size;
  96. {    object newObj;
  97.     int i;
  98.  
  99.     newObj = newArray(size);
  100.     for (i = 1; i <= size; i++) {
  101.         basicAtPut(newObj, i, basicAt(obj, start));
  102.         start++;
  103.         }
  104.     return newObj;
  105. }
  106.  
  107. object newContext(link, method, args, temp)
  108. int link;
  109. object method, args, temp;
  110. {    object newObj;
  111.  
  112.     newObj = allocObject(contextSize);
  113.     setClass(newObj, globalSymbol("Context"));
  114.     basicAtPut(newObj, linkPtrInContext, newInteger(link));
  115.     basicAtPut(newObj, methodInContext, method);
  116.     basicAtPut(newObj, argumentsInContext, args);
  117.     basicAtPut(newObj, temporariesInContext, temp);
  118.     return newObj;
  119. }
  120.  
  121. object newDictionary(size)
  122. int size;
  123. {    object newObj;
  124.  
  125.     newObj = allocObject(1);
  126.     setClass(newObj, globalSymbol("Dictionary"));
  127.     basicAtPut(newObj, 1, newArray(size));
  128.     return newObj;
  129. }
  130.  
  131. object newFloat(d)
  132. double d;
  133. {    object newObj;
  134.  
  135.     newObj = allocByte((int) sizeof (double));
  136.     ncopy(charPtr(newObj), (char *) &d, (int) sizeof (double));
  137.     setClass(newObj, globalSymbol("Float"));
  138.     return newObj;
  139. }
  140.  
  141. double floatValue(o)
  142. object o;
  143. {    double d;
  144.  
  145.     ncopy((char *) &d, charPtr(o), (int) sizeof(double));
  146.     return d;
  147. }
  148.  
  149. object newLink(key, value)
  150. object key, value;
  151. {    object newObj;
  152.  
  153.     newObj = allocObject(3);
  154.     setClass(newObj, globalSymbol("Link"));
  155.     basicAtPut(newObj, 1, key);
  156.     basicAtPut(newObj, 2, value);
  157.     return newObj;
  158. }
  159.  
  160. object newMethod()
  161. {    object newObj;
  162.  
  163.     newObj = allocObject(methodSize);
  164.     setClass(newObj, globalSymbol("Method"));
  165.     return newObj;
  166. }
  167.  
  168. object newStString(value)
  169. char *value;
  170. {    object newObj;
  171.  
  172.     newObj = allocStr(value);
  173.     if (stringClass == nilobj)
  174.         stringClass = globalSymbol("String");
  175.     setClass(newObj, stringClass);
  176.     return(newObj);
  177. }
  178.  
  179. object newSymbol(str)
  180. char *str;
  181. {     object newObj;
  182.  
  183.     /* first see if it is already there */
  184.     newObj = globalKey(str);
  185.     if (newObj) 
  186.         return newObj;
  187.  
  188.     /* not found, must make */
  189.     newObj = allocStr(str);
  190.     if (symbolClass == nilobj)
  191.         symbolClass = globalSymbol("Symbol");
  192.     setClass(newObj, symbolClass);
  193.     nameTableInsert(symbols, strHash(str), newObj, nilobj);
  194.     return newObj;
  195. }
  196.