|
Packit |
70b277 |
/*
|
|
Packit |
70b277 |
* implement stack functions for dc
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* Copyright (C) 1994, 1997, 1998, 2000, 2005, 2006, 2008, 2012, 2016
|
|
Packit |
70b277 |
* Free Software Foundation, Inc.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
70b277 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
70b277 |
* the Free Software Foundation; either version 3, or (at your option)
|
|
Packit |
70b277 |
* any later version.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* This program is distributed in the hope that it will be useful,
|
|
Packit |
70b277 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
70b277 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
70b277 |
* GNU General Public License for more details.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
70b277 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* This module is the only one that knows what stacks (both the
|
|
Packit |
70b277 |
* regular evaluation stack and the named register stacks)
|
|
Packit |
70b277 |
* look like.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#include "config.h"
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
#include <stdio.h>
|
|
Packit |
70b277 |
#ifdef HAVE_STDLIB_H
|
|
Packit |
70b277 |
# include <stdlib.h>
|
|
Packit |
70b277 |
#endif
|
|
Packit |
70b277 |
#include "dc.h"
|
|
Packit |
70b277 |
#include "dc-proto.h"
|
|
Packit |
70b277 |
#include "dc-regdef.h"
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* an oft-used error message: */
|
|
Packit |
70b277 |
#define Empty_Stack fprintf(stderr, "%s: stack empty\n", progname)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* simple linked-list implementation suffices: */
|
|
Packit |
70b277 |
struct dc_list {
|
|
Packit |
70b277 |
dc_data value;
|
|
Packit |
70b277 |
struct dc_array *array; /* opaque */
|
|
Packit |
70b277 |
struct dc_list *link;
|
|
Packit |
70b277 |
};
|
|
Packit |
70b277 |
typedef struct dc_list dc_list;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* the anonymous evaluation stack */
|
|
Packit |
70b277 |
static dc_list *dc_stack=NULL;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* the named register stacks */
|
|
Packit |
70b277 |
typedef dc_list *dc_listp;
|
|
Packit |
70b277 |
static dc_listp dc_register[DC_REGCOUNT];
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* allocate a new dc_list item */
|
|
Packit |
70b277 |
static dc_list *
|
|
Packit |
70b277 |
dc_alloc DC_DECLVOID()
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *result;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
result = dc_malloc(sizeof *result);
|
|
Packit |
70b277 |
result->value.dc_type = DC_UNINITIALIZED;
|
|
Packit |
70b277 |
result->array = NULL;
|
|
Packit |
70b277 |
result->link = NULL;
|
|
Packit |
70b277 |
return result;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* check that there are two numbers on top of the stack,
|
|
Packit |
70b277 |
* then call op with the popped numbers. Construct a dc_data
|
|
Packit |
70b277 |
* value from the dc_num returned by op and push it
|
|
Packit |
70b277 |
* on the stack.
|
|
Packit |
70b277 |
* If the op call doesn't return DC_SUCCESS, then leave the stack
|
|
Packit |
70b277 |
* unmodified.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_binop DC_DECLARG((op, kscale))
|
|
Packit |
70b277 |
int (*op)DC_PROTO((dc_num, dc_num, int, dc_num *)) DC_DECLSEP
|
|
Packit |
70b277 |
int kscale DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_data a;
|
|
Packit |
70b277 |
dc_data b;
|
|
Packit |
70b277 |
dc_data r;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (dc_stack == NULL || dc_stack->link == NULL){
|
|
Packit |
70b277 |
Empty_Stack;
|
|
Packit |
70b277 |
return;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
if (dc_stack->value.dc_type!=DC_NUMBER
|
|
Packit |
70b277 |
|| dc_stack->link->value.dc_type!=DC_NUMBER){
|
|
Packit |
70b277 |
fprintf(stderr, "%s: non-numeric value\n", progname);
|
|
Packit |
70b277 |
return;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
(void)dc_pop(&b);
|
|
Packit |
70b277 |
(void)dc_pop(&a);
|
|
Packit |
70b277 |
if ((*op)(a.v.number, b.v.number, kscale, &r.v.number) == DC_SUCCESS){
|
|
Packit |
70b277 |
r.dc_type = DC_NUMBER;
|
|
Packit |
70b277 |
dc_push(r);
|
|
Packit |
70b277 |
dc_free_num(&a.v.number);
|
|
Packit |
70b277 |
dc_free_num(&b.v.number);
|
|
Packit |
70b277 |
}else{
|
|
Packit |
70b277 |
/* op failed; restore the stack */
|
|
Packit |
70b277 |
dc_push(a);
|
|
Packit |
70b277 |
dc_push(b);
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* check that there are two numbers on top of the stack,
|
|
Packit |
70b277 |
* then call op with the popped numbers. Construct two dc_data
|
|
Packit |
70b277 |
* values from the dc_num's returned by op and push them
|
|
Packit |
70b277 |
* on the stack.
|
|
Packit |
70b277 |
* If the op call doesn't return DC_SUCCESS, then leave the stack
|
|
Packit |
70b277 |
* unmodified.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_binop2 DC_DECLARG((op, kscale))
|
|
Packit |
70b277 |
int (*op)DC_PROTO((dc_num, dc_num, int, dc_num *, dc_num *)) DC_DECLSEP
|
|
Packit |
70b277 |
int kscale DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_data a;
|
|
Packit |
70b277 |
dc_data b;
|
|
Packit |
70b277 |
dc_data r1;
|
|
Packit |
70b277 |
dc_data r2;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (dc_stack == NULL || dc_stack->link == NULL){
|
|
Packit |
70b277 |
Empty_Stack;
|
|
Packit |
70b277 |
return;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
if (dc_stack->value.dc_type!=DC_NUMBER
|
|
Packit |
70b277 |
|| dc_stack->link->value.dc_type!=DC_NUMBER){
|
|
Packit |
70b277 |
fprintf(stderr, "%s: non-numeric value\n", progname);
|
|
Packit |
70b277 |
return;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
(void)dc_pop(&b);
|
|
Packit |
70b277 |
(void)dc_pop(&a);
|
|
Packit |
70b277 |
if ((*op)(a.v.number, b.v.number, kscale,
|
|
Packit |
70b277 |
&r1.v.number, &r2.v.number) == DC_SUCCESS){
|
|
Packit |
70b277 |
r1.dc_type = DC_NUMBER;
|
|
Packit |
70b277 |
dc_push(r1);
|
|
Packit |
70b277 |
r2.dc_type = DC_NUMBER;
|
|
Packit |
70b277 |
dc_push(r2);
|
|
Packit |
70b277 |
dc_free_num(&a.v.number);
|
|
Packit |
70b277 |
dc_free_num(&b.v.number);
|
|
Packit |
70b277 |
}else{
|
|
Packit |
70b277 |
/* op failed; restore the stack */
|
|
Packit |
70b277 |
dc_push(a);
|
|
Packit |
70b277 |
dc_push(b);
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* check that there are two numbers on top of the stack,
|
|
Packit |
70b277 |
* then call dc_compare with the popped numbers.
|
|
Packit |
70b277 |
* Return negative, zero, or positive based on the ordering
|
|
Packit |
70b277 |
* of the two numbers.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
int
|
|
Packit |
70b277 |
dc_cmpop DC_DECLVOID()
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
int result;
|
|
Packit |
70b277 |
dc_data a;
|
|
Packit |
70b277 |
dc_data b;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (dc_stack == NULL || dc_stack->link == NULL){
|
|
Packit |
70b277 |
Empty_Stack;
|
|
Packit |
70b277 |
return 0;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
if (dc_stack->value.dc_type!=DC_NUMBER
|
|
Packit |
70b277 |
|| dc_stack->link->value.dc_type!=DC_NUMBER){
|
|
Packit |
70b277 |
fprintf(stderr, "%s: non-numeric value\n", progname);
|
|
Packit |
70b277 |
return 0;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
(void)dc_pop(&b);
|
|
Packit |
70b277 |
(void)dc_pop(&a);
|
|
Packit |
70b277 |
result = dc_compare(b.v.number, a.v.number);
|
|
Packit |
70b277 |
dc_free_num(&a.v.number);
|
|
Packit |
70b277 |
dc_free_num(&b.v.number);
|
|
Packit |
70b277 |
return result;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* check that there are three numbers on top of the stack,
|
|
Packit |
70b277 |
* then call op with the popped numbers. Construct a dc_data
|
|
Packit |
70b277 |
* value from the dc_num returned by op and push it
|
|
Packit |
70b277 |
* on the stack.
|
|
Packit |
70b277 |
* If the op call doesn't return DC_SUCCESS, then leave the stack
|
|
Packit |
70b277 |
* unmodified.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_triop DC_DECLARG((op, kscale))
|
|
Packit |
70b277 |
int (*op)DC_PROTO((dc_num, dc_num, dc_num, int, dc_num *)) DC_DECLSEP
|
|
Packit |
70b277 |
int kscale DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_data a;
|
|
Packit |
70b277 |
dc_data b;
|
|
Packit |
70b277 |
dc_data c;
|
|
Packit |
70b277 |
dc_data r;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (dc_stack == NULL
|
|
Packit |
70b277 |
|| dc_stack->link == NULL
|
|
Packit |
70b277 |
|| dc_stack->link->link == NULL){
|
|
Packit |
70b277 |
Empty_Stack;
|
|
Packit |
70b277 |
return;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
if (dc_stack->value.dc_type!=DC_NUMBER
|
|
Packit |
70b277 |
|| dc_stack->link->value.dc_type!=DC_NUMBER
|
|
Packit |
70b277 |
|| dc_stack->link->link->value.dc_type!=DC_NUMBER){
|
|
Packit |
70b277 |
fprintf(stderr, "%s: non-numeric value\n", progname);
|
|
Packit |
70b277 |
return;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
(void)dc_pop(&c);
|
|
Packit |
70b277 |
(void)dc_pop(&b);
|
|
Packit |
70b277 |
(void)dc_pop(&a);
|
|
Packit |
70b277 |
if ((*op)(a.v.number, b.v.number, c.v.number,
|
|
Packit |
70b277 |
kscale, &r.v.number) == DC_SUCCESS){
|
|
Packit |
70b277 |
r.dc_type = DC_NUMBER;
|
|
Packit |
70b277 |
dc_push(r);
|
|
Packit |
70b277 |
dc_free_num(&a.v.number);
|
|
Packit |
70b277 |
dc_free_num(&b.v.number);
|
|
Packit |
70b277 |
dc_free_num(&c.v.number);
|
|
Packit |
70b277 |
}else{
|
|
Packit |
70b277 |
/* op failed; restore the stack */
|
|
Packit |
70b277 |
dc_push(a);
|
|
Packit |
70b277 |
dc_push(b);
|
|
Packit |
70b277 |
dc_push(c);
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* initialize the register stacks to their initial values */
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_register_init DC_DECLVOID()
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
int i;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
for (i=0; i
|
|
Packit |
70b277 |
dc_register[i] = NULL;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* clear the evaluation stack */
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_clear_stack DC_DECLVOID()
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *n;
|
|
Packit |
70b277 |
dc_list *t;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
for (n=dc_stack; n!=NULL; n=t){
|
|
Packit |
70b277 |
t = n->link;
|
|
Packit |
70b277 |
if (n->value.dc_type == DC_NUMBER)
|
|
Packit |
70b277 |
dc_free_num(&n->value.v.number);
|
|
Packit |
70b277 |
else if (n->value.dc_type == DC_STRING)
|
|
Packit |
70b277 |
dc_free_str(&n->value.v.string);
|
|
Packit |
70b277 |
else
|
|
Packit |
70b277 |
dc_garbage("in stack", -1);
|
|
Packit |
70b277 |
dc_array_free(n->array);
|
|
Packit |
70b277 |
free(n);
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
dc_stack = NULL;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* push a value onto the evaluation stack */
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_push DC_DECLARG((value))
|
|
Packit |
70b277 |
dc_data value DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *n = dc_alloc();
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (value.dc_type!=DC_NUMBER && value.dc_type!=DC_STRING)
|
|
Packit |
70b277 |
dc_garbage("in data being pushed", -1);
|
|
Packit |
70b277 |
n->value = value;
|
|
Packit |
70b277 |
n->link = dc_stack;
|
|
Packit |
70b277 |
dc_stack = n;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* push a value onto the named register stack */
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_register_push DC_DECLARG((stackid, value))
|
|
Packit |
70b277 |
int stackid DC_DECLSEP
|
|
Packit |
70b277 |
dc_data value DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *n = dc_alloc();
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
stackid = regmap(stackid);
|
|
Packit |
70b277 |
n->value = value;
|
|
Packit |
70b277 |
n->link = dc_register[stackid];
|
|
Packit |
70b277 |
dc_register[stackid] = n;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* set *result to the value on the top of the evaluation stack */
|
|
Packit |
70b277 |
/* The caller is responsible for duplicating the value if it
|
|
Packit |
70b277 |
* is to be maintained as anything more than a transient identity.
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* DC_FAIL is returned if the stack is empty (and *result unchanged),
|
|
Packit |
70b277 |
* DC_SUCCESS is returned otherwise
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
int
|
|
Packit |
70b277 |
dc_top_of_stack DC_DECLARG((result))
|
|
Packit |
70b277 |
dc_data *result DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
if (dc_stack == NULL){
|
|
Packit |
70b277 |
Empty_Stack;
|
|
Packit |
70b277 |
return DC_FAIL;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
if (dc_stack->value.dc_type!=DC_NUMBER
|
|
Packit |
70b277 |
&& dc_stack->value.dc_type!=DC_STRING)
|
|
Packit |
70b277 |
dc_garbage("at top of stack", -1);
|
|
Packit |
70b277 |
*result = dc_stack->value;
|
|
Packit |
70b277 |
return DC_SUCCESS;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* set *result to a dup of the value on the top of the named register stack,
|
|
Packit |
70b277 |
* or 0 (zero) if the stack is empty */
|
|
Packit |
70b277 |
/*
|
|
Packit |
70b277 |
* DC_FAIL is returned if an internal bug is detected
|
|
Packit |
70b277 |
* DC_SUCCESS is returned otherwise
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
int
|
|
Packit |
70b277 |
dc_register_get DC_DECLARG((regid, result))
|
|
Packit |
70b277 |
int regid DC_DECLSEP
|
|
Packit |
70b277 |
dc_data *result DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *r;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
regid = regmap(regid);
|
|
Packit |
70b277 |
r = dc_register[regid];
|
|
Packit |
70b277 |
if (r==NULL){
|
|
Packit |
70b277 |
*result = dc_int2data(0);
|
|
Packit |
70b277 |
}else if (r->value.dc_type==DC_UNINITIALIZED){
|
|
Packit |
70b277 |
fprintf(stderr, "%s: BUG: register ", progname);
|
|
Packit |
70b277 |
dc_show_id(stderr, regid, " exists but is uninitialized?\n");
|
|
Packit |
70b277 |
return DC_FAIL;
|
|
Packit |
70b277 |
}else{
|
|
Packit |
70b277 |
*result = dc_dup(r->value);
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
return DC_SUCCESS;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* set the top of the named register stack to the indicated value */
|
|
Packit |
70b277 |
/* If the named stack is empty, craft a stack entry to enter the
|
|
Packit |
70b277 |
* value into.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_register_set DC_DECLARG((regid, value))
|
|
Packit |
70b277 |
int regid DC_DECLSEP
|
|
Packit |
70b277 |
dc_data value DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *r;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
regid = regmap(regid);
|
|
Packit |
70b277 |
r = dc_register[regid];
|
|
Packit |
70b277 |
if (r == NULL)
|
|
Packit |
70b277 |
dc_register[regid] = dc_alloc();
|
|
Packit |
70b277 |
else if (r->value.dc_type == DC_NUMBER)
|
|
Packit |
70b277 |
dc_free_num(&r->value.v.number);
|
|
Packit |
70b277 |
else if (r->value.dc_type == DC_STRING)
|
|
Packit |
70b277 |
dc_free_str(&r->value.v.string);
|
|
Packit |
70b277 |
else if (r->value.dc_type == DC_UNINITIALIZED)
|
|
Packit |
70b277 |
;
|
|
Packit |
70b277 |
else
|
|
Packit |
70b277 |
dc_garbage("", regid);
|
|
Packit |
70b277 |
dc_register[regid]->value = value;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* pop from the evaluation stack
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* DC_FAIL is returned if the stack is empty (and *result unchanged),
|
|
Packit |
70b277 |
* DC_SUCCESS is returned otherwise
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
int
|
|
Packit |
70b277 |
dc_pop DC_DECLARG((result))
|
|
Packit |
70b277 |
dc_data *result DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *r;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
r = dc_stack;
|
|
Packit |
70b277 |
if (r==NULL || r->value.dc_type==DC_UNINITIALIZED){
|
|
Packit |
70b277 |
Empty_Stack;
|
|
Packit |
70b277 |
return DC_FAIL;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
if (r->value.dc_type!=DC_NUMBER && r->value.dc_type!=DC_STRING)
|
|
Packit |
70b277 |
dc_garbage("at top of stack", -1);
|
|
Packit |
70b277 |
*result = r->value;
|
|
Packit |
70b277 |
dc_stack = r->link;
|
|
Packit |
70b277 |
dc_array_free(r->array);
|
|
Packit |
70b277 |
free(r);
|
|
Packit |
70b277 |
return DC_SUCCESS;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* pop from the named register stack
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* DC_FAIL is returned if the named stack is empty (and *result unchanged),
|
|
Packit |
70b277 |
* DC_SUCCESS is returned otherwise
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
int
|
|
Packit |
70b277 |
dc_register_pop DC_DECLARG((stackid, result))
|
|
Packit |
70b277 |
int stackid DC_DECLSEP
|
|
Packit |
70b277 |
dc_data *result DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *r;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
stackid = regmap(stackid);
|
|
Packit |
70b277 |
r = dc_register[stackid];
|
|
Packit |
70b277 |
if (r==NULL || r->value.dc_type==DC_UNINITIALIZED){
|
|
Packit |
70b277 |
fprintf(stderr, "%s: stack register ", progname);
|
|
Packit |
70b277 |
dc_show_id(stderr, stackid, " is empty\n");
|
|
Packit |
70b277 |
return DC_FAIL;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
if (r->value.dc_type!=DC_NUMBER && r->value.dc_type!=DC_STRING)
|
|
Packit |
70b277 |
dc_garbage(" stack", stackid);
|
|
Packit |
70b277 |
*result = r->value;
|
|
Packit |
70b277 |
dc_register[stackid] = r->link;
|
|
Packit |
70b277 |
dc_array_free(r->array);
|
|
Packit |
70b277 |
free(r);
|
|
Packit |
70b277 |
return DC_SUCCESS;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* cyclically rotate the "n" topmost elements of the stack;
|
|
Packit |
70b277 |
* negative "n" rotates forward (topomost element becomes n-th deep)
|
|
Packit |
70b277 |
* positive "n" rotates backward (topmost element becomes 2nd deep)
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* If stack depth is less than "n", whole stack is rotated
|
|
Packit |
70b277 |
* (without raising an error).
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_stack_rotate(int n)
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *p; /* becomes bottom of sub-stack */
|
|
Packit |
70b277 |
dc_list *r; /* predecessor of "p" */
|
|
Packit |
70b277 |
int absn = n<0 ? -n : n;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* always do nothing for empty stack or degenerate rotation depth */
|
|
Packit |
70b277 |
if (!dc_stack || absn < 2)
|
|
Packit |
70b277 |
return;
|
|
Packit |
70b277 |
/* find bottom of rotation sub-stack */
|
|
Packit |
70b277 |
r = NULL;
|
|
Packit |
70b277 |
for (p=dc_stack; p->link && --absn>0; p=p->link)
|
|
Packit |
70b277 |
r = p;
|
|
Packit |
70b277 |
/* if stack has only one element, treat rotation as no-op */
|
|
Packit |
70b277 |
if (!r)
|
|
Packit |
70b277 |
return;
|
|
Packit |
70b277 |
/* do the rotation, in appropriate direction */
|
|
Packit |
70b277 |
if (n > 0) {
|
|
Packit |
70b277 |
r->link = p->link;
|
|
Packit |
70b277 |
p->link = dc_stack;
|
|
Packit |
70b277 |
dc_stack = p;
|
|
Packit |
70b277 |
} else {
|
|
Packit |
70b277 |
dc_list *new_tos = dc_stack->link;
|
|
Packit |
70b277 |
dc_stack->link = p->link;
|
|
Packit |
70b277 |
p->link = dc_stack;
|
|
Packit |
70b277 |
dc_stack = new_tos;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* tell how many entries are currently on the evaluation stack */
|
|
Packit |
70b277 |
int
|
|
Packit |
70b277 |
dc_tell_stackdepth DC_DECLVOID()
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *n;
|
|
Packit |
70b277 |
int depth=0;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
for (n=dc_stack; n!=NULL; n=n->link)
|
|
Packit |
70b277 |
++depth;
|
|
Packit |
70b277 |
return depth;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* return the length of the indicated data value;
|
|
Packit |
70b277 |
* if discard_p is DC_TOSS, the deallocate the value when done
|
|
Packit |
70b277 |
*
|
|
Packit |
70b277 |
* The definition of a datum's length is deligated to the
|
|
Packit |
70b277 |
* appropriate module.
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
int
|
|
Packit |
70b277 |
dc_tell_length DC_DECLARG((value, discard_p))
|
|
Packit |
70b277 |
dc_data value DC_DECLSEP
|
|
Packit |
70b277 |
dc_discard discard_p DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
int length;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
if (value.dc_type == DC_NUMBER){
|
|
Packit |
70b277 |
length = dc_numlen(value.v.number);
|
|
Packit |
70b277 |
if (discard_p == DC_TOSS)
|
|
Packit |
70b277 |
dc_free_num(&value.v.number);
|
|
Packit |
70b277 |
} else if (value.dc_type == DC_STRING) {
|
|
Packit |
70b277 |
length = (int) dc_strlen(value.v.string);
|
|
Packit |
70b277 |
if (discard_p == DC_TOSS)
|
|
Packit |
70b277 |
dc_free_str(&value.v.string);
|
|
Packit |
70b277 |
} else {
|
|
Packit |
70b277 |
dc_garbage("in tell_length", -1);
|
|
Packit |
70b277 |
/*NOTREACHED*/
|
|
Packit |
70b277 |
length = 0; /*just to suppress spurious compiler warnings*/
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
return length;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* print out all of the values on the evaluation stack */
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_printall DC_DECLARG((obase))
|
|
Packit |
70b277 |
int obase DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *n;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
for (n=dc_stack; n!=NULL; n=n->link)
|
|
Packit |
70b277 |
dc_print(n->value, obase, DC_WITHNL, DC_KEEP);
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* get the current array head for the named array */
|
|
Packit |
70b277 |
struct dc_array *
|
|
Packit |
70b277 |
dc_get_stacked_array DC_DECLARG((array_id))
|
|
Packit |
70b277 |
int array_id DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *r = dc_register[regmap(array_id)];
|
|
Packit |
70b277 |
return r == NULL ? NULL : r->array;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* set the current array head for the named array */
|
|
Packit |
70b277 |
void
|
|
Packit |
70b277 |
dc_set_stacked_array DC_DECLARG((array_id, new_head))
|
|
Packit |
70b277 |
int array_id DC_DECLSEP
|
|
Packit |
70b277 |
struct dc_array *new_head DC_DECLEND
|
|
Packit |
70b277 |
{
|
|
Packit |
70b277 |
dc_list *r;
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
array_id = regmap(array_id);
|
|
Packit |
70b277 |
r = dc_register[array_id];
|
|
Packit |
70b277 |
if (r == NULL)
|
|
Packit |
70b277 |
r = dc_register[array_id] = dc_alloc();
|
|
Packit |
70b277 |
r->array = new_head;
|
|
Packit |
70b277 |
}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/*
|
|
Packit |
70b277 |
* Local Variables:
|
|
Packit |
70b277 |
* mode: C
|
|
Packit |
70b277 |
* tab-width: 4
|
|
Packit |
70b277 |
* End:
|
|
Packit |
70b277 |
* vi: set ts=4 :
|
|
Packit |
70b277 |
*/
|