[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Access Lua's last evaluated result from C
- From: "Peter Cawley" <lua@...>
- Date: Wed, 14 May 2008 10:21:44 +0100
I would insist on an explicit 'return' instruction, or course you may
decide that the | character can be shorthand for 'return ', and
perform that replacement before loading the chunk
2008/5/14 steve donovan <steve.j.donovan@gmail.com>:
> The form of the chunk you actually send to Lua would be 'return
> param2*1.5', and gets compiled to a function which returns the value.
> You would of course have to insert the values for 'param2' etc into
> the global table directly.
>
> steve d.
>
> On Wed, May 14, 2008 at 10:38 AM, Maggie Webb and Pete Kerr
> <magnpete@activ8.net.au> wrote:
>> Hi
>>
>> Is there a clean way I can access, from C, the last value computed by Lua,
>> if it wasn't assigned to a variable?
>>
>> Here's why I want to do it:
>>
>> My C application requires the user to specify parameter values in a file
>> like this
>>
>> param1 3.5
>> param2 -0.4
>>
>> I want to extend this by allowing the user to replace the explicit value
>> with some Lua code between dollar signs
>>
>> param3 $ param2*1.5 $
>>
>> If I send this chunk to Lua, how do I get the value back? If it was MATLAB,
>> say, I'd just fetch the value of variable ans, which always holds the last
>> value computed.
>>
>> I could concatenate "param3=" with "param2*1.5" and fetch the value of
>> param3, but in more complex examples the user might submit a list of
>> statements with the last one evaluating to the required parameter value.
>> e.g.
>>
>> param3 $ lots of code ; required_value $
>>
>> I could of course insist on:
>>
>> param3 $ lots of code ; param3 = required_value $
>>
>> Which gives me the easy to read (by a human) list of parameter names down
>> the left, but forces the user to type param3 twice, which somehow seems
>> inelegant in the context of the already defined syntax for constant values.
>>
>> Anyway, in another part of the input file, the user supplies simple values
>> without a name, where the requirement to supply one would be even more
>> clunky.
>>
>> Thanks,
>>
>> Pete Kerr
>>
>>
>>
>