lua-users home
lua-l archive

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


Ok I will just describe more the context:
I have exisiting code building hierarchical c++ structure
struct C : HierarchyNamed {
  C(const char* local_name) 
     : HierarchyNamed(local_name) 
  { get_parm(global_name() + ".my_parm", &my_parm); }
  int my_parm;
};
I want to preserve existing code, but use eventually a lua file to configure system, ie reimplement get_parm();
I know I can use my own tokenizer and perform a lookup for each word with checking their type and presence (well this is what I did). 
But I'm aware this is mainly rewriting the interpreter, so I was wondering if there was a simpler way to do it.
luaL_dostring(L, "return a.b[1].c") seems to be able interpret the expression. But I just wonder if this is need and likely to do this to catch "attempt to index ..." error.
bool get_parm(string exp, int& parm)
  exp = tolua_exp(exp);
  bool is_int = false;
  int top = lua_gettop(L);
  if (!luaL_dostring(L, string("return " + parm_exp).c_str()) && lua_isnumber(L, -1)) {
    lua_Number n = lua_tonumber(L, -1);
    lua_number2int(parm, n);
    is_int = luai_numeq(cast_num(parm), n));
  }
  lua_settop(L, top);
  return is_int;
}

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Philippe Lhoste
Sent: Wednesday, October 04, 2006 1:47 PM
To: lua@bazar2.conectiva.com.br
Subject: Re: Simple example using Lua as a "config file" alternative?

Grellier, Thierry a écrit :
> So one would have to do something like:
> lua_dostring(L, "local pushed = a.b.c[1].d");
> lua_to<mytype>(L, -1);
> lua_pop(L, 1);
> and eventually catch error? (what if b is nil for example?)
> It is just that my configuration shall be a valid lua expression, so I
> don't really feel like I shall tokenize and interpret it myself.

No, you can access any table member in C without using this syntax.
I can't give you code here right now, because I am a bit rusty on the C 
side of the API, but there are example around (mailing list, Wiki, PiL 
aka. Programming in Lua, etc.)

Side note: I had an old unfinished project to improve existing table 
dump routines by indenting nicely the output. I recently took it back, 
and rewrote it entirely, making it somehow usable.
It is the opportunity to share it, I give the DumpObject.lua and its 
companion TestDumpObject.lua, trying to explore most of the aspects of 
table building...

I don't manage user-data (depending on the implementation) nor 
functions: I discovered later that I can dump and reread the functions, 
perhaps wrapping the dump with a conversion to Ascii (Base64, Ascii85, 
to hex, with escapes, whatever), but I feel lazy here...
Another limitation is that I transform implicit array numbers to 
explicit ones, it would add some complexity to do otherwise.

On the other hand, it handles loops and makes its output using the real 
Lua syntax with some optimizations (using literal string when needed), 
unlike a previous version which was merely a debug dump.

The main issue, for somebody used to Windows' .INI files, is that 
comments are lost, and initial order of key/value pairs too. That's the 
limits of Lua as configuration language, to do otherwise would need to 
re-parse the initial file and update the values in place...

The strong point is that it supports expressions and even functions, 
good and clean hierarchy, references, etc.
I suppose we can't get the best of the two worlds (or it will be hard).

We have the same problem with the SciTE .properties files: they are 
quite flexible, but read-only, it is hard to add a configuration dialog 
to allow friendly option editing and automatically update the properties 
on disk, and even then, we would loose lines like 
keywords4.$(file.patterns.lua)=$(keywordclass4.lua5).

-- 
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --