Merging use_file and run_file
This commit is contained in:
parent
7410e42bc0
commit
52a3631ed6
72
nomsu.lua
72
nomsu.lua
@ -263,7 +263,7 @@ end
|
|||||||
local NomsuCompiler
|
local NomsuCompiler
|
||||||
do
|
do
|
||||||
local _class_0
|
local _class_0
|
||||||
local stub_defs, stub_pattern, var_pattern, _nomsu_chunk_counter
|
local stub_defs, stub_pattern, var_pattern, _nomsu_chunk_counter, _running_files
|
||||||
local _base_0 = {
|
local _base_0 = {
|
||||||
define_action = function(self, signature, fn, is_compile_action)
|
define_action = function(self, signature, fn, is_compile_action)
|
||||||
if is_compile_action == nil then
|
if is_compile_action == nil then
|
||||||
@ -390,10 +390,36 @@ do
|
|||||||
if compile_fn == nil then
|
if compile_fn == nil then
|
||||||
compile_fn = nil
|
compile_fn = nil
|
||||||
end
|
end
|
||||||
|
local loaded = self.environment.LOADED
|
||||||
|
if loaded[filename] then
|
||||||
|
return loaded[filename]
|
||||||
|
end
|
||||||
local ret = nil
|
local ret = nil
|
||||||
for filename in all_files(filename) do
|
for filename in all_files(filename) do
|
||||||
local _continue_0 = false
|
local _continue_0 = false
|
||||||
repeat
|
repeat
|
||||||
|
if loaded[filename] then
|
||||||
|
ret = loaded[filename]
|
||||||
|
_continue_0 = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
for i, running in ipairs(_running_files) do
|
||||||
|
if running == filename then
|
||||||
|
local loop
|
||||||
|
do
|
||||||
|
local _accum_0 = { }
|
||||||
|
local _len_0 = 1
|
||||||
|
for j = i, #_running_files do
|
||||||
|
_accum_0[_len_0] = _running_files[j]
|
||||||
|
_len_0 = _len_0 + 1
|
||||||
|
end
|
||||||
|
loop = _accum_0
|
||||||
|
end
|
||||||
|
insert(loop, filename)
|
||||||
|
error("Circular import, this loops forever: " .. tostring(concat(loop, " -> ")))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
insert(_running_files, filename)
|
||||||
if filename:match("%.lua$") then
|
if filename:match("%.lua$") then
|
||||||
local file = assert(FILE_CACHE[filename], "Could not find file: " .. tostring(filename))
|
local file = assert(FILE_CACHE[filename], "Could not find file: " .. tostring(filename))
|
||||||
ret = self:run_lua(Lua(Source(filename), file))
|
ret = self:run_lua(Lua(Source(filename), file))
|
||||||
@ -415,46 +441,17 @@ do
|
|||||||
else
|
else
|
||||||
error("Invalid filetype for " .. tostring(filename), 0)
|
error("Invalid filetype for " .. tostring(filename), 0)
|
||||||
end
|
end
|
||||||
|
loaded[filename] = ret or true
|
||||||
|
remove(_running_files)
|
||||||
_continue_0 = true
|
_continue_0 = true
|
||||||
until true
|
until true
|
||||||
if not _continue_0 then
|
if not _continue_0 then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
loaded[filename] = ret or true
|
||||||
return ret
|
return ret
|
||||||
end,
|
end,
|
||||||
use_file = function(self, filename)
|
|
||||||
local loaded = self.environment.LOADED
|
|
||||||
if not loaded[filename] then
|
|
||||||
local ret = nil
|
|
||||||
for filename in all_files(filename) do
|
|
||||||
if not loaded[filename] then
|
|
||||||
for i, f in ipairs(self.use_stack) do
|
|
||||||
if f == filename then
|
|
||||||
local loop
|
|
||||||
do
|
|
||||||
local _accum_0 = { }
|
|
||||||
local _len_0 = 1
|
|
||||||
for j = i, #self.use_stack do
|
|
||||||
_accum_0[_len_0] = self.use_stack[j]
|
|
||||||
_len_0 = _len_0 + 1
|
|
||||||
end
|
|
||||||
loop = _accum_0
|
|
||||||
end
|
|
||||||
insert(loop, filename)
|
|
||||||
error("Circular import, this loops forever: " .. tostring(concat(loop, " -> ")))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
insert(self.use_stack, filename)
|
|
||||||
loaded[filename] = self:run_file(filename) or true
|
|
||||||
ret = loaded[filename]
|
|
||||||
remove(self.use_stack)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
loaded[filename] = ret
|
|
||||||
end
|
|
||||||
return loaded[filename]
|
|
||||||
end,
|
|
||||||
run_lua = function(self, lua)
|
run_lua = function(self, lua)
|
||||||
assert(type(lua) ~= 'string', "Attempt to run lua string instead of Lua (object)")
|
assert(type(lua) ~= 'string', "Attempt to run lua string instead of Lua (object)")
|
||||||
local lua_string = tostring(lua)
|
local lua_string = tostring(lua)
|
||||||
@ -674,13 +671,10 @@ do
|
|||||||
end
|
end
|
||||||
return lua
|
return lua
|
||||||
end)
|
end)
|
||||||
self:define_action("run file %filename", function(_filename)
|
|
||||||
return nomsu:run_file(_filename)
|
|
||||||
end)
|
|
||||||
return self:define_compile_action("use %path", function(self, _path)
|
return self:define_compile_action("use %path", function(self, _path)
|
||||||
local path = nomsu:tree_to_value(_path)
|
local path = nomsu:tree_to_value(_path)
|
||||||
nomsu:use_file(path)
|
nomsu:run_file(path)
|
||||||
return Lua(self.source, "nomsu:use_file(" .. tostring(repr(path)) .. ");")
|
return Lua(self.source, "nomsu:run_file(" .. tostring(repr(path)) .. ");")
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
@ -702,7 +696,6 @@ do
|
|||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
self.use_stack = { }
|
|
||||||
self.file_metadata = setmetatable({ }, {
|
self.file_metadata = setmetatable({ }, {
|
||||||
__mode = "k"
|
__mode = "k"
|
||||||
})
|
})
|
||||||
@ -810,6 +803,7 @@ do
|
|||||||
]=], stub_defs)
|
]=], stub_defs)
|
||||||
var_pattern = re.compile("{| %space ((('%' {%varname}) / %word) %space)+ |}", stub_defs)
|
var_pattern = re.compile("{| %space ((('%' {%varname}) / %word) %space)+ |}", stub_defs)
|
||||||
_nomsu_chunk_counter = 0
|
_nomsu_chunk_counter = 0
|
||||||
|
_running_files = { }
|
||||||
NomsuCompiler = _class_0
|
NomsuCompiler = _class_0
|
||||||
end
|
end
|
||||||
if arg and debug_getinfo(2).func ~= require then
|
if arg and debug_getinfo(2).func ~= require then
|
||||||
|
45
nomsu.moon
45
nomsu.moon
@ -240,7 +240,6 @@ class NomsuCompiler
|
|||||||
@[key] = id
|
@[key] = id
|
||||||
return id
|
return id
|
||||||
})
|
})
|
||||||
@use_stack = {}
|
|
||||||
@file_metadata = setmetatable({}, {__mode:"k"})
|
@file_metadata = setmetatable({}, {__mode:"k"})
|
||||||
|
|
||||||
@environment = {
|
@environment = {
|
||||||
@ -345,9 +344,24 @@ class NomsuCompiler
|
|||||||
compile_fn(lua)
|
compile_fn(lua)
|
||||||
return @run_lua(lua)
|
return @run_lua(lua)
|
||||||
|
|
||||||
|
_running_files = {}
|
||||||
run_file: (filename, compile_fn=nil)=>
|
run_file: (filename, compile_fn=nil)=>
|
||||||
|
loaded = @environment.LOADED
|
||||||
|
if loaded[filename]
|
||||||
|
return loaded[filename]
|
||||||
ret = nil
|
ret = nil
|
||||||
for filename in all_files(filename)
|
for filename in all_files(filename)
|
||||||
|
if loaded[filename]
|
||||||
|
ret = loaded[filename]
|
||||||
|
continue
|
||||||
|
|
||||||
|
for i,running in ipairs _running_files
|
||||||
|
if running == filename
|
||||||
|
loop = [_running_files[j] for j=i,#_running_files]
|
||||||
|
insert loop, filename
|
||||||
|
error("Circular import, this loops forever: #{concat loop, " -> "}")
|
||||||
|
|
||||||
|
insert _running_files, filename
|
||||||
if filename\match("%.lua$")
|
if filename\match("%.lua$")
|
||||||
file = assert(FILE_CACHE[filename], "Could not find file: #{filename}")
|
file = assert(FILE_CACHE[filename], "Could not find file: #{filename}")
|
||||||
ret = @run_lua(Lua(Source(filename), file))
|
ret = @run_lua(Lua(Source(filename), file))
|
||||||
@ -364,25 +378,11 @@ class NomsuCompiler
|
|||||||
ret = @run(Nomsu(Source(filename), file), compile_fn)
|
ret = @run(Nomsu(Source(filename), file), compile_fn)
|
||||||
else
|
else
|
||||||
error("Invalid filetype for #{filename}", 0)
|
error("Invalid filetype for #{filename}", 0)
|
||||||
return ret
|
loaded[filename] = ret or true
|
||||||
|
remove _running_files
|
||||||
|
|
||||||
use_file: (filename)=>
|
loaded[filename] = ret or true
|
||||||
loaded = @environment.LOADED
|
return ret
|
||||||
if not loaded[filename]
|
|
||||||
ret = nil
|
|
||||||
for filename in all_files(filename)
|
|
||||||
if not loaded[filename]
|
|
||||||
for i,f in ipairs @use_stack
|
|
||||||
if f == filename
|
|
||||||
loop = [@use_stack[j] for j=i,#@use_stack]
|
|
||||||
insert loop, filename
|
|
||||||
error("Circular import, this loops forever: #{concat loop, " -> "}")
|
|
||||||
insert @use_stack, filename
|
|
||||||
loaded[filename] = @run_file(filename) or true
|
|
||||||
ret = loaded[filename]
|
|
||||||
remove @use_stack
|
|
||||||
loaded[filename] = ret
|
|
||||||
return loaded[filename]
|
|
||||||
|
|
||||||
run_lua: (lua)=>
|
run_lua: (lua)=>
|
||||||
assert(type(lua) != 'string', "Attempt to run lua string instead of Lua (object)")
|
assert(type(lua) != 'string', "Attempt to run lua string instead of Lua (object)")
|
||||||
@ -550,13 +550,10 @@ class NomsuCompiler
|
|||||||
lua\append bit_lua
|
lua\append bit_lua
|
||||||
return lua
|
return lua
|
||||||
|
|
||||||
@define_action "run file %filename", (_filename)->
|
|
||||||
return nomsu\run_file(_filename)
|
|
||||||
|
|
||||||
@define_compile_action "use %path", (_path)=>
|
@define_compile_action "use %path", (_path)=>
|
||||||
path = nomsu\tree_to_value(_path)
|
path = nomsu\tree_to_value(_path)
|
||||||
nomsu\use_file(path)
|
nomsu\run_file(path)
|
||||||
return Lua(@source, "nomsu:use_file(#{repr path});")
|
return Lua(@source, "nomsu:run_file(#{repr path});")
|
||||||
|
|
||||||
-- Only run this code if this file was run directly with command line arguments, and not require()'d:
|
-- Only run this code if this file was run directly with command line arguments, and not require()'d:
|
||||||
if arg and debug_getinfo(2).func != require
|
if arg and debug_getinfo(2).func != require
|
||||||
|
Loading…
Reference in New Issue
Block a user