Modul:Vorlage:CompilerExplorerLink

Es existiert keine Dokumentation. Wenn du eine anlegen willst, solltest du keine „/Doku“-Unteseite benutzen, sondern LuaDokumentation.
- Diese Dokumentation wurde mithilfe von Modul:LuaDokumentation erstellt und befindet sich im Quelltext.
- Liste der Unterseiten
local CompilerExplorerLink = {}
-- Konvertiert Dezimal-Integer in Hexadezimal-Integer
local function hex(dec)
local hb = math.floor(dec/16)
local lb = dec%16
if lb > 9 then
lb = string.char(lb +55)
end
if hb > 9 then
hb = string.char(hb +55)
end
return hb .. lb
end
-- Wandelt einen String so um, dass er innerhalb einer URL verwendet werden kann
-- Dabei werden Leerzeichen durch + ersetzt, andere Sonderzeichen durch ihren Hex-Code und
-- einfachen Anführungszeichen(') und Ausrufezeichen(!) ein Ausrufungszeichen vorgestellt
local function transform(code)
local codestring = ""
if code == nil then
return "";
end
for i = 1, #code, 1 do
local zeichen = code:byte(i)
if (((zeichen >= 48) and (zeichen <=57)) or ((zeichen >= 65) and (zeichen <=90)) or ((zeichen >= 97) and (zeichen <=122))) then
codestring = codestring .. string.char(zeichen)
elseif zeichen == 39 then
codestring = codestring .. "!'"
elseif zeichen == 33 then
codestring = codestring .. "!!"
elseif zeichen == 95 then
codestring = codestring .. "_"
elseif zeichen == 32 then
codestring = codestring .. "+"
else
codestring = codestring .. "%" .. hex(zeichen)
end
end
return codestring
end
-- Wandelt eine 1 in eine 0 um und umgekehrt
-- Dies ist notwendig, da der Compiler Optionen eine 0 in der URL als true und eine 1 als false interpretiert
local function invert(a)
if a == true then
return "0"
elseif a == false then
return "1"
else
return nil
end
end
-- Erzeugt eine URL zum Compiler Explorer
-- für jede "Datei" wird ein Eintrag in den Arrays "code" (der Inhalt der Datei)
-- und "filename" (der Name der Datei) benötigt.
-- Optionen können weggelassen werden. in diesem fall werden Standardwerte benutzt.
local function convert(source)
-- Die Standardwerte der Optionen werden festgelegt
local options = ""
local compiler = "cg122"
local execArgsPanelShown = "1"
local execStdin = ""
local execArgs = "___"
local stdinPanelShown = "1"
local wrap = "1"
local compilerOutShown = "0"
local compilationPanelShown = "0"
local isCMakeProject = "1"
local cmakeArgs = ""
local customOutputFilename = ""
local libs = {}
-- falls vorhanden, werden die Optionen und Bibliotheken ausgewertet
if (source.execArgsPanelShown == true) or (source.execArgsPanelShown == false) then
execArgsPanelShown = invert(source.execArgsPanelShown)
end
if (source.execCompilationPanelShown == true) or (source.execCompilationPanelShown == false) then
compilationPanelShown = invert(source.execCompilationPanelShown)
end
if source.execCompiler ~= nil then
compiler = source.execCompiler
end
if (source.execCompilerOutputShown == true) or (source.execCompilerOutputShown == false) then
compilerOutShown = invert(source.execCompilerOutputShown)
end
if source.execArgs ~= nil then
execArgs = source.execArgs
end
if source.execStdin ~= nil then
execStdin = source.execStdin
end
if source.execCompilerOptions ~= nil then
options = source.execCompilerOptions
end
if (source.execStdinPanelShown == true) or (source.execStdinPanelShown == false) then
stdinPanelShown = invert(source.execStdinPanelShown)
end
if (source.execLineWrap == true) or (source.execLineWrap == false) then
wrap = invert(source.execLineWrap)
end
if (source.isCMakeProject == true) or (source.isCMakeProject == false) then
isCMakeProject = invert(source.isCMakeProject)
end
if source.CMakeArgs ~= nil then
cmakeArgs = source.CMakeArgs
end
if source.CMakeCustomOutputFilename ~= nil then
customOutputFilename = source.CMakeCustomOutputFilename
end
if source.cs50 ~= nil then
libs[#libs+1] = "(name:cs50,ver:'".. source.cs50 .. "')"
end
if source.hedley ~= nil then
libs[#libs+1] = "(name:hedley,ver:".. source.hedley .. ")"
end
if source.libuv ~= nil then
libs[#libs+1] = "(name:libuv,ver:'".. source.libuv .. "')"
end
if source.lua ~= nil then
libs[#libs+1] = "(name:lua,ver:'".. source.lua .. "')"
end
if source.nsimd ~= nil then
libs[#libs+1] = "(name:nsimd,ver:'".. source.nsimd .. "')"
end
if source.openssl ~= nil then
libs[#libs+1] = "(name:openssl,ver:'".. source.openssl .. "')"
end
if source.python ~= nil then
libs[#libs+1] = "(name:python,ver:'".. source.python .. "')"
end
if source.simde ~= nil then
libs[#libs+1] = "(name:simde,ver:".. source.simde .. ")"
end
if source.curl ~= nil then
libs[#libs+1] = "(name:curl,ver:'".. source.curl .. "')"
end
if source.sqlite ~= nil then
libs[#libs+1] = "(name:sqlite,ver:'".. source.sqlite .. "')"
end
local name = {}
local code = {}
for key,value in pairs(source) do
if key:find('_') ~= nil then
key = string.gsub(key,'_','.')
name[#name+1] = key
code[#code+1] = value
end
end
-- falls kein Dateiname oder Quellcode angegeben wurde,
-- wird nur eine aus Schema und Host gebildete URL zurückgegeben
if (#name == 0) or (#code == 0) then
return "https://www.godbolt.org/"
end
-- Der Fragment-Teil der URL wird erzeugt
local url = "g:!((g:!("
-- da der Compiler Explorer verlangt, dass die Hauptdatei einen bestimmten Namen hat, wird dies hier sicher gestellt
if #name == 1 then
name[1] = "example.c"
isCMakeProject = "1"
end
for i = 1, #name, 1 do
url = url .. "(g:!((h:codeEditor,i:(filename:" .. name[i] .. ",fontScale:14,fontUsePx:'0',j:" .. i .. ",lang:___c,selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:'" .. transform(code[i]) .. "'),l:'5',n:'0',o:" .. name[i] .. ",t:'0')),header:(),k:" .. (100/(#name+1)) .. ",l:'4',m:100,n:'0',o:%27%27,s:0,t:'0'),"
end
url = url .. "(g:!((g:!((h:tree,i:(cmakeArgs:%27" .. cmakeArgs .. "%27,compilerLanguageId:___c,customOutputFilename:%27" .. customOutputFilename .. "%27,files:!(";
for i = 1, #name, 1 do
url = url .. "(editorId:" .. i .. ",fileId:" .. i .. ",filename:" .. name[i] .. ",g:%27%27,isIncluded:'0',isMainSource:'"
if isCMakeProject == "0" then
if name[i] == "CMakeLists.txt" then
url = url .. "0"
else
url = url .."1"
end
else
if name[i] == "example.c" then
url = url .. "0"
else
url = url .."1"
end
end
url = url .. "',isOpen:'0',langId:%27%27)"
if i < #name then
url = url .. ","
end
end
url = url .. "),isCMakeProject:'".. isCMakeProject .. "',j:1,newFileId:" .. (#name+1) .. "),l:'5',n:'0',o:'Tree+%231',t:'0')),header:(),l:'4',m:50,n:'0',o:%27%27,s:0,t:'0'),(g:!((h:executor,i:(argsPanelShown:'" .. execArgsPanelShown .. "',compilationPanelShown:'" .. compilationPanelShown .. "',compiler:" .. compiler .. ",compilerOutShown:'" .. compilerOutShown .. "',execArgs:"
if execArgs == "___" then
url = url .. execArgs
else
url = url .. "'" .. transform(execArgs) .. "'"
end
url = url .. ",execStdin:%27" .. transform(execStdin) .."%27,fontScale:14,fontUsePx:'0',j:1,lang:___c,libs:!("
if #libs > 0 then
for i = 1, #libs, 1 do
url = url .. libs[i]
if i < #libs then
url = url ..","
end
end
end
url = url .. "),options:%27" .. transform(options) .. "%27,source:'1',stdinPanelShown:'" .. stdinPanelShown .. "',tree:1,wrap:'" .. wrap .. "'),l:'5',n:'0',o:'Executor+(C,+Tree+%231)',t:'0')),header:(),k:50,l:'4',m:50,n:'0',o:%27%27,s:0,t:'0')),k:" .. (100/(#name+1)) .. ",l:'3',n:'0',o:%27%27,s:0,t:'0')),l:'2',n:'0',o:%27%27,t:'0')),version:4";
-- Die vollständieg URL (Schema + Host + Fragment) wird zurückgegeben
return "https://www.godbolt.org/#" .. url
end
-- Hauptprogramm: Erzeugt einen Link zum Compiler Explorer
-- Dateinamen und Quellcodes bilden ein Schlüssel-Wert-Paar,
-- wobei Punkte im Dateinamen durch '_' dargestellt werden müssen (so muss z.B. statt "main.c" "main_c" geschrieben werden)
-- Für den Executor Frame gibt es folgende Optionen, die ebenfalls Schlüssel-Wert-Paare bilden, aber angegeben werden müssen:
-- execCompilerOptions wird für die Compiler-Optionen verwendet (standardmäßig "")
-- execArgs enthält die Execution Arguments (standardmäßig ___)
-- execStdin enthät den Stdin (standardmäßig "")
-- execCompiler enthält den Namen des verwendeten Compilers (standardmäßig "cg122")
-- execArgsPanelShown gibt an, ob die Execution Arguments angezeigt werden (standardmäßig "0")
-- execStdinPanelShown gibt an, ob das Stdin angezeigt wird (standardmäßig "0")
-- execLineWrap gibt an, ob es einen automatischen Zeilenumbruch gibt (standardmäßig "0")
-- execCompilerOutputShown gibt an, ob der Compiler Output angezeigt wird (standardmäßig "1")
-- execCompilationPanelShown gibt an, ob der Compiler und die Compiler Options angezeigt werden (standardmäßig "1")
function CompilerExplorerLink.createLink(frame)
local source = frame:getParent().args
return tostring(convert(source))
end
return CompilerExplorerLink