From 5637676bc45ce9aa3015726485f63a2a5745a45a Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 30 May 2018 14:29:08 -0700 Subject: [PATCH] Dead code cleanup --- core/metaprogramming.nom | 8 +++++-- nomsu.lua | 49 ++++++++-------------------------------- nomsu.moon | 38 ++++++++----------------------- 3 files changed, 25 insertions(+), 70 deletions(-) diff --git a/core/metaprogramming.nom b/core/metaprogramming.nom index 717690d..36f7d34 100644 --- a/core/metaprogramming.nom +++ b/core/metaprogramming.nom @@ -152,8 +152,12 @@ immediately Lua "\(%code as lua expr):remove_free_vars(\(%vars as lua expr));" action [%tree as value] - =lua "nomsu:tree_to_value(\%tree)" - + lua> ".." + if \%tree.type == 'Text' and #\%tree == 1 and type(\%tree[1]) == 'string' then + return \%tree[1] + end + local lua = Lua(\%tree.source, "return ",nomsu:tree_to_lua(\%tree)) + return nomsu:run_lua(lua) action [%tree's stub] =lua "\%tree:get_stub()" diff --git a/nomsu.lua b/nomsu.lua index cccb1a6..c5f1c92 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -260,7 +260,7 @@ end local NomsuCompiler do local _class_0 - local stub_pattern, var_pattern, _nomsu_chunk_counter, _running_files, MAX_LINE, math_expression + local stub_pattern, var_pattern, _running_files, MAX_LINE, math_expression local _base_0 = { define_action = function(self, signature, fn, is_compile_action) if is_compile_action == nil then @@ -309,12 +309,7 @@ do return self:define_action(signature, fn, true) end, parse = function(self, nomsu_code) - if type(nomsu_code) == 'string' then - _nomsu_chunk_counter = _nomsu_chunk_counter + 1 - local filename = ".nom" - FILE_CACHE[filename] = nomsu_code - nomsu_code = Nomsu(Source(filename, 1, #nomsu_code), nomsu_code) - end + assert(type(nomsu_code) ~= 'string') local userdata = { source_code = nomsu_code, indent = 0, @@ -348,13 +343,10 @@ do if compile_fn == nil then compile_fn = nil end - if #tostring(nomsu_code) == 0 then + local tree = assert(self:parse(nomsu_code)) + if type(tree) == 'number' then return nil end - local tree = self:parse(nomsu_code) - if not (tree) then - error("Failed to parse: " .. tostring(nomsu_code)) - end local lua = self:tree_to_lua(tree):as_statements() lua:declare_locals() lua:prepend("-- File: " .. tostring(nomsu_code.source or "") .. "\n") @@ -1107,28 +1099,6 @@ do return error("Unknown type: " .. tostring(tree.type)) end end, - tree_to_value = function(self, tree) - if tree.type == 'Text' and #tree == 1 and type(tree[1]) == 'string' then - return tree[1] - end - local lua = Lua(tree.source, "return ", self:tree_to_lua(tree), ";") - return self:run_lua(lua) - end, - walk_tree = function(self, tree, depth) - if depth == nil then - depth = 0 - end - coroutine.yield(tree, depth) - if tree.is_multi then - local _list_0 = tree.value - for _index_0 = 1, #_list_0 do - local v = _list_0[_index_0] - if Types.is_node(v) then - self:walk_tree(v, depth + 1) - end - end - end - end, initialize_core = function(self) local nomsu = self self:define_compile_action("immediately %block", function(self, _block) @@ -1200,7 +1170,12 @@ do return add_lua_bits(Lua.Value(self.source), _code) end) return self:define_compile_action("use %path", function(self, _path) - local path = nomsu:tree_to_value(_path) + local path + if _path.type == 'Text' and #_path == 1 and type(_path[1]) == 'string' then + path = _path[1] + else + path = nomsu:run_lua(Lua(_path.source, "return ", nomsu:tree_to_lua(_path))) + end nomsu:run_file(path) return Lua(_path.source, "nomsu:run_file(" .. tostring(repr(path)) .. ");") end) @@ -1224,9 +1199,6 @@ do return id end }) - self.file_metadata = setmetatable({ }, { - __mode = "k" - }) self.source_map = { } self.environment = { nomsu = self, @@ -1370,7 +1342,6 @@ do tok <- ({'%'} %varname) / {%word} ]=], stub_defs) var_pattern = re.compile("{| ((('%' {%varname}) / %word) [ ]*)+ !. |}", stub_defs) - _nomsu_chunk_counter = 0 _running_files = { } MAX_LINE = 80 math_expression = re.compile([[ ([+-] " ")* "%" (" " [*/^+-] (" " [+-])* " %")+ !. ]]) diff --git a/nomsu.moon b/nomsu.moon index 59c1251..4951aba 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -231,7 +231,6 @@ class NomsuCompiler @[key] = id return id }) - @file_metadata = setmetatable({}, {__mode:"k"}) @source_map = {} @environment = { @@ -317,20 +316,13 @@ class NomsuCompiler define_compile_action: (signature, fn)=> return @define_action(signature, fn, true) - _nomsu_chunk_counter = 0 parse: (nomsu_code)=> - if type(nomsu_code) == 'string' - _nomsu_chunk_counter += 1 - filename = ".nom" - FILE_CACHE[filename] = nomsu_code - nomsu_code = Nomsu(Source(filename,1,#nomsu_code), nomsu_code) - + assert(type(nomsu_code) != 'string') userdata = { source_code:nomsu_code, indent: 0, errors: {}, source: nomsu_code.source, } tree = NOMSU_PATTERN\match(tostring(nomsu_code), nil, userdata) - unless tree error "In file #{colored.blue filename} failed to parse:\n#{colored.onyellow colored.black nomsu_code}" @@ -344,10 +336,9 @@ class NomsuCompiler return tree run: (nomsu_code, compile_fn=nil)=> - if #tostring(nomsu_code) == 0 then return nil - tree = @parse(nomsu_code) - unless tree - error "Failed to parse: #{nomsu_code}" + tree = assert(@parse(nomsu_code)) + if type(tree) == 'number' -- Happens if pattern matches, but there are no captures, e.g. an empty string + return nil lua = @tree_to_lua(tree)\as_statements! lua\declare_locals! lua\prepend "-- File: #{nomsu_code.source or ""}\n" @@ -355,7 +346,7 @@ class NomsuCompiler compile_fn(lua) return @run_lua(lua) - _running_files = {} + _running_files = {} -- For detecting circular imports run_file: (filename, compile_fn=nil)=> loaded = @environment.LOADED if loaded[filename] @@ -886,20 +877,6 @@ class NomsuCompiler else error("Unknown type: #{tree.type}") - tree_to_value: (tree)=> - -- Special case for text literals - if tree.type == 'Text' and #tree == 1 and type(tree[1]) == 'string' - return tree[1] - lua = Lua(tree.source, "return ",@tree_to_lua(tree),";") - return @run_lua(lua) - - walk_tree: (tree, depth=0)=> - coroutine.yield(tree, depth) - if tree.is_multi - for v in *tree.value - if Types.is_node(v) - @walk_tree(v, depth+1) - initialize_core: => -- Sets up some core functionality nomsu = self @@ -957,7 +934,10 @@ class NomsuCompiler return add_lua_bits(Lua.Value(@source), _code) @define_compile_action "use %path", (_path)=> - path = nomsu\tree_to_value(_path) + path = if _path.type == 'Text' and #_path == 1 and type(_path[1]) == 'string' + _path[1] + else + nomsu\run_lua Lua(_path.source, "return ",nomsu\tree_to_lua(_path)) nomsu\run_file(path) return Lua(_path.source, "nomsu:run_file(#{repr path});")