446 lines
12 KiB
JavaScript
446 lines
12 KiB
JavaScript
function traduction(langage_source, langage_cible, remplacer) {
|
|
var nouveau = "", u, v, z, n, result, last = new Array(), last_indice = 0;
|
|
var ligne;
|
|
|
|
var autres = [
|
|
"Hasard", "#Ran", "rand",
|
|
"Partie_entière", "Int", "Int",
|
|
"Partie_décimale", "Frac", "fPart",
|
|
"Valeur_absolue", "Abs", "Abs"
|
|
];
|
|
|
|
$('#programm .action_buttons .pause').hide();
|
|
$('#programm .action_buttons .executer').show();
|
|
reset = 1;
|
|
process = 0;
|
|
execution_init();
|
|
$("#saisie").cleditor()[0].disable(false);
|
|
|
|
result = compilation(langage_source);
|
|
last[0] = "rien";
|
|
|
|
//affiche_resultats();
|
|
if(result == "failed")
|
|
return "failed";
|
|
|
|
for(v = 0; v < k; v++)
|
|
{
|
|
if(!((code[indices[v - 1]] == "alors" || code[indices[v - 1]] == "sinon") && (code[indices[v - 1] + 1] != "fin_si" && code[indices[v - 1] + 1] != "fin_si_ou_boucle")))
|
|
ligne = " ";
|
|
|
|
switch(code[indices[v]])
|
|
{
|
|
case "lire":
|
|
z = (langage_source == 2 ? 1 : 0);
|
|
|
|
if(langage_cible == 1)
|
|
ligne += "Lire " + code[indices[v] + 2 + z].toUpperCase();
|
|
if(langage_cible == 2)
|
|
ligne += "? → " + code[indices[v] + 2 + z].toUpperCase();
|
|
if(langage_cible == 3)
|
|
ligne += "Input " + code[indices[v] + 2 + z].toUpperCase();
|
|
break;
|
|
|
|
case "afficher":
|
|
if(langage_cible == 1)
|
|
ligne += "Afficher ";
|
|
if(langage_cible == 3)
|
|
ligne += "Disp ";
|
|
if(code[indices[v] + 1] == "variable")
|
|
ligne += code[indices[v] + 2].toUpperCase();
|
|
else if(code[indices[v] + 1] == "chaine" || code[indices[v] + 1] == "calcul")
|
|
ligne += code[indices[v] + 2];
|
|
if(langage_cible == 2)
|
|
ligne += "∠";
|
|
break;
|
|
|
|
case "si":
|
|
if(langage_cible == 1)
|
|
{
|
|
ligne += "Si";
|
|
last[last_indice] = "Fin_si";
|
|
}
|
|
else
|
|
{
|
|
ligne += "If";
|
|
last[last_indice] = "IfEnd";
|
|
}
|
|
last_indice++;
|
|
|
|
for(z = indices[v] + 1; z < indices[v + 1]; z++)
|
|
{
|
|
if(code[z] == "variable")
|
|
ligne += " " + code[z + 1].toUpperCase();
|
|
if(code[z] == "signe" || code[z] == "calcul")
|
|
ligne += " " + code[z + 1];
|
|
if(code[z] == "et")
|
|
{
|
|
if(langage_cible == 1)
|
|
ligne += " ET";
|
|
else
|
|
ligne += " and";
|
|
}
|
|
if(code[z] == "ou")
|
|
{
|
|
if(langage_cible == 1)
|
|
ligne += " OU";
|
|
else
|
|
ligne += " or";
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case "alors":
|
|
if(langage_cible == 1)
|
|
ligne += "Alors ";
|
|
if(langage_cible == 2 || langage_cible == 3)
|
|
ligne += "Then ";
|
|
if(code[indices[v] + 1] != "fin_si" && code[indices[v] + 1] != "fin_si_ou_boucle" && code[indices[v] + 1] != "sinon")
|
|
continue;
|
|
break;
|
|
|
|
case "sinon":
|
|
if(langage_cible == 1)
|
|
ligne += "Sinon ";
|
|
if(langage_cible == 2 || langage_cible == 3)
|
|
ligne += "Else ";
|
|
if(code[indices[v] + 1] != "fin_si" && code[indices[v] + 1] != "fin_si_ou_boucle")
|
|
continue;
|
|
break;
|
|
|
|
case "fin_si":
|
|
if(langage_cible == 1)
|
|
ligne += "Fin_si";
|
|
if(langage_cible == 2)
|
|
ligne += "IfEnd";
|
|
if(langage_cible == 3)
|
|
ligne += "End";
|
|
break;
|
|
|
|
case "fin_si_ou_boucle":
|
|
if(last[last_indice - 1] != "rien")
|
|
{
|
|
ligne += last[last_indice - 1];
|
|
if(last_indice > 0) last_indice--;
|
|
}
|
|
else
|
|
{
|
|
alert("Erreur de syntaxe");
|
|
return;
|
|
}
|
|
break;
|
|
|
|
case "tant_que":
|
|
if(langage_cible == 1)
|
|
{
|
|
ligne += "Tant_que";
|
|
last[last_indice] = "Fin_Tant_que";
|
|
}
|
|
else
|
|
{
|
|
ligne += "While";
|
|
last[last_indice] = "WhileEnd";
|
|
}
|
|
|
|
last_indice++;
|
|
|
|
for(z = indices[v] + 1; z < indices[v + 1]; z++)
|
|
{
|
|
if(code[z] == "variable")
|
|
ligne += " " + code[z + 1].toUpperCase();
|
|
if(code[z] == "signe" || code[z] == "calcul")
|
|
ligne += " " + code[z + 1];
|
|
if(code[z] == "et")
|
|
{
|
|
if(langage_cible == 1)
|
|
ligne += " ET";
|
|
else
|
|
ligne += " and";
|
|
}
|
|
if(code[z] == "ou")
|
|
{
|
|
if(langage_cible == 1)
|
|
ligne += " OU";
|
|
else
|
|
ligne += " or";
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case "fin_tant_que":
|
|
if(langage_cible == 1)
|
|
ligne += "Fin_Tant_que";
|
|
if(langage_cible == 2)
|
|
ligne += "WhileEnd";
|
|
if(langage_cible == 3)
|
|
ligne += "End";
|
|
break;
|
|
|
|
case "pour":
|
|
var elem = new Array();
|
|
|
|
if(langage_source == 1)
|
|
{
|
|
elem[0] = code[indices[v] + 2].toUpperCase();
|
|
elem[1] = code[indices[v] + 5];
|
|
elem[2] = code[indices[v] + 8];
|
|
}
|
|
if(langage_source == 2)
|
|
{
|
|
elem[0] = code[indices[v] + 5].toUpperCase();
|
|
elem[1] = code[indices[v] + 2];
|
|
elem[2] = code[indices[v] + 8];
|
|
}
|
|
if(langage_source == 3)
|
|
{
|
|
elem[0] = code[indices[v] + 2].toUpperCase();
|
|
elem[1] = code[indices[v] + 5];
|
|
elem[2] = code[indices[v] + 8].substring(0, code[indices[v] + 8].length - 1);
|
|
}
|
|
|
|
if(langage_cible == 1)
|
|
{
|
|
ligne += "Pour " + elem[0] + " Allant_de " + elem[1] + " à " + elem[2];
|
|
last[last_indice] = "Fin_Pour";
|
|
}
|
|
if(langage_cible == 2)
|
|
{
|
|
ligne += "For " + elem[1] + " → " + elem[0] + " To " + elem[2];
|
|
last[last_indice] = "Next";
|
|
}
|
|
if(langage_cible == 3)
|
|
{
|
|
ligne += "For(" + elem[0] + ", " + elem[1] + ", " + elem[2] + ")" ;
|
|
last[last_indice] = "End";
|
|
}
|
|
|
|
last_indice++;
|
|
break;
|
|
|
|
case "fin_pour":
|
|
if(langage_cible == 1)
|
|
ligne += "Fin_Pour";
|
|
if(langage_cible == 2)
|
|
ligne += "Next";
|
|
if(langage_cible == 3)
|
|
ligne += "End";
|
|
break;
|
|
|
|
case "fin_programme":
|
|
if(langage_cible == 1)
|
|
ligne += "Fin_programme";
|
|
else
|
|
ligne += "Stop";
|
|
break;
|
|
|
|
case "variable":
|
|
if(langage_source == 2 && code[indices[v] + 2] == "∠")
|
|
{
|
|
if(langage_cible == 1)
|
|
ligne += "Afficher ";
|
|
if(langage_cible == 3)
|
|
ligne += "Disp ";
|
|
ligne += code[indices[v] + 1].toUpperCase();
|
|
if(langage_cible == 2)
|
|
ligne += "∠";
|
|
}
|
|
|
|
if(code[indices[v] + 2] == "prend_la_valeur")
|
|
ligne += code[indices[v] + 4].toUpperCase() + " → " + code[indices[v] + 1].toUpperCase();
|
|
|
|
if(code[indices[v] + 2] == "→" && langage_cible != 1)
|
|
ligne += code[indices[v] + 1].toUpperCase() + " → " + code[indices[v] + 4].toUpperCase();
|
|
|
|
if(code[indices[v] + 2] == "→" && langage_cible == 1)
|
|
ligne += code[indices[v] + 4].toUpperCase() + " prend_la_valeur " + code[indices[v] + 1].toUpperCase();
|
|
break;
|
|
|
|
case "calcul":
|
|
if(code[indices[v] + 2] == "→")
|
|
{
|
|
if(langage_cible == 1)
|
|
ligne += code[indices[v] + 4].toUpperCase() + " Prend_la_valeur " + code[indices[v] + 1].toUpperCase();
|
|
else
|
|
ligne += code[indices[v] + 1].toUpperCase() + " → " + code[indices[v] + 4].toUpperCase();
|
|
}
|
|
else if(code[indices[v] + 2] == "∠")
|
|
{
|
|
if(langage_cible == 1)
|
|
ligne += "Afficher " + code[indices[v] + 1].toUpperCase();
|
|
else
|
|
ligne += "Disp " + code[indices[v] + 1].toUpperCase();
|
|
}
|
|
break;
|
|
|
|
case "chaine":
|
|
if(langage_source == 2 && code[indices[v] + 2] == "∠")
|
|
{
|
|
if(langage_cible == 1)
|
|
ligne += "Afficher ";
|
|
if(langage_cible == 3)
|
|
ligne += "Disp ";
|
|
ligne += code[indices[v] + 1];
|
|
if(langage_cible == 2)
|
|
ligne += "∠";
|
|
}
|
|
break;
|
|
}
|
|
|
|
while(ligne.indexOf("#ran") != -1)
|
|
ligne = ligne.replace("#ran","#Ran");
|
|
|
|
while(ligne.indexOf("#RAN") != -1)
|
|
ligne = ligne.replace("#RAN","#Ran");
|
|
|
|
while(ligne.indexOf("hasard") != -1)
|
|
ligne = ligne.replace("hasard","Hasard");
|
|
|
|
while(ligne.indexOf("HASARD") != -1)
|
|
ligne = ligne.replace("HASARD","Hasard");
|
|
|
|
while(ligne.indexOf("Rand") != -1)
|
|
ligne = ligne.replace("Rand","rand");
|
|
|
|
while(ligne.indexOf("RAND") != -1)
|
|
ligne = ligne.replace("RAND","rand");
|
|
|
|
while(ligne.indexOf("Int") != -1)
|
|
ligne = ligne.replace("Int","INT");
|
|
|
|
while(ligne.indexOf("int") != -1)
|
|
ligne = ligne.replace("int","INT");
|
|
|
|
while(ligne.indexOf("Frac") != -1)
|
|
ligne = ligne.replace("Frac","FRAC");
|
|
|
|
while(ligne.indexOf("frac") != -1)
|
|
ligne = ligne.replace("frac","FRAC");
|
|
|
|
while(ligne.indexOf("Abs") != -1)
|
|
ligne = ligne.replace("Abs","ABS");
|
|
|
|
while(ligne.indexOf("abs") != -1)
|
|
ligne = ligne.replace("abs","ABS");
|
|
|
|
while(ligne.indexOf("fPart") != -1)
|
|
ligne = ligne.replace("fPart","FPART");
|
|
|
|
while(ligne.indexOf("fpart") != -1)
|
|
ligne = ligne.replace("fpart","FPART");
|
|
|
|
while(ligne.indexOf("Partie_entière") != -1)
|
|
ligne = ligne.replace("Partie_entière","PARTIE_ENTIÈRE");
|
|
|
|
while(ligne.indexOf("partie_entière") != -1)
|
|
ligne = ligne.replace("partie_entière","PARTIE_ENTIÈRE");
|
|
|
|
while(ligne.indexOf("Partie_décimale") != -1)
|
|
ligne = ligne.replace("Partie_décimale","PARTIE_DÉCIMALE");
|
|
|
|
while(ligne.indexOf("partie_décimale") != -1)
|
|
ligne = ligne.replace("partie_décimale","PARTIE_DÉCIMALE");
|
|
|
|
while(ligne.indexOf("Valeur_absolue") != -1)
|
|
ligne = ligne.replace("Valeur_absolue","VALEUR_ABSOLUE");
|
|
|
|
while(ligne.indexOf("valeur_absolue") != -1)
|
|
ligne = ligne.replace("valeur_absolue","VALEUR_ABSOLUE");
|
|
|
|
n = (langage_source == 1 ? 0 : 1);
|
|
|
|
for(z = 0; z < 4; z++)
|
|
{
|
|
var ajout, current, position1, position2, expression, rebours, parentheses;
|
|
var tab = ["Hasard", "rand"];
|
|
|
|
while(ligne.toLowerCase().indexOf(autres[3 * z + langage_source - 1].toLowerCase()) != -1)
|
|
{
|
|
ajout = autres[3 * z + langage_cible - 1];
|
|
if(ajout != "Int" && ajout != "fPart")
|
|
ajout += " ";
|
|
|
|
current = ligne.indexOf(autres[3 * z + langage_source - 1]);
|
|
if(langage_source == 2)
|
|
{
|
|
if(autres[3 * z + langage_source - 1] == "#Ran" && ligne.substring(current - 1, current) == ")" && ligne.indexOf("(") < current && ligne.indexOf("(") != -1)
|
|
{
|
|
position2 = current - 1;
|
|
|
|
parentheses = 0;
|
|
for(rebours = 0; parentheses >= 0; rebours++)
|
|
{
|
|
if(ligne.substring(current - 2 - rebours, current - 1 - rebours) == ")")
|
|
parentheses++;
|
|
if(ligne.substring(current - 2 - rebours, current - 1 - rebours) == "(")
|
|
parentheses--;
|
|
}
|
|
position1 = current - 1 - rebours;
|
|
|
|
if(position1 != -1 && position2 != -1)
|
|
{
|
|
expression = ligne.substring(position1, position2 + 1);
|
|
ligne = ligne.substring(0, position1) + ligne.substring(position2 + 1, current + 4).toUpperCase() + expression + ligne.substring(current + 4, ligne.length);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ligne = ligne.replace(autres[3 * z + langage_source - 1].toUpperCase(), ajout);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(langage_source != 2 && langage_cible == 2)
|
|
{
|
|
if(autres[3 * z + langage_source - 1] == tab[n] && ligne.substring(current + tab[n].length, current + tab[n].length + 1) == "(" && ligne.lastIndexOf(")") > current + tab[n].length && ligne.lastIndexOf(")") != -1)
|
|
{
|
|
position1 = current + tab[n].length;
|
|
parentheses = 0;
|
|
for(rebours = 0; parentheses >= 0; rebours++)
|
|
{
|
|
if(ligne.substring(current + tab[n].length + 1 + rebours, current + tab[n].length + 2 + rebours) == "(")
|
|
parentheses++;
|
|
if(ligne.substring(current + tab[n].length + 1 + rebours, current + tab[n].length + 2 + rebours) == ")")
|
|
parentheses--;
|
|
}
|
|
position2 = current + tab[n].length + rebours;
|
|
|
|
if(position1 != -1 && position2 != -1)
|
|
{
|
|
expression = ligne.substring(position1, position2 + 1);
|
|
ligne = ligne.substring(0, position1 - tab[n].length) + expression + ligne.substring(position1 - tab[n].length, position1).toUpperCase() + ligne.substring(position2 + 1, ligne.length);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ligne = ligne.replace(autres[3 * z + langage_source - 1].toUpperCase(), ajout);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(langage_source != 2 && langage_cible != 2)
|
|
ligne = ligne.replace(autres[3 * z + langage_source - 1], autres[3 * z + langage_source - 1].toUpperCase());
|
|
|
|
ligne = ligne.replace(autres[3 * z + langage_source - 1].toUpperCase(), ajout);
|
|
}
|
|
}
|
|
|
|
if(ligne.search(" ") == 0)
|
|
ligne = ligne.substring(1, ligne.length);
|
|
|
|
nouveau += "<div>" + ligne + "</div>";
|
|
}
|
|
|
|
if(remplacer == 1)
|
|
remplace_saisie(nouveau);
|
|
else
|
|
return nouveau;
|
|
|
|
return "completed";
|
|
}
|
|
|
|
function remplace_saisie(nouvelle_saisie) {
|
|
document.getElementById("saisie").value = nouvelle_saisie;
|
|
$("#saisie").cleditor()[0].updateFrame().focus();
|
|
} |