Advertisement
LazerAio

SWBf_interpreter.lua

Dec 9th, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.64 KB | None | 0 0
  1. -- Inputs
  2. -- b1 = add > +
  3. -- b2 = sub > -
  4. -- b3 = nxt > >
  5. -- b4 = lst > <
  6. -- b5 = stb > [
  7. -- b6 = enb > ]
  8. -- b7 = set > .
  9. -- b8 = nop > do nothing
  10. -- n1 = pos > instruction possition
  11. -- n2 = svl > value to use with set
  12. -- Outputs
  13. -- n1 = tape[index] > tape value at index
  14. -- n2 = rpos > Requested next instruction possition
  15. -- Others
  16. -- tape = memory
  17. -- index = memory possition
  18. -- stbs = pos when all stb calls was made
  19. -- wab = do not execute anything untill next enb
  20. tape = {}
  21. index = 1
  22. stbs = {}
  23. wab = false
  24. function onTick()
  25.     add = input.getBool(1)
  26.     sub = input.getBool(2)
  27.     nxt = input.getBool(3)
  28.     lst = input.getBool(4)
  29.     stb = input.getBool(5)
  30.     enb = input.getBool(6)
  31.     set = input.getBool(7)
  32.     nop = input.getBool(8)
  33.     pos = input.getNumber(1)
  34.     svl = input.getNumber(2)
  35.     if tape[index] == nil then
  36.         tape[index] = 0
  37.     end
  38.     if add and not wab then
  39.         tape[index] = tape[index] + 1
  40.         rpos = pos + 1
  41.     elseif sub and not wab then
  42.         tape[index] = tape[index] - 1
  43.         rpos = pos + 1
  44.     elseif nxt and not wab then
  45.         index = index + 1
  46.         rpos = pos + 1
  47.     elseif lst and not wab then
  48.         index = index - 1
  49.         rpos = pos + 1
  50.     elseif stb and not wab then
  51.         if tape[index] == 0 then
  52.             wab = true
  53.         else
  54.             stbs[#stbs+1] = pos
  55.         end
  56.         rpos = pos + 1
  57.     elseif enb then
  58.         rpos = pos + 1
  59.         if wab == false then
  60.             if tape[index] == 0 then
  61.                 stbs[#stbs] = nil
  62.             else
  63.                 rpos = stbs[#stbs]
  64.                 stbs[#stbs] = nil
  65.             end
  66.         else
  67.             wab = false
  68.         end
  69.     elseif set and not wab then
  70.         rpos = pos + 1
  71.         tape[index] = svl
  72.     elseif nop and not wab then
  73.         rpos = pos + 1
  74.     end
  75.     output.setNumber(2,rpos)
  76.     output.setNumber(1,tape[index])
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement