lua-users home
lua-l archive

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


> my test data consists of an integer and 2 float values:

> Particles = 100
> Emitter.delay = 0.001
> Emitter.delayFuzz = 0

The easiest thing is to simply compile this as a Lua program; that is, use
Lua itself to lex and parse.

So create a global environment in which Emitter is defined as a table
(along with other objects which you might wish to be heirarchical) and then
evaluate the input file in that environment. You don't have to worry about
scalars because they are already defined.

If you don't know which things might turn out to be heirarchical, you need
a trick; the trick is to set up the global environment with an __index
metamethod which defines any undefined key as an empty table. This will not
effect direct assignments like:

  Particles = 100

because that does not attempt to read the value of Particles, but it will
work for

  Emitter.delay = 0.001

because to evaluate that, Lua has to look up the value of Emitter.

So you can set up the environment as follows:

environ = setmetatable({}, {
  __index = function(table, key)
              local r = {}
              table[key] = r
              return r
            end
  }
)

Then you can load the input file, which converts the file into an
executable chunk, and set the environment of the chunk to environ. (First
check to make sure that there were no syntax errors, of course.)

The advantage of this approach is that the configuration file can include
computations, which might be useful. And it saves you from having to write
a lexer and parser.

If you want to allow the configuration file to call Lua built-in functions
(you should be cautious with this), simply add the relevant functions to
environ.

Hope this helps.

Rici.



"Milking It", Oxfam's first online resource for secondary schools, is now
live. Find out how Oxfam is encouraging young people to learn about trade
and dumping. http://www.oxfam.org.uk/coolplanet/milkingit

Oxfam works with others to find lasting solutions to poverty and suffering.

Oxfam GB is a member of Oxfam International, a company limited by guarantee and registered in England No. 612172.
Registered office: 274 Banbury Road, Oxford OX2 7DZ.
Registered charity No. 202918.

Visit the web site at http://www.oxfam.org.uk