Skip to content

Commit

Permalink
Pull #91: move is_XXX and to_bool into pl.types
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedonovan committed Sep 6, 2013
1 parent 8b8cc8d commit 0e586d8
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 89 deletions.
4 changes: 2 additions & 2 deletions lua/pl/List.lua
Expand Up @@ -28,7 +28,7 @@ local tsub = tablex.sub
local utils = require 'pl.utils'
local class = require 'pl.class'

local array_tostring,split,is_type,assert_arg,function_arg = utils.array_tostring,utils.split,utils.is_type,utils.assert_arg,utils.function_arg
local array_tostring,split,assert_arg,function_arg = utils.array_tostring,utils.split,utils.assert_arg,utils.function_arg
local normalize_slice = tablex._normalize_slice

-- metatable for our list and map objects has already been defined..
Expand Down Expand Up @@ -269,7 +269,7 @@ function List.range(start,finish,incr)
end
if incr then
assert_arg(3,incr,'number')
if not utils.is_integer(incr) then finish = finish + eps end
if math.ceil(incr) ~= incr then finish = finish + eps end
else
incr = 1
end
Expand Down
1 change: 0 additions & 1 deletion lua/pl/Map.lua
Expand Up @@ -12,7 +12,6 @@
local tablex = require 'pl.tablex'
local utils = require 'pl.utils'
local stdmt = utils.stdmt
local is_callable = utils.is_callable
local tmakeset,deepcompare,merge,keys,difference,tupdate = tablex.makeset,tablex.deepcompare,tablex.merge,tablex.keys,tablex.difference,tablex.update

local pretty_write = require 'pl.pretty' . write
Expand Down
1 change: 0 additions & 1 deletion lua/pl/MultiMap.lua
Expand Up @@ -11,7 +11,6 @@ local List = require 'pl.List'
local index_by,tsort,concat = tablex.index_by,table.sort,table.concat
local append,extend,slice = List.append,List.extend,List.slice
local append = table.insert
local is_type = utils.is_type

local class = require 'pl.class'
local Map = require 'pl.Map'
Expand Down
6 changes: 3 additions & 3 deletions lua/pl/array2d.lua
@@ -1,7 +1,7 @@
--- Operations on two-dimensional arrays.
-- See @{02-arrays.md.Operations_on_two_dimensional_tables|The Guide}
--
-- Dependencies: `pl.utils`, `pl.tablex`
-- Dependencies: `pl.utils`, `pl.tablex`, `pl.types`
-- @module pl.array2d

local require, type,tonumber,assert,tostring,io,ipairs,string,table =
Expand All @@ -10,7 +10,7 @@ local setmetatable,getmetatable = setmetatable,getmetatable

local tablex = require 'pl.tablex'
local utils = require 'pl.utils'

local types = require 'pl.types'
local imap,tmap,reduce,keys,tmap2,tset,index_by = tablex.imap,tablex.map,tablex.reduce,tablex.keys,tablex.map2,tablex.set,tablex.index_by
local remove = table.remove
local splitv,fprintf,assert_arg = utils.splitv,utils.fprintf,utils.assert_arg
Expand Down Expand Up @@ -483,7 +483,7 @@ end
-- @return new 2d array
function array2d.new(rows,cols,val)
local res = {}
local fun = utils.is_callable(val)
local fun = types.is_callable(val)
for i = 1,rows do
local row = {}
if fun then
Expand Down
4 changes: 2 additions & 2 deletions lua/pl/import_into.lua
Expand Up @@ -5,7 +5,7 @@
-- then that module is dynamically loaded. The submodules are all brought into
-- the table that is provided as the argument, or returned in a new table.
-- If a table is provided, that table's metatable is clobbered, but the values are not.
-- This module returns a single function, which is passed the environment.
-- This module returns a single function, which is passed the environment.
-- If this is `true`, then return a 'shadow table' as the module
-- See @{01-introduction.md.To_Inject_or_not_to_Inject_|the Guide}

Expand All @@ -24,7 +24,7 @@ return function(env)
input=true,seq=true,lexer=true,stringx=true,
config=true,pretty=true,data=true,func=true,text=true,
operator=true,lapp=true,array2d=true,
comprehension=true,xml=true,
comprehension=true,xml=true,types=true,
test = true, app = true, file = true, class = true, List = true,
Map = true, Set = true, OrderedMap = true, MultiMap = true,
Date = true,
Expand Down
108 changes: 108 additions & 0 deletions lua/pl/lint64.c
@@ -0,0 +1,108 @@
/*
* lint64.c
* int64 nummbers for Lua
* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
* Thu Aug 1 22:56:17 BRT 2013
* This code is hereby placed in the public domain.
*/

#include <stdlib.h>

#define Int long long

#include "lua.h"
#include "lauxlib.h"

#define MYNAME "int64"
#define MYTYPE MYNAME
#define MYVERSION MYTYPE " library for " LUA_VERSION " / Aug 2013"

#define Z(i) Pget(L,i)
#define O(i) luaL_optnumber(L,i,0)

#define add(z,w) ((z)+(w))
#define sub(z,w) ((z)-(w))
#define mul(z,w) ((z)*(w))
#define div(z,w) ((z)/(w))
#define neg(z) (-(z))
#define new(z) (z)

static Int Pget(lua_State *L, int i)
{
switch (lua_type(L,i))
{
case LUA_TNUMBER:
return luaL_checkint(L,i);
case LUA_TSTRING:
return atoll(luaL_checkstring(L,i));
default:
return *((Int*)luaL_checkudata(L,i,MYTYPE));
}
}

static int pushInt(lua_State *L, Int z)
{
Int *p=lua_newuserdata(L,sizeof(Int));
*p=z;
luaL_setmetatable(L,MYTYPE);
return 1;
}

static int Leq(lua_State *L) /** __eq(z,w) */
{
lua_pushboolean(L,Z(1)==Z(2));
return 1;
}

static int Llt(lua_State *L) /** __lt(z,w) */
{
lua_pushboolean(L,Z(1)<Z(2));
return 1;
}

static int Ltostring(lua_State *L) /** __tostring(z) */
{
char b[100];
sprintf(b,"%lld",Z(1));
lua_pushstring(L,b);
return 1;
}

#define A(f,e) static int L##f(lua_State *L) { return pushInt(L,e); }
#define B(f) A(f,f(Z(1),Z(2)))
#define F(f) A(f,f(Z(1)))

B(add) /** __add(z,w) */
B(div) /** __div(z,w) */
B(mul) /** __mul(z,w) */
B(sub) /** __sub(z,w) */
F(neg) /** __unm(z) */
F(new) /** new(z) */

static const luaL_Reg R[] =
{
{ "__add", Ladd },
{ "__div", Ldiv },
{ "__eq", Leq },
{ "__lt", Llt },
{ "__mul", Lmul },
{ "__sub", Lsub },
{ "__unm", Lneg },
{ "__tostring", Ltostring},
{ "new", Lnew },
{ NULL, NULL }
};

LUALIB_API int luaopen_int64(lua_State *L)
{
if (sizeof(Int)!=8) luaL_error(L,"Int has %d bytes but expected 8",sizeof(Int));
luaL_newmetatable(L,MYTYPE);
luaL_setfuncs(L,R,0);
lua_pushliteral(L,"version"); /** version */
lua_pushliteral(L,MYVERSION);
lua_settable(L,-3);
lua_pushliteral(L,"__index");
lua_pushvalue(L,-2);
lua_settable(L,-3);
return 1;
}
4 changes: 2 additions & 2 deletions lua/pl/seq.lua
@@ -1,7 +1,7 @@
--- Manipulating iterators as sequences.
-- See @{07-functional.md.Sequences|The Guide}
--
-- Dependencies: `pl.utils`, `debug`
-- Dependencies: `pl.utils`, `pl.types`, `debug`
-- @module pl.seq

local next,assert,type,pairs,tonumber,type,setmetatable,getmetatable,_G = next,assert,type,pairs,tonumber,type,setmetatable,getmetatable,_G
Expand All @@ -10,6 +10,7 @@ local mrandom = math.random
local remove,tsort,tappend = table.remove,table.sort,table.insert
local io = io
local utils = require 'pl.utils'
local callable = require 'pl.types'.is_callable
local function_arg = utils.function_arg
local _List = utils.stdmt.List
local _Map = utils.stdmt.Map
Expand Down Expand Up @@ -465,7 +466,6 @@ end
---------------------- Sequence Adapters ---------------------

local SMT
local callable = utils.is_callable

local function SW (iter,...)
if callable(iter) then
Expand Down
23 changes: 6 additions & 17 deletions lua/pl/tablex.lua
Expand Up @@ -2,9 +2,10 @@
--
-- See @{02-arrays.md.Useful_Operations_on_Tables|the Guide}
--
-- Dependencies: `pl.utils`
-- Dependencies: `pl.utils`, `pl.types`
-- @module pl.tablex
local utils = require ('pl.utils')
local types = require ('pl.types')
local getmetatable,setmetatable,require = getmetatable,setmetatable,require
local tsort,append,remove = table.sort,table.insert,table.remove
local min,max = math.min,math.max
Expand All @@ -29,40 +30,28 @@ local function makelist (res)
return setmetatable(res,List)
end

local function check_meta (val)
if type(val) == 'table' then return true end
return getmetatable(val)
end

local function complain (idx,msg)
error(('argument %d is not %s'):format(idx,msg),3)
end

local function assert_arg_indexable (idx,val)
local mt = check_meta(val)
if mt == true then return end
if not(mt and mt.__len and mt.__index) then
if not types.is_indexable(val) then
complain(idx,"indexable")
end
end

local function assert_arg_iterable (idx,val)
local mt = check_meta(val)
if mt == true then return end
if not(mt and mt.__pairs) then
if not types.is_iterable(val) then
complain(idx,"iterable")
end
end

local function assert_arg_writeable (idx,val)
local mt = check_meta(val)
if mt == true then return end
if not(mt and mt.__newindex) then
if not types.is_writeable(val) then
complain(idx,"writeable")
end
end


--- copy a table into another, in-place.
-- @param t1 destination table
-- @param t2 source (any iterable object)
Expand Down Expand Up @@ -714,7 +703,7 @@ end
function tablex.set (t,val,i1,i2)
assert_arg_indexable(1,t)
i1,i2 = i1 or 1,i2 or #t
if utils.is_callable(val) then
if types.is_callable(val) then
for i = i1,i2 do
t[i] = val(i)
end
Expand Down
5 changes: 3 additions & 2 deletions lua/pl/text.lua
Expand Up @@ -15,13 +15,14 @@
-- > = '$name = $value' % {name='dog',value='Pluto'}
-- dog = Pluto
--
-- Dependencies: `pl.utils`
-- Dependencies: `pl.utils`, `pl.types`
-- @module pl.text

local gsub = string.gsub
local concat,append = table.concat,table.insert
local utils = require 'pl.utils'
local bind1,usplit,assert_arg,is_callable = utils.bind1,utils.split,utils.assert_arg,utils.is_callable
local bind1,usplit,assert_arg = utils.bind1,utils.split,utils.assert_arg
local is_callable = require 'pl.types'.is_callable
local unpack = utils.unpack

local function lstrip(str) return (str:gsub('^%s+','')) end
Expand Down

0 comments on commit 0e586d8

Please sign in to comment.