lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


As far as I know, what you're suggesting is not reasonable to do.

It's not possible to wrap any arbitrary block in a return(). 

  Lua 4.0  Copyright (C) 1994-2000 TeCGraf, PUC-Rio
  > function a(str) print(str); end;
  > return(function a(str) print(str); end;)
  error: `(' expected;
    last token read: `a' at line 1 in string "return(function a(str) print(str); end;)"

In order to do what you're saying, I would have to develop my own Lua
parser and embed it into my interpreter, so that I could figure out
what the last expression in the block was (if there is one) and put a
return around it.

The way to change the BNF for lua from:

  block          = <statement-list>
  statement-list = <statement> | <statement> *

To something like:

  block          = <statement-list> ( <rtn_expression> ) ?
  statement-list = <statement> | <statement> *

I know that the lua parser isn't automatically generated anymore. If
need be I'll see if I can find time to modify the parser myself and
submit a patch, however, I'm sure Luiz and Roberto can do this much
easier than I can if they agree it's a good idea.


On Tue, Jun 26, 2001 at 10:18:05AM -0700, Curt Carpenter wrote:
> And to be clear, you'd also have to preface every user-entered command
> with "return " before handing off to lua_dostring.
> 
> -----Original Message-----
> From: Curt Carpenter [mailto:curtc@microsoft.com] 
> Sent: Tuesday, June 26, 2001 10:13 AM
> To: Multiple recipients of list
> Subject: RE: block return semantic
> 
> 
> Seems like you could easily modify the stand-alone interpreter to do
> this by checking the return value and printing it if it's non-nil. Lua
> is the language, not the stand-alone interpreter. 
> 
> -----Original Message-----
> From: David Jeske [mailto:jeske@chat.net] 
> Sent: Tuesday, June 26, 2001 9:53 AM
> To: Multiple recipients of list
> Subject: block return semantic
> 
> 
> I'd like to formally request something that I suggested as an offhanded
> comment a short while ago. Lua should allow the last element in a block
> to be an expression, and if so, implicitly return it as the return value
> of the block. If this is done, Lua will be much more useful as an
> interactive shell.
> 
> Here is an example of attempting to use Lua interatively:
> 
>   mozart:~/Project/Lua/lua31/bin> lua
>   Lua 3.1 (alpha)  Copyright (C) 1994-1998 TeCGraf
>   > 1 + 1;
>   lua: parse error;
>     last token read: "1" at line 1 in file (dostring) >> 1 + 1;
>   > sin(45);
>   > a = { 1 , 2};
>   > a[1];
>   lua: parse error;
>     last token read: ";" at line 1 in file (dostring) >> a[1];
> 
> 
> Here is an example of the kind of thing which is useful in Python and
> should happen in Lua:
> 
>   mozart:~/Project/Lua/lua31/bin> python
>   Python 1.5.2 (#1, Sep 17 1999, 20:15:36)
>   Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam 
>   >>> 1 + 1 
>   2
>   >>> import math
>   >>> math.sin(45)
>   0.850903524534
>   >>> a = [ 1 , 2 ] 
>   >>> a[0]  
>   1
> 
> -- 
> David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net