blob: dac59d48c3f34fd3a5860cc60c468f79cb2e5cac (
plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#!/usr/bin/lua
require "lspipat"
function EXIT(...)
io.stderr:write(string.format(...))
os.exit()
end
stack = {}
function push(val) table.insert(stack, val) end
function binop()
table.insert(stack, {
l = table.remove(stack),
type = table.remove(stack),
r = table.remove(stack)
})
end
function compile(node)
if type(node) ~= "table" then return string.rep("+", tonumber(node)) end
local ret = compile(node.l)..">"..compile(node.r)
node.type:smatch( Any("+-") % function(o) ret = ret.."[<"..o..">-]<" end
+ "*" * -function() ret = ">>"..ret.."[<[<+<+>>-]<[>+<-]>>-]<[-]<<" end
+ "/" * -function() ret = ">"..ret.."<[->->+>>+<<<[>>+>[-]<<<-]>>"..
"[<<+>>-]>[-<<[<+>-]<<<+>>>>>]<<<<]>[-]>[-]<<<" end,
spipat.match_anchored )
return ret
end
if #arg ~= 1 then EXIT("Invalid number of parameters\n") end
space = NSpan(" ")
pre = space * ("(" * -"exp" * space * ")" + Span("0123456789") % push)
post = space * ( Any("+-") % push * -"exp" * -binop
+ Any("*/") % push * pre * -binop * -"post" ) + ""
exp = pre * post
if not arg[1]:smatch(exp * RPos(0), spipat.match_anchored) then EXIT("Invalid expression!\n") end
src = compile(stack[1])..
"[>++++++++++<[->->+>>+<<<[>>+>[-]<<<-]>>[<<+>>-]"..
">[-<<[<+>-]>>>+<]<<<<]>>>>>[<<<<<+>>>>>-]>[>]"..string.rep("+", string.byte("0"))..
"[<]<<<[>>>>[>]<+[<]<<<-]<[-]<]>>>>>>[>]<[.<]"
print(src)
|