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

  1. /*
  2.     Little Smalltalk, version 3
  3.     Written by Tim Budd, Oregon State University, June 1988
  4.  
  5.     routines used in reading in textual descriptions of classes
  6. */
  7.  
  8. # include <stdio.h>
  9. # include "env.h"
  10. # include "memory.h"
  11. # include "names.h"
  12. # include "lex.h"
  13. # ifdef STRING
  14. # include <string.h>
  15. # endif
  16. # ifdef STRINGS
  17. # include <strings.h>
  18. # endif
  19.  
  20. # define MethodTableSize 39
  21.  
  22. /*
  23.     the following are switch settings, with default values
  24. */
  25. boolean savetext = true;
  26.  
  27. /*
  28.     we read the input a line at a time, putting lines into the following
  29.     buffer.  In addition, all methods must also fit into this buffer.
  30. */
  31. # define TextBufferSize 1024
  32. static char textBuffer[TextBufferSize];
  33.  
  34. /*
  35.     findClass gets a class object,
  36.     either by finding it already or making it
  37.     in addition, it makes sure it has a size, by setting
  38.     the size to zero if it is nil.
  39. */
  40. static object findClass(name)
  41. char *name;
  42. {    object newobj;
  43.  
  44.     newobj = globalSymbol(name);
  45.     if (newobj == nilobj)
  46.         newobj = newClass(name);
  47.     if (basicAt(newobj, sizeInClass) == nilobj) {
  48.         basicAtPut(newobj, sizeInClass, newInteger(0));
  49.         }
  50.     return newobj;
  51. }
  52.  
  53. /*
  54.     readDeclaration reads a declaration of a class
  55. */
  56. static readClassDeclaration()
  57. {    object classObj, super, vars;
  58.     int i, size, instanceTop;
  59.     object instanceVariables[15];
  60.  
  61.     if (nextToken() != nameconst)
  62.         sysError("bad file format","no name in declaration");
  63.     classObj = findClass(tokenString);
  64.     size = 0;
  65.     if (nextToken() == nameconst) {    /* read superclass name */
  66.         super = findClass(tokenString);
  67.         basicAtPut(classObj, superClassInClass, super);
  68.         size = intValue(basicAt(super, sizeInClass));
  69.         ignore nextToken();
  70.         }
  71.     if (token == nameconst) {        /* read instance var names */
  72.         instanceTop = 0;
  73.         while (token == nameconst) {
  74.             instanceVariables[instanceTop++] = newSymbol(tokenString);
  75.             size++;
  76.             ignore nextToken();
  77.             }
  78.         vars = newArray(instanceTop);
  79.         for (i = 0; i < instanceTop; i++) {
  80.             basicAtPut(vars, i+1, instanceVariables[i]);
  81.             }
  82.         basicAtPut(classObj, variablesInClass, vars);
  83.         }
  84.     basicAtPut(classObj, sizeInClass, newInteger(size));
  85. }
  86.  
  87. /*
  88.     readClass reads a class method description
  89. */
  90. static readMethods(fd, printit)
  91. FILE *fd;
  92. boolean printit;
  93. {    object classObj, methTable, theMethod, selector;
  94. # define LINEBUFFERSIZE 512
  95.     char *cp, *eoftest, lineBuffer[LINEBUFFERSIZE];
  96.  
  97.     if (nextToken() != nameconst)
  98.         sysError("missing name","following Method keyword");
  99.     classObj = findClass(tokenString);
  100.     setInstanceVariables(classObj);
  101.     if (printit)
  102.         cp = charPtr(basicAt(classObj, nameInClass));
  103.  
  104.     /* now find or create a method table */
  105.     methTable = basicAt(classObj, methodsInClass);
  106.     if (methTable == nilobj) {    /* must make */
  107.         methTable = newDictionary(MethodTableSize);
  108.         basicAtPut(classObj, methodsInClass, methTable);
  109.         }
  110.  
  111.     /* now go read the methods */
  112.     do {
  113.         if (lineBuffer[0] == '|')    /* get any left over text */
  114.             strcpy(textBuffer,&lineBuffer[1]);
  115.         else
  116.             textBuffer[0] = '\0';
  117.         while((eoftest = fgets(lineBuffer, LINEBUFFERSIZE, fd)) != NULL) {
  118.             if ((lineBuffer[0] == '|') || (lineBuffer[0] == ']'))
  119.                 break;
  120.             ignore strcat(textBuffer, lineBuffer);
  121.             }
  122.         if (eoftest == NULL) {
  123.             sysError("unexpected end of file","while reading method");
  124.             break;
  125.             }
  126.  
  127.         /* now we have a method */
  128.         theMethod = newMethod();
  129.         if (parse(theMethod, textBuffer, savetext)) {
  130.             selector = basicAt(theMethod, messageInMethod);
  131.             basicAtPut(theMethod, methodClassInMethod, classObj);
  132.             if (printit)
  133.                 dspMethod(cp, charPtr(selector));
  134.             nameTableInsert(methTable, (int) selector, 
  135.                 selector, theMethod);
  136.             }
  137.         else {
  138.             /* get rid of unwanted method */
  139.             incr(theMethod);
  140.             decr(theMethod);
  141.             givepause();
  142.             }
  143.         
  144.     } while (lineBuffer[0] != ']');
  145. }
  146.  
  147. /*
  148.     fileIn reads in a module definition
  149. */
  150. fileIn(fd, printit)
  151. FILE *fd;
  152. boolean printit;
  153. {
  154.     while(fgets(textBuffer, TextBufferSize, fd) != NULL) {
  155.         lexinit(textBuffer);
  156.         if (token == inputend)
  157.             ; /* do nothing, get next line */
  158.         else if ((token == binary) && streq(tokenString, "*"))
  159.             ; /* do nothing, its a comment */
  160.         else if ((token == nameconst) && streq(tokenString, "Class"))
  161.             readClassDeclaration();
  162.         else if ((token == nameconst) && streq(tokenString,"Methods"))
  163.             readMethods(fd, printit);
  164.         else 
  165.             sysError("unrecognized line", textBuffer);
  166.         }
  167. }
  168.