Last active
October 29, 2023 02:35
-
-
Save romgrk/75d6dfc87c7c581e10d7 to your computer and use it in GitHub Desktop.
MoonScript REPL (using luarepl)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
-- This is an example quick-implementation of a MoonScript REPL, | |
-- using LuaRepl. | |
-- | |
-- Requires: | |
-- - moonscript and luarepl, of course | |
-- - log (https://github.com/romgrk/log.moon) | |
-- | |
-- The additionnal 'log' is my personnal logging utility. | |
-- provided at https://github.com/romgrk/log.moon | |
-- It is NOT standard nor production-use oriented. | |
-- You could simply replace it by calls to print. | |
-- However it can pretty print, has support for term-coloring | |
-- and other utilities. (Bit & Hex dump) | |
local log = require 'log' | |
local repl = require 'repl.console' | |
local moonscript = require 'moonscript.base' | |
local options = { | |
moon = true, | |
debug = false | |
} | |
local context = repl:getcontext() | |
--local metaenv = { } | |
--setmetatable(metaenv, { | |
--__index = function (table, key) | |
--log.info('access key: ' .. key, log.RST) | |
--return context[key] | |
--end, | |
--__newindex = function (table, key, value) | |
--log.debug('new key: ' .. key, log.RST, value) | |
--rawset(context, key, value) | |
--end | |
--}) | |
context.log = log | |
context.p = log.pp | |
context.inspect = require 'inspect' | |
context.keys = context._.keys | |
local function gather_results(success, ...) | |
local n = select('#', ...) | |
return success, { n = n, ... } | |
end | |
function repl:displayresults(results) | |
for i, res in ipairs(results) do | |
log.pp(res) | |
context.it = res | |
end | |
end | |
function repl:displayerror(err) | |
log.err(err .. log.RST) | |
end | |
function repl:compilechunk(chunk) | |
return loadstring(chunk, self:name()) | |
end | |
function repl:handleline(line) | |
local lua_code | |
if options.moon then | |
local chunk = "export *\n" .. self._buffer .. line | |
lua_code, more = moonscript.to_lua(chunk) | |
if not lua_code then | |
self._buffer = '' | |
self:displayerror(more) | |
return 1 | |
end | |
else | |
lua_code = self._buffer .. line | |
end | |
--log.warn(lua_code .. log.RST) | |
local f, err = self:compilechunk(lua_code) | |
if f then | |
self._buffer = '' | |
setfenv(f, context) | |
local success, results = gather_results(xpcall(f, function(...) return self:traceback(...) end)) | |
if success then | |
self:displayresults(results) | |
else | |
self:displayerror(results[1]) | |
end | |
else | |
if self:detectcontinue(err) then | |
self._buffer = chunk .. '\n' | |
return 2 | |
else | |
self:displayerror(err) | |
end | |
end | |
return 1 | |
end | |
-- Plugins | |
repl:loadplugin 'linenoise' | |
repl:loadplugin 'history' | |
repl:loadplugin 'completion' | |
repl:loadplugin 'filename_completion' | |
--repl:loadplugin 'autoreturn' | |
log.info('Moon REPL' .. log.RST) | |
repl:run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment