home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / bob13st / bob.h < prev    next >
C/C++ Source or Header  |  1991-12-24  |  10KB  |  302 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.  
  23. /* non-character tokens */
  24. #define _TMIN            256
  25. #define T_STRING        256
  26. #define T_IDENTIFIER    257
  27. #define T_NUMBER        258
  28. #define T_CLASS            259
  29. #define T_STATIC        260
  30. #define T_IF            261
  31. #define T_ELSE            262
  32. #define T_WHILE            263
  33. #define T_RETURN        264
  34. #define T_FOR            265
  35. #define T_BREAK            266
  36. #define T_CONTINUE        267
  37. #define T_DO            268
  38. #define T_NEW            269
  39. #define T_NIL            270
  40. #define T_LE            271    /* '<=' */
  41. #define T_EQ            272    /* '==' */
  42. #define T_NE            273    /* '!=' */
  43. #define T_GE            274    /* '>=' */
  44. #define T_SHL            275    /* '<<' */
  45. #define T_SHR            276    /* '>>' */
  46. #define T_AND            277    /* '&&' */
  47. #define T_OR            278    /* '||' */
  48. #define T_INC            279    /* '++' */
  49. #define T_DEC            280    /* '--' */
  50. #define T_ADDEQ            281    /* '+=' */
  51. #define T_SUBEQ            282    /* '-=' */
  52. #define T_MULEQ            283    /* '*=' */
  53. #define T_DIVEQ            284    /* '/=' */
  54. #define T_REMEQ            285    /* '%=' */
  55. #define T_ANDEQ            286    /* '&=' */
  56. #define T_OREQ            287    /* '|=' */
  57. #define T_XOREQ            288    /* '^=' */
  58. #define T_SHLEQ            289    /* '<<=' */
  59. #define T_SHREQ            290    /* '>>=' */
  60. #define T_CC            291    /* '::' */
  61. #define T_MEMREF        292    /* '->' */
  62. #define _TMAX            292
  63.  
  64. /* stack manipulation macros */
  65. #define check(n)          { if (sp - (n) < stkbase) stackover(); }
  66. #define chktype(o,t)     { if (sp[o].v_type != t) badtype(o,t); }
  67. #define push(x,t,f)          (--sp, sp->v_type = (t), sp->v.f = (x))
  68. #define push_integer(x)     push(x,DT_INTEGER,v_integer)
  69. #define push_class(x)     push(x,DT_CLASS,v_class)
  70. #define push_object(x)     push(x,DT_OBJECT,v_object)
  71. #define push_bytecode(x) push(x,DT_BYTECODE,v_vector)
  72. #define push_var(x)          push(x,DT_VAR,v_var)
  73. #define push_nil()          (--sp, sp->v_type = DT_NIL)
  74.  
  75. /* macros to set values */
  76. #define set(s,x,t,f)        ((s)->v.f = (x), (s)->v_type = (t))
  77. #define set_integer(s,x)    set(s,x,DT_INTEGER,v_integer)
  78. #define set_class(s,x)        set(s,x,DT_CLASS,v_class)
  79. #define set_object(s,x)     set(s,x,DT_OBJECT,v_object)
  80. #define set_code(s,x)       set(s,x,DT_CODE,v_code)
  81. #define set_bytecode(s,x)   set(s,x,DT_BYTECODE,v_vector)
  82. #define set_dictionary(s,x) set(s,x,DT_DICTIONARY,v_dictionary)
  83. #define set_var(s,x)        set(s,x,DT_VAR,v_var)
  84. #define set_string(s,x)        set(s,x,DT_STRING,v_string)
  85. #define set_vector(s,x)        set(s,x,DT_VECTOR,v_vector)
  86. #define set_file(s,x)        set(s,x,DT_FILE,v_fp)
  87. #define set_nil(s)            ((s)->v_type = DT_NIL)
  88.  
  89. /* value field access macros */
  90. #define valtype(x)            ((x)->v_type)
  91. #define isnil(x)            ((x)->v_type == DT_NIL)
  92.  
  93. /* class field access macros */
  94. #define claddr(x)            ((x)->v.v_class)
  95. #define clgetname(x)        (&claddr(x)->cl_name)
  96. #define clgetbase(x)        (&claddr(x)->cl_base)
  97. #define clgetmembers(x)        (&claddr(x)->cl_members)
  98. #define clgetfunctions(x)    (&claddr(x)->cl_functions)
  99. #define clgetsize(x)        (claddr(x)->cl_size)
  100.  
  101. /* object field access macros */
  102. #define objaddr(x)            ((x)->v.v_object)
  103. #define objgetclass(x)        (&objaddr(x)->obj_class)
  104. #define objgetmember(x,i)    (&objaddr(x)->obj_members[i])
  105. #define objsetmember(x,i,v)    (objaddr(x)->obj_members[i] = (v))
  106.  
  107. /* vector field access macros */
  108. #define vecaddr(x)                ((x)->v.v_vector)
  109. #define vecgetsize(x)            (vecaddr(x)->vec_size)
  110. #define vecgetelement(x,i)        (&vecaddr(x)->vec_data[i])
  111. #define vecsetelement(x,i,v)    (vecaddr(x)->vec_data[i] = (v))
  112.  
  113. /* string field access macros */
  114. #define straddr(x)            ((x)->v.v_string)
  115. #define strgetsize(x)        (straddr(x)->str_size)
  116. #define strgetdata(x)        (straddr(x)->str_data)
  117.  
  118. /* dictionary field access macros */
  119. #define diaddr(x)            ((x)->v.v_dictionary)
  120. #define digetclass(x)        (&diaddr(x)->di_class)
  121. #define digetcontents(x)    (&diaddr(x)->di_contents)
  122.  
  123. /* dictionary entry field access macros */
  124. #define deaddr(x)            ((x)->v.v_var)
  125. #define degetdictionary(x)    (&deaddr(x)->de_dictionary)
  126. #define degetkey(x)            (&deaddr(x)->de_key)
  127. #define degetvalue(x)        (&deaddr(x)->de_value)
  128. #define degetnext(x)        (&deaddr(x)->de_next)
  129. #define degettype(x)        (deaddr(x)->de_type)
  130.  
  131. /* value descriptor structure */
  132. typedef struct value 
  133. {
  134.     int v_type;                                /* data type */
  135.     union 
  136.     {                                        /* value */
  137.         struct class *v_class;                /*   class (in heap) */
  138.         struct object *v_object;            /*   object (in heap) */
  139.         struct vector *v_vector;            /*   vector (in heap) */
  140.         struct string *v_string;            /*   string (in heap) */
  141.         struct dictionary *v_dictionary;    /*   dictionary (in heap) */
  142.         struct dict_entry *v_var;            /*   variable (in heap) */
  143.         int (*v_code)();                    /*   code for built-in function */
  144.         long v_integer;                        /*   integer */
  145.         FILE *v_fp;                            /*   file pointer */
  146.         struct hdr *v_hdr;                    /*   (used by garbage collector) */
  147.         struct value *v_chain;                /*   (used by garbage collector) */
  148.     } v;
  149. } VALUE;
  150.  
  151. typedef struct hdr 
  152. {
  153.     char   hdr_type;
  154.     char   hdr_flags;
  155.     VALUE *hdr_chain;
  156. } HDR;
  157.  
  158. typedef struct class 
  159. {
  160.     HDR   cl_hdr;
  161.     VALUE cl_name;
  162.     VALUE cl_base;
  163.     VALUE cl_members;
  164.     VALUE cl_functions;
  165.     int   cl_size;
  166. } CLASS;
  167.  
  168. typedef struct object 
  169. {
  170.     HDR   obj_hdr;
  171.     VALUE obj_class;
  172.     VALUE obj_members[1];
  173. } OBJECT;
  174.  
  175. typedef struct vector 
  176. {
  177.     HDR   vec_hdr;
  178.     int   vec_size;
  179.     VALUE vec_data[1];
  180. } VECTOR;
  181.  
  182. typedef struct string 
  183. {
  184.     HDR  str_hdr;
  185.     int  str_size;
  186.     char str_data[1];
  187. } STRING;
  188.  
  189. typedef struct dictionary 
  190. {
  191.     HDR   di_hdr;
  192.     VALUE di_class;
  193.     VALUE di_contents;
  194. } DICTIONARY;
  195.  
  196. /* dictionary entry structure */
  197. typedef struct dict_entry 
  198. {
  199.     HDR   de_hdr;
  200.     VALUE de_dictionary;    /* backpointer to dictionary */
  201.     VALUE de_key;            /* symbol name */
  202.     int   de_type;            /* symbol type */
  203.     VALUE de_value;            /* symbol value */
  204.     VALUE de_next;            /* next entry */
  205. } DICT_ENTRY;
  206.  
  207. /* symbol types */
  208. #define ST_CLASS        1    /* class definition */
  209. #define ST_DATA            2    /* data member */
  210. #define ST_SDATA        3    /* static data member */
  211. #define ST_FUNCTION        4    /* function member */
  212. #define ST_SFUNCTION    5    /* static function member */
  213.  
  214. /* data types */
  215. #define _DTMIN            0
  216. #define DT_NIL            0
  217. #define DT_CLASS        1
  218. #define DT_OBJECT        2
  219. #define DT_VECTOR        3
  220. #define DT_INTEGER        4
  221. #define DT_STRING        5
  222. #define DT_BYTECODE        6
  223. #define DT_CODE            7
  224. #define DT_DICTIONARY    8
  225. #define DT_VAR            9
  226. #define DT_FILE            10
  227. #define _DTMAX            10
  228.  
  229. /* function argument structure */
  230. typedef struct argument 
  231. {
  232.     char *arg_name;                /* argument name */
  233.     struct argument *arg_next;    /* next argument */
  234. } ARGUMENT;
  235.  
  236. /* literal structure */
  237. typedef struct literal 
  238. {
  239.     VALUE lit_value;            /* literal value */
  240.     struct literal *lit_next;    /* next literal */
  241. } LITERAL;
  242.  
  243. /* opcodes */
  244. #define OP_BRT        0x01    /* branch on true */
  245. #define OP_BRF        0x02    /* branch on false */
  246. #define OP_BR        0x03    /* branch unconditionally */
  247. #define OP_NIL        0x04    /* load top of stack with nil */
  248. #define OP_PUSH        0x05    /* push nil onto stack */
  249. #define OP_NOT        0x06    /* logical negate top of stack */
  250. #define OP_NEG        0x07    /* negate top of stack */
  251. #define OP_ADD        0x08    /* add top two stack entries */
  252. #define OP_SUB        0x09    /* subtract top two stack entries */
  253. #define OP_MUL        0x0A    /* multiply top two stack entries */
  254. #define OP_DIV        0x0B    /* divide top two stack entries */
  255. #define OP_REM        0x0C    /* remainder of top two stack entries */
  256. #define OP_BAND        0x0D    /* bitwise and of top two stack entries */
  257. #define OP_BOR        0x0E    /* bitwise or of top two stack entries */
  258. #define OP_XOR        0x0F    /* bitwise xor of top two stack entries */
  259. #define OP_BNOT        0x10    /* bitwise not of top two stack entries */
  260. #define OP_SHL        0x11    /* shift left top two stack entries */
  261. #define OP_SHR        0x12    /* shift right top two stack entries */
  262. #define OP_LT        0x13    /* less than */
  263. #define OP_LE        0x14    /* less than or equal to */
  264. #define OP_EQ        0x15    /* equal to */
  265. #define OP_NE        0x16    /* not equal to */
  266. #define OP_GE        0x17    /* greater than or equal to */
  267. #define OP_GT        0x18    /* greater than */
  268. #define OP_INC        0x19    /* increment */
  269. #define OP_DEC        0x1A    /* decrement */
  270. #define OP_LIT        0x1B    /* load literal */
  271. #define OP_RETURN    0x1C    /* return from interpreter */
  272. #define OP_CALL        0x1D    /* call a function */
  273. #define OP_REF        0x1E    /* load a variable value */
  274. #define OP_SET        0x1F    /* set the value of a variable */
  275. #define OP_VREF        0x20    /* load a vector element */
  276. #define OP_VSET        0x21    /* set a vector element */
  277. #define OP_MREF        0x22    /* load a member variable value */
  278. #define OP_MSET        0x23    /* set a member variable */
  279. #define OP_AREF        0x24    /* load an argument value */
  280. #define OP_ASET        0x25    /* set an argument value */
  281. #define OP_TREF        0x26    /* load a temporary variable value */
  282. #define OP_TSET        0x27    /* set a temporary variable */
  283. #define OP_TSPACE    0x28    /* allocate temporary variable space */
  284. #define OP_SEND        0x29    /* send a message to an object */
  285. #define OP_DUP2        0x2A    /* duplicate top two elements on the stack */
  286. #define OP_NEW        0x2B    /* create a new class object */
  287.  
  288. /* external variables */
  289. extern VALUE *stkbase,*sp,*fp,*stktop;
  290. extern VALUE  nil;
  291.  
  292. /* external routines */
  293. extern CLASS *newclass();
  294. extern OBJECT *newobject();
  295. extern VECTOR *newvector();
  296. extern STRING *newstring();
  297. extern STRING *makestring();
  298. extern DICTIONARY *newdictionary();
  299. extern DICT_ENTRY *findentry();
  300. extern DICT_ENTRY *addentry();
  301. extern char *getcstring();
  302.