home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / umbscheme-2.12.lha / UMBScheme / src / object.h < prev    next >
C/C++ Source or Header  |  1993-11-29  |  27KB  |  970 lines

  1. /* object.h -- UMB Scheme, object interface.
  2.  
  3. UMB Scheme Interpreter                  $Revision: 2.12 $
  4. Copyright (C) 1988, 1991 William R Campbell
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. UMB Scheme was written by Bill Campbell with help from Karl Berry,
  21. Barbara Dixey, Ira Gerstein, Mary Glaser, Kathy Hargreaves, Bill McCabe,
  22. Long Nguyen, Susan Quina, Jeyashree Sivasubram, Bela Sohoni and Thang Quoc Tran.
  23.  
  24. For additional information about UMB Scheme, contact the author:
  25.  
  26.     Bill Campbell
  27.     Department of Mathematics and Computer Science
  28.     University of Massachusetts at Boston
  29.     Harbor Campus
  30.     Boston, MA 02125
  31.  
  32.     Telephone: 617-287-6449        Internet: bill@cs.umb.edu
  33.  
  34. */
  35.  
  36. /* Declarations for all the data objects and forms.
  37.  
  38. The Scheme interpreter operates on the C type `Object', meant to represent 
  39. a Scheme object; thus, Object is our central data abstraction.
  40.  
  41. The data type, Object, is the union of many sub-types which taken together
  42. can be used to denote the programs we are interpreting.  Most types 
  43. (e.g., Number_Object, String_Object, If_Object) correspond to objects and
  44. special forms that are discussed in the Scheme Reference Manual.  Others
  45. (e.g.,  Frame_Object) are necessary to our implementation.
  46.  
  47. There are six operations that are generally applicable to all objects:
  48.  
  49. 1.  Eval_Object        -- directs the explicit evaluator for this type.
  50. 2.  Compile_Object    -- compiles the result of a (read) to code.
  51. 3.  Display_Object    -- in human readable (eg "cat" => cat) form.
  52. 4.  Write_Object    -- in machine readable (eg "cat" => "cat") from.
  53. 5.  Show_Object        -- as part of the environment (in abbreviated form).
  54. 5.  GC_Object    -- for gc-ing objects of this type on the heap.
  55.  
  56. We can think of a sub-type as being defined by how these operations are
  57. particularly defined over that sub-type.  This is our basis for representing
  58. objects.  An object is represented by (a pointer to) a structure having one
  59. or more components, including (a pointer to) an operation vector of the five
  60. procedures that implement these operations.  An object's type is distinguished
  61. by its operation vector; e.g., Number_Objects are designated by (the pointer to)
  62. the vector of operations for evaluating, compiling, displaying, writing, and
  63. garbage collecting Number_Objects. */
  64.  
  65.  
  66. /* Here are the six common operations. */
  67.  
  68. typedef struct
  69. {
  70.     void (*Eval)();
  71.     void (*Compile)();
  72.     Integer (*Display)();
  73.     Integer (*Write)();
  74.     Integer (*Show)();
  75.     struct Object_Struct *(*GC)();
  76. } Op_Vector;
  77.  
  78. typedef Op_Vector *Scheme_Type;
  79.  
  80. /* The value register needs to be assigned a type t quite often. The call
  81. `Set_Result_Type(t)' will do it. */
  82.  
  83. #define Set_Result_Type(t) { Get_Type(Value_Register) = t;\
  84.                  Get_Type_Name(Value_Register) = ""  /* #t */; }
  85.  
  86. /* Every object has at least a `Common_Struct'. */
  87. typedef struct
  88. {
  89.     Scheme_Type Type;
  90.     String Type_Name;
  91. } Common_Struct;
  92.  
  93. /* And a way to access the common fields. */
  94.  
  95. #define Get_Type(o) ((o)->Common.Type)
  96. #define Get_Type_Name(o) ((o)->Common.Type_Name)
  97.  
  98. #define Eval_Object(o)        ((Get_Type(o)->Eval)(o))
  99. #define Compile_Object(o)    ((Get_Type(o)->Compile)())
  100. #define Display_Object(o,m)    ((Get_Type(o)->Display)(o,m))
  101. #define Write_Object(o,m)    ((Get_Type(o)->Write)(o,m))
  102. #define Show_Object(o,m)    ((Get_Type(o)->Show)(o,m))
  103. #define GC_Object(o)        ((Get_Type(o)->GC)(o))
  104.  
  105. /* Boolean; only the objects: The_True_Object and The_False_Object */
  106.  
  107. typedef struct
  108. {
  109.     int dummy;
  110. } Boolean_Struct;
  111.  
  112. Import    Op_Vector Boolean_Ops;
  113. Import    Integer    Boolean_Print();
  114. Import    struct    Object_Struct *Boolean_GC();
  115.  
  116. Import    Scheme_Type Boolean_Type;
  117.  
  118. Import    struct Object_Struct *The_True_Object, *The_False_Object;
  119.  
  120. #define Is_Boolean(o) (Get_Type(o) == Boolean_Type)
  121. #define Is_False(o) ((o) == The_False_Object)
  122.  
  123. #define Boolean_Size (sizeof(Boolean_Struct)+sizeof(Common_Struct))
  124.  
  125. /* Pair. */
  126.  
  127. typedef struct
  128. {
  129.     struct Object_Struct *Car;
  130.     struct Object_Struct *Cdr;
  131. } Pair_Struct;
  132.  
  133. Import    Op_Vector Pair_Ops;
  134. Import    Integer Pair_Display(), Pair_Write(), Pair_Show();
  135. struct    Object_Struct *Pair_GC();
  136.  
  137. Import    Scheme_Type Pair_Type;
  138.  
  139. #define Is_Pair(o) (Get_Type(o) == Pair_Type)
  140. #define Is_List(o) (Is_Pair(o) || Is_Empty_List(o))
  141.  
  142. #define Pair_Size (sizeof(Pair_Struct)+sizeof(Common_Struct))
  143.  
  144. #define Get_Pair_Car(o) ((o)->Specific.Pair.Car)
  145. #define Get_Pair_Cdr(o) ((o)->Specific.Pair.Cdr)
  146.  
  147. Import    void Make_Pair();
  148. Import    Integer Length();
  149. Import    struct Object_Struct *First();
  150. Import    struct Object_Struct *Second();
  151. Import    struct Object_Struct *Third();
  152. Import    struct Object_Struct *Fourth();
  153. Import    struct Object_Struct *Rest();
  154. Import    Boolean    Member();
  155.  
  156. /* Empty List */
  157.  
  158. typedef struct
  159. {
  160.     int dummy;
  161. } Empty_List_Struct;
  162.  
  163. Import    Op_Vector Empty_List_Ops;
  164. Import    Integer Empty_List_Print();
  165. Import    Integer Empty_List_Display();
  166. Import    Integer Empty_List_Write();
  167. struct    Object_Struct *Empty_List_GC();
  168.  
  169. Import    Scheme_Type Empty_List_Type;
  170.  
  171. Import    struct Object_Struct *Nil;
  172.  
  173. #define Is_Empty_List(o) (Get_Type(o) == Empty_List_Type)
  174. #define Empty_List_Size (sizeof(Empty_List_Struct)+sizeof(Common_Struct))
  175.  
  176. /* Symbol. */
  177. typedef struct
  178. {
  179.     struct    Object_Struct *Global_Binding;
  180.     struct    Object_Struct *How;
  181.     struct    Object_Struct *Property_List;
  182.     Boolean    User_Defined;
  183.     String    Name;
  184. } Symbol_Struct;
  185.  
  186. Import    Op_Vector Symbol_Ops;
  187. Import    Integer    Symbol_Print();
  188. struct    Object_Struct *Symbol_GC();
  189.  
  190. Import    Scheme_Type Symbol_Type;
  191.  
  192. Import    struct Object_Struct *QUOTE_Symbol, *DEFINE_Symbol,*SET_Symbol,
  193. *IF_Symbol, *MACRO_Symbol, *BEGIN_Symbol,
  194. *DELAY_Symbol, *LAMBDA_Symbol, *Special_Binding;
  195. Import    struct Object_Struct *The_Syntactic_Keyword, *The_Undefined_Symbol,
  196.                 *An_Argument;
  197.  
  198. #define Is_Symbol(o) (Get_Type(o) == Symbol_Type)
  199. #define Symbol_Size (sizeof(Symbol_Struct)+sizeof(Common_Struct))
  200.  
  201. #define Get_Global_Binding(o)    ((o)->Specific.Symbol.Global_Binding)
  202. #define    Get_Symbol_How(o)    ((o)->Specific.Symbol.How)
  203. #define    Get_Symbol_User_Defined(o) ((o)->Specific.Symbol.User_Defined)
  204. #define Get_Property_List(o)    ((o)->Specific.Symbol.Property_List)
  205. #define Get_Symbol_Name(o)    ((o)->Specific.Symbol.Name)
  206.  
  207. /* Is_Special_Symbol is not an lvalue. `Special_Binding' is simply
  208.    a dummy object. */
  209.  
  210. #define Is_Special_Symbol(o) (Get_Global_Binding(o)==Special_Binding)
  211.  
  212. Import    void Make_Symbol();
  213.  
  214. /* Number. */
  215.  
  216. typedef Integer Tower_Position;
  217.  
  218. #define FIXNUM_LEVEL     0
  219. #define BIGNUM_LEVEL     1
  220. #define RATIONAL_LEVEL    2
  221. #define    REAL_LEVEL    3
  222. #define    COMPLEX_LEVEL    4
  223.  
  224.  
  225. #define TOWER_LEVEL_COUNT 5
  226.  
  227. /* Fixnums */
  228.  
  229. #define Get_Number_Fixnum_Value(n) ((n)->Specific.Number.Specific.Fixnum_Value)
  230.  
  231.  
  232. /* Bignums */
  233.  
  234. typedef Short Number_Digit_Type;
  235.  
  236. typedef struct
  237. {
  238.     Integer Length;
  239.     Number_Digit_Type Digits[1];
  240. } Bignum_Struct;
  241.  
  242. #define Get_Number_Length(n) ((n)->Specific.Number.Specific.Bignum.Length)
  243. #define Get_Number_Digits(n) ((n)->Specific.Number.Specific.Bignum.Digits)
  244. #define Get_Number_Digit(n,i) (((n)->Specific.Number.Specific.Bignum.Digits)[i])
  245.  
  246. /* Rationals */
  247.  
  248. typedef struct
  249. {
  250.     struct Object_Struct *Numerator;
  251.     struct Object_Struct *Denominator;
  252. } Rational_Struct;
  253.  
  254. #define Get_Number_Rational_Numerator(n)\
  255.         ((n)->Specific.Number.Specific.Rational.Numerator)
  256. #define Get_Number_Rational_Denominator(n)\
  257.         ((n)->Specific.Number.Specific.Rational.Denominator)
  258.  
  259. /* Reals */
  260.  
  261. #define Get_Number_Real_Value(n) ((n)->Specific.Number.Specific.Real_Value)
  262.  
  263. /* Complex Numbers */
  264.  
  265. typedef struct
  266. {
  267.     Double    Real_Part;
  268.     Double    Imaginary_Part;
  269. } Complex_Struct;
  270.  
  271. #define    Get_Number_Complex_Real_Part(n)\
  272.         ((n)->Specific.Number.Specific.Complex.Real_Part)
  273. #define    Get_Number_Complex_Imaginary_Part(n)\
  274.         ((n)->Specific.Number.Specific.Complex.Imaginary_Part)
  275.  
  276. typedef