[SR-Dev] git:master: core: type casts support in the script

Andrei Pelinescu-Onciul andrei at iptel.org
Tue May 5 21:14:44 CEST 2009


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

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Tue May  5 19:43:18 2009 +0200

core: type casts support in the script

Support for casts added: (int) and (str).
E.g.: (int)$v ; (str)$v+"test".

---

 NEWS    |    5 +++--
 cfg.lex |    4 ++++
 cfg.y   |    5 +++++
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index ed1ab96..460d70c 100644
--- a/NEWS
+++ b/NEWS
@@ -6,13 +6,14 @@ $Id$
 sip-router changes
 
 core:
+  - type casts operators: (int), (str).
   - new operators eq, ne for string compares and ieq, ine for interger 
     compares. The names are not yet final (use them at your own risk).
     Future version might use ==/!= only for ints (ieq/ine) and eq/ne for
     strings (under debate).
     They are almost equivalent to == or !=, but they force the conversion 
-	of their operands (eq to string and ieq to int), allowing among other
-	things better type checking on startup and more optimizations.
+    of their operands (eq to string and ieq to int), allowing among other
+    things better type checking on startup and more optimizations.
     Non equiv. examples: 0 == "" (true) is not equivalent to 0 eq ""
     (false: it evaluates to "0" eq ""). "a" ieq "b" (true: (int)"a" is 0
      and (int)"b" is 0) is not equivalent to "a" == "b" (false).
diff --git a/cfg.lex b/cfg.lex
index b599d13..167fc95 100644
--- a/cfg.lex
+++ b/cfg.lex
@@ -252,6 +252,8 @@ STREQ	eq
 INTEQ	ieq
 STRDIFF	ne
 INTDIFF	ine
+INTCAST	\(int\)
+STRCAST \(str\)
 
 /* Attribute specification */
 ATTR_MARK   "%"
@@ -783,6 +785,8 @@ EAT_ABLE	[\ \t\b\r]
 <INITIAL>{INTEQ}	{ count(); return INTEQ; }
 <INITIAL>{STRDIFF}	{ count(); return STRDIFF; }
 <INITIAL>{INTDIFF}	{ count(); return INTDIFF; }
+<INITIAL>{INTCAST}	{ count(); return INTCAST; }
+<INITIAL>{STRCAST}	{ count(); return STRCAST; }
 
 <INITIAL>{SELECT_MARK}  { count(); state = SELECT_S; BEGIN(SELECT); return SELECT_MARK; }
 <SELECT>{ID}		{ count(); addstr(&s_buf, yytext, yyleng);
diff --git a/cfg.y b/cfg.y
index 5897914..8480434 100644
--- a/cfg.y
+++ b/cfg.y
@@ -494,6 +494,7 @@ static int case_check_default(struct case_stms* stms);
 %left STAR SLASH
 %right NOT
 %right DEFINED
+%right INTCAST STRCAST
 %left DOT
 
 /* no precedence, they use () */
@@ -2269,6 +2270,8 @@ rval_expr: rval						{ $$=$1;
 											*/
 									}
 		| rve_un_op %prec NOT rval_expr	{$$=mk_rve1($1, $2); }
+		| INTCAST rval_expr				{$$=mk_rve1(RVE_INT_OP, $2); }
+		| STRCAST rval_expr				{$$=mk_rve1(RVE_STR_OP, $2); }
 		| rval_expr PLUS rval_expr		{$$=mk_rve2(RVE_PLUS_OP, $1, $3); }
 		| rval_expr MINUS rval_expr		{$$=mk_rve2(RVE_MINUS_OP, $1, $3); }
 		| rval_expr STAR rval_expr		{$$=mk_rve2(RVE_MUL_OP, $1, $3); }
@@ -2285,6 +2288,8 @@ rval_expr: rval						{ $$=$1;
 		| STREMPTY LPAREN rval_expr RPAREN {$$=mk_rve1(RVE_STREMPTY_OP, $3);}
 		| DEFINED rval_expr				{ $$=mk_rve1(RVE_DEFINED_OP, $2);}
 		| rve_un_op %prec NOT error		{ $$=0; yyerror("bad expression"); }
+		| INTCAST error					{ $$=0; yyerror("bad expression"); }
+		| STRCAST error					{ $$=0; yyerror("bad expression"); }
 		| rval_expr PLUS error			{ yyerror("bad expression"); }
 		| rval_expr MINUS error			{ yyerror("bad expression"); }
 		| rval_expr STAR error			{ yyerror("bad expression"); }




More information about the sr-dev mailing list