File: stack.c

package info (click to toggle)
rpncalc 1.20-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 244 kB
  • ctags: 101
  • sloc: ansic: 871; sed: 72; makefile: 70
file content (311 lines) | stat: -rw-r--r-- 6,263 bytes parent folder | download | duplicates (2)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/************************************************************************
 * stack.c -- part of rpncalc.c						*
 *									*
 * Refer to rpncalc.c for copyrights and license permissions.           *
 ************************************************************************/

/* $Id: stack.c,v 1.6 1997/09/12 19:02:44 david Rel $
 * $Log: stack.c,v $
 * Revision 1.6  1997/09/12 19:02:44  david
 * Clearer cleanup of linked list,
 * marking ^? explicitely as \127
 *
 * Revision 1.5  1997/01/19 20:02:00  david
 * Renamed sun_sunos[45] to sunos[45]
 *
 * Revision 1.4  1997/01/19 18:54:16  david
 * Some braces rearranged.
 *
 * Revision 1.4  1997/01/19 18:19:23  david
 * new command `char'.
 *
 * Revision 1.2  1996/09/13 20:21:29  david
 * lclint additions
 *
 * Revision 1.1  1996/07/13 20:58:08  david
 * Cosmetic changes due to linting.
 *
 * Revision 1.0  1995/12/31 18:18:55  david
 * Initial revision
 *
 * Revision 1.2  1995/11/25 21:45:20  david
 * Unlimited stack depth due to dynamical allocation of the stack
 * elements (linear linked list).
 *
 * The stack operations have now the right value in the cmd->argno field. This
 * is done even at the expense of superfluous push/pops (dup),
 * unfortunately.
 * */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <locale.h>

#ifdef sunos5
#include <ieeefp.h>
#endif
#include <limits.h>

#include "cmds.h"
#include "utils.h"
#include "stack.h"

struct elm *anchor=NULL;

int pushtostack=1;

enum BASE base=DEC;
int digits=3;			/* accuracy, number of digits */

/* Push an element on the stack */
double push(double elem)
{
  struct elm *next;

  if (pushtostack)
  {
    next=anchor;
    anchor=(struct elm *)xmalloc(sizeof(struct elm));
    anchor->value=elem;  anchor->next=next; anchor->prev=NULL;
    if (anchor->next != NULL) anchor->next->prev=anchor;
  }
  return 0.0; /* dummy */
}

/* Pop an element from the stack */
double pop(void)
{
  double value;
  struct elm *next;

  if (anchor != NULL)
  {
    value=anchor->value; next=anchor->next; 
    if (anchor != NULL) free(anchor);

    anchor=next; 
    if (anchor != NULL) anchor->prev=NULL;
  }
  else
  {
    fprintf(stderr, "pop: stack empty.\n"); value=0.0; 
    pushtostack=0;
  }
  return value;
}

/* Returns the elm-th element from the stack */
double pick(double op1)
{
  long int n, i;
  struct elm *p;
  
  n=(long int)op1; p=anchor; i=1; 
  while ((p != NULL) && (p->next != NULL) && (i<n)) { p=p->next; i++; }

  if ((i==n) && (p!=NULL)) return p->value;
  else
  {
    fprintf(stderr, "pick: element not available.\n"); 
    pushtostack=0;
    return 0.0;
  }
}

/* Drop top element */
double drop(void)
{
  struct elm *top;
  if (anchor != NULL) 
  {
    top=anchor; anchor=top->next; 
    if (anchor != NULL) anchor->prev=NULL; 
    free(top);
  }
  else
  {
    fprintf(stderr, "drop: stack empty.\n"); pushtostack=0;
  }

  return 0.0; /* dummy value */
}

/* Duplicate top element */
double dupel(double d)
{
  (void)push(d); (void)push(d);
  return 0.0; /* dummy */
}

/* Duplicate top element two times */
double dupel2(double d)
{
  (void)push(d); (void)push(d); (void)push(d);
  return 0.0; /* dummy */
}

double dupn(double n, double d)
{
  long int i;

  for (i=1; i<=(long int)n; i++) (void)push(d);
  return 0.0; /* dummy value */
}

double dropn(double n)
{
  long int i;

  for (i=1; i<=(long int)n; i++) (void)drop();
  return 0.0; /* dummy value */
}

double clear(void)
{
  struct elm *p, *t;

  p=anchor;
  while (p != NULL) 
  {
    t=p; p=p->next; 
    free(t);
  }
  anchor=NULL;
  return 0.0; /* dummy value */
}

/* Push 2nd-top element on the stack */
double over(void)
{
  (void)push(pick(2));
  return 0.0; /* dummy value */
}

/* How many elements are on the stack ? */
double depth(void)
{
  struct elm *p;
  double n;

  p=anchor; n=0;
  while (p != NULL) { p=p->next; n++; }

  return n;
}

/* Swap the 1st and 2nd element on stack */
double swap(void)
{
  double tmp1, tmp2;

  tmp1 = pop(); tmp2 = pop();
  (void)push(tmp1); (void)push(tmp2);
  return 0.0; /* dummy value */
}

/* Roll top three elements: 1 2 3 -> 3 1 2 */
double roll(void)
{
  double tmp1, tmp2, tmp3;

  tmp1 = pop(); tmp2 = pop(); tmp3 = pop();
  
  (void)push(tmp1); (void)push(tmp3); (void)push(tmp2);
  return 0.0; /* dummy value */
}

/*
 * Show the contents of the stack. Depending on the base the stack contents 
 * will be shown in dec, hex or oct notation (integers only) or in the normal
 * way: Show integers as ii and floats as ff.ffff
 *
 * Infinite results and NaN (not a number) will be displayed by using
 * isinf() and isnan().
 */
double showstack()
{
  struct elm *p;
  double value;
  short int i;

  if (anchor != NULL)
  {
    p=anchor; i=1;
    while (p->next != NULL) { p=p->next; i++; }
    while (p != NULL)
    {
      printf("%2i: ", i);
      value=p->value;  
      switch (base)
      {
        case HEX:  printf("0x%lx", (unsigned long int)value); break;
        case OCT:  printf("0%lo",  (signed long int)value); break;
	case CHAR: {
			unsigned long int  v=floor(value);
			int c=(int)v;

			printf("%03ld 0%03lo 0x%02lx ",v,v,v);
			if (isprint(c)) printf("'%c'",c);
			else if (iscntrl(c)) {
			  if (c < ' ')          printf("^%c", c+64);
			  else if (c == '\127') printf("^?");
			  else                  printf("M-^%c", c-64);
			}
			break;
		   }
        case DEC:
        default:
#ifdef sunos5
		  if (!finite(value)) {
#else
	          if (isinf(value)) {
#endif
	            printf("%cinfinity",
#ifdef sunos5
			   finite(value) == 0 ? '+' : '-');
#else
			   isinf(value) == 1 ? '+' : '-');
#endif
		  }
	          else if (isnan(value)) { printf("NaN"); }
	          else if ((floor(value) == value) && 
			  ((long int)value <= LONG_MAX)) {
		    printf("%.0f",value);
		  } else {
		    printf("%.*g", (digits+1), value);
		  }
	          break;
	  
      }
      printf("\n");
      p=p->prev; i--;
    }  
  }
  return 0.0; /* dummy value */
}

double sethex(void)
{
  base = HEX;
  return 0.0; /* dummy value */
}

double setdec(void)
{
  base = DEC;
  return 0.0; /* dummy value */
}
  
double setoct(void)
{
  base = OCT;
  return 0.0; /* dummy value */
}

double setchar(void)
{
  base = CHAR;
  return 0.0; /* dummy value */
}