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