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.

brainfuck_parse.lua 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. local node_plus_const = function(tape_offset, amount)
  2. return
  3. {
  4. nodetype = "plus",
  5. pure = true,
  6. left = {valtype = "tape", offset = tape_offset},
  7. right = {valtype = "immediate", value = amount},
  8. out = {valtype = "tape", offset = tape_offset},
  9. temporal_dependencies = {},
  10. pure_dependencies = {},
  11. }
  12. end
  13. local node_out = function(tape_offset)
  14. return
  15. {
  16. nodetype = "out",
  17. pure = false,
  18. input = { valtype = "tape", offset = tape_offset },
  19. temporal_dependencies = {},
  20. pure_dependencies = {},
  21. }
  22. end
  23. local node_in = function(tape_offset)
  24. return
  25. {
  26. nodetype = "in",
  27. pure = false,
  28. output = { valtype = "tape", offset = tape_offset },
  29. temporal_dependencies = {},
  30. pure_dependencies = {},
  31. }
  32. end
  33. local node_root = function()
  34. return
  35. {
  36. nodetype = "root",
  37. pure = true,
  38. temporal_dependencies = {},
  39. pure_dependencies = {}
  40. }
  41. end
  42. local func_tab =
  43. {
  44. [1] = function(t)
  45. t.head_pos = t.head_pos - 1
  46. t.idx = t.idx + 1
  47. end,
  48. [2] = function(t)
  49. t.head_pos = t.head_pos + 1
  50. t.idx = t.idx + 1
  51. end,
  52. [3] = function(t)
  53. local node = node_plus_const(tape_offset, 1)
  54. node.temporal_dependencies = { t.current_node }
  55. t.current_node = node
  56. t.idx = t.idx + 1
  57. end,
  58. [4] = function(t)
  59. local node = node_plus_const(tape_offset, 1)
  60. node.temporal_dependencies = { t.current_node }
  61. t.current_node = node
  62. t.idx = t.idx + 1
  63. end,
  64. [5] = function(t)
  65. local node = node_out(tape_offset)
  66. node.temporal_dependencies = { t.current_node }
  67. t.current_node = node
  68. t.idx = t.idx + 1
  69. end,
  70. [6] = function(t)
  71. local node = node_in(tape_offset)
  72. node.temporal_dependencies = { t.current_node }
  73. t.current_node = node
  74. t.idx = t.idx + 1
  75. end,
  76. [7] = function(t)
  77. error("unimplemented")
  78. end,
  79. [8] = function(t)
  80. error("unimplemented")
  81. end,
  82. -- [nil] = function(t)
  83. -- t.ret_idx = t.idx
  84. -- end,
  85. }
  86. function do_parse_seq(sequence, idx)
  87. local t =
  88. {
  89. sym = sequence[idx],
  90. ret_idx = nil,
  91. idx = idx,
  92. head_pos = 0,
  93. current_node = node_root(),
  94. }
  95. while t.ret_idx == nil do
  96. func_tab[t.sym](t)
  97. t.sym = sequence[t.idx]
  98. if t.sym == nil then
  99. t.ret_idx = t.idx
  100. end
  101. end
  102. return t
  103. end
  104. function io_to_string(io)
  105. if io.valtype == "immediate" then
  106. return tostring(io.value)
  107. elseif io.valtype == "tape" then
  108. return "tape[" .. tostring(io.offset) .. "]"
  109. else
  110. return "unknown i/o"
  111. end
  112. end
  113. function recursive_node_string(node)
  114. if node.nodetype == "root" then
  115. return { "root" }
  116. elseif node.nodetype == "plus" then
  117. local dep = recursive_node_string(node.temporal_dependencies[1])
  118. table.insert(dep, "add " .. io_to_string(node.left) .. " + " .. io_to_string(node.right))
  119. return dep
  120. elseif node.nodetype == "out" then
  121. local dep = recursive_node_string(node.temporal_dependencies[1])
  122. table.insert(dep, "out " .. io_to_string(node.input))
  123. return dep
  124. elseif node.nodetype == "in" then
  125. local dep = recursive_node_string(node.temporal_dependencies[1])
  126. table.insert(dep, "in " .. tostring(node.output))
  127. return dep
  128. else
  129. return { "unknown node" }
  130. end
  131. end
  132. --str = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
  133. --str = "+[-]..."
  134. str = ".+.>.+.<.-.>.-."
  135. --str = "+[+]"
  136. char_table =
  137. {
  138. [">"] = 1,
  139. ["<"] = 2,
  140. ["+"] = 3,
  141. ["-"] = 4,
  142. ["."] = 5,
  143. [","] = 6,
  144. ["["] = 7,
  145. ["]"] = 8,
  146. }
  147. prog = {}
  148. for i = 1,#str do
  149. prog[#prog + 1] = char_table[string.sub(str, i, i)]
  150. end
  151. local q = do_parse_seq(prog, 1)
  152. print(table.concat(recursive_node_string(q.current_node), "\n"))