[lua-torch-trepl] 01/01: Imported Upstream version 0~20160613-g06128f9
Zhou Mo
cdluminate-guest at moszumanska.debian.org
Sat Jun 18 07:31:36 UTC 2016
This is an automated email from the git hooks/post-receive script.
cdluminate-guest pushed a commit to branch master
in repository lua-torch-trepl.
commit d034522cf69fa53369c8e0dd017ef2f35dcbbf35
Author: Zhou Mo <cdluminate at gmail.com>
Date: Sat Jun 18 07:30:11 2016 +0000
Imported Upstream version 0~20160613-g06128f9
---
.gitignore | 2 +
LICENSE | 27 ++
README.md | 192 ++++++++++++++
colorize.lua | 11 +
colors.lua | 27 ++
doc/index.md | 196 +++++++++++++++
init.lua | 697 +++++++++++++++++++++++++++++++++++++++++++++++++++
mkdocs.yml | 8 +
readline.c | 316 +++++++++++++++++++++++
th | 200 +++++++++++++++
trepl-scm-1.rockspec | 56 +++++
utils.c | 41 +++
12 files changed, 1773 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..9d22eb4
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.o
+*.so
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..4e8363d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,27 @@
+Copyright (c) 2015, Torch
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+* Neither the name of trepl nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..fa7cbff
--- /dev/null
+++ b/README.md
@@ -0,0 +1,192 @@
+# TREPL: A REPL for Torch
+
+```
+ ______ __ | Torch7
+ /_ __/__ ________/ / | Scientific computing for LuaJIT.
+ / / / _ \/ __/ __/ _ \ |
+ /_/ \___/_/ \__/_//_/ | https://github.com/torch
+ | http://torch.ch
+
+th>
+```
+
+A pure Lua [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)
+for LuaJIT, with heavy support for Torch types.
+
+Uses Readline for tab completion.
+
+This package installs a new binary named `th`, which
+comes packed with all these features:
+
+Features:
+
+ * Tab-completion on nested namespaces
+ * Tab-completion on disk files (when opening a string)
+ * History
+ * Pretty print (table introspection and coloring)
+ * Auto-print after eval (can be stopped with ;)
+ * Each command is profiled, timing is reported
+ * No need for '=' to print
+ * Easy help with: `? funcname`
+ * Self help: `?`
+ * Shell commands with: $ cmd (example: `$ ls`)
+ * Print all user globals with `who()`
+ * Import a package's symbols globally with `import(package)`
+ * Require is overloaded to provide relative search paths: `require('./mylocallib/')`
+ * Optional strict global namespace monitoring
+ * Optional async repl (based on [async](https://github.com/clementfarabet/async))
+
+Install
+-------
+
+Via luarocks:
+
+```
+luarocks install trepl
+```
+
+Launch
+------
+
+We install a binary, simple to remember:
+
+```
+th
+> -- amazing repl!
+```
+
+Alternatively, you can always bring up the repl by loading it as a lib,
+from anywhere:
+
+```
+luajit
+> repl = require 'trepl'
+> repl()
+```
+
+Use
+---
+
+Completion:
+
+```lua
+> cor+TAB ... completes to: coroutine
+```
+
+History:
+
+```lua
+> ARROW_UP | ARROW_DOWN
+```
+
+Help (shortcut to Torch's help method):
+
+```lua
+> ? torch.FloatTensor
+prints help...
+```
+
+Shell commands:
+
+```lua
+> $ ls
+README.md
+init.lua
+trepl-scm-1.rockspec
+
+[Lua # 2] > $ ll
+...
+
+> $ ls
+...
+```
+
+History / last results. Two variables are used:
+
+```
+_RESULTS: contains the history of results:
+
+> a = 1
+> a
+1
+> 'test'
+test
+> _RESULTS
+{
+ 1 : 1
+ 2 : test
+}
+
+_LAST: contains the last result
+> _LAST
+test
+
+Convenient to get output from shell commands:
+> $ ls -l
+> _LAST
+contains the results from ls -l, in a string.
+```
+
+Hide output. By default, TREPL always tries to dump
+the content of what's evaluated. Use ; to stop it.
+
+```lua
+> a = torch.Tensor(3)
+> a:zero()
+0
+0
+0
+[torch.DoubleTensor of dimension 3]
+
+> a:zero();
+>
+```
+
+Helpers
+-------
+
+Colors libraries can be loaded independently:
+
+```lua
+> c = require 'trepl.colorize'
+> print(c.red('a red string') .. c.Blue('a bold blue string'))
+```
+
+Globals
+-------
+
+Global variables are a well known issue with Lua. `th` can be run
+with a flag `-g` that will monitor global variables creation and access.
+
+Creation of a variable will generate a warning message, while access
+will generate an error.
+
+```sh
+th -g
+> require 'sys';
+created global variable: sys @ [c-module]
+> a = 1
+created global variable: a @ a = 1
+> b
+error: attempt to read undeclared variable b
+```
+
+Async repl [BETA]
+-----------------
+
+An asynchronous repl can be started with `-a`. Based on [async](https://github.com/clementfarabet/async),
+this repl is non-blocking, and can be used to spawn/schedule asyncrhonous jobs. It is still beta,
+and does not yet have readline support:
+
+```sh
+th -a
+> idx = 1
+> async.setInterval(1000, function() print('will be printed every second - step #' .. idx) idx = idx + 1 end)
+will be printed every second - step #1
+will be printed every second - step #2
+will be printed every second - step #3
+> idx = 20
+will be printed every second - step #20
+will be printed every second - step #21
+```
+
diff --git a/colorize.lua b/colorize.lua
new file mode 100644
index 0000000..0192397
--- /dev/null
+++ b/colorize.lua
@@ -0,0 +1,11 @@
+local colors = require 'trepl.colors'
+
+local f = {}
+
+for name in pairs(colors) do
+ f[name] = function(txt)
+ return colors[name] .. txt .. colors.none
+ end
+end
+
+return f
diff --git a/colors.lua b/colors.lua
new file mode 100644
index 0000000..9930380
--- /dev/null
+++ b/colors.lua
@@ -0,0 +1,27 @@
+return {
+ none = '\27[0m',
+ black = '\27[0;30m',
+ red = '\27[0;31m',
+ green = '\27[0;32m',
+ yellow = '\27[0;33m',
+ blue = '\27[0;34m',
+ magenta = '\27[0;35m',
+ cyan = '\27[0;36m',
+ white = '\27[0;37m',
+ Black = '\27[1;30m',
+ Red = '\27[1;31m',
+ Green = '\27[1;32m',
+ Yellow = '\27[1;33m',
+ Blue = '\27[1;34m',
+ Magenta = '\27[1;35m',
+ Cyan = '\27[1;36m',
+ White = '\27[1;37m',
+ _black = '\27[40m',
+ _red = '\27[41m',
+ _green = '\27[42m',
+ _yellow = '\27[43m',
+ _blue = '\27[44m',
+ _magenta = '\27[45m',
+ _cyan = '\27[46m',
+ _white = '\27[47m'
+}
diff --git a/doc/index.md b/doc/index.md
new file mode 100644
index 0000000..0a7d94f
--- /dev/null
+++ b/doc/index.md
@@ -0,0 +1,196 @@
+# TREPL: A REPL for Torch
+
+```
+ ______ __ | Torch7
+ /_ __/__ ________/ / | Scientific computing for LuaJIT.
+ / / / _ \/ __/ __/ _ \ |
+ /_/ \___/_/ \__/_//_/ | https://github.com/torch
+ | http://torch.ch
+
+th>
+```
+
+A pure Lua [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)
+for LuaJIT, with heavy support for Torch types.
+
+Uses Readline for tab completion, with code borrowed from
+[iluajit](https://github.com/jdesgats/ILuaJIT).
+
+If Readline is not found, it defaults to using
+[Linenoise](https://github.com/hoelzro/lua-linenoise),
+which is significantly more simplistic.
+
+This package installs a new binary named `th`, which
+comes packed with all these features:
+
+Features:
+
+ * Tab-completion on nested namespaces
+ * Tab-completion on disk files (when opening a string)
+ * History
+ * Pretty print (table introspection and coloring)
+ * Auto-print after eval (can be stopped with ;)
+ * Each command is profiled, timing is reported
+ * No need for '=' to print
+ * Easy help with: `? funcname`
+ * Self help: `?`
+ * Shell commands with: $ cmd (example: `$ ls`)
+ * Print all user globals with `who()`
+ * Import a package's symbols globally with `import(package)`
+ * Require is overloaded to provide relative search paths: `require('./mylocallib/')`
+ * Optional strict global namespace monitoring
+ * Optional async repl (based on [async](https://github.com/clementfarabet/async))
+
+Install
+-------
+
+Via luarocks:
+
+```
+luarocks install trepl
+```
+
+Launch
+------
+
+We install a binary, simple to remember:
+
+```
+th
+> -- amazing repl!
+```
+
+Alternatively, you can always bring up the repl by loading it as a lib,
+from anywhere:
+
+```
+luajit
+> repl = require 'trepl'
+> repl()
+```
+
+Use
+---
+
+Completion:
+
+```lua
+> cor+TAB ... completes to: coroutine
+```
+
+History:
+
+```lua
+> ARROW_UP | ARROW_DOWN
+```
+
+Help (shortcut to Torch's help method):
+
+```lua
+> ? torch.FloatTensor
+prints help...
+```
+
+Shell commands:
+
+```lua
+> $ ls
+README.md
+init.lua
+trepl-scm-1.rockspec
+
+[Lua # 2] > $ ll
+...
+
+> $ ls
+...
+```
+
+History / last results. Two variables are used:
+
+```
+_RESULTS: contains the history of results:
+
+> a = 1
+> a
+1
+> 'test'
+test
+> _RESULTS
+{
+ 1 : 1
+ 2 : test
+}
+
+_LAST: contains the last result
+> _LAST
+test
+
+Convenient to get output from shell commands:
+> $ ls -l
+> _LAST
+contains the results from ls -l, in a string.
+```
+
+Hide output. By default, TREPL always tries to dump
+the content of what's evaluated. Use ; to stop it.
+
+```lua
+> a = torch.Tensor(3)
+> a:zero()
+0
+0
+0
+[torch.DoubleTensor of dimension 3]
+
+> a:zero();
+>
+```
+
+Helpers
+-------
+
+Colors libraries can be loaded independently:
+
+```lua
+> c = require 'trepl.colorize'
+> print(c.red('a red string') .. c.Blue('a bold blue string'))
+```
+
+Globals
+-------
+
+Global variables are a well known issue with Lua. `th` can be run
+with a flag `-g` that will monitor global variables creation and access.
+
+Creation of a variable will generate a warning message, while access
+will generate an error.
+
+```sh
+th -g
+> require 'sys';
+created global variable: sys @ [c-module]
+> a = 1
+created global variable: a @ a = 1
+> b
+error: attempt to read undeclared variable b
+```
+
+Async repl [BETA]
+-----------------
+
+An asynchronous repl can be started with `-a`. Based on [async](https://github.com/clementfarabet/async),
+this repl is non-blocking, and can be used to spawn/schedule asyncrhonous jobs. It is still beta,
+and does not yet have readline support:
+
+```sh
+th -a
+> idx = 1
+> async.setInterval(1000, function() print('will be printed every second - step #' .. idx) idx = idx + 1 end)
+will be printed every second - step #1
+will be printed every second - step #2
+will be printed every second - step #3
+> idx = 20
+will be printed every second - step #20
+will be printed every second - step #21
+```
diff --git a/init.lua b/init.lua
new file mode 100644
index 0000000..167843d
--- /dev/null
+++ b/init.lua
@@ -0,0 +1,697 @@
+--[============================================================================[
+ REPL: A REPL for Lua (with support for Torch objects).
+
+ This REPL is embeddable, and doesn't depend on C libraries.
+ If readline.so is built and found at runtime, then tab-completion is enabled.
+
+ Support for SHELL commands:
+ > $ ls
+ > $ ll
+ > $ ls -l
+ (prepend any command by $, from the Lua interpreter)
+
+ Copyright: MIT / BSD / Do whatever you want with it.
+ Clement Farabet, 2013
+--]============================================================================]
+
+-- Require Torch
+pcall(require,'torch')
+pcall(require,'paths')
+pcall(require,'sys')
+pcall(require,'xlua')
+local dok_loaded_ok = pcall(require,'dok')
+
+
+-- Colors:
+local colors = require 'trepl.colors'
+local col = require 'trepl.colorize'
+
+-- Lua 5.2 compatibility
+local loadstring = loadstring or load
+local unpack = unpack or table.unpack
+
+-- Kill colors:
+function noColors()
+ for k,v in pairs(colors) do
+ colors[k] = ''
+ end
+end
+
+local cutils = require 'treplutils'
+
+-- best effort isWindows. Not robust
+local function isWindows()
+ return type(package) == 'table' and
+ type(package.config) == 'string' and
+ package.config:sub(1,1) == '\\'
+end
+
+if isWindows() or (not cutils.isatty()) then
+ noColors()
+else
+ local outp = os.execute('tput colors >/dev/null')
+ if type(outp) == 'boolean' and not outp then
+ noColors()
+ elseif type(outp) == 'number' and outp ~= 0 then
+ noColors()
+ end
+end
+
+-- Help string:
+local selfhelp = [[
+ ______ __
+ /_ __/__ ________/ /
+ / / / _ \/ __/ __/ _ \
+ /_/ \___/_/ \__/_//_/
+
+]]..col.red('th')..[[ is an enhanced interpreter (repl) for Torch7/Lua.
+
+]]..col.blue('Features:')..[[
+
+ Tab-completion on nested namespaces
+ Tab-completion on disk files (when opening a string)
+ History stored in:
+
+ ]]..col.magenta("_RESULTS")..[[
+ ]]..col.magenta("_LAST")..[[
+
+ Pretty print (table introspection and coloring)
+ Auto-print after eval (no need for '='), can be stopped with ]]..col.magenta(";")..[[
+ Each command is profiled, timing is reported
+ Easy help on functions/packages:
+
+ ]]..col.magenta("? torch.randn")..[[
+
+ Documentation browsable with:
+
+ ]]..col.magenta("browse()")..[[
+ ]]..col.magenta("browse(package)")..[[
+
+ Shell commands with:
+
+ ]]..col.magenta("$ ls -l")..[[
+
+ and the string result can be retrieved with:
+
+ ]]..col.magenta("_LAST")..[[
+
+ Print all user globals with:
+
+ ]]..col.magenta("who()")..[[
+
+ Clear screen with:
+
+ ]]..col.magenta("<Ctrl> L")..[[
+
+ Quit Torch7 with:
+
+ ]]..col.magenta("<Ctrl> D")..[[
+
+ Import a package's symbols globally with:
+
+ ]]..col.magenta("import 'torch'")..[[
+
+ Require is overloaded to provide relative (form within a file) search paths:
+
+ ]]..col.magenta("require './local/lib' ")..[[
+
+ Optional strict global namespace monitoring:
+
+ ]]..col.magenta('th -g')..[[
+
+ Optional async repl (based on https://github.com/clementfarabet/async):
+
+ ]]..col.magenta('th -a')..[[
+
+ Using colors:
+
+ ]]..col.magenta("c = require 'trepl.colorize'")..[[
+ print(c.red(]]..col.red("'a red string'")..[[) .. c.blue(]]..col.blue("'a blue string'")..[[))
+]]
+
+-- If no Torch:
+if not torch then
+ torch = {
+ typename = function() return '' end,
+ setheaptracking = function() end
+ }
+end
+
+-- helper
+local function sizestr(x)
+ local strt = {}
+ if _G.torch.typename(x):find('torch.*Storage') then
+ return _G.torch.typename(x):match('torch%.(.+)') .. ' - size: ' .. x:size()
+ end
+ if x:nDimension() == 0 then
+ table.insert(strt, _G.torch.typename(x):match('torch%.(.+)') .. ' - empty')
+ else
+ table.insert(strt, _G.torch.typename(x):match('torch%.(.+)') .. ' - size: ')
+ for i=1,x:nDimension() do
+ table.insert(strt, x:size(i))
+ if i ~= x:nDimension() then
+ table.insert(strt, 'x')
+ end
+ end
+ end
+ return table.concat(strt)
+end
+
+-- k : name of variable
+-- m : max length
+local function printvar(key,val,m)
+ local name = '[' .. tostring(key) .. ']'
+ --io.write(name)
+ name = name .. string.rep(' ',m-name:len()+2)
+ local tp = type(val)
+ if tp == 'userdata' then
+ tp = torch.typename(val) or ''
+ if tp:find('torch.*Tensor') then
+ tp = sizestr(val)
+ elseif tp:find('torch.*Storage') then
+ tp = sizestr(val)
+ else
+ tp = tostring(val)
+ end
+ elseif tp == 'table' then
+ if torch.type(val) == 'table' then
+ tp = tp .. ' - size: ' .. #val
+ else
+ tp = torch.type(val)
+ end
+ elseif tp == 'string' then
+ local tostr = val:gsub('\n','\\n')
+ if #tostr>40 then
+ tostr = tostr:sub(1,40) .. '...'
+ end
+ tp = tp .. ' : "' .. tostr .. '"'
+ else
+ tp = tostring(val)
+ end
+ return name .. ' = ' .. tp
+end
+
+-- helper
+local function getmaxlen(vars)
+ local m = 0
+ if type(vars) ~= 'table' then return tostring(vars):len() end
+ for k,v in pairs(vars) do
+ local s = tostring(k)
+ if s:len() > m then
+ m = s:len()
+ end
+ end
+ return m
+end
+
+-- overload print:
+if not print_old then
+ print_old=print
+end
+
+-- a function to colorize output:
+local function colorize(object,nested)
+ -- Apply:
+ local apply = col
+
+ -- Type?
+ if object == nil then
+ return apply['Black']('nil')
+ elseif type(object) == 'number' then
+ return apply['cyan'](tostring(object))
+ elseif type(object) == 'boolean' then
+ return apply['blue'](tostring(object))
+ elseif type(object) == 'string' then
+ if nested then
+ return apply['Black']('"')..apply['green'](object)..apply['Black']('"')
+ else
+ return apply['none'](object)
+ end
+ elseif type(object) == 'function' then
+ return apply['magenta'](tostring(object))
+ elseif type(object) == 'userdata' or type(object) == 'cdata' then
+ local tp = torch.typename(object) or ''
+ if tp:find('torch.*Tensor') then
+ tp = sizestr(object)
+ elseif tp:find('torch.*Storage') then
+ tp = sizestr(object)
+ else
+ tp = tostring(object)
+ end
+ if tp ~= '' then
+ return apply['red'](tp)
+ else
+ return apply['red'](tostring(object))
+ end
+ elseif type(object) == 'table' then
+ return apply['green'](tostring(object))
+ else
+ return apply['_black'](tostring(object))
+ end
+end
+
+-- This is a new recursive, colored print.
+local ndepth = 4
+function print_new(...)
+ local function rawprint(o)
+ io.write(tostring(o or '') .. '\n')
+ io.flush()
+ end
+ local objs = {...}
+ local function printrecursive(obj,depth)
+ local depth = depth or 0
+ local tab = depth*4
+ local line = function(s) for i=1,tab do io.write(' ') end rawprint(s) end
+ if next(obj) then
+ line('{')
+ tab = tab+2
+ for k,v in pairs(obj) do
+ if type(v) == 'table' then
+ if depth >= (ndepth-1) or next(v) == nil then
+ line(tostring(k) .. ' : {...}')
+ else
+ line(tostring(k) .. ' : ') printrecursive(v,depth+1)
+ end
+ else
+ line(tostring(k) .. ' : ' .. colorize(v,true))
+ end
+ end
+ tab = tab-2
+ line('}')
+ else
+ line('{}')
+ end
+ end
+ for i = 1,select('#',...) do
+ local obj = select(i,...)
+ if type(obj) ~= 'table' then
+ if type(obj) == 'userdata' or type(obj) == 'cdata' then
+ rawprint(obj)
+ else
+ io.write(colorize(obj) .. '\t')
+ if i == select('#',...) then
+ rawprint()
+ end
+ end
+ elseif getmetatable(obj) and getmetatable(obj).__tostring then
+ rawprint(obj)
+ else
+ printrecursive(obj)
+ end
+ end
+end
+
+
+function setprintlevel(n)
+ if n == nil or n < 0 then
+ error('expected number [0,+)')
+ end
+ n = math.floor(n)
+ ndepth = n
+ if ndepth == 0 then
+ print = print_old
+ else
+ print = print_new
+ end
+end
+setprintlevel(5)
+
+-- Import, ala Python
+function import(package, forced)
+ local ret = require(package)
+ local symbols = {}
+ if _G[package] then
+ _G._torchimport = _G._torchimport or {}
+ _G._torchimport[package] = _G[package]
+ symbols = _G[package]
+ elseif ret and type(ret) == 'table' then
+ _G._torchimport = _G._torchimport or {}
+ _G._torchimport[package] = ret
+ symbols = ret
+ end
+ for k,v in pairs(symbols) do
+ if not _G[k] or forced then
+ _G[k] = v
+ end
+ end
+end
+
+-- Smarter require (ala Node.js)
+local drequire = require
+function require(name)
+ if name:find('^%.') then
+ local file = debug.getinfo(2).source:gsub('^@','')
+ local dir = '.'
+ if paths.filep(file) then
+ dir = paths.dirname(file)
+ end
+ local pkgpath = paths.concat(dir,name)
+ if paths.filep(pkgpath..'.lua') then
+ return dofile(pkgpath..'.lua')
+ elseif pkgpath:find('%.th$') and paths.filep(pkgpath) then
+ return torch.load(pkgpath)
+ elseif pkgpath:find('%.net$') and paths.filep(pkgpath) then
+ require 'nn'
+ return torch.load(pkgpath)
+ elseif pkgpath:find('%.json$') and paths.filep(pkgpath) then
+ return require('cjson').decode(io.open(pkgpath):read('*all'))
+ elseif pkgpath:find('%.csv$') and paths.filep(pkgpath) then
+ return require('csv').load(pkgpath)
+ elseif paths.filep(pkgpath) then
+ return dofile(pkgpath)
+ elseif paths.filep(pkgpath..'.th') then
+ return torch.load(pkgpath..'.th')
+ elseif paths.filep(pkgpath..'.net') then
+ require 'nn'
+ return torch.load(pkgpath..'.net')
+ elseif paths.filep(pkgpath..'.json') then
+ return require('cjson').decode(io.open(pkgpath..'.json'):read('*all'))
+ elseif paths.filep(pkgpath..'.csv') then
+ return require('csv').load(pkgpath..'.csv')
+ elseif paths.filep(pkgpath..'.so') then
+ return package.loadlib(pkgpath..'.so', 'luaopen_'..path.basename(name))()
+ elseif paths.filep(pkgpath..'.dylib') then
+ return package.loadlib(pkgpath..'.dylib', 'luaopen_'..path.basename(name))()
+ else
+ local initpath = paths.concat(pkgpath,'init.lua')
+ return dofile(initpath)
+ end
+ else
+ local ok,res = pcall(drequire,name)
+ if not ok then
+ local ok2,res2 = pcall(require,'./'..name)
+ if not ok2 then
+ error(res)
+ end
+ return res2
+ end
+ return res
+ end
+end
+
+-- Who
+-- a simple function that prints all the symbols defined by the user
+-- very much like Matlab's who function
+function who(system)
+ local m = getmaxlen(_G)
+ local p = _G._preloaded_
+ local function printsymb(sys)
+ for k,v in pairs(_G) do
+ if (sys and p[k]) or (not sys and not p[k]) then
+ print(printvar(k,_G[k],m))
+ end
+ end
+ end
+ if system then
+ print('== System Variables ==')
+ printsymb(true)
+ end
+ print('== User Variables ==')
+ printsymb(false)
+ print('==')
+end
+
+-- Monitor Globals
+function monitor_G(cb)
+ -- user CB or strict mode
+ local strict
+ if type(cb) == 'boolean' then
+ strict = true
+ cb = nil
+ end
+
+ -- Force load of penlight packages:
+ stringx = require 'pl.stringx'
+ tablex = require 'pl.tablex'
+ path = require 'pl.path'
+ dir = require 'pl.dir'
+ text = require 'pl.text'
+
+ -- Store current globals:
+ local evercreated = {}
+ for k in pairs(_G) do
+ evercreated[k] = true
+ end
+
+ -- Overwrite global namespace meta tables to monitor it:
+ setmetatable(_G, {
+ __newindex = function(G,key,val)
+ if not evercreated[key] then
+ if cb then
+ cb(key,'newindex')
+ else
+ local file = debug.getinfo(2).source:gsub('^@','')
+ local line = debug.getinfo(2).currentline
+ local report = print
+ if strict then
+ report = error
+ end
+ if line > 0 then
+ report(colors.red .. 'created global variable: '
+ .. colors.blue .. key .. colors.none
+ .. ' @ ' .. colors.magenta .. file .. colors.none
+ .. ':' .. colors.green .. line .. colors.none
+ )
+ else
+ report(colors.red .. 'created global variable: '
+ .. colors.blue .. key .. colors.none
+ .. ' @ ' .. colors.yellow .. '[C-module]' .. colors.none
+ )
+ end
+ end
+ end
+ evercreated[key] = true
+ rawset(G,key,val)
+ end,
+ __index = function (table, key)
+ if cb then
+ cb(key,'index')
+ else
+ local file = debug.getinfo(2).source:gsub('^@','')
+ local line = debug.getinfo(2).currentline
+ local report = print
+ if strict then
+ report = error
+ end
+ if line > 0 then
+ report(colors.red .. 'atempt to read undeclared variable: '
+ .. colors.blue .. key .. colors.none
+ .. ' @ ' .. colors.magenta .. file .. colors.none
+ .. ':' .. colors.green .. line .. colors.none
+ )
+ else
+ report(colors.red .. 'atempt to read undeclared variable: '
+ .. colors.blue .. key .. colors.none
+ .. ' @ ' .. colors.yellow .. '[C-module]' .. colors.none
+ )
+ end
+ end
+ end,
+ })
+end
+
+-- Tracekback (error printout)
+local function traceback(message)
+ local tp = type(message)
+ if tp ~= "string" and tp ~= "number" then return message end
+ local debug = _G.debug
+ if type(debug) ~= "table" then return message end
+ local tb = debug.traceback
+ if type(tb) ~= "function" then return message end
+ return tb(message)
+end
+
+-- Prompt:
+local function prompt(aux)
+ local s
+ if not aux then
+ s = 'th> '
+ else
+ s = '..> '
+ end
+ return s
+end
+
+-- Aliases:
+local aliases = [[
+ alias ls='ls -GF';
+ alias ll='ls -lhF';
+ alias la='ls -ahF';
+ alias lla='ls -lahF';
+]]
+
+-- Penlight
+pcall(require,'pl')
+
+-- Useful globally, from penlight
+if text then
+ text.format_operator()
+end
+
+-- Reults:
+_RESULTS = {}
+_LAST = ''
+
+-- Readline:
+local readline_ok,readline = pcall(require,'readline')
+local nextline,saveline
+if readline_ok and (os.getenv('HOME') or os.getenv('USERPROFILE')) ~= nil then
+ -- Readline found:
+ local history = (os.getenv('HOME') or os.getenv('USERPROFILE')) .. '/.luahistory'
+ readline.setup()
+ readline.read_history(history)
+ nextline = function(aux)
+ return readline.readline(prompt(aux))
+ end
+ saveline = function(line)
+ readline.add_history(line)
+ readline.write_history(history)
+ end
+else
+ -- No readline... default to plain io:
+ nextline = function(aux)
+ io.write(prompt(aux)) io.flush()
+ return io.read('*line')
+ end
+ saveline = function() end
+end
+
+-- The repl
+function repl()
+ -- Timer
+ local timer_start, timer_stop
+ if torch and torch.Timer then
+ local t = torch.Timer()
+ local start = 0
+ timer_start = function()
+ start = t:time().real
+ end
+ timer_stop = function()
+ local step = t:time().real - start
+ for i = 1,70 do io.write(' ') end
+ print(col.Black(string.format('[%0.04fs]', step)))
+ end
+ else
+ timer_start = function() end
+ timer_stop = function() end
+ end
+
+ -- REPL:
+ while true do
+ -- READ:
+ local line = nextline()
+
+ -- Interupt?
+ if not line or line == 'exit' then
+ io.write('Do you really want to exit ([y]/n)? ') io.flush()
+ local line = io.read('*l')
+ if not line or line == '' or line:lower() == 'y' then
+ if not line then print('') end
+ os.exit()
+ end
+ end
+ if line == 'break' then
+ break
+ end
+
+ -- OS Commands:
+ if line and line:find('^%s-%$') then
+ line = line:gsub('^%s-%$','')
+ if io.popen then
+ local f = io.popen(aliases .. ' ' .. line)
+ local res = f:read('*a')
+ f:close()
+ io.write(col._black(res)) io.flush()
+ table.insert(_RESULTS, res)
+ else
+ os.execute(aliases .. ' ' .. line)
+ end
+ line = nil
+ end
+
+ -- Support the crappy '=', as Lua does:
+ if line and line:find('^%s-=') then
+ line = line:gsub('^%s-=','')
+ end
+
+ -- Shortcut to get help:
+ if line and line:find('^%s-?') then
+ if line:gsub('^%s-?','') == '' then
+ print(selfhelp)
+ line = nil
+ elseif dok_loaded_ok then
+ line = 'help(' .. line:gsub('^%s-?','') .. ')'
+ else
+ print('error: could not load help backend')
+ line = nil
+ end
+ end
+
+ -- EVAL:
+ if line then
+ -- Try to load line first, for multiline support:
+ local valid = loadstring('return ' .. line) or loadstring(line)
+ while not valid do
+ local nline = nextline(true)
+ if nline == '' or not nline then
+ break
+ end
+ line = line .. '\n' .. nline
+ valid = loadstring(line)
+ end
+
+ -- Execute, first by trying to auto return result:
+ timer_start()
+ local done = false
+ local err
+ if not (line:find(';%s-$') or line:find('^%s-print')) then
+ -- Try to compile statement with "return", to auto-print
+ local parsed = loadstring('local f = function() return '..line..' end')
+ if parsed then
+ local parsed = loadstring('_RESULT={'..line..'}')
+ local ok,err=xpcall(parsed, traceback)
+ if ok then
+ print(unpack(_RESULT))
+ table.insert(_RESULTS,_RESULT[1])
+ else
+ print(err)
+ end
+ done = true
+ end
+ end
+
+ -- If not done executing, execute normally:
+ if not done then
+ -- We only get here if statement could not be printed/returned
+ local parsed,perr = loadstring(line)
+ if not parsed then
+ print('syntax error: ' .. perr)
+ else
+ local ok,err = xpcall(parsed, traceback)
+ if not ok then
+ print(err)
+ end
+ end
+ end
+ timer_stop()
+ end
+
+ -- Last result:
+ _LAST = _RESULTS[#_RESULTS]
+
+ -- Save:
+ saveline(line)
+ end
+end
+
+-- Store preloaded symbols, for who()
+_G._preloaded_ = {}
+for k,v in pairs(_G) do
+ _G._preloaded_[k] = true
+end
+
+-- Enable heap tracking
+torch.setheaptracking(true)
+
+-- return repl, just call it to start it!
+return repl
diff --git a/mkdocs.yml b/mkdocs.yml
new file mode 100644
index 0000000..7252eaf
--- /dev/null
+++ b/mkdocs.yml
@@ -0,0 +1,8 @@
+site_name: trepl
+theme : simplex
+repo_url : https://github.com/torch/trepl
+use_directory_urls : false
+markdown_extensions: [extra]
+docs_dir : doc
+pages:
+- [index.md, TREPL]
diff --git a/readline.c b/readline.c
new file mode 100644
index 0000000..e14c867
--- /dev/null
+++ b/readline.c
@@ -0,0 +1,316 @@
+// Bindings to readline
+#include <stdlib.h>
+#include <string.h>
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+#include <readline/readline.h>
+#include <readline/history.h>
+#include <ctype.h>
+
+#if LUA_VERSION_NUM == 501
+# define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX)
+# define luaL_setfuncs(L, libs, _) luaL_register(L, NULL, libs)
+#else
+# define lua_strlen lua_rawlen
+#endif
+
+/*
+** Lua 5.1.4 advanced readline support for the GNU readline and history
+** libraries or compatible replacements.
+**
+** Author: Mike Pall.
+** Maintainer: Sean Bolton (sean at smbolton dot com).
+**
+** Copyright (C) 2004-2006, 2011 Mike Pall. Same license as Lua. See lua.h.
+**
+** Advanced features:
+** - Completion of keywords and global variable names.
+** - Recursive and metatable-aware completion of variable names.
+** - Context sensitive delimiter completion.
+** - Save/restore of the history to/from a file (LUA_HISTORY env variable).
+** - Setting a limit for the size of the history (LUA_HISTSIZE env variable).
+** - Setting the app name to allow for $if lua ... $endif in ~/.inputrc.
+**
+** Start lua and try these (replace ~ with the TAB key):
+**
+** ~~
+** fu~foo() ret~fa~end<CR>
+** io~~~s~~~o~~~w~"foo\n")<CR>
+**
+** The ~~ are just for demonstration purposes (io~s~o~w~ suffices, of course).
+**
+** If you are used to zsh/tcsh-style completion support, try adding
+** 'TAB: menu-complete' and 'C-d: possible-completions' to your ~/.inputrc.
+**
+** The patch has been successfully tested with:
+**
+** GNU readline 2.2.1 (1998-07-17)
+** GNU readline 4.0 (1999-02-18) [harmless compiler warning]
+** GNU readline 4.3 (2002-07-16)
+** GNU readline 5.0 (2004-07-27)
+** GNU readline 5.1 (2005-12-07)
+** GNU readline 5.2 (2006-10-11)
+** GNU readline 6.0 (2009-02-20)
+** GNU readline 6.2 (2011-02-13)
+** MacOSX libedit 2.11 (2008-07-12)
+** NETBSD libedit 2.6.5 (2002-03-25)
+** NETBSD libedit 2.6.9 (2004-05-01)
+**
+** Change Log:
+** 2004-2006 Mike Pall - original patch
+** 2009/08/24 Sean Bolton - updated for GNU readline version 6
+** 2011/12/14 Sean Bolton - fixed segfault when using Mac OS X libedit 2.11
+*/
+
+static char *lua_rl_hist;
+static int lua_rl_histsize;
+
+static lua_State *lua_rl_L; /* User data is not passed to rl callbacks. */
+
+/* Reserved keywords. */
+static const char *const lua_rl_keywords[] = {
+ "and", "break", "do", "else", "elseif", "end", "false",
+ "for", "function", "if", "in", "local", "nil", "not", "or",
+ "repeat", "return", "then", "true", "until", "while", NULL
+};
+
+static int valididentifier(const char *s)
+{
+ if (!(isalpha(*s) || *s == '_')) return 0;
+ for (s++; *s; s++) if (!(isalpha(*s) || isdigit(*s) || *s == '_')) return 0;
+ return 1;
+}
+
+/* Dynamically resizable match list. */
+typedef struct {
+ char **list;
+ size_t idx, allocated, matchlen;
+} dmlist;
+
+/* Add prefix + string + suffix to list and compute common prefix. */
+static int lua_rl_dmadd(dmlist *ml, const char *p, size_t pn, const char *s,
+ int suf)
+{
+ char *t = NULL;
+
+ if (ml->idx+1 >= ml->allocated &&
+ !(ml->list = realloc(ml->list, sizeof(char *)*(ml->allocated += 32))))
+ return -1;
+
+ if (s) {
+ size_t n = strlen(s);
+ if (!(t = (char *)malloc(sizeof(char)*(pn+n+(suf?2:1))))) return 1;
+ memcpy(t, p, pn);
+ memcpy(t+pn, s, n);
+ n += pn;
+ t[n] = suf;
+ if (suf) t[++n] = '\0';
+
+ if (ml->idx == 0) {
+ ml->matchlen = n;
+ } else {
+ size_t i;
+ for (i = 0; i < ml->matchlen && i < n && ml->list[1][i] == t[i]; i++) ;
+ ml->matchlen = i; /* Set matchlen to common prefix. */
+ }
+ }
+
+ ml->list[++ml->idx] = t;
+ return 0;
+}
+
+/* Get __index field of metatable of object on top of stack. */
+static int lua_rl_getmetaindex(lua_State *L)
+{
+ if (!lua_getmetatable(L, -1)) { lua_pop(L, 1); return 0; }
+
+ /* prefer __metatable if it exists */
+ lua_pushstring(L, "__metatable");
+ lua_rawget(L, -2);
+ if(lua_istable(L, -1))
+ {
+ lua_remove(L, -2);
+ return 1;
+ }
+ else
+ lua_pop(L, 1);
+
+ lua_pushstring(L, "__index");
+ lua_rawget(L, -2);
+ lua_replace(L, -2);
+ if (lua_isnil(L, -1) || lua_rawequal(L, -1, -2)) { lua_pop(L, 2); return 0; }
+ lua_replace(L, -2);
+ return 1;
+} /* 1: obj -- val, 0: obj -- */
+
+/* Get field from object on top of stack. Avoid calling metamethods. */
+static int lua_rl_getfield(lua_State *L, const char *s, size_t n)
+{
+ int i = 20; /* Avoid infinite metatable loops. */
+ do {
+ if (lua_istable(L, -1)) {
+ lua_pushlstring(L, s, n);
+ lua_rawget(L, -2);
+ if (!lua_isnil(L, -1)) { lua_replace(L, -2); return 1; }
+ lua_pop(L, 1);
+ }
+ } while (--i > 0 && lua_rl_getmetaindex(L));
+ lua_pop(L, 1);
+ return 0;
+} /* 1: obj -- val, 0: obj -- */
+
+static int lua_rl_isglobaltable(lua_State *L, int idx)
+{
+#if LUA_VERSION_NUM == 501
+ return lua_rawequal(L, idx, LUA_GLOBALSINDEX);
+#else
+ idx = lua_absindex(L, idx);
+ lua_pushglobaltable(L);
+ int same = lua_rawequal(L, idx, -1);
+ lua_pop(L, 1);
+ return same;
+#endif
+}
+
+/* Completion callback. */
+static char **lua_rl_complete(const char *text, int start, int end)
+{
+ lua_State *L = lua_rl_L;
+ dmlist ml;
+ const char *s;
+ size_t i, n, dot, loop;
+ int savetop;
+
+ if (!(text[0] == '\0' || isalpha(text[0]) || text[0] == '_')) return NULL;
+
+ ml.list = NULL;
+ ml.idx = ml.allocated = ml.matchlen = 0;
+
+ savetop = lua_gettop(L);
+ lua_pushglobaltable(L);
+ for (n = (size_t)(end-start), i = dot = 0; i < n; i++)
+ if (text[i] == '.' || text[i] == ':') {
+ if (!lua_rl_getfield(L, text+dot, i-dot))
+ goto error; /* Invalid prefix. */
+ dot = i+1; /* Points to first char after dot/colon. */
+ }
+
+ /* Add all matches against keywords if there is no dot/colon. */
+ if (dot == 0)
+ for (i = 0; (s = lua_rl_keywords[i]) != NULL; i++)
+ if (!strncmp(s, text, n) && lua_rl_dmadd(&ml, NULL, 0, s, ' '))
+ goto error;
+
+ /* Add all valid matches from all tables/metatables. */
+ loop = 0; /* Avoid infinite metatable loops. */
+ do {
+ if (lua_istable(L, -1) &&
+ (loop == 0 || !lua_rl_isglobaltable(L, -1)))
+ for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1))
+ if (lua_type(L, -2) == LUA_TSTRING) {
+ s = lua_tostring(L, -2);
+ /* Only match names starting with '_' if explicitly requested. */
+ if (!strncmp(s, text+dot, n-dot) && valididentifier(s) &&
+ (*s != '_' || text[dot] == '_')) {
+ int suf = ' '; /* Default suffix is a space. */
+ switch (lua_type(L, -1)) {
+ case LUA_TTABLE: suf = '.'; break; /* No way to guess ':'. */
+ case LUA_TFUNCTION: suf = '('; break;
+ case LUA_TUSERDATA:
+ if (lua_getmetatable(L, -1)) { lua_pop(L, 1); suf = ':'; }
+ break;
+ }
+ if (lua_rl_dmadd(&ml, text, dot, s, suf)) goto error;
+ }
+ }
+ } while (++loop < 20 && lua_rl_getmetaindex(L));
+
+ if (ml.idx == 0) {
+error:
+ lua_settop(L, savetop);
+ return NULL;
+ } else {
+ /* list[0] holds the common prefix of all matches (may be ""). */
+ /* If there is only one match, list[0] and list[1] will be the same. */
+ if (!(ml.list[0] = (char *)malloc(sizeof(char)*(ml.matchlen+1))))
+ goto error;
+ memcpy(ml.list[0], ml.list[1], ml.matchlen);
+ ml.list[0][ml.matchlen] = '\0';
+ /* Add the NULL list terminator. */
+ if (lua_rl_dmadd(&ml, NULL, 0, NULL, 0)) goto error;
+ }
+
+ lua_settop(L, savetop);
+#if RL_READLINE_VERSION >= 0x0600
+ rl_completion_suppress_append = 1;
+#endif
+ return ml.list;
+}
+
+/* Initialize readline library. */
+static int f_setup(lua_State *L)
+{
+ char *s;
+
+ lua_rl_L = L;
+
+ /* This allows for $if lua ... $endif in ~/.inputrc. */
+ rl_readline_name = "lua";
+ /* Break words at every non-identifier character except '.' and ':'. */
+ rl_completer_word_break_characters =
+ "\t\r\n !\"#$%&'()*+,-/;<=>?@[\\]^`{|}~";
+ rl_completer_quote_characters = "\"'";
+/* #if RL_READLINE_VERSION < 0x0600 */
+ rl_completion_append_character = '\0';
+/* #endif */
+ rl_attempted_completion_function = lua_rl_complete;
+ rl_initialize();
+
+ return 0;
+}
+
+static int f_readline(lua_State* L)
+{
+ const char* prompt = lua_tostring(L,1);
+ const char* line = readline(prompt);
+ lua_pushstring(L,line);
+ free((void *)line); // Lua makes a copy...
+ return 1;
+}
+
+static int f_add_history(lua_State* L)
+{
+ if (lua_strlen(L,1) > 0)
+ add_history(lua_tostring(L, 1));
+ return 0;
+}
+
+static int f_write_history(lua_State* L)
+{
+ if (lua_strlen(L,1) > 0)
+ write_history(lua_tostring(L, 1));
+ return 0;
+}
+
+static int f_read_history(lua_State* L)
+{
+ if (lua_strlen(L,1) > 0)
+ read_history(lua_tostring(L, 1));
+ return 0;
+}
+
+static const struct luaL_Reg lib[] = {
+ {"readline", f_readline},
+ {"add_history",f_add_history},
+ {"write_history",f_write_history},
+ {"read_history",f_read_history},
+ {"setup",f_setup},
+ {NULL, NULL},
+};
+
+int luaopen_readline (lua_State *L) {
+ lua_newtable(L);
+ luaL_setfuncs(L, lib, 0);
+ return 1;
+}
diff --git a/th b/th
new file mode 100644
index 0000000..a5c8ee2
--- /dev/null
+++ b/th
@@ -0,0 +1,200 @@
+#!/usr/bin/env luajit
+
+loadstring = loadstring or load -- for lua 5.2 compat
+
+-- Tracekback (error printout)
+local function traceback(message)
+ local tp = type(message)
+ if tp ~= "string" and tp ~= "number" then return message end
+ local debug = _G.debug
+ if type(debug) ~= "table" then return message end
+ local tb = debug.traceback
+ if type(tb) ~= "function" then return message end
+ return tb(message)
+end
+
+-- help
+local help = [==[
+Usage: th [options] [script.lua [arguments]]
+
+Options:
+ -l name load library name
+ -e statement execute statement
+ -h,--help print this help
+ -a,--async preload async (libuv) and start async repl (BETA)
+ -g,--globals monitor global variables (print a warning on creation/access)
+ -gg,--gglobals monitor global variables (throw an error on creation/access)
+ -x,--gfx start gfx server and load gfx env
+ -i,--interactive enter the REPL after executing a script ]==]
+
+-- parse arguments
+local asyncrepl,run,interactive,progargs,statement,lib,globs,sglobs,lgfx,_
+local parg = arg
+local nextarg
+for _,arg in ipairs(parg) do
+ -- nextarg set?
+ if nextarg == 'exec' then
+ statement = arg
+ nextarg = nil
+ elseif nextarg == 'lib' then
+ lib = arg
+ nextarg = nil
+ elseif not progargs then
+ _,_,lib = arg:find('^%-l(.*)')
+ if lib == '' then lib = nil end
+ end
+ -- load libraries
+ if lib then
+ local ok, err = xpcall(function() require(lib) end, traceback)
+ if not ok then
+ print('could not load ' .. lib .. ', skipping')
+ print(err)
+ end
+ elseif progargs then
+ -- program args
+ table.insert(progargs,arg)
+ elseif not statement then
+ -- option?
+ local _,_,option = arg:find('^%-%-(.*)')
+ local shortopt
+ if not option then
+ _,_,shortopt = arg:find('^%-(.*)')
+ end
+ if option or shortopt then
+ -- help
+ if shortopt == 'h' or option == 'help' then
+ print(help)
+ return
+ elseif shortopt == 'i' or option == 'interactive' then
+ interactive = true
+ elseif shortopt == 'e' then
+ nextarg = 'exec'
+ elseif shortopt == 'a' or option == 'async' then
+ asyncrepl = true
+ interactive = true
+ async = require 'async'
+ elseif shortopt == 'g' or option == 'globals' then
+ globs = true
+ elseif shortopt == 'gg' or option == 'gglobals' then
+ sglobs = true
+ elseif shortopt == 'x' or option == 'gfx' then
+ lgfx = true
+ elseif shortopt == 'l' then
+ nextarg = 'lib'
+ else
+ -- unknown
+ print('Error: unrecognized flag --' .. (option ~= nil and option or shortopt))
+ print(help)
+ return
+ end
+ else
+ -- exec program
+ run = arg
+ progargs = {}
+ for k,v in pairs(parg) do
+ if k <= 0 then
+ progargs[k] = v
+ end
+ end
+ end
+ end
+end
+
+-- load repl
+local repl = require 'trepl'
+local col = require 'trepl.colorize'
+
+-- load gfx env?
+if lgfx then
+ local ok = pcall(require, 'gfx.js')
+ if ok then
+ gfx.startserver()
+ gfx.clear()
+ gfx.show()
+ sys.sleep(1)
+ else
+ print('could not load gfx.js, please install with: luarocks install gfx.js')
+ end
+end
+
+-- monitor globals
+if globs then
+ monitor_G()
+elseif sglobs then
+ monitor_G(true)
+end
+
+-- statement
+if statement then
+ -- exec statement:
+ local s = loadstring(statement)
+ local ok,res = pcall(s)
+ if not ok then
+ print(res)
+ return
+ end
+ -- quit by default
+ if not interactive then return end
+end
+
+-- run program
+if run then
+ -- set prog args:
+ arg = progargs
+ -- run
+ dofile(run)
+ -- quit by default
+ if not interactive then return end
+end
+
+-- start repl
+if asyncrepl then
+ -- verbose
+ print(
+[[
+
+ ______ __ ]]..col.Black[[| Torch7 ]]..[[
+ /_ __/__ ________/ / ]]..col.Black[[| ]]..col.magenta[[Scientific computing for Lua.]]..[[
+ / / / _ \/ __/ __/ _ \ ]]..col.Black[[| Type ? for help ]]..[[
+ /_/ \___/_/ \__/_//_/ ]]..col.Black[[| ]]..col.blue[[https://github.com/torch ]]..[[
+ ]]..col.Black[[| ]]..col.blue[[http://torch.ch ]]..[[
+
+]] .. col.red('WARNING: ') .. col.Black('you are running an experimental asynchronous interpreter for Torch.') .. [[
+]] .. col.Black('Note 1: It has no support for readline/completion yet.') .. [[
+]] .. col.Black('Note 2: The event-loop has been started in the background: ') .. col.none('async.go()') .. [[
+]] .. col.Black(' Statements like ') .. col.none('async.setInterval(1000, function() print("test") end)') .. [[
+]] .. col.Black(' are run asynchronously to the interpreter. ') .. [[
+]] .. col.Black('Note 3: See ') .. col.blue('http://github.com/clementfarabet/async') .. col.Black(' for help' ) .. [[
+
+]]
+)
+
+ -- BETA: async repl
+ async.repl()
+ async.go()
+
+else
+
+ -- verbose
+ print(
+[[
+
+ ______ __ ]]..col.Black[[| Torch7 ]]..[[
+ /_ __/__ ________/ / ]]..col.Black[[| ]]..col.magenta[[Scientific computing for Lua.]]..[[
+ / / / _ \/ __/ __/ _ \ ]]..col.Black[[| Type ? for help ]]..[[
+ /_/ \___/_/ \__/_//_/ ]]..col.Black[[| ]]..col.blue[[https://github.com/torch ]]..[[
+ ]]..col.Black[[| ]]..col.blue[[http://torch.ch ]]..[[
+]]
+)
+
+ -- globs?
+ if globs then
+ print(
+ col.red('WARNING: ') .. col.Black('global monitoring is on.\n') ..
+ col.Black('This is typically not a good idea when running a live interpreter.\n')
+ )
+ end
+
+ -- regular repl
+ repl()
+end
diff --git a/trepl-scm-1.rockspec b/trepl-scm-1.rockspec
new file mode 100644
index 0000000..54269b0
--- /dev/null
+++ b/trepl-scm-1.rockspec
@@ -0,0 +1,56 @@
+package = "trepl"
+version = "scm-1"
+
+source = {
+ url = "git://github.com/torch/trepl",
+ branch = "master",
+}
+
+description = {
+ summary = "An embedabble, Lua-only REPL for Torch.",
+ detailed = [[
+An embedabble, Lua-only REPL for Torch.
+ ]],
+ homepage = "https://github.com/torch/trepl",
+ license = "BSD"
+}
+
+dependencies = {
+ "torch >= 7.0",
+ "penlight >= 1.1.0",
+}
+
+build = {
+ type = "builtin",
+ modules = {
+ ['trepl.init'] = 'init.lua',
+ ['trepl.colors'] = 'colors.lua',
+ ['trepl.colorize'] = 'colorize.lua',
+ ['readline'] = {
+ sources = {'readline.c'},
+ libraries = {'readline'}
+ },
+ ['treplutils'] = {
+ sources = {'utils.c'},
+ }
+ },
+ platforms = {
+ windows = {
+ modules = {
+ ['readline'] = {
+ sources = {'readline.c'},
+ libraries = {'readline'},
+ defines = {"USE_READLINE_STATIC"},
+ incdirs = {"windows"},
+ libdirs = {"windows"},
+ libraries = {'readline-win'}
+ }
+ }
+ }
+ },
+ install = {
+ bin = {
+ 'th'
+ }
+ }
+}
diff --git a/utils.c b/utils.c
new file mode 100644
index 0000000..62a3e4e
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,41 @@
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+
+#if LUA_VERSION_NUM == 501
+# define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX)
+# define luaL_setfuncs(L, libs, _) luaL_register(L, NULL, libs)
+#else
+# define lua_strlen lua_rawlen
+#endif
+
+#if defined(_WIN32) || defined(LUA_WIN)
+
+int treplutils_isatty(lua_State *L)
+{
+ lua_pushboolean(L, 0);
+ return 1;
+}
+
+#else
+
+#include <unistd.h>
+
+int treplutils_isatty(lua_State *L)
+{
+ lua_pushboolean(L, isatty(1));
+ return 1;
+}
+
+#endif
+
+static const struct luaL_Reg utils[] = {
+ {"isatty", treplutils_isatty},
+ {NULL, NULL}
+};
+
+int luaopen_treplutils(lua_State *L) {
+ lua_newtable(L);
+ luaL_setfuncs(L, utils, 0);
+ return 1;
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/lua-torch-trepl.git
More information about the debian-science-commits
mailing list