Updating file stuff for better compatibility with Lua 5.2 and fixing

some bugs. Also updated README to provide more compatibility info and
list Lua5.2+ as a requirement.
This commit is contained in:
Bruce Hill 2018-07-24 16:43:06 -07:00
parent 6014c5aa43
commit b1c0446a3c
6 changed files with 60 additions and 59 deletions

View File

@ -6,7 +6,7 @@ revolving around natural language rule-making and self modification.
## Dependencies ## 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 ## Usage

View File

@ -2,6 +2,24 @@ local lpeg = require('lpeg')
local re = require('re') local re = require('re')
local Files = { } local Files = { }
assert(package.nomsupath, "No package.nomsupath was found") 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 _SPOOFED_FILES = { }
local _FILE_CACHE = setmetatable({ }, { local _FILE_CACHE = setmetatable({ }, {
__index = _SPOOFED_FILES __index = _SPOOFED_FILES
@ -47,11 +65,11 @@ Files.exists = function(path)
if _SPOOFED_FILES[path] then if _SPOOFED_FILES[path] then
return true return true
end end
if not (io.popen("ls " .. tostring(sanitize(path))):close()) then if run_cmd("ls " .. tostring(sanitize(path))) then
return true return true
end end
for nomsupath in package.nomsupath:gmatch("[^;]+") do 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 return true
end end
end end
@ -66,24 +84,7 @@ browse = function(path)
path path
} }
else else
local result = false _BROWSE_CACHE[path] = run_cmd('find -L "' .. path .. '" -not -path "*/\\.*" -type f') or 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
end end
end end
return _BROWSE_CACHE[path] return _BROWSE_CACHE[path]
@ -121,19 +122,12 @@ if ok then
} }
else else
local file_type, err = lfs.attributes(filename, 'mode') local file_type, err = lfs.attributes(filename, 'mode')
if file_type == 'file' then local _exp_0 = file_type
if match(filename, "%.nom$") or match(filename, "%.lua$") then if "file" == _exp_0 or "char device" == _exp_0 then
_BROWSE_CACHE[filename] = {
filename
}
else
_BROWSE_CACHE[filename] = false
end
elseif file_type == 'char device' then
_BROWSE_CACHE[filename] = { _BROWSE_CACHE[filename] = {
filename filename
} }
elseif file_type == 'directory' or file_type == 'link' then elseif "directory" == _exp_0 or "link" == _exp_0 then
local files = { } local files = { }
for subfile in lfs.dir(filename) do for subfile in lfs.dir(filename) do
local _continue_0 = false local _continue_0 = false
@ -162,7 +156,7 @@ if ok then
return _BROWSE_CACHE[filename] return _BROWSE_CACHE[filename]
end end
else 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) 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
end end

View File

@ -4,6 +4,13 @@ re = require 're'
Files = {} Files = {}
assert package.nomsupath, "No package.nomsupath was found" 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 = {} _SPOOFED_FILES = {}
_FILE_CACHE = setmetatable {}, __index:_SPOOFED_FILES _FILE_CACHE = setmetatable {}, __index:_SPOOFED_FILES
_BROWSE_CACHE = {} _BROWSE_CACHE = {}
@ -39,9 +46,9 @@ sanitize = (path)->
Files.exists = (path)-> Files.exists = (path)->
return true if _SPOOFED_FILES[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("[^;]+") 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 return false
browse = (path)-> browse = (path)->
@ -49,15 +56,7 @@ browse = (path)->
local files local files
_BROWSE_CACHE[path] = if _SPOOFED_FILES[path] _BROWSE_CACHE[path] = if _SPOOFED_FILES[path]
{path} {path}
else else run_cmd('find -L "'..path..'" -not -path "*/\\.*" -type f') or false
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
return _BROWSE_CACHE[path] return _BROWSE_CACHE[path]
ok, lfs = pcall(require, "lfs") ok, lfs = pcall(require, "lfs")
@ -79,23 +78,20 @@ if ok
{filename} {filename}
else else
file_type, err = lfs.attributes(filename, 'mode') file_type, err = lfs.attributes(filename, 'mode')
if file_type == 'file' switch file_type
if match(filename, "%.nom$") or match(filename, "%.lua$") when "file", "char device"
{filename} {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 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] return _BROWSE_CACHE[filename]
else 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 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)-> Files.walk = (path, flush_cache=false)->

View File

@ -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 if NOMSU_VERSION and NOMSU_PREFIX then
local ver_bits local ver_bits
do do
@ -52,7 +57,6 @@ if NOMSU_VERSION and NOMSU_PREFIX then
else else
package.nomsupath = "." package.nomsupath = "."
end end
local EXIT_SUCCESS, EXIT_FAILURE = 0, 1
local usage = [=[Nomsu Compiler 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...]] 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...]]

View File

@ -1,5 +1,13 @@
#!/usr/bin/env moon #!/usr/bin/env moon
-- This file contains the command-line Nomsu runner. -- 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 if NOMSU_VERSION and NOMSU_PREFIX
ver_bits = [ver_bit for ver_bit in NOMSU_VERSION\gmatch("[0-9]+")] 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] 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 else
package.nomsupath = "." package.nomsupath = "."
EXIT_SUCCESS, EXIT_FAILURE = 0, 1
usage = [=[ usage = [=[
Nomsu Compiler Nomsu Compiler

View File

@ -471,7 +471,7 @@ do
source = nil source = nil
end end
local lua_string = tostring(lua) 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 if not run_lua_fn then
local line_numbered_lua = concat((function() local line_numbered_lua = concat((function()
local _accum_0 = { } local _accum_0 = { }