lua-users home
lua-l archive

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


On Mon, Jul 30, 2001 at 09:29:31AM -0400, Jay Carlson wrote:
> David Jeske writes:
> 
> > I would be far happier if I had a mechanism for making my class system
> > have more bearable syntax. However, I recognize the likely-hood of Lua
> > ending up with the meta-syntax mechanisms that I think would go so
> > well with it's meta-language mechanisms is about zero. :)
> 
> What does your class system look like?  

Below is a real example of a class (cut and pasted right out of my
game project) which inherits from three 'trait/mixin' style base
classes. Here are a few notes about why I do the things I do:

  1) I use the "declare_class()" function to declare a class, 
     because I don't want the global namespaces populated with a
     bunch of junk. The declare_class function puts the class table
     off in a special place, and gives any required special elements
     such as "_CLASSNAME".
  2) I use the anonymous function declaration syntax because it's the
     only way for me to add methods give the above "declare_class()"
     function class declaration style.
  3) I use a tag-method inheritence scheme as explained in LTN #8.

> What help do you need from the syntax?

  1) Personally, I find languages with task-dedicated syntax 
     easier to use and read. If you dedicate your life to
     writing ASTs directly by coding in LISP or Scheme you
     may disagree with me. Please just agree to disagree and
     move on to the next email in your inbox.

  2) It's hard for people to write a script object for my
     game because they have trouble figuring out where they
     are supposed to put the puntcuation in the table/class 
     declaration. They have to put a comma after each element, and 
     after each "end" of a method, except the last one. Then,
     they better not forget all the end punctuation in the
     closing table/function/statement end. i.e. "});" 

     They normally just copy my class, but when it breaks, they
     don't know which little comma they were missing.

  3) If you forget something or make a syntax mistake inside a
     function deep within the table, the lack of keywords for 
     the compiler to get it's bearings sometimes makes the syntax 
     errors obtuse.

  4) Personally I find the syntax ugly, and hard to read.

> Do you need reader macros, or can you get away with runtime
> interpretation?

I'm not sure what you mean by runtime interpretation. 

I basically want to create new tokens and new BNF elements which fit
into the expression/statement part of the language and macro evaluate
into other existing structures of the language. From what I know of
lisp-reader-macros, this is sort of what they do.



------[ real class example follows:

declare_class("MainShip", {
	_parents = { air_physics, controllable, collidable },

	Condition = "Healthy",
	imgdir = 2.0,   -- the image index

	rimgdir = 1.0,
	direction = 0.0, -- in degrees
	bullet_type = "bullet",
	objtype = "mainship",
	exp_timer = 0.0,
	frame_time = 70,
	layer = 1,

	damage = 0,
	damage_max = 10,
	recharge_rate = 0.1,
	
	VisualRep = VisualReps.newDropShip,

	-- dummy ai_event
	ai_event = function(self)
	end,

	-- constructor
	new = function (self,a_list) -- constructor
			if (type(a_list) ~= "table") then
			print("mainship:new() called with non-table "..tostring(a_list));
		else 
			a_list._parents = { self };
			a_list.key = {}; -- make our private keydown list
			a_list.ctrl_centered = 1.0;
			a_list.dest_dir = 0.0;
		end
		return a_list
	end,

	recharge = function (self,byWhom) -- recharge method
		local damage = self.damage;
		if (damage > 0) then
			damage = max(0,damage - recharge_rate);
			self.damage = damage;
		end
	end
}); -- register done


-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + jeske@chat.net