lua-users home
lua-l archive

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


You've got metalua (http://metalua.luaforge.net) which offers, among others, full macros. Your example would be something like (untested code):

-{block:
mlp.stat:add{ 'try', mlp.block, 'except', mlp.block, 'end', builder = exn_builder }

function exn_builder(args)
   local try_block, exn_block = unpack(args)
   return +{stat:
      do
         local status, exn = pcall(function() -{try_block} end)

         if not status then -{exn_block} end
      end }

end }

The big difference with C-style macros is that it works at the tree level, not on token streams. It essentially works as lisp macros, except that there are a modular syntax extension system, and a couple of operators such as +{...} and -{...} which take care of translation between source-code representation for humans, and tree representation for macros and for the compiler.

The drawbacks are that it's a bit harder to build a quick-n'dirty hack (there definitely is a learning curve, and for advanced tricks you need to know the tree representation of code) and it takes more resources to compile. But it makes it easier to build robust extensions, to analyze or manipulate code in non-trivial ways, to get unrelated extensions to work together. It's possible (although there still is some work to do there) to get proper, self-explanatory error messages when a program won't compile; since the compiler directly takes trees, it keeps a lot of information that would be lost by a source->source preprocessor. Admittedly, it's an overkill for simple syntax sugar.