Module:Pseudo code
Apparence
La documentation de ce module est générée par le modèle {{Documentation module}}.
Les éditeurs peuvent travailler dans le bac à sable (créer).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
-- Chantier de mon module
local p = {}
local function is_in(t,e)
for k = 1, #t do
if (t[k] == e) then
return true
end
end
return false
end
local t_fin = {"fin", "end", "Fin", "End"}
local t_renvoyer = {"renvoyer", "retourner", "return", "Return", "Renvoyer", "Retourner"}
local t_fonctions = {"function", "fonction", "Fonction", "Function"}
local t_si = {"if", "si", "If", "Si"}
local t_alors = {"alors", "then", "Alors", "Then"}
local t_sinon = {"sinon", "Sinon", "else", "Else"}
local t_et = {"et", "Et", "and", "And"}
local t_ou = {"ou", "Ou", "or", "Or"}
local t_non = {"non", "Non", "not", "Not"}
local t_tant = {"tant", "while"}
local t_pour = {"pour", "Pour", "for", "For"}
local t_operateurs = {"mod", "modulo","=","≠","+","-","−",":=","%","/",";",":",">","<","->","<-","←","→","...","#"}
local t_keywords = {"Répéter", "répéter"}
local function word_to_colored(word)
if (is_in(t_fin, word)) then
return "<span style=\"color:blue\">'''fin'''</span>"
elseif (is_in(t_renvoyer, word)) then
return "<span style=\"color:green\">'''renvoyer'''</span>"
elseif (tonumber(word)) then
return '<span style="color:blue">'..word..'</span>'
elseif (is_in(t_fonctions, word)) then
return "<span style=\"color:blue\">'''fonction'''</span>"
elseif (is_in(t_si, word)) then
return "<span style=\"color:darkviolet\">'''si'''</span>"
elseif (is_in(t_alors, word)) then
return "<span style=\"color:darkviolet\">'''alors'''</span>"
elseif (is_in(t_sinon, word)) then
return "<span style=\"color:darkviolet\">'''sinon'''</span>"
elseif (is_in(t_et, word)) then
return "<span style=\"color:violet\">'''et'''</span>"
elseif (is_in(t_ou, word)) then
return "<span style=\"color:violet\">'''ou'''</span>"
elseif (is_in(t_non, word)) then
return "<span style=\"color:violet\">'''non'''</span>"
elseif (is_in(t_tant, word)) then
return "<span style=\"color:darkviolet\">'''tant'''</span>"
elseif (is_in(t_pour, word)) then
return "<span style=\"color:darkviolet\">'''pour'''</span>"
elseif (is_in(t_operateurs, word)) then
return "<span style=\"color:green\">'''"..word.."'''</span>"
elseif (word == "que") then
return "<span style=\"color:darkviolet\">'''que'''</span>"
elseif (is_in(t_keywords, word)) then
return "<span style=\"color:darkviolet\">'''"..word.."'''</span>"
else
return word
end
end
local function convert_all(str, pattern, replace)
local i = 0
local modified = ""
while (i ~= nil and i <= #str) do
d, f = string.find(str, pattern, i)
if (d ~= nil) then
modified = modified..string.sub(str, i, d-1)..replace
i = f + 1
else
modified = modified..string.sub(str, i, #str)
i = nil
end
end
return modified
end
local function coloration(text)
word = ""
final = ""
local separators = {" ", ",", ";", ":", "(", ")","?","!",".","<",">","/","=","+","-","[","]","{","}"," ","\n"}
for k = 1, #text do
c = string.sub(text, k, k)
if (is_in(separators, c)) then
final = final..word_to_colored(word)..word_to_colored(c)
word = ""
else
word = word..c
end
end
final = final..word_to_colored(word)
return final
end
local function convert_code(code,tabs)
local colored = coloration(code)
local translated = convert_all(convert_all(colored, "\n", "<br>"),tabs, " ")
return translated
end
function p.code(frame)
local pframe = frame:getParent()
local code = pframe.args["code"]
local entree = pframe.args["entree"]
local sortie = pframe.args["sortie"]
local titre = pframe.args["titre"]
local n_tabs = pframe.args["tabs"] or 4
tabs = ""
for k = 1, n_tabs do
tabs = tabs.." "
end
if (titre ~= nil) then
text = "{| class=\"wikitable alternance\"\n|+"..titre.."\n|-\n|'''Entrée = '''"..entree.."<br>'''Sortie = '''"..sortie.."\n|-\n|width=90%|"..convert_code(code,tabs).."\n|}"
else
text = "{| class=\"wikitable alternance\"\n|-\n|'''Entrée = '''"..entree.."<br>'''Sortie = '''"..sortie.."\n|-\n|width=90%|"..convert_code(code,tabs).."\n|}"
end
return frame:preprocess(text)
end
return p