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 785
 <INITIAL>{INTEQ}	{ count(); return INTEQ; }
784 786
 <INITIAL>{STRDIFF}	{ count(); return STRDIFF; }
785 787
 <INITIAL>{INTDIFF}	{ count(); return INTDIFF; }
788
+<INITIAL>{INTCAST}	{ count(); return INTCAST; }
789
+<INITIAL>{STRCAST}	{ count(); return STRCAST; }
786 790
 
787 791
 <INITIAL>{SELECT_MARK}  { count(); state = SELECT_S; BEGIN(SELECT); return SELECT_MARK; }
788 792
 <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 2270
 											*/
2270 2271
 									}
2271 2272
 		| rve_un_op %prec NOT rval_expr	{$$=mk_rve1($1, $2); }
2273
+		| INTCAST rval_expr				{$$=mk_rve1(RVE_INT_OP, $2); }
2274
+		| STRCAST rval_expr				{$$=mk_rve1(RVE_STR_OP, $2); }
2272 2275
 		| rval_expr PLUS rval_expr		{$$=mk_rve2(RVE_PLUS_OP, $1, $3); }
2273 2276
 		| rval_expr MINUS rval_expr		{$$=mk_rve2(RVE_MINUS_OP, $1, $3); }
2274 2277
 		| rval_expr STAR rval_expr		{$$=mk_rve2(RVE_MUL_OP, $1, $3); }
... ...
@@ -2285,6 +2288,8 @@ rval_expr: rval						{ $$=$1;
2285 2288
 		| STREMPTY LPAREN rval_expr RPAREN {$$=mk_rve1(RVE_STREMPTY_OP, $3);}
2286 2289
 		| DEFINED rval_expr				{ $$=mk_rve1(RVE_DEFINED_OP, $2);}
2287 2290
 		| rve_un_op %prec NOT error		{ $$=0; yyerror("bad expression"); }
2291
+		| INTCAST error					{ $$=0; yyerror("bad expression"); }
2292
+		| STRCAST error					{ $$=0; yyerror("bad expression"); }
2288 2293
 		| rval_expr PLUS error			{ yyerror("bad expression"); }
2289 2294
 		| rval_expr MINUS error			{ yyerror("bad expression"); }
2290 2295
 		| rval_expr STAR error			{ yyerror("bad expression"); }