lua-users home
lua-l archive

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


I think you should look at both the debugging API's "hooks" as well as at
coroutines.  Both of those suggestions assume that you did not mean to
"parse" the script "one instruction at a time", but rather to "execute" the
script "one instruction at a time".

The debugging hooks should give you instruction by instruction (or maybe
just line by line) control, while coroutines would let the scripts run until
they are ready to "yield", and to "return" values back out of themselves at
the "yield" points.



-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of David Byard
Sent: Wednesday, May 26, 2004 1:06 PM
To: lua@bazar2.conectiva.com.br
Subject: Instruction By Instruction Parsing


Not sure if this is where I should post, but...

I've been teaching myself some of the Lua API, but the documentation is
very confusing. What I'm trying to do is make a system such that a Lua
script is parsed one instruction at a time, returning information to the
function doing the processing.

For example:
int hypothetical_function ()
{
  ...

  while ( !stop )
  {
    result = run_next_instruction ( script );
    switch (result)
    {
      case DO_SOMETHING:
        ... // DO_SOMETHING would be returned if the Lua Script called,
say, the function do_something() as the last instruction.
        break;
      case DO_SOMETHING_ELSE:
        ... // DO_SOMETHING_ELSE would be returned if the Lua Script
called, say, the function do_something_else() as the last instruction.
        break;
      case STOP_PARSING:
        stop = TRUE;
        break;
    }
    ...
  }
}

Now, I've managed to get the thing going an instruction at a time using
Lua debug hooks set to 1 instruction, pointing to a function that make
it yield.

The bit that's baffling me is how to make the 'yield' return a value. I
thought about maybe making do_something() and do_something_else()
setting a global which is then checked directly after the script yields
(for example do_something() sets the global return_value to something
which is picked up). But I have no idea how to go about this.

Can anyone help (or offer suggestions)?