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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
/*
* implement stack functions for dc
*
* Copyright (C) 1994, 1997, 1998, 2000, 2005, 2006, 2008, 2012, 2016
* Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* This module is the only one that knows what stacks (both the
* regular evaluation stack and the named register stacks)
* look like.
*/
#include "config.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#include "dc.h"
#include "dc-proto.h"
#include "dc-regdef.h"
#include "number.h"
/* an oft-used error message: */
#define Empty_Stack do{ \
fprintf(stderr, "%s: stack empty\n", progname); \
checkferror_output(stderr); \
}while(0)
/* simple linked-list implementation suffices: */
struct dc_list {
dc_data value;
struct dc_array *array; /* opaque */
struct dc_list *link;
};
typedef struct dc_list dc_list;
/* the anonymous evaluation stack */
static dc_list *dc_stack=NULL;
/* the named register stacks */
typedef dc_list *dc_listp;
static dc_listp dc_register[DC_REGCOUNT];
/* allocate a new dc_list item */
static dc_list *
dc_alloc DC_DECLVOID()
{
dc_list *result;
result = dc_malloc(sizeof *result);
result->value.dc_type = DC_UNINITIALIZED;
result->array = NULL;
result->link = NULL;
return result;
}
/* check that there are two numbers on top of the stack,
* then call op with the popped numbers. Construct a dc_data
* value from the dc_num returned by op and push it
* on the stack.
* If the op call doesn't return DC_SUCCESS, then leave the stack
* unmodified.
*/
void
dc_binop DC_DECLARG((op, kscale))
int (*op)DC_PROTO((dc_num, dc_num, int, dc_num *)) DC_DECLSEP
int kscale DC_DECLEND
{
dc_data a;
dc_data b;
dc_data r;
if (dc_stack == NULL || dc_stack->link == NULL){
Empty_Stack;
return;
}
if (dc_stack->value.dc_type!=DC_NUMBER
|| dc_stack->link->value.dc_type!=DC_NUMBER){
fprintf(stderr, "%s: non-numeric value\n", progname);
checkferror_output(stderr);
return;
}
(void)dc_pop(&b);
(void)dc_pop(&a);
if ((*op)(a.v.number, b.v.number, kscale, &r.v.number) == DC_SUCCESS){
r.dc_type = DC_NUMBER;
dc_push(r);
dc_free_num(&a.v.number);
dc_free_num(&b.v.number);
}else{
/* op failed; restore the stack */
dc_push(a);
dc_push(b);
}
}
/* check that there are two numbers on top of the stack,
* then call op with the popped numbers. Construct two dc_data
* values from the dc_num's returned by op and push them
* on the stack.
* If the op call doesn't return DC_SUCCESS, then leave the stack
* unmodified.
*/
void
dc_binop2 DC_DECLARG((op, kscale))
int (*op)DC_PROTO((dc_num, dc_num, int, dc_num *, dc_num *)) DC_DECLSEP
int kscale DC_DECLEND
{
dc_data a;
dc_data b;
dc_data r1;
dc_data r2;
if (dc_stack == NULL || dc_stack->link == NULL){
Empty_Stack;
return;
}
if (dc_stack->value.dc_type!=DC_NUMBER
|| dc_stack->link->value.dc_type!=DC_NUMBER){
fprintf(stderr, "%s: non-numeric value\n", progname);
checkferror_output(stderr);
return;
}
(void)dc_pop(&b);
(void)dc_pop(&a);
if ((*op)(a.v.number, b.v.number, kscale,
&r1.v.number, &r2.v.number) == DC_SUCCESS){
r1.dc_type = DC_NUMBER;
dc_push(r1);
r2.dc_type = DC_NUMBER;
dc_push(r2);
dc_free_num(&a.v.number);
dc_free_num(&b.v.number);
}else{
/* op failed; restore the stack */
dc_push(a);
dc_push(b);
}
}
/* check that there are two numbers on top of the stack,
* then call dc_compare with the popped numbers.
* Return negative, zero, or positive based on the ordering
* of the two numbers.
*/
int
dc_cmpop DC_DECLVOID()
{
int result;
dc_data a;
dc_data b;
if (dc_stack == NULL || dc_stack->link == NULL){
Empty_Stack;
return 0;
}
if (dc_stack->value.dc_type!=DC_NUMBER
|| dc_stack->link->value.dc_type!=DC_NUMBER){
fprintf(stderr, "%s: non-numeric value\n", progname);
checkferror_output(stderr);
return 0;
}
(void)dc_pop(&b);
(void)dc_pop(&a);
result = dc_compare(b.v.number, a.v.number);
dc_free_num(&a.v.number);
dc_free_num(&b.v.number);
return result;
}
/* check that there are three numbers on top of the stack,
* then call op with the popped numbers. Construct a dc_data
* value from the dc_num returned by op and push it
* on the stack.
* If the op call doesn't return DC_SUCCESS, then leave the stack
* unmodified.
*/
void
dc_triop DC_DECLARG((op, kscale))
int (*op)DC_PROTO((dc_num, dc_num, dc_num, int, dc_num *)) DC_DECLSEP
int kscale DC_DECLEND
{
dc_data a;
dc_data b;
dc_data c;
dc_data r;
if (dc_stack == NULL
|| dc_stack->link == NULL
|| dc_stack->link->link == NULL){
Empty_Stack;
return;
}
if (dc_stack->value.dc_type!=DC_NUMBER
|| dc_stack->link->value.dc_type!=DC_NUMBER
|| dc_stack->link->link->value.dc_type!=DC_NUMBER){
fprintf(stderr, "%s: non-numeric value\n", progname);
checkferror_output(stderr);
return;
}
(void)dc_pop(&c);
(void)dc_pop(&b);
(void)dc_pop(&a);
if ((*op)(a.v.number, b.v.number, c.v.number,
kscale, &r.v.number) == DC_SUCCESS){
r.dc_type = DC_NUMBER;
dc_push(r);
dc_free_num(&a.v.number);
dc_free_num(&b.v.number);
dc_free_num(&c.v.number);
}else{
/* op failed; restore the stack */
dc_push(a);
dc_push(b);
dc_push(c);
}
}
/* initialize the register stacks to their initial values */
void
dc_register_init DC_DECLVOID()
{
int i;
for (i=0; i<DC_REGCOUNT; ++i)
dc_register[i] = NULL;
}
/* clear the evaluation stack */
void
dc_clear_stack DC_DECLVOID()
{
dc_list *n;
dc_list *t;
for (n=dc_stack; n!=NULL; n=t){
t = n->link;
if (n->value.dc_type == DC_NUMBER)
dc_free_num(&n->value.v.number);
else if (n->value.dc_type == DC_STRING)
dc_free_str(&n->value.v.string);
else
dc_garbage("in stack", -1);
dc_array_free(n->array);
free(n);
}
dc_stack = NULL;
}
/* push a value onto the evaluation stack */
void
dc_push DC_DECLARG((value))
dc_data value DC_DECLEND
{
dc_list *n = dc_alloc();
if (value.dc_type!=DC_NUMBER && value.dc_type!=DC_STRING)
dc_garbage("in data being pushed", -1);
n->value = value;
n->link = dc_stack;
dc_stack = n;
}
/* push a value onto the named register stack */
void
dc_register_push DC_DECLARG((stackid, value))
int stackid DC_DECLSEP
dc_data value DC_DECLEND
{
dc_list *n = dc_alloc();
stackid = regmap(stackid);
n->value = value;
n->link = dc_register[stackid];
dc_register[stackid] = n;
}
/* set *result to the value on the top of the evaluation stack */
/* The caller is responsible for duplicating the value if it
* is to be maintained as anything more than a transient identity.
*
* DC_FAIL is returned if the stack is empty (and *result unchanged),
* DC_SUCCESS is returned otherwise
*/
int
dc_top_of_stack DC_DECLARG((result))
dc_data *result DC_DECLEND
{
if (dc_stack == NULL){
Empty_Stack;
return DC_FAIL;
}
if (dc_stack->value.dc_type!=DC_NUMBER
&& dc_stack->value.dc_type!=DC_STRING)
dc_garbage("at top of stack", -1);
*result = dc_stack->value;
return DC_SUCCESS;
}
/* set *result to a dup of the value on the top of the named register stack,
* or 0 (zero) if the stack is empty */
/*
* DC_FAIL is returned if an internal bug is detected
* DC_SUCCESS is returned otherwise
*/
int
dc_register_get DC_DECLARG((regid, result))
int regid DC_DECLSEP
dc_data *result DC_DECLEND
{
dc_list *r;
regid = regmap(regid);
r = dc_register[regid];
if (r==NULL){
*result = dc_int2data(0);
}else if (r->value.dc_type==DC_UNINITIALIZED){
fprintf(stderr, "%s: BUG: register ", progname);
checkferror_output(stderr);
dc_show_id(stderr, regid, " exists but is uninitialized?\n");
return DC_FAIL;
}else{
*result = dc_dup(r->value);
}
return DC_SUCCESS;
}
/* set the top of the named register stack to the indicated value */
/* If the named stack is empty, craft a stack entry to enter the
* value into.
*/
void
dc_register_set DC_DECLARG((regid, value))
int regid DC_DECLSEP
dc_data value DC_DECLEND
{
dc_list *r;
regid = regmap(regid);
r = dc_register[regid];
if (r == NULL)
dc_register[regid] = dc_alloc();
else if (r->value.dc_type == DC_NUMBER)
dc_free_num(&r->value.v.number);
else if (r->value.dc_type == DC_STRING)
dc_free_str(&r->value.v.string);
else if (r->value.dc_type == DC_UNINITIALIZED)
;
else
dc_garbage("", regid);
dc_register[regid]->value = value;
}
/* pop from the evaluation stack
*
* DC_FAIL is returned if the stack is empty (and *result unchanged),
* DC_SUCCESS is returned otherwise
*/
int
dc_pop DC_DECLARG((result))
dc_data *result DC_DECLEND
{
dc_list *r;
r = dc_stack;
if (r==NULL || r->value.dc_type==DC_UNINITIALIZED){
Empty_Stack;
return DC_FAIL;
}
if (r->value.dc_type!=DC_NUMBER && r->value.dc_type!=DC_STRING)
dc_garbage("at top of stack", -1);
*result = r->value;
dc_stack = r->link;
dc_array_free(r->array);
free(r);
return DC_SUCCESS;
}
/* pop from the named register stack
*
* DC_FAIL is returned if the named stack is empty (and *result unchanged),
* DC_SUCCESS is returned otherwise
*/
int
dc_register_pop DC_DECLARG((stackid, result))
int stackid DC_DECLSEP
dc_data *result DC_DECLEND
{
dc_list *r;
stackid = regmap(stackid);
r = dc_register[stackid];
if (r==NULL || r->value.dc_type==DC_UNINITIALIZED){
fprintf(stderr, "%s: stack register ", progname);
checkferror_output(stderr);
dc_show_id(stderr, stackid, " is empty\n");
return DC_FAIL;
}
if (r->value.dc_type!=DC_NUMBER && r->value.dc_type!=DC_STRING)
dc_garbage(" stack", stackid);
*result = r->value;
dc_register[stackid] = r->link;
dc_array_free(r->array);
free(r);
return DC_SUCCESS;
}
/* cyclically rotate the "n" topmost elements of the stack;
* negative "n" rotates forward (topomost element becomes n-th deep)
* positive "n" rotates backward (topmost element becomes 2nd deep)
*
* If stack depth is less than "n", whole stack is rotated
* (without raising an error).
*/
void
dc_stack_rotate(int n)
{
dc_list *p; /* becomes bottom of sub-stack */
dc_list *r; /* predecessor of "p" */
int absn = n<0 ? -n : n;
/* always do nothing for empty stack or degenerate rotation depth */
if (!dc_stack || absn < 2)
return;
/* find bottom of rotation sub-stack */
r = NULL;
for (p=dc_stack; p->link && --absn>0; p=p->link)
r = p;
/* if stack has only one element, treat rotation as no-op */
if (!r)
return;
/* do the rotation, in appropriate direction */
if (n > 0) {
r->link = p->link;
p->link = dc_stack;
dc_stack = p;
} else {
dc_list *new_tos = dc_stack->link;
dc_stack->link = p->link;
p->link = dc_stack;
dc_stack = new_tos;
}
}
/* tell how many entries are currently on the evaluation stack */
int
dc_tell_stackdepth DC_DECLVOID()
{
dc_list *n;
int depth=0;
for (n=dc_stack; n!=NULL; n=n->link)
++depth;
return depth;
}
/* return the length of the indicated data value;
* if discard_p is DC_TOSS, the deallocate the value when done
*
* The definition of a datum's length is deligated to the
* appropriate module.
*/
int
dc_tell_length DC_DECLARG((value, discard_p))
dc_data value DC_DECLSEP
dc_discard discard_p DC_DECLEND
{
int length;
if (value.dc_type == DC_NUMBER){
length = dc_numlen(value.v.number);
if (discard_p == DC_TOSS)
dc_free_num(&value.v.number);
} else if (value.dc_type == DC_STRING) {
length = (int) dc_strlen(value.v.string);
if (discard_p == DC_TOSS)
dc_free_str(&value.v.string);
} else {
dc_garbage("in tell_length", -1);
/*NOTREACHED*/
length = 0; /*just to suppress spurious compiler warnings*/
}
return length;
}
/* print out all of the values on the evaluation stack */
void
dc_printall DC_DECLARG((obase))
int obase DC_DECLEND
{
dc_list *n;
for (n=dc_stack; n!=NULL; n=n->link)
dc_print(n->value, obase, DC_WITHNL, DC_KEEP);
}
/* get the current array head for the named array */
struct dc_array *
dc_get_stacked_array DC_DECLARG((array_id))
int array_id DC_DECLEND
{
dc_list *r = dc_register[regmap(array_id)];
return r == NULL ? NULL : r->array;
}
/* set the current array head for the named array */
void
dc_set_stacked_array DC_DECLARG((array_id, new_head))
int array_id DC_DECLSEP
struct dc_array *new_head DC_DECLEND
{
dc_list *r;
array_id = regmap(array_id);
r = dc_register[array_id];
if (r == NULL)
r = dc_register[array_id] = dc_alloc();
r->array = new_head;
}
/*
* Local Variables:
* mode: C
* tab-width: 4
* End:
* vi: set ts=4 :
*/
|