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