Advertisement
Tritonio

Brainfuck arbitrary size array implementation

Jan 12th, 2024
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 KB | None | 0 0
  1. --[[ Copyright 2008 Tritonio
  2.  
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9.  
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12.  
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. --]]
  21.  
  22. rep=string.rep
  23.  
  24. function read(n)
  25.     return  seek(n,k,1,0,1)..
  26.             rep(">",k)..
  27.             rep("<"..rep(">",2*k+2*n)..
  28.             "[-"..rep("<",n+k).."+"..rep("<",n+k).."+"..rep(">",2*n+2*k).."]"..
  29.             rep("<",2*k+2*n).."[-"..rep(">",2*k+2*n).."+"..rep("<",2*k+2*n)..
  30.             "]",k)..
  31.             retur(n,k,1,0,1)
  32. end
  33.  
  34. function left(n,k,w)
  35.     return  rep(">",n+k-1)..
  36.             rep(">"..rep("[-"..rep("<",k).."+"..rep(">",k).."]",1-w),k)..
  37.             rep(">[-"..rep("<",k).."+"..rep(">",k).."]",n)..
  38.             rep("<",2*k+2*n-1)..
  39.             rep("<[-"..rep(">",2*k+2*n).."+"..rep("<",2*k+2*n).."]",k)
  40. end
  41.  
  42. function retur(n,k,i,w,f)
  43.     local ret=  rep(">",2*k+2*n-i).."[-"..rep("<",2*k+2*n-i)..
  44.                 left(n,k,w)..rep(">",2*k+2*n-i)
  45.                 if i>1 then ret=ret..
  46.                 rep(">-<"..rep("<",2*k+2*n-i)..
  47.                 retur(n,k,i-1,w,0)..
  48.                 rep(">",2*k+2*n-i),(i>1) and 1 or 0) end
  49.                 ret=ret.."]"..rep("<",2*k+2*n-i) --simplirosa kai mia ].
  50.                 if i<n and f==1 then ret=ret..rep(retur(n,k,i+1,w,1),(i<n and 1 or 0)*f) end
  51.                 return ret
  52. end
  53.  
  54. function seek(n,k,i,w,f)
  55.     local ret=  rep(">",n+k-i).."[-"..rep(rep(">",n+k).."+"..rep("<",n+k),f)..
  56.                 rep("<",n+k-i)..right(n,k,w)..
  57.                 rep(">",n+k-i) --diakopi apotoma se sxesi me to xarti
  58.                 if i>1 then ret=ret..rep(">-<"..rep("<",n+k-i)..
  59.                 seek(n,k,i-1,w,0)..
  60.                 rep(">",n+k-i),(i>1) and 1 or 0) end
  61.                 --apotomi sinexia
  62.                 ret=ret.."]"..rep("<",n+k-i)
  63.                 if i<n and f==1 then ret=ret..rep(seek(n,k,i+1,w,1),(i<n and 1 or 0)*f) end
  64.                 return ret
  65. end
  66.  
  67. function write(n,k,i,w,f)
  68.     return  seek(n,k,1,1,1)..
  69.             rep(">",3*k+2*n)..
  70.             rep("<[-]",k)..rep("<",n)..
  71.             rep("<[-"..rep(">",n+k).."+"..rep("<",n+k).."]",k)..
  72.             rep("<",n+k)..
  73.             retur(n,k,1,1,1)
  74. end
  75.  
  76. function right(n,k,w)
  77.     return  rep(">",3*k+2*n)..
  78.             rep("<[-"..rep("<",2*k+2*n).."+"..rep(">",2*k+2*n).."]",k)..
  79.             rep("<[-"..rep(">",k).."+"..rep("<",k).."]",n)..
  80.             rep("<"..rep("[-"..rep(">",k).."+"..rep("<",k).."]",w),k)..
  81.             rep("<[-"..rep(">",k).."+"..rep("<",k).."]",n)
  82. end
  83.  
  84. function optimize(code)
  85.     repeat
  86.         local changes=0
  87.         local tempchanges=0
  88.         code,tempchanges=string.gsub(code,"<>","")
  89.         changes=tempchanges+changes
  90.         code,tempchanges=string.gsub(code,"><","")
  91.         changes=tempchanges+changes
  92.         code,tempchanges=string.gsub(code,"%-%+","")
  93.         changes=tempchanges+changes
  94.         code,tempchanges=string.gsub(code,"%+%-","")
  95.         changes=tempchanges+changes
  96.         --print(changes)
  97.     until changes==0
  98.     return code
  99. end
  100.  
  101. print("Brainfuck Array Wizard by Tritonio")
  102. print()
  103. io.write("How many bytes per cell? ")
  104. k=io.read()/1
  105. io.write("How many cells? ")
  106. cells=io.read()
  107. n=math.ceil(math.log(cells)/math.log(256))
  108. if n==0 then n=1 end
  109. print()
  110. print("This is the structure of the table: "..rep("0",k)..rep("I",n)..rep("D",k)..rep("0",n).."("..cells*k.." empty cells for array data)") --to n*k den einai sosto.
  111. print("(0 stands for empty cell, I for index, D for data)")
  112. print("(The most significant index is the first one)")
  113. print()
  114. print("The total size of the table is "..cells*k+k+n+k+n..".")
  115. print("The overhead because of the indexes etc is "..math.floor(0.5+100000*(k+n+k+n)/(cells*k+k+n+k+n))/1000 .."% of the total size.")
  116. print()
  117. print("To write to the table fill in the I (index) and the D (data) positions in the header, move the Brainfuck pointer to the beginning of the structure and execute the following code:")
  118. print(optimize(write(n,k)))
  119. print("After the execution, the indexes and the data fields will be zeroed and the Brainfuck pointer will be left at the beginning of the structure.")
  120. --print(write(n,k))
  121. print()
  122. print("To read from the table fill the I (index) fields, move the Brainfuck pointer to the beginning of the structure and execute the following code:")
  123. print(optimize(read(n,k)))
  124. --print(read(n,k))
  125. print("After the execution, the indexes will be zeroed, the Brainfuck pointer will be left at the beginning of the structure and the data fields will contain the read data.")
  126. io.read()
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement