File: utils.c

package info (click to toggle)
xlispstat 3.52.0-3
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 7,472 kB
  • ctags: 12,480
  • sloc: ansic: 89,534; lisp: 21,690; sh: 1,525; makefile: 520; csh: 1
file content (215 lines) | stat: -rw-r--r-- 4,566 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* utilities - basic utility functions                                 */
/* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */

/****** ///// clean this stuff up */

#include "xlisp.h"
#include "xlstat.h"

typedef LVAL (*subrfun)(V);

/************************************************************************/
/**                           Basic Utilities                          **/
/************************************************************************/

/* return list of two elements */
LVAL list2 P2C(LVAL, x1, LVAL, x2)
{
  LVAL list, y1, y2;
  
  /* protect some pointers */
  xlstkcheck(3);
  xlsave(list);
  xlsave(y1);
  xlsave(y2);
  
  y1 = x1;
  y2 = x2;
  list = consa(y2);
  list = cons(y1, list);
  
  /* restore the stack frame */
  xlpopn(3);
  
  return(list);
}

/* return list of three elements */
LVAL list3 P3C(LVAL, x1, LVAL, x2, LVAL, x3)
{
  LVAL list, y1, y2, y3;
  
  /* protect some pointers */
  xlstkcheck(4);
  xlsave(list);
  xlsave(y1);
  xlsave(y2);
  xlsave(y3);

  y1 = x1;
  y2 = x2;
  y3 = x3;
  list = consa(y3);
  list = cons(y2, list);
  list = cons(y1, list);
  
  /* restore the stack frame */
  xlpopn(4);
  
  return(list);
}

/* return the i-th argument, without popping it; signal an error if needed. */
LVAL peekarg P1C(int, i)
{
  if (xlargc <= i) xltoofew();
  return(xlargv[i]);
}

/* Get the next element in the sequence; cdr the pointer if it is a list */
LVAL getnextelement P2C(LVAL *, pseq, int, i)
{
  LVAL value;

  switch (ntype(*pseq)) {
  case VECTOR:
    value = getelement(*pseq, i);
    break;
  case TVEC:
  case STRING:
    value = gettvecelement(*pseq, i);
    break;
  case CONS:  
    value = car(*pseq);
    *pseq = cdr(*pseq);
    break;
  default:
    xlbadtype(*pseq);
    value = NIL;
  }
  return(value);
}

/* Set the next element in the sequence; cdr the pointer if it is a list */
VOID setnextelement P3C(LVAL *, pseq, int, i, LVAL, value)
{
  switch (ntype(*pseq)) {
  case VECTOR:
    setelement(*pseq, i, value);
    break;
  case TVEC:
  case STRING:
    settvecelement(*pseq, i, value);
    break;
  case CONS:
    rplaca(*pseq, value);
    *pseq = cdr(*pseq);
    break;
  default:
    xlbadtype(*pseq);
  }
}


/************************************************************************/
/**                  Function Applicaiton Utilities                    **/
/************************************************************************/

VOID pushargvec P3C(LVAL, fun, int, argc, LVAL *, argv)
{
  LVAL *newfp, *oldsp;
  int i;

  /* build a new argument stack frame */
  newfp = oldsp = xlsp;
  pusharg(NIL); /* place holder for stack frame increment */
  pusharg(fun);
  pusharg(NIL); /* place holder for argc */

  /* push the arguments */
  for (i = 0; i < argc; i++)
    pusharg(argv[i]);

  /* establish the new stack frame */
  oldsp[0] = cvfixnum((FIXTYPE)(newfp - xlfp));
  oldsp[2] = cvfixnum((FIXTYPE) argc);
  xlfp = newfp;
}

LVAL xsapplysubr P2C(subrfun, f, LVAL, args)
{
  LVAL *oldargv, val;
  int argc, oldargc;
   
  xlprot1(args); /* protect arguments while pushing */
  argc = pushargs(NIL, args);
  xlpop();       /* now they are protected since they are on the stack */

  oldargc = xlargc;
  oldargv = xlargv;
  xlargc = argc;
  xlargv = xlfp + 3;
  val = (*f)();
  xlargc = oldargc;
  xlargv = oldargv;

  /* remove the call frame */
  xlsp = xlfp;
  xlfp = xlfp - (int)getfixnum(*xlfp);
  return(val);
}

LVAL xscallsubrvec P3C(subrfun, f, int, argc, LVAL *, argv)
{
  LVAL *oldargv, val;
  int oldargc;
   
  pushargvec(NIL, argc, argv);
  oldargc = xlargc;
  oldargv = xlargv;
  xlargc = argc;
  xlargv = xlfp + 3;
  val = (*f)();
  xlargc = oldargc;
  xlargv = oldargv;

  /* remove the call frame */
  xlsp = xlfp;
  xlfp = xlfp - (int)getfixnum(*xlfp);
  return(val);
}

LVAL xsfuncall0 P1C(LVAL, fun)
{
  pushargvec(fun, 0, NULL);
  return(xlapply(0));
}

LVAL xsfuncall1 P2C(LVAL, fun, LVAL, x)
{
  pushargvec(fun, 1, &x);
  return(xlapply(1));
}

LVAL xsfuncall2 P3C(LVAL, fun, LVAL, x, LVAL, y)
{
  LVAL args[2];
  
  args[0] = x;
  args[1] = y;
  pushargvec(fun, 2, args);
  return(xlapply(2));
}

/* replicates a list n times */ 
int xsboolkey P2C(LVAL, key, int, dflt)
{
  LVAL val;
  int result = dflt;
  
  if (xlgetkeyarg(key, &val)) result = ((val != NIL) ? TRUE : FALSE);
  return(result);
}