Browse Source

core: type casts support in the script

Support for casts added: (int) and (str).
E.g.: (int)$v ; (str)$v+"test".
Andrei Pelinescu-Onciul 16 years ago
parent
commit
e41bc0575e
3 changed files with 12 additions and 2 deletions
  1. 3 2
      NEWS
  2. 4 0
      cfg.lex
  3. 5 0
      cfg.y

+ 3 - 2
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).

+ 4 - 0
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);

+ 5 - 0
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"); }