home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 018.lha / sources / courier.c < prev    next >
C/C++ Source or Header  |  1986-10-19  |  5KB  |  170 lines

  1. /*
  2.     Little Smalltalk
  3.         courier - message passing interface
  4.  
  5.         timothy a. budd 10/84
  6. */
  7. /*
  8.     The source code for the Little Smalltalk System may be freely
  9.     copied provided that the source of all files is acknowledged
  10.     and that this condition is copied with each file.
  11.  
  12.     The Little Smalltalk System is distributed without responsibility
  13.     for the performance of the program and without any guarantee of
  14.     maintenance.
  15.  
  16.     All questions concerning Little Smalltalk should be addressed to:
  17.  
  18.         Professor Tim Budd
  19.         Department of Computer Science
  20.         The University of Arizona
  21.         Tucson, Arizona
  22.         85721
  23.         USA
  24. */
  25. # include <stdio.h>
  26. # include "object.h"
  27. # include "interp.h"
  28. # include "string.h"
  29. # include "symbol.h"
  30. # include "primitive.h"
  31. # define streq(x,y) (strcmp(x,y) == 0)
  32.  
  33. /* send_mess - find the method needed to respond to a message, create the
  34.     proper context and interpreter for executing the method */
  35. send_mess(sender, receiver, message, args, numargs)
  36. interpreter *sender;
  37. object *receiver, **args;
  38. char *message;
  39. int numargs;
  40. {    object *robject, *method;
  41.     register object *message_array;
  42.     object *context, *fnd_super(), *fnd_class();
  43.     class  *objclass;
  44.     interpreter *anInterpreter;
  45.     int    i, maxc;
  46.  
  47.     for (robject = receiver; robject; ) {
  48.         if (robject == (object *) 0) break;
  49.         if (is_bltin(robject))
  50.             objclass = (class *) fnd_class(robject);
  51.         else
  52.             objclass = robject->class;
  53.         if ((objclass == (class *) 0) || ! is_class(objclass))  break;
  54.  
  55.         message_array = objclass->message_names;
  56.         for (i = 0; i < message_array->size; i++) {
  57.             if (symbol_value(message_array->inst_var[i]) ==
  58.                         message) {
  59.                 method = (objclass->methods)->inst_var[ i ];
  60.                 goto do_cmd;
  61.                 }
  62.             }
  63.         if (is_bltin(robject))
  64.             robject = fnd_super(robject);
  65.         else
  66.             robject = robject->super_obj;
  67.         }
  68.  
  69. /* if we reach this point then no method has been found matching message */
  70.     sassign(robject, new_obj((class *) 0, 2, 0));
  71.     sassign(robject->inst_var[0], receiver);
  72.     sassign(robject->inst_var[1], new_sym(message));
  73.     primitive(NORESPONDERROR, 2, &(robject->inst_var[0]));
  74.     obj_dec(robject);
  75.     /* generate a message passing trace */
  76.     backtrace(sender);
  77.     /* return nil by default */
  78.     if (is_interpreter(sender))
  79.         push_object(sender, o_nil);
  80.     link_to_process(sender);
  81.     goto clean_up;
  82.  
  83. /* do an interpreted method */
  84. /* make a context and fill it in, make an interpeter and link it into
  85. process queue */
  86. do_cmd:
  87.     maxc = objclass->context_size;
  88.     sassign(context, new_obj((class *)0, maxc, 0));
  89.     for (i = 0; i <= numargs; i++)
  90.         sassign(context->inst_var[i], args[i]);
  91.     for ( ; i < maxc ; i++ )
  92.         sassign(context->inst_var[i], o_nil);
  93.     anInterpreter = cr_interpreter(sender, robject, method->inst_var[1],
  94.         method->inst_var[0], context);
  95.     link_to_process(anInterpreter);
  96.     obj_dec(context);
  97.     goto clean_up;
  98.  
  99. /* clean up after yourself */
  100. clean_up:
  101.     return;
  102. }
  103.  
  104. /* responds_to - see if a class responds to a message */
  105. int responds_to(message, aClass)
  106. char *message;
  107. class *aClass;
  108. {    object *message_names;
  109.     int i;
  110.  
  111.     message_names = aClass->message_names;
  112.     for (i = 0; i < message_names->size; i++)
  113.         if (streq(symbol_value(message_names->inst_var[i]),
  114.                 message))
  115.             return(1);
  116.     return(0);
  117. }
  118.  
  119. /* backtrace - generate a backwards message passing trace */
  120. static backtrace(current)
  121. interpreter *current;
  122. {
  123.     while (is_interpreter(current->sender) &&
  124.             ! is_driver(current->sender)) {
  125.         fnd_message(current->receiver, current->bytecodes);
  126.         current = current->sender;
  127.         }
  128. }
  129.  
  130. /* fnd_message - find the message associated with an interpreter */
  131. static fnd_message(receiver, bytecodes)
  132. object *receiver, *bytecodes;
  133. {    int i;
  134.     class *oclass;
  135.     object *messar, *temp;
  136.     char buffer[100];
  137.  
  138.     oclass = (class *) fnd_class(receiver);
  139.     if (! is_class(oclass)) return;
  140.  
  141.     messar = oclass->methods;
  142.     for (i = 0; i < messar->size; i++) {
  143.         if ((messar->inst_var[i])->inst_var[0] == bytecodes) {
  144.             sprintf(buffer,"%s: backtrace. message  %s",
  145.                 symbol_value(oclass->class_name),
  146.                 symbol_value(
  147.                     (oclass->message_names)->inst_var[i]));
  148.             sassign(temp, new_str(buffer));
  149.             primitive(ERRPRINT, 1, &temp);
  150.             obj_dec(temp);
  151.             return;
  152.             }
  153.         }
  154.     cant_happen(24);
  155. }
  156.  
  157. /* prnt_messages - print all the messages a class responds to.
  158.     needed because the messages names array for some of the classes is
  159.     created before ArrayedCollection, and thus some do not respond to
  160.     do: */
  161. prnt_messages(aClass)
  162. class *aClass;
  163. {    object *message_names;
  164.     int i;
  165.  
  166.     message_names = aClass->message_names;
  167.     for (i = 0; i < message_names->size; i++)
  168.         primitive(SYMPRINT, 1, &message_names->inst_var[i]);
  169. }
  170.