aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--files.lua56
-rw-r--r--files.moon44
-rw-r--r--nomsu.lua6
-rwxr-xr-xnomsu.moon9
-rw-r--r--nomsu_compiler.lua2
6 files changed, 60 insertions, 59 deletions
diff --git a/README.md b/README.md
index 5f6b7c7..11547dc 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ revolving around natural language rule-making and self modification.
## Dependencies
-Nomsu's only dependencies are [Lua 5.1 or later](https://www.lua.org/) or [Luajit](http://luajit.org/) and [LPEG](http://www.inf.puc-rio.br/~roberto/lpeg/) (`luarocks install lpeg`). Nomsu's compiler was written in [Moonscript](http://moonscript.org/), but all of the .moon files have been compiled into lua for convenience, so Moonscript is not a dependency.
+Nomsu's only dependencies are [Lua 5.2 or later](https://www.lua.org/) or [Luajit 2.0 or later](http://luajit.org/) and [LPEG](http://www.inf.puc-rio.br/~roberto/lpeg/) (`luarocks install lpeg`). Nomsu's compiler was written in [Moonscript](http://moonscript.org/), but all of the .moon files have been compiled into lua for convenience, so Moonscript is not a dependency. Optionally, if [luafilesystem](https://github.com/spacewander/luafilesystem) is installed, it will be used. Otherwise Nomsu will fall back to using system commands (`find` and `ls`), which is slower and a bit less safe. Nomsu has been tested on Mac and Linux, but not Windows.
## Usage
diff --git a/files.lua b/files.lua
index dfa52c2..0de29ed 100644
--- a/files.lua
+++ b/files.lua
@@ -2,6 +2,24 @@ local lpeg = require('lpeg')
local re = require('re')
local Files = { }
assert(package.nomsupath, "No package.nomsupath was found")
+local run_cmd
+run_cmd = function(cmd)
+ local f = io.popen(cmd)
+ local lines
+ do
+ local _accum_0 = { }
+ local _len_0 = 1
+ for line in f:lines() do
+ _accum_0[_len_0] = line
+ _len_0 = _len_0 + 1
+ end
+ lines = _accum_0
+ end
+ if not (f:close()) then
+ return nil
+ end
+ return lines
+end
local _SPOOFED_FILES = { }
local _FILE_CACHE = setmetatable({ }, {
__index = _SPOOFED_FILES
@@ -47,11 +65,11 @@ Files.exists = function(path)
if _SPOOFED_FILES[path] then
return true
end
- if not (io.popen("ls " .. tostring(sanitize(path))):close()) then
+ if run_cmd("ls " .. tostring(sanitize(path))) then
return true
end
for nomsupath in package.nomsupath:gmatch("[^;]+") do
- if not (io.popen("ls " .. tostring(nomsupath) .. "/" .. tostring(sanitize(path))):close()) then
+ if run_cmd("ls " .. tostring(nomsupath) .. "/" .. tostring(sanitize(path))) then
return true
end
end
@@ -66,24 +84,7 @@ browse = function(path)
path
}
else
- local result = false
- for nomsupath in package.nomsupath:gmatch("[^;]+") do
- local f = io.popen('find -L "' .. nomsupath .. '/' .. path .. '" -not -path "*/\\.*" -type f -name "*.nom"')
- do
- local _accum_0 = { }
- local _len_0 = 1
- for line in f:lines() do
- _accum_0[_len_0] = line
- _len_0 = _len_0 + 1
- end
- files = _accum_0
- end
- if f:close() then
- result = files
- break
- end
- end
- _BROWSE_CACHE[path] = result
+ _BROWSE_CACHE[path] = run_cmd('find -L "' .. path .. '" -not -path "*/\\.*" -type f') or false
end
end
return _BROWSE_CACHE[path]
@@ -121,19 +122,12 @@ if ok then
}
else
local file_type, err = lfs.attributes(filename, 'mode')
- if file_type == 'file' then
- if match(filename, "%.nom$") or match(filename, "%.lua$") then
- _BROWSE_CACHE[filename] = {
- filename
- }
- else
- _BROWSE_CACHE[filename] = false
- end
- elseif file_type == 'char device' then
+ local _exp_0 = file_type
+ if "file" == _exp_0 or "char device" == _exp_0 then
_BROWSE_CACHE[filename] = {
filename
}
- elseif file_type == 'directory' or file_type == 'link' then
+ elseif "directory" == _exp_0 or "link" == _exp_0 then
local files = { }
for subfile in lfs.dir(filename) do
local _continue_0 = false
@@ -162,7 +156,7 @@ if ok then
return _BROWSE_CACHE[filename]
end
else
- if io.popen('find . -maxdepth 0'):close() then
+ if not (run_cmd('find . -maxdepth 0')) then
error("Could not find 'luafilesystem' module and couldn't run system command `find` (this might happen on Windows). Please install `luafilesystem` (which can be found at: http://keplerproject.github.io/luafilesystem/ or `luarocks install luafilesystem`)", 0)
end
end
diff --git a/files.moon b/files.moon
index 2631db5..d62a030 100644
--- a/files.moon
+++ b/files.moon
@@ -4,6 +4,13 @@ re = require 're'
Files = {}
assert package.nomsupath, "No package.nomsupath was found"
+run_cmd = (cmd)->
+ f = io.popen(cmd)
+ lines = [line for line in f\lines!]
+ return nil unless f\close!
+ return lines
+
+
_SPOOFED_FILES = {}
_FILE_CACHE = setmetatable {}, __index:_SPOOFED_FILES
_BROWSE_CACHE = {}
@@ -39,9 +46,9 @@ sanitize = (path)->
Files.exists = (path)->
return true if _SPOOFED_FILES[path]
- return true unless io.popen("ls #{sanitize(path)}")\close!
+ return true if run_cmd("ls #{sanitize(path)}")
for nomsupath in package.nomsupath\gmatch("[^;]+")
- return true unless io.popen("ls #{nomsupath}/#{sanitize(path)}")\close!
+ return true if run_cmd("ls #{nomsupath}/#{sanitize(path)}")
return false
browse = (path)->
@@ -49,15 +56,7 @@ browse = (path)->
local files
_BROWSE_CACHE[path] = if _SPOOFED_FILES[path]
{path}
- else
- result = false
- for nomsupath in package.nomsupath\gmatch("[^;]+")
- f = io.popen('find -L "'..nomsupath..'/'..path..'" -not -path "*/\\.*" -type f -name "*.nom"')
- files = [line for line in f\lines!]
- if f\close!
- result = files
- break
- result
+ else run_cmd('find -L "'..path..'" -not -path "*/\\.*" -type f') or false
return _BROWSE_CACHE[path]
ok, lfs = pcall(require, "lfs")
@@ -79,23 +78,20 @@ if ok
{filename}
else
file_type, err = lfs.attributes(filename, 'mode')
- if file_type == 'file'
- if match(filename, "%.nom$") or match(filename, "%.lua$")
+ switch file_type
+ when "file", "char device"
{filename}
+ when "directory", "link"
+ files = {}
+ for subfile in lfs.dir(filename)
+ continue if subfile == "." or subfile == ".."
+ for f in *(browse(filename.."/"..subfile) or {})
+ files[#files+1] = f
+ files
else false
- elseif file_type == 'char device'
- {filename}
- elseif file_type == 'directory' or file_type == 'link'
- files = {}
- for subfile in lfs.dir(filename)
- continue if subfile == "." or subfile == ".."
- for f in *(browse(filename.."/"..subfile) or {})
- files[#files+1] = f
- files
- else false
return _BROWSE_CACHE[filename]
else
- if io.popen('find . -maxdepth 0')\close!
+ unless run_cmd('find . -maxdepth 0')
error "Could not find 'luafilesystem' module and couldn't run system command `find` (this might happen on Windows). Please install `luafilesystem` (which can be found at: http://keplerproject.github.io/luafilesystem/ or `luarocks install luafilesystem`)", 0
Files.walk = (path, flush_cache=false)->
diff --git a/nomsu.lua b/nomsu.lua
index 8953e99..cfb243d 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -1,3 +1,8 @@
+local EXIT_SUCCESS, EXIT_FAILURE = 0, 1
+if _VERSION == "Lua 5.1" and not jit then
+ print("Sorry, Nomsu does not run on Lua 5.1. Please use LuaJIT 2+ or Lua 5.2+")
+ os.exit(EXIT_FAILURE)
+end
if NOMSU_VERSION and NOMSU_PREFIX then
local ver_bits
do
@@ -52,7 +57,6 @@ if NOMSU_VERSION and NOMSU_PREFIX then
else
package.nomsupath = "."
end
-local EXIT_SUCCESS, EXIT_FAILURE = 0, 1
local usage = [=[Nomsu Compiler
Usage: (nomsu | lua nomsu.lua | moon nomsu.moon) [-V version] [-O optimization level] [-v] [-c] [-s] [-t] [-I file] [--help | -h] [--version] [--no-core] [file [nomsu args...]]
diff --git a/nomsu.moon b/nomsu.moon
index 44bdf65..bf6a1f6 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -1,5 +1,13 @@
#!/usr/bin/env moon
-- This file contains the command-line Nomsu runner.
+
+EXIT_SUCCESS, EXIT_FAILURE = 0, 1
+
+if _VERSION == "Lua 5.1" and not jit
+ -- Cannot run on Lua5.1 because it doesn't have gotos.
+ print("Sorry, Nomsu does not run on Lua 5.1. Please use LuaJIT 2+ or Lua 5.2+")
+ os.exit(EXIT_FAILURE)
+
if NOMSU_VERSION and NOMSU_PREFIX
ver_bits = [ver_bit for ver_bit in NOMSU_VERSION\gmatch("[0-9]+")]
partial_vers = [table.concat(ver_bits,'.',1,i) for i=#ver_bits,1,-1]
@@ -9,7 +17,6 @@ if NOMSU_VERSION and NOMSU_PREFIX
else
package.nomsupath = "."
-EXIT_SUCCESS, EXIT_FAILURE = 0, 1
usage = [=[
Nomsu Compiler
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index 56bd1e8..62a71fd 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -471,7 +471,7 @@ do
source = nil
end
local lua_string = tostring(lua)
- local run_lua_fn, err = load(lua_string, nil and tostring(source or lua.source), "t", self)
+ local run_lua_fn, err = load(lua_string, tostring(source or lua.source), "t", self)
if not run_lua_fn then
local line_numbered_lua = concat((function()
local _accum_0 = { }