Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local path = arg[1]
- if(path == nil) then
- print("#param 1 : filePath")
- return
- end
- if(fs.exists(path) == false) then
- print("file "..path.." is not exist!")
- return
- end
- local inf = io.open(path,"r") -- the source_code filename
- local str = inf.read(inf,"*all")
- inf.close(inf)
- local fPort = peripheral.find("focal_port")
- local lastIndex = 0
- local index = -1
- local leftBrackIndex = nil
- local hexlist = {} -- the final table use to output
- hexMap = { -- add pattern table to this table
- ["me"] = {["startDir"]="EAST",["angles"]="qaq"},
- ["{"] = {["startDir"]="WEST",["angles"]="qqq"},
- ["}"] = {["startDir"]="EAST",["angles"]="eee"},
- ["pos"] = {["startDir"]="EAST",["angles"]="aa"},
- ["sight"] = {["startDir"]="EAST",["angles"]="wa"},
- ["unpack"] = {["startDir"]="NORTH_WEST",["angles"]="qwaeawq"},
- ["+"] = {["startDir"]="NORTH_EAST",["angles"]="waaw"},
- ["-"] = {["startDir"]="NORTH_WEST",["angles"]="wddw"},
- ["*"] = {["startDir"]="SOUTH_EAST",["angles"]="waqaw"}
- }
- NumMap = {
- [0] = {["startDir"]="SOUTH_EAST",["angles"]="aqaa"},
- [1] = {["startDir"]="SOUTH_EAST",["angles"]="aqaaw"},
- [2] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawa"},
- [3] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawaw"},
- [4] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawaa"},
- [5] = {["startDir"]="SOUTH_EAST",["angles"]="aqaaq"},
- [6] = {["startDir"]="SOUTH_EAST",["angles"]="aqaaqw"},
- [7] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawaq"},
- [8] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawwaa"},
- [9] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawaaq"},
- [10] = {["startDir"]="SOUTH_EAST",["angles"]="aqaaqa"}
- }
- local regMap = {
- ["^[\t ]*([{}*+])[\t ]*$"] = (function (cStr)
- table.insert(hexlist,hexMap[cStr])
- return true end),
- ["^[\t ]*rm[ ]*(%d+)[\t ]*$"] = (function (cStr)
- addRMPattern(cStr)
- return true end),
- ["^[\t ]*(-?[%w_]+)[\t ]*$"] = (function (cStr)
- if( tonumber(cStr) ~= nil) then
- genNumPattern(tonumber(cStr))
- else
- local t = hexMap[cStr]
- if t == nil then
- return false
- end
- table.insert(hexlist,t)
- end
- return true end),
- }
- function genNumPattern(num)
- local stackOpe = {}
- local len = 0
- local oper = 0
- local rem = num > 0 and num or -num
- if num < 0 then
- table.insert(hexlist,NumMap[0])
- end
- repeat
- oper = rem % 10
- rem = (rem - oper) /10
- table.insert(stackOpe,oper)
- len = len + 1
- until rem < 1
- rem = 0
- i = len
- while true do
- table.insert(hexlist,NumMap[stackOpe[i]])
- if(i < len) then
- table.insert(hexlist,hexMap["+"])
- end
- i = i - 1
- if i < 1 then
- break
- end
- table.insert(hexlist,NumMap[10])
- table.insert(hexlist,hexMap["*"])
- end
- if num < 0 then
- table.insert(hexlist,hexMap["-"])
- end
- end
- function addRMPattern(rmPos)
- local rmPattern = {}
- local angleStr = ""
- local pos = tonumber(rmPos)
- rmPattern["startDir"] = "EAST"
- if (pos > 1) then
- for i=1,pos-1,1 do
- angleStr = angleStr.."w"
- end
- angleStr = angleStr.."ea"
- else
- angleStr = "a"
- end
- rmPattern["angles"] = angleStr
- table.insert(hexlist,rmPattern)
- end
- function mainloop()
- local lastIndex = 0
- local index = -1
- local cut = ""
- local lineIndex = 0
- while ( index ~= nil) do
- local syntaxFlag = true;
- lineIndex = lineIndex + 1
- index = string.find(str,"\n", index + 2);
- if( index ~= nil) then
- cut = string.sub(str,lastIndex, index-1);
- else
- cut = string.sub(str,lastIndex, index);
- end
- cut = string.gsub(cut,"\n","")
- lastIndex = index
- for key, cb in pairs(regMap) do
- if (string.match(cut,key)~= nil) then
- local cStr = string.match(cut,key)
- syntaxFlag = cb(cStr)
- break
- end
- end
- if syntaxFlag ~= true then
- print("Line "..lineIndex.." : "..cut.." is illegal syntax")
- return
- end
- end
- -- out put final hexlist
- if(fPort ~= nil) then
- fPort.writeIota(hexlist)
- end
- end
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement