aboutsummaryrefslogtreecommitdiff
path: root/nomsu.lua
diff options
context:
space:
mode:
Diffstat (limited to 'nomsu.lua')
-rw-r--r--nomsu.lua183
1 files changed, 62 insertions, 121 deletions
diff --git a/nomsu.lua b/nomsu.lua
index 2f8856f..88bf986 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -1,70 +1,19 @@
+if NOMSU_VERSION and NOMSU_PREFIX then
+ package.path = tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(NOMSU_VERSION) .. "/?.lua;" .. package.path
+ package.cpath = tostring(NOMSU_PREFIX) .. "/lib/nomsu/" .. tostring(NOMSU_VERSION) .. "/?.so;" .. package.cpath
+end
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
- local _accum_0 = { }
- local _len_0 = 1
- for ver_bit in NOMSU_VERSION:gmatch("[0-9]+") do
- _accum_0[_len_0] = ver_bit
- _len_0 = _len_0 + 1
- end
- ver_bits = _accum_0
- end
- local partial_vers
- do
- local _accum_0 = { }
- local _len_0 = 1
- for i = #ver_bits, 1, -1 do
- _accum_0[_len_0] = table.concat(ver_bits, '.', 1, i)
- _len_0 = _len_0 + 1
- end
- partial_vers = _accum_0
- end
- package.path = table.concat((function()
- local _accum_0 = { }
- local _len_0 = 1
- for _index_0 = 1, #partial_vers do
- local v = partial_vers[_index_0]
- _accum_0[_len_0] = tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(v) .. "/?.lua"
- _len_0 = _len_0 + 1
- end
- return _accum_0
- end)(), ";") .. ";" .. package.path
- package.cpath = table.concat((function()
- local _accum_0 = { }
- local _len_0 = 1
- for _index_0 = 1, #partial_vers do
- local v = partial_vers[_index_0]
- _accum_0[_len_0] = tostring(NOMSU_PREFIX) .. "/lib/nomsu/" .. tostring(v) .. "/?.so"
- _len_0 = _len_0 + 1
- end
- return _accum_0
- end)(), ";") .. ";" .. package.cpath
- package.nomsupath = table.concat((function()
- local _accum_0 = { }
- local _len_0 = 1
- for _index_0 = 1, #partial_vers do
- local v = partial_vers[_index_0]
- _accum_0[_len_0] = tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(v)
- _len_0 = _len_0 + 1
- end
- return _accum_0
- end)(), ";") .. ";."
-else
- package.nomsupath = "."
-end
local usage = [=[Nomsu Compiler
-Usage: (nomsu | lua nomsu.lua | moon nomsu.moon) [-V version] [--help | -h] [--version] [-O optimization level] [-v] [-c] [-s] [-d debugger] [--no-core] [(file | -t tool | -e "nomsu code..." | -m files... [--]) [nomsu args...]]
+Usage: (nomsu | lua nomsu.lua | moon nomsu.moon) [-V version] [--help | -h] [--version] [-O optimization level] [-v] [-c] [-s] [-d debugger] [--no-core] [(file | -t tool | -e "nomsu code..." | files... -- ) [nomsu args...]]
OPTIONS
-t <tool> Run a tool.
-e Execute the specified string.
- -m Run multiple files (all extra arguments).
-O <level> Run the compiler with the given optimization level (>0: use precompiled .lua versions of Nomsu files, when available).
-v Verbose: print compiled lua code.
-c Compile the input files into a .lua files.
@@ -96,21 +45,20 @@ do
local _obj_0 = require('containers')
List, Dict, Text = _obj_0.List, _obj_0.Dict, _obj_0.Text
end
-local nomsu_environment = require('nomsu_environment')
-if not arg or debug.getinfo(2).func == require then
- return nomsu_environment
-end
local sep = "\3"
local parser = re.compile([[ args <- {| (flag %sep)*
- (("-e" %sep {:execute: {[^%sep]+} :} %sep)
- / {:files: {|
- ("-t" %sep {~ {[^%sep]+} -> "nomsu://tools/%1.nom" ~} %sep
- / "-m" %sep (!("--" %sep) {[^%sep]+} %sep)* ("--" %sep)?
- / {[^%sep]+} %sep
- / {~ '' %sep? -> 'nomsu://tools/repl.nom' ~}) |} :})
+ {:files: {|
+ ( ("-m" %sep)? (!("--" %sep) file)* ("--" %sep)
+ / file
+ / {~ '' %sep? -> 'nomsu://tools/repl.nom' ~}) |} :}
{:nomsu_args: {| (nomsu_flag %sep)* {:extras: {| ({[^%sep]+} %sep)* |} :} |} :}
|} !.
+ file <-
+ ( "-e" %sep ({[^%sep]+} -> spoof) %sep
+ / "-t" %sep {~ {[^%sep]+} -> "nomsu://tools/%1.nom" ~} %sep
+ / {[^%sep]+} %sep)
+
flag <- longflag / shortflag / "-" shortboolflag+
longflag <-
{:help: "--help" %true :}
@@ -132,7 +80,8 @@ local parser = re.compile([[ args <- {| (flag %sep)*
]], {
["true"] = lpeg.Cc(true),
number = lpeg.R("09") ^ 1 / tonumber,
- sep = lpeg.P(sep)
+ sep = lpeg.P(sep),
+ spoof = Files.spoof
})
local arg_string = table.concat(arg, sep) .. sep
local args = parser:match(arg_string)
@@ -147,74 +96,62 @@ for _index_0 = 1, #_list_0 do
nomsu_args[argpair.key] = argpair.value
end
nomsu_args.extras = List(args.nomsu_args.extras or { })
+local optimization = tonumber(args.optimization or 1)
+local nomsupath = { }
+if NOMSU_VERSION and NOMSU_PREFIX then
+ if optimization > 0 then
+ table.insert(nomsupath, tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(NOMSU_VERSION) .. "/?.lua")
+ table.insert(nomsupath, tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(NOMSU_VERSION) .. "/?/init.lua")
+ end
+ table.insert(nomsupath, tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(NOMSU_VERSION) .. "/?.nom")
+ table.insert(nomsupath, tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(NOMSU_VERSION) .. "/?/init.nom")
+end
+if NOMSU_PACKAGEPATH then
+ if optimization > 0 then
+ table.insert(nomsupath, tostring(NOMSU_PACKAGEPATH) .. "/nomsu/?.lua")
+ table.insert(nomsupath, tostring(NOMSU_PACKAGEPATH) .. "/nomsu/?/init.lua")
+ end
+ table.insert(nomsupath, tostring(NOMSU_PACKAGEPATH) .. "/nomsu/?.nom")
+ table.insert(nomsupath, tostring(NOMSU_PACKAGEPATH) .. "/nomsu/?/init.nom")
+end
+if optimization > 0 then
+ table.insert(nomsupath, "./?.lua")
+ table.insert(nomsupath, "./?/init.lua")
+end
+table.insert(nomsupath, "./?.nom")
+table.insert(nomsupath, "./?/init.nom")
+package.nomsupath = table.concat(nomsupath, ";")
+local nomsu_environment = require('nomsu_environment')
nomsu_environment.COMMAND_LINE_ARGS = nomsu_args
-nomsu_environment.OPTIMIZATION = tonumber(args.optimization or 1)
+nomsu_environment.OPTIMIZATION = optimization
+nomsu_environment.NOMSU_PACKAGEPATH = NOMSU_PACKAGEPATH
+nomsu_environment.NOMSU_PREFIX = NOMSU_PREFIX
local run
run = function()
if not (args.no_core) then
- for nomsupath in package.nomsupath:gmatch("[^;]+") do
- local _continue_0 = false
- repeat
- local files = Files.list(nomsupath .. "/core")
- if not (files) then
- _continue_0 = true
- break
- end
- for _index_0 = 1, #files do
- local _continue_1 = false
- repeat
- local f = files[_index_0]
- if not (f:match("%.nom$")) then
- _continue_1 = true
- break
- end
- nomsu_environment.run_file_1_in(f, nomsu_environment, nomsu_environment.OPTIMIZATION)
- _continue_1 = true
- until true
- if not _continue_1 then
- break
- end
- end
- _continue_0 = true
- until true
- if not _continue_0 then
- break
- end
- end
+ nomsu_environment:export("core")
end
if args.version then
- nomsu_environment.run_1_in("say (Nomsu version)", nomsu_environment)
+ nomsu_environment:run("say (Nomsu version)")
os.exit(EXIT_SUCCESS)
end
local input_files = { }
- if args.execute then
- table.insert(input_files, Files.spoof("<input command>", args.execute))
- end
if args.files then
local _list_1 = args.files
for _index_0 = 1, #_list_1 do
local f = _list_1[_index_0]
- local files
do
- local nomsu_name = f:match("^nomsu://(.*)")
+ local nomsu_name = f:match("^nomsu://(.*)%.nom")
if nomsu_name then
- for nomsupath in package.nomsupath:gmatch("[^;]+") do
- files = Files.list(nomsupath .. "/" .. nomsu_name)
- if files then
- break
- end
+ local path, err = package.searchpath(nomsu_name, package.nomsupath, "/")
+ if not path then
+ error(err)
end
+ table.insert(input_files, path)
else
- files = Files.list(f)
+ table.insert(input_files, f)
end
end
- if not (files and #files > 0) then
- error("Could not find: '" .. tostring(f) .. "'")
- end
- for _index_1 = 1, #files do
- local filename = files[_index_1]
- table.insert(input_files, filename)
- end
end
end
for _index_0 = 1, #input_files do
@@ -241,13 +178,13 @@ run = function()
}
end
for chunk_no, chunk in ipairs(tree) do
- local lua = nomsu_environment.compile(chunk)
+ local lua = nomsu_environment:compile(chunk)
lua:declare_locals()
lua:prepend((chunk_no > 1) and '\n' or '', "-- File " .. tostring(filename) .. " chunk #" .. tostring(chunk_no) .. "\n")
if args.verbose then
print(lua:text())
end
- nomsu_environment.run_1_in(chunk, nomsu_environment)
+ nomsu_environment:run(chunk)
output:write(lua:text(), "\n")
end
print(("Compiled %-25s -> %s"):format(filename, filename:gsub("%.nom$", ".lua")))
@@ -263,14 +200,18 @@ run = function()
}
end
for chunk_no, chunk in ipairs(tree) do
- local lua = nomsu_environment.compile(chunk)
+ local lua = nomsu_environment:compile(chunk)
lua:declare_locals()
lua:prepend((chunk_no > 1) and '\n' or '', "-- File " .. tostring(filename) .. " chunk #" .. tostring(chunk_no) .. "\n")
print(lua:text())
- nomsu_environment.run_1_in(lua, nomsu_environment)
+ nomsu_environment:run(lua)
end
else
- nomsu_environment.run_file_1_in(filename, nomsu_environment, 0)
+ local f = Files.read(filename)
+ if filename:match("%.lua$") then
+ f = LuaCode:from(Source(filename, 1, #f), f)
+ end
+ nomsu_environment:run(f)
end
end
end