summaryrefslogtreecommitdiff
path: root/expr.c
blob: 6cd8237584b314ae803fca0374a77709198b766d (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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*
    expr - Simple, expression evaluator

    Copyright (C) 2003  Ian Cowburn (ianc@noddybox.demon.co.uk)

    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 2 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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    -------------------------------------------------------------------------

    Expands an expression.

*/
static const char id[]="$Id$";

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* ---------------------------------------- MACROS
*/
#define TRUE		1
#define FALSE		0

#define TYPE_OPERAND	0
#define TYPE_VARIABLE	1
#define TYPE_OPERATOR	2	/* This acts as a base for operator tokens */
#define TYPE_LPAREN	3
#define TYPE_RPAREN	4
#define TYPE_DIVIDE	5
#define TYPE_MULTIPLY	6
#define TYPE_ADD	7
#define TYPE_SUBTRACT	8
#define TYPE_NOT	9
#define TYPE_AND	10
#define TYPE_OR		11
#define TYPE_XOR	12
#define TYPE_UNARY_PLUS	13
#define TYPE_UNARY_NEG	14
#define TYPE_SHIFTL	15
#define TYPE_SHIFTR	16
#define TYPE_EQUALITY	17
#define TYPE_BOOL_AND	18
#define TYPE_BOOL_OR	19
#define TYPE_MODULUS	20
#define TYPE_LT		21
#define TYPE_GT		22
#define TYPE_LTEQ	23
#define TYPE_GTEQ	24
#define TYPE_NOTEQUAL	25

#define SYM_LPAREN	"("
#define SYM_RPAREN	")"
#define SYM_DIVIDE	"/"
#define SYM_MULTIPLY	"*"
#define SYM_ADD		"+"
#define SYM_SUBTRACT	"-"
#define SYM_NOT		"~"
#define SYM_AND		"&"
#define SYM_OR		"|"
#define SYM_XOR		"^"
#define SYM_UNARY_PLUS	"+"
#define SYM_UNARY_NEG	"-"
#define SYM_SHIFTL	"<<"
#define SYM_SHIFTR	">>"
#define SYM_EQUALITY	"=="
#define SYM_BOOL_AND	"&&"
#define SYM_BOOL_OR	"||"
#define SYM_MODULUS	"%"
#define SYM_LT		"<"
#define SYM_GT		">"
#define SYM_LTEQ	"<="
#define SYM_GTEQ	">="
#define SYM_NOTEQUAL	"!="

#define OPERATOR_CHARS	"()/*+-~&|^<>!=%"

#define IS_OPERATOR_TYPE(a)	((a)>=TYPE_OPERATOR)

#define IS_PAREN(c)		((c)==SYM_LPAREN[0] || (c)==SYM_RPAREN[0])

#define IS_OPERATOR(c)		(strchr(OPERATOR_CHARS,(c)))

#define IS_OPERAND_END(c)	((c)==' ' || (c)=='\t' || IS_OPERATOR(c))

/* ---------------------------------------- TYPES
*/

typedef struct stk
    {
    int		token;
    int		priority;
    int		is_unary;
    char	*text;
    struct stk	*next;
    } Stack;


typedef enum {OpBinary, OpPretendUnary, OpUnary} OpType;

typedef struct
    {
    const char	*symbol;
    int		priority;
    int		token;
    OpType	type;
    int		allow_unary;
    } Operator;


typedef enum {OpOk, OpSyntaxError, OpUnknown} OpError;


/* ---------------------------------------- GLOBALS
*/
static char		error[1024];

static int		(*expand)(void *client_data,
				  const char *p,
				  long *value);

static void		*client=NULL;

static const Operator	op_info[]=
    {
	/* Unary ops - must be first in list.  Note that LPAREN is treated
	   as a unary as it must be preceeded by another operator, rather
	   than an operand.
	*/
    	{SYM_NOT,	9,	TYPE_NOT,	OpUnary,	TRUE},
    	{SYM_UNARY_PLUS,9,	TYPE_UNARY_PLUS,OpUnary,	TRUE},
    	{SYM_UNARY_NEG,	9,	TYPE_UNARY_NEG,	OpUnary,	TRUE},
    	{SYM_LPAREN,	99,	TYPE_LPAREN,	OpPretendUnary,	TRUE},

	/* Binary ops and don't care ops
	*/
    	{SYM_RPAREN,	99,	TYPE_RPAREN,	OpBinary,	FALSE},
    	{SYM_SHIFTL,	6,	TYPE_SHIFTL,	OpBinary,	TRUE},
    	{SYM_SHIFTR,	6,	TYPE_SHIFTR,	OpBinary,	TRUE},
    	{SYM_MULTIPLY,	5,	TYPE_MULTIPLY,	OpBinary,	TRUE},
    	{SYM_DIVIDE,	5,	TYPE_DIVIDE,	OpBinary,	TRUE},
    	{SYM_MODULUS,	5,	TYPE_MODULUS,	OpBinary,	TRUE},
    	{SYM_ADD,	4,	TYPE_ADD,	OpBinary,	TRUE},
    	{SYM_SUBTRACT,	4,	TYPE_SUBTRACT,	OpBinary,	TRUE},
    	{SYM_EQUALITY,	1,	TYPE_EQUALITY,	OpBinary,	TRUE},
    	{SYM_NOTEQUAL,	1,	TYPE_NOTEQUAL,	OpBinary,	TRUE},
    	{SYM_LTEQ,	1,	TYPE_LTEQ,	OpBinary,	TRUE},
    	{SYM_GTEQ,	1,	TYPE_GTEQ,	OpBinary,	TRUE},
    	{SYM_LT,	1,	TYPE_LT,	OpBinary,	TRUE},
    	{SYM_GT,	1,	TYPE_GT,	OpBinary,	TRUE},
    	{SYM_BOOL_AND,	0,	TYPE_BOOL_AND,	OpBinary,	TRUE},
    	{SYM_AND,	0,	TYPE_AND,	OpBinary,	TRUE},
    	{SYM_BOOL_OR,	0,	TYPE_BOOL_OR,	OpBinary,	TRUE},
    	{SYM_OR,	0,	TYPE_OR,	OpBinary,	TRUE},
    	{SYM_XOR,	0,	TYPE_XOR,	OpBinary,	TRUE},

	{0,0,0}
    };


/* ---------------------------------------- PRIVATE FUNCTIONS
*/

/* malloc wrapper
*/
static void *Malloc(size_t size)
{
    void *p=malloc(size);

    if (!p)
    {
    	fprintf(stderr,"malloc failed\n");
	exit(EXIT_FAILURE);
    }

    return p;
}


/* Copy a string
*/
static char *CopyStr(const char *p)
{
    char *new=Malloc(strlen(p)+1);

    return strcpy(new,p);
}


/* Empty a stack
*/
static Stack *ClearStack(Stack *stack)
{
    while(stack)
    {
    	Stack *tmp=stack;

	stack=stack->next;
	free(tmp->text);
	free(tmp);
    }

    return stack;
}


/* Push onto the stack
*/
static Stack *Push(Stack *stack, int token, int priority, 
		   int is_unary, const char *text)
{
    Stack *e=Malloc(sizeof *e);

    e->text=CopyStr(text);
    e->priority=priority;
    e->token=token;
    e->is_unary=is_unary;
    e->next=stack;
    stack=e;

    return stack;
}


/* Destroy the top element on the stack
*/
static Stack *Pop(Stack *stack)
{
    if (stack)
    {
	Stack *tmp=stack;

	stack=stack->next;

    	free(tmp->text);
	free(tmp);
    }

    return stack;
}


/* Debug to dump a stack non-destructively
static void Dump(const char *name, Stack *stack)
{
    while (stack)
    {
    	printf("%-10s: Type: %-2d  is_unary: %d  text: %s\n",
			name,stack->token,stack->is_unary,stack->text);

	stack=stack->next;
    }
}
*/


/* Find the symbol for an operator
*/
static const char *ToString(int token)
{
    static char buff[32];
    int f;

    strcpy(buff,"UNKNOWN");

    for(f=0;op_info[f].symbol;f++)
    {
    	if (op_info[f].token==token)
	{
	    strcpy(buff,op_info[f].symbol);
	    return buff;
	}
    }

    return "UNKNOWN";
}


/* Search the operator info
*/
static OpError FindOp(const char *p, int prev_was_op, const Operator **op)
{
    int f;
    int found=FALSE;

    *op=NULL;

    for(f=0;op_info[f].symbol;f++)
    {
    	if (strncmp(op_info[f].symbol,p,strlen(op_info[f].symbol))==0)
	{
	    found=TRUE;

	    if ((prev_was_op && op_info[f].type!=OpBinary) ||
		(!prev_was_op && op_info[f].type==OpBinary))
	    	
	    {
		*op=op_info+f;
		return OpOk;
	    }
	}
    }

    if (found)
    	return OpSyntaxError;
    else
    	return OpUnknown;
}


static Stack *ToPostfix(const char *expr)
{
    Stack *stack=NULL;
    Stack *output=NULL;
    char buff[1024];
    char *p;
    int prev_was_op=TRUE;

    /* Parse the infix expression into postfix
    */
    while(*expr)
    {
    	if (IS_OPERATOR(*expr))
	{
	    /* Found an operator - parse it
	    */
	    const Operator *op;
	    OpError op_err;
	    
	    op_err=FindOp(expr,prev_was_op,&op);

	    /* Unknown operator in expression
	    */
	    if (!op)
	    {
		if (op_err==OpSyntaxError)
		    sprintf(error,"Syntax error with operator %c",*expr);
		else
		    sprintf(error,"Unknown operator %c",*expr);

	    	stack=ClearStack(stack);
	    	output=ClearStack(output);

		return NULL;
	    }

	    /* Special handling for closing parenthesis
	    */
	    if (op->token==TYPE_RPAREN)
	    {
	    	while(stack && stack->token!=TYPE_LPAREN)
		{
		    output=Push(output,
		    		stack->token,
				stack->priority,
				stack->is_unary,
				stack->text);
		    stack=Pop(stack);
		}

		/* Syntax error in expression
		*/
		if (!stack)
		{
		    sprintf(error,"Missing %s",SYM_LPAREN);

		    stack=ClearStack(stack);
		    output=ClearStack(output);

		    return NULL;
		}

		/* Remove the opening bracket and continue
		*/
		stack=Pop(stack);
	    }
	    else
	    {
	    	/* Output the stack until an operator of lower precedence is
		   found
		*/
		if (op->type==OpUnary)
		{
		    while(stack && !IS_PAREN(stack->text[0]) &&
			    !stack->is_unary &&
				(stack->priority >= op->priority))
		    {
			output=Push(output,
				    stack->token,
				    stack->priority,
				    stack->is_unary,
				    stack->text);

			stack=Pop(stack);
		    }
		}
		else
		{
		    while(stack && !IS_PAREN(stack->text[0]) &&
				(stack->priority >= op->priority))
		    {
			output=Push(output,
				    stack->token,
				    stack->priority,
				    stack->is_unary,
				    stack->text);

			stack=Pop(stack);
		    }
		}

		stack=Push(stack,
			   op->token,
			   op->priority,
			   op->type==OpUnary,
			   op->symbol);
	    }

	    /* Move past token
	    */
	    expr+=strlen(op->symbol);

	    prev_was_op=op->allow_unary;
	}
	else
	{
	    if (!isspace(*expr))
	    {
		/* Found an operand - parse and store the operand
		*/
		p=buff;

		while(*expr && !IS_OPERAND_END(*expr))
		{
		    *p++=*expr++;
		}

		*p=0;

		/* Note that operands are marked as unary just to chose one over
		   the other branch in EvalPostfix() - no other reason
		*/
		output=Push(output,TYPE_OPERAND,0,TRUE,buff);

		prev_was_op=FALSE;
	    }
	    else
	    {
	    	expr++;
	    }
	}
    }

    while(stack)
    {
	output=Push(output,
		    stack->token,
		    stack->priority,
		    stack->is_unary,
		    stack->text);

	stack=Pop(stack);
    }

    /* Return the stack
    */
    return output;
}


static int EvalPostfix(Stack **stack, long *result)
{
    Stack *expr;
    int token;

    expr=*stack;

    if (!expr)
    {
	sprintf(error,"Called with empty postfix stack");
	return FALSE;
    }

    token=expr->token;

    if (expr->is_unary)
    {
    	if (token==TYPE_OPERAND)
	{
	    char *endp;

	    *result=strtol(expr->text,&endp,0);

	    if (!(expr->text[0] && *endp==0))
	    {
		int ok=FALSE;

		if (expand)
		    ok=expand(client,expr->text,result);

		if (!ok)
		{
		    sprintf(error,"Undefined variable '%s'",expr->text);
		    return FALSE;
		}
	    }

	    expr=Pop(expr);
	}
	else if (token==TYPE_VARIABLE)
	{
	    int ok=FALSE;

	    if (expand)
	    	ok=expand(client,expr->text,result);

	    if (!ok)
	    {
	    	sprintf(error,"Undefined variable '%s'",expr->text);
		return FALSE;
	    }

	    expr=Pop(expr);
	}
	else
	{
	    long val;

	    expr=Pop(expr);

	    if (!expr || !EvalPostfix(&expr,&val))
	    {
		sprintf(error,"Operator '%s' expects an argument",
				ToString(token));
		*stack=expr;
		return FALSE;
	    }

	    switch(token)
	    {
		case TYPE_NOT:
		    *result=~val;
		    break;

		case TYPE_UNARY_PLUS:
		    *result=+val;
		    break;

		case TYPE_UNARY_NEG:
		    *result=-val;
		    break;

		default:
		    sprintf(error,"Execpected unary token '%s' processed",
		    			ToString(token));
		    *stack=expr;
		    return FALSE;
		    break;
	    }
	}
    }
    else
    {
	long left,right;

	expr=Pop(expr);

	if (!expr || !EvalPostfix(&expr,&right))
	{
	    sprintf(error,"Operator '%s' expects two arguments",
	    			ToString(token));
	    *stack=expr;
	    return FALSE;
	}

	if (!expr || !EvalPostfix(&expr,&left))
	{
	    sprintf(error,"Operator '%s' expects two arguments",
	    			ToString(token));
	    *stack=expr;
	    return FALSE;
	}

	switch(token)
	{
	    case TYPE_DIVIDE:
		*result=left/right;
		break;

	    case TYPE_MULTIPLY:
		*result=left*right;
		break;

	    case TYPE_MODULUS:
		*result=left%right;
		break;

	    case TYPE_ADD:
		*result=left+right;
		break;

	    case TYPE_SUBTRACT:
		*result=left-right;
		break;

	    case TYPE_AND:
		*result=left&right;
		break;

	    case TYPE_OR:
		*result=left|right;
		break;

	    case TYPE_BOOL_AND:
		*result=left&&right;
		break;

	    case TYPE_BOOL_OR:
		*result=left||right;
		break;

	    case TYPE_XOR:
		*result=left^right;
		break;

	    case TYPE_SHIFTL:
		if (right<0)
		{
		    sprintf(error,"Cannot shift left by a "
		    			"negative number (%ld)",right);
		    *stack=expr;
		    return FALSE;
		}

		*result=left<<right;
		break;

	    case TYPE_SHIFTR:
		if (right<0)
		{
		    sprintf(error,"Cannot shift right by a "
		    			"negative number (%ld)",right);
		    *stack=expr;
		    return FALSE;
		}

		*result=left>>right;
		break;

	    case TYPE_EQUALITY:
		*result=left==right;
		break;

	    case TYPE_NOTEQUAL:
		*result=left!=right;
		break;

	    case TYPE_LT:
		*result=left<right;
		break;

	    case TYPE_GT:
		*result=left>right;
		break;

	    case TYPE_LTEQ:
		*result=left<=right;
		break;

	    case TYPE_GTEQ:
		*result=left>=right;
		break;

	    default:
		sprintf(error,"Unexpected binary token '%s' processed",
				ToString(token));
		*stack=expr;
		return FALSE;
		break;
	}
    }

    *stack=expr;

    return TRUE;
}


/* ---------------------------------------- INTERFACES
*/

int ExprEval(const char *expr, long *result,
	     int (*expand_var)(void *client_data, const char *p, long *value),
	     void *client_data)
{
    Stack *output;
    int ret;

    expand=expand_var;
    client=client_data;

    output=ToPostfix(expr);

    if (!output)
	return FALSE;

    ret=EvalPostfix(&output,result);

    ClearStack(output);

    return ret;
}


/* Gets a readable reason for an error from ExprEval() or ExprParse.
*/
const char *ExprError(void)
{
    return error;
}


/* END OF FILE */