diff --git a/nomsu.lua b/nomsu.lua index 94eec80..60f74ea 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -416,9 +416,26 @@ do return error("Invalid filetype for " .. tostring(filename), 0) end end, - require_file = function(self, filename) + use_file = function(self, filename) local loaded = self.environment.LOADED 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 end return loaded[filename] @@ -1358,9 +1375,9 @@ do end) return self:define_compile_action("use %filename", get_line_no(), function(_filename) local filename = nomsu:tree_to_value(_filename) - nomsu:require_file(filename) + nomsu:use_file(filename) return { - expr = "nomsu:require_file(" .. tostring(repr(filename)) .. ");" + expr = "nomsu:use_file(" .. tostring(repr(filename)) .. ");" } end) end @@ -1383,6 +1400,7 @@ do return id end }) + self.use_stack = { } self.compilestack = { } self.debug = false self.environment = { diff --git a/nomsu.moon b/nomsu.moon index cb2efdd..0c58a2d 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -155,6 +155,7 @@ class NomsuCompiler @[key] = id return id }) + @use_stack = {} @compilestack = {} @debug = false @@ -323,9 +324,15 @@ class NomsuCompiler else error("Invalid filetype for #{filename}", 0) - require_file: (filename)=> + use_file: (filename)=> loaded = @environment.LOADED 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 return loaded[filename] @@ -933,8 +940,8 @@ class NomsuCompiler @define_compile_action "use %filename", get_line_no!, (_filename)-> filename = nomsu\tree_to_value(_filename) - nomsu\require_file(filename) - return expr:"nomsu:require_file(#{repr filename});" + nomsu\use_file(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: if arg and debug.getinfo(2).func != require