home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 09 / bob / bob.h < prev    next >
Text File  |  1991-07-11  |  7KB  |  219 lines

  1. /* bob.h - bob definitions */
  2. /*
  3.     Copyright (c) 1991, by David Michael Betz
  4.     All rights reserved
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9.  
  10. /* limits */
  11. #define TKNSIZE        50    /* maximum token size */
  12. #define SMAX        500    /* runtime stack size */
  13. #define CMAX        32767    /* code buffer size */
  14.  
  15. /* useful definitions */
  16. #define TRUE        1
  17. #define FALSE        0
  18.  
  19. /* token definitions */
  20. #define T_NOTOKEN    -1
  21. #define T_EOF        0
  22. #define T_STRING    1
  23. #define T_IDENTIFIER    2
  24. #define T_NUMBER    3
  25. #define T_CLASS        4
  26. #define T_STATIC    5
  27. #define T_IF        6
  28. #define T_ELSE        7
  29. #define T_WHILE        8
  30. #define T_RETURN    9
  31. #define T_FOR        10
  32. #define T_BREAK        11
  33. #define T_CONTINUE    12
  34. #define T_DO        13
  35. #define T_NEW        14
  36. #define T_NIL        15
  37. #define T_LE        16    /* '<=' */
  38. #define T_EQ        17    /* '==' */
  39. #define T_NE        18    /* '!=' */
  40. #define T_GE        19    /* '>=' */
  41. #define T_SHL        20    /* '<<' */
  42. #define T_SHR        21    /* '>>' */
  43. #define T_AND        22    /* '&&' */
  44. #define T_OR        23    /* '||' */
  45. #define T_INC        24    /* '++' */
  46. #define T_DEC        25    /* '--' */
  47. #define T_ADDEQ        26    /* '+=' */
  48. #define T_SUBEQ        27    /* '-=' */
  49. #define T_MULEQ        28    /* '*=' */
  50. #define T_DIVEQ        29    /* '/=' */
  51. #define T_CC        30    /* '::' */
  52. #define T_MEMREF    31    /* '->' */
  53. #define _TMAX        31
  54.  
  55. /* stack manipulation macros */
  56. #define check(n)     { if (sp - (n) < stkbase) stackover(); }
  57. #define chktype(o,t)     { if (sp[o].v_type != t) badtype(o,t); }
  58. #define push(x,t,f)     (--sp, sp->v_type = (t), sp->v.f = (x))
  59. #define push_integer(x)     push(x,DT_INTEGER,v_integer)
  60. #define push_class(x)     push(x,DT_CLASS,v_class)
  61. #define push_object(x)     push(x,DT_OBJECT,v_object)
  62. #define push_bytecode(x) push(x,DT_CODE,v_bytecode)
  63.  
  64. /* macros to set values */
  65. #define set(s,x,t,f)      ((s)->v_type = (t), (s)->v.f = (x))
  66. #define set_integer(s,x)  set(s,x,DT_INTEGER,v_integer)
  67. #define set_class(s,x)      set(s,x,DT_CLASS,v_class)
  68. #define set_object(s,x)   set(s,x,DT_OBJECT,v_object)
  69. #define set_code(s,x)     set(s,x,DT_CODE,v_code)
  70. #define set_bytecode(s,x) set(s,x,DT_BYTECODE,v_bytecode)
  71. #define set_var(s,x)      set(s,x,DT_VAR,v_var)
  72. #define set_string(s,x)      set(s,x,DT_STRING,v_string)
  73. #define set_vector(s,x)      set(s,x,DT_VECTOR,v_vector)
  74. #define set_file(s,x)      set(s,x,DT_FILE,v_fp)
  75. #define set_nil(s)      ((s)->v_type = DT_NIL)
  76.  
  77. /* string value structure */
  78. typedef struct string {
  79.   int s_length;            /* length */
  80.   unsigned char s_data[1];    /* data */
  81. } STRING;
  82.  
  83. /* value descriptor structure */
  84. typedef struct value {
  85.   int v_type;            /* data type */
  86.   union {            /* value */
  87.     struct class *v_class;
  88.     struct value *v_object;
  89.     struct value *v_vector;
  90.     struct string *v_string;
  91.     struct value *v_bytecode;
  92.     struct dict_entry *v_var;
  93.     int (*v_code)();
  94.     long v_integer;
  95.     FILE *v_fp;
  96.   } v;
  97. } VALUE;
  98.  
  99. /* object fields */
  100. #define OB_CLASS    0    /* class */
  101. #define _OBSIZE        1
  102.  
  103. /* class definition structure */
  104. typedef struct class {
  105.     char *cl_name;            /* class name */
  106.     struct class *cl_base;        /* base class */
  107.     struct dictionary *cl_members;    /* data member */
  108.     struct dictionary *cl_functions;    /* function members */
  109.     int cl_size;            /* object size */
  110. } CLASS;
  111.  
  112. /* dictionary structure */
  113. typedef struct dictionary {
  114.     CLASS *di_class;            /* associated class */
  115.     struct dict_entry *di_contents;    /* dictionary entries */
  116. } DICTIONARY;
  117.  
  118. /* dictionary entry structure */
  119. typedef struct dict_entry {
  120.     DICTIONARY *de_dictionary;        /* backpointer to dictionary */
  121.     char *de_key;            /* symbol name */
  122.     int de_type;            /* symbol type */
  123.     VALUE de_value;            /* symbol value */
  124.     struct dict_entry *de_next;        /* next entry */
  125. } DICT_ENTRY;
  126.  
  127. /* symbol types */
  128. #define ST_CLASS    1    /* class definition */
  129. #define ST_DATA        2    /* data member */
  130. #define ST_SDATA    3    /* static data member */
  131. #define ST_FUNCTION    4    /* function member */
  132. #define ST_SFUNCTION    5    /* static function member */
  133.  
  134. /* data types */
  135. #define _DTMIN        0
  136. #define DT_NIL        0
  137. #define DT_CLASS    1
  138. #define DT_OBJECT    2
  139. #define DT_VECTOR    3
  140. #define DT_INTEGER    4
  141. #define DT_STRING    5
  142. #define DT_BYTECODE    6
  143. #define DT_CODE        7
  144. #define DT_VAR        8
  145. #define DT_FILE        9
  146. #define _DTMAX        9
  147.  
  148. /* function argument structure */
  149. typedef struct argument {
  150.     char *arg_name;        /* argument name */
  151.     struct argument *arg_next;    /* next argument */
  152. } ARGUMENT;
  153.  
  154. /* literal structure */
  155. typedef struct literal {
  156.     VALUE lit_value;        /* literal value */
  157.     struct literal *lit_next;    /* next literal */
  158. } LITERAL;
  159.  
  160. /* opcodes */
  161. #define OP_BRT        0x01    /* branch on true */
  162. #define OP_BRF        0x02    /* branch on false */
  163. #define OP_BR        0x03    /* branch unconditionally */
  164. #define OP_NIL        0x04    /* load top of stack with nil */
  165. #define OP_PUSH        0x05    /* push nil onto stack */
  166. #define OP_NOT        0x06    /* logical negate top of stack */
  167. #define OP_NEG        0x07    /* negate top of stack */
  168. #define OP_ADD        0x08    /* add top two stack entries */
  169. #define OP_SUB        0x09    /* subtract top two stack entries */
  170. #define OP_MUL        0x0A    /* multiply top two stack entries */
  171. #define OP_DIV        0x0B    /* divide top two stack entries */
  172. #define OP_REM        0x0C    /* remainder of top two stack entries */
  173. #define OP_BAND        0x0D    /* bitwise and of top two stack entries */
  174. #define OP_BOR        0x0E    /* bitwise or of top two stack entries */
  175. #define OP_XOR        0x0F    /* bitwise xor of top two stack entries */
  176. #define OP_BNOT        0x10    /* bitwise not of top two stack entries */
  177. #define OP_SHL        0x11    /* shift left top two stack entries */
  178. #define OP_SHR        0x12    /* shift right top two stack entries */
  179. #define OP_LT        0x13    /* less than */
  180. #define OP_LE        0x14    /* less than or equal to */
  181. #define OP_EQ        0x15    /* equal to */
  182. #define OP_NE        0x16    /* not equal to */
  183. #define OP_GE        0x17    /* greater than or equal to */
  184. #define OP_GT        0x18    /* greater than */
  185. #define OP_INC        0x19    /* increment */
  186. #define OP_DEC        0x1A    /* decrement */
  187. #define OP_LIT        0x1B    /* load literal */
  188. #define OP_RETURN    0x1C    /* return from interpreter */
  189. #define OP_CALL        0x1D    /* call a function */
  190. #define OP_REF        0x1E    /* load a variable value */
  191. #define OP_SET        0x1F    /* set the value of a variable */
  192. #define OP_VREF        0x20    /* load a vector element */
  193. #define OP_VSET        0x21    /* set a vector element */
  194. #define OP_MREF        0x22    /* load a member variable value */
  195. #define OP_MSET        0x23    /* set a member variable */
  196. #define OP_AREF        0x24    /* load an argument value */
  197. #define OP_ASET        0x25    /* set an argument value */
  198. #define OP_TREF        0x26    /* load a temporary variable value */
  199. #define OP_TSET        0x27    /* set a temporary variable */
  200. #define OP_TSPACE    0x28    /* allocate temporary variable space */
  201. #define OP_SEND        0x29    /* send a message to an object */
  202. #define OP_DUP2        0x2A    /* duplicate top two elements on the stack */
  203. #define OP_NEW        0x2B    /* create a new class object */
  204.  
  205. /* external variables */
  206. extern VALUE *stkbase,*sp,*fp,*stktop;
  207.  
  208. /* external routines */
  209. extern CLASS *newclass();
  210. extern VALUE *newobject();
  211. extern VALUE *newvector();
  212. extern STRING *newstring();
  213. extern STRING *makestring();
  214. extern DICTIONARY *newdictionary();
  215. extern DICT_ENTRY *findentry();
  216. extern DICT_ENTRY *addentry();
  217. extern char *copystring();
  218. extern char *getmemory();
  219.