123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | if pcall(debug.getlocal, 4, 1) then
return require("nomsu_library")
end
local clibtype = package.cpath:match("?%.(so)") or package.cpath:match("?%.(dll)")
if 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) .. "/?." .. tostring(clibtype) .. ";") .. package.cpath
end
local COLOR_ENABLED = true
if clibtype == "dll" then
local ok, enable_colors = pcall(require, 'wincolors')
COLOR_ENABLED = ok and pcall(enable_colors)
pcall(os.execute, "chcp 65001>nul")
end
local Text = require('text')
local EXIT_FAILURE = 1
local EXIT_SUCCESS = 0
local usage = [=[Nomsu Usage: nomsu [-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.
-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.
-s Check the input files for syntax errors.
-d <debugger> Attempt to use the specified debugger to wrap the main body of execution.
-h/--help Print this message.
--version Print the version number and exit.
--no-core Skip loading the Nomsu core by default.
-V specify which Nomsu version is desired.
<file> The Nomsu file to run (can be "-" to use stdin).
]=]
local ok, lpeg = pcall(require, 'lpeg')
local re = ok and require('re')
if not ok then
print("Error: unable to find the 'lpeg' Lua module. Please install LPEG either from http://www.inf.puc-rio.br/~roberto/lpeg/re.html or, if you use luarocks: `luarocks install lpeg`")
os.exit(EXIT_FAILURE)
end
local Files = require("files")
local NomsuCode, LuaCode, Source
do
local _obj_0 = require("code_obj")
NomsuCode, LuaCode, Source = _obj_0.NomsuCode, _obj_0.LuaCode, _obj_0.Source
end
local sep = "\3"
local parser = re.compile([[ args <- {| (flag %sep)*
{:files: {|
( ("-m" %sep)? (file %sep)+ "--" %sep
/ file %sep
/ {~ '' %sep? -> 'nomsu://tools/repl.nom' ~}) |} :}
{:nomsu_args: {| (nomsu_flag %sep)* {:extras: {| ({[^%sep]+} %sep)* |} :} |} :}
|} ({.+}?)
file <-
( "-e" %sep ({[^%sep]+} -> spoof)
/ "-t" %sep {~ {[^%sep]+} -> "nomsu://tools/%1.nom" ~}
/ !"--" {[^%sep]+})
flag <- longflag / shortflag / "-" shortboolflag+
longflag <-
{:help: "--help" %true :}
/ {:version: "--version" %true :}
/ {:no_core: "--no-core" %true :}
shortflag <-
{:optimization: "-O" %sep? %number :}
/ {:debugger: ("-d" %sep? {[^%sep]+}) :}
/ {:requested_version: "-V" %sep? {([0-9.])+} :}
shortboolflag <-
{:check_syntax: "s" %true:}
/ {:compile: "c" %true:}
/ {:verbose: "v" %true :}
/ {:help: "h" %true :}
nomsu_flag <- nomsu_longflag / "-" nomsu_shortboolflag+
nomsu_shortboolflag <- {| {:key: [a-zA-Z] :} {:value: %true :} |}
nomsu_longflag <- '--' {| {:key: [^%sep=]+ :} {:value: ('=' {[^%sep]+}) / %true :} |}
]], {
["true"] = lpeg.Cc(true),
number = lpeg.R("09") ^ 1 / tonumber,
sep = lpeg.P(sep),
spoof = Files.spoof
})
local arg_string = table.concat(arg, sep) .. sep
local args, err = parser:match(arg_string)
if not args or err then
if err then
print("Didn't understand: " .. tostring(err))
end
print(usage)
os.exit(EXIT_FAILURE)
end
if args.help then
print("Nomsu - A dynamically typed programming language with natural syntax and strong metaprogramming abilities.")
print("https://nomsu.bruce-hill.com\n")
print(usage)
os.exit(EXIT_SUCCESS)
end
if args.version then
print(table.concat(require("nomsu_version"), "."))
os.exit(EXIT_SUCCESS)
end
local nomsu_args = { }
local _list_0 = args.nomsu_args
for _index_0 = 1, #_list_0 do
local argpair = _list_0[_index_0]
nomsu_args[argpair.key] = argpair.value
end
nomsu_args.extras = args.nomsu_args.extras or { }
local optimization = tonumber(args.optimization or 1)
local new_instance, LIBRARY_VERSION
do
local _obj_0 = require('nomsu_environment')
new_instance, LIBRARY_VERSION = _obj_0.new_instance, _obj_0.NOMSU_VERSION
end
local search_path, file_loader
do
local _obj_0 = require('nomsu_loaders')
search_path, file_loader = _obj_0.search_path, _obj_0.file_loader
end
if NOMSU_VERSION then
assert(table.concat(LIBRARY_VERSION, ".") == NOMSU_VERSION, "The Nomsu runner's version doesn't match the version of the Nomsu library it found")
end
require('colors').enabled = COLOR_ENABLED
local NOMSU_PACKAGEPATH = NOMSU_PACKAGEPATH or "/opt/nomsu"
local lib_dir
if NOMSU_PREFIX then
lib_dir = tostring(NOMSU_PREFIX) .. "/share/nomsu/" .. tostring(NOMSU_VERSION) .. "/lib"
else
lib_dir = "./lib"
end
local nomsupath = search_path({
lib_dir,
NOMSU_PACKAGEPATH,
"."
}, optimization)
local run
run = function()
local nomsu_environment = new_instance({
loader = file_loader({
lib_dir,
NOMSU_PACKAGEPATH,
"."
}, optimization),
optimization = optimization
})
nomsu_environment.COMMAND_LINE_ARGS = nomsu_args
nomsu_environment.NOMSU_PACKAGEPATH = NOMSU_PACKAGEPATH
nomsu_environment.NOMSU_PREFIX = NOMSU_PREFIX
nomsu_environment.COLOR_ENABLED = COLOR_ENABLED
if not (args.no_core) then
nomsu_environment:export("core")
end
local input_files = { }
if args.files and #args.files > 0 then
local _list_1 = args.files
for _index_0 = 1, #_list_1 do
local f = _list_1[_index_0]
do
local nomsu_name = f:match("^nomsu://(.*)%.nom")
if nomsu_name then
local path
path, err = package.searchpath(nomsu_name, nomsupath, "/")
if not path then
error(err)
end
table.insert(input_files, path)
else
table.insert(input_files, f)
end
end
end
end
for _index_0 = 1, #input_files do
local _continue_0 = false
repeat
local filename = input_files[_index_0]
local code = Files.read(filename)
if not (code) then
if filename:match("^-") then
print("Not a valid flag: " .. filename .. "\n")
print(usage)
else
print("File not found: " .. filename)
end
os.exit(EXIT_FAILURE)
end
local source = Source(filename, 1, #code)
if args.check_syntax then
nomsu_environment._1_parsed(NomsuCode:from(source, code))
print("Parse succeeded: " .. tostring(filename))
_continue_0 = true
break
end
local env = nomsu_environment.new_environment()
env.MODULE_NAME = filename
if not (args.compile) then
env.WAS_RUN_DIRECTLY = true
end
if not (args.compile or args.verbose) then
env:run((filename:match("%.lua$") and LuaCode or NomsuCode):from(source, code))
_continue_0 = true
break
end
local tree, lua
if filename:match("%.lua$") then
if args.compile then
error("Cannot compile a lua file (expected a nomsu file as input)")
end
lua = LuaCode:from(source, code)
else
tree = env._1_parsed(NomsuCode:from(source, code))
lua = env:compile_file(tree)
lua:declare_locals()
lua:prepend("-- File " .. tostring(filename) .. "\n")
end
if args.compile then
lua:prepend("\n-- Source hash: " .. tostring(Files.hash(code)) .. "\n")
if args.verbose then
print(lua:text())
end
local lua_filename = filename:gsub("%.nom$", ".lua")
local output
if filename == 'stdin' then
output = io.output()
else
output = io.open(lua_filename, "w")
end
if tree.shebang then
output:write(tree.shebang)
end
output:write(lua:text(), "\n")
print(("Compiled %-25s -> %s"):format(filename, lua_filename))
output:close()
else
print(lua:text())
env:run(lua)
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
end
local debugger
if args.debugger == "nil" then
debugger = { }
else
debugger = require(args.debugger or 'error_handling')
end
local guard
if type(debugger) == 'function' then
guard = debugger
else
guard = debugger.guard or debugger.call or debugger.wrap or debugger.run or (function(fn)
return fn()
end)
end
return guard(run)
|