home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 018.lha / sources / main.c < prev    next >
C/C++ Source or Header  |  1986-10-19  |  8KB  |  342 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.         The University of Arizona
  31.         Tucson, Arizona
  32.         85721
  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. static object *null_object;    /* a totally classless object */
  48. static char filebase[80];    /* base for forming temp file names */
  49.  
  50. extern int n_incs, n_decs, n_mallocs;    /* counters */
  51.  
  52. extern int ca_block, ca_barray, ca_class, ca_terp, ca_int, ca_float;
  53. extern int ca_obj, ca_str, ca_sym, ca_wal, ca_cdict;
  54. extern int ca_cobj[];
  55. extern int btabletop, wtop;    /* more counters */
  56.  
  57. # ifdef INLINE
  58. object *_dx;        /* object pointer used for decrementing */
  59. # endif
  60.  
  61. int silence = 0;        /* 1 if silence is desired on output */
  62. int noload = 0;         /* 1 if no loading of standard prelude is desired */
  63. int debug = 0;        /* debug flag, set by a primitive call */
  64. int fastload = 0;    /* 1 if doing a fast load of saved image */
  65. int lexprnt = 0;    /* 1 if printing during lex is desired (for debug) */
  66. int prallocs = 0;    /* 1 if printing final allocation figures is wanted */
  67. int started = 0;    /* 1 if we have started reading user commands */
  68. int prntcmd = 1;    /* 1 or 2 and commands will be printed as evaled */
  69.  
  70. /* pseudo-variables */
  71. object *o_acollection;        /* arrayed collection (used internally) */
  72. object *o_drive;        /* driver interpreter */
  73. object *o_empty;        /* the empty array (used during initial) */
  74. object *o_false;        /* value for pseudo variable false */
  75. object *o_magnitude;        /* instance of class Magnitude */
  76. object *o_nil;            /* value for pseudo variable nil */
  77. object *o_number;        /* instance of class Number */
  78. object *o_object;        /* instance of class Object */
  79. object *o_tab;            /* string with tab only */
  80. object *o_true;            /* value of pseudo variable true */
  81. object *o_smalltalk;        /* value of pseudo variable smalltalk */
  82.  
  83. /* classes to be initialized */
  84. extern class *Array;
  85. extern class *ArrayedCollection;
  86.  
  87. /* input stack */
  88. extern FILE *fdstack[];
  89. extern int fdtop;
  90.  
  91. /* main - main driver */
  92. main(argc, argv)
  93. int argc;
  94. char **argv;
  95. {    int i;
  96.     class *null_class();
  97.     object *tempobj;
  98.     FILE *sfd;
  99.  
  100. # ifdef FASTDEFAULT
  101.     fastload = 1;
  102. # endif
  103. # ifndef FASTDEFAULT
  104.     fastload = 0;
  105. # endif
  106.  
  107.     /* first check for flags */
  108.     for (i = 1; i < argc; i++)
  109.         if (argv[i][0] == '-')
  110.             switch(argv[i][1]) {
  111.                 case 'f': fastload = 1; break;
  112.                 case 'l':         /* fall through */
  113.                 case 'n': noload = 1; /* fall through */
  114.                 case 'm': fastload = 0; break;
  115.                 case 'z': lexprnt = 1; break;
  116.             }
  117.  
  118.     if (fastload) {
  119.         dofast();
  120.         }
  121.     else {            /* gotta do it the hard way */
  122.         strcpy(filebase, TEMPFILE);
  123.         mktemp(filebase);
  124.  
  125.         byte_init();
  126.         class_init();
  127.         cdic_init();
  128.         int_init();
  129.         str_init();
  130.         sym_init();
  131.         init_objs();
  132.  
  133.         null_object = new_obj((class *) 0, 0, 0);
  134.  
  135.         sassign(o_object, null_object);
  136.         /* true is given a different object from others , so comparisons
  137.                     work correctly */
  138.         sassign(o_true, new_obj((class *) 0, 0, 0));
  139.         sassign(o_false, null_object);
  140.         sassign(o_nil, null_object);
  141.         sassign(o_number, null_object);
  142.         sassign(o_magnitude, null_object);
  143.         sassign(o_empty, null_object);
  144.         sassign(o_smalltalk, null_object);
  145.         sassign(o_acollection, null_object);
  146.  
  147.         sassign(Array, null_class("Array"));
  148.         sassign(ArrayedCollection, null_class("ArrayedCollection"));
  149.  
  150.         drv_init();    /* initialize the driver */
  151.         sassign(o_drive, (object *) cr_interpreter((interpreter *) 0,
  152.             null_object, null_object, null_object, null_object));
  153.         init_process((interpreter *) o_drive);
  154.  
  155.         /* now read in standard prelude */
  156.         if (! noload) {
  157.             sfd = fopen(PRELUDE, "r");
  158.             if (sfd == NULL) cant_happen(20);
  159.             set_file(sfd);
  160.             start_execution();
  161.             fclose(sfd);
  162.             }
  163.  
  164.         /* then set lexer up to read stdin */
  165.         set_file(stdin);
  166.         sassign(o_tab, new_str("\t"));
  167.  
  168. # ifdef CURSES
  169.         /* finally initialize the curses window package */
  170.         initscr();
  171. # endif
  172. # ifdef PLOT3
  173.         /* initialize the plotting device */
  174.         openpl();
  175. # endif
  176.         }
  177.  
  178.     /* announce that we're ready for action */
  179.     sassign(tempobj, new_sym("Little Smalltalk"));
  180.     primitive(SYMPRINT, 1, &tempobj);
  181.     obj_dec(tempobj);
  182.     started = 1;
  183.  
  184.     /* now read in the command line files */
  185.     user_read(argc, argv);
  186.  
  187.     start_execution();
  188.  
  189.     /* print out one last newline - to move everything out of output
  190.     queue */
  191.     sassign(tempobj, new_sym("\n"));
  192.     primitive(SYMPRINT, 1, &tempobj);
  193.     obj_dec(tempobj);
  194.  
  195.     /* now free things up, hopefully keeping ref counts straight */
  196.  
  197.     drv_free();
  198.  
  199.     flush_processes();
  200.  
  201.     free_low_nums();
  202.  
  203.     obj_dec((object *) Array);
  204.     obj_dec((object *) ArrayedCollection);
  205.  
  206.     free_all_classes();
  207.     
  208.     obj_dec(o_tab);
  209.     obj_dec(o_drive);
  210.     obj_dec(o_magnitude);
  211.     obj_dec(o_number);
  212.     obj_dec(o_nil);
  213.     obj_dec(o_false);
  214.     obj_dec(o_true);
  215.     obj_dec(o_object);
  216.     obj_dec(o_empty);
  217.     obj_dec(o_smalltalk);
  218.     obj_dec(o_acollection);
  219.  
  220.     if (! silence)
  221.         fprintf(stderr,"incs %u decs %u difference %d allocs %d\n", 
  222.             n_incs, n_decs, n_incs - n_decs, n_mallocs);
  223.     if (prallocs) {
  224.         fprintf(stderr,"blocks allocated %d\n", ca_block);
  225.         fprintf(stderr,"bytearrays allocated %d\n", ca_barray);
  226.         fprintf(stderr,"classes allocated %d\n", ca_class);
  227.         fprintf(stderr,"interpreters allocated %d\n", ca_terp);
  228.         fprintf(stderr,"ints allocated %d\n", ca_int);
  229.         fprintf(stderr,"floats allocated %d\n", ca_float);
  230.         fprintf(stderr,"strings allocated %d\n", ca_str);
  231.         fprintf(stderr,"symbols allocated %d\n", ca_sym);
  232.         fprintf(stderr,"class entryies %d\n", ca_cdict);
  233.         fprintf(stderr,"wallocs %d\n", ca_wal);
  234.         fprintf(stderr,"wtop %d\n", wtop);
  235.         fprintf(stderr,"byte table top %d\n", btabletop);
  236.         fprintf(stderr,"smalltalk objects allocated %d\n", ca_obj);
  237.         for (i = 0; i < 5; i++)
  238.             fprintf(stderr,"size %d objects %d\n", i, ca_cobj[i]);
  239.     }
  240.     clean_files();
  241.  
  242. # ifdef PLOT3
  243.     closepl();
  244. # endif
  245. # ifdef CURSES
  246.     endwin();
  247. # endif
  248.  
  249.     exit(0);    /* say good by gracie */
  250. }
  251.  
  252. /* dofast - do a fast load of the standard prelude */
  253. static dofast() {
  254.     char buffer[100];
  255.  
  256.     sprintf(buffer,")l %s\n", FAST);
  257.     dolexcommand(buffer);
  258. }
  259.  
  260. /* null_class - create a null class for bootstrapping purposes */
  261. static class *null_class(name)
  262. char *name;
  263. {    class *new, *new_class();
  264.  
  265.     new = new_class();
  266.     assign(new->class_name, new_sym(name));
  267.     enter_class(name, (object *) new);
  268.     return(new);
  269. }
  270.  
  271. /* user_read - read the user command line arguments */
  272. static user_read(argc, argv)
  273. int argc;
  274. char **argv;
  275. {    int i, count;
  276.     char c, buffer[100];
  277.     char name[100];
  278.     FILE *fd = 0;
  279.  
  280.     gettemp(name);
  281.     count = 0;
  282.     fd = fopen(name, "w");
  283.     if (fd == NULL)
  284.         cant_happen(22);
  285.     for (i = 1; i < argc; i++)
  286.         if (argv[i][0] == '-') {
  287.             switch(argv[i][1]) {
  288.                 case 'a':
  289.                     prallocs = 1; break;
  290.                 case 'g': case 'l': case 'r':
  291.                     c = argv[i][1];
  292.                     sprintf(buffer,")%c %s\n", 
  293.                         c, argv[++i]);
  294.                     count++;
  295.                     fputs(buffer, fd);
  296.                     break;
  297.                 case 'd':
  298.                     prntcmd = argv[i][1] - '0';
  299.                     break;
  300.                 case 's':
  301.                     silence = 1;
  302.                     break;
  303.                 }
  304.             }
  305.         else {
  306.             sprintf(buffer,")i %s\n", argv[i]);
  307.             count++;
  308.             fputs(buffer, fd);
  309.             }
  310.     fclose(fd);
  311.     if (count) {
  312.         fd = fopen(name, "r");
  313.         if (fd == NULL)
  314.             cant_happen(22);
  315.         set_file(fd);
  316.         }
  317. }
  318.  
  319. /* gettemp makes a temp file name that can be deleted when finished */
  320. static char c = 'a';
  321. gettemp(buffer)
  322. char *buffer;
  323. {
  324.     sprintf(buffer,"%s%c", filebase, c++);
  325.     if (c > 'z') c = 'a';    /* wrap around forever */
  326. }
  327.  
  328. /* clean_files - delete all temp files created */
  329. static clean_files()
  330. {
  331.     char buffer[100];
  332.  
  333. # ifndef NOSYSTEM
  334.     sprintf(buffer,/*"rm -f %s*"*/ "delete %s#?", filebase);
  335.     system(buffer);
  336. # endif
  337. }
  338.  
  339. system(s) char *s; {
  340.     return(! Execute(s, 0L, 0L));
  341. }
  342.