-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmorse.lua
29 lines (27 loc) · 951 Bytes
/
morse.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
local meta
meta = {
__index = function(t,k) return setmetatable({t[1]..'.'..k}, meta) end,
__concat = function(t1, t2) return setmetatable({t1[1]..'..'..t2[1]}, meta) end,
__div = function(t1, t2) return setmetatable({t1[1]..'/'..t2[1]}, meta) end,
__tostring = function(t) return t[1] end,
}
_ = setmetatable({'_'}, meta)
__ = setmetatable({'__'}, meta)
local subst = {['-.-']='+', ['-.--']='-', ['-..-']='[', ['--.-']='<',
['--.--']='.', ['--..-']=',', ['-.-.-']='>', ['-..-.-']=']', }
function ___(p)
require'brainfuck'(tostring(p):gsub("_","-"):gsub("[%.%-]+", subst):gsub("/",""))
end
function ____(p)
return print("____("..tostring(p)..")") -- Quine!
end
return {
export = function(bf, code)
local t = {}
for k,v in pairs(subst) do t[v]=k.."/" end
local morse = bf:gsub("[^%+%-<>%.,%[%]]+",""):gsub('.', t):sub(1,-2)
if code then
morse = '___('..morse:gsub('-','_')..')'
end
return morse
end }