Browse code

core: added some binary operators

- not, xor, left shift and right shift

Daniel-Constantin Mierla authored on 21/05/2011 10:33:00
Showing 4 changed files
... ...
@@ -300,6 +300,10 @@ LOG_AND		"and"|"&&"
300 300
 BIN_AND         "&"
301 301
 LOG_OR		"or"|"||"
302 302
 BIN_OR          "|"
303
+BIN_NOT     "~"
304
+BIN_XOR     "^"
305
+BIN_LSHIFT     "<<"
306
+BIN_RSHIFT     ">>"
303 307
 PLUS	"+"
304 308
 MINUS	"-"
305 309
 MODULO	"mod"
... ...
@@ -935,6 +939,10 @@ SUBST       subst
935 935
 <INITIAL>{BIN_AND}	{ count(); return BIN_AND; }
936 936
 <INITIAL>{LOG_OR}	{ count(); return LOG_OR;  }
937 937
 <INITIAL>{BIN_OR}	{ count(); return BIN_OR;  }
938
+<INITIAL>{BIN_NOT}	{ count(); return BIN_NOT;  }
939
+<INITIAL>{BIN_XOR}	{ count(); return BIN_XOR;  }
940
+<INITIAL>{BIN_LSHIFT}	{ count(); return BIN_LSHIFT;  }
941
+<INITIAL>{BIN_RSHIFT}	{ count(); return BIN_RSHIFT;  }
938 942
 <INITIAL>{PLUS}		{ count(); return PLUS; }
939 943
 <INITIAL>{MINUS}	{ count(); return MINUS; }
940 944
 <INITIAL>{MODULO}	{ count(); return MODULO; }
... ...
@@ -567,11 +567,14 @@ extern char *finame;
567 567
 %left LOG_AND
568 568
 %left BIN_OR
569 569
 %left BIN_AND
570
+%left BIN_XOR
571
+%left BIN_LSHIFT
572
+%left BIN_RSHIFT
570 573
 %left EQUAL_T DIFF MATCH INTEQ INTDIFF STREQ STRDIFF
571 574
 %left GT LT GTE LTE
572 575
 %left PLUS MINUS
573 576
 %left STAR SLASH MODULO
574
-%right NOT UNARY
577
+%right NOT UNARY BIN_NOT
575 578
 %right DEFINED
576 579
 %right INTCAST STRCAST
577 580
 %left DOT
... ...
@@ -2748,6 +2751,7 @@ rval: intno			{$$=mk_rve_rval(RV_INT, (void*)$1); }
2748 2748
 
2749 2749
 
2750 2750
 rve_un_op: NOT	{ $$=RVE_LNOT_OP; }
2751
+		|  BIN_NOT 	{ $$=RVE_BNOT_OP; }
2751 2752
 		|  MINUS %prec UNARY	{ $$=RVE_UMINUS_OP; }
2752 2753
 		/* TODO: RVE_BOOL_OP, RVE_NOT_OP? */
2753 2754
 	;
... ...
@@ -2777,6 +2781,9 @@ rval_expr: rval						{ $$=$1;
2777 2777
 		| rval_expr MODULO rval_expr	{$$=mk_rve2(RVE_MOD_OP, $1, $3); }
2778 2778
 		| rval_expr BIN_OR rval_expr	{$$=mk_rve2(RVE_BOR_OP, $1,  $3); }
2779 2779
 		| rval_expr BIN_AND rval_expr	{$$=mk_rve2(RVE_BAND_OP, $1,  $3);}
2780
+		| rval_expr BIN_XOR rval_expr	{$$=mk_rve2(RVE_BXOR_OP, $1,  $3);}
2781
+		| rval_expr BIN_LSHIFT rval_expr {$$=mk_rve2(RVE_BLSHIFT_OP, $1,  $3);}
2782
+		| rval_expr BIN_RSHIFT rval_expr {$$=mk_rve2(RVE_BRSHIFT_OP, $1,  $3);}
2780 2783
 		| rval_expr rve_cmpop rval_expr %prec GT { $$=mk_rve2( $2, $1, $3);}
2781 2784
 		| rval_expr rve_equalop rval_expr %prec EQUAL_T
2782 2785
 			{ $$=mk_rve2( $2, $1, $3);}
... ...
@@ -501,12 +501,16 @@ enum rval_type rve_guess_type( struct rval_expr* rve)
501 501
 		case RVE_UMINUS_OP:
502 502
 		case RVE_BOOL_OP:
503 503
 		case RVE_LNOT_OP:
504
+		case RVE_BNOT_OP:
504 505
 		case RVE_MINUS_OP:
505 506
 		case RVE_MUL_OP:
506 507
 		case RVE_DIV_OP:
507 508
 		case RVE_MOD_OP:
508 509
 		case RVE_BOR_OP:
509 510
 		case RVE_BAND_OP:
511
+		case RVE_BXOR_OP:
512
+		case RVE_BLSHIFT_OP:
513
+		case RVE_BRSHIFT_OP:
510 514
 		case RVE_LAND_OP:
511 515
 		case RVE_LOR_OP:
512 516
 		case RVE_GT_OP:
... ...
@@ -565,6 +569,7 @@ int rve_is_constant(struct rval_expr* rve)
565 565
 		case RVE_UMINUS_OP:
566 566
 		case RVE_BOOL_OP:
567 567
 		case RVE_LNOT_OP:
568
+		case RVE_BNOT_OP:
568 569
 		case RVE_STRLEN_OP:
569 570
 		case RVE_STREMPTY_OP:
570 571
 		case RVE_DEFINED_OP:
... ...
@@ -577,6 +582,9 @@ int rve_is_constant(struct rval_expr* rve)
577 577
 		case RVE_MOD_OP:
578 578
 		case RVE_BOR_OP:
579 579
 		case RVE_BAND_OP:
580
+		case RVE_BXOR_OP:
581
+		case RVE_BLSHIFT_OP:
582
+		case RVE_BRSHIFT_OP:
580 583
 		case RVE_LAND_OP:
581 584
 		case RVE_LOR_OP:
582 585
 		case RVE_GT_OP:
... ...
@@ -625,6 +633,7 @@ static int rve_op_unary(enum rval_expr_op op)
625 625
 		case RVE_UMINUS_OP:
626 626
 		case RVE_BOOL_OP:
627 627
 		case RVE_LNOT_OP:
628
+		case RVE_BNOT_OP:
628 629
 		case RVE_STRLEN_OP:
629 630
 		case RVE_STREMPTY_OP:
630 631
 		case RVE_DEFINED_OP:
... ...
@@ -637,6 +646,9 @@ static int rve_op_unary(enum rval_expr_op op)
637 637
 		case RVE_MOD_OP:
638 638
 		case RVE_BOR_OP:
639 639
 		case RVE_BAND_OP:
640
+		case RVE_BXOR_OP:
641
+		case RVE_BLSHIFT_OP:
642
+		case RVE_BRSHIFT_OP:
640 643
 		case RVE_LAND_OP:
641 644
 		case RVE_LOR_OP:
642 645
 		case RVE_GT_OP:
... ...
@@ -689,6 +701,7 @@ int rve_check_type(enum rval_type* type, struct rval_expr* rve,
689 689
 		case RVE_UMINUS_OP:
690 690
 		case RVE_BOOL_OP:
691 691
 		case RVE_LNOT_OP:
692
+		case RVE_BNOT_OP:
692 693
 			*type=RV_INT;
693 694
 			if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
694 695
 				if (type1==RV_STR){
... ...
@@ -707,6 +720,9 @@ int rve_check_type(enum rval_type* type, struct rval_expr* rve,
707 707
 		case RVE_MOD_OP:
708 708
 		case RVE_BOR_OP:
709 709
 		case RVE_BAND_OP:
710
+		case RVE_BXOR_OP:
711
+		case RVE_BLSHIFT_OP:
712
+		case RVE_BRSHIFT_OP:
710 713
 		case RVE_LAND_OP:
711 714
 		case RVE_LOR_OP:
712 715
 		case RVE_GT_OP:
... ...
@@ -1326,6 +1342,9 @@ inline static int int_intop1(int* res, enum rval_expr_op op, int v)
1326 1326
 		case RVE_LNOT_OP:
1327 1327
 			*res=!v;
1328 1328
 			break;
1329
+		case RVE_BNOT_OP:
1330
+			*res=~v;
1331
+			break;
1329 1332
 		default:
1330 1333
 			BUG("rv unsupported intop1 %d\n", op);
1331 1334
 			return -1;
... ...
@@ -1371,6 +1390,15 @@ inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
1371 1371
 		case RVE_BAND_OP:
1372 1372
 			*res=v1&v2;
1373 1373
 			break;
1374
+		case RVE_BXOR_OP:
1375
+			*res=v1^v2;
1376
+			break;
1377
+		case RVE_BLSHIFT_OP:
1378
+			*res=v1<<v2;
1379
+			break;
1380
+		case RVE_BRSHIFT_OP:
1381
+			*res=v1>>v2;
1382
+			break;
1374 1383
 		case RVE_LAND_OP:
1375 1384
 			*res=v1 && v2;
1376 1385
 			break;
... ...
@@ -1870,6 +1898,7 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1870 1870
 		case RVE_UMINUS_OP:
1871 1871
 		case RVE_BOOL_OP:
1872 1872
 		case RVE_LNOT_OP:
1873
+		case RVE_BNOT_OP:
1873 1874
 			if (unlikely(
1874 1875
 					(ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1875 1876
 				break;
... ...
@@ -1886,6 +1915,9 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1886 1886
 		case RVE_IPLUS_OP:
1887 1887
 		case RVE_BOR_OP:
1888 1888
 		case RVE_BAND_OP:
1889
+		case RVE_BXOR_OP:
1890
+		case RVE_BLSHIFT_OP:
1891
+		case RVE_BRSHIFT_OP:
1889 1892
 		case RVE_GT_OP:
1890 1893
 		case RVE_GTE_OP:
1891 1894
 		case RVE_LT_OP:
... ...
@@ -2154,12 +2186,16 @@ int rval_expr_eval_rvint(			   struct run_act_ctx* h,
2154 2154
 		case RVE_UMINUS_OP:
2155 2155
 		case RVE_BOOL_OP:
2156 2156
 		case RVE_LNOT_OP:
2157
+		case RVE_BNOT_OP:
2157 2158
 		case RVE_MINUS_OP:
2158 2159
 		case RVE_MUL_OP:
2159 2160
 		case RVE_DIV_OP:
2160 2161
 		case RVE_MOD_OP:
2161 2162
 		case RVE_BOR_OP:
2162 2163
 		case RVE_BAND_OP:
2164
+		case RVE_BXOR_OP:
2165
+		case RVE_BLSHIFT_OP:
2166
+		case RVE_BRSHIFT_OP:
2163 2167
 		case RVE_LAND_OP:
2164 2168
 		case RVE_LOR_OP:
2165 2169
 		case RVE_GT_OP:
... ...
@@ -2273,12 +2309,16 @@ struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2273 2273
 		case RVE_UMINUS_OP:
2274 2274
 		case RVE_BOOL_OP:
2275 2275
 		case RVE_LNOT_OP:
2276
+		case RVE_BNOT_OP:
2276 2277
 		case RVE_MINUS_OP:
2277 2278
 		case RVE_MUL_OP:
2278 2279
 		case RVE_DIV_OP:
2279 2280
 		case RVE_MOD_OP:
2280 2281
 		case RVE_BOR_OP:
2281 2282
 		case RVE_BAND_OP:
2283
+		case RVE_BXOR_OP:
2284
+		case RVE_BLSHIFT_OP:
2285
+		case RVE_BRSHIFT_OP:
2282 2286
 		case RVE_LAND_OP:
2283 2287
 		case RVE_LOR_OP:
2284 2288
 		case RVE_GT_OP:
... ...
@@ -2531,6 +2571,7 @@ struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
2531 2531
 		case RVE_UMINUS_OP:
2532 2532
 		case RVE_BOOL_OP:
2533 2533
 		case RVE_LNOT_OP:
2534
+		case RVE_BNOT_OP:
2534 2535
 		case RVE_STRLEN_OP:
2535 2536
 		case RVE_STREMPTY_OP:
2536 2537
 		case RVE_DEFINED_OP:
... ...
@@ -2573,6 +2614,9 @@ struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
2573 2573
 		case RVE_MINUS_OP:
2574 2574
 		case RVE_BOR_OP:
2575 2575
 		case RVE_BAND_OP:
2576
+		case RVE_BXOR_OP:
2577
+		case RVE_BLSHIFT_OP:
2578
+		case RVE_BRSHIFT_OP:
2576 2579
 		case RVE_LAND_OP:
2577 2580
 		case RVE_LOR_OP:
2578 2581
 		case RVE_GT_OP:
... ...
@@ -2616,6 +2660,7 @@ static int rve_op_is_assoc(enum rval_expr_op op)
2616 2616
 		case RVE_UMINUS_OP:
2617 2617
 		case RVE_BOOL_OP:
2618 2618
 		case RVE_LNOT_OP:
2619
+		case RVE_BNOT_OP:
2619 2620
 		case RVE_STRLEN_OP:
2620 2621
 		case RVE_STREMPTY_OP:
2621 2622
 		case RVE_DEFINED_OP:
... ...
@@ -2626,6 +2671,8 @@ static int rve_op_is_assoc(enum rval_expr_op op)
2626 2626
 		case RVE_DIV_OP:
2627 2627
 		case RVE_MOD_OP:
2628 2628
 		case RVE_MINUS_OP:
2629
+		case RVE_BLSHIFT_OP:
2630
+		case RVE_BRSHIFT_OP:
2629 2631
 			return 0;
2630 2632
 		case RVE_PLUS_OP:
2631 2633
 			/* the generic plus is not assoc, e.g.
... ...
@@ -2636,6 +2683,7 @@ static int rve_op_is_assoc(enum rval_expr_op op)
2636 2636
 		case RVE_MUL_OP:
2637 2637
 		case RVE_BAND_OP:
2638 2638
 		case RVE_BOR_OP:
2639
+		case RVE_BXOR_OP:
2639 2640
 			return 1;
2640 2641
 		case RVE_LAND_OP:
2641 2642
 		case RVE_LOR_OP:
... ...
@@ -2667,6 +2715,7 @@ static int rve_op_is_commutative(enum rval_expr_op op)
2667 2667
 		case RVE_UMINUS_OP:
2668 2668
 		case RVE_BOOL_OP:
2669 2669
 		case RVE_LNOT_OP:
2670
+		case RVE_BNOT_OP:
2670 2671
 		case RVE_STRLEN_OP:
2671 2672
 		case RVE_STREMPTY_OP:
2672 2673
 		case RVE_DEFINED_OP:
... ...
@@ -2677,6 +2726,8 @@ static int rve_op_is_commutative(enum rval_expr_op op)
2677 2677
 		case RVE_DIV_OP:
2678 2678
 		case RVE_MOD_OP:
2679 2679
 		case RVE_MINUS_OP:
2680
+		case RVE_BLSHIFT_OP:
2681
+		case RVE_BRSHIFT_OP:
2680 2682
 			return 0;
2681 2683
 		case RVE_PLUS_OP:
2682 2684
 			/* non commut. when diff. type 
... ...
@@ -2687,6 +2738,7 @@ static int rve_op_is_commutative(enum rval_expr_op op)
2687 2687
 		case RVE_MUL_OP:
2688 2688
 		case RVE_BAND_OP:
2689 2689
 		case RVE_BOR_OP:
2690
+		case RVE_BXOR_OP:
2690 2691
 		case RVE_LAND_OP:
2691 2692
 		case RVE_LOR_OP:
2692 2693
 		case RVE_IEQ_OP:
... ...
@@ -3712,6 +3764,7 @@ int fix_rval_expr(void* p)
3712 3712
 		case RVE_UMINUS_OP: /* unary operators */
3713 3713
 		case RVE_BOOL_OP:
3714 3714
 		case RVE_LNOT_OP:
3715
+		case RVE_BNOT_OP:
3715 3716
 		case RVE_STRLEN_OP:
3716 3717
 		case RVE_STREMPTY_OP:
3717 3718
 		case RVE_DEFINED_OP:
... ...
@@ -3726,6 +3779,9 @@ int fix_rval_expr(void* p)
3726 3726
 		case RVE_MINUS_OP:
3727 3727
 		case RVE_BOR_OP:
3728 3728
 		case RVE_BAND_OP:
3729
+		case RVE_BXOR_OP:
3730
+		case RVE_BLSHIFT_OP:
3731
+		case RVE_BRSHIFT_OP:
3729 3732
 		case RVE_LAND_OP:
3730 3733
 		case RVE_LOR_OP:
3731 3734
 		case RVE_GT_OP:
... ...
@@ -53,12 +53,16 @@ enum rval_expr_op{
53 53
 	RVE_UMINUS_OP, /* one member expression, returns -(val) */
54 54
 	RVE_BOOL_OP,  /* one member evaluate as bool. : (val!=0)*/
55 55
 	RVE_LNOT_OP,  /* one member evaluate as bool. : (!val)*/
56
+	RVE_BNOT_OP,  /* one member evaluate as binary : (~ val)*/
56 57
 	RVE_MUL_OP,   /* 2 members, returns left * right */
57 58
 	RVE_DIV_OP,   /* 2 members, returns left / right */
58 59
 	RVE_MOD_OP,   /* 2 members, returns left % right */
59 60
 	RVE_MINUS_OP, /* 2 members, returns left - right */
60 61
 	RVE_BAND_OP,  /* 2 members, returns left | right */
61 62
 	RVE_BOR_OP,   /* 2 members, returns left & right */
63
+	RVE_BXOR_OP,   /* 2 members, returns left XOR right */
64
+	RVE_BLSHIFT_OP,   /* 2 members, returns left << right */
65
+	RVE_BRSHIFT_OP,   /* 2 members, returns left >> right */
62 66
 	RVE_LAND_OP,  /* 2 members, returns left && right */
63 67
 	RVE_LOR_OP,   /* 2 members, returns left || right */
64 68
 	RVE_GT_OP,    /*  2 members, returns left > right */