Archived

This topic is now archived and is closed to further replies.

RolandofGilead

lua code generation in lua

Recommended Posts

As I haven''t seen it mentioned, and myself and possibly others could use pointers, anyone have any resources or tips? I haven''t used lisp, but from what I''ve read, I still use lua instead of lisp because lua can do whatever lisp can do(though not easily) and I believe it will be easier for non-programmers to pick up lua(though I could write a layer in lisp to make a user-friendly extension, I know). It does seem however, that code generation is something which could be handled by lisp much more easily. In lua, I need to write a lot of stuff. I''m tired, must end now, good night.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What do you mean by code generation?

Lua compiles itself very easily, if that''s what you''re talking about. loadstring and string.dump do the job nicely.

Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> luaprog = loadstring ("print \"abcdefg\""
> s = string.dump (luaprog, "myprogram"
> luaprog()
abcdefg
> os.exit()


s, the returned value from string.dump, is the compiled bytecode of the Lua chunk loaded in loadstring.

You can basically do on-the-fly interpreting of Lua code with something like this... I guess that''s kinda useful in the same sense as "eval" that Perl (ugh) provides. I use code similar to the above to place the lua bytecode in a C file for my lua2c script, which is a helpful step in making a Lua script a (sort of) standalone executable.

Share this post


Link to post
Share on other sites
I mean having a lua program modify or write from scratch other lua programs.
Another clue as to what I mean, no matter what language you write a code generator in, the writing program needs to have a data structure containing all reserved words and special characters. That kind of generator.

Share this post


Link to post
Share on other sites
You don''t need to store everything, only the bits you need to be able to generate. It edits existing code by making no distinction between code and data. It''s not the same as some sort of ''automatic text generator'' where you''d need a large grammar to show how to create the code.

Maybe this link will help, maybe not.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
You don''t need to store everything, only the bits you need to be able to generate. It edits existing code by making no distinction between code and data. It''s not the same as some sort of ''automatic text generator'' where you''d need a large grammar to show how to create the code.

Maybe this link will help, maybe not.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]



That''s it exactly. Reflection, I should have remembered. I knew lisp was good at it, I just couldn''t remember what it was.
Thank you so much because I thought it was going to be complex and I''d have to store a string containing each var''s name. This is so much better. Now I finally understand what the .. operator does as well.
Awesome.

You should put the OpenGL Framework in your sig as well

<a href="http://glfw.sourceforge.net/projects/tinyxml">GLFW</a>

Share this post


Link to post
Share on other sites