home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / XSCHEME2.ZIP / xscheme.h < prev    next >
Text File  |  1992-04-30  |  14KB  |  440 lines

  1. /* xscheme.h - xscheme definitions */
  2. /*    Copyright (c) 1988, by David Michael Betz
  3.     All Rights Reserved
  4.     Permission is granted for unrestricted non-commercial use    */
  5.  
  6. /* system specific definitions */
  7. /*#define _TURBOC_*/
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <setjmp.h>
  12.  
  13. /* FORWARD    type of a forward declaration () */
  14. /* LOCAL    type of a local function (static) */
  15. /* AFMT        printf format for addresses ("%x") */
  16. /* OFFTYPE    number the size of an address (int) */
  17. /* FIXTYPE    data type for fixed point numbers (long) */
  18. /* ITYPE    fixed point input conversion routine type (long atol()) */
  19. /* ICNV        fixed point input conversion routine (atol) */
  20. /* IFMT        printf format for fixed point numbers ("%ld") */
  21. /* FLOTYPE    data type for floating point numbers (float) */
  22. /* FFMT        printf format for floating point numbers (%.15g) */
  23.  
  24. /* for the Microsoft C 6.0 under OS/2 2.0 (32bit) compiler */
  25. #ifdef M_I386
  26. #define AFMT        "%lx"
  27. #endif
  28.  
  29. /* for the Lightspeed C compiler - Macintosh */
  30. #ifdef LSC
  31. #define AFMT        "%lx"
  32. #define OFFTYPE        long
  33. #define NIL        (void *)0
  34. #define MACINTOSH
  35. #endif
  36.  
  37. /* for the UNIX System V C compiler */
  38. #ifdef UNIX
  39. #endif
  40.  
  41. /* for the Aztec C compiler - Amiga */
  42. #ifdef AZTEC_AMIGA
  43. #define AFMT        "%lx"
  44. #define OFFTYPE        long
  45. #endif
  46.  
  47. /* for the Mark Williams C compiler - Atari ST */
  48. #ifdef MWC
  49. #define AFMT        "%lx"
  50. #define OFFTYPE        long
  51. #endif
  52.  
  53. /* for the Microsoft C 5.0 compiler */
  54. #ifdef MSC
  55. #define AFMT        "%lx"
  56. #define OFFTYPE        long
  57. #define INSEGMENT(n,s)    (((OFFTYPE)(n) >> 16) == ((OFFTYPE)(s) >> 16))
  58. #define VCOMPARE(f,s,t)    ((LVAL huge *)(f) + (s) <= (LVAL huge *)(t))
  59. /* #define MSDOS -- MSC 5.0 defines this automatically */
  60. #endif
  61.  
  62. /* for the Turbo C compiler */
  63. #ifdef _TURBOC_
  64. #define AFMT        "%lx"
  65. #define OFFTYPE        long
  66. #define INSEGMENT(n,s)    (((OFFTYPE)(n) >> 16) == ((OFFTYPE)(s) >> 16))
  67. #define VCOMPARE(f,s,t)    ((LVAL huge *)(f) + (s) <= (LVAL huge *)(t))
  68. #define MSDOS
  69. #endif
  70.  
  71. /* size of each type of memory segment */
  72. #ifndef NSSIZE
  73. #define NSSIZE    4000    /* number of nodes per node segment */
  74. #endif
  75. #ifndef VSSIZE
  76. #define VSSIZE    10000    /* number of LVAL's per vector segment */
  77. #endif
  78.  
  79. /* default important definitions */
  80. #ifndef FORWARD
  81. #define FORWARD
  82. #endif
  83. #ifndef LOCAL
  84. #define LOCAL        static
  85. #endif
  86. #ifndef AFMT
  87. #define AFMT        "%x"
  88. #endif
  89. #ifndef OFFTYPE
  90. #define OFFTYPE        int
  91. #endif
  92. #ifndef FIXTYPE
  93. #define FIXTYPE        long
  94. #endif
  95. #ifndef ITYPE
  96. #define ITYPE        long atol()
  97. #endif
  98. #ifndef ICNV
  99. #define ICNV(n)        atol(n)
  100. #endif
  101. #ifndef IFMT
  102. #define IFMT        "%ld"
  103. #endif
  104. #ifndef FLOTYPE
  105. #define FLOTYPE        double
  106. #endif
  107. #ifndef FFMT
  108. #define FFMT        "%.15g"
  109. #endif
  110. #ifndef SFIXMIN
  111. #define SFIXMIN        -1048576
  112. #define SFIXMAX        1048575
  113. #endif
  114. #ifndef INSEGMENT
  115. #define INSEGMENT(n,s)    ((n) >= &(s)->ns_data[0] \
  116.                       && (n) <  &(s)->ns_data[0] + (s)->ns_size)
  117. #endif
  118. #ifndef VCOMPARE
  119. #define VCOMPARE(f,s,t)    ((f) + (s) <= (t))
  120. #endif
  121.  
  122. /* useful definitions */
  123. #define TRUE    1
  124. #define FALSE    0
  125. #ifndef NIL
  126. #define NIL    (LVAL)0
  127. #endif
  128.  
  129. /* program limits */
  130. #define STRMAX        100        /* maximum length of a string constant */
  131. #define HSIZE        199        /* symbol hash table size */
  132. #define SAMPLE        100        /* control character sample rate */
  133.  
  134. /* stack manipulation macros */
  135. #define check(n)    { if (xlsp - (n) < xlstkbase) xlstkover(); }
  136. #define cpush(v)    { if (xlsp > xlstkbase) push(v); else xlstkover(); }
  137. #define push(v)        (*--xlsp = (v))
  138. #define pop()        (*xlsp++)
  139. #define top()        (*xlsp)
  140. #define settop(v)    (*xlsp = (v))
  141. #define drop(n)        (xlsp += (n))
  142.  
  143. /* argument list parsing macros */
  144. #define xlgetarg()    (testarg(nextarg()))
  145. #define xllastarg()    {if (xlargc != 0) xltoomany();}
  146. #define xlpoprest()    {xlsp += xlargc;}
  147. #define testarg(e)    (moreargs() ? (e) : xltoofew())
  148. #define typearg(tp)    (tp(*xlsp) ? nextarg() : xlbadtype(*xlsp))
  149. #define nextarg()    (--xlargc, *xlsp++)
  150. #define moreargs()    (xlargc > 0)
  151.  
  152. /* macros to get arguments of a particular type */
  153. #define xlgacons()    (testarg(typearg(consp)))
  154. #define xlgalist()    (testarg(typearg(listp)))
  155. #define xlgasymbol()    (testarg(typearg(symbolp)))
  156. #define xlgastring()    (testarg(typearg(stringp)))
  157. #define xlgaobject()    (testarg(typearg(objectp)))
  158. #define xlgafixnum()    (testarg(typearg(fixp)))
  159. #define xlganumber()    (testarg(typearg(numberp)))
  160. #define xlgachar()    (testarg(typearg(charp)))
  161. #define xlgavector()    (testarg(typearg(vectorp)))
  162. #define xlgaport()    (testarg(typearg(portp)))
  163. #define xlgaiport()    (testarg(typearg(iportp)))
  164. #define xlgaoport()    (testarg(typearg(oportp)))
  165. #define xlgaclosure()    (testarg(typearg(closurep)))
  166. #define xlgaenv()    (testarg(typearg(envp)))
  167.  
  168. /* node types */
  169. #define FREE        0
  170. #define CONS        1
  171. #define SYMBOL        2
  172. #define FIXNUM        3
  173. #define FLONUM        4
  174. #define STRING        5
  175. #define OBJECT        6
  176. #define PORT        7
  177. #define VECTOR        8
  178. #define CLOSURE        9
  179. #define METHOD        10
  180. #define CODE        11
  181. #define SUBR        12
  182. #define XSUBR        13
  183. #define CSUBR        14
  184. #define CONTINUATION    15
  185. #define CHAR        16
  186. #define PROMISE        17
  187. #define ENV        18
  188.  
  189. /* node flags */
  190. #define MARK        1
  191. #define LEFT        2
  192.  
  193. /* port flags */
  194. #define PF_INPUT    1
  195. #define PF_OUTPUT    2
  196. #define PF_BINARY    4
  197.  
  198. /* new node access macros */
  199. #define ntype(x)    ((OFFTYPE)(x) & 1 ? FIXNUM : (x)->n_type)
  200.  
  201. /* macro to determine if a non-nil value is a pointer */
  202. #define ispointer(x)    (((OFFTYPE)(x) & 1) == 0)
  203.  
  204. /* type predicates */
  205. #define atom(x)        ((x) == NIL || ntype(x) != CONS)
  206. #define null(x)        ((x) == NIL)
  207. #define listp(x)    ((x) == NIL || ntype(x) == CONS)
  208. #define numberp(x)    ((x) && (ntype(x) == FIXNUM || ntype(x) == FLONUM))
  209. #define boundp(x)    (getvalue(x) != s_unbound)
  210. #define iportp(x)    (portp(x) && (getpflags(x) & PF_INPUT) != 0)
  211. #define oportp(x)    (portp(x) && (getpflags(x) & PF_OUTPUT) != 0)
  212.  
  213. /* basic type predicates */
  214. #define consp(x)    ((x) && ntype(x) == CONS)
  215. #define stringp(x)    ((x) && ntype(x) == STRING)
  216. #define symbolp(x)    ((x) && ntype(x) == SYMBOL)
  217. #define portp(x)    ((x) && ntype(x) == PORT)
  218. #define objectp(x)    ((x) && ntype(x) == OBJECT)
  219. #define fixp(x)        ((x) && ntype(x) == FIXNUM)
  220. #define floatp(x)    ((x) && ntype(x) == FLONUM)
  221. #define vectorp(x)    ((x) && ntype(x) == VECTOR)
  222. #define closurep(x)    ((x) && ntype(x) == CLOSURE)
  223. #define continuationp(x) ((x) && ntype(x) == CONTINUATION)
  224. #define codep(x)    ((x) && ntype(x) == CODE)
  225. #define methodp(x)    ((x) && ntype(x) == METHOD)
  226. #define subrp(x)    ((x) && ntype(x) == SUBR)
  227. #define xsubrp(x)    ((x) && ntype(x) == XSUBR)
  228. #define charp(x)    ((x) && ntype(x) == CHAR)
  229. #define promisep(x)    ((x) && ntype(x) == PROMISE)
  230. #define envp(x)        ((x) && ntype(x) == ENV)
  231. #define booleanp(x)    ((x) == NIL || ntype(x) == BOOLEAN)
  232.  
  233. /* vector update macro
  234.    This is necessary because the memory pointed to by the n_vdata field
  235.    of a vector object can move during a garbage collection.  This macro
  236.    guarantees that evaluation happens in the right order.
  237. */
  238. #define vupdate(x,i,v)    { LVAL vutmp=(v); (x)->n_vdata[i] = vutmp; }
  239.  
  240. /* cons access macros */
  241. #define car(x)        ((x)->n_car)
  242. #define cdr(x)        ((x)->n_cdr)
  243. #define rplaca(x,y)    ((x)->n_car = (y))
  244. #define rplacd(x,y)    ((x)->n_cdr = (y))
  245.  
  246. /* symbol access macros */
  247. #define getvalue(x)     ((x)->n_vdata[0])
  248. #define setvalue(x,v)     vupdate(x,0,v)
  249. #define getpname(x)     ((x)->n_vdata[1])
  250. #define setpname(x,v)     vupdate(x,1,v)
  251. #define getplist(x)     ((x)->n_vdata[2])
  252. #define setplist(x,v)     vupdate(x,2,v)
  253. #define SYMSIZE        3
  254.  
  255. /* vector access macros */
  256. #define getsize(x)    ((x)->n_vsize)
  257. #define getelement(x,i)    ((x)->n_vdata[i])
  258. #define setelement(x,i,v) vupdate(x,i,v)
  259.  
  260. /* object access macros */
  261. #define getclass(x)    ((x)->n_vdata[1])
  262. #define setclass(x,v)    vupdate(x,1,v)
  263. #define getivar(x,i)    ((x)->n_vdata[i])
  264. #define setivar(x,i,v)    vupdate(x,i,v)
  265.  
  266. /* promise access macros */
  267. #define getpproc(x)    ((x)->n_car)
  268. #define setpproc(x,v)    ((x)->n_car = (v))
  269. #define getpvalue(x)    ((x)->n_cdr)
  270. #define setpvalue(x,v)    ((x)->n_cdr = (v))
  271.  
  272. /* closure access macros */
  273. #define getcode(x)    ((x)->n_car)
  274. #define getenv(x)    ((x)->n_cdr)
  275.  
  276. /* code access macros */
  277. #define getbcode(x)        ((x)->n_vdata[0])
  278. #define setbcode(x,v)        vupdate(x,0,v)
  279. #define getcname(x)        ((x)->n_vdata[1])
  280. #define setcname(x,v)        vupdate(x,1,v)
  281. #define getvnames(x)        ((x)->n_vdata[2])
  282. #define setvnames(x,v)        vupdate(x,2,v)
  283. #define FIRSTLIT        3
  284.  
  285. /* fixnum/flonum/character access macros */
  286. #define getfixnum(x)    ((OFFTYPE)(x) & 1 ? getsfixnum(x) : (x)->n_int)
  287. #define getflonum(x)    ((x)->n_flonum)
  288. #define getchcode(x)    ((x)->n_chcode)
  289.  
  290. /* small fixnum access macros */
  291. #define cvsfixnum(x)    ((LVAL)(((OFFTYPE)x << 1) | 1))
  292. #define getsfixnum(x)    ((FIXTYPE)((OFFTYPE)(x) >> 1))
  293.  
  294. /* string access macros */
  295. #define getstring(x)    ((unsigned char *)(x)->n_vdata)
  296. #define getslength(x)    ((x)->n_vsize)
  297.  
  298. /* iport/oport access macros */
  299. #define getfile(x)    ((x)->n_fp)
  300. #define setfile(x,v)    ((x)->n_fp = (v))
  301. #define getsavech(x)    ((x)->n_savech)
  302. #define setsavech(x,v)    ((x)->n_savech = (v))
  303. #define getpflags(x)    ((x)->n_pflags)
  304. #define setpflags(x,v)    ((x)->n_pflags = (v))
  305.  
  306. /* subr access macros */
  307. #define getsubr(x)    ((x)->n_subr)
  308. #define getoffset(x)    ((x)->n_offset)
  309.  
  310. /* list node */
  311. #define n_car        n_info.n_xlist.xl_car
  312. #define n_cdr        n_info.n_xlist.xl_cdr
  313.  
  314. /* integer node */
  315. #define n_int        n_info.n_xint.xi_int
  316.  
  317. /* flonum node */
  318. #define n_flonum    n_info.n_xflonum.xf_flonum
  319.  
  320. /* character node */
  321. #define n_chcode    n_info.n_xchar.xc_chcode
  322.  
  323. /* string node */
  324. #define n_str        n_info.n_xstr.xst_str
  325. #define n_strlen    n_info.n_xstr.xst_length
  326.  
  327. /* file pointer node */
  328. #define n_fp        n_info.n_xfptr.xf_fp
  329. #define n_savech    n_info.n_xfptr.xf_savech
  330. #define n_pflags    n_info.n_xfptr.xf_pflags
  331.  
  332. /* vector/object node */
  333. #define n_vsize        n_info.n_xvect.xv_size
  334. #define n_vdata        n_info.n_xvect.xv_data
  335.  
  336. /* subr node */
  337. #define n_subr        n_info.n_xsubr.xs_subr
  338. #define n_offset    n_info.n_xsubr.xs_offset
  339.  
  340. /* node structure */
  341. typedef struct node {
  342.     char n_type;        /* type of node */
  343.     char n_flags;        /* flag bits */
  344.     union ninfo {         /* value */
  345.     struct xlist {        /* list node (cons) */
  346.         struct node *xl_car;    /* the car pointer */
  347.         struct node *xl_cdr;    /* the cdr pointer */
  348.     } n_xlist;
  349.     struct xint {        /* integer node */
  350.         FIXTYPE xi_int;        /* integer value */
  351.     } n_xint;
  352.     struct xflonum {    /* flonum node */
  353.         FLOTYPE xf_flonum;        /* flonum value */
  354.     } n_xflonum;
  355.     struct xchar {        /* character node */
  356.         int xc_chcode;        /* character code */
  357.     } n_xchar;
  358.     struct xstr {        /* string node */
  359.         int xst_length;        /* string length */
  360.         unsigned char *xst_str;    /* string pointer */
  361.     } n_xstr;
  362.     struct xfptr {        /* file pointer node */
  363.         FILE *xf_fp;        /* the file pointer */
  364.         short xf_savech;        /* lookahead character for input files */
  365.         short xf_pflags;        /* port flags */
  366.     } n_xfptr;
  367.     struct xvect {        /* vector node */
  368.         int xv_size;        /* vector size */
  369.         struct node **xv_data;    /* vector data */
  370.     } n_xvect;
  371.     struct xsubr {        /* subr/fsubr node */
  372.         struct node *(*xs_subr)();    /* function pointer */
  373.         int xs_offset;        /* offset into funtab */
  374.     } n_xsubr;
  375.     } n_info;
  376. } NODE,*LVAL;
  377.  
  378. /* memory allocator definitions */
  379.  
  380. /* macros to compute the size of a segment */
  381. #define nsegsize(n) (sizeof(NSEGMENT)+((n)-1)*sizeof(struct node))
  382. #define vsegsize(n) (sizeof(VSEGMENT)+((n)-1)*sizeof(LVAL))
  383.  
  384. /* macro to convert a byte size to a word size */
  385. #define btow_size(n)    (((n) + sizeof(LVAL) - 1) / sizeof(LVAL))
  386.  
  387. /* node segment structure */
  388. typedef struct nsegment {
  389.     struct nsegment *ns_next;    /* next node segment */
  390.     unsigned int ns_size;    /* number of nodes in this segment */
  391.     struct node ns_data[1];    /* segment data */
  392. } NSEGMENT;
  393.  
  394. /* vector segment structure */
  395. typedef struct vsegment {
  396.     struct vsegment *vs_next;    /* next vector segment */
  397.     LVAL *vs_free;        /* next free location in this segment */
  398.     LVAL *vs_top;        /* top of segment (plus one) */
  399.     LVAL vs_data[1];        /* segment data */
  400. } VSEGMENT;
  401.  
  402. /* function definition structure */
  403. typedef struct {
  404.     char *fd_name;    /* function name */
  405.     LVAL (*fd_subr)();    /* function entry point */
  406. } FUNDEF;
  407.  
  408. /* external variables */
  409. extern LVAL *xlstkbase;     /* base of value stack */
  410. extern LVAL *xlstktop;        /* top of value stack */
  411. extern LVAL *xlsp;            /* value stack pointer */
  412. extern int xlargc;        /* argument count for current call */
  413.  
  414. /* external routine declarations */
  415. extern LVAL cons();        /* (cons x y) */
  416. extern LVAL xlenter();        /* enter a symbol */
  417. extern LVAL xlgetprop();    /* get the value of a property */
  418. extern LVAL cvsymbol();     /* convert a string to a symbol */
  419. extern LVAL cvstring();     /* convert a string */
  420. extern LVAL cvfixnum();     /* convert a fixnum */
  421. extern LVAL cvflonum();           /* convert a flonum */
  422. extern LVAL cvchar();         /* convert a character */
  423. extern LVAL cvclosure();    /* convert code and an env to a closure */
  424. extern LVAL cvmethod();        /* convert code and an env to a method */
  425. extern LVAL cvsubr();        /* convert a function into a subr */
  426. extern LVAL cvport();        /* convert a file pointer to an input port */
  427. extern LVAL cvpromise();    /* convert a procedure to a promise */
  428. extern LVAL newstring();    /* create a new string */
  429. extern LVAL newobject();    /* create a new object */
  430. extern LVAL newvector();    /* create a new vector */
  431. extern LVAL newcode();        /* create a new code object */
  432. extern LVAL newcontinuation();    /* create a new continuation object */
  433. extern LVAL newframe();        /* create a new environment frame */
  434. extern LVAL newnode();        /* create a new node */
  435. extern LVAL xltoofew();        /* report "too few arguments" */
  436. extern LVAL xlbadtype();    /* report "wrong argument type" */
  437. extern LVAL curinput();        /* get the current input port */
  438. extern LVAL curoutput();    /* get the current output port */
  439.  
  440.