2019-12-27 01:57:39 +01:00
var code = new Array ( ) ;
var indices = new Array ( ) ;
var erreur _syntaxe = new Array ( ) , nb _erreurs ;
var previsions = new Array ( ) ;
var type , indice _previsions ;
var j , k , for _ti , n ;
var conditions _ouvertes , conditions _fermees , alors , boucles _ouvertes , boucles _fermees , pour _ouverts , pour _fermes , ti _ouverts , ti _fermes ;
var bug1 , bug2 , bug3 , bug4 , bug5 ;
function saut _de _ligne ( texte ) {
while ( texte . indexOf ( "<hr>" ) != - 1 )
texte = texte . replace ( "<hr>" , "@" ) ;
while ( texte . indexOf ( " " ) != - 1 )
texte = texte . replace ( " " , "" ) ;
/ * w h i l e ( t e x t e . i n d e x O f ( " < s p a n c l a s s = \ " A p p l e - t a b - s p a n \ " s t y l e = \ " w h i t e - s p a c e : p r e \ " > < / s p a n > " ) ! = - 1 )
texte = texte . replace ( "<span class=\"Apple-tab-span\" style=\"white-space:pre\"> </span>" , "" ) ;
while ( texte . indexOf ( "<span style=\"font-size: 10pt;\">" ) != - 1 )
texte = texte . replace ( "<span style=\"font-size: 10pt;\">" , "" ) ;
while ( texte . indexOf ( "<font face=\"Arial, Verdana\" size=\"2\">" ) != - 1 )
texte = texte . replace ( "<font face=\"Arial, Verdana\" size=\"2\">" , "" ) ;
while ( texte . indexOf ( "<span style=\"font-family: 'Times New Roman'; font-size: medium;\">" ) != - 1 )
texte = texte . replace ( "<span style=\"font-family: 'Times New Roman'; font-size: medium;\">" , "" ) ;
while ( texte . indexOf ( "</font>" ) != - 1 )
texte = texte . replace ( "</font>" , "" ) ;
while ( texte . indexOf ( "</span>" ) != - 1 )
texte = texte . replace ( "</span>" , "" ) ; * /
var balises = [
"<font" , "</font" ,
"<p" , "</p" ,
"<a" , "</a" ,
"<i" , "</i" ,
"<u" , "</u"
] ;
for ( var k = 0 ; k < 10 ; k ++ )
{
while ( texte . indexOf ( balises [ k ] ) != - 1 )
{
var start , end ;
start = texte . indexOf ( balises [ k ] , 0 ) ;
end = texte . indexOf ( ">" , start ) ;
if ( start != - 1 && end != - 1 )
texte = texte . substring ( 0 , start ) + texte . substring ( end + 1 , texte . length ) ;
}
}
do
{
var pos = texte . indexOf ( '<span style="background-color:yellow;">' ) ;
if ( pos != - 1 )
{
var pos2 = texte . indexOf ( "</span>" , pos ) ;
texte = texte . substring ( 0 , pos2 ) + texte . substring ( pos2 + 7 , texte . length ) ;
texte = texte . replace ( '<span style="background-color:yellow;">' , "" ) ;
}
}
while ( pos != - 1 ) ;
while ( texte . indexOf ( " " ) != - 1 )
texte = texte . replace ( " " , " " ) ;
while ( texte . indexOf ( "<div" ) != - 1 )
{
if ( texte . indexOf ( "<div>" ) == 0 )
texte = texte . replace ( "<div>" , "" ) ;
else
{
while ( texte . indexOf ( "<div" ) != - 1 )
{
var start , end ;
start = texte . indexOf ( "<div" , 0 ) ;
end = texte . indexOf ( ">" , start ) ;
if ( start != - 1 && end != - 1 )
texte = texte . substring ( 0 , start ) + "@" + texte . substring ( end + 1 , texte . length ) ;
}
}
}
while ( texte . indexOf ( "</div>" ) != - 1 )
texte = texte . replace ( "</div>" , "" ) ;
while ( texte . indexOf ( "<span" ) != - 1 )
{
if ( texte . indexOf ( "<span>" ) == 0 )
texte = texte . replace ( "<span>" , "" ) ;
else
{
while ( texte . indexOf ( "<span" ) != - 1 )
{
var start , end ;
start = texte . indexOf ( "<span" , 0 ) ;
end = texte . indexOf ( ">" , start ) ;
if ( start != - 1 && end != - 1 )
texte = texte . substring ( 0 , start ) + "@" + texte . substring ( end + 1 , texte . length ) ;
}
}
}
while ( texte . indexOf ( "</span>" ) != - 1 )
texte = texte . replace ( "</span>" , "" ) ;
while ( texte . indexOf ( "<br" ) != - 1 )
{
var start , end ;
start = texte . indexOf ( "<br" , 0 ) ;
end = texte . indexOf ( ">" , start ) ;
if ( start != - 1 && end != - 1 )
texte = texte . substring ( 0 , start ) + "@" + texte . substring ( end + 1 , texte . length ) ;
}
while ( texte . substring ( texte . length - 1 , texte . length ) == " " || texte . substring ( texte . length - 1 , texte . length ) == "@" )
texte = texte . substring ( 0 , texte . length - 1 ) ;
return texte ;
}
function caracteres _speciaux ( texte , sens ) { // sens = 0 : § → §
var i ; // sens = 1 : § → §
var car = [
" " , "" ,
"­" , "" ,
"‎" , "" ,
"‏" , "" ,
"»" , "«" ,
"‹" , "‹ " ,
"›" , "› " ,
"“" , "“" ,
"”" , "”" ,
"„" , "„" ,
"'" , "'" ,
"‘" , "‘ " ,
"’" , "’ " ,
"‚" , "‚ " ,
"…" , "…" ,
"¡" , "¡" ,
"¿" , "¿" ,
"¨" , "¨" ,
"´" , "´ " ,
"ˆ" , "ˆ " ,
"¸" , "¸ " ,
"§" , "§" ,
"©" , "©" ,
"®" , "®" ,
"™" , "™" ,
"&" , "&" ,
"←" , "←" ,
"↑" , "↑" ,
"→" , "→" ,
"↓" , "↓" ,
"↔" , "↔" ,
"á" , "á" ,
"Á" , "Á" ,
"â" , "â" ,
"Â" , "Â" ,
"à" , "à" ,
"À" , "À" ,
"ã" , "ã" ,
"ä" , "ä" ,
"æ" , "æ" ,
"ç" , "ç" ,
"Ç" , "Ç" ,
"é" , "é" ,
"É" , "É" ,
"ê" , "ê" ,
"Ê" , "Ê" ,
"È" , "È" ,
"ë" , "ë" ,
"Ë" , "Ë" ,
"î" , "î" ,
"Î" , "Î" ,
"ï" , "ï" ,
"Ï" , "Ï" ,
"ñ" , "ñ" ,
"Ñ" , "Ñ" ,
"ô" , "ô" ,
"Ô" , "Ô" ,
"ø" , "ø" ,
"Ø" , "Ø" ,
"ö" , "ö" ,
"Ö" , "Ö" ,
"œ" , "œ" ,
"ß" , "ß" ,
"ð" , "ð" ,
"ú" , "ú" ,
"Ú" , "Ú" ,
"û" , "û" ,
"ù" , "ù" ,
"ü" , "ü" ,
"Ü" , "Ü" ,
"¤" , "¤" ,
"€" , "€" ,
"¢" , "¢" ,
"£" , "£" ,
"¥" , "¥" ,
"ƒ" , "ƒ" ,
"α" , "α " ,
"Α" , "Α " ,
"β" , "β" ,
"Β" , "Β " ,
"γ" , "γ " ,
"Γ" , "Γ" ,
"δ" , "δ" ,
"Δ" , "Δ" ,
"ε" , "ε" ,
"Ε" , "Ε " ,
"ζ" , "ζ" ,
"Ζ" , "Ζ " ,
"η" , "η" ,
"Η" , "Η " ,
"θ" , "θ" ,
"Θ" , "Θ" ,
"ι" , "ι " ,
"Ι" , "Ι " ,
"κ" , "κ" ,
"Κ" , "Κ " ,
"λ" , "λ" ,
"Λ" , "Λ" ,
"μ" , "μ" ,
"Μ" , "Μ " ,
"ν" , "ν " ,
"Ν" , "Ν " ,
"ξ" , "ξ" ,
"Ξ" , "Ξ" ,
"ο" , "ο " ,
"Ο" , "Ο " ,
"π" , "π" ,
"Π" , "Π" ,
"ρ" , "ρ " ,
"Ρ" , "Ρ " ,
"σ" , "σ " ,
"ς" , "ς" ,
"Σ" , "Σ" ,
"τ" , "τ" ,
"Τ" , "Τ " ,
"υ" , "υ " ,
"Υ" , "Υ " ,
"φ" , "φ" ,
"Φ" , "Φ" ,
"χ" , "χ" ,
"Χ" , "Χ " ,
"ψ" , "ψ" ,
"Ψ" , "Ψ" ,
"ω" , "ω" ,
"Ω" , "Ω" ,
"°" , "°" ,
"µ" , "µ" ,
"<" , "<" ,
">" , ">" ,
"≤" , "≤" ,
"≥" , "≥" ,
"≈" , "≈" ,
"≠" , "≠" ,
"≡" , "≡" ,
"±" , "±" ,
"−" , "− " ,
"×" , "× " ,
"÷" , "÷" ,
"⁄" , "⁄ " ,
"‰" , "‰" ,
"¼" , "¼" ,
"½" , "½" ,
"¾" , "¾" ,
"¹" , "¹" ,
"²" , "²" ,
"³" , "³" ,
"º" , "º" ,
"ª" , "ª" ,
"ƒ" , "ƒ" ,
"′" , "′ " ,
"″" , "″" ,
"∂" , "∂" ,
"∏" , "∏" ,
"∑" , "∑" ,
"√" , "√" ,
"∞" , "∞" ,
"¬" , "¬" ,
"∩" , "∩" ,
"∫" , "∫"
]
for ( i = 0 ; i < 159 * 2 ; i += 2 )
{
while ( texte . indexOf ( car [ i + sens ] ) != - 1 )
texte = texte . replace ( car [ i + sens ] , car [ i + 1 - sens ] ) ;
}
return texte ;
}
function assouplie _syntaxe ( texte , langage _source ) {
while ( texte . indexOf ( "Hasard()" ) != - 1 )
texte = texte . replace ( "Hasard()" , "Hasard(1)" ) ;
while ( texte . indexOf ( "hasard()" ) != - 1 )
texte = texte . replace ( "hasard()" , "hasard(1)" ) ;
while ( texte . indexOf ( "Hasard ()" ) != - 1 )
texte = texte . replace ( "Hasard ()" , "Hasard(1)" ) ;
while ( texte . indexOf ( "hasard ()" ) != - 1 )
texte = texte . replace ( "hasard ()" , "hasard(1)" ) ;
return texte ;
}
function compilation ( langage _source ) {
var texte = document . getElementById ( 'saisie' ) . value ;
var i , mot = "" , dernier _mot = 0 , espace _position , mot _en _cours ;
var ligne = 1 , type ;
var calcul _memoire = "undefined" , ligne _memoire , chaine = 0 ;
var texte _compil = escape ( texte ) ;
createCookie ( "last_algo" , texte _compil , 30 ) ;
createCookie ( "last_language" , langage _source , 30 ) ;
createCookie ( 'exemple' , 'init' , 30 ) ;
debug _efface ( ) ;
// remise à 0 des variables
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
nb _erreurs = 0 ;
conditions _ouvertes = 0 ;
conditions _fermees = 0 ;
alors = 0 ;
boucles _ouvertes = 0 ;
boucles _fermees = 0 ;
pour _ouverts = 0 ;
pour _fermes = 0 ;
ti _ouverts = 0 ;
ti _fermes = 0 ;
for _ti = 0 ;
j = 0 ;
k = 0 ;
next = 0 ;
while ( escape ( texte ) . indexOf ( "%0A" ) != - 1 )
texte = unescape ( escape ( texte ) . replace ( "%0A" , "" ) ) ;
// modifie le texte pour lire les retours à la ligne (@)
texte = saut _de _ligne ( texte ) ;
// remplace les entités html par les caractères spéciaux correspondants
texte = caracteres _speciaux ( texte , 0 ) ;
texte = assouplie _syntaxe ( texte , langage _source ) ;
// détection des chaines de caractères
for ( i = 0 ; i < texte . length ; i ++ )
{
mot _en _cours = texte . substring ( dernier _mot , i + 1 ) ;
if ( chaine == 1 && texte . substring ( i , i + 1 ) == "\"" )
{
chaine = 0 ;
identification ( mot _en _cours , ligne , "chaine" , langage _source ) ;
mot _en _cours = "" ;
dernier _mot = i + 1 ;
if ( dernier _mot == texte . length )
break ;
}
if ( dernier _mot == i && mot _en _cours . substring ( 0 , 1 ) == "\"" )
{
chaine = 1 ;
if ( calcul _memoire != "undefined" )
{
identification ( calcul _memoire , ligne _memoire , "calcul" , langage _source ) ;
calcul _memoire = "undefined" ;
}
}
// détection des instruction (attention : piège si/sinon, if/ifend...)
type = detecte _instruction ( mot _en _cours , langage _source ) ;
if ( ( type == "si" && texte . toLowerCase ( ) . substring ( dernier _mot , i + 4 ) == "sinon" ) || ( type == "if" && texte . toLowerCase ( ) . substring ( dernier _mot , i + 4 ) == "ifend" ) || ( type == "while" && texte . toLowerCase ( ) . substring ( dernier _mot , i + 4 ) == "whileend" ) )
type = "undefined" ;
if ( type == "undefined" )
{
// détection des signes (>, <, =, ...)
type = detecte _signe ( mot _en _cours ) ;
if ( type == "signe" && detecte _signe ( texte . substring ( dernier _mot , i + 2 ) ) == "signe" )
{
i ++ ;
mot _en _cours = texte . substring ( dernier _mot , i + 1 ) ;
}
}
if ( next == 1 )
{
next = 0 ;
previsions [ indice _previsions ] = "PREV_END" ;
}
if ( mot _en _cours . indexOf ( "@" , 0 ) >= 0 && previsions [ indice _previsions ] == "condition_add" )
next = 1 ;
// découpage en "mots" et traitement
if ( ( ( mot _en _cours . indexOf ( " " , 0 ) >= 0 || escape ( texte . substring ( dernier _mot + 1 , i + 2 ) ) == "%u2220" || type != "undefined" || ( detecte _signe ( texte . substring ( i + 1 , i + 2 ) ) == "signe" && texte . substring ( i , i + 1 ) != "-" ) || ( escape ( texte . substring ( i + 1 , i + 7 ) ) == "%u2220" && langage _source == 2 && mot _en _cours . length > 0 ) || ( escape ( texte . substring ( i + 1 , i + 3 ) ) == "%u2192" && langage _source != 1 ) ) && chaine == 0 ) || mot _en _cours . indexOf ( "@" , 0 ) >= 0 || i + 1 == texte . length || ( for _ti == 1 && mot _en _cours . length > 0 && texte . substring ( dernier _mot , i + 2 ) . indexOf ( "," , 0 ) >= 0 ) || mot _en _cours == "," )
{
// gère les retours à la ligne
if ( mot _en _cours != "@" && mot _en _cours != " " )
{
mot = mot _en _cours ;
espace _position = mot . search ( " " ) ;
if ( espace _position == - 1 )
{
espace _position = mot . search ( "@" ) ;
chaine = 0 ;
}
if ( espace _position != - 1 )
{
mot = mot . substring ( 0 , espace _position ) ;
chaine = 0 ;
}
// gère les calculs
if ( detecte _calculs ( mot , langage _source ) == "calcul" )
{
if ( calcul _memoire != "undefined" )
{
mot = calcul _memoire + mot ;
}
else
ligne _memoire = ligne ;
calcul _memoire = mot ;
if ( i + 1 == texte . length || mot _en _cours . indexOf ( "@" , 0 ) >= 0 || texte . substring ( i + 1 , i + 2 ) == "@" )
{
identification ( calcul _memoire , ligne _memoire , "calcul" , langage _source ) ;
calcul _memoire = "undefined" ;
}
}
else
{
if ( calcul _memoire != "undefined" )
{
identification ( calcul _memoire , ligne _memoire , "calcul" , langage _source ) ;
calcul _memoire = "undefined" ;
}
identification ( mot , ligne , type , langage _source ) ;
}
}
mot = "" ;
dernier _mot = i + 1 ;
if ( mot _en _cours . indexOf ( "@" , 0 ) >= 0 )
ligne ++ ;
}
}
// Après la détection, on gère les erreurs si il manque des éléments attendus
if ( previsions [ indice _previsions ] == "tout" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
}
else
{
for ( n = 0 ; n <= indice _previsions ; n ++ )
{
if ( previsions [ n ] == "PREV_END" )
{
previsions [ indice _previsions ] = "PREV_END" ;
break ;
}
}
if ( previsions [ indice _previsions ] != "PREV_END" && previsions [ indice _previsions ] != "condition_add" )
{
erreur _syntaxe [ 4 * nb _erreurs ] = previsions [ indice _previsions ] ;
ameliore _syntaxe _suggestion ( 2 ) ;
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "manquant" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
//ameliore_syntaxe_suggestion(langage_source);
nb _erreurs ++ ;
}
}
// on compte les éléments de conditions et de boucles
if ( ( langage _source != 3 && conditions _ouvertes - conditions _fermees != 0 ) || conditions _ouvertes - alors != 0 )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "erreur_conditions" ;
nb _erreurs ++ ;
}
if ( langage _source != 3 && ( boucles _ouvertes - boucles _fermees != 0 || pour _ouverts - pour _fermes != 0 ) )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "erreur_boucles" ;
nb _erreurs ++ ;
}
if ( langage _source == 3 && ti _ouverts - ti _fermes != 0 )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "erreur_ti" ;
nb _erreurs ++ ;
}
// si il y a des erreurs, on les affiche
if ( nb _erreurs > 0 )
{
texte = affiche _erreurs ( texte ) ;
texte = texte . replace ( "@" , "<div>" ) ;
while ( texte . indexOf ( "@" ) != - 1 )
texte = texte . replace ( "@" , "</div><div>" ) ;
texte += "</div>" ;
document . getElementById ( "saisie" ) . value = texte ;
$ ( "#saisie" ) . cleditor ( ) [ 0 ] . updateFrame ( ) . focus ( ) ;
return "failed" ;
}
else
{
masque _erreurs ( ) ;
return "completed" ;
}
}
function identification ( mot _recu , ligne , type , langage _source ) {
var mot ;
mot = mot _recu . toLowerCase ( ) ;
if ( mot == "," )
type = "," ;
// on différencie variables et calculs
if ( type == "calcul" )
{
type = detecte _variable ( mot ) ;
if ( type != "variable" )
type = "calcul" ;
}
// on affiche une erreur si le type reçu est différent du type attendu
if ( ( type != previsions [ indice _previsions ] && previsions [ indice _previsions ] != "tout" && ! ( ( type == "chaine" || type == "variable" || type == "calcul" ) && previsions [ indice _previsions ] == "chaine_variable_calcul" ) && ! ( ( escape ( type ) == "%u2192" || escape ( type ) == "%u2220" || type == "prend_la_valeur" || type == "#ran" ) && previsions [ indice _previsions ] == "→_ou_∠" ) && ! ( ( type == "variable" || type == "calcul" || type == "signe" || type == "et" || type == "ou" || type == "and" || type == "or" ) && ( previsions [ indice _previsions ] == "condition" || previsions [ indice _previsions ] == "condition_alors" ) ) && ! ( previsions [ indice _previsions ] == "condition_alors" ) && ! ( type == "variable" && previsions [ indice _previsions ] == "calcul" ) ) && ! ( escape ( type ) == "%u2192" && previsions [ indice _previsions ] == "→" ) && ! ( escape ( type ) == "%u2220" && previsions [ indice _previsions ] == "∠" ) && previsions [ indice _previsions ] != "PREV_END" && previsions [ indice _previsions ] != "condition_add" )
{
erreur _syntaxe [ 4 * nb _erreurs ] = previsions [ indice _previsions ] ;
if ( type == "chaine" )
mot _recu = mot _recu . substring ( 1 , mot _recu . length - 1 ) ;
erreur _syntaxe [ 4 * nb _erreurs + 1 ] = mot _recu ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
ameliore _syntaxe _description ( ) ;
ameliore _syntaxe _suggestion ( langage _source ) ;
nb _erreurs ++ ;
}
indice _previsions ++ ;
// gestion des conditions
if ( previsions [ indice _previsions - 1 ] == "condition" || previsions [ indice _previsions - 1 ] == "condition_alors" || previsions [ indice _previsions - 1 ] == "condition_add" )
{
code [ j ] = mot ;
if ( previsions [ indice _previsions - 1 ] == "condition" )
previsions [ 0 ] = "condition_add" ;
else
previsions [ 0 ] = previsions [ indice _previsions - 1 ] ;
previsions [ 1 ] = "PREV_END" ;
if ( previsions [ indice _previsions ] == "alors" )
previsions [ 1 ] = "alors" ;
if ( previsions [ indice _previsions ] == "then" )
previsions [ 1 ] = "then" ;
indice _previsions = 0 ;
}
code [ j ] = "rien" ;
// reconnaissance des fonctions "algorithme"
if ( langage _source == 1 )
{
switch ( type )
{
case "lire" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "lire" ;
previsions [ 0 ] = "variable" ;
previsions [ 1 ] = "PREV_END" ;
indice _previsions = 0 ;
break ;
case "afficher" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "afficher" ;
previsions [ 0 ] = "chaine_variable_calcul" ;
previsions [ 1 ] = "PREV_END" ;
indice _previsions = 0 ;
break ;
case "si" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "si" ;
previsions [ 0 ] = "condition" ;
previsions [ 1 ] = "alors" ;
previsions [ 2 ] = "PREV_END" ;
indice _previsions = 0 ;
conditions _ouvertes ++ ;
break ;
case "alors" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "alors" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
alors ++ ;
break ;
case "sinon" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "sinon" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
verifie _conditions ( mot _recu , ligne ) ;
break ;
case "fin_si" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "fin_si" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
verifie _conditions ( mot _recu , ligne ) ;
conditions _fermees ++ ;
break ;
case "tant_que" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "tant_que" ;
previsions [ 0 ] = "condition" ;
previsions [ 1 ] = "PREV_END" ;
indice _previsions = 0 ;
boucles _ouvertes ++ ;
break ;
case "fin_tant_que" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "fin_tant_que" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
verifie _boucles ( mot _recu , ligne , langage _source ) ;
boucles _fermees ++ ;
break ;
case "pour" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "pour" ;
previsions [ 0 ] = "variable" ;
previsions [ 1 ] = "allant_de" ;
previsions [ 2 ] = "calcul" ;
previsions [ 3 ] = "à" ;
previsions [ 4 ] = "calcul" ;
previsions [ 5 ] = "PREV_END" ;
indice _previsions = 0 ;
pour _ouverts ++ ;
break ;
case "allant_de" :
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
code [ j ] = "allant_de" ;
break ;
case "à" :
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
code [ j ] = "à" ;
break ;
case "fin_pour" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "fin_pour" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
verifie _pour ( mot _recu , ligne , langage _source ) ;
pour _fermes ++ ;
break ;
case "prend_la_valeur" :
code [ j ] = "prend_la_valeur" ;
previsions [ 0 ] = "calcul" ;
previsions [ 1 ] = "PREV_END" ;
indice _previsions = 0 ;
break ;
case "et" :
code [ j ] = "et" ;
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
break ;
case "ou" :
code [ j ] = "ou" ;
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
break ;
case "fin_programme" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "fin_programme" ;
if ( previsions [ indice _previsions - 1 ] != "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
break ;
}
}
// reconnaissance des fonctions Casio
if ( langage _source == 2 )
{
switch ( type )
{
case "?" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "lire" ;
previsions [ 0 ] = "→" ;
previsions [ 1 ] = "variable" ;
previsions [ 2 ] = "PREV_END" ;
indice _previsions = 0 ;
break ;
case "ifend" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "fin_si" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
verifie _conditions ( mot _recu , ligne ) ;
conditions _fermees ++ ;
break ;
case "for" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "pour" ;
previsions [ 0 ] = "calcul" ;
previsions [ 1 ] = "→" ;
previsions [ 2 ] = "variable" ;
previsions [ 3 ] = "to" ;
previsions [ 4 ] = "calcul" ;
previsions [ 5 ] = "PREV_END" ;
indice _previsions = 0 ;
pour _ouverts ++ ;
break ;
case "to" :
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
code [ j ] = "to" ;
break ;
case "next" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "fin_pour" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
verifie _pour ( mot _recu , ligne , langage _source ) ;
pour _fermes ++ ;
break ;
}
}
// reconnaissance des fonctions TI
if ( langage _source == 3 )
{
switch ( type )
{
case "input" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "lire" ;
previsions [ 0 ] = "variable" ;
previsions [ 1 ] = "PREV_END" ;
indice _previsions = 0 ;
break ;
case "disp" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "afficher" ;
previsions [ 0 ] = "chaine_variable_calcul" ;
previsions [ 1 ] = "PREV_END" ;
indice _previsions = 0 ;
break ;
case "end" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "fin_si_ou_boucle" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
ti _fermes ++ ;
break ;
case "for(" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "pour" ;
previsions [ 0 ] = "variable" ;
previsions [ 1 ] = "," ;
previsions [ 2 ] = "calcul" ;
previsions [ 3 ] = "," ;
previsions [ 4 ] = "calcul" ;
previsions [ 5 ] = "PREV_END" ;
indice _previsions = 0 ;
for _ti = 1 ;
ti _ouverts ++ ;
break ;
}
}
// reconnaissance des fonctions communes à Casio et TI
if ( langage _source == 2 || langage _source == 3 )
{
switch ( type )
{
case "if" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "si" ;
previsions [ 0 ] = "condition" ;
previsions [ 1 ] = "then" ;
previsions [ 2 ] = "PREV_END" ;
indice _previsions = 0 ;
conditions _ouvertes ++ ;
ti _ouverts ++ ;
break ;
case "then" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "alors" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
alors ++ ;
break ;
case "else" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "sinon" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
verifie _conditions ( mot _recu , ligne ) ;
break ;
case "while" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "tant_que" ;
previsions [ 0 ] = "condition" ;
previsions [ 1 ] = "PREV_END" ;
indice _previsions = 0 ;
boucles _ouvertes ++ ;
ti _ouverts ++ ;
break ;
case "whileend" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "fin_tant_que" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
verifie _boucles ( mot _recu , ligne , langage _source ) ;
boucles _fermees ++ ;
break ;
case "→" :
code [ j ] = "→" ;
if ( previsions [ indice _previsions ] == "PREV_END" )
{
previsions [ 0 ] = "variable" ;
previsions [ 1 ] = "PREV_END" ;
indice _previsions = 0 ;
}
break ;
case "and" :
code [ j ] = "et" ;
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
break ;
case "or" :
code [ j ] = "ou" ;
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
break ;
case "stop" :
indices [ k ] = j ;
k ++ ;
code [ j ] = "fin_programme" ;
if ( previsions [ indice _previsions - 1 ] != "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
break ;
}
switch ( escape ( type ) )
{
case "%u2192" :
code [ j ] = "→" ;
if ( previsions [ indice _previsions ] == "PREV_END" )
{
previsions [ 0 ] = "variable" ;
previsions [ 1 ] = "PREV_END" ;
indice _previsions = 0 ;
}
break ;
case "%u2220" :
if ( langage _source == 2 )
{
code [ j ] = "∠" ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
}
break ;
}
}
// reconnaissance des fonctions communes aux trois langages
switch ( type )
{
case "chaine" :
if ( previsions [ indice _previsions - 1 ] == "PREV_END" || previsions [ indice _previsions ] == "condition_add" )
{
if ( langage _source == 2 )
{
indices [ k ] = j ;
k ++ ;
previsions [ 0 ] = "∠" ;
previsions [ 1 ] = "PREV_END" ;
}
else
{
previsions [ 0 ] = "PREV_END" ;
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
}
indice _previsions = 0 ;
}
code [ j ] = "chaine" ;
j ++ ;
code [ j ] = mot _recu ;
break ;
case "variable" :
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
indices [ k ] = j ;
k ++ ;
code [ j ] = "variable" ;
if ( langage _source != 1 )
{
if ( langage _source == 2 ) previsions [ 0 ] = "→_ou_∠" ;
if ( langage _source == 3 ) previsions [ 0 ] = "→" ;
previsions [ 1 ] = "PREV_END" ;
}
else
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
}
else
code [ j ] = "variable" ;
j ++ ;
code [ j ] = mot ;
break ;
case "calcul" :
if ( mot == ")" && for _ti == 1 )
{
j -- ;
for _ti = 0 ;
break ;
}
if ( mot == "," && for _ti == 1 )
{
j -- ;
break ;
}
if ( previsions [ indice _previsions - 1 ] == "PREV_END" || previsions [ indice _previsions ] == "condition_add" )
{
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
indices [ k ] = j ;
k ++ ;
}
code [ j ] = "calcul" ;
if ( langage _source != 1 && previsions [ indice _previsions ] != "condition_add" )
{
previsions [ 0 ] = "→_ou_∠" ;
previsions [ 1 ] = "PREV_END" ;
}
else
previsions [ 0 ] = "condition_add" ;
indice _previsions = 0 ;
}
else
{
code [ j ] = "calcul" ;
}
if ( for _ti == 1 && indice _previsions == 5 )
for _ti = 0 ;
j ++ ;
code [ j ] = mot ;
break ;
case "," :
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
code [ j ] = "," ;
break ;
case "signe" :
if ( previsions [ indice _previsions - 1 ] == "PREV_END" )
{
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "tout" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
indice _previsions = 0 ;
}
code [ j ] = "signe" ;
j ++ ;
code [ j ] = mot ;
break ;
}
// en cas de non reconnaissance de l'instruction : erreur
if ( code [ j ] == "rien" )
{
erreur _syntaxe [ 4 * nb _erreurs ] = previsions [ indice _previsions - 1 ] ;
erreur _syntaxe [ 4 * nb _erreurs + 1 ] = mot _recu ;
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "non_reconnu" ;
erreur _syntaxe [ 4 * nb _erreurs + 3 ] = ligne ;
nb _erreurs ++ ;
previsions [ 0 ] = "PREV_END" ;
indice _previsions = 0 ;
}
j ++ ;
}
// affichage des erreurs
function affiche _erreurs ( code ) {
var a ;
affiche _debug ( ) ;
for ( a = nb _erreurs - 1 ; a >= 0 ; a -- )
{
var expression = 0 ;
switch ( erreur _syntaxe [ 4 * a + 2 ] )
{
case "non_reconnu" :
2019-12-27 11:51:42 +01:00
$ ( "#debug_titre" ) . after ( "<p class='erreurs'><span id='description_erreur'>• ligne " + erreur _syntaxe [ 4 * a + 3 ] + " : expression \"" + erreur _syntaxe [ 4 * a + 1 ] + "\" non reconnue.</span><br /><img class='ampoule' src='assets/img/programm/ampoule.png'/><span id='suggestion_erreur'>Suggestion : vérifiez la syntaxe du mot.</span></p>" ) ;
2019-12-27 01:57:39 +01:00
expression = erreur _syntaxe [ 4 * a + 1 ] ;
break ;
case "manquant" :
2019-12-27 11:51:42 +01:00
$ ( "#debug_titre" ) . after ( "<p class='erreurs'><span id='description_erreur'>• ligne " + erreur _syntaxe [ 4 * a + 3 ] + " : élément de syntaxe manquant.</span><br /><img class='ampoule' src='assets/img/programm/ampoule.png'/><span id='suggestion_erreur'>Suggestion : ajoutez " + erreur _syntaxe [ 4 * a ] + ".</span></p>" ) ;
2019-12-27 01:57:39 +01:00
break ;
case "tout" :
$ ( "#debug_titre" ) . after ( "<p class='erreurs'><span id='description_erreur'>• ligne " + erreur _syntaxe [ 4 * a + 3 ] + " : élément manquant.</span></p>" ) ;
break ;
case "instruction_illegale" :
2019-12-27 11:51:42 +01:00
$ ( "#debug_titre" ) . after ( "<p class='erreurs'><span id='description_erreur'>• ligne " + erreur _syntaxe [ 4 * a + 3 ] + " : instruction \"" + erreur _syntaxe [ 4 * a + 1 ] + "\" inadaptée au contexte.</span><br /><img class='ampoule' src='assets/img/programm/ampoule.png'/><span id='suggestion_erreur'>Suggestion : vérifiez vos " + erreur _syntaxe [ 4 * a + 4 ] + ".</span></p>" ) ;
2019-12-27 01:57:39 +01:00
expression = erreur _syntaxe [ 4 * a + 1 ] ;
break ;
case "erreur_conditions" :
2019-12-27 11:51:42 +01:00
$ ( "#debug_titre" ) . after ( "<p class='erreurs'><span id='description_erreur'>• Erreur de conditions</span><br /><img class='ampoule' src='assets/img/programm/ampoule.png'/><span id='suggestion_erreur'>Suggestion : vérifiez vos conditions.</span></p>" ) ;
2019-12-27 01:57:39 +01:00
break ;
case "erreur_boucles" :
2019-12-27 11:51:42 +01:00
$ ( "#debug_titre" ) . after ( "<p class='erreurs'><span id='description_erreur'>• Erreur de boucles</span><br /><img class='ampoule' src='assets/img/programm/ampoule.png'/><span id='suggestion_erreur'>Suggestion : vérifiez vos boucles.</span></p>" ) ;
2019-12-27 01:57:39 +01:00
break ;
case "erreur_ti" :
2019-12-27 11:51:42 +01:00
$ ( "#debug_titre" ) . after ( "<p class='erreurs'><span id='description_erreur'>• Erreur de structure</span><br /><img class='ampoule' src='assets/img/programm/ampoule.png'/><span id='suggestion_erreur'>Suggestion : vérifiez vos conditions et vos boucles.</span></p>" ) ;
2019-12-27 01:57:39 +01:00
break ;
default :
2019-12-27 11:51:42 +01:00
$ ( "#debug_titre" ) . after ( "<p class='erreurs'><span id='description_erreur'>• ligne " + erreur _syntaxe [ 4 * a + 3 ] + " : " + erreur _syntaxe [ 4 * a + 2 ] + " au contexte.</span><br /><img class='ampoule' src='assets/img/programm/ampoule.png'/><span id='suggestion_erreur'>Suggestion : remplacer \"" + erreur _syntaxe [ 4 * a + 1 ] + "\" par " + erreur _syntaxe [ 4 * a ] + ".</span></p>" ) ;
2019-12-27 01:57:39 +01:00
expression = erreur _syntaxe [ 4 * a + 1 ] ;
break ;
}
if ( erreur _syntaxe [ 4 * a + 2 ] != "erreur_conditions" && erreur _syntaxe [ 4 * a + 2 ] != "erreur_boucles" && erreur _syntaxe [ 4 * a + 2 ] != "erreur_ti" )
code = surligne _erreurs ( code , erreur _syntaxe [ 4 * a + 3 ] , expression ) ;
}
return code ;
}
// affiche le "debug"
function affiche _debug ( ) {
$ ( '#execution' ) . css ( 'height' , '85px' ) ;
$ ( '#debug' ) . show ( ) ;
}
// cache le "debug"
function masque _erreurs ( ) {
$ ( '#debug' ) . hide ( ) ;
$ ( '#execution' ) . css ( 'height' , '210px' ) ;
}
// efface le debug
function debug _efface ( ) {
$ ( "" ) . replaceAll ( '.erreurs' ) ;
}
function ameliore _syntaxe _description ( ) {
erreur _syntaxe [ 4 * nb _erreurs + 2 ] = "expression \"" + erreur _syntaxe [ 4 * nb _erreurs + 1 ] + "\" inadaptée" ;
}
function ameliore _syntaxe _suggestion ( langage _source ) {
var a , nouveau ;
switch ( erreur _syntaxe [ 4 * nb _erreurs ] )
{
case "variable" :
nouveau = "une variable" ;
break ;
case "chaine" :
nouveau = "un texte" ;
break ;
case "calcul" :
nouveau = "une valeur" ;
break ;
case "chaine_variable_calcul" :
nouveau = "un texte ou une valeur" ;
break ;
case "condition" :
nouveau = "une condition" ;
break ;
case "condition_alors" :
if ( langage _source == 1 )
nouveau = "l'instruction \"Alors\"" ;
else
nouveau = "l'instruction \"Then\"" ;
break ;
case "→_ou_∠" :
if ( langage _source == 2 )
nouveau = "\"→\" suivi d'une variable ou de \"∠\"" ;
else
nouveau = "\"→\" suivi d'une variable" ;
break ;
default :
nouveau = "\"" + erreur _syntaxe [ 4 * nb _erreurs ] + "\"" ;
break ;
}
erreur _syntaxe [ 4 * nb _erreurs ] = nouveau ;
}
function detecte _variable ( mot ) {
if ( mot == "a" || mot == "b" || mot == "c" || mot == "d" || mot == "e" || mot == "f" || mot == "g" || mot == "h" || mot == "i" || mot == "j" || mot == "k" || mot == "l" || mot == "m" || mot == "n" || mot == "o" || mot == "p" || mot == "q" || mot == "r" || mot == "s" || mot == "t" || mot == "u" || mot == "v" || mot == "w" || mot == "x" || mot == "y" || mot == "z" )
return "variable" ;
else return mot ;
}
function detecte _calculs ( mot , langage _source ) {
var lettre , dernier , avant _dernier = "undefined" ;
mot = mot . toLowerCase ( ) ;
for ( a = 0 ; a < mot . length ; a ++ )
{
lettre = mot . substring ( a , a + 1 ) ;
if ( mot . substring ( a , a + 14 ) == "racine_carrée(" )
{
lettre = "+" ;
a += 13 ;
}
if ( mot . substring ( a , a + 4 ) == "exp(" )
{
lettre = "+" ;
a += 3 ;
}
if ( mot . substring ( a , a + 2 ) == "ln" )
{
lettre = "+" ;
a += 1 ;
}
if ( langage _source == 1 )
{
if ( mot . substring ( a , a + 6 ) == "hasard" )
{
lettre = "+" ;
a += 5 ;
}
if ( mot . substring ( a , a + 14 ) == "partie_entière" )
{
lettre = "+" ;
a += 13 ;
}
if ( mot . substring ( a , a + 15 ) == "partie_décimale" )
{
lettre = "+" ;
a += 14 ;
}
if ( mot . substring ( a , a + 14 ) == "valeur_absolue" )
{
lettre = "+" ;
a += 13 ;
}
}
if ( langage _source == 2 )
{
if ( mot . substring ( a , a + 4 ) == "#ran" )
{
lettre = "+" ;
a += 3 ;
}
if ( mot . substring ( a , a + 3 ) == "int" )
{
lettre = "+" ;
a += 2 ;
}
if ( mot . substring ( a , a + 4 ) == "frac" )
{
lettre = "+" ;
a += 3 ;
}
if ( mot . substring ( a , a + 3 ) == "abs" )
{
lettre = "+" ;
a += 2 ;
}
}
if ( langage _source == 3 )
{
if ( mot . substring ( a , a + 4 ) == "rand" )
{
lettre = "+" ;
a += 3 ;
}
if ( mot . substring ( a , a + 4 ) == "int(" )
{
lettre = "+" ;
a += 3 ;
}
if ( mot . substring ( a , a + 6 ) == "fpart(" )
{
lettre = "+" ;
a += 5 ;
}
if ( mot . substring ( a , a + 4 ) == "abs(" )
{
lettre = "+" ;
a += 3 ;
}
}
dernier = detecte _variable ( lettre ) ;
if ( ( dernier == "variable" && dernier == avant _dernier ) || ! ( detecte _nombre ( lettre , 0 ) == "nombre" || dernier == "variable" || lettre == "+" || lettre == "-" || lettre == "*" || lettre == "%" || lettre == "/" || lettre == "^" || lettre == "(" || lettre == ")" || escape ( lettre ) == "%u03C0" ) )
return "undefined" ;
avant _dernier = dernier ;
}
return "calcul" ;
}
function detecte _nombre ( mot , lire ) {
var a , current , point = 0 ;
if ( mot == "π" )
return "nombre" ;
for ( a = 0 ; a < mot . length ; a ++ )
{
current = mot . substring ( a , a + 1 ) ;
if ( lire == 1 && a == 0 && ( current == "+" || current == "-" ) && mot . length > 1 )
continue ;
if ( current == "." )
point ++ ;
if ( point > 1 || ! ( current == "0" || current == "1" || current == "2" || current == "3" || current == "4" || current == "5" || current == "6" || current == "7" || current == "8" || current == "9" || current == "." ) )
return mot ;
}
return "nombre" ;
}
function surligne _erreurs ( code , ligne , expression ) {
var i ;
var begin = 0 , end ;
for ( i = 1 ; i < ligne ; i ++ )
{
begin = code . indexOf ( "@" , begin ) + 1 ;
}
if ( expression )
{
begin = code . indexOf ( expression , begin ) ;
end = begin + expression . length ;
}
else
{
end = code . indexOf ( "@" , begin ) + 1 ;
if ( end == - 1 )
end = code . length ;
}
var newcode = code . substring ( 0 , begin ) + "<span style='background-color:yellow;'>" + code . substring ( begin , end ) + "</span>" + code . substring ( end , code . length ) ;
return newcode ;
}