lua-users home
lua-l archive

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


If you want to experiment with variables at compile time, metalua offers all the bricks and mortar you need, most specifically with the library walk.id.

For instance, if you add the following piece of code in your program, it will attach a callback to the chunk compiler, which will list the global variables it uses on stdout. You can automatically shadow them with local vars, check that they have some properties, transform them into locals when used in a certain context, etc. You can also do that for entries in module-like tables (e.g. change occurrences of string.match into references to a properly initialized _string_match local var).

-{ block:
   require 'walk.id'
   function list_globals (ast)
      local walk_cfg, globals = { id = { } }, { }
      function walk_cfg.id.free(v) globals[v[1]] = true end
      walk_id.block(walk_cfg, ast) -- accumulate global var names in the table "globals"
      print "Global vars used in this chunk:"
      for v in keys(globals) do print(" - "..v) end
   end
   mlp.chunk.transformers:add(list_globals) }

If you want your meta-programs to be robust, you might also want to use H, the hygienization library, although it's not yet very mature.