home *** CD-ROM | disk | FTP | other *** search
/ Really Useful CD 1 / ReallyUsefulCD1.iso / extras / languages / smalltalk / _smalltalk / sources / c / main < prev    next >
Encoding:
Text File  |  1989-11-20  |  11.8 KB  |  382 lines

  1. /*
  2.      Little Smalltalk -
  3.           main driver
  4.  
  5.           timothy a. budd
  6.  
  7. 1.   initializes various smalltalk constants and classes with
  8.      legitimate values.  these values, however, will for the most part
  9.      be overridden when the standard prelude is read in.
  10.  
  11. 2.   reads in the standard prelude, plus any additional files listed
  12.      on the command line.
  13.  
  14. 3.   places the driver reading stdin on the process queue and starts
  15.      the process driver running.
  16. */
  17. /*
  18.      The source code for the Little Smalltalk System may be freely
  19.      copied provided that the source of all files is acknowledged
  20.      and that this condition is copied with each file.
  21.  
  22.      The Little Smalltalk System is distributed without responsibility
  23.      for the performance of the program and without any guarantee of
  24.      maintenance.
  25.  
  26.      All questions concerning Little Smalltalk should be addressed to:
  27.  
  28.           Professor Tim Budd
  29.           Department of Computer Science
  30.           Oregon State University
  31.           Corvallis, Oregon
  32.           97331
  33.           USA
  34. */
  35.  
  36. int version = 2; /* a Kludge to get us the start of the data segment.
  37.                used to save and restore contexts */
  38.  
  39.  
  40. # include <stdio.h>
  41. # include "object.h"
  42. # include "string.h"
  43. # include "symbol.h"
  44. # include "interp.h"
  45. # include "primitive.h"
  46.  
  47. extern int ssystem();  /* TPR substitute a call to our system call */
  48.  
  49. static object *null_object;   /* a totally classless object */static char filebase[80];   /* base for forming temp file names */
  50.  
  51. extern int n_incs, n_decs, n_mallocs;   /* counters */
  52. extern int opcount[], ohcount, spcount[];
  53.  
  54. extern int ca_block, ca_barray, ca_class, ca_terp, ca_int, ca_float;
  55. extern int ca_obj, ca_str, ca_sym, ca_wal, ca_cdict;
  56. extern int ca_cobj[];
  57. extern int btabletop, wtop;   /* more counters */
  58.  
  59. # ifdef INLINE
  60. object *_dx;        /* object pointer used for decrementing */
  61. # endif
  62.  
  63. int silence = 0;         /* 1 if silence is desired on output */
  64. int noload = 0;          /* 1 if no loading of standard prelude is desired */
  65. int debug = 0;      /* debug flag, set by a primitive call */
  66. int fastload = 0;   /* 1 if doing a fast load of saved image */
  67. int lexprnt = 0;    /* 1 if printing during lex is desired (for debug) */
  68. int prallocs = 0;   /* 1 if printing final allocation figures is wanted */
  69. int started = 0;    /* 1 if we have started reading user commands */
  70. int prntcmd = 1;    /* 1 or 2 and commands will be printed as evaled */
  71.  
  72. /* pseudo-variables */
  73. object *o_acollection;        /* arrayed collection (used internally) */
  74. object *o_drive;         /* driver interpreter */
  75. object *o_empty;         /* the empty array (used during initial) */
  76. object *o_false;         /* value for pseudo variable false */
  77. object *o_magnitude;          /* instance of class Magnitude */
  78. object *o_nil;           /* value for pseudo variable nil */
  79. object *o_number;        /* instance of class Number */
  80. object *o_object;        /* instance of class Object */
  81. object *o_tab;           /* string with tab only */
  82. object *o_true;               /* value of pseudo variable true */
  83. object *o_smalltalk;          /* value of pseudo variable smalltalk */
  84.  
  85. /* classes to be initialized */
  86. extern class *Array;
  87. extern class *ArrayedCollection;
  88.  
  89. /* input stack */
  90. extern FILE *fdstack[];
  91. extern int fdtop;
  92.  
  93. /* Forward declarations... [SLD] */
  94.  
  95. #ifdef ARM
  96. static int dofast();
  97. static int user_read();
  98. static int clean_files();
  99. #endif
  100.  
  101. /* main - main driver */
  102. main(argc, argv)
  103. int argc;
  104. char **argv;
  105. {    int i;
  106.      class *null_class();
  107.      object *tempobj;
  108.      FILE *sfd;
  109.  
  110. #ifdef ARM 
  111.   /* RiscOS startup - MA */
  112.   int dummy;
  113.  
  114.   init_text();
  115. #endif
  116.  
  117. # ifdef FASTDEFAULT
  118.      fastload = 1;
  119. # endif
  120. # ifndef FASTDEFAULT
  121.      fastload = 0;
  122. # endif
  123.  
  124.      /* first check for flags */
  125.      for (i = 1; i < argc; i++)
  126.           if (argv[i][0] == '-')
  127.                switch(argv[i][1]) {
  128.                     case 'f': fastload = 1; break;
  129.                     case 'l':           /* fall through */
  130.                     case 'n': noload = 1; /* fall through */
  131.                     case 'm': fastload = 0; break;
  132.                     case 'z': lexprnt = 1; break;
  133.                }
  134.  
  135.      if (fastload) {
  136.           dofast();
  137.           }
  138.      else {              /* gotta do it the hard way */
  139.           strcpy(filebase, TEMPFILE);
  140. # ifndef ARM
  141.           mktemp(filebase);
  142. # endif
  143.  
  144.           byte_init();
  145.           class_init();
  146.           cdic_init();
  147.           int_init();
  148.           str_init();
  149.           sym_init();
  150.           init_objs();
  151.  
  152.           null_object = new_obj((class *) 0, 0, 0);
  153.  
  154.           sassign(o_object, null_object);
  155.           /* true is given a different object from others , so comparisons
  156.                          work correctly */
  157.           sassign(o_true, new_obj((class *) 0, 0, 0));
  158.           sassign(o_false, null_object);
  159.           sassign(o_nil, null_object);
  160.           sassign(o_number, null_object);
  161.           sassign(o_magnitude, null_object);
  162.           sassign(o_empty, null_object);
  163.           sassign(o_smalltalk, null_object);
  164.           sassign(o_acollection, null_object);
  165.  
  166.           sassign(Array, null_class("Array"));
  167.           sassign(ArrayedCollection, null_class("ArrayedCollection"));
  168.  
  169.           drv_init();    /* initialize the driver */
  170.           sassign(o_drive, (object *) cr_interpreter((interpreter *) 0,
  171.                null_object, null_object, null_object, null_object));
  172.           init_process((interpreter *) o_drive);
  173.  
  174.           /* now read in standard prelude */
  175.           if (! noload) {
  176.                sfd = fopen(PRELUDE, "r");
  177.                if (sfd == NULL) cant_happen(20);
  178.                set_file(sfd);
  179.                start_execution();
  180.                fclose(sfd);
  181.                }
  182.  
  183.           /* then set lexer up to read stdin */
  184.           set_file(stdin);
  185.           sassign(o_tab, new_str("\t"));
  186.  
  187. # ifdef CURSES
  188.           /* finally initialize the curses window package */
  189.           initscr();
  190. # endif
  191. # ifdef PLOT3
  192.           /* initialize the plotting device */
  193.           openpl();
  194. # endif
  195.           }
  196.  
  197.      /* announce that we're ready for action */
  198.      sassign(tempobj, new_sym("Little Smalltalk"));
  199.      primitive(SYMPRINT, 1, &tempobj);
  200.      obj_dec(tempobj);
  201.      started = 1;
  202.  
  203.      /* now read in the command line files */
  204.      user_read(argc, argv);
  205.  
  206.      start_execution();
  207.  
  208.      /* print out one last newline - to move everything out of output
  209.      queue */
  210.      sassign(tempobj, new_sym("\n"));
  211.      primitive(SYMPRINT, 1, &tempobj);
  212.      obj_dec(tempobj);
  213.  
  214.      /* now free things up, hopefully keeping ref counts straight */
  215.  
  216.      drv_free();
  217.  
  218.      flush_processes();
  219.  
  220.      free_low_nums();
  221.  
  222.      obj_dec((object *) Array);
  223.      obj_dec((object *) ArrayedCollection);
  224.  
  225.      free_all_classes();
  226.      
  227.      obj_dec(o_tab);
  228.      obj_dec(o_drive);
  229.      obj_dec(o_magnitude);
  230.      obj_dec(o_number);
  231.      obj_dec(o_nil);
  232.      obj_dec(o_false);
  233.      obj_dec(o_true);
  234.      obj_dec(o_object);
  235.      obj_dec(o_empty);
  236.      obj_dec(o_smalltalk);
  237.      obj_dec(o_acollection);
  238.  
  239.      if (! silence)
  240.           ARMX(tprintf("incs %u decs %u difference %d allocs %d\n", 
  241.                n_incs, n_decs, n_incs - n_decs, n_mallocs), fprintf(stderr, "incs %u decs %u difference %d allocs %d\n", 
  242.                n_incs, n_decs, n_incs - n_decs, n_mallocs));
  243.      ohcount = 0;
  244.      for (i = 0; i < 16; i++)
  245.           ohcount += opcount[i];
  246.      ARMX(tprintf("opcount %d\n", ohcount), fprintf(stderr, "opcount %d\n", ohcount));
  247.           /*ARMX(tprintf("opcode [%d] counts %d\n", i, opcount[i]), fprintf(stderr, "opcode [%d] counts %d\n", i, opcount[i]));*/
  248.      /*ARMX(tprintf("ohcount %d\n", ohcount), fprintf(stderr, "ohcount %d\n", ohcount));
  249.      for (i = 0; i < 16; i++)
  250.           ARMX(tprintf("sp count %d %d\n", i , spcount[i]), fprintf(stderr, "sp count %d %d\n", i , spcount[i]));*/
  251.      if (prallocs) {
  252.           ARMX(tprintf("blocks allocated %d\n", ca_block), fprintf(stderr, "blocks allocated %d\n", ca_block));
  253.           ARMX(tprintf("bytearrays allocated %d\n", ca_barray), fprintf(stderr, "bytearrays allocated %d\n", ca_barray));
  254.           ARMX(tprintf("classes allocated %d\n", ca_class), fprintf(stderr, "classes allocated %d\n", ca_class));
  255.           ARMX(tprintf("interpreters allocated %d\n", ca_terp), fprintf(stderr, "interpreters allocated %d\n", ca_terp));
  256.           ARMX(tprintf("ints allocated %d\n", ca_int), fprintf(stderr, "ints allocated %d\n", ca_int));
  257.           ARMX(tprintf("floats allocated %d\n", ca_float), fprintf(stderr, "floats allocated %d\n", ca_float));
  258.           ARMX(tprintf("strings allocated %d\n", ca_str), fprintf(stderr, "strings allocated %d\n", ca_str));
  259.           ARMX(tprintf("symbols allocated %d\n", ca_sym), fprintf(stderr, "symbols allocated %d\n", ca_sym));
  260.           ARMX(tprintf("class entryies %d\n", ca_cdict), fprintf(stderr, "class entryies %d\n", ca_cdict));
  261.           ARMX(tprintf("wallocs %d\n", ca_wal), fprintf(stderr, "wallocs %d\n", ca_wal));
  262.           ARMX(tprintf("wtop %d\n", wtop), fprintf(stderr, "wtop %d\n", wtop));
  263.           ARMX(tprintf("byte table top %d\n", btabletop), fprintf(stderr, "byte table top %d\n", btabletop));
  264.           ARMX(tprintf("smalltalk objects allocated %d\n", ca_obj), fprintf(stderr, "smalltalk objects allocated %d\n", ca_obj));
  265.           for (i = 0; i < 5; i++)
  266.                ARMX(tprintf("size %d objects %d\n", i, ca_cobj[i]), fprintf(stderr, "size %d objects %d\n", i, ca_cobj[i]));
  267.      }
  268.      clean_files();
  269.  
  270. # ifdef PLOT3
  271.      closepl();
  272. # endif
  273. # ifdef CURSES
  274.      endwin();
  275. # endif
  276.  
  277.      exit(0);  /* say good by gracie */
  278. }
  279.  
  280. /* dofast - do a fast load of the standard prelude */
  281. static dofast() {
  282.      char buffer[100];
  283.  
  284.      sprintf(buffer,")l %s\n", FAST);
  285.      dolexcommand(buffer);
  286. }
  287.  
  288. /* null_class - create a null class for bootstrapping purposes */
  289. static class *null_class(name)
  290. char *name;
  291. {    class *new, *new_class();
  292.  
  293.      new = new_class();
  294.      assign(new->class_name, new_sym(name));
  295.      enter_class(name, (object *) new);
  296.      return(new);
  297. }
  298.  
  299. /* user_read - read the user command line arguments */
  300. static user_read(argc, argv)
  301. int argc;
  302. char **argv;
  303. {    int i, count;
  304.      char c, buffer[100];
  305.      char name[100];
  306.      FILE *fd = 0;
  307.  
  308.      gettemp(name);
  309.      count = 0;
  310.      fd = fopen(name, "w");
  311.      if (fd == NULL)
  312.           cant_happen(22);
  313.      for (i = 1; i < argc; i++)
  314.           if (argv[i][0] == '-') {
  315.                switch(argv[i][1]) {
  316.                     case 'a':
  317.                          prallocs = 1; break;
  318.                     case 'g': case 'l': case 'r':
  319.                          c = argv[i][1];
  320.                          sprintf(buffer,")%c %s\n", 
  321.                               c, argv[++i]);
  322.                          count++;
  323.                          fputs(buffer, fd);
  324.                          break;
  325.                     case 'd':
  326.                          prntcmd = argv[i][1] - '0';
  327.                          break;
  328.                     case 's':
  329.                          silence = 1;
  330.                          break;
  331.                     }
  332.                }
  333.           else {
  334.                sprintf(buffer,")i %s\n", argv[i]);
  335.                count++;
  336.                fputs(buffer, fd);
  337.                }
  338.      fclose(fd);
  339.      if (count) {
  340.           fd = fopen(name, "r");
  341.           if (fd == NULL)
  342.                cant_happen(22);
  343.           set_file(fd);
  344.           }
  345. }
  346.  
  347. /* gettemp makes a temp file name that can be deleted when finished */
  348. static char c = 'a';
  349. gettemp(buffer)
  350. char *buffer;
  351. {
  352. #ifdef IBMPC
  353.      strcpy(buffer,filebase);
  354.      buffer[strlen(buffer)-1]=c;
  355. #else
  356.           sprintf(buffer,"%s%c", filebase, c++);
  357. #endif
  358.      if (c > 'z') c = 'a';    /* wrap around forever */
  359. }
  360.  
  361. /* clean_files - delete all temp files created */
  362. static clean_files()
  363. {
  364.      char buffer[100];
  365. # ifdef ARM
  366.         sprintf(buffer,"*wipe %s* F~C~V",filebase); /* non-verbose - MA */
  367.         ssystem(buffer);  /* CP system(buffer) */
  368. #else
  369.  
  370. # ifdef IBMPC
  371.      sprintf(buffer,"del \\LST\\TMP\\ST??????");
  372.      system(buffer);
  373. # else
  374. # ifndef NOSYSTEM
  375.      sprintf(buffer,"rm -f %s*", filebase);
  376.      system(buffer);
  377. # endif
  378. # endif
  379. # endif
  380. }
  381.  
  382.