Advertisement
Lanzr

HexCompiler_LanzrMap

Jun 16th, 2024 (edited)
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.22 KB | None | 0 0
  1. --[[
  2.     * a  mineCraft HexCasting Mod Compiler, can compile the hCode in game and output to the focus
  3.     * need mod : Ducky peripheral
  4.     * author : Lanzr
  5. ]]
  6. local path = arg[1]
  7. if(path == nil) then
  8.     print("#param 1 : filePath")
  9.     return
  10. end
  11. if(fs.exists(path) == false) then
  12.     print("file "..path.." is not exist!")
  13.     return
  14. end
  15.  
  16. local inf = io.open(path,"r") -- the source_code filename
  17. local codeStr = inf.read(inf,"*all")
  18. inf.close(inf)
  19.  
  20. local fPort = peripheral.find("focal_port")
  21.  
  22. local lastIndex = 0
  23. local index = -1
  24. local leftBrackIndex = nil
  25.  
  26. local hexlist = {} -- the final table use to output
  27.  
  28. local funcKey = nil
  29. local funcMap = {}
  30.  
  31. function genRegex(str) return "^[\t ]*"..str.."[\t ]*$" end
  32.  
  33. local preMap = {
  34.     ["include"] = genRegex("@include[ ]+([%w_]+)"),
  35.     ["func"] = genRegex("@func[ ]+([%w_]+)"),
  36. }
  37. local hexMap = { -- add pattern table to this table
  38.     ["me"] = {["startDir"]="EAST",["angles"]="qaq"},
  39.     ["getEntityHight"] = {["startDir"]="EAST",["angles"]="awq"},
  40.     ["{"] = {["startDir"]="WEST",["angles"]="qqq"},
  41.     ["}"] = {["startDir"]="EAST",["angles"]="eee"},
  42.     ["pos"] = {["startDir"]="EAST",["angles"]="aa"},
  43.     ["sight"] = {["startDir"]="EAST",["angles"]="wa"},
  44.     ["unpackList"] = {["startDir"]="NORTH_WEST",["angles"]="qwaeawq"},
  45.     ["packList"] = {["startDir"]="SOUTH_WEST",["angles"]="ewdqdwe"},
  46.     ["packVec"] = {["startDir"]="SOUTH_WEST",["angles"]="eqqqqq"},
  47.     ["+"] = {["startDir"]="NORTH_EAST",["angles"]="waaw"},
  48.     ["-"] = {["startDir"]="NORTH_WEST",["angles"]="wddw"},
  49.     ["*"] = {["startDir"]="SOUTH_EAST",["angles"]="waqaw"},
  50.     ["/"] = {["startDir"]="SOUTH_EAST",["angles"]="wdedw"},
  51.     ["isEqual"] = {["startDir"]="SOUTH_EAST",["angles"]="ad"},
  52.     ["dig"] = {["startDir"]="EAST",["angles"]="qaqqqqq"},
  53.     ["rayCast_getEntity"] = {["startDir"]="EAST",["angles"]="weaqa"},
  54.     ["rayCast_getBlock"] = {["startDir"]="EAST",["angles"]="wqaawdd"},
  55.     ["rayCast_getBlockRule"] = {["startDir"]="EAST",["angles"]="weddwaa"},
  56.     ["makeCypher"] = {["startDir"]="EAST",["angles"]="waqqqqq"},
  57.     ["makeTrinket"] = {["startDir"]="EAST",["angles"]="wwaqqqqqeaqeaeqqqeaeq"},
  58.     ["makeArtifact"] = {["startDir"]="EAST",["angles"]="wwaqqqqqeawqwqwqwqwqwwqqeadaeqqeqqeadaeqq"},
  59.     ["clean"] = {["startDir"]="EAST",["angles"]="qdqawwaww"},
  60.  
  61.     ["blink"] = {["startDir"]="SOUTH_WEST",["angles"]="awqqqwaq"},
  62.     ["listSelect"] = {["startDir"]="NORTH_WEST",["angles"]="deeed"},
  63.     ["areaGetItems"] = {["startDir"]="SOUTH_EAST",["angles"]="qqqqqwdeddww"},
  64.     ["swap"] = {["startDir"]="NORTH_EAST",["angles"]="aawdd"},
  65.     ["dup"] = {["startDir"]="EAST",["angles"]="aadaa"},
  66.     ["mod"] = {["startDir"]="NORTH_EAST",["angles"]="wqaqw"},
  67.     ["return"] = {["startDir"]="SOUTH_WEST",["angles"]="aqdee"},
  68.  
  69.     ["true2or3"] = {["startDir"]="SOUTH_EAST",["angles"]="awdd"},
  70.     ["any2bool"] = {["startDir"]="EAST",["angles"]="aw"},
  71.  
  72.     ["run"] = {["startDir"]="SOUTH_EAST",["angles"]="deaqq"},
  73.     ["debug"] = {["startDir"]="NORTH_EAST",["angles"]="de"},
  74.     ["readFhand"] = {["startDir"]="EAST",["angles"]="aqqqqq"},
  75.     ["canOffhandBeRead"] = {["startDir"]="EAST",["angles"]="aqqqqqe"},
  76.     ["readFring"] = {["startDir"]="EAST",["angles"]="aqqqqqeawqwqwqwqwqw"},
  77.     ["write2ring"] = {["startDir"]="EAST",["angles"]="deeeeeqdwewewewewew"},
  78.     ["write2Neck"] = {["startDir"]="SOUTH_EAST",["angles"]="wadeeeeeq"},
  79.     ["readFNeck"] = {["startDir"]="SOUTH_EAST",["angles"]="waaqqqqqe"},
  80.     ["readFBlock"] = {["startDir"]="EAST",["angles"]="aqqqqqeawqwaw"},
  81.     ["CanReadFBlock"] = {["startDir"]="EAST",["angles"]="aqqqqqeawqwawe"},
  82.  
  83.     ["write2mm"] = {["startDir"]="NORTH_WEST",["angles"]="eqqwawqaaw"},
  84.     ["readFmm"] = {["startDir"]="NORTH_EAST",["angles"]="qeewdweddw"},
  85.    
  86.     ["door_open"] = {["startDir"]="WEST",["angles"]="qaqeede"},
  87.     ["door_close"] = {["startDir"]="WEST",["angles"]="qqqwwqqqwqqawdedw"},
  88.     ["charge"] = {["startDir"]="NORTH_WEST",["angles"]="qqqqqwaeaeaeaeaea"},
  89.     ["runFEList"] = {["startDir"]="WEST",["angles"]="dadad"},
  90.     ["light"] = {["startDir"]="WEST",["angles"]="qqd"},
  91.     ["tp"] = {["startDir"]="EAST",["angles"]="wqeeweewwweedqqewqqqwwwqqaeewqq"},
  92.     ["makeBattery"] = {["startDir"]="SOUTH_WEST",["angles"]="aaeaaqeqqawdeeaeddwqwqqeqqwqeaqaa"},
  93.     ["grandGuard"] = {["startDir"]="NORTH_WEST",["angles"]="eeweeewaqqqeqqqqe"},
  94.  
  95.     ["soulCast"] = {["startDir"]="NORTH_WEST",["angles"]="eqqwqwadaadqeedqwdeqqaawdwdaq"},
  96.     ["soulPos"] = {["startDir"]="NORTH_EAST",["angles"]="qaqqqqaq"},
  97.     ["soulSight"] = {["startDir"]="NORTH_EAST",["angles"]="waawaq"},
  98.  
  99.     ["makeGloopArtifact"] = {["startDir"]="EAST",["angles"]="wwaadaqwaweqqwaweewawqwwwwadeeeeeqww"},
  100.     ["gloopDownload"] = {["startDir"]="NORTH_EAST",["angles"]="qedadewdde"},
  101.     ["gloopIotaRead"] = {["startDir"]="NORTH_EAST",["angles"]="aqqqqqeqadaqw"},
  102.     ["gloopIotaWrite"] = {["startDir"]="NORTH_EAST",["angles"]="deeeeeqqadaqw"},
  103.     ["canGloopIotaRead"] = {["startDir"]="NORTH_EAST",["angles"]="wwqadaqwwaqqqqq"},
  104. }
  105.  
  106. local NumMap = {
  107.     [0] = {["startDir"]="SOUTH_EAST",["angles"]="aqaa"},
  108.     ["+1"] = (function () return "w" end),
  109.     ["*2"] = (function () return "a" end)
  110. }
  111.  
  112. local regMap = {
  113.     [genRegex("([{}>%*%+-=</])")] = (function (cStr)
  114.         table.insert(hexlist,hexMap[cStr])
  115.     return true end),
  116.     [genRegex("rm[ ]+(%d+)")] = (function (cStr)
  117.         addRMPattern(cStr)
  118.     return true end),
  119.     [genRegex("(-?[%d]+)")] = (function (cStr)
  120.         addNumPattern(tonumber(cStr))
  121.     return true end),
  122.     [genRegex("([%a_]+[%w_]*)")] = (function (cStr)
  123.         local t = hexMap[cStr]
  124.         if t == nil then
  125.             return false
  126.         end
  127.         table.insert(hexlist,t)
  128.     return true end),
  129.     [genRegex("([%a_]+[%w_]*)%(%)")] = (function (cStr)
  130.         parseStr(funcMap[cStr])
  131.     return true end)
  132. }
  133.  
  134. function addNumPattern(num)
  135.     local numPattern = {}
  136.     local opers = {}
  137.     local size = 0
  138.     local rem = num > 0 and num or -num
  139.     local numStr = "aqaa"
  140.     numPattern["startDir"] = "SOUTH_EAST"
  141.     repeat
  142.         if rem % 2 == 0 then
  143.             table.insert(opers, "*2")
  144.             rem = rem / 2
  145.         else
  146.             table.insert(opers,"+1")
  147.             rem = rem -1
  148.         end
  149.         size = size +1
  150.     until  rem < 1  
  151.     for i = size, 1, -1 do
  152.         numStr = numStr..NumMap[opers[i]]()
  153.     end
  154.     numPattern["angles"] = numStr
  155.     if num < 0 then
  156.         table.insert(hexlist,NumMap[0])
  157.         table.insert(hexlist,numPattern)
  158.         table.insert(hexlist,hexMap["-"])
  159.     else
  160.         table.insert(hexlist,numPattern)
  161.     end
  162. end
  163.  
  164. function addRMPattern(rmPos)
  165.     local rmPattern = {}
  166.     local angleStr = ""
  167.     local pos = tonumber(rmPos)
  168.     rmPattern["startDir"] = "EAST"
  169.     if (pos > 1) then
  170.         for i=1,pos-1,1 do
  171.             angleStr = angleStr.."w"
  172.         end
  173.         angleStr = angleStr.."ea"
  174.     else
  175.         angleStr = "a"
  176.     end
  177.     rmPattern["angles"] = angleStr
  178.     table.insert(hexlist,rmPattern)
  179. end
  180.  
  181. function parseStr(str)
  182.     local lastIndex = 0
  183.     local index = -1
  184.     local cut = ""
  185.     local lineIndex = 0
  186.     while ( index ~= nil) do
  187.         local syntaxFlag = true;
  188.         lineIndex = lineIndex + 1
  189.         index = string.find(str,"\n", index + 1);
  190.         if( index ~= nil) then
  191.             cut = string.sub(str,lastIndex+1, index-1)
  192.         else
  193.             cut = string.sub(str,lastIndex+1, index);
  194.         end
  195.         -- comment check
  196.         repeat
  197.             lastIndex = index
  198.             local commentPos = string.find(cut,"#")
  199.             if commentPos ~= nil then
  200.                 cut = string.sub(cut, 1,commentPos-1)
  201.             end
  202.             -- preExp regMap
  203.             -- include check
  204.             if (string.match(cut,preMap["include"])) then
  205.                 local cStr = string.match(cut,preMap["include"])
  206.                 local inf = io.open(cStr,"r") -- the source_code filename
  207.                 local subStr = inf.read(inf,"*all")
  208.                 inf.close(inf)
  209.                 parseStr(subStr)
  210.                 break
  211.             end
  212.             -- func check
  213.             if (string.match(cut,genRegex("@func[ ]+([%w_]+)"))~= nil) then
  214.                 local cStr = string.match(cut,preMap["func"])
  215.                 funcMap[cStr] = ""
  216.                 funcKey = cStr
  217.                 break
  218.             elseif(string.match(cut,genRegex("@end"))) then
  219.                 funcKey = nil
  220.                 break
  221.             else
  222.                 if(funcKey ~= nil) then
  223.                     funcMap[funcKey] = funcMap[funcKey]..cut.."\n"
  224.                     break
  225.                 end
  226.             end
  227.             -- common regMap
  228.             for key, cb in pairs(regMap) do
  229.                 if (string.match(cut,key)~= nil) then
  230.                     local cStr = string.match(cut,key)
  231.                     syntaxFlag = cb(cStr)
  232.                     break
  233.                 end  
  234.             end
  235.         until true
  236.         if syntaxFlag ~= true then
  237.             print("Line "..lineIndex.." : "..cut.." is illegal syntax")
  238.         end
  239.     end
  240.  
  241.     -- out put final hexlist    
  242.     if(fPort ~= nil) then
  243.         fPort.writeIota(hexlist)
  244.         return
  245.     end
  246. end
  247.  
  248. function mainloop()
  249.     parseStr(codeStr)
  250.  end
  251.  mainloop()
  252.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement