4 * (C) Copyright 1989-1995
14 #if !defined(_MSC_VER)
21 * The module lkeval.c contains the routines to evaluate
22 * arithmetic/numerical expressions. The functions in
23 * lkeval.c perform a recursive evaluation of the arithmetic
24 * expression read from the input text line.
25 * The expression may include binary/unary operators, brackets,
26 * symbols, labels, and constants in hexadecimal, decimal, octal
27 * and binary. Arithmetic operations are prioritized and
28 * evaluated by normal arithmetic conventions.
30 * lkeval.c contains the following functions:
37 * lkeval.c contains no local/static variables
40 /*)Function Addr_T eval()
42 * The function eval() evaluates a character string to a
46 * int c character from input string
47 * int v value of character in current radix
48 * Addr_T n evaluation value
51 * int radix current number conversion radix
54 * int digit() lkeval.c
56 * char getnb() lklex.c
57 * VOID unget() lklex.c
60 * Input test is scanned and evaluated to a
72 while ((v = digit(c, radix)) >= 0) {
80 /*)Function Addr_T expr(n)
82 * int n a firewall priority; all top
83 * level calls (from the user)
84 * should be made with n set to 0.
86 * The function expr() evaluates an expression and
90 * int c current input text character
91 * int p current operator priority
92 * Addr_T v value returned by term()
93 * Addr_T ve value returned by a
94 * recursive call to expr()
97 * char ctype[] array of character types, one per
99 * int lkerr error flag
100 * FILE * stderr c_library
103 * VOID expr() lkeval.c
104 * int fprintf() c_library
105 * int getnb() lklex.c
106 * int oprio() lkeval.c
107 * VOID term() lkeval.c
108 * VOID unget() lklex.c
112 * An expression is evaluated by scanning the input
120 register Addr_T v, ve;
123 while (ctype[c = getnb()] & BINOP) {
124 if ((p = oprio(c)) <= n)
126 if ((c == '>' || c == '<') && c != get()) {
127 fprintf(stderr, "Invalid expression");
178 /*)Function Addr_T term()
180 * The function term() evaluates a single constant
181 * or symbol value prefaced by any unary operator
182 * ( +, -, ~, ', ", >, or < ).
185 * int c current character
186 * char id[] symbol name
187 * int n value of digit in current radix
188 * int r current evaluation radix
189 * sym * sp pointer to a sym structure
190 * Addr_T v evaluation value
193 * char ctype[] array of character types, one per
195 * int lkerr error flag
198 * int digit() lkeval.c
199 * VOID expr() lkeval.c
200 * int fprintf() c_library
202 * VOID getid() lklex.c
203 * int getmap() lklex.c
204 * int getnb() lklex.c
205 * sym * lkpsym() lksym.c
206 * Addr_T symval() lksym.c
207 * VOID unget() lklex.c
210 * An arithmetic term is evaluated by scanning input text.
216 register int c, r, n;
222 if (c == '#') { c = getnb(); }
225 if (getnb() != ')') {
226 fprintf(stderr, "Missing delimiter");
238 return(getmap(-1)&0377);
242 v = (getmap(-1)&0377)<<8;
243 v |= getmap(-1)&0377;
246 v |= (getmap(-1)&0377)<<8;
250 if (c == '>' || c == '<') {
256 if (ctype[c] & DIGIT) {
291 while ((n = digit(c, r)) >= 0) {
298 if (ctype[c] & LETTER) {
300 if ((sp = lkpsym(id, 0)) == NULL) {
301 fprintf(stderr, "Undefined symbol %8s\n", id);
311 /*)Function int digit(c, r)
313 * int c digit character
314 * int r current radix
316 * The function digit() returns the value of c
317 * in the current radix r. If the c value is not
318 * a number of the current radix then a -1 is returned.
324 * char ctype[] array of character types, one per
339 if (ctype[c] & RAD16) {
340 if (c >= 'A' && c <= 'F')
341 return (c - 'A' + 10);
342 if (c >= 'a' && c <= 'f')
343 return (c - 'a' + 10);
348 if (ctype[c] & RAD10)
362 /*)Function int oprio(c)
364 * int c operator character
366 * The function oprio() returns a relative priority
367 * for all valid unary and binary operators.
386 if (c == '*' || c == '/' || c == '%')
388 if (c == '+' || c == '-')
390 if (c == '<' || c == '>')