lua-users home
lua-l archive

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


Hi guys,

http://lua-users.org/wiki/LuaMacros

has the write-up;

http://luaforge.net/frs/?group_id=328

has the files.

Main feature of this release is: no more silly token literals. That
is, a simple macro is now just:

macro.define('PLUS',{'L','C'},' ((L)+(C)) ')

I've improved the try/catch definition; this kind of code now works as
you would expect - previously one could not return out of a nested try
block.

function test_try_except(x,y)
	try
		x.a = 10
		try
			y.a = 10
			return 1
		except(e)
			return 3
		end
	except(e)
		return 2
	end
end

Another example is a little bit of sugar for defining Lua classes:

class Animal is
	-- _init is a special constructor method, invoked by class.new
	function _init(self,name)
		self.name = name
	end
	
	-- you don't have to qualify method names, since our environment
	-- is the class metatable!
    function speak(self)
        print ('growl '..self:get_name())
    end

    function get_name(self)
        return self.name
    end
endclass

class Dog(Animal) is

	-- necessary to explicitly call the base class ctor
	function _init(self,name)
		_base._init(self,name)
	end

	function speak(self)
		G.print('bark '..self:get_name())		
	end
endclass


As always, macros work best when they provide a thin sugar coating
over the Lua syntax. In the above case, 'class Dog(Animal) is' expands
to 'Dog = _class(Animal,function()' and 'endclass' to 'end)'.

steve d.