From 9f09d45bfaf4afd1bd28d31d393315941f0f2e13 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Tue, 14 Apr 2020 10:58:21 +0200 Subject: [PATCH] Make globals persistent in = calculator The expression evaluation environment is now persistent across an interactive calculator session to allow easily using "global" variables. --- bin/= | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/bin/= b/bin/= index 438003a..25c506d 100755 --- a/bin/= +++ b/bin/= @@ -1,11 +1,22 @@ #!/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) 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. 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. ]] -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() require 'moonscript.base' end) @@ -52,9 +56,7 @@ local function run(line, name, addreturn) if addreturn~=false and not moonscript then line = "return ("..line..")" end - local expression = assert(load(parse(line), name, 't', setmetatable({}, {__index = function(self, key) - return math[key] or _G[key] or nil - end}))) + local expression = assert(load(parse(line), name, 't', env)) success, math.ans = xpcall(expression, print) print(math.ans) end