xsawyerx / guacamole

Guacamole is a parser toolkit for Standard Perl. It provides fully static BNF-based parsing capability to a reasonable subset of Perl.
https://metacpan.org/pod/Guacamole
20 stars 8 forks source link

return statement #1

Closed vickenty closed 8 years ago

vickenty commented 8 years ago

'Subroutine calls' mentions that return there is no special cases for return, but it does not seem possible.

For example:

foo(1, return(2, 3), 4);

If return is not special and treated like any other subroutine, it will be possible to use it in an expression. If return was a subroutine, it would get arguments (2, 3), so under these rules the snippet above should return (2, 3) as well. But in perl this returns (2, 3, 4) because return consumes everything until the last closing parenthesis.

One way or another return needs to be special. Some options are:

1) only allow return as a stand-alone statement with a pair of top-level parenthesis around arguments.

return(1, 2); # ok
return(1, 2), 3; # not ok
return(@foo); # ok
return @foo; #not ok
foo(1, return 2); # not ok

(package keyword is not allowed in expressions, so it seems there is precedent for stand-alone statement operators like this already).

2) only allow return as a stand-alone statement, but accept full syntax:

return (1, 2), 3; # ok
return @foo; # ok
foo(1, return 2) # not ok

3) don't restrict use of return in any way:

foo(1, return 2) # ok
foo(1, return (2, 3), 4) # ok
xsawyerx commented 8 years ago

I think (1) is the best option and I've changed the wiki to reflect this.

Thank you! :)