--****************************************************************************** --* * --* File: Lua_04_Lib.lua Revision: 1.0 * --* * --* Purpose: provides functions from "Lua_04" for other scripts * --* * --* Creation: 07.03.2002 Last Modification: 07.03.2002 * --* * --* Platform: IBM-compatible PC running Windows 98SE * --* * --* Environment: Lua 4.0, TkLua 4.0 * --* * --* Author: Andreas Rozek Phone: ++49 (711) 6770682 * --* Kirschblütenweg 15 Fax: - * --* D-70569 Stuttgart EMail: Andreas.Rozek@T-Online.De * --* Germany * --* * --* URL: http://www.Andreas-Rozek.de/ * --* * --* Copyright: the software is published under the "GNU Lesser General Pub- * --* lic License" (see "http://www.fsf.org/copyleft/lesser.html" * --* for additional information) * --* * --* Comments: (none) * --* * --****************************************************************************** dofile("Lua_03_Lib.lua"); --**** map numeric operators on synonymous object methods **** local add_Mapper = function (Op1, Op2) if (tag(Op1) == tag(Object)) then return Op1:__add__(Op2); -- naming convention similar to Python elseif (tag(Op2) == tag(Object)) then return Op2:__radd__(Op1); -- naming convention similar to Python else error( "error: unable to add non-numeric values (argument types are \"".. type(Op1).."\" and \""..type(Op2).."\")" ); end; end; settagmethod(tag(Object),"add",add_Mapper); local sub_Mapper = function (Op1, Op2) if (tag(Op1) == tag(Object)) then return Op1:__sub__(Op2); -- naming convention similar to Python elseif (tag(Op2) == tag(Object)) then return Op2:__rsub__(Op1); -- naming convention similar to Python else error( "error: unable to subtract non-numeric values (argument types are \"".. type(Op1).."\" and \""..type(Op2).."\")" ); end; end; settagmethod(tag(Object),"sub",sub_Mapper); local mul_Mapper = function (Op1, Op2) if (tag(Op1) == tag(Object)) then return Op1:__mul__(Op2); -- naming convention similar to Python elseif (tag(Op2) == tag(Object)) then return Op2:__rmul__(Op1); -- naming convention similar to Python else error( "error: unable to multiply non-numeric values (argument types are \"".. type(Op1).."\" and \""..type(Op2).."\")" ); end; end; settagmethod(tag(Object),"mul",mul_Mapper); local div_Mapper = function (Op1, Op2) if (tag(Op1) == tag(Object)) then return Op1:__div__(Op2); -- naming convention similar to Python elseif (tag(Op2) == tag(Object)) then return Op2:__rdiv__(Op1); -- naming convention similar to Python else error( "error: unable to divide non-numeric values (argument types are \"".. type(Op1).."\" and \""..type(Op2).."\")" ); end; end; settagmethod(tag(Object),"div",div_Mapper); local pow_Mapper = function (Op1, Op2) if (tag(Op1) == tag(Object)) then return Op1:__pow__(Op2); -- naming convention similar to Python elseif (tag(Op2) == tag(Object)) then return Op2:__rpow__(Op1); -- naming convention similar to Python else error( "error: unable to exponentiate non-numeric values (argument types are \"".. type(Op1).."\" and \""..type(Op2).."\")" ); end; end; settagmethod(tag(Object),"pow",pow_Mapper); local unm_Mapper = function (Op) if (tag(Op) == tag(Object)) then return Op:__neg__(); -- naming convention similar to Python else error( "error: unable to negate non-numeric values (argument type is \"".. type(Op).."\")" ); end; end; settagmethod(tag(Object),"unm",unm_Mapper); local lt_Mapper = function (Op1, Op2) if (tag(Op1) == tag(Object)) then return Op1:__lt__(Op2); -- naming convention similar to Python elseif (tag(Op2) == tag(Object)) then return Op2:__rlt__(Op1); -- naming convention similar to Python else error( "error: unable to compare non-numeric and non-string values ".. "(argument types are \""..type(Op1).."\" and \""..type(Op2).."\")" ); end; end; settagmethod(tag(Object),"lt",lt_Mapper); --**** provide "empty" methods within "Object" **** function Object:__add__ (Operand) -- responsible for self + Operand error("error: no implementation for \"+\" operator found"); end; function Object:__radd__ (Operand) -- responsible for Operand + self error("error: no implementation for \"+\" operator found"); end; function Object:__sub__ (Operand) -- responsible for self - Operand error("error: no implementation for \"-\" operator found"); end; function Object:__rsub__ (Operand) -- responsible for Operand - self error("error: no implementation for \"-\" operator found"); end; function Object:__mul__ (Operand) -- responsible for self * Operand error("error: no implementation for \"*\" operator found"); end; function Object:__rmul__ (Operand) -- responsible for Operand * self error("error: no implementation for \"*\" operator found"); end; function Object:__div__ (Operand) -- responsible for self / Operand error("error: no implementation for \"/\" operator found"); end; function Object:__rdiv__ (Operand) -- responsible for Operand / self error("error: no implementation for \"/\" operator found"); end; function Object:__pow__ (Operand) -- responsible for self ^ Operand error("error: no implementation for \"^\" operator found"); end; function Object:__rpow__ (Operand) -- responsible for Operand ^ self error("error: no implementation for \"^\" operator found"); end; function Object:__neg__ () -- responsible for -self error("error: no implementation for sign operator found"); end; function Object:__lt__ (Operand) -- responsible for self < Operand error("error: no implementation for \"<\" operator (or similar) found"); end; function Object:__rlt__ (Operand) -- responsible for Operand < self error("error: no implementation for \"<\" operator (or similar) found"); end;