Make globals persistent in = calculator

The expression evaluation environment is now persistent across an
interactive calculator session to allow easily using "global" variables.
This commit is contained in:
Talia 2020-04-14 10:58:21 +02:00
parent fd0f08754b
commit 9f09d45bfa

26
bin/=
View file

@ -1,11 +1,22 @@
#!/usr/bin/env luajit #!/usr/bin/env luajit
banner = [[ local env = setmetatable({}, {__index = function(self, key)
return math[key] or _G[key] or nil
end})
env.banner = [[
Calculator (by DarkWiiPlayer) Calculator (by DarkWiiPlayer)
License: the Unlicense (type 'license') License: the Unlicense (type 'license')
]] ]]
license = [[ env.help = [[
This is a simple terminal calculator written in Lua.
Call without arguments for an interactive shell.
Call with `-` argument for a multiline calculation.
Call with any other argument for inline calculation.
]]
env.license = [[
This is free and unencumbered software released into the public domain. This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or Anyone is free to copy, modify, publish, use, compile, sell, or
@ -30,13 +41,6 @@ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. OTHER DEALINGS IN THE SOFTWARE.
]] ]]
help = [[
This is a simple terminal calculator written in Lua.
Call without arguments for an interactive shell.
Call with `-` argument for a multiline calculation.
Call with any other argument for inline calculation.
]]
local moonscript = pcall(function() local moonscript = pcall(function()
require 'moonscript.base' require 'moonscript.base'
end) end)
@ -52,9 +56,7 @@ local function run(line, name, addreturn)
if addreturn~=false and not moonscript then if addreturn~=false and not moonscript then
line = "return ("..line..")" line = "return ("..line..")"
end end
local expression = assert(load(parse(line), name, 't', setmetatable({}, {__index = function(self, key) local expression = assert(load(parse(line), name, 't', env))
return math[key] or _G[key] or nil
end})))
success, math.ans = xpcall(expression, print) success, math.ans = xpcall(expression, print)
print(math.ans) print(math.ans)
end end