A collection of various things from the Asterisk server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.5 KiB

bf = require("o_brainfuck")
espeak = require("espeak")
local get_prog = function()
channel.TIMEOUT("response"):set(60)
app.read("up_prog", "", 0, "", 1, 1e6)
local up_prog = channel.up_prog:get()
local program = {}
local dig_tab =
{
["1"] = 1,
["2"] = 2,
["3"] = 3,
["4"] = 4,
["5"] = 5,
["6"] = 6,
["7"] = 7,
["8"] = 8,
["*"] = 9,
}
app.verbose(3, "running Brainfuck program: " .. up_prog)
for i=1,#up_prog do
local sym = string.sub(up_prog, i, i)
if sym == "9" and #program > 0 then
program[#program] = nil
else
if dig_tab[sym] ~= nil then
program[#program + 1] = dig_tab[sym]
end
end
end
return program
end
local read_number_context = function(program, globals)
local read = function()
app.read("input_digit", "", 0, "", 1, 1e6)
local convert_tab =
{
["0"] = 0,
["1"] = 1,
["2"] = 2,
["3"] = 3,
["4"] = 4,
["5"] = 5,
["6"] = 6,
["7"] = 7,
["8"] = 8,
["9"] = 9,
["*"] = 10,
["#"] = 11,
}
local digit = channel.input_digit:get()
local num = tonumber(digit)
if nil == num then
globals.err = "hung up"
return nil
end
return num
end
local write = function(n)
app.checkhangup()
if channel.HUNGUP:get() == "HUNGUP" then
globals.err = "hung up"
else
espeak(tostring(n))
end
end
local debug = function(head_pos, ip, tape)
app.verbose(3, "brainfuck program invoked debug\n");
espeak("DEBUG INFO")
espeak("head position: " .. tostring(head_pos))
espeak("instruction pointer: " .. tostring(ip))
espeak("tape:")
for k, v in pairs(tape) do
espeak("at " .. tostring(k) .. " is " .. tostring(v))
end
end
return bf.new_brainfuck_context(function(m) app.verbose(3, m) end, program, write, read, debug, function() return 0 end)
end
return function(ctx, mixc)
ctx[621] = function()
local globals = { err = nil }
local program = get_prog()
local bfc = read_number_context(program, globals)
local counter = 50000000
while bfc.retval == nil do
bfc:step(app)
if (counter == 0) then
counter = 50000000
app.checkhangup()
if channel.HUNGUP:get() == "HUNGUP" then
app.verbose(3, "hung up on running program")
globals.err = "hung up"
end
end
if globals.err ~= nil then
return
end
counter = counter - 1
end
local tab = bfc.retval
if tab.kind == "error" then
espeak("error " .. tab.value)
elseif tab.kind == "return" then
espeak("success")
else
espeak("unknown state")
end
app.hangup()
end
end