RedTech0Inc

Lua FIle Linker

Aug 31st, 2026 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.03 KB | None | 0 0
  1. local LH_AUTO_SAVE_INTERVALS = 20
  2. local TABS = "    "
  3.  
  4. local function arrowRead(arrow,arrowColor,char)
  5.     local color = term.getTextColor()
  6.     if type(arrowColor) == "number" then
  7.         term.setTextColor(arrowColor)
  8.     end
  9.     term.write(arrow)
  10.     term.setTextColor(color)
  11.  
  12.     return read(char)
  13. end
  14.  
  15. local function setupFolder(folderPath)
  16.     if not fs.isDir(folderPath) then
  17.         if fs.exists(folderPath) then
  18.             error("please delete or rename '"..folderPath.."' for setup stage to work")
  19.         end
  20.         fs.makeDir(folderPath)
  21.     end
  22. end
  23.  
  24. local function warn(msg)
  25.     local color = term.getTextColor()
  26.     term.setTextColor(colors.orange)
  27.     print("warn: "..tostring(msg))
  28.     term.setTextColor(color)
  29. end
  30.  
  31. local function jsonStyleToString(value)
  32.     if type(value) == "nil" then return "null"
  33.     elseif type(value) == "number" or type(value) == "boolean" then return tostring(value)
  34.     end
  35.     return "\""..tostring(value).."\""
  36. end
  37.  
  38. local function getSimpleJSONStyleMap(map)
  39.     local output = "{\n"
  40.     local firstItr = true
  41.     for k,v in pairs(map) do
  42.         if firstItr then
  43.             output = output.."    \""..tostring(k).."\":"..jsonStyleToString(v)
  44.         else
  45.             output = output..",\n    \""..tostring(k).."\":"..jsonStyleToString(v)
  46.         end
  47.         firstItr = false
  48.     end
  49.     return output.."\n}"
  50. end
  51.  
  52. local function makeAssemblyEnv(filePath)
  53.     local env = {}
  54.  
  55.     if turtle then
  56.         env.DEVICE_PLATFORM = "turtle"
  57.     elseif pocket then
  58.         env.DEVICE_PLATFORM = "pocket"
  59.     else
  60.         env.DEVICE_PLATFORM = "computer"
  61.     end
  62.  
  63.     local versionNumber = tonumber((os.version() or ""):match("(%d+%.%d+)"))
  64.     env.COS_VERSION = versionNumber or -1
  65.     env.LINKER_VERSION = 1.2
  66.  
  67.     local file = io.open(filePath,"w")
  68.     file:write(getSimpleJSONStyleMap(env))
  69.     file:close()
  70.  
  71.     return env
  72. end
  73.  
  74. local function getEnvTable(filePath)
  75.     if not fs.exists(filePath) then makeAssemblyEnv(filePath) end
  76.     local file = io.open(filePath,"r")
  77.     local table = textutils.unserialiseJSON(file:read("a"))
  78.     file:close()
  79.  
  80.     if not table then
  81.         table = makeAssemblyEnv(filePath) --generate the default one to replace the brocken one
  82.     end
  83.  
  84.     table.BUILD_TIME = nil --ensures that we can not have a second BUILD_TIME
  85.     table.BUILD_EPOCH = nil --ensures that we can not have a second BUILD_EPOCH
  86.  
  87.     return table
  88. end
  89.  
  90. local function parseEnvValue(value)
  91.     if type(value) == "number" or type(value) == "boolean" then
  92.         return tostring(value)
  93.     else
  94.         return "\""..tostring(value).."\""
  95.     end
  96. end
  97.  
  98. --TODO: fixe the absence of a UI cause typing f***ing sucks
  99. local mountPath = fs.getDir(shell.getRunningProgram())
  100. if mountPath == "rom/" then mountPath = "" end
  101.  
  102. local outputFolderPath = fs.combine(mountPath,"build")
  103. local cacheFolderPath = fs.combine(mountPath,"cache")
  104. local logsFolderPath = fs.combine(mountPath,"logs")
  105. local envFilePath = fs.combine(mountPath,"env.json")
  106.  
  107. setupFolder(outputFolderPath)
  108. setupFolder(cacheFolderPath)
  109. setupFolder(logsFolderPath)
  110.  
  111. if not fs.exists(envFilePath) then makeAssemblyEnv(envFilePath) end
  112.  
  113. print()
  114. term.setTextColor(colors.orange)
  115. print("please enter the path from root of the file you would like to \"assemble\"")
  116. term.setTextColor(colors.white)
  117. local filePath = arrowRead("> ",colors.lightGray)
  118. if not fs.exists(filePath) then error("could not locate '"..filePath.."'") end
  119. print()
  120.  
  121. term.setTextColor(colors.blue)
  122. print("please enter the name of the finished file (without .lua)")
  123. term.setTextColor(colors.white)
  124. local outputName = arrowRead("> ",colors.lightGray)
  125. local outputPath = fs.combine(outputFolderPath,outputName..".lua")
  126.  
  127. local canProceed = true
  128. if fs.exists(outputPath) then
  129.     term.setTextColor(colors.yellow)
  130.     print("'"..outputName..".lua' already exists do you want to override it?")
  131.     term.setTextColor(colors.white)
  132.     local override = arrowRead("Y/N: ",colors.lightGray):lower()
  133.     canProceed = override:sub(1,1) == "y"
  134. end
  135.  
  136. local instructions = {}
  137. local lineLambdas = {}
  138. lineLambdas.general = {} --all lambdas that apply to all files
  139.  
  140. local layer = 0
  141. local lookupFileTree = {}
  142. local cacheFileTree = {}
  143.  
  144. lookupFileTree[filePath] = 0
  145.  
  146. local createTree
  147.  
  148. local function parseInstruction(line)
  149.     if line:sub(1, 3) ~= "--#" then
  150.         return nil
  151.     end
  152.  
  153.     local output = {}
  154.  
  155.     for value in line:sub(4):gmatch("%S+") do
  156.         if #output == 0 then table.insert(output, value:lower())
  157.         else table.insert(output, value)
  158.         end
  159.     end
  160.  
  161.     return output
  162. end
  163.  
  164. local function insertInLookupTree(reference,requiredLayer)
  165.     if lookupFileTree[reference] then
  166.         if lookupFileTree[reference] < requiredLayer then
  167.             lookupFileTree[reference] = requiredLayer
  168.         end
  169.         return false --file was already scanned once by dependency resolver
  170.     else
  171.         lookupFileTree[reference] = requiredLayer
  172.         return true --haven't found the file before (apply dependency resolver check)
  173.     end
  174. end
  175.  
  176. local function lookupToTopologicalTree()
  177.     local output = {}
  178.     for filePath, layer in pairs(lookupFileTree) do
  179.         if not output[layer] then output[layer] = {} end
  180.         table.insert(output[layer],filePath)
  181.     end
  182.     return output
  183. end
  184.  
  185. local function insertInLineLambdas(path,func)
  186.     if type(lineLambdas[path]) ~= "table" then lineLambdas[path] = {} end
  187.     table.insert(lineLambdas[path],func)
  188. end
  189.  
  190. local function applyLineLambdas(path,line,lineCount)
  191.     local lambdas = lineLambdas[path] or {}
  192.     for i=1,#lambdas do
  193.         if type(lambdas[i]) == "function" then
  194.             line = tostring(lambdas[i](line,lineCount) or "")
  195.         end
  196.     end
  197.     for i=1,#lineLambdas.general do
  198.         if type(lineLambdas.general[i]) == "function" then
  199.             line = tostring(lineLambdas.general[i](line,lineCount) or "")
  200.         end
  201.     end
  202.     return line
  203. end
  204.  
  205. local function include(path,tokens)
  206.     layer = layer + 1
  207.     local fileMountPath = fs.getDir(path)
  208.  
  209.     local filePath = fs.combine(fileMountPath,tokens[2])
  210.     if fs.exists(filePath) then
  211.         if insertInLookupTree(filePath, layer) then createTree(filePath) end
  212.     else
  213.         error("could not find '"..filePath.."' included by '"..path.."'")
  214.     end
  215.  
  216.     layer = layer - 1
  217. end
  218.  
  219. local function define(path,tokens)
  220.     local keyword = tokens[2]:gsub("([%(%)%.%%%+%-%*%?%[%^%$])", "%%%1")
  221.     local pattern = table.concat(tokens," ",3) --can use concat cause all tokens are strings
  222.     insertInLineLambdas(path,
  223.     function (line)
  224.         line = line:gsub("%f[%w_]"..keyword.."%f[^%w_]", pattern)
  225.         return line
  226.     end)
  227. end
  228.  
  229. local function eval(expression)
  230.     local result, err = load("return " .. expression)
  231.     if result then return result() end
  232. end
  233.  
  234. local ifIndices, ifLayer = {}, 0
  235.  
  236. local function validateIf(blockName,lineCount)
  237.     if ifLayer < 1 or type(ifIndices[ifLayer]) ~= "table" then
  238.         warn(blockName.." in line "..lineCount.." missing if block")
  239.         return false
  240.     elseif #ifIndices[ifLayer] == 0 then
  241.         warn(blockName.." in line "..lineCount.." missing if block")
  242.         return false
  243.     end
  244.     return true
  245. end
  246.  
  247. local function startIf(path,tokens,lineCount) --can't call the method 'if' cause lua keyword
  248.     ifLayer = ifLayer + 1
  249.     if not ifIndices[ifLayer] then ifIndices[ifLayer] = {} end
  250.     local statement = table.concat(tokens," ",2) --concat the tokens e.g VAR1 == VAR2
  251.     statement = applyLineLambdas(path,statement,-1) --use unrealistic line count since this is not in a file
  252.  
  253.     ifIndices[ifLayer][1] = {eval(statement),lineCount}
  254. end
  255.  
  256. local function elseIf(path,tokens,lineCount)
  257.     if not validateIf("elseif",lineCount) then return end
  258.  
  259.     local statement = table.concat(tokens," ",2) --concat the tokens e.g VAR1 == VAR2
  260.     statement = applyLineLambdas(path,statement,-1) --use unrealistic line count since this is not in a file
  261.  
  262.     local layer = ifIndices[ifLayer]
  263.     layer[#layer][3] = lineCount-1
  264.     table.insert(layer,{eval(statement),lineCount})
  265. end
  266.  
  267. local function elsE(_,_,lineCount) --weird name but gets rid of lua key word name being used
  268.     if not validateIf("else",lineCount) then return end
  269.  
  270.     local layer = ifIndices[ifLayer]
  271.     layer[#layer][3] = lineCount-1
  272.     table.insert(layer,{true,lineCount}) --since this block is always true if none other's are
  273. end
  274.  
  275. local function endif(path,_,lineCount)
  276.     if not validateIf("endif",lineCount) then return end
  277.  
  278.     local lambdas = {}
  279.     local layer = ifIndices[ifLayer]
  280.     layer[#layer][3] = lineCount-1
  281.  
  282.     local subLayer, startIndex
  283.     local endIndex = 0
  284.     local foundTrue = false
  285.     for i=1,#layer do
  286.         subLayer = layer[i]
  287.         if (not subLayer[1]) or foundTrue then
  288.             if not startIndex then startIndex = subLayer[2] end
  289.             if endIndex < subLayer[3] then endIndex = subLayer[3] end
  290.         elseif i > 1 then
  291.             local lambdaStart = startIndex
  292.             local lambdaEnd = endIndex
  293.             table.insert(lambdas,
  294.             function (line,lineCount)
  295.                 if lambdaStart <= lineCount and lambdaEnd >= lineCount then
  296.                     return ""
  297.                 end
  298.                 return line
  299.             end)
  300.             startIndex = nil
  301.             endIndex = 0
  302.         end
  303.         if not foundTrue then foundTrue = subLayer[1] end
  304.     end
  305.     if startIndex and endIndex > 0 then
  306.         local lambdaStart = startIndex
  307.         local lambdaEnd = endIndex
  308.         table.insert(lambdas,
  309.         function (line,lineCount)
  310.             if lambdaStart <= lineCount and lambdaEnd >= lineCount then
  311.                 return ""
  312.             end
  313.             return line
  314.         end)
  315.     end
  316.  
  317.     for i=1,#lambdas do
  318.         insertInLineLambdas(path,lambdas[i])
  319.     end
  320.  
  321.     ifIndices[ifLayer] = {} --reset for next time using it (so no deprecated values are left behind)
  322.     ifLayer = ifLayer - 1
  323. end
  324.  
  325. --* DEFINE AVAILABLE INSTRUCTIONS HERE
  326. instructions.include = include
  327. instructions.define = define
  328. instructions["if"] = startIf --have to do this cause if is a keyword
  329. instructions["elseif"] = elseIf
  330. instructions["else"] = elsE
  331. instructions.endif = endif
  332.  
  333. createTree = function(filePath)
  334.  
  335.     local lineCount, execute = 0, true
  336.     local layer
  337.     for line in io.lines(filePath) do
  338.         lineCount = lineCount + 1
  339.         local data = parseInstruction(line)
  340.         if data then
  341.             if data[1] == "if" or data[1] == "elseif" or data[1] == "else" or data[1] == "endif" then
  342.                 instructions[data[1]](filePath, data, lineCount)
  343.  
  344.                 execute = true
  345.                 for i=1,#ifIndices do
  346.                     layer = ifIndices[i]
  347.                     if layer and type(layer[#layer]) == "table" then
  348.                         if not layer[#layer][1] then
  349.                             execute = false
  350.                             break
  351.                         end
  352.                     end
  353.                 end
  354.             elseif type(instructions[data[1]]) == "function" then
  355.                 if execute then
  356.                     instructions[data[1]](filePath, data, lineCount)
  357.                 end
  358.             end
  359.         end
  360.     end
  361. end
  362.  
  363. local inLongComment = false
  364.  
  365. local function removeComment(line)
  366.     local inString = false
  367.     local backslashes = 0
  368.     local output = ""
  369.     local i = 1
  370.  
  371.     while i <= #line do
  372.         local char = line:sub(i, i)
  373.         local nextChar = line:sub(i+1, i+1)
  374.  
  375.         if inLongComment then
  376.             if char == "]" and nextChar == "]" then
  377.                 inLongComment = false
  378.                 i = i + 2
  379.             else
  380.                 i = i + 1
  381.             end
  382.  
  383.         elseif not inString and char == "-" and nextChar == "-" then
  384.             if line:sub(i + 2, i + 3) == "[[" then
  385.                 inLongComment = true
  386.                 i = i + 4
  387.             else
  388.                 return output
  389.             end
  390.  
  391.         elseif char == '"' then
  392.             local escaped = backslashes % 2 == 1
  393.             backslashes = 0
  394.  
  395.             if not escaped then
  396.                 inString = not inString
  397.             end
  398.  
  399.             output = output .. char
  400.             i = i + 1
  401.  
  402.         elseif char == "\\" then
  403.             backslashes = backslashes + 1
  404.             output = output .. char
  405.             i = i + 1
  406.  
  407.         else
  408.             backslashes = 0
  409.             output = output .. char
  410.             i = i + 1
  411.         end
  412.     end
  413.  
  414.     return output
  415. end
  416.  
  417. local totalLines, sizeBefore = 0, 0
  418. local function compactFile(path,layer)
  419.     local fileName = fs.getName(path)
  420.     fileName = fileName:sub(1,#fileName-4) --chop of the .lua so we can put stuff like .lh
  421.  
  422.     local cachePath = fs.combine(cacheFolderPath,fileName..".lh");
  423.  
  424.     local file = io.open(cachePath,"w")
  425.     if not file then error("was unable to get file handle for '"..cachePath.."'") end
  426.  
  427.     local lambdas = lineLambdas[path] or {}
  428.  
  429.     local linesSinceLastSave, lineCount = 0, 0
  430.     sizeBefore = sizeBefore + fs.getSize(path)
  431.     for line in io.lines(path) do
  432.         lineCount = lineCount + 1
  433.  
  434.         line = line:gsub(TABS, "")
  435.         line = removeComment(line)
  436.         line = line:gsub("%s+$", "")
  437.         line = line:gsub("%s*([%+%-%*/%%%^~=<>])%s*", "%1")
  438.         --line = line:gsub("%s*%-%-.*$", "")
  439.  
  440.         for i=1,#lambdas do
  441.             if type(lambdas[i]) == "function" then
  442.                 line = tostring(lambdas[i](line, lineCount) or "")
  443.             end
  444.         end
  445.         for i=1,#lineLambdas.general do
  446.             if type(lineLambdas.general[i]) == "function" then
  447.                 line = tostring(lineLambdas.general[i](line) or "") --! if you need lineCount you have to add it but now i don't see the use
  448.             end
  449.         end
  450.  
  451.         if #line > 0 then
  452.             linesSinceLastSave = linesSinceLastSave+1
  453.             totalLines = totalLines + 1
  454.             print(totalLines..": "..line)
  455.             file:write(line,"\n")
  456.             if linesSinceLastSave >= LH_AUTO_SAVE_INTERVALS then
  457.                 file:flush()
  458.                 linesSinceLastSave = 0
  459.             end
  460.         end
  461.     end
  462.     file:close();
  463.  
  464.     if not cacheFileTree[layer] then cacheFileTree[layer] = {} end
  465.     table.insert(cacheFileTree[layer],fileName..".lh") --make a "lua header" file
  466. end
  467.  
  468. local startTime, startDateStr, endTime
  469. --main code
  470. if canProceed then
  471.     startTime = os.epoch("utc")
  472.     startDateStr = "\""..os.date("%c").."\""
  473.  
  474.     --empty the cache
  475.     fs.delete(cacheFolderPath)
  476.     fs.makeDir(cacheFolderPath)
  477.  
  478.     local env = getEnvTable(envFilePath)
  479.     for k,v in pairs(env) do
  480.         local keyword = k
  481.         local replacement = parseEnvValue(v)
  482.         table.insert(lineLambdas.general,
  483.         function(line)
  484.             line = line:gsub("%f[%w_]"..keyword.."%f[^%w_]", replacement)
  485.             return line
  486.         end)
  487.     end
  488.     table.insert(lineLambdas.general,
  489.     function(line)
  490.         line = line:gsub("%f[%w_]BUILD_TIME%f[^%w_]", startDateStr)
  491.         return line
  492.     end)
  493.     table.insert(lineLambdas.general,
  494.     function(line)
  495.         line = line:gsub("%f[%w_]BUILD_EPOCH%f[^%w_]", tostring(startTime))
  496.         return line
  497.     end)
  498.  
  499.     createTree(filePath)
  500.  
  501.     local fileTree = lookupToTopologicalTree()
  502.  
  503.     for i=#fileTree,0,-1 do
  504.         local filesToCompact = fileTree[i]
  505.         for j=1,#filesToCompact do
  506.             local path = filesToCompact[j]
  507.             compactFile(path,i)
  508.         end
  509.     end
  510.  
  511.     local outputFile = io.open(fs.combine(cacheFolderPath,cacheFileTree[0][1]),"r") --open the original file as a base
  512.     if not outputFile then error("was unable to open file reading handle for '"..outputPath.."'") end
  513.     local outputContent = outputFile:read("a")
  514.     outputFile:close()
  515.     outputFile = io.open(outputPath,"w")
  516.     if not outputFile then error("was unable to open file writing handle for '"..outputPath.."'") end
  517.  
  518.     local sizeBetween = 0
  519.     local path, file
  520.     for i=1,#cacheFileTree do
  521.  
  522.         local filesToResolve = cacheFileTree[i]
  523.         for j=1,#filesToResolve do
  524.             path = fs.combine(cacheFolderPath,filesToResolve[j])
  525.             sizeBetween = sizeBetween + fs.getSize(path)
  526.  
  527.             file = io.open(path,"r")
  528.  
  529.             if file then --tries to stick it together but if the handle doesn't work just skips it (and hopes it works)
  530.                 outputContent = "--== "..filesToResolve[j].."-begin ==--\n"..file:read("a").."--== "..filesToResolve[j].."-end ==--\n"..outputContent
  531.                 file:close()
  532.             else
  533.                 warn("couldn't link file '"..path.."' cause of file handle issues")
  534.             end
  535.         end
  536.     end
  537.     outputFile:write(outputContent)
  538.  
  539.     outputFile:close()
  540.  
  541.     local completionLog = io.open(fs.combine(logsFolderPath,outputName..".log"),"w")
  542.  
  543.     local function printToLog(text)
  544.         if completionLog then
  545.             if text then
  546.                 completionLog:write("\n",tostring(text))
  547.             else
  548.                 completionLog:write("\n")
  549.             end
  550.         end
  551.     end
  552.  
  553.     endTime = os.epoch("utc")
  554.     local takenTime = endTime-startTime
  555.  
  556.     --file info
  557.     term.setTextColor(colors.green)
  558.     print()
  559.     print("SUCCESS!")
  560.  
  561.     if completionLog then completionLog:write("<==FILE CONTENT INFO==>") end
  562.     printToLog("Characters: "..#outputContent)
  563.     printToLog("lines: "..totalLines)
  564.     outputContent = nil --gc can now get rid of it (frees up a lot of ram)
  565.  
  566.     printToLog()
  567.     printToLog("<=====ENVIRONMENT=====>")
  568.     printToLog("loaded from: "..envFilePath)
  569.     printToLog(getSimpleJSONStyleMap(env))
  570.  
  571.     --dependency lookup tree (just logged)
  572.     printToLog()
  573.     printToLog("<==DEPENDENCY LOOKUP==>")
  574.     printToLog("{")
  575.     for k,v in pairs(lookupFileTree) do --starts at 1 cause base file isn't inserted here
  576.         printToLog("    "..k.." = layer "..v)
  577.     end
  578.     printToLog("}")
  579.  
  580.     --dependency topological tree (just logged)
  581.     printToLog()
  582.     printToLog("<===DEPENDENCY TREE===>")
  583.     printToLog("{")
  584.     local tableStr
  585.     for i=0,#fileTree do --starts at 1 cause base file isn't inserted here
  586.         tableStr = textutils.serialize(fileTree[i],{compact=true})
  587.         printToLog("    layer "..i..":"..tableStr)
  588.     end
  589.     printToLog("}")
  590.  
  591.     --dependency tree (logged and printed)
  592.     printToLog()
  593.     printToLog("<=====HEADER TREE=====>")
  594.     printToLog("{")
  595.     local tableStr
  596.     for i=0,#cacheFileTree do
  597.         tableStr = textutils.serialize(cacheFileTree[i],{compact=true})
  598.         printToLog("    layer "..i..":"..tableStr)
  599.     end
  600.     printToLog("}")
  601.  
  602.     --file metrics
  603.     local sizeNow = fs.getSize(outputPath)
  604.     local reductionPercent = math.floor((1-(sizeNow/sizeBefore))*10000)
  605.     reductionPercent = reductionPercent/100 --to get back the last two decimal digits
  606.  
  607.     print()
  608.     printToLog()
  609.     term.setTextColor(colors.purple)
  610.     print("<==FILE SIZE METRICS==>")
  611.     printToLog("<==FILE SIZE METRICS==>")
  612.     term.setTextColor(colors.red)
  613.     print("file size before: "..sizeBefore.." bytes")
  614.     printToLog("file size before: "..sizeBefore.." bytes")
  615.     term.setTextColor(colors.yellow)
  616.     print("file size between: "..sizeBetween.." bytes")
  617.     printToLog("file size between: "..sizeBetween.." bytes")
  618.     term.setTextColor(colors.green)
  619.     print("file size after: "..sizeNow.." bytes")
  620.     printToLog("file size after: "..sizeNow.." bytes")
  621.     term.setTextColor(colors.blue)
  622.     print("reduced by: "..reductionPercent.."%")
  623.     printToLog("reduced by: "..reductionPercent.."%")
  624.  
  625.     printToLog()
  626.     printToLog("<======TIME INFO======>")
  627.     printToLog("finished in: "..takenTime.." ms")
  628.     printToLog("start-epoch: "..startTime)
  629.     printToLog("end  -epoch: "..endTime)
  630.  
  631.     term.setTextColor(colors.white)
  632.     print("finished in: "..takenTime.." ms")
  633.  
  634.     if completionLog then
  635.         completionLog:close()
  636.         term.setTextColor(colors.gray)
  637.         print("generated '"..outputName..".log' in '"..logsFolderPath.."'")
  638.     end
  639.     term.setTextColor(colors.white)
  640. end
Advertisement
Add Comment
Please, Sign In to add comment