Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- local DEBUG = false
- if #tArgs ~= 2 or
- type(tArgs[1]) ~= "string" or
- type(tArgs[2]) ~= "string" then
- if DEBUG then
- tArgs[1] = "wmain.lua"
- tArgs[2] = "mfile.lua"
- else
- error("Usage: build <in_filename> <out_filename>", 0)
- end
- end
- if not fs.exists("src/"..tArgs[1]) then
- error("File does not exist", 0)
- end
- local inFilename = tArgs[1]
- local outFilename = tArgs[2]
- local fOut = fs.open("out/"..outFilename, "w")
- -- Find path in table
- local getffTable = function(sInput, tFull)
- for sLine in string.gmatch(sInput, "(%w+)/") do
- tFull = tFull[sLine]
- end
- return tFull
- end
- -- load all files
- tPath = { }
- loadPath = function(path, cPath)
- local tDir = fs.list(path)
- for _, sName in pairs(tDir) do
- if fs.isDir(path..sName) then
- cPath[sName] = { }
- loadPath(path..sName.."/", cPath[sName])
- else
- local file = fs.open(path..sName, "r")
- cPath[sName] = file.readAll()
- file.close()
- end
- end
- end
- loadPath("src/", tPath)
- loadPath = nil
- -- split parameter for macros
- local splitParam = function(sParam)
- local tParam = { }
- for sp in sParam:gmatch("%s*,?%s*([^,]*)%s*") do
- table.insert(tParam, sp)
- end
- return table.unpack(tParam)
- end
- -- convert raw bytecode to string bytecode
- rawtostr = function(bytecode)
- local sReturn = ""
- for ch in bytecode:gmatch(".") do
- sReturn = sReturn.."\\"..string.byte(ch)
- end
- return sReturn
- end
- -- load macros and convert to bytecode
- doFile = function(path, sFilename, sMode, tParam)
- local sFile = ""
- local sFiledef = ""
- local nLine = 0
- for sLine in path[sFilename]:gmatch("([^\n]*)[\n]*") do
- nLine = nLine + 1
- local sMacro, sParam = string.match(sLine, "^#(.*)[(](.*)[)]")
- if sMacro then
- local bMacro = false
- -- File definitions
- if sMacro == "LIB" or
- sMacro == "SOURCE" then
- sFiledef = sMacro
- bMacro = true
- else
- -- Public macro definitions
- if sMacro == "GLIBFUNC" then
- bMacro = true
- if tParam.gfunct == nil then
- tParam.gfunct = { }
- end
- -- #GETLIBFUNCTION(module/abc, functionname)
- local sLib, sName = splitParam(sParam)
- if tParam.gfunct[sLib] == nil then
- tParam.gfunct[sLib] = { }
- end
- table.insert(tParam.gfunct[sLib], sName)
- elseif sMacro == "IMPORTLIB" then
- bMacro = true
- local iFile, svName = splitParam(sParam)
- local tFileDir = getffTable(iFile, path)
- local thisgfunct = tParam.gfunct[iFile]
- if tFileDir ~= path then
- iFile = iFile:match(".*[\\/](.*)")
- end
- if doFile(tFileDir, iFile, "DEFAULT", { pfunct = thisgfunct }) then
- return true
- end
- sFile = sFile.."local "..svName.." = \""
- sFile = sFile..tFileDir[iFile].."\"\n"
- end
- -- Source file definitions
- if sFiledef == "SOURCE" then
- if sMacro == "IMPORT" then
- bMacro = true
- local iFile, svName = splitParam(sParam)
- local tFileDir = getffTable(iFile, path)
- if tFileDir ~= path then
- iFile = iFile:match(".*[\\/](.*)")
- end
- if doFile(tFileDir, iFile, "DEFAULT", { }) then
- return true
- end
- sFile = sFile.."local "..svName.." = \""
- sFile = sFile..tFileDir[iFile].."\"\n"
- end
- end
- print(sFiledef.." : "..sMacro)
- -- Libary file definitions
- if sFiledef == "LIB" then
- print(sMacro.." : "..sMode)
- if sMacro == "BEGINFUNCTION" and sMode ~= "FUNCTION" then
- bMacro = true
- if tParam.pfunct == nil then
- tParam.bLoadAllFunction = true
- tParam.bLoadFunction = true
- end
- sMode = "FUNCTION"
- elseif sMacro == "ENDFUNCTION" then
- bMacro = true
- sMode = "DEFAULT"
- elseif sMacro == "FUNCTION" and sMode == "FUNCTION" then
- bMacro = true
- if not tParam.bLoadAllFunction then
- local bContains = false
- for _, sFunction in pairs(tParam.pfunct) do
- if sFunction == sParam then
- bContains = true
- break
- end
- end
- tParam.bLoadFunction = bContains
- end
- end
- end
- end
- if not bMacro then
- printError("Unable to resolve macro in file \""..sFilename.."\": \""..sMacro.."\" at line '"..nLine.."'")
- return true
- end
- elseif sMode == "DEFAULT" then
- sFile = sFile..sLine.."\n"
- elseif sMode == "FUNCTION" and tParam.bLoadFunction then
- sFile = sFile..sLine.."\n"
- end
- end
- local file = fs.open("_"..sFilename, "w")
- file.write(sFile)
- file.close()
- local fFile, sError = loadstring(sFile)
- if sError then
- printError("Error in file \""..sFilename.."\": "..sError)
- return true
- end
- --if sFilename == "wmain.lua" then
- -- path[sFilename] = (string.dump( fFile ))
- --else
- path[sFilename] = rawtostr(string.dump( fFile ))
- --end
- end
- if doFile(tPath, inFilename, "DEFAULT", { }) then
- return
- end
- doFile = nil
- rawtostr = nil
- fOut.write("loadstring(\"")
- fOut.write(tPath[ inFilename ])
- fOut.write("\")()")
- fOut.close()
- print(string.len(tPath[inFilename]).." bytes have been written to: out/"..outFilename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement