1 /* utilities - basic utility functions */
2 /* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney */
3 /* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz */
4 /* You may give out copies of this software; for conditions see the */
5 /* file COPYING included with this distribution. */
7 /****** ///// clean this stuff up */
12 typedef LVAL (*subrfun
)(V
);
14 /************************************************************************/
15 /** Basic Utilities **/
16 /************************************************************************/
18 /* return list of two elements */
19 LVAL list2
P2C(LVAL
, x1
, LVAL
, x2
)
23 /* protect some pointers */
32 list
= cons(y1
, list
);
34 /* restore the stack frame */
40 /* return list of three elements */
41 LVAL list3
P3C(LVAL
, x1
, LVAL
, x2
, LVAL
, x3
)
43 LVAL list
, y1
, y2
, y3
;
45 /* protect some pointers */
56 list
= cons(y2
, list
);
57 list
= cons(y1
, list
);
59 /* restore the stack frame */
65 /* return the i-th argument, without popping it; signal an error if needed. */
66 LVAL peekarg
P1C(int, i
)
68 if (xlargc
<= i
) xltoofew();
72 /* Get the next element in the sequence; cdr the pointer if it is a list */
73 LVAL getnextelement
P2C(LVAL
*, pseq
, int, i
)
77 switch (ntype(*pseq
)) {
79 value
= getelement(*pseq
, i
);
83 value
= gettvecelement(*pseq
, i
);
96 /* Set the next element in the sequence; cdr the pointer if it is a list */
97 VOID setnextelement
P3C(LVAL
*, pseq
, int, i
, LVAL
, value
)
99 switch (ntype(*pseq
)) {
101 setelement(*pseq
, i
, value
);
105 settvecelement(*pseq
, i
, value
);
108 rplaca(*pseq
, value
);
117 /************************************************************************/
118 /** Function Applicaiton Utilities **/
119 /************************************************************************/
121 VOID pushargvec
P3C(LVAL
, fun
, int, argc
, LVAL
*, argv
)
126 /* build a new argument stack frame */
127 newfp
= oldsp
= xlsp
;
128 pusharg(NIL
); /* place holder for stack frame increment */
130 pusharg(NIL
); /* place holder for argc */
132 /* push the arguments */
133 for (i
= 0; i
< argc
; i
++)
136 /* establish the new stack frame */
137 oldsp
[0] = cvfixnum((FIXTYPE
)(newfp
- xlfp
));
138 oldsp
[2] = cvfixnum((FIXTYPE
) argc
);
142 LVAL xsapplysubr
P2C(subrfun
, f
, LVAL
, args
)
147 xlprot1(args
); /* protect arguments while pushing */
148 argc
= pushargs(NIL
, args
);
149 xlpop(); /* now they are protected since they are on the stack */
159 /* remove the call frame */
161 xlfp
= xlfp
- (int)getfixnum(*xlfp
);
165 LVAL xscallsubrvec
P3C(subrfun
, f
, int, argc
, LVAL
*, argv
)
170 pushargvec(NIL
, argc
, argv
);
179 /* remove the call frame */
181 xlfp
= xlfp
- (int)getfixnum(*xlfp
);
185 LVAL xsfuncall0
P1C(LVAL
, fun
)
187 pushargvec(fun
, 0, NULL
);
191 LVAL xsfuncall1
P2C(LVAL
, fun
, LVAL
, x
)
193 pushargvec(fun
, 1, &x
);
197 LVAL xsfuncall2
P3C(LVAL
, fun
, LVAL
, x
, LVAL
, y
)
203 pushargvec(fun
, 2, args
);
207 /* replicates a list n times */
208 int xsboolkey
P2C(LVAL
, key
, int, dflt
)
213 if (xlgetkeyarg(key
, &val
)) result
= ((val
!= NIL
) ? TRUE
: FALSE
);