aboutsummaryrefslogtreecommitdiff
path: root/libbb/arith.c
blob: 04c45ec3d83daa6028d41533607035e7cbfbd20d (plain)
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
/* Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
   
   Permission is hereby granted, free of charge, to any person obtaining
   a copy of this software and associated documentation files (the
   "Software"), to deal in the Software without restriction, including
   without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense, and/or sell copies of the Software, and to
   permit persons to whom the Software is furnished to do so, subject to
   the following conditions:
   
   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/* This is my infix parser/evaluator. It is optimized for size, intended
 * as a replacement for yacc-based parsers. However, it may well be faster
 * than a comparable parser writen in yacc. The supported operators are
 * listed in #defines below. Parens, order of operations, and error handling
 * are supported. This code is threadsafe. */

/* To use the routine, call it with an expression string. It returns an
 * integer result. You will also need to define an "error" function
 * that takes printf arguments and _does not return_, or modify the code
 * to use another error mechanism. */

#include <stdlib.h>
#include <string.h>
#include "libbb.h"

typedef char operator;

#define tok_decl(prec,id) (((id)<<5)|(prec))
#define PREC(op) ((op)&0x1F)

#define TOK_LPAREN tok_decl(0,0)

#define TOK_OR tok_decl(1,0)

#define TOK_AND tok_decl(2,0)

#define TOK_BOR tok_decl(3,0)

#define TOK_BXOR tok_decl(4,0)

#define TOK_BAND tok_decl(5,0)

#define TOK_EQ tok_decl(6,0)
#define TOK_NE tok_decl(6,1)

#define TOK_LT tok_decl(7,0)
#define TOK_GT tok_decl(7,1)
#define TOK_GE tok_decl(7,2)
#define TOK_LE tok_decl(7,3)

#define TOK_LSHIFT tok_decl(8,0)
#define TOK_RSHIFT tok_decl(8,1)

#define TOK_ADD tok_decl(9,0)
#define TOK_SUB tok_decl(9,1)

#define TOK_MUL tok_decl(10,0)
#define TOK_DIV tok_decl(10,1)
#define TOK_REM tok_decl(10,2)

#define UNARYPREC 14
#define TOK_BNOT tok_decl(UNARYPREC,0)
#define TOK_NOT tok_decl(UNARYPREC,1)
#define TOK_UMINUS tok_decl(UNARYPREC,2)

#define TOK_NUM tok_decl(15,0)

#define ARITH_APPLY(op) arith_apply(op, numstack, &numstackptr)
#define NUMPTR (*numstackptr)
static short arith_apply(operator op, long *numstack, long **numstackptr)
{
	if (NUMPTR == numstack) goto err;
	if (op == TOK_UMINUS)
		NUMPTR[-1] *= -1;
	else if (op == TOK_NOT)
		NUMPTR[-1] = !(NUMPTR[-1]);
	else if (op == TOK_BNOT)
		NUMPTR[-1] = ~(NUMPTR[-1]);

	/* Binary operators */
	else {
	if (NUMPTR-1 == numstack) goto err;
	--NUMPTR;
	if (op == TOK_BOR)
		NUMPTR[-1] |= *NUMPTR;
	else if (op == TOK_OR)
		NUMPTR[-1] = *NUMPTR || NUMPTR[-1];
	else if (op == TOK_BAND)
		NUMPTR[-1] &= *NUMPTR;
	else if (op == TOK_AND)
		NUMPTR[-1] = NUMPTR[-1] && *NUMPTR;
	else if (op == TOK_EQ)
		NUMPTR[-1] = (NUMPTR[-1] == *NUMPTR);
	else if (op == TOK_NE)
		NUMPTR[-1] = (NUMPTR[-1] != *NUMPTR);
	else if (op == TOK_GE)
		NUMPTR[-1] = (NUMPTR[-1] >= *NUMPTR);
	else if (op == TOK_RSHIFT)
		NUMPTR[-1] >>= *NUMPTR;
	else if (op == TOK_LSHIFT)
		NUMPTR[-1] <<= *NUMPTR;
	else if (op == TOK_GT)
		NUMPTR[-1] = (NUMPTR[-1] > *NUMPTR);
	else if (op == TOK_LT)
		NUMPTR[-1] = (NUMPTR[-1] < *NUMPTR);
	else if (op == TOK_LE)
		NUMPTR[-1] = (NUMPTR[-1] <= *NUMPTR);
	else if (op == TOK_MUL)
		NUMPTR[-1] *= *NUMPTR;
	else if (op == TOK_DIV) {
		if(*NUMPTR==0)
			return -2;
		NUMPTR[-1] /= *NUMPTR;
		}
	else if (op == TOK_REM) {
		if(*NUMPTR==0)
			return -2;
		NUMPTR[-1] %= *NUMPTR;
		}
	else if (op == TOK_ADD)
		NUMPTR[-1] += *NUMPTR;
	else if (op == TOK_SUB)
		NUMPTR[-1] -= *NUMPTR;
	}
	return 0;
err: return(-1);
}

extern long arith (const char *startbuf, int *errcode)
{
	register char arithval;
	const char *expr = startbuf;

	operator lasttok = TOK_MUL, op;
	size_t datasizes = strlen(startbuf);
	unsigned char prec;

	long *numstack, *numstackptr;
	operator *stack = alloca(datasizes * sizeof(operator)), *stackptr = stack;

	*errcode = 0;
	numstack = alloca((datasizes/2+1)*sizeof(long)), numstackptr = numstack;

	while ((arithval = *expr)) {
		if (arithval == ' ' || arithval == '\n' || arithval == '\t')
			goto prologue;
		if ((unsigned)arithval-'0' <= 9) /* isdigit */ {
			*numstackptr++ = strtol(expr, (char **) &expr, 10);
			lasttok = TOK_NUM;
			continue;
		} if (arithval == '(') {
			*stackptr++ = TOK_LPAREN;
			lasttok = TOK_LPAREN;
			goto prologue;
		} if (arithval == ')') {
			lasttok = TOK_NUM;
			while (stackptr != stack) {
				op = *--stackptr;
				if (op == TOK_LPAREN)
					goto prologue;
				*errcode = ARITH_APPLY(op);
				if(*errcode) return *errcode;
			}
			goto err; /* Mismatched parens */
		} if (arithval == '|') {
			if (*++expr == '|')
				op = TOK_OR;
			else {
				--expr;
				op = TOK_BOR;
			}
		} else if (arithval == '&') {
			if (*++expr == '&')
				op = TOK_AND;
			else {
				--expr;
				op = TOK_BAND;
			}
		} else if (arithval == '=') {
			if (*++expr != '=') goto err; /* Unknown token */
			op = TOK_EQ;
		} else if (arithval == '!') {
			if (*++expr == '=')
				op = TOK_NE;
			else {
				--expr;
				op = TOK_NOT;
			}
		} else if (arithval == '>') {
			switch (*++expr) {
				case '=':
					op = TOK_GE;
					break;
				case '>':
					op = TOK_RSHIFT;
					break;
				default:
					--expr;
					op = TOK_GT;
			}
		} else if (arithval == '<') {
			switch (*++expr) {
				case '=':
					op = TOK_LE;
					break;
				case '<':
					op = TOK_LSHIFT;
					break;
				default:
					--expr;
					op = TOK_LT;
			}
		} else if (arithval == '*')
			op = TOK_MUL;
		else if (arithval == '/')
			op = TOK_DIV;
		else if (arithval == '%')
			op = TOK_REM;
		else if (arithval == '+') {
			if (lasttok != TOK_NUM) goto prologue; /* Unary plus */
			op = TOK_ADD;
		} else if (arithval == '-')
			op = (lasttok == TOK_NUM) ? TOK_SUB : TOK_UMINUS;
		else if (arithval == '~')
			op = TOK_BNOT;
		else goto err; /* Unknown token */

		prec = PREC(op);
		if (prec != UNARYPREC)
			while (stackptr != stack && PREC(stackptr[-1]) >= prec) {
				*errcode = ARITH_APPLY(*--stackptr);
				if(*errcode) return *errcode;
			}
		*stackptr++ = op;
		lasttok = op;
prologue: ++expr;
	} /* yay */

	while (stackptr != stack) {
		*errcode = ARITH_APPLY(*--stackptr);
		if(*errcode) return *errcode;
	}
	if (numstackptr != numstack+1) {
err: 
	    *errcode = -1;
	    return -1;
	 /* NOTREACHED */
	}

	return *numstack;
}