Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- * a mineCraft HexCasting Mod Compiler, can compile the hCode in game and output to the focus
- * need mod : Ducky peripheral
- * author : Lanzr
- ]]
- 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 codeStr = 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
- local funcKey = nil
- local funcMap = {}
- function genRegex(str) return "^[\t ]*"..str.."[\t ]*$" end
- local preMap = {
- ["include"] = genRegex("@include[ ]+([%w_]+)"),
- ["func"] = genRegex("@func[ ]+([%w_]+)"),
- }
- local hexMap = { -- add pattern table to this table
- ["me"] = {["startDir"]="EAST",["angles"]="qaq"},
- ["getEntityHight"] = {["startDir"]="EAST",["angles"]="awq"},
- ["{"] = {["startDir"]="WEST",["angles"]="qqq"},
- ["}"] = {["startDir"]="EAST",["angles"]="eee"},
- ["pos"] = {["startDir"]="EAST",["angles"]="aa"},
- ["sight"] = {["startDir"]="EAST",["angles"]="wa"},
- ["unpackList"] = {["startDir"]="NORTH_WEST",["angles"]="qwaeawq"},
- ["packList"] = {["startDir"]="SOUTH_WEST",["angles"]="ewdqdwe"},
- ["packVec"] = {["startDir"]="SOUTH_WEST",["angles"]="eqqqqq"},
- ["+"] = {["startDir"]="NORTH_EAST",["angles"]="waaw"},
- ["-"] = {["startDir"]="NORTH_WEST",["angles"]="wddw"},
- ["*"] = {["startDir"]="SOUTH_EAST",["angles"]="waqaw"},
- ["/"] = {["startDir"]="SOUTH_EAST",["angles"]="wdedw"},
- ["isEqual"] = {["startDir"]="SOUTH_EAST",["angles"]="ad"},
- ["dig"] = {["startDir"]="EAST",["angles"]="qaqqqqq"},
- ["rayCast_getEntity"] = {["startDir"]="EAST",["angles"]="weaqa"},
- ["rayCast_getBlock"] = {["startDir"]="EAST",["angles"]="wqaawdd"},
- ["rayCast_getBlockRule"] = {["startDir"]="EAST",["angles"]="weddwaa"},
- ["makeCypher"] = {["startDir"]="EAST",["angles"]="waqqqqq"},
- ["makeTrinket"] = {["startDir"]="EAST",["angles"]="wwaqqqqqeaqeaeqqqeaeq"},
- ["makeArtifact"] = {["startDir"]="EAST",["angles"]="wwaqqqqqeawqwqwqwqwqwwqqeadaeqqeqqeadaeqq"},
- ["clean"] = {["startDir"]="EAST",["angles"]="qdqawwaww"},
- ["blink"] = {["startDir"]="SOUTH_WEST",["angles"]="awqqqwaq"},
- ["listSelect"] = {["startDir"]="NORTH_WEST",["angles"]="deeed"},
- ["areaGetItems"] = {["startDir"]="SOUTH_EAST",["angles"]="qqqqqwdeddww"},
- ["swap"] = {["startDir"]="NORTH_EAST",["angles"]="aawdd"},
- ["dup"] = {["startDir"]="EAST",["angles"]="aadaa"},
- ["mod"] = {["startDir"]="NORTH_EAST",["angles"]="wqaqw"},
- ["return"] = {["startDir"]="SOUTH_WEST",["angles"]="aqdee"},
- ["true2or3"] = {["startDir"]="SOUTH_EAST",["angles"]="awdd"},
- ["any2bool"] = {["startDir"]="EAST",["angles"]="aw"},
- ["run"] = {["startDir"]="SOUTH_EAST",["angles"]="deaqq"},
- ["debug"] = {["startDir"]="NORTH_EAST",["angles"]="de"},
- ["readFhand"] = {["startDir"]="EAST",["angles"]="aqqqqq"},
- ["canOffhandBeRead"] = {["startDir"]="EAST",["angles"]="aqqqqqe"},
- ["readFring"] = {["startDir"]="EAST",["angles"]="aqqqqqeawqwqwqwqwqw"},
- ["write2ring"] = {["startDir"]="EAST",["angles"]="deeeeeqdwewewewewew"},
- ["write2Neck"] = {["startDir"]="SOUTH_EAST",["angles"]="wadeeeeeq"},
- ["readFNeck"] = {["startDir"]="SOUTH_EAST",["angles"]="waaqqqqqe"},
- ["readFBlock"] = {["startDir"]="EAST",["angles"]="aqqqqqeawqwaw"},
- ["CanReadFBlock"] = {["startDir"]="EAST",["angles"]="aqqqqqeawqwawe"},
- ["write2mm"] = {["startDir"]="NORTH_WEST",["angles"]="eqqwawqaaw"},
- ["readFmm"] = {["startDir"]="NORTH_EAST",["angles"]="qeewdweddw"},
- ["door_open"] = {["startDir"]="WEST",["angles"]="qaqeede"},
- ["door_close"] = {["startDir"]="WEST",["angles"]="qqqwwqqqwqqawdedw"},
- ["charge"] = {["startDir"]="NORTH_WEST",["angles"]="qqqqqwaeaeaeaeaea"},
- ["runFEList"] = {["startDir"]="WEST",["angles"]="dadad"},
- ["light"] = {["startDir"]="WEST",["angles"]="qqd"},
- ["tp"] = {["startDir"]="EAST",["angles"]="wqeeweewwweedqqewqqqwwwqqaeewqq"},
- ["makeBattery"] = {["startDir"]="SOUTH_WEST",["angles"]="aaeaaqeqqawdeeaeddwqwqqeqqwqeaqaa"},
- ["grandGuard"] = {["startDir"]="NORTH_WEST",["angles"]="eeweeewaqqqeqqqqe"},
- ["soulCast"] = {["startDir"]="NORTH_WEST",["angles"]="eqqwqwadaadqeedqwdeqqaawdwdaq"},
- ["soulPos"] = {["startDir"]="NORTH_EAST",["angles"]="qaqqqqaq"},
- ["soulSight"] = {["startDir"]="NORTH_EAST",["angles"]="waawaq"},
- ["makeGloopArtifact"] = {["startDir"]="EAST",["angles"]="wwaadaqwaweqqwaweewawqwwwwadeeeeeqww"},
- ["gloopDownload"] = {["startDir"]="NORTH_EAST",["angles"]="qedadewdde"},
- ["gloopIotaRead"] = {["startDir"]="NORTH_EAST",["angles"]="aqqqqqeqadaqw"},
- ["gloopIotaWrite"] = {["startDir"]="NORTH_EAST",["angles"]="deeeeeqqadaqw"},
- ["canGloopIotaRead"] = {["startDir"]="NORTH_EAST",["angles"]="wwqadaqwwaqqqqq"},
- }
- local NumMap = {
- [0] = {["startDir"]="SOUTH_EAST",["angles"]="aqaa"},
- ["+1"] = (function () return "w" end),
- ["*2"] = (function () return "a" end)
- }
- local regMap = {
- [genRegex("([{}>%*%+-=</])")] = (function (cStr)
- table.insert(hexlist,hexMap[cStr])
- return true end),
- [genRegex("rm[ ]+(%d+)")] = (function (cStr)
- addRMPattern(cStr)
- return true end),
- [genRegex("(-?[%d]+)")] = (function (cStr)
- addNumPattern(tonumber(cStr))
- return true end),
- [genRegex("([%a_]+[%w_]*)")] = (function (cStr)
- local t = hexMap[cStr]
- if t == nil then
- return false
- end
- table.insert(hexlist,t)
- return true end),
- [genRegex("([%a_]+[%w_]*)%(%)")] = (function (cStr)
- parseStr(funcMap[cStr])
- return true end)
- }
- function addNumPattern(num)
- local numPattern = {}
- local opers = {}
- local size = 0
- local rem = num > 0 and num or -num
- local numStr = "aqaa"
- numPattern["startDir"] = "SOUTH_EAST"
- repeat
- if rem % 2 == 0 then
- table.insert(opers, "*2")
- rem = rem / 2
- else
- table.insert(opers,"+1")
- rem = rem -1
- end
- size = size +1
- until rem < 1
- for i = size, 1, -1 do
- numStr = numStr..NumMap[opers[i]]()
- end
- numPattern["angles"] = numStr
- if num < 0 then
- table.insert(hexlist,NumMap[0])
- table.insert(hexlist,numPattern)
- table.insert(hexlist,hexMap["-"])
- else
- table.insert(hexlist,numPattern)
- 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 parseStr(str)
- 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 + 1);
- if( index ~= nil) then
- cut = string.sub(str,lastIndex+1, index-1)
- else
- cut = string.sub(str,lastIndex+1, index);
- end
- -- comment check
- repeat
- lastIndex = index
- local commentPos = string.find(cut,"#")
- if commentPos ~= nil then
- cut = string.sub(cut, 1,commentPos-1)
- end
- -- preExp regMap
- -- include check
- if (string.match(cut,preMap["include"])) then
- local cStr = string.match(cut,preMap["include"])
- local inf = io.open(cStr,"r") -- the source_code filename
- local subStr = inf.read(inf,"*all")
- inf.close(inf)
- parseStr(subStr)
- break
- end
- -- func check
- if (string.match(cut,genRegex("@func[ ]+([%w_]+)"))~= nil) then
- local cStr = string.match(cut,preMap["func"])
- funcMap[cStr] = ""
- funcKey = cStr
- break
- elseif(string.match(cut,genRegex("@end"))) then
- funcKey = nil
- break
- else
- if(funcKey ~= nil) then
- funcMap[funcKey] = funcMap[funcKey]..cut.."\n"
- break
- end
- end
- -- common regMap
- 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
- until true
- if syntaxFlag ~= true then
- print("Line "..lineIndex.." : "..cut.." is illegal syntax")
- end
- end
- -- out put final hexlist
- if(fPort ~= nil) then
- fPort.writeIota(hexlist)
- return
- end
- end
- function mainloop()
- parseStr(codeStr)
- end
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement