From 171e9d674e4ae85e303c90679ad47bceda3b3810 Mon Sep 17 00:00:00 2001
From: Bruce Hill <bruce@bruce-hill.com>
Date: Sun, 11 Nov 2018 18:28:10 -0800
Subject: [PATCH] Re-implemented nomsu -> lua comment translation and added
 file chunk comments.

---
 nomsu.lua              | 4 ++--
 nomsu.moon             | 3 ++-
 nomsu_compiler.lua     | 2 +-
 nomsu_compiler.moon    | 7 +------
 nomsu_environment.lua  | 5 ++---
 nomsu_environment.moon | 4 ++--
 6 files changed, 10 insertions(+), 15 deletions(-)

diff --git a/nomsu.lua b/nomsu.lua
index b0e13bb..4cbaf7c 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -199,11 +199,11 @@ run = function()
               tree
             }
           end
-          for _index_1 = 1, #tree do
-            local chunk = tree[_index_1]
+          for chunk_no, chunk in ipairs(tree) do
             local lua = nomsu_environment.compile(chunk)
             lua:declare_locals()
             nomsu_environment.run_1_in(chunk, nomsu_environment)
+            output:write((chunk_no > 1) and '\n' or '', "-- File " .. tostring(filename) .. " chunk #" .. tostring(chunk_no) .. "\n")
             output:write(tostring(lua), "\n")
             if args.verbose then
               print(tostring(lua))
diff --git a/nomsu.moon b/nomsu.moon
index 7fd7d13..dd37034 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -128,10 +128,11 @@ run = ->
                 code = NomsuCode\from(source, code)
                 tree = nomsu_environment._1_parsed(code)
                 tree = {tree} unless tree.type == 'FileChunks'
-                for chunk in *tree
+                for chunk_no, chunk in ipairs tree
                     lua = nomsu_environment.compile(chunk)
                     lua\declare_locals!
                     nomsu_environment.run_1_in(chunk, nomsu_environment)
+                    output\write((chunk_no > 1) and '\n' or '', "-- File #{filename} chunk ##{chunk_no}\n")
                     output\write(tostring(lua), "\n")
                     if args.verbose then print(tostring(lua))
                 print ("Compiled %-25s -> %s")\format(filename, filename\gsub("%.nom$", ".lua"))
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index 91e1237..0ea2079 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -433,7 +433,7 @@ local compile = setmetatable({
     elseif "FileChunks" == _exp_0 then
       return error("Can't convert FileChunks to a single block of lua, since each chunk's " .. "compilation depends on the earlier chunks")
     elseif "Comment" == _exp_0 then
-      return LuaCode:from(tree.source, "")
+      return LuaCode:from(tree.source, "-- ", (tree[1]:gsub('\n', '\n-- ')))
     elseif "Error" == _exp_0 then
       return error("Can't compile errors")
     else
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index c71ee1d..497d210 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -270,10 +270,6 @@ compile = setmetatable({
                         i += 1
                     lua\append "\n    return comprehension\nend)()"
                 return lua
-                --lua = LuaCode\from tree.source, "#{tree.type}{"
-                --lua\concat_append([compile(e) for e in *tree when e.type != 'Comment'], ", ", ",\n  ")
-                --lua\append "}"
-                --return lua
 
             when "DictEntry"
                 key, value = tree[1], tree[2]
@@ -323,8 +319,7 @@ compile = setmetatable({
                     "compilation depends on the earlier chunks")
             
             when "Comment"
-                -- TODO: implement?
-                return LuaCode\from(tree.source, "")
+                return LuaCode\from(tree.source, "-- ", (tree[1]\gsub('\n', '\n-- ')))
             
             when "Error"
                 error("Can't compile errors")
diff --git a/nomsu_environment.lua b/nomsu_environment.lua
index a55fe88..4345947 100644
--- a/nomsu_environment.lua
+++ b/nomsu_environment.lua
@@ -208,11 +208,10 @@ local nomsu_environment = Importer({
         }
       end
       local ret = nil
-      for _index_0 = 1, #to_run do
-        local chunk = to_run[_index_0]
+      for chunk_no, chunk in ipairs(to_run) do
         local lua = environment.compile(chunk)
         lua:declare_locals()
-        lua:prepend("-- File: " .. tostring(filename) .. "\n")
+        lua:prepend("-- File: " .. tostring(filename) .. " chunk #" .. tostring(chunk_no) .. "\n")
         ret = environment.run_1_in(lua, environment)
       end
       return ret
diff --git a/nomsu_environment.moon b/nomsu_environment.moon
index e17dcb2..37f239d 100644
--- a/nomsu_environment.moon
+++ b/nomsu_environment.moon
@@ -107,10 +107,10 @@ nomsu_environment = Importer{
             -- (typically), so each chunk needs to compile and run before the next one
             -- compiles.
             ret = nil
-            for chunk in *to_run
+            for chunk_no, chunk in ipairs to_run
                 lua = environment.compile(chunk)
                 lua\declare_locals!
-                lua\prepend "-- File: #{filename}\n"
+                lua\prepend "-- File: #{filename} chunk ##{chunk_no}\n"
                 ret = environment.run_1_in(lua, environment)
             return ret
         elseif LuaCode\is_instance(to_run)