Download source/src/training/diagnostics.js from FormaLau/microduck-reachy-simulator: direct link, hf CLI and curl.
- Browser
- Download file 5.93 kB
-
https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/src/training/diagnostics.js
- Command line
-
hf download hf://spaces/FormaLau/microduck-reachy-simulator/source/src/training/diagnostics.js
-
curl -L -o diagnostics.js https://huggingface.co/spaces/FormaLau/microduck-reachy-simulator/resolve/main/source/src/training/diagnostics.js
5.93 kB
| function typoDistance(a, b) { | |
| const d = Array.from({ length: a.length + 1 }, (_, i) => [i]); | |
| for (let j = 0; j <= b.length; j++) d[0][j] = j; | |
| for (let i = 1; i <= a.length; i++) for (let j = 1; j <= b.length; j++) { | |
| d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1)); | |
| if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1); | |
| } | |
| return d[a.length][b.length]; | |
| } | |
| // Explain the first error. Its full technical wording stays in the editor. | |
| export function explainDiagnostic(diagnostics, source = "", language = "en") { | |
| const t = (en, fr) => language === "fr" ? fr : en; | |
| const match = diagnostics.match(/main\.c:(\d+):(\d+):\s*(?:fatal )?error:\s*([^\n]+)/); | |
| const detail = match?.[3] || diagnostics.split("\n").find((line) => /error:/.test(line)) || diagnostics.trim().split("\n")[0]; | |
| const where = match ? t(`On line ${match[1]}. `, `À la ligne ${match[1]}. `) : ""; | |
| let hint = t("The compiler could not accept this part of the program. Check the highlighted line and the statement before it. The detailed diagnostic is available below.", "Le compilateur n’a pas pu accepter cette partie du programme. Vérifiez la ligne indiquée et l’instruction qui la précède. Le diagnostic détaillé est affiché ci-dessus."); | |
| if (/expected ';'/.test(detail)) hint = t("A semicolon is missing. In C, it marks the end of a statement. Add it at the end of this statement or the previous one.", "Il manque un point-virgule. En C, il marque la fin d’une instruction. Ajoutez-le à la fin de cette instruction ou de la précédente."); | |
| else if (/undeclared identifier/.test(detail)) { | |
| const name = detail.match(/identifier '([^']+)'/)?.[1]; | |
| hint = t(`The name${name ? ` '${name}'` : " you used"} has not been declared in this scope. Declare this variable before using it, or correct its spelling.`, `Le nom${name ? ` « ${name} »` : " utilisé"} n’a pas été déclaré à cet endroit. Déclarez cette variable avant de l’utiliser, ou corrigez son orthographe.`); | |
| } else if (/undeclared function|implicit function/.test(detail)) { | |
| const name = detail.match(/(?:undeclared function|function) ['‘]([A-Za-z_]\w*)/)?.[1]; | |
| const suggestion = name && ["scanf", "printf", "puts", "getchar", "fgets", "malloc", "free"].find((candidate) => candidate !== name && typoDistance(name, candidate) === 1); | |
| hint = name ? t(`The compiler does not recognize '${name}' as a function. `, `Le compilateur ne reconnaît pas « ${name} » comme une fonction. `) : t("The compiler has not seen a declaration for this function. ", "Le compilateur n’a vu aucune déclaration pour cette fonction. "); | |
| if (suggestion) hint += t(`Did you mean '${suggestion}'? Correct the spelling of the function name.`, `Vouliez-vous écrire « ${suggestion} » ? Corrigez l’orthographe du nom de la fonction.`); | |
| else if (name && /^(scanf|printf|puts|getchar|fgets)$/.test(name) && !/#\s*include\s*[<"]stdio\.h[>"]/.test(source)) hint += t("Include stdio dot h. That header tells the compiler how to call this standard input or output function.", "Ajoutez #include <stdio.h>. Cet en-tête indique au compilateur comment appeler cette fonction d’entrée ou de sortie standard."); | |
| else hint += t("Check its spelling, or add its declaration before the call. Include its header if it belongs to a library.", "Vérifiez son orthographe, ou déclarez-la avant l’appel. Incluez son en-tête si elle provient d’une bibliothèque."); | |
| } else if (/expected '\}'/.test(detail)) hint = t("A code block is still open. Add its missing closing brace. Each opening brace needs a matching closing brace.", "Un bloc de code n’est pas fermé. Ajoutez l’accolade fermante manquante : chaque accolade ouvrante doit avoir son accolade fermante."); | |
| else if (/expected '\)'/.test(detail)) hint = t("A parenthesis is still open. Close the function call or condition before continuing the statement.", "Une parenthèse n’est pas fermée. Fermez l’appel de fonction ou la condition avant de continuer l’instruction."); | |
| else if (/file not found/.test(detail)) hint = t("The compiler cannot find an included header. Check its name in the include directive. This environment provides the bundled C standard library.", "Le compilateur ne trouve pas un fichier d’en-tête inclus. Vérifiez son nom dans la directive #include. Cet environnement fournit la bibliothèque standard du C."); | |
| else if (/undefined symbol: main/.test(detail)) hint = t("The program has no main entry point. Add a main function so the runtime knows where to start.", "Le programme n’a pas de point d’entrée main. Ajoutez une fonction main pour indiquer où le programme commence."); | |
| else if (/undefined symbol/.test(detail)) hint = t("The program refers to a function or variable whose definition cannot be found. Provide its implementation or link the library that defines it.", "Le programme utilise une fonction ou une variable dont la définition est introuvable. Écrivez son implémentation ou vérifiez son nom."); | |
| else if (/incompatible|conversion/.test(detail)) hint = t("These values have incompatible types. Compare the expected type with the value you passed. A pointer and a number, for example, are not interchangeable.", "Ces valeurs ont des types incompatibles. Comparez le type attendu avec la valeur fournie : un pointeur et un nombre, par exemple, ne sont pas interchangeables."); | |
| else if (/too (?:few|many) arguments/.test(detail)) hint = t("The number of arguments does not match the function declaration. Check which inputs it expects, then adjust the call.", "Le nombre d’arguments ne correspond pas à la déclaration de la fonction. Vérifiez les paramètres attendus, puis corrigez l’appel."); | |
| return `${t("Compilation failed. ", "La compilation a échoué. ")}${where}${hint}`; | |
| } | |