Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Author ayangd
- Thing: robot script interpret
- Description:
- Easily move robot with a line of string.
- Note:
- For ease use, just import this thing by:
- rim = require('<this module>').interpret
- then just do:
- rim('3u4d2(u4(lr))') or whatever movement you want to do
- (rim stands for RobotInterpretMovement)
- Usage:
- - 6 main functions:
- ( u, d, l, r, f, b ), each corresponding to
- ( up, down, left, right, forward, back)
- e.g robot forward twice then up:
- rim('ffu')
- - numbers:
- You can specify repetition of a function, just by specifying a number in front.
- e.g robot forward 4 times then up:
- rim('4fu')
- - subFunction:
- Wrap a snippet of instruction, and do numbers to easily repeat a function pattern.
- e.g robot (forward 4 times then up) 3 times:
- rim('3(4fu)')
- - xternalFunction:
- Call an external function parsed after the script.
- e.g robot forward 4 times and do something else:
- rim('4fx', doSomething)
- P.S it will pass no argument.
- ]]--
- local robot = require('robot')
- local interpreter = {}
- -- Functions
- local funcs = {}
- function funcs.u()
- robot.up()
- end
- function funcs.d()
- robot.down()
- end
- function funcs.l()
- robot.turnLeft()
- end
- function funcs.r()
- robot.turnRight()
- end
- function funcs.f()
- robot.forward()
- end
- function funcs.b()
- robot.back()
- end
- -- Interpreter
- function interpreter.interpret(s, xf)
- local repetition = 1
- local repeScan = ''
- local subScan = {}
- local scanMode = false
- local innerBrackets = 0
- for cp = 1, #s do
- local c = s:sub(cp, cp)
- if c == '(' then
- if scanMode == true then
- innerBrackets = innerBrackets + 1
- table.insert(subScan, c)
- else
- scanMode = true
- end
- elseif c == ')' then
- if innerBrackets ~= 0 then
- innerBrackets = innerBrackets - 1
- table.insert(subScan, c)
- else
- scanMode = false
- if repeScan ~= '' then
- repetition = tonumber(repeScan)
- repeScan = ''
- end
- local subScanRes = table.concat(subScan)
- for repe = 1, repetition do
- interpreter.interpret(subScanRes, xf)
- end
- repetition = 1
- subScan = {}
- end
- elseif scanMode then
- table.insert(subScan, c)
- elseif tonumber(c) ~= nil then
- repeScan = repeScan .. c
- elseif c == 'x' then
- if repeScan ~= '' then
- repetition = tonumber(repeScan)
- repeScan = ''
- end
- for repe = 1, repetition do
- xf()
- end
- else
- local executed = false
- if repeScan ~= '' then
- repetition = tonumber(repeScan)
- repeScan = ''
- end
- for fn, f in pairs(funcs) do
- if c == fn then
- executed = true
- for repe = 1, repetition do
- f()
- end
- repetition = 1
- break
- end
- end
- if not executed then
- error(string.format('Interpretation failed at %d [unknown char %s].', cp, c), 2)
- end
- end
- end
- if scanMode then
- error('Expected ) at eol.', 2)
- end
- if repeScan ~= '' then
- error('Expected a function to repeat, found eol instead.', 2)
- end
- end
- return interpreter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement