summaryrefslogtreecommitdiff
path: root/sdas/linksrc/lkeval.c
blob: e4662c956d28c696625b70d4255cad68e9c6a2bd (plain) (blame)
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
/* lkeval.c */

/*
 *  Copyright (C) 1989-2009  Alan R. Baldwin
 *
 *  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 of the License, 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/>.
 *
 *
 * Alan R. Baldwin
 * 721 Berkeley St.
 * Kent, Ohio  44240
 */

#include "aslink.h"

/*)Module	lkeval.c
 *
 *	The module lkeval.c contains the routines to evaluate
 *	arithmetic/numerical expressions.  The functions in
 *	lkeval.c perform a recursive evaluation of the arithmetic
 *	expression read from the input text line.
 *	The expression may include binary/unary operators, brackets,
 *	symbols, labels, and constants in hexadecimal, decimal, octal
 *	and binary.  Arithmetic operations are prioritized and
 *	evaluated by normal arithmetic conventions.
 *
 *	lkeval.c contains the following functions:
 *		int	digit()
 *		a_uint	eval()
 *		a_uint	expr()
 *		int	oprio()
 *		a_uint	term()
 *
 *	lkeval.c contains no local/static variables
 */

/*)Function	a_uint	eval()
 *
 *	The function eval() evaluates a character string to a
 *	numerical value.
 *
 *	Notes about the arithmetic:
 *		The coding emulates X-Bit unsigned
 *		arithmetic operations.  This allows
 *		program compilation without regard to the
 *		intrinsic integer length of the host
 *		machine.
 *
 *	local variables:
 *		int	c		character from input string
 *		int	v		value of character in current radix
 *		a_uint	n		evaluation value
 *
 *	global variables:
 *		int	radix		current number conversion radix
 *
 *	functions called:
 *		int	digit()		lkeval.c
 *		int	get()		lklex.c
 *		int	getnb()		lklex.c
 *		VOID	unget()		lklex.c
 *
 *	side effects:
 *		Input test is scanned and evaluated to a
 *		numerical value.
 */

a_uint
eval(void)
{
	int c, v;
	a_uint n;

	c = getnb();
	n = 0;
	while ((v = digit(c, radix)) >= 0) {
		n = n*radix + v;
		c = get();
	}
	unget(c);
	return (n & a_mask);
}

/*)Function	a_uint	expr(n)
 *
 *		int	n		a firewall priority; all top
 *					level calls (from the user)
 *					should be made with n set to 0.
 *
 *	The function expr() evaluates an expression and
 *	returns the value.
 *
 *	Notes about the arithmetic:
 *		The coding emulates X-Bit unsigned
 *		arithmetic operations.  This allows
 *		program compilation without regard to the
 *		intrinsic integer length of the host
 *		machine.
 *
 *	local variables:
 *		int	c		current input text character
 *		int	p		current operator priority
 *		a_uint	v		value returned by term()
 *		a_uint	ve		value returned by a
 *					recursive call to expr()
 *
 *	global variables:
 *		char	ctype[]		array of character types, one per
 *					ASCII character
 *		int	lkerr		error flag
 *		FILE *	stderr		c_library
 *
 *	functions called:
 *		VOID	expr()		lkeval.c
 *		int	fprintf()	c_library
 *		int	getnb()		lklex.c
 *		int	oprio()		lkeval.c
 *		VOID	term()		lkeval.c
 *		VOID	unget()		lklex.c
 *
 *
 *	side effects:
 *		An expression is evaluated by scanning the input
 *		text string.
 */

a_uint
expr (int n)
{
	int c, p;
	a_uint v, ve;

	v = term();
	while (ctype[c = getnb()] & BINOP) {
		if ((p = oprio(c)) <= n)
			break;
		if ((c == '>' || c == '<') && c != get()) {
			fprintf(stderr, "Invalid expression");
			lkerr++;
			return(v);
		}
		ve = expr(p);

		/*
		 * X-Bit Unsigned Arithmetic
		 */
		v  &= a_mask;
		ve &= a_mask;

		if (c == '+') {
			v += ve;
		} else
		if (c == '-') {
			v -= ve;
		} else {
			switch (c) {

			case '*':
				v *= ve;
				break;

			case '/':
				if (ve == 0) {
					v = 0;
				} else {
					v /= ve;
				}
				break;

			case '&':
				v &= ve;
				break;

			case '|':
				v |= ve;
				break;

			case '%':
				if (ve == 0) {
					v = 0;
				} else {
					v %= ve;
				}
				break;

			case '^':
				v ^= ve;
				break;

			case '<':
				v <<= ve;
				break;

			case '>':
				v >>= ve;
				break;
			}
		}
		v = (v & a_mask);
	}
	unget(c);
	return(v);
}

/*)Function	a_uint	term()
 *
 *	The function term() evaluates a single constant
 *	or symbol value prefaced by any unary operator
 *	( +, -, ~, ', ", >, or < ).
 *
 *	Notes about the arithmetic:
 *		The coding emulates X-Bit unsigned
 *		arithmetic operations.  This allows
 *		program compilation without regard to the
 *		intrinsic integer length of the host
 *		machine.
 *
 *	local variables:
 *		int	c		current character
 *		char	id[]		symbol name
 *		int	n		value of digit in current radix
 *		int	r		current evaluation radix
 *		sym *	sp		pointer to a sym structure
 *		a_uint	v		evaluation value
 *
 *	global variables:
 *		char	ctype[]		array of character types, one per
 *					ASCII character
 *		int	lkerr		error flag
 *
 *	functions called:
 *		int	digit()		lkeval.c
 *		VOID	expr()		lkeval.c
 *		int	fprintf()	c_library
 *		int	get()		lklex.c
 *		VOID	getid()		lklex.c
 *		int	getmap()	lklex.c
 *		int	getnb()		lklex.c
 *		sym *	lkpsym()	lksym.c
 *		a_uint	symval()	lksym.c
 *		VOID	unget()		lklex.c
 *
 *	side effects:
 *		An arithmetic term is evaluated by scanning input text.
 */

a_uint
term(void)
{
	int c, r, n;
	a_uint v;
	struct sym *sp;
	char id[NCPS];

	c = getnb();
	if (c == '#') { c = getnb(); }
	if (c == '(') {
		v = expr(0);
		if (getnb() != ')') {
			fprintf(stderr, "Missing delimiter");
			lkerr++;
		}
		return(v);
	}
	if (c == '-') {
		return(~expr(100)+1);
	}
	if (c == '~') {
		return(~expr(100));
	}
	if (c == '\'') {
		v = getmap(-1)&0377;
		c = get();
		if (c != '\'') { unget(c); }
		return(v);
	}
	if (c == '\"') {
		if (hilo) {
			v  = (getmap(-1)&0377)<<8;
			v |=  getmap(-1)&0377;
		} else {
			v  =  getmap(-1)&0377;
			v |= (getmap(-1)&0377)<<8;
		}
		c = get();
		if (c != '\"') { unget(c); }
		return(v & a_mask);
	}
	if (c == '>' || c == '<') {
		v = expr(100);
		if (c == '>')
			v >>= 8;
		return(v&0377);
	}
	if (ctype[c] & DIGIT) {
		r = 10;
		if (c == '0') {
			c = get();
			switch (c) {
			case 'b':
			case 'B':
				r = 2;
				c = get();
				break;
			case '@':
			case 'o':
			case 'O':
			case 'q':
			case 'Q':
				r = 8;
				c = get();
				break;
			case 'd':
			case 'D':
				r = 10;
				c = get();
				break;
			case 'h':
			case 'H':
			case 'x':
			case 'X':
				r = 16;
				c = get();
				break;
			default:
				break;
			}
		}
		v = 0;
		while ((n = digit(c, r)) >= 0) {
			v = r*v + n;
			c = get();
		}
		unget(c);
		return(v & a_mask);
	}
	if (ctype[c] & LETTER) {
		getid(id, c);
		if ((sp = lkpsym(id, 0)) == NULL) {
			fprintf(stderr, "Undefined symbol %s\n", id);
			lkerr++;
			return(0);
		} else {
			return(symval(sp));
		}
	}
	fprintf(stderr, "Unknown operator %c\n", c);
	lkerr++;
	return(0);
}

/*)Function	int	digit(c, r)
 *
 *		int	c		digit character
 *		int	r		current radix
 *
 *	The function digit() returns the value of c
 *	in the current radix r.  If the c value is not
 *	a number of the current radix then a -1 is returned.
 *
 *	local variables:
 *		none
 *
 *	global variables:
 *		char	ctype[]		array of character types, one per
 *					ASCII character
 *
 *	functions called:
 *		none
 *
 *	side effects:
 *		none
 */

int
digit(int c, int r)
{
	if (r == 16) {
		if (ctype[c] & RAD16) {
			if (c >= 'A' && c <= 'F')
				return (c - 'A' + 10);
			if (c >= 'a' && c <= 'f')
				return (c - 'a' + 10);
			return (c - '0');
		}
	} else
	if (r == 10) {
		if (ctype[c] & RAD10)
			return (c - '0');
	} else
	if (r == 8) {
		if (ctype[c] & RAD8)
			return (c - '0');
	} else
	if (r == 2) {
		if (ctype[c] & RAD2)
			return (c - '0');
	}
	return (-1);
}

/*)Function	int	oprio(c)
 *
 *		int	c		operator character
 *
 *	The function oprio() returns a relative priority
 *	for all valid unary and binary operators.
 *
 *	local variables:
 *		none
 *
 *	global variables:
 *		none
 *
 *	functions called:
 *		none
 *
 *	side effects:
 *		none
 */

int
oprio(int c)
{
	if (c == '*' || c == '/' || c == '%')
		return (10);
	if (c == '+' || c == '-')
		return (7);
	if (c == '<' || c == '>')
		return (5);
	if (c == '^')
		return (4);
	if (c == '&')
		return (3);
	if (c == '|')
		return (1);
	return (0);
}