Advertisement
ayangd

Robot Script Interpreter

Oct 5th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. --[[
  2.  
  3.     Author ayangd
  4.    
  5.     Thing: robot script interpret
  6.     Description:
  7.         Easily move robot with a line of string.
  8.    
  9.     Note:
  10.         For ease use, just import this thing by:
  11.             rim = require('<this module>').interpret
  12.         then just do:
  13.             rim('3u4d2(u4(lr))') or whatever movement you want to do
  14.         (rim stands for RobotInterpretMovement)
  15.    
  16.     Usage:
  17.         - 6 main functions:
  18.             ( u, d, l, r, f, b ), each corresponding to
  19.             ( up, down, left, right, forward, back)
  20.             e.g robot forward twice then up:
  21.                 rim('ffu')
  22.         - numbers:
  23.             You can specify repetition of a function, just by specifying a number in front.
  24.             e.g robot forward 4 times then up:
  25.                 rim('4fu')
  26.         - subFunction:
  27.             Wrap a snippet of instruction, and do numbers to easily repeat a function pattern.
  28.             e.g robot (forward 4 times then up) 3 times:
  29.                 rim('3(4fu)')
  30.         - xternalFunction:
  31.             Call an external function parsed after the script.
  32.             e.g robot forward 4 times and do something else:
  33.                 rim('4fx', doSomething)
  34.             P.S it will pass no argument.
  35.  
  36. ]]--
  37. local robot = require('robot')
  38.  
  39. local interpreter = {}
  40.  
  41. -- Functions
  42. local funcs = {}
  43. function funcs.u()
  44.     robot.up()
  45. end
  46.  
  47. function funcs.d()
  48.     robot.down()
  49. end
  50.  
  51. function funcs.l()
  52.     robot.turnLeft()
  53. end
  54.  
  55. function funcs.r()
  56.     robot.turnRight()
  57. end
  58.  
  59. function funcs.f()
  60.     robot.forward()
  61. end
  62.  
  63. function funcs.b()
  64.     robot.back()
  65. end
  66.  
  67. -- Interpreter
  68.  
  69. function interpreter.interpret(s, xf)
  70.     local repetition = 1
  71.     local repeScan = ''
  72.     local subScan = {}
  73.     local scanMode = false
  74.     local innerBrackets = 0
  75.     for cp = 1, #s do
  76.         local c = s:sub(cp, cp)
  77.         if c == '(' then
  78.             if scanMode == true then
  79.                 innerBrackets = innerBrackets + 1
  80.                 table.insert(subScan, c)
  81.             else
  82.                 scanMode = true
  83.             end
  84.         elseif c == ')' then
  85.             if innerBrackets ~= 0 then
  86.                 innerBrackets = innerBrackets - 1
  87.                 table.insert(subScan, c)
  88.             else
  89.                 scanMode = false
  90.                 if repeScan ~= '' then
  91.                     repetition = tonumber(repeScan)
  92.                     repeScan = ''
  93.                 end
  94.                 local subScanRes = table.concat(subScan)
  95.                 for repe = 1, repetition do
  96.                     interpreter.interpret(subScanRes, xf)
  97.                 end
  98.                 repetition = 1
  99.                 subScan = {}
  100.             end
  101.         elseif scanMode then
  102.             table.insert(subScan, c)
  103.         elseif tonumber(c) ~= nil then
  104.             repeScan = repeScan .. c
  105.         elseif c == 'x' then
  106.             if repeScan ~= '' then
  107.                 repetition = tonumber(repeScan)
  108.                 repeScan = ''
  109.             end
  110.             for repe = 1, repetition do
  111.                 xf()
  112.             end
  113.         else
  114.             local executed = false
  115.             if repeScan ~= '' then
  116.                 repetition = tonumber(repeScan)
  117.                 repeScan = ''
  118.             end
  119.             for fn, f in pairs(funcs) do
  120.                 if c == fn then
  121.                     executed = true
  122.                     for repe = 1, repetition do
  123.                         f()
  124.                     end
  125.                     repetition = 1
  126.                     break
  127.                 end
  128.             end
  129.             if not executed then
  130.                 error(string.format('Interpretation failed at %d [unknown char %s].', cp, c), 2)
  131.             end
  132.         end
  133.     end
  134.     if scanMode then
  135.         error('Expected ) at eol.', 2)
  136.     end
  137.     if repeScan ~= '' then
  138.         error('Expected a function to repeat, found eol instead.', 2)
  139.     end
  140. end
  141.  
  142. return interpreter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement