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 939
 <INITIAL>{BIN_AND}	{ count(); return BIN_AND; }
936 940
 <INITIAL>{LOG_OR}	{ count(); return LOG_OR;  }
937 941
 <INITIAL>{BIN_OR}	{ count(); return BIN_OR;  }
942
+<INITIAL>{BIN_NOT}	{ count(); return BIN_NOT;  }
943
+<INITIAL>{BIN_XOR}	{ count(); return BIN_XOR;  }
944
+<INITIAL>{BIN_LSHIFT}	{ count(); return BIN_LSHIFT;  }
945
+<INITIAL>{BIN_RSHIFT}	{ count(); return BIN_RSHIFT;  }
938 946
 <INITIAL>{PLUS}		{ count(); return PLUS; }
939 947
 <INITIAL>{MINUS}	{ count(); return MINUS; }
940 948
 <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 2751
 
2749 2752
 
2750 2753
 rve_un_op: NOT	{ $$=RVE_LNOT_OP; }
2754
+		|  BIN_NOT 	{ $$=RVE_BNOT_OP; }
2751 2755
 		|  MINUS %prec UNARY	{ $$=RVE_UMINUS_OP; }
2752 2756
 		/* TODO: RVE_BOOL_OP, RVE_NOT_OP? */
2753 2757
 	;
... ...
@@ -2777,6 +2781,9 @@ rval_expr: rval						{ $$=$1;
2777 2781
 		| rval_expr MODULO rval_expr	{$$=mk_rve2(RVE_MOD_OP, $1, $3); }
2778 2782
 		| rval_expr BIN_OR rval_expr	{$$=mk_rve2(RVE_BOR_OP, $1,  $3); }
2779 2783
 		| rval_expr BIN_AND rval_expr	{$$=mk_rve2(RVE_BAND_OP, $1,  $3);}
2784
+		| rval_expr BIN_XOR rval_expr	{$$=mk_rve2(RVE_BXOR_OP, $1,  $3);}
2785
+		| rval_expr BIN_LSHIFT rval_expr {$$=mk_rve2(RVE_BLSHIFT_OP, $1,  $3);}
2786
+		| rval_expr BIN_RSHIFT rval_expr {$$=mk_rve2(RVE_BRSHIFT_OP, $1,  $3);}
2780 2787
 		| rval_expr rve_cmpop rval_expr %prec GT { $$=mk_rve2( $2, $1, $3);}
2781 2788
 		| rval_expr rve_equalop rval_expr %prec EQUAL_T
2782 2789
 			{ $$=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 569
 		case RVE_UMINUS_OP:
566 570
 		case RVE_BOOL_OP:
567 571
 		case RVE_LNOT_OP:
572
+		case RVE_BNOT_OP:
568 573
 		case RVE_STRLEN_OP:
569 574
 		case RVE_STREMPTY_OP:
570 575
 		case RVE_DEFINED_OP:
... ...
@@ -577,6 +582,9 @@ int rve_is_constant(struct rval_expr* rve)
577 582
 		case RVE_MOD_OP:
578 583
 		case RVE_BOR_OP:
579 584
 		case RVE_BAND_OP:
585
+		case RVE_BXOR_OP:
586
+		case RVE_BLSHIFT_OP:
587
+		case RVE_BRSHIFT_OP:
580 588
 		case RVE_LAND_OP:
581 589
 		case RVE_LOR_OP:
582 590
 		case RVE_GT_OP:
... ...
@@ -625,6 +633,7 @@ static int rve_op_unary(enum rval_expr_op op)
625 633
 		case RVE_UMINUS_OP:
626 634
 		case RVE_BOOL_OP:
627 635
 		case RVE_LNOT_OP:
636
+		case RVE_BNOT_OP:
628 637
 		case RVE_STRLEN_OP:
629 638
 		case RVE_STREMPTY_OP:
630 639
 		case RVE_DEFINED_OP:
... ...
@@ -637,6 +646,9 @@ static int rve_op_unary(enum rval_expr_op op)
637 646
 		case RVE_MOD_OP:
638 647
 		case RVE_BOR_OP:
639 648
 		case RVE_BAND_OP:
649
+		case RVE_BXOR_OP:
650
+		case RVE_BLSHIFT_OP:
651
+		case RVE_BRSHIFT_OP:
640 652
 		case RVE_LAND_OP:
641 653
 		case RVE_LOR_OP:
642 654
 		case RVE_GT_OP:
... ...
@@ -689,6 +701,7 @@ int rve_check_type(enum rval_type* type, struct rval_expr* rve,
689 701
 		case RVE_UMINUS_OP:
690 702
 		case RVE_BOOL_OP:
691 703
 		case RVE_LNOT_OP:
704
+		case RVE_BNOT_OP:
692 705
 			*type=RV_INT;
693 706
 			if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
694 707
 				if (type1==RV_STR){
... ...
@@ -707,6 +720,9 @@ int rve_check_type(enum rval_type* type, struct rval_expr* rve,
707 720
 		case RVE_MOD_OP:
708 721
 		case RVE_BOR_OP:
709 722
 		case RVE_BAND_OP:
723
+		case RVE_BXOR_OP:
724
+		case RVE_BLSHIFT_OP:
725
+		case RVE_BRSHIFT_OP:
710 726
 		case RVE_LAND_OP:
711 727
 		case RVE_LOR_OP:
712 728
 		case RVE_GT_OP:
... ...
@@ -1326,6 +1342,9 @@ inline static int int_intop1(int* res, enum rval_expr_op op, int v)
1326 1342
 		case RVE_LNOT_OP:
1327 1343
 			*res=!v;
1328 1344
 			break;
1345
+		case RVE_BNOT_OP:
1346
+			*res=~v;
1347
+			break;
1329 1348
 		default:
1330 1349
 			BUG("rv unsupported intop1 %d\n", op);
1331 1350
 			return -1;
... ...
@@ -1371,6 +1390,15 @@ inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
1371 1390
 		case RVE_BAND_OP:
1372 1391
 			*res=v1&v2;
1373 1392
 			break;
1393
+		case RVE_BXOR_OP:
1394
+			*res=v1^v2;
1395
+			break;
1396
+		case RVE_BLSHIFT_OP:
1397
+			*res=v1<<v2;
1398
+			break;
1399
+		case RVE_BRSHIFT_OP:
1400
+			*res=v1>>v2;
1401
+			break;
1374 1402
 		case RVE_LAND_OP:
1375 1403
 			*res=v1 && v2;
1376 1404
 			break;
... ...
@@ -1870,6 +1898,7 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1870 1898
 		case RVE_UMINUS_OP:
1871 1899
 		case RVE_BOOL_OP:
1872 1900
 		case RVE_LNOT_OP:
1901
+		case RVE_BNOT_OP:
1873 1902
 			if (unlikely(
1874 1903
 					(ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1875 1904
 				break;
... ...
@@ -1886,6 +1915,9 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1886 1915
 		case RVE_IPLUS_OP:
1887 1916
 		case RVE_BOR_OP:
1888 1917
 		case RVE_BAND_OP:
1918
+		case RVE_BXOR_OP:
1919
+		case RVE_BLSHIFT_OP:
1920
+		case RVE_BRSHIFT_OP:
1889 1921
 		case RVE_GT_OP:
1890 1922
 		case RVE_GTE_OP:
1891 1923
 		case RVE_LT_OP:
... ...
@@ -2154,12 +2186,16 @@ int rval_expr_eval_rvint(			   struct run_act_ctx* h,
2154 2186
 		case RVE_UMINUS_OP:
2155 2187
 		case RVE_BOOL_OP:
2156 2188
 		case RVE_LNOT_OP:
2189
+		case RVE_BNOT_OP:
2157 2190
 		case RVE_MINUS_OP:
2158 2191
 		case RVE_MUL_OP:
2159 2192
 		case RVE_DIV_OP:
2160 2193
 		case RVE_MOD_OP:
2161 2194
 		case RVE_BOR_OP:
2162 2195
 		case RVE_BAND_OP:
2196
+		case RVE_BXOR_OP:
2197
+		case RVE_BLSHIFT_OP:
2198
+		case RVE_BRSHIFT_OP:
2163 2199
 		case RVE_LAND_OP:
2164 2200
 		case RVE_LOR_OP:
2165 2201
 		case RVE_GT_OP:
... ...
@@ -2273,12 +2309,16 @@ struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2273 2309
 		case RVE_UMINUS_OP:
2274 2310
 		case RVE_BOOL_OP:
2275 2311
 		case RVE_LNOT_OP:
2312
+		case RVE_BNOT_OP:
2276 2313
 		case RVE_MINUS_OP:
2277 2314
 		case RVE_MUL_OP:
2278 2315
 		case RVE_DIV_OP:
2279 2316
 		case RVE_MOD_OP:
2280 2317
 		case RVE_BOR_OP:
2281 2318
 		case RVE_BAND_OP:
2319
+		case RVE_BXOR_OP:
2320
+		case RVE_BLSHIFT_OP:
2321
+		case RVE_BRSHIFT_OP:
2282 2322
 		case RVE_LAND_OP:
2283 2323
 		case RVE_LOR_OP:
2284 2324
 		case RVE_GT_OP:
... ...
@@ -2531,6 +2571,7 @@ struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
2531 2571
 		case RVE_UMINUS_OP:
2532 2572
 		case RVE_BOOL_OP:
2533 2573
 		case RVE_LNOT_OP:
2574
+		case RVE_BNOT_OP:
2534 2575
 		case RVE_STRLEN_OP:
2535 2576
 		case RVE_STREMPTY_OP:
2536 2577
 		case RVE_DEFINED_OP:
... ...
@@ -2573,6 +2614,9 @@ struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
2573 2614
 		case RVE_MINUS_OP:
2574 2615
 		case RVE_BOR_OP:
2575 2616
 		case RVE_BAND_OP:
2617
+		case RVE_BXOR_OP:
2618
+		case RVE_BLSHIFT_OP:
2619
+		case RVE_BRSHIFT_OP:
2576 2620
 		case RVE_LAND_OP:
2577 2621
 		case RVE_LOR_OP:
2578 2622
 		case RVE_GT_OP:
... ...
@@ -2616,6 +2660,7 @@ static int rve_op_is_assoc(enum rval_expr_op op)
2616 2660
 		case RVE_UMINUS_OP:
2617 2661
 		case RVE_BOOL_OP:
2618 2662
 		case RVE_LNOT_OP:
2663
+		case RVE_BNOT_OP:
2619 2664
 		case RVE_STRLEN_OP:
2620 2665
 		case RVE_STREMPTY_OP:
2621 2666
 		case RVE_DEFINED_OP:
... ...
@@ -2626,6 +2671,8 @@ static int rve_op_is_assoc(enum rval_expr_op op)
2626 2671
 		case RVE_DIV_OP:
2627 2672
 		case RVE_MOD_OP:
2628 2673
 		case RVE_MINUS_OP:
2674
+		case RVE_BLSHIFT_OP:
2675
+		case RVE_BRSHIFT_OP:
2629 2676
 			return 0;
2630 2677
 		case RVE_PLUS_OP:
2631 2678
 			/* the generic plus is not assoc, e.g.
... ...
@@ -2636,6 +2683,7 @@ static int rve_op_is_assoc(enum rval_expr_op op)
2636 2683
 		case RVE_MUL_OP:
2637 2684
 		case RVE_BAND_OP:
2638 2685
 		case RVE_BOR_OP:
2686
+		case RVE_BXOR_OP:
2639 2687
 			return 1;
2640 2688
 		case RVE_LAND_OP:
2641 2689
 		case RVE_LOR_OP:
... ...
@@ -2667,6 +2715,7 @@ static int rve_op_is_commutative(enum rval_expr_op op)
2667 2715
 		case RVE_UMINUS_OP:
2668 2716
 		case RVE_BOOL_OP:
2669 2717
 		case RVE_LNOT_OP:
2718
+		case RVE_BNOT_OP:
2670 2719
 		case RVE_STRLEN_OP:
2671 2720
 		case RVE_STREMPTY_OP:
2672 2721
 		case RVE_DEFINED_OP:
... ...
@@ -2677,6 +2726,8 @@ static int rve_op_is_commutative(enum rval_expr_op op)
2677 2726
 		case RVE_DIV_OP:
2678 2727
 		case RVE_MOD_OP:
2679 2728
 		case RVE_MINUS_OP:
2729
+		case RVE_BLSHIFT_OP:
2730
+		case RVE_BRSHIFT_OP:
2680 2731
 			return 0;
2681 2732
 		case RVE_PLUS_OP:
2682 2733
 			/* non commut. when diff. type 
... ...
@@ -2687,6 +2738,7 @@ static int rve_op_is_commutative(enum rval_expr_op op)
2687 2738
 		case RVE_MUL_OP:
2688 2739
 		case RVE_BAND_OP:
2689 2740
 		case RVE_BOR_OP:
2741
+		case RVE_BXOR_OP:
2690 2742
 		case RVE_LAND_OP:
2691 2743
 		case RVE_LOR_OP:
2692 2744
 		case RVE_IEQ_OP:
... ...
@@ -3712,6 +3764,7 @@ int fix_rval_expr(void* p)
3712 3764
 		case RVE_UMINUS_OP: /* unary operators */
3713 3765
 		case RVE_BOOL_OP:
3714 3766
 		case RVE_LNOT_OP:
3767
+		case RVE_BNOT_OP:
3715 3768
 		case RVE_STRLEN_OP:
3716 3769
 		case RVE_STREMPTY_OP:
3717 3770
 		case RVE_DEFINED_OP:
... ...
@@ -3726,6 +3779,9 @@ int fix_rval_expr(void* p)
3726 3779
 		case RVE_MINUS_OP:
3727 3780
 		case RVE_BOR_OP:
3728 3781
 		case RVE_BAND_OP:
3782
+		case RVE_BXOR_OP:
3783
+		case RVE_BLSHIFT_OP:
3784
+		case RVE_BRSHIFT_OP:
3729 3785
 		case RVE_LAND_OP:
3730 3786
 		case RVE_LOR_OP:
3731 3787
 		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 */