local input,output = ... if not input then error("No input provided") end output = output or input..".funny" local file = io.open(input) if not file then error("Failed to read: "..input) end local content = file:read("*a") local definitions = {} local def_pattern = "%$(%a-)={(.-)}" for identifier,block in content:gmatch(def_pattern) do definitions[identifier] = block end content = content:gsub(def_pattern,"") content = content:gsub("/*.-*/","") content = content:gsub("//.-[\n\r]","") content = content:gsub("//.-[\n\r]","") content = content:gsub("%$(%a+)",function(identifier) return definitions[identifier] end) content = content:gsub("%$(%a+)",function(identifier) return definitions[identifier] end) local lines = {} local index = 0 content=content:gsub("repeat(%d-){(.-)}",function(num,block) return string.rep(block,num) end) content=content:gsub("[^\n\r]+",function(line) index = index + 1 local result,line2 = line:match("^(.+)%s?:(%a+)") if line2 then lines[line2] = index end return result or line end) local subs = {} local index = 0 content=content:gsub("[^\n\r]+",function(line) index = index + 1 return line:gsub("%%(%a+)",function(name) return tostring(lines[name] - index) end) end) content = content:gsub("^[\n\r]+","") content = content:gsub("[\n\r%s]*(\n)[\n\r%s]*","\n") local result = io.open(output,"w") result:write(content) print(content) result:close() file:close()