From b7efcd131046ed921ae1075d7c0f6a3b64a570f7 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 18 Mar 2021 11:51:52 -0700 Subject: [PATCH 1/3] show class name when dumping in front of { --- moonscript/util.lua | 6 +++++- moonscript/util.moon | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/moonscript/util.lua b/moonscript/util.lua index bde0d57..714767e 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -102,7 +102,11 @@ dump = function(what) lines = _accum_0 end seen[what] = false - return "{\n" .. concat(lines) .. (" "):rep((depth - 1) * 4) .. "}\n" + local class_name + if what.__class then + class_name = "<" .. tostring(what.__class.__name) .. ">" + end + return tostring(class_name or "") .. "{\n" .. concat(lines) .. (" "):rep((depth - 1) * 4) .. "}\n" else return tostring(what) .. "\n" end diff --git a/moonscript/util.moon b/moonscript/util.moon index ea7c8a5..9516c8b 100644 --- a/moonscript/util.moon +++ b/moonscript/util.moon @@ -70,7 +70,10 @@ dump = (what) -> seen[what] = false - "{\n" .. concat(lines) .. (" ")\rep((depth - 1)*4) .. "}\n" + class_name = if what.__class + "<#{what.__class.__name}>" + + "#{class_name or ""}{\n" .. concat(lines) .. (" ")\rep((depth - 1)*4) .. "}\n" else tostring(what).."\n" From b3dfdc9cad86d09e7f13de79c8638a9525c163d6 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 2 Nov 2022 14:45:36 -0700 Subject: [PATCH 2/3] fix indentation in file --- bin/moonc | 295 +++++++++++++++++++++++++++--------------------------- 1 file changed, 149 insertions(+), 146 deletions(-) diff --git a/bin/moonc b/bin/moonc index 015979a..09785d0 100755 --- a/bin/moonc +++ b/bin/moonc @@ -6,38 +6,41 @@ local lfs = require "lfs" local parser = argparse() parser:flag("-l --lint", "Perform a lint on the file instead of compiling") + parser:flag("-v --version", "Print version") parser:flag("-w --watch", "Watch file/directory for updates") parser:option("--transform", "Transform syntax tree with module") + parser:mutex( - parser:option("-t --output-to", "Specify where to place compiled files"), - parser:option("-o", "Write output to file"), - parser:flag("-p", "Write output to standard output"), - parser:flag("-T", "Write parse tree instead of code (to stdout)"), - parser:flag("-b", "Write parse and compile time instead of code(to stdout)"), - parser:flag("-X", "Write line rewrite map instead of code (to stdout)") + parser:option("-t --output-to", "Specify where to place compiled files"), + parser:option("-o", "Write output to file"), + parser:flag("-p", "Write output to standard output"), + parser:flag("-T", "Write parse tree instead of code (to stdout)"), + parser:flag("-b", "Write parse and compile time instead of code(to stdout)"), + parser:flag("-X", "Write line rewrite map instead of code (to stdout)") ) + parser:flag("-", - "Read from standard in, print to standard out (Must be only argument)") + "Read from standard in, print to standard out (Must be only argument)") local read_stdin = arg[1] == "--" -- luacheck: ignore 113 if not read_stdin then - parser:argument("file/directory"):args("+") + parser:argument("file/directory"):args("+") end local opts = parser:parse() if opts.version then - local v = require "moonscript.version" - v.print_version() - os.exit() + local v = require "moonscript.version" + v.print_version() + os.exit() end function log_msg(...) - if not opts.p then - io.stderr:write(table.concat({...}, " ") .. "\n") - end + if not opts.p then + io.stderr:write(table.concat({...}, " ") .. "\n") + end end local moonc = require("moonscript.cmd.moonc") @@ -47,186 +50,186 @@ local compile_and_write = moonc.compile_and_write local path_to_target = moonc.path_to_target local function scan_directory(root, collected) - root = normalize_dir(root) - collected = collected or {} + root = normalize_dir(root) + collected = collected or {} - for fname in lfs.dir(root) do - if not fname:match("^%.") then - local full_path = root..fname + for fname in lfs.dir(root) do + if not fname:match("^%.") then + local full_path = root..fname - if lfs.attributes(full_path, "mode") == "directory" then - scan_directory(full_path, collected) - elseif fname:match("%.moon$") then - table.insert(collected, full_path) - end - end - end + if lfs.attributes(full_path, "mode") == "directory" then + scan_directory(full_path, collected) + elseif fname:match("%.moon$") then + table.insert(collected, full_path) + end + end + end - return collected + return collected end local function remove_dups(tbl, key_fn) - local hash = {} - local final = {} + local hash = {} + local final = {} - for _, v in ipairs(tbl) do - local dup_key = key_fn and key_fn(v) or v - if not hash[dup_key] then - table.insert(final, v) - hash[dup_key] = true - end - end + for _, v in ipairs(tbl) do + local dup_key = key_fn and key_fn(v) or v + if not hash[dup_key] then + table.insert(final, v) + hash[dup_key] = true + end + end - return final + return final end -- creates tuples of input and target local function get_files(fname, files) - files = files or {} + files = files or {} - if lfs.attributes(fname, "mode") == "directory" then - for _, sub_fname in ipairs(scan_directory(fname)) do - table.insert(files, { - sub_fname, - path_to_target(sub_fname, opts.output_to, fname) - }) - end - else - table.insert(files, { - fname, - path_to_target(fname, opts.output_to) - }) - end + if lfs.attributes(fname, "mode") == "directory" then + for _, sub_fname in ipairs(scan_directory(fname)) do + table.insert(files, { + sub_fname, + path_to_target(sub_fname, opts.output_to, fname) + }) + end + else + table.insert(files, { + fname, + path_to_target(fname, opts.output_to) + }) + end - return files + return files end if read_stdin then - local parse = require "moonscript.parse" - local compile = require "moonscript.compile" + local parse = require "moonscript.parse" + local compile = require "moonscript.compile" - local text = io.stdin:read("*a") - local tree, err = parse.string(text) + local text = io.stdin:read("*a") + local tree, err = parse.string(text) - if not tree then error(err) end - local code, err, pos = compile.tree(tree) + if not tree then error(err) end + local code, err, pos = compile.tree(tree) - if not code then - error(compile.format_error(err, pos, text)) - end + if not code then + error(compile.format_error(err, pos, text)) + end - print(code) - os.exit() + print(code) + os.exit() end local inputs = opts["file/directory"] local files = {} for _, input in ipairs(inputs) do - get_files(input, files) + get_files(input, files) end files = remove_dups(files, function(f) - return f[2] + return f[2] end) -- returns an iterator that returns files that have been updated local function create_watcher(files) - local watchers = require("moonscript.cmd.watchers") + local watchers = require("moonscript.cmd.watchers") - if watchers.InotifyWacher:available() then - return watchers.InotifyWacher(files):each_update() - end + if watchers.InotifyWacher:available() then + return watchers.InotifyWacher(files):each_update() + end - return watchers.SleepWatcher(files):each_update() + return watchers.SleepWatcher(files):each_update() end if opts.watch then - -- build function to check for lint or compile in watch - local handle_file - if opts.lint then - local lint = require "moonscript.cmd.lint" - handle_file = lint.lint_file - else - handle_file = compile_and_write - end + -- build function to check for lint or compile in watch + local handle_file + if opts.lint then + local lint = require "moonscript.cmd.lint" + handle_file = lint.lint_file + else + handle_file = compile_and_write + end - local watcher = create_watcher(files) - -- catches interrupt error for ctl-c - local protected = function() - local status, file = true, watcher() - if status then - return file - elseif file ~= "interrupted!" then - error(file) - end - end + local watcher = create_watcher(files) + -- catches interrupt error for ctl-c + local protected = function() + local status, file = true, watcher() + if status then + return file + elseif file ~= "interrupted!" then + error(file) + end + end - for fname in protected do - local target = path_to_target(fname, opts.t) + for fname in protected do + local target = path_to_target(fname, opts.t) - if opts.o then - target = opts.o - end + if opts.o then + target = opts.o + end - local success, err = handle_file(fname, target) - if opts.lint then - if success then - io.stderr:write(success .. "\n\n") - elseif err then - io.stderr:write(fname .. "\n" .. err .. "\n\n") - end - elseif not success then - io.stderr:write(table.concat({ - "", - "Error: " .. fname, - err, - "\n", - }, "\n")) - elseif success == "build" then - log_msg("Built", fname, "->", target) - end - end + local success, err = handle_file(fname, target) + if opts.lint then + if success then + io.stderr:write(success .. "\n\n") + elseif err then + io.stderr:write(fname .. "\n" .. err .. "\n\n") + end + elseif not success then + io.stderr:write(table.concat({ + "", + "Error: " .. fname, + err, + "\n", + }, "\n")) + elseif success == "build" then + log_msg("Built", fname, "->", target) + end + end - io.stderr:write("\nQuitting...\n") + io.stderr:write("\nQuitting...\n") elseif opts.lint then - local has_linted_with_error; - local lint = require "moonscript.cmd.lint" - for _, tuple in pairs(files) do - local fname = tuple[1] - local res, err = lint.lint_file(fname) - if res then - has_linted_with_error = true - io.stderr:write(res .. "\n\n") - elseif err then - has_linted_with_error = true - io.stderr:write(fname .. "\n" .. err.. "\n\n") - end - end - if has_linted_with_error then - os.exit(1) - end + local has_linted_with_error; + local lint = require "moonscript.cmd.lint" + for _, tuple in pairs(files) do + local fname = tuple[1] + local res, err = lint.lint_file(fname) + if res then + has_linted_with_error = true + io.stderr:write(res .. "\n\n") + elseif err then + has_linted_with_error = true + io.stderr:write(fname .. "\n" .. err.. "\n\n") + end + end + if has_linted_with_error then + os.exit(1) + end else - for _, tuple in ipairs(files) do - local fname, target = util.unpack(tuple) - if opts.o then - target = opts.o - end + for _, tuple in ipairs(files) do + local fname, target = util.unpack(tuple) + if opts.o then + target = opts.o + end - local success, err = compile_and_write(fname, target, { - print = opts.p, - fname = fname, - benchmark = opts.b, - show_posmap = opts.X, - show_parse_tree = opts.T, - transform_module = opts.transform - }) + local success, err = compile_and_write(fname, target, { + print = opts.p, + fname = fname, + benchmark = opts.b, + show_posmap = opts.X, + show_parse_tree = opts.T, + transform_module = opts.transform + }) - if not success then - io.stderr:write(fname .. "\t" .. err .. "\n") - os.exit(1) - end - end + if not success then + io.stderr:write(fname .. "\t" .. err .. "\n") + os.exit(1) + end + end end From 87fa9e8da828594bf03f871fbdde31b8f6dcfb03 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 12:49:30 -0700 Subject: [PATCH 3/3] clean up the makefile --- Makefile | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Makefile b/Makefile index d1c2b1f..5f29047 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ LUA ?= lua5.1 LUA_VERSION = $(shell $(LUA) -e 'print(_VERSION:match("%d%.%d"))') -LUAROCKS = luarocks-$(LUA_VERSION) +LUAROCKS = luarocks --lua-version=$(LUA_VERSION) LUA_PATH_MAKE = $(shell $(LUAROCKS) path --lr-path);./?.lua;./?/init.lua LUA_CPATH_MAKE = $(shell $(LUAROCKS) path --lr-cpath);./?.so @@ -25,12 +25,6 @@ compile: $(LUA) bin/moonc -p bin/moon.moon >> bin/moon echo "-- vim: set filetype=lua:" >> bin/moon -compile_system: - moonc moon/ moonscript/ - echo "#!/usr/bin/env lua" > bin/moon - moonc -p bin/moon.moon >> bin/moon - echo "-- vim: set filetype=lua:" >> bin/moon - watch: moonc moon/ moonscript/ && moonc -w moon/ moonscript/