[sr-dev] git:andrei/rve_f_params: NEWS: notes about expressions in function parameters

Andrei Pelinescu-Onciul andrei at iptel.org
Fri Aug 13 11:11:14 CEST 2010


On Aug 12, 2010 at 09:26, Klaus Darilion <klaus.mailinglists at pernau.at> wrote:
> forgot to cc the list...
> 
> Am 11.08.2010 20:11, schrieb Andrei Pelinescu-Onciul:
> >On Aug 11, 2010 at 18:39, Klaus Darilion<klaus.mailinglists at pernau.at>  wrote:
> >>
> >>
> >>Am 11.08.2010 17:19, schrieb Andrei Pelinescu-Onciul:
> >>>On Aug 11, 2010 at 17:13, Klaus Darilion<klaus.mailinglists at pernau.at>   wrote:
> >>>>Hi Andrei!
> >>>>
> >>>>Am 11.08.2010 16:18, schrieb Andrei Pelinescu-Onciul:
> >>>>>+  - all the module functions can now be called with any constant expression
> >>>>>+      as parameters. E.g.: f("7 *" +" 6 = " + 7 * 6);
> >>>>
> >>>>What is the result of this example?
> >>>
> >>>f("7 * 6 =  42")
> >>
> >>
> >>... + 7 * 6)
> >>     ^   ^
> >>     |   |
> >>     A   B
> >>
> >>IMO it is very confusing (if not even wrong) that A is a string
> >>concat and B is a arithmetic operation. What happens with:
> >>
> >>... + 7 + 6) ?
> >
> >f("7 + 6 = 76") :-)
> >
> >but
> >
> >f("7 +" +" 6 = " + (7 + 6)) is f("7 + 6 = 13"), as expected.
> >
> >I agree it's confusing, but when I wanted to add a separate operator for
> >string concat (e.g. '.') lots of people opposed it on the grounds that
> >it will be too difficult for a script writer to use 2 different operators
> >  (or something similar).
> >Now we have auto-conversion everywhere...
> >
> >Maybe we can revisit this and the option of declaring only typed
> >variable for a future release.
> 
> I can't remember if I was for or against a dedicated concat
> parameter, but now I think a dedicated parameter would be good. '.'
> might be confusing as well once we support float numbers.
> 
> Is it possible to analyze how sr evaluates the term?

f("7 + 6 = " + 7 + 6) =>

since '+' has the same priority:
  ("7 + 6 = " + 7) + 6   =>
  ("7 + 6 = " + (str) 7) + 6     [first term is str => forces str
                                 conversion]  
  ("7 + 6 = 7" + 6)  =>
  ("7 + 6 = 7" + (str)6) =>
  "7 + 6 = 76"

In the '*' case:
  ("7 + 6 = " + 7 * 6)
   '*' has higher prio then '+' =>
  ("7 + 6 = " + (7*6)) =>
  ("7 + 6 = " + 42 ) ...

The '+' operator is special in the sense that it converts its right
operand to the type of the left operand and the result also has the type
of the left operand. The other operators force conversion
and always have a fixed return type (e..g '*' converts both of its
operands to int and always returns int).

More examples:

"1" + 2 = "12"
1 + "2"  => error
"1" + "2" = "12"

"1" * 2 => error
1 * "2" => error
"1" * "2" => error
(int)"1" * 2 = 2

Note also that the code internally supports a CONCAT operator (that
works only on strings) and IPLUS operator (+ for integers only). Right
now they are used in optimizations (when possible the PLUS operator is
replaced during the optimization phase with either CONCAT or IPLUS).
They could easily be enabled from cfg.y (if we find a "name" for them).


Andrei



More information about the sr-dev mailing list