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.

o_pbx_brainfuck.lua 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. bf = require("o_brainfuck")
  2. espeak = require("espeak")
  3. local get_prog = function()
  4. channel.TIMEOUT("response"):set(60)
  5. app.read("up_prog", "", 0, "", 1, 1e6)
  6. local up_prog = channel.up_prog:get()
  7. local program = {}
  8. local dig_tab =
  9. {
  10. ["1"] = 1,
  11. ["2"] = 2,
  12. ["3"] = 3,
  13. ["4"] = 4,
  14. ["5"] = 5,
  15. ["6"] = 6,
  16. ["7"] = 7,
  17. ["8"] = 8,
  18. ["*"] = 9,
  19. }
  20. app.verbose(3, "running Brainfuck program: " .. up_prog)
  21. for i=1,#up_prog do
  22. local sym = string.sub(up_prog, i, i)
  23. if sym == "9" and #program > 0 then
  24. program[#program] = nil
  25. else
  26. if dig_tab[sym] ~= nil then
  27. program[#program + 1] = dig_tab[sym]
  28. end
  29. end
  30. end
  31. return program
  32. end
  33. local read_number_context = function(program, globals)
  34. local read = function()
  35. app.read("input_digit", "", 0, "", 1, 1e6)
  36. local convert_tab =
  37. {
  38. ["0"] = 0,
  39. ["1"] = 1,
  40. ["2"] = 2,
  41. ["3"] = 3,
  42. ["4"] = 4,
  43. ["5"] = 5,
  44. ["6"] = 6,
  45. ["7"] = 7,
  46. ["8"] = 8,
  47. ["9"] = 9,
  48. ["*"] = 10,
  49. ["#"] = 11,
  50. }
  51. local digit = channel.input_digit:get()
  52. local num = tonumber(digit)
  53. if nil == num then
  54. globals.err = "hung up"
  55. return nil
  56. end
  57. return num
  58. end
  59. local write = function(n)
  60. app.checkhangup()
  61. if channel.HUNGUP:get() == "HUNGUP" then
  62. globals.err = "hung up"
  63. else
  64. espeak(tostring(n))
  65. end
  66. end
  67. local debug = function(head_pos, ip, tape)
  68. app.verbose(3, "brainfuck program invoked debug\n");
  69. espeak("DEBUG INFO")
  70. espeak("head position: " .. tostring(head_pos))
  71. espeak("instruction pointer: " .. tostring(ip))
  72. espeak("tape:")
  73. for k, v in pairs(tape) do
  74. espeak("at " .. tostring(k) .. " is " .. tostring(v))
  75. end
  76. end
  77. return bf.new_brainfuck_context(function(m) app.verbose(3, m) end, program, write, read, debug, function() return 0 end)
  78. end
  79. return function(ctx, mixc)
  80. ctx[621] = function()
  81. local globals = { err = nil }
  82. local program = get_prog()
  83. local bfc = read_number_context(program, globals)
  84. local counter = 50000000
  85. while bfc.retval == nil do
  86. bfc:step(app)
  87. if (counter == 0) then
  88. counter = 50000000
  89. app.checkhangup()
  90. if channel.HUNGUP:get() == "HUNGUP" then
  91. app.verbose(3, "hung up on running program")
  92. globals.err = "hung up"
  93. end
  94. end
  95. if globals.err ~= nil then
  96. return
  97. end
  98. counter = counter - 1
  99. end
  100. local tab = bfc.retval
  101. if tab.kind == "error" then
  102. espeak("error " .. tab.value)
  103. elseif tab.kind == "return" then
  104. espeak("success")
  105. else
  106. espeak("unknown state")
  107. end
  108. app.hangup()
  109. end
  110. end