Browse code

core: type casts support in the script

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

Andrei Pelinescu-Onciul authored on 05/05/2009 17:43:18
Showing 3 changed files
... ...
@@ -6,13 +6,14 @@ $Id$
6 6
 sip-router changes
7 7
 
8 8
 core:
9
+  - type casts operators: (int), (str).
9 10
   - new operators eq, ne for string compares and ieq, ine for interger 
10 11
     compares. The names are not yet final (use them at your own risk).
11 12
     Future version might use ==/!= only for ints (ieq/ine) and eq/ne for
12 13
     strings (under debate).
13 14
     They are almost equivalent to == or !=, but they force the conversion 
14
-	of their operands (eq to string and ieq to int), allowing among other
15
-	things better type checking on startup and more optimizations.
15
+    of their operands (eq to string and ieq to int), allowing among other
16
+    things better type checking on startup and more optimizations.
16 17
     Non equiv. examples: 0 == "" (true) is not equivalent to 0 eq ""
17 18
     (false: it evaluates to "0" eq ""). "a" ieq "b" (true: (int)"a" is 0
18 19
      and (int)"b" is 0) is not equivalent to "a" == "b" (false).
... ...
@@ -252,6 +252,8 @@ STREQ	eq
252 252
 INTEQ	ieq
253 253
 STRDIFF	ne
254 254
 INTDIFF	ine
255
+INTCAST	\(int\)
256
+STRCAST \(str\)
255 257
 
256 258
 /* Attribute specification */
257 259
 ATTR_MARK   "%"
... ...
@@ -783,6 +785,8 @@ EAT_ABLE	[\ \t\b\r]
783 783
 <INITIAL>{INTEQ}	{ count(); return INTEQ; }
784 784
 <INITIAL>{STRDIFF}	{ count(); return STRDIFF; }
785 785
 <INITIAL>{INTDIFF}	{ count(); return INTDIFF; }
786
+<INITIAL>{INTCAST}	{ count(); return INTCAST; }
787
+<INITIAL>{STRCAST}	{ count(); return STRCAST; }
786 788
 
787 789
 <INITIAL>{SELECT_MARK}  { count(); state = SELECT_S; BEGIN(SELECT); return SELECT_MARK; }
788 790
 <SELECT>{ID}		{ count(); addstr(&s_buf, yytext, yyleng);
... ...
@@ -494,6 +494,7 @@ static int case_check_default(struct case_stms* stms);
494 494
 %left STAR SLASH
495 495
 %right NOT
496 496
 %right DEFINED
497
+%right INTCAST STRCAST
497 498
 %left DOT
498 499
 
499 500
 /* no precedence, they use () */
... ...
@@ -2269,6 +2270,8 @@ rval_expr: rval						{ $$=$1;
2269 2269
 											*/
2270 2270
 									}
2271 2271
 		| rve_un_op %prec NOT rval_expr	{$$=mk_rve1($1, $2); }
2272
+		| INTCAST rval_expr				{$$=mk_rve1(RVE_INT_OP, $2); }
2273
+		| STRCAST rval_expr				{$$=mk_rve1(RVE_STR_OP, $2); }
2272 2274
 		| rval_expr PLUS rval_expr		{$$=mk_rve2(RVE_PLUS_OP, $1, $3); }
2273 2275
 		| rval_expr MINUS rval_expr		{$$=mk_rve2(RVE_MINUS_OP, $1, $3); }
2274 2276
 		| rval_expr STAR rval_expr		{$$=mk_rve2(RVE_MUL_OP, $1, $3); }
... ...
@@ -2285,6 +2288,8 @@ rval_expr: rval						{ $$=$1;
2285 2285
 		| STREMPTY LPAREN rval_expr RPAREN {$$=mk_rve1(RVE_STREMPTY_OP, $3);}
2286 2286
 		| DEFINED rval_expr				{ $$=mk_rve1(RVE_DEFINED_OP, $2);}
2287 2287
 		| rve_un_op %prec NOT error		{ $$=0; yyerror("bad expression"); }
2288
+		| INTCAST error					{ $$=0; yyerror("bad expression"); }
2289
+		| STRCAST error					{ $$=0; yyerror("bad expression"); }
2288 2290
 		| rval_expr PLUS error			{ yyerror("bad expression"); }
2289 2291
 		| rval_expr MINUS error			{ yyerror("bad expression"); }
2290 2292
 		| rval_expr STAR error			{ yyerror("bad expression"); }