Advertisement
Lanzr

hexCompiler_multiPattern

Jun 11th, 2024 (edited)
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.41 KB | None | 0 0
  1. local path = arg[1]
  2. if(path == nil) then
  3.     print("#param 1 : filePath")
  4.     return
  5. end
  6. if(fs.exists(path) == false) then
  7.     print("file "..path.." is not exist!")
  8.     return
  9. end
  10. local inf = io.open(path,"r") -- the source_code filename
  11. local str = inf.read(inf,"*all")
  12. inf.close(inf)
  13.  
  14. local fPort = peripheral.find("focal_port")
  15.  
  16. local lastIndex = 0
  17. local index = -1
  18. local leftBrackIndex = nil
  19.  
  20. local hexlist = {} -- the final table use to output
  21.  
  22. hexMap = { -- add pattern table to this table
  23.     ["me"] = {["startDir"]="EAST",["angles"]="qaq"},
  24.     ["{"] = {["startDir"]="WEST",["angles"]="qqq"},
  25.     ["}"] = {["startDir"]="EAST",["angles"]="eee"},
  26.     ["pos"] = {["startDir"]="EAST",["angles"]="aa"},
  27.     ["sight"] = {["startDir"]="EAST",["angles"]="wa"},
  28.     ["unpack"] = {["startDir"]="NORTH_WEST",["angles"]="qwaeawq"},
  29.     ["+"] = {["startDir"]="NORTH_EAST",["angles"]="waaw"},
  30.     ["-"] = {["startDir"]="NORTH_WEST",["angles"]="wddw"},
  31.     ["*"] = {["startDir"]="SOUTH_EAST",["angles"]="waqaw"}
  32. }
  33.  
  34. NumMap = {
  35.     [0] = {["startDir"]="SOUTH_EAST",["angles"]="aqaa"},
  36.     [1] = {["startDir"]="SOUTH_EAST",["angles"]="aqaaw"},
  37.     [2] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawa"},
  38.     [3] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawaw"},
  39.     [4] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawaa"},
  40.     [5] = {["startDir"]="SOUTH_EAST",["angles"]="aqaaq"},
  41.     [6] = {["startDir"]="SOUTH_EAST",["angles"]="aqaaqw"},
  42.     [7] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawaq"},
  43.     [8] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawwaa"},
  44.     [9] = {["startDir"]="SOUTH_EAST",["angles"]="aqaawaaq"},
  45.     [10] = {["startDir"]="SOUTH_EAST",["angles"]="aqaaqa"}
  46. }
  47.  
  48. local regMap = {
  49.     ["^[\t ]*([{}*+])[\t ]*$"] = (function (cStr)
  50.         table.insert(hexlist,hexMap[cStr])
  51.     return true end),
  52.     ["^[\t ]*rm[ ]*(%d+)[\t ]*$"] = (function (cStr)
  53.         addRMPattern(cStr)
  54.     return true end),
  55.     ["^[\t ]*(-?[%w_]+)[\t ]*$"] = (function (cStr)
  56.         if( tonumber(cStr) ~= nil) then
  57.             genNumPattern(tonumber(cStr))
  58.         else
  59.             local t = hexMap[cStr]
  60.             if t == nil then
  61.                 return false
  62.             end
  63.             table.insert(hexlist,t)
  64.         end
  65.     return true end),
  66. }
  67.  
  68. function genNumPattern(num)
  69.     local stackOpe = {}
  70.     local len = 0
  71.     local oper = 0
  72.     local rem = num > 0 and num or -num
  73.     if num < 0 then
  74.         table.insert(hexlist,NumMap[0])
  75.     end
  76.     repeat
  77.         oper = rem % 10
  78.         rem = (rem - oper) /10
  79.         table.insert(stackOpe,oper)
  80.         len = len + 1
  81.     until  rem < 1  
  82.     rem = 0
  83.     i = len
  84.     while true do
  85.         table.insert(hexlist,NumMap[stackOpe[i]])
  86.         if(i < len) then
  87.             table.insert(hexlist,hexMap["+"])
  88.         end
  89.         i = i - 1
  90.         if i < 1 then
  91.             break
  92.         end
  93.         table.insert(hexlist,NumMap[10])
  94.         table.insert(hexlist,hexMap["*"])
  95.     end
  96.     if num < 0 then
  97.         table.insert(hexlist,hexMap["-"])
  98.     end
  99. end
  100.  
  101. function addRMPattern(rmPos)
  102.     local rmPattern = {}
  103.     local angleStr = ""
  104.     local pos = tonumber(rmPos)
  105.     rmPattern["startDir"] = "EAST"
  106.     if (pos > 1) then
  107.         for i=1,pos-1,1 do
  108.             angleStr = angleStr.."w"
  109.         end
  110.         angleStr = angleStr.."ea"
  111.     else
  112.         angleStr = "a"
  113.     end
  114.     rmPattern["angles"] = angleStr
  115.     table.insert(hexlist,rmPattern)
  116. end
  117.    
  118. function mainloop()
  119.     local lastIndex = 0
  120.     local index = -1
  121.     local cut = ""
  122.     local lineIndex = 0
  123.     while ( index ~= nil) do
  124.         local syntaxFlag = true;
  125.         lineIndex = lineIndex + 1
  126.         index = string.find(str,"\n", index + 2);
  127.         if( index ~= nil) then
  128.             cut = string.sub(str,lastIndex, index-1);
  129.         else
  130.             cut = string.sub(str,lastIndex, index);
  131.         end
  132.         cut = string.gsub(cut,"\n","")
  133.         lastIndex = index
  134.  
  135.         for key, cb in pairs(regMap) do
  136.             if (string.match(cut,key)~= nil) then
  137.                 local cStr = string.match(cut,key)
  138.                 syntaxFlag = cb(cStr)
  139.                 break
  140.             end  
  141.         end
  142.         if syntaxFlag ~= true then
  143.             print("Line "..lineIndex.." : "..cut.." is illegal syntax")
  144.             return
  145.         end
  146.     end
  147.    
  148.     -- out put final hexlist    
  149.     if(fPort ~= nil) then
  150.         fPort.writeIota(hexlist)
  151.     end
  152. end
  153.  
  154. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement