146 lines
5.4 KiB
JavaScript
146 lines
5.4 KiB
JavaScript
document.addEventListener("DOMContentLoaded", function () {
|
|
const analyzeBtn = document.getElementById("analyzeBtn");
|
|
const aiHelpBtn = document.getElementById("aiHelpBtn");
|
|
const translateBtn = document.getElementById("translateBtn");
|
|
const output = document.getElementById("output");
|
|
const aiHelpOutput = document.getElementById("aiHelpOutput");
|
|
const translateOutput = document.getElementById("translateOutput");
|
|
const languageSelect = document.getElementById("languageSelect");
|
|
const translateContainer = document.querySelector(".translate-container");
|
|
|
|
let lastErrors = [];
|
|
|
|
aiHelpBtn.disabled = true;
|
|
translateBtn.disabled = true;
|
|
|
|
function toggleTranslateVisibility() {
|
|
if (languageSelect.value === "jane") {
|
|
translateContainer.style.display = "flex";
|
|
translateOutput.style.display = "block";
|
|
} else {
|
|
translateContainer.style.display = "none";
|
|
translateOutput.style.display = "none";
|
|
}
|
|
}
|
|
|
|
toggleTranslateVisibility();
|
|
|
|
languageSelect.addEventListener("change", toggleTranslateVisibility);
|
|
|
|
analyzeBtn.addEventListener("click", function () {
|
|
let code = document.getElementById("codeInput").value;
|
|
let language = languageSelect.value;
|
|
|
|
let result = [];
|
|
|
|
try {
|
|
let checker;
|
|
switch (language) {
|
|
case "jane":
|
|
checker = checkJaneSyntax;
|
|
break;
|
|
case "c":
|
|
checker = checkCSyntax;
|
|
break;
|
|
case "python":
|
|
checker = checkPythonSyntax;
|
|
break;
|
|
case "java":
|
|
checker = checkJavaSyntax;
|
|
break;
|
|
default:
|
|
result.push("Unknown language selected");
|
|
}
|
|
|
|
if (checker) {
|
|
result = checker(code);
|
|
}
|
|
} catch (error) {
|
|
result.push("Error: " + error.message);
|
|
console.error(error);
|
|
}
|
|
|
|
if (result.length === 0 || result[0] === "Syntax is correct!") {
|
|
result = ["No errors found!"];
|
|
}
|
|
|
|
output.innerText = result.join("\n");
|
|
lastErrors = result;
|
|
|
|
aiHelpBtn.disabled = false;
|
|
translateBtn.disabled = false;
|
|
});
|
|
|
|
aiHelpBtn.addEventListener("click", function () {
|
|
aiHelpOutput.innerText = "";
|
|
|
|
if (lastErrors.length === 0 || lastErrors[0] === "No errors found!") {
|
|
typeText("There are no errors here, keep up the good work! If you encounter any errors in your new job, I will be happy to help you fix them.", aiHelpOutput);
|
|
return;
|
|
}
|
|
|
|
let helpText = [];
|
|
|
|
lastErrors.forEach(error => {
|
|
if (error.includes("Missing semicolon")) {
|
|
helpText.push("Error: Missing semicolon.\nFix: Add a semicolon at the end of the line.");
|
|
} else if (error.includes("Incorrect operator")) {
|
|
helpText.push("Error: Incorrect operator.\nFix: Use the correct operator, such as 'x++' or 'x--'.");
|
|
} else if (error.includes("Misspelled keyword")) {
|
|
helpText.push("Error: Misspelled keyword.\nFix: Check the spelling of the keyword.");
|
|
} else if (error.includes("Missing ':' after")) {
|
|
helpText.push("Error: Missing colon.\nFix: Add a colon after the keyword.");
|
|
} else {
|
|
helpText.push(`Error: ${error}\nFix: Review the syntax and correct the error.`);
|
|
}
|
|
});
|
|
|
|
typeText(helpText.join("\n\n"), aiHelpOutput);
|
|
});
|
|
|
|
translateBtn.addEventListener("click", function () {
|
|
if (lastErrors.length > 0 && lastErrors[0] !== "No errors found!") {
|
|
translateOutput.innerText = "It's impossible to translate incorrect code, fix the errors first.";
|
|
return;
|
|
}
|
|
|
|
let code = document.getElementById("codeInput").value;
|
|
let targetLanguage = document.getElementById("translateLanguageSelect").value;
|
|
let translatedCode = translateJaneToLanguage(code, targetLanguage);
|
|
translateOutput.innerText = translatedCode;
|
|
});
|
|
|
|
function typeText(text, element, speed = 30) {
|
|
let i = 0;
|
|
element.innerText = "";
|
|
function typing() {
|
|
if (i < text.length) {
|
|
element.innerText += text.charAt(i);
|
|
i++;
|
|
setTimeout(typing, speed);
|
|
}
|
|
}
|
|
typing();
|
|
}
|
|
|
|
function translateJaneToLanguage(code, targetLanguage) {
|
|
switch (targetLanguage) {
|
|
case "c":
|
|
return `// Translated to C\n#include <stdio.h>\n\nint main() {\n ${code.replace(/:=/g, "=")}\n return 0;\n}`;
|
|
case "python":
|
|
let pythonCode = code
|
|
.replace(/:=/g, "=")
|
|
.replace(/;\n/g, "\n")
|
|
.replace(/\bthen\b/g, "")
|
|
.replace(/\bdo\b/g, "")
|
|
.replace(/{/g, ":")
|
|
.replace(/}/g, "");
|
|
return `# Translated to Python\n${pythonCode}`;
|
|
case "java":
|
|
return `// Translated to Java\npublic class Main {\n public static void main(String[] args) {\n ${code.replace(/:=/g, "=")}\n }\n}`;
|
|
default:
|
|
return "Translation not supported for this language.";
|
|
}
|
|
}
|
|
});
|