lua-users home
lua-l archive

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


On Thu, Sep 23, 2004 at 09:18:25AM -0500, Rici Lake wrote:
> That's not really possible, since Lua compiles the program before 
> executing it.
> 
> I think you need to come up with a less general concept than "for 
> undefined reasons", which is what I meant when I said "you need to be 
> clearer". If there are particular things you want to control, that may 
> be possible, depending on what they are.

If it doesn't need to be fast, I think it may not be too hard to do by
treating each /statement/ as a separate chunk (roughly what the
interactive interpreter does, but also breaking up statements on a
single line). I only see two issues to address:

- Propagation of locals between chunks. That's not too hard since the
  debug interface provides the necessary information. Each chunk needs
  code to save the set of locals added to the end before execution,
  and the next chunk needs code to set the same values back up added
  to the start.

- Breaking up of blocks into chunks. This should be possible using a
  recursive textual substitution, basically replacing each "do BLOCK
  end" with "do my_evaluator(LOCALS_TABLE, [[BLOCK]])
  SAVE_BACK_TO_LOCALS end" (where my_evaluator is the same function
  that handles the top level), and the same for blocks starting with
  "then", and the three forms of function definition. In the case of
  functions, the replacement is more like: "function(x,y,z) return
  my_evaluator(UPVALUES_AND_PARAMS_TABLE, [[BLOCK]])
  SAVE_BACK_TO_UPVALUES end". The function upvalues can be assumed to
  be all the locals in scope.
  
It's nontrivial, but not impractically so I think. Of course, there
may well be cases I haven't thought of where it wouldn't work. The end
result would be that this:

local x = 5
print(x)
function add_to_x(y)
	x = x+y
	return x
end
if add_to_x(7) == 12 then
	print(x)
end

Is transformed into something like this (with each chunk being
constructed only after the last has executed and the resulting locals
are known):

-- CHUNK 1:
local x = 5
save_locals()

-- CHUNK 2:
local x = stored_locals.x
print(x)
save_locals()

-- CHUNK 3:
local x = stored_locals.x
function add_to_x(y)
	local locals = {x=x, y=y}
	local result = my_evaluator(locals, [[
		x = x+y
		return x
	]])
	x = locals.x
	return result
end
save_locals()

-- CHUNK 4:
local x = stored_locals.x
if add_to_x(7) == 12 then
	my_evaluator(stored_locals, [[
		print(x)
	]])
	x = stored_locals.x
end
save_locals()

-- Jamie Webb