Advertisement
Lanzr

hexCompiler_infinityTri

Jun 11th, 2024 (edited)
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 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. local 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. local NumMap = {
  35.     [0] = {["startDir"]="SOUTH_EAST",["angles"]="aqaa"},
  36.     ["+1"] = (function () return "w" end),
  37.     ["*2"] = (function () return "a" end)
  38. }
  39.  
  40. local regMap = {
  41.     ["^[\t ]*([{}*+])[\t ]*$"] = (function (cStr)
  42.         table.insert(hexlist,hexMap[cStr])
  43.     return true end),
  44.     ["^[\t ]*rm[ ]*(%d+)[\t ]*$"] = (function (cStr)
  45.         addRMPattern(cStr)
  46.     return true end),
  47.     ["^[\t ]*(-?[%w_]+)[\t ]*$"] = (function (cStr)
  48.         if( tonumber(cStr) ~= nil) then
  49.             addNumPattern(tonumber(cStr))
  50.         else
  51.             local t = hexMap[cStr]
  52.             if t == nil then
  53.                 return false
  54.             end
  55.             table.insert(hexlist,t)
  56.         end
  57.     return true end),
  58. }
  59.  
  60. function addNumPattern(num)
  61.     local numPattern = {}
  62.     local opers = {}
  63.     local size = 0
  64.     local rem = num > 0 and num or -num
  65.     local numStr = "aqaa"
  66.     numPattern["startDir"] = "SOUTH_EAST"
  67.     repeat
  68.         if rem % 2 == 0 then
  69.             table.insert(opers, "*2")
  70.             rem = rem / 2
  71.         else
  72.             table.insert(opers,"+1")
  73.             rem = rem -1
  74.         end
  75.         size = size +1
  76.     until  rem < 1  
  77.     for i = size, 1, -1 do
  78.         numStr = numStr..NumMap[opers[i]]()
  79.     end
  80.     numPattern["angles"] = numStr
  81.     if num < 0 then
  82.         table.insert(hexlist,NumMap[0])
  83.         table.insert(hexlist,numPattern)
  84.         table.insert(hexlist,hexMap["-"])
  85.     else
  86.         table.insert(hexlist,numPattern)
  87.     end
  88. end
  89.  
  90. function addRMPattern(rmPos)
  91.     local rmPattern = {}
  92.     local angleStr = ""
  93.     local pos = tonumber(rmPos)
  94.     rmPattern["startDir"] = "EAST"
  95.     if (pos > 1) then
  96.         for i=1,pos-1,1 do
  97.             angleStr = angleStr.."w"
  98.         end
  99.         angleStr = angleStr.."ea"
  100.     else
  101.         angleStr = "a"
  102.     end
  103.     rmPattern["angles"] = angleStr
  104.     table.insert(hexlist,rmPattern)
  105. end
  106.    
  107. function mainloop()
  108.     local lastIndex = 0
  109.     local index = -1
  110.     local cut = ""
  111.     local lineIndex = 0
  112.     while ( index ~= nil) do
  113.         local syntaxFlag = true;
  114.         lineIndex = lineIndex + 1
  115.         index = string.find(str,"\n", index + 2);
  116.         if( index ~= nil) then
  117.             cut = string.sub(str,lastIndex, index-1);
  118.         else
  119.             cut = string.sub(str,lastIndex, index);
  120.         end
  121.         cut = string.gsub(cut,"\n","")
  122.         lastIndex = index
  123.  
  124.         for key, cb in pairs(regMap) do
  125.             if (string.match(cut,key)~= nil) then
  126.                 local cStr = string.match(cut,key)
  127.                 syntaxFlag = cb(cStr)
  128.                 break
  129.             end  
  130.         end
  131.         if syntaxFlag ~= true then
  132.             print("Line "..lineIndex.." : "..cut.." is illegal syntax")
  133.         end
  134.     end
  135.  
  136.     -- out put final hexlist    
  137.     if(fPort ~= nil) then
  138.         fPort.writeIota(hexlist)
  139.         return
  140.     end
  141. end
  142.  
  143. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement