Lua code objects

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua code objects

Nick Trout-2
Good day one and all. I was wondering if anyone had had any thoughts about treating Lua code as an object so that you can play with it and use it interactively. I think this was mentioned a while ago but nor followed up?
 
Off the top of my head... I was thinking of having a Lua class called "_Code". You make an instance of this and instead of using dofile() you call _Code:dofile() for things you want to keep, edit, recompile and save. When you use _Code:dofile(), it reads your file, parses it and executes it. The class remembers the source code of the code executed and if you want to edit something you can go to _Code and retrieve it. You then edit it and return it to _Code which executes it and can save it for you. _Code could also supply you with a table containing all the editable stuff available. Ergo, you have code editing mungojiggery. This could perhaps be a wrapper for the debug interface?
 
Any thoughts please?
 
Cheers,
Nick

---
Clothes make the man. Naked people have little or no influence on society.
Mark Twain (1835 - 1910)
 
 
Reply | Threaded
Open this post in threaded view
|

Re: Lua code objects

Scott Rutledge
I've been playing with that idea for a while now. I'm working on a game that has an in-game editor, and was looking for a way to allow the level designers easy access to the lua code of the scripting engine. I was thinking something along the lines of good old-fashioned muds: a database of objects which users can create, destroy, and edit on the fly, and have the engine be able to save and load the entire database as source code.
This could get really messy quickly, so I was thinking of keeping it simple. The concept would be a thin layer of wrapper functions that takes a table name, index, and string of source to assign to that table and index. The layer would do the actual assignment, as well as dump the source to a structured file of some sort (XML, ini-style, whatever). There would also have to be calls to handle the addition and removal of tables from the database, which would make the relevant changes to the source file.
I thought about wrapping dofile, but that would involve manually parsing out the code, which I'd rather leave to lua itself.
I'd be willing to help work on a library to support this. Sort of on the topic, had anyone been crazy enough to make an XML parser for lua?

Scott Rutledge

At 05:52 PM 26/02/2001 +0000, Nick Trout wrote:
Good day one and all. I was wondering if anyone had had any thoughts about treating Lua code as an object so that you can play with it and use it interactively. I think this was mentioned a while ago but nor followed up?
 
Off the top of my head... I was thinking of having a Lua class called "_Code". You make an instance of this and instead of using dofile() you call _Code:dofile() for things you want to keep, edit, recompile and save. When you use _Code:dofile(), it reads your file, parses it and executes it. The class remembers the source code of the code executed and if you want to edit something you can go to _Code and retrieve it. You then edit it and return it to _Code which executes it and can save it for you. _Code could also supply you with a table containing all the editable stuff available. Ergo, you have code editing mungojiggery. This could perhaps be a wrapper for the debug interface?
 
Any thoughts please?
 
Cheers,
Nick

---
Clothes make the man. Naked people have little or no influence on society.
Mark Twain (1835 - 1910)
 
 
Reply | Threaded
Open this post in threaded view
|

Re: Lua code objects

Reuben Thomas-3
> source to a structured file of some sort (XML, ini-style, whatever).

Unless you've got a very good reason against (e.g. a pre-defined format to
support, other tools that must manipulate it) use Lua source as your
structured file format. Lua was designed for this, after all.

> I'd be willing to help work on a library to support this. Sort of on the
> topic, had anyone been crazy enough to make an XML parser for lua?

There's little need, for the reason given above.

-- 
http://sc3d.org/rrt/ | wit, n.  educated insolence (Aristotle)


Reply | Threaded
Open this post in threaded view
|

Re: Lua code objects

Nick Trout-2
In reply to this post by Scott Rutledge
>>From: Scott Rutledge
>>This could get really messy quickly, so I was thinking of keeping it
simple. The concept would be a thin layer of wrapper functions that takes a
table name, index, and string of source to assign to that table and index.
The layer would do the actual assignment, as well as dump the source to a
structured file of some sort (XML, ini-style, whatever). There would also
have to be calls to handle the addition and removal of tables from the
database, which would make the relevant changes to the source file.
I thought about wrapping dofile, but that would involve manually parsing out
the code, which I'd rather leave to lua itself.

I think you would need to format your source somehow to make it "modular".
Parsing it automatically eg. to group stuff for editing. eg. class and all
member functions could get messy and would the tie up implementaion style?

eg. something parsable like :- (which you could also execute and use as
normal)

-- {begin: class Point}
function Point()
    local p = {}
    p.x,p.y = 0,0
    return p
end
-- {end: class Point}

or you could have tables of source? (which wouldnt require parsing)

_source = {
    ['class Point'] = [[
function Point()
    local p = {}
    p.x,p.y = 0,0
    return p
end
]]
}

>>I'd be willing to help work on a library to support this. Sort of on the
topic, had anyone been crazy enough to make an XML parser for lua?

I agree with Reuben, little point in doing this as you could store stuff as
shown above. :-)

Is this whole idea too restrictive and application specific?

N




Reply | Threaded
Open this post in threaded view
|

Re: Lua code objects

Scott Rutledge

or you could have tables of source? (which wouldnt require parsing)

_source = {
    ['class Point'] = [[
function Point()
    local p = {}
    p.x,p.y = 0,0
    return p
end
]]
}

Tables of source..I do like that idea. Flexible little language, isn't it?

>>I'd be willing to help work on a library to support this. Sort of on the
topic, had anyone been crazy enough to make an XML parser for lua?

I agree with Reuben, little point in doing this as you could store stuff as
shown above. :-)

Yup, that you could. Certainly will make things easier. Thanks for the feedback.

Is this whole idea too restrictive and application specific?

No, I think this could be made into a fairly general module: the lua live code database (er...whatever, not quite awake yet). I can see there being a few restrictions, won't know until we try.

I'll start jotting this down into a design spec. Will post it in a few days.

Scott Rutledge