174
Chapter 15: Expressions and The Expression Stack
TI
-
89 / TI
-
92 Plus Developer Guide
Not for Distribution
Beta Version January 26, 2001
B_VAR_TAG MULTIPLY_TAG A_VAR_TAG ADD_TAG. The expression
(a + b) + (c + d) becomes D_VAR_TAG C_VAR_TAG ADD_TAG B_VAR_TAG
ADD_TAG A_VAR_TAG ADD_TAG.
15.5. The Expression Stack
The simplification of an expression usually requires intermediate operations,
such as the replacement of variables with their assigned values, or the
computation of partial results that are combined to form the final result. The
Operating System uses a generalized stack called an expression stack (estack)
to perform these operations. The tokenizer also produces the external tokenized
form on the estack (expression at the highest address).
The system allocates the estack in a fixed location as an array of Quantums. The
bottom of the stack is at the lowest address, and the stack grows toward higher
addresses. This stack is described as generalized because the system allows a
variety of operations on any expression on the stack, not just the top expression.
References to expressions on the estack are via pointers defined in the system
by the C declaration:
typedef Quantum * EStackIndex;
This pointer type is used to point to tokenized expressions whether they are on
the estack or elsewhere in memory.
The system also defines a macro for accessing expressions via estack pointers.
The C declaration is:
#define ESTACK(i) (*(i))
The bottom of the estack is delimited by a global EStackIndex called
bottom_estack
. This pointer does not change and always points to an
END_OF_SEGMENT_TAG to denote the end of the stack. The topmost
occupied Quantum of the estack is accessed by a global EStackIndex called
top_estack
.
The system provides a variety of routines that perform operations on the estack.
Routines whose names begin with “push_” push something on the estack. For
example,
push_parse_text
pushes the external tokenized form of a text
expression onto the estack;
push_quantum
pushes a single Quantum value
onto the estack; and
push_between
pushes the data that resides between two
pointers onto the estack.
Routines whose names begin with “replace_” replace one or more expressions
that are on top of the stack with a new expression. For example,
replace_top2_with_sum
replaces the top two expressions on the estack with
the sum of those two expressions;
replace_top_with_reciprocal
replaces the