Advertisement
ecoMeco

bfluac -- bf to lua

Jul 19th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. --Compiler args. Sort of...
  2. local memLimit = (2^8) - 1
  3. local memBlocks = 17
  4. local dbgSleep = .005
  5. local dbg = false
  6.  
  7. local args = { ... }
  8. local w, h = term.getSize()
  9. local x, y = 0, 0;
  10. local instCnt = 0
  11. local semStr = " "
  12. local memBlockStr = ""
  13.  
  14. local function interpet(code)
  15. print("Creating memory table...")
  16. for i = 1, memBlocks do
  17. memBlockStr = table.concat({memBlockStr, "0,"}, "")
  18. end
  19.  
  20. local soFar = 1
  21. local o = fs.open(args[1] .. ".bf.lua", "w")
  22. print("Writing common vars...")
  23. o.writeLine([[
  24. --CC bfluac!
  25. local mem = {]] .. memBlockStr .. [[}
  26. local ptr = 1;
  27. ]])
  28. if dbg then
  29. print("Writing debug functions...")
  30.  
  31. semstr = "drawMemory() sleep(" .. dbgSleep .. ") "
  32. o.writeLine([[
  33. local w, h = term.getSize()
  34. local x, y = 0, 0;
  35. local drewInst = 0;
  36.  
  37. local function drawBFInst(c)
  38. x, y = term.getCursorPos()
  39. term.setCursorPos(1 + drewInst, h)
  40. drewInst = drewInst + 1
  41.  
  42. if drewInst == w then
  43. term.clearLine()
  44. drewInst = 1
  45. term.setCursorPos(1, h)
  46. end
  47. write(c)
  48. term.setCursorPos(x, y)
  49. end
  50.  
  51. local function drawMemory()
  52. x, y = term.getCursorPos()
  53. term.setCursorPos(1, h - 3)
  54. term.clearLine()
  55. term.setCursorPos(1, h - 2)
  56. for i = 1, #mem do
  57. if ptr == i then
  58. _x, _ = term.getCursorPos()
  59. term.setCursorPos(_x, h - 3)
  60. write("v")
  61. term.setCursorPos(_x, h - 2)
  62. end
  63. write(mem[i] .. ",")
  64. end
  65. term.setCursorPos(x, y)
  66. end
  67. drawMemory()
  68. ]])
  69. else
  70. o.writeLine([[local function drawBFInst(c) end --Unused]])
  71. semstr = " "
  72. end
  73.  
  74. write("Interpeting: ")
  75. code:gsub(".", function(c)
  76. if c == ">" then
  77. o.writeLine(semstr .. " ptr = ptr + 1 drawBFInst('" .. c .. "')")
  78. write(c)
  79. elseif c == "<" then
  80. o.writeLine(semstr .. " ptr = ptr - 1 drawBFInst('" .. c .. "')")
  81. write(c)
  82. elseif c == "+" then
  83. o.writeLine(semstr .. " mem[ptr] = mem[ptr] + 1 if mem[ptr] >= " .. memLimit .. " then mem[ptr] = 0 + (mem[ptr] - " .. memLimit .. ") end drawBFInst('" .. c .. "')")
  84. write(c)
  85. elseif c == "-" then
  86. o.writeLine(semstr .. " mem[ptr] = mem[ptr] - 1 if mem[ptr] <= -1 then mem[ptr] = " .. memLimit .. " - mem[ptr] end drawBFInst('" .. c .. "')")
  87. write(c)
  88. elseif c == "." then
  89. o.writeLine(semstr .. " if mem[ptr] >= " .. memLimit .. " then mem[ptr] = 0 + (mem[ptr] - " .. memLimit .. ") end write(string.char(mem[ptr])) drawBFInst('" .. c .. "')")
  90. write(c)
  91. elseif c == "," then
  92. o.writeLine(semstr .. " mem[ptr] = read() drawBFInst('" .. c .. "')")
  93. write(c)
  94. elseif c == "[" then
  95. o.writeLine(semstr .. " while mem[ptr] ~= 0 do drawBFInst('" .. c .. "')")
  96. write(c)
  97. elseif c == "]" then
  98. o.writeLine(semstr .. " end drawBFInst('" .. c .. "')")
  99. write(c)
  100. end
  101. end)
  102.  
  103. if dbg then
  104. write("\nAppending memory display cleaner...")
  105. o.writeLine([[local x, y = term.getCursorPos()
  106. term.setCursorPos(1, h) term.clearLine()
  107. term.setCursorPos(1, h - 2) term.clearLine()
  108. term.setCursorPos(1, h - 3) term.clearLine()
  109. term.setCursorPos(x, y)]])
  110. end
  111. print("\nAppending source...")
  112. o.writeLine("-- Original: " .. code .. "")
  113. o.close()
  114. print("Done.")
  115. end
  116.  
  117. if #args >= 1 then
  118. print("bfluac 1.0.0 by Admicos")
  119. print("Checking args...")
  120. for i = 1, #args do
  121. if args[i] == "-memLimit" then
  122. memLimit = tonumber(args[i + 1])
  123. i = i + 1
  124. print("memLimit set to " .. memLimit)
  125. elseif args[i] == "-memBlocks" then
  126. memBlocks = tonumber(args[i + 1])
  127. i = i + 1
  128. print("memBlocks set to " .. memBlocks)
  129. elseif args[i] == "-dbgSleep" then
  130. dbgSleep = tonumber(args[i + 1])
  131. i = i + 1
  132. print("dbgSleep set to " .. dbgSleep)
  133. elseif args[i] == "+dbg" then
  134. dbg = true
  135. print("Debugging turned on.")
  136. end
  137. end
  138.  
  139. local f = fs.open(fs.combine(shell.dir(), args[1]), "r")
  140. local code = f.readAll()
  141. f.close()
  142.  
  143. interpet(code)
  144. else
  145. print([[bfluac -- convert bf to lua
  146.  
  147. HOW TO USE
  148. bfluac.lua <file containing bf code> [extra arguments]
  149. This will save the lua code to <filename>.bf.lua
  150.  
  151. ARGUMENTS
  152. -memLimit <number> allows you the set maximum value of a memory block (default 28-1)
  153. -memBlocks <number> allows you the set how many memory blocks will be available for the program. Too low might cause crashes (default: 2^8-1)
  154. -dbgSleep <number> how many seconds should the program wait before executing the next instruction (debug only) (default: 0.005)
  155. +dbg enables debug mode for the program]])
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement