2020-03-19 09:59:20 +00:00
|
|
|
#!/usr/bin/env luajit
|
|
|
|
|
2020-04-14 08:58:21 +00:00
|
|
|
local env = setmetatable({}, {__index = function(self, key)
|
|
|
|
return math[key] or _G[key] or nil
|
|
|
|
end})
|
|
|
|
|
|
|
|
env.banner = [[
|
2020-03-19 10:20:16 +00:00
|
|
|
Calculator (by DarkWiiPlayer)
|
|
|
|
License: the Unlicense (type 'license')
|
|
|
|
]]
|
|
|
|
|
2020-04-14 08:58:21 +00:00
|
|
|
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 = [[
|
2020-03-19 10:20:16 +00:00
|
|
|
This is free and unencumbered software released into the public domain.
|
|
|
|
|
|
|
|
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
|
|
distribute this software, either in source code form or as a compiled
|
|
|
|
binary, for any purpose, commercial or non-commercial, and by any
|
|
|
|
means.
|
|
|
|
|
|
|
|
In jurisdictions that recognize copyright laws, the author or authors
|
|
|
|
of this software dedicate any and all copyright interest in the
|
|
|
|
software to the public domain. We make this dedication for the benefit
|
|
|
|
of the public at large and to the detriment of our heirs and
|
|
|
|
successors. We intend this dedication to be an overt act of
|
|
|
|
relinquishment in perpetuity of all present and future rights to this
|
|
|
|
software under copyright law.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
]]
|
|
|
|
|
2020-03-19 09:59:20 +00:00
|
|
|
local moonscript = pcall(function()
|
|
|
|
require 'moonscript.base'
|
|
|
|
end)
|
|
|
|
|
|
|
|
if moonscript then
|
|
|
|
parse = require('moonscript.base').to_lua
|
|
|
|
else
|
2020-03-19 10:20:16 +00:00
|
|
|
print("Could not load Moonscript, falling back to Lua...")
|
2020-03-19 09:59:20 +00:00
|
|
|
parse = function(...) return ... end
|
|
|
|
end
|
|
|
|
|
2020-03-19 10:20:16 +00:00
|
|
|
local function run(line, name, addreturn)
|
|
|
|
if addreturn~=false and not moonscript then
|
2020-03-19 09:59:20 +00:00
|
|
|
line = "return ("..line..")"
|
|
|
|
end
|
2020-04-14 08:58:21 +00:00
|
|
|
local expression = assert(load(parse(line), name, 't', env))
|
2020-03-19 10:20:16 +00:00
|
|
|
success, math.ans = xpcall(expression, print)
|
|
|
|
print(math.ans)
|
2020-03-19 09:59:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local line = table.concat({...}, ' ')
|
|
|
|
|
|
|
|
if line == '' then
|
2020-03-19 10:20:16 +00:00
|
|
|
print(banner)
|
|
|
|
io.write '> '
|
2020-03-19 09:59:20 +00:00
|
|
|
for line in io.stdin:lines() do
|
2020-03-19 10:20:16 +00:00
|
|
|
io.write("= ")
|
2020-03-19 09:59:20 +00:00
|
|
|
run(line, "User Input", true, "=")
|
2020-03-19 10:20:16 +00:00
|
|
|
io.write '> '
|
2020-03-19 09:59:20 +00:00
|
|
|
end
|
2020-03-19 10:20:16 +00:00
|
|
|
print('os.exit(0, true) -- See you again soon ♥')
|
2020-03-19 09:59:20 +00:00
|
|
|
elseif line == '-' then
|
|
|
|
run(io.stdin:read("*a"), "User Input", false, "=")
|
|
|
|
else
|
|
|
|
run(line, "Commandline")
|
|
|
|
end
|