Lanzr

hex.lua

Jul 4th, 2024 (edited)
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.17 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. require("hexMap")
  7. local completion = require "cc.shell.completion"
  8. local complete = completion.build(
  9.     completion.file
  10. )
  11. shell.setCompletionFunction("hex.lua", complete)
  12. local path = arg[1]
  13. if(path == nil) then
  14.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  15.     print("Usage: " .. programName .. " <path>")
  16.     return
  17. end
  18. if(fs.exists(path) == false) then
  19.     print("file "..path.." is not exist!")
  20.     return
  21. end
  22.  
  23. local inf = io.open(path,"r") -- the source_code filename
  24. local codeStr = inf.read(inf,"*all")
  25. inf.close(inf)
  26.  
  27. local fPort = peripheral.find("focal_port")
  28.  
  29. local lastIndex = 0
  30. local index = -1
  31. local leftBrackIndex = nil
  32.  
  33. local hexlist = {} -- the final table use to output
  34.  
  35. local funcKey = nil
  36. local funcMap = {}
  37.  
  38.  
  39. local NumMap = {
  40.     [0] = {["startDir"]="SOUTH_EAST",["angles"]="aqaa"},
  41.     ["+1"] = (function () return "w" end),
  42.     ["*2"] = (function () return "a" end)
  43. }
  44.  
  45. local regMap = {
  46.     [genRegex("([{}>%*%+-=</])")] = (function (cStr)
  47.         table.insert(hexlist,hexMap[cStr])
  48.     return true end),
  49.     [genRegex("rm[ ]+(%d+)")] = (function (cStr)
  50.         addRMPattern(cStr)
  51.     return true end),
  52.     [genRegex("(-?[%d]+)")] = (function (cStr)
  53.         addNumPattern(tonumber(cStr))
  54.     return true end),
  55.     [genRegex("([%a_]+[%w_]*)")] = (function (cStr)
  56.         local t = hexMap[cStr]
  57.         if t == nil then
  58.             return false
  59.         end
  60.         table.insert(hexlist,t)
  61.     return true end),
  62.     [genRegex("([%a_]+[%w_]*)%(%)")] = (function (cStr)
  63.         parseStr(funcMap[cStr])
  64.     return true end)
  65. }
  66.  
  67. function addNumPattern(num)
  68.     local numPattern = {}
  69.     local opers = {}
  70.     local size = 0
  71.     local rem = num > 0 and num or -num
  72.     local numStr = "aqaa"
  73.     numPattern["startDir"] = "SOUTH_EAST"
  74.     repeat
  75.         if rem % 2 == 0 then
  76.             table.insert(opers, "*2")
  77.             rem = rem / 2
  78.         else
  79.             table.insert(opers,"+1")
  80.             rem = rem -1
  81.         end
  82.         size = size +1
  83.     until  rem < 1  
  84.     for i = size, 1, -1 do
  85.         numStr = numStr..NumMap[opers[i]]()
  86.     end
  87.     numPattern["angles"] = numStr
  88.     if num < 0 then
  89.         table.insert(hexlist,NumMap[0])
  90.         table.insert(hexlist,numPattern)
  91.         table.insert(hexlist,hexMap["-"])
  92.     else
  93.         table.insert(hexlist,numPattern)
  94.     end
  95. end
  96.  
  97. function addRMPattern(rmPos)
  98.     local rmPattern = {}
  99.     local angleStr = ""
  100.     local pos = tonumber(rmPos)
  101.     rmPattern["startDir"] = "EAST"
  102.     if (pos > 1) then
  103.         for i=1,pos-1,1 do
  104.             angleStr = angleStr.."w"
  105.         end
  106.         angleStr = angleStr.."ea"
  107.     else
  108.         angleStr = "a"
  109.     end
  110.     rmPattern["angles"] = angleStr
  111.     table.insert(hexlist,rmPattern)
  112. end
  113.  
  114. function parseStr(str)
  115.     local lastIndex = 0
  116.     local index = -1
  117.     local cut = ""
  118.     local lineIndex = 0
  119.     while ( index ~= nil) do
  120.         local syntaxFlag = true;
  121.         lineIndex = lineIndex + 1
  122.         index = string.find(str,"\n", index + 1);
  123.         if( index ~= nil) then
  124.             cut = string.sub(str,lastIndex+1, index-1)
  125.         else
  126.             cut = string.sub(str,lastIndex+1, index);
  127.         end
  128.         -- comment check
  129.         repeat
  130.             lastIndex = index
  131.             local commentPos = string.find(cut,"#")
  132.             if commentPos ~= nil then
  133.                 cut = string.sub(cut, 1,commentPos-1)
  134.             end
  135.             -- preExp regMap
  136.             -- include check
  137.             if (string.match(cut,preMap["include"])) then
  138.                 local cStr = string.match(cut,preMap["include"])
  139.                 local inf = io.open(cStr,"r") -- the source_code filename
  140.                 local subStr = inf.read(inf,"*all")
  141.                 inf.close(inf)
  142.                 parseStr(subStr)
  143.                 break
  144.             end
  145.             -- func check
  146.             if (string.match(cut,genRegex("@func[ ]+([%w_]+)"))~= nil) then
  147.                 local cStr = string.match(cut,preMap["func"])
  148.                 funcMap[cStr] = ""
  149.                 funcKey = cStr
  150.                 break
  151.             elseif(string.match(cut,genRegex("@end"))) then
  152.                 funcKey = nil
  153.                 break
  154.             else
  155.                 if(funcKey ~= nil) then
  156.                     funcMap[funcKey] = funcMap[funcKey]..cut.."\n"
  157.                     break
  158.                 end
  159.             end
  160.             -- common regMap
  161.             for key, cb in pairs(regMap) do
  162.                 if (string.match(cut,key)~= nil) then
  163.                     local cStr = string.match(cut,key)
  164.                     syntaxFlag = cb(cStr)
  165.                     break
  166.                 end  
  167.             end
  168.         until true
  169.         if syntaxFlag ~= true then
  170.             print("Line "..lineIndex.." : "..cut.." is illegal syntax")
  171.         end
  172.     end
  173.  
  174.     -- out put final hexlist    
  175.     if(fPort ~= nil) then
  176.         fPort.writeIota(hexlist)
  177.         return
  178.     end
  179. end
  180.  
  181. function mainloop()
  182.     parseStr(codeStr)
  183. end
  184.  
  185. mainloop()
  186.  
Advertisement
Add Comment
Please, Sign In to add comment