[sr-dev] git:master: core: added some binary operators

Daniel-Constantin Mierla miconda at gmail.com
Sat May 21 12:34:00 CEST 2011


Module: sip-router
Branch: master
Commit: 508ad1c0011352e6d24234dbef70a3f6abbdc276
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=508ad1c0011352e6d24234dbef70a3f6abbdc276

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Sat May 21 12:33:00 2011 +0200

core: added some binary operators

- not, xor, left shift and right shift

---

 cfg.lex  |    8 ++++++++
 cfg.y    |    9 ++++++++-
 rvalue.c |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 rvalue.h |    4 ++++
 4 files changed, 76 insertions(+), 1 deletions(-)

diff --git a/cfg.lex b/cfg.lex
index d8586db..50c23cc 100644
--- a/cfg.lex
+++ b/cfg.lex
@@ -300,6 +300,10 @@ LOG_AND		"and"|"&&"
 BIN_AND         "&"
 LOG_OR		"or"|"||"
 BIN_OR          "|"
+BIN_NOT     "~"
+BIN_XOR     "^"
+BIN_LSHIFT     "<<"
+BIN_RSHIFT     ">>"
 PLUS	"+"
 MINUS	"-"
 MODULO	"mod"
@@ -935,6 +939,10 @@ SUBST       subst
 <INITIAL>{BIN_AND}	{ count(); return BIN_AND; }
 <INITIAL>{LOG_OR}	{ count(); return LOG_OR;  }
 <INITIAL>{BIN_OR}	{ count(); return BIN_OR;  }
+<INITIAL>{BIN_NOT}	{ count(); return BIN_NOT;  }
+<INITIAL>{BIN_XOR}	{ count(); return BIN_XOR;  }
+<INITIAL>{BIN_LSHIFT}	{ count(); return BIN_LSHIFT;  }
+<INITIAL>{BIN_RSHIFT}	{ count(); return BIN_RSHIFT;  }
 <INITIAL>{PLUS}		{ count(); return PLUS; }
 <INITIAL>{MINUS}	{ count(); return MINUS; }
 <INITIAL>{MODULO}	{ count(); return MODULO; }
diff --git a/cfg.y b/cfg.y
index dde4040..1da6b8c 100644
--- a/cfg.y
+++ b/cfg.y
@@ -567,11 +567,14 @@ extern char *finame;
 %left LOG_AND
 %left BIN_OR
 %left BIN_AND
+%left BIN_XOR
+%left BIN_LSHIFT
+%left BIN_RSHIFT
 %left EQUAL_T DIFF MATCH INTEQ INTDIFF STREQ STRDIFF
 %left GT LT GTE LTE
 %left PLUS MINUS
 %left STAR SLASH MODULO
-%right NOT UNARY
+%right NOT UNARY BIN_NOT
 %right DEFINED
 %right INTCAST STRCAST
 %left DOT
@@ -2748,6 +2751,7 @@ rval: intno			{$$=mk_rve_rval(RV_INT, (void*)$1); }
 
 
 rve_un_op: NOT	{ $$=RVE_LNOT_OP; }
+		|  BIN_NOT 	{ $$=RVE_BNOT_OP; }
 		|  MINUS %prec UNARY	{ $$=RVE_UMINUS_OP; }
 		/* TODO: RVE_BOOL_OP, RVE_NOT_OP? */
 	;
@@ -2777,6 +2781,9 @@ rval_expr: rval						{ $$=$1;
 		| rval_expr MODULO rval_expr	{$$=mk_rve2(RVE_MOD_OP, $1, $3); }
 		| rval_expr BIN_OR rval_expr	{$$=mk_rve2(RVE_BOR_OP, $1,  $3); }
 		| rval_expr BIN_AND rval_expr	{$$=mk_rve2(RVE_BAND_OP, $1,  $3);}
+		| rval_expr BIN_XOR rval_expr	{$$=mk_rve2(RVE_BXOR_OP, $1,  $3);}
+		| rval_expr BIN_LSHIFT rval_expr {$$=mk_rve2(RVE_BLSHIFT_OP, $1,  $3);}
+		| rval_expr BIN_RSHIFT rval_expr {$$=mk_rve2(RVE_BRSHIFT_OP, $1,  $3);}
 		| rval_expr rve_cmpop rval_expr %prec GT { $$=mk_rve2( $2, $1, $3);}
 		| rval_expr rve_equalop rval_expr %prec EQUAL_T
 			{ $$=mk_rve2( $2, $1, $3);}
diff --git a/rvalue.c b/rvalue.c
index 9195972..ea861e6 100644
--- a/rvalue.c
+++ b/rvalue.c
@@ -501,12 +501,16 @@ enum rval_type rve_guess_type( struct rval_expr* rve)
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 		case RVE_MINUS_OP:
 		case RVE_MUL_OP:
 		case RVE_DIV_OP:
 		case RVE_MOD_OP:
 		case RVE_BOR_OP:
 		case RVE_BAND_OP:
+		case RVE_BXOR_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
 		case RVE_GT_OP:
@@ -565,6 +569,7 @@ int rve_is_constant(struct rval_expr* rve)
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 		case RVE_STRLEN_OP:
 		case RVE_STREMPTY_OP:
 		case RVE_DEFINED_OP:
@@ -577,6 +582,9 @@ int rve_is_constant(struct rval_expr* rve)
 		case RVE_MOD_OP:
 		case RVE_BOR_OP:
 		case RVE_BAND_OP:
+		case RVE_BXOR_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
 		case RVE_GT_OP:
@@ -625,6 +633,7 @@ static int rve_op_unary(enum rval_expr_op op)
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 		case RVE_STRLEN_OP:
 		case RVE_STREMPTY_OP:
 		case RVE_DEFINED_OP:
@@ -637,6 +646,9 @@ static int rve_op_unary(enum rval_expr_op op)
 		case RVE_MOD_OP:
 		case RVE_BOR_OP:
 		case RVE_BAND_OP:
+		case RVE_BXOR_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
 		case RVE_GT_OP:
@@ -689,6 +701,7 @@ int rve_check_type(enum rval_type* type, struct rval_expr* rve,
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 			*type=RV_INT;
 			if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
 				if (type1==RV_STR){
@@ -707,6 +720,9 @@ int rve_check_type(enum rval_type* type, struct rval_expr* rve,
 		case RVE_MOD_OP:
 		case RVE_BOR_OP:
 		case RVE_BAND_OP:
+		case RVE_BXOR_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
 		case RVE_GT_OP:
@@ -1326,6 +1342,9 @@ inline static int int_intop1(int* res, enum rval_expr_op op, int v)
 		case RVE_LNOT_OP:
 			*res=!v;
 			break;
+		case RVE_BNOT_OP:
+			*res=~v;
+			break;
 		default:
 			BUG("rv unsupported intop1 %d\n", op);
 			return -1;
@@ -1371,6 +1390,15 @@ inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
 		case RVE_BAND_OP:
 			*res=v1&v2;
 			break;
+		case RVE_BXOR_OP:
+			*res=v1^v2;
+			break;
+		case RVE_BLSHIFT_OP:
+			*res=v1<<v2;
+			break;
+		case RVE_BRSHIFT_OP:
+			*res=v1>>v2;
+			break;
 		case RVE_LAND_OP:
 			*res=v1 && v2;
 			break;
@@ -1870,6 +1898,7 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 			if (unlikely(
 					(ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
 				break;
@@ -1886,6 +1915,9 @@ int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
 		case RVE_IPLUS_OP:
 		case RVE_BOR_OP:
 		case RVE_BAND_OP:
+		case RVE_BXOR_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 		case RVE_GT_OP:
 		case RVE_GTE_OP:
 		case RVE_LT_OP:
@@ -2154,12 +2186,16 @@ int rval_expr_eval_rvint(			   struct run_act_ctx* h,
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 		case RVE_MINUS_OP:
 		case RVE_MUL_OP:
 		case RVE_DIV_OP:
 		case RVE_MOD_OP:
 		case RVE_BOR_OP:
 		case RVE_BAND_OP:
+		case RVE_BXOR_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
 		case RVE_GT_OP:
@@ -2273,12 +2309,16 @@ struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 		case RVE_MINUS_OP:
 		case RVE_MUL_OP:
 		case RVE_DIV_OP:
 		case RVE_MOD_OP:
 		case RVE_BOR_OP:
 		case RVE_BAND_OP:
+		case RVE_BXOR_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
 		case RVE_GT_OP:
@@ -2531,6 +2571,7 @@ struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 		case RVE_STRLEN_OP:
 		case RVE_STREMPTY_OP:
 		case RVE_DEFINED_OP:
@@ -2573,6 +2614,9 @@ struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
 		case RVE_MINUS_OP:
 		case RVE_BOR_OP:
 		case RVE_BAND_OP:
+		case RVE_BXOR_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
 		case RVE_GT_OP:
@@ -2616,6 +2660,7 @@ static int rve_op_is_assoc(enum rval_expr_op op)
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 		case RVE_STRLEN_OP:
 		case RVE_STREMPTY_OP:
 		case RVE_DEFINED_OP:
@@ -2626,6 +2671,8 @@ static int rve_op_is_assoc(enum rval_expr_op op)
 		case RVE_DIV_OP:
 		case RVE_MOD_OP:
 		case RVE_MINUS_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 			return 0;
 		case RVE_PLUS_OP:
 			/* the generic plus is not assoc, e.g.
@@ -2636,6 +2683,7 @@ static int rve_op_is_assoc(enum rval_expr_op op)
 		case RVE_MUL_OP:
 		case RVE_BAND_OP:
 		case RVE_BOR_OP:
+		case RVE_BXOR_OP:
 			return 1;
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
@@ -2667,6 +2715,7 @@ static int rve_op_is_commutative(enum rval_expr_op op)
 		case RVE_UMINUS_OP:
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 		case RVE_STRLEN_OP:
 		case RVE_STREMPTY_OP:
 		case RVE_DEFINED_OP:
@@ -2677,6 +2726,8 @@ static int rve_op_is_commutative(enum rval_expr_op op)
 		case RVE_DIV_OP:
 		case RVE_MOD_OP:
 		case RVE_MINUS_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 			return 0;
 		case RVE_PLUS_OP:
 			/* non commut. when diff. type 
@@ -2687,6 +2738,7 @@ static int rve_op_is_commutative(enum rval_expr_op op)
 		case RVE_MUL_OP:
 		case RVE_BAND_OP:
 		case RVE_BOR_OP:
+		case RVE_BXOR_OP:
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
 		case RVE_IEQ_OP:
@@ -3712,6 +3764,7 @@ int fix_rval_expr(void* p)
 		case RVE_UMINUS_OP: /* unary operators */
 		case RVE_BOOL_OP:
 		case RVE_LNOT_OP:
+		case RVE_BNOT_OP:
 		case RVE_STRLEN_OP:
 		case RVE_STREMPTY_OP:
 		case RVE_DEFINED_OP:
@@ -3726,6 +3779,9 @@ int fix_rval_expr(void* p)
 		case RVE_MINUS_OP:
 		case RVE_BOR_OP:
 		case RVE_BAND_OP:
+		case RVE_BXOR_OP:
+		case RVE_BLSHIFT_OP:
+		case RVE_BRSHIFT_OP:
 		case RVE_LAND_OP:
 		case RVE_LOR_OP:
 		case RVE_GT_OP:
diff --git a/rvalue.h b/rvalue.h
index 8e67c2e..cc9085c 100644
--- a/rvalue.h
+++ b/rvalue.h
@@ -53,12 +53,16 @@ enum rval_expr_op{
 	RVE_UMINUS_OP, /* one member expression, returns -(val) */
 	RVE_BOOL_OP,  /* one member evaluate as bool. : (val!=0)*/
 	RVE_LNOT_OP,  /* one member evaluate as bool. : (!val)*/
+	RVE_BNOT_OP,  /* one member evaluate as binary : (~ val)*/
 	RVE_MUL_OP,   /* 2 members, returns left * right */
 	RVE_DIV_OP,   /* 2 members, returns left / right */
 	RVE_MOD_OP,   /* 2 members, returns left % right */
 	RVE_MINUS_OP, /* 2 members, returns left - right */
 	RVE_BAND_OP,  /* 2 members, returns left | right */
 	RVE_BOR_OP,   /* 2 members, returns left & right */
+	RVE_BXOR_OP,   /* 2 members, returns left XOR right */
+	RVE_BLSHIFT_OP,   /* 2 members, returns left << right */
+	RVE_BRSHIFT_OP,   /* 2 members, returns left >> right */
 	RVE_LAND_OP,  /* 2 members, returns left && right */
 	RVE_LOR_OP,   /* 2 members, returns left || right */
 	RVE_GT_OP,    /*  2 members, returns left > right */




More information about the sr-dev mailing list