Added check for circular imports.
This commit is contained in:
parent
d02b4b8718
commit
e8d5d2a240
24
nomsu.lua
24
nomsu.lua
@ -416,9 +416,26 @@ do
|
|||||||
return error("Invalid filetype for " .. tostring(filename), 0)
|
return error("Invalid filetype for " .. tostring(filename), 0)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
require_file = function(self, filename)
|
use_file = function(self, filename)
|
||||||
local loaded = self.environment.LOADED
|
local loaded = self.environment.LOADED
|
||||||
if not loaded[filename] then
|
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
|
loaded[filename] = self:run_file(filename) or true
|
||||||
end
|
end
|
||||||
return loaded[filename]
|
return loaded[filename]
|
||||||
@ -1358,9 +1375,9 @@ do
|
|||||||
end)
|
end)
|
||||||
return self:define_compile_action("use %filename", get_line_no(), function(_filename)
|
return self:define_compile_action("use %filename", get_line_no(), function(_filename)
|
||||||
local filename = nomsu:tree_to_value(_filename)
|
local filename = nomsu:tree_to_value(_filename)
|
||||||
nomsu:require_file(filename)
|
nomsu:use_file(filename)
|
||||||
return {
|
return {
|
||||||
expr = "nomsu:require_file(" .. tostring(repr(filename)) .. ");"
|
expr = "nomsu:use_file(" .. tostring(repr(filename)) .. ");"
|
||||||
}
|
}
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@ -1383,6 +1400,7 @@ do
|
|||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
self.use_stack = { }
|
||||||
self.compilestack = { }
|
self.compilestack = { }
|
||||||
self.debug = false
|
self.debug = false
|
||||||
self.environment = {
|
self.environment = {
|
||||||
|
13
nomsu.moon
13
nomsu.moon
@ -155,6 +155,7 @@ class NomsuCompiler
|
|||||||
@[key] = id
|
@[key] = id
|
||||||
return id
|
return id
|
||||||
})
|
})
|
||||||
|
@use_stack = {}
|
||||||
@compilestack = {}
|
@compilestack = {}
|
||||||
@debug = false
|
@debug = false
|
||||||
|
|
||||||
@ -323,9 +324,15 @@ class NomsuCompiler
|
|||||||
else
|
else
|
||||||
error("Invalid filetype for #{filename}", 0)
|
error("Invalid filetype for #{filename}", 0)
|
||||||
|
|
||||||
require_file: (filename)=>
|
use_file: (filename)=>
|
||||||
loaded = @environment.LOADED
|
loaded = @environment.LOADED
|
||||||
if not loaded[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
|
loaded[filename] = @run_file(filename) or true
|
||||||
return loaded[filename]
|
return loaded[filename]
|
||||||
|
|
||||||
@ -933,8 +940,8 @@ class NomsuCompiler
|
|||||||
|
|
||||||
@define_compile_action "use %filename", get_line_no!, (_filename)->
|
@define_compile_action "use %filename", get_line_no!, (_filename)->
|
||||||
filename = nomsu\tree_to_value(_filename)
|
filename = nomsu\tree_to_value(_filename)
|
||||||
nomsu\require_file(filename)
|
nomsu\use_file(filename)
|
||||||
return expr:"nomsu:require_file(#{repr filename});"
|
return expr:"nomsu:use_file(#{repr filename});"
|
||||||
|
|
||||||
-- 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