compiler.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. function compile() {
  2. let operations = document.getElementsByName('mainOperationsType');
  3. let stringToBeCompiled = ``;
  4. let compiledJavascriptCode = $("#compiledJavascriptCode");
  5. // Cleaning compiled code area
  6. compiledJavascriptCode.html(``);
  7. // declaring variables
  8. compiledJavascriptCode.append(`//Declarando variáveis<br/>`);
  9. for (let i = 0; i < variables.length; i++) {
  10. stringToBeCompiled += `let ${variables[i].name} = ${getVariableTypeBarrier(variables[i].type)}${variables[i].value}${getVariableTypeBarrier(variables[i].type)};\n`;
  11. compiledJavascriptCode.append(`let ${variables[i].name} = ${getVariableTypeBarrier(variables[i].type)}${variables[i].value}${getVariableTypeBarrier(variables[i].type)}; <br/>`);
  12. }
  13. for (let i = 0; i < operations.length; i++) {
  14. switch (operations[i].value) {
  15. case mainCodeTypes.attribution: {
  16. console.log(operations[i].previousElementSibling.value);
  17. if (i !== 0) {
  18. stringToBeCompiled += `;`;
  19. }
  20. stringToBeCompiled += `\n${getVariableById(operations[i].previousElementSibling.value).name} = `;
  21. compiledJavascriptCode.append(`<br/>${getVariableById(operations[i].previousElementSibling.value).name} = `);
  22. break;
  23. }
  24. case mainCodeTypes.variable: {
  25. console.log(operations[i].previousElementSibling.value);
  26. stringToBeCompiled += `${getVariableById(operations[i].previousElementSibling.value).name} `;
  27. compiledJavascriptCode.append(`${getVariableById(operations[i].previousElementSibling.value).name} `);
  28. break;
  29. }
  30. case mainCodeTypes.operation: {
  31. let operation = getOperationById(operations[i].previousElementSibling.value);
  32. console.log(`${operation.firstVariable.name} ${operation.operation.operator} ${operation.secondVariable.name}`);
  33. stringToBeCompiled += `${operation.firstVariable.name} ${operation.operation.operator} ${operation.secondVariable.name} `;
  34. compiledJavascriptCode.append(`${operation.firstVariable.name} ${operation.operation.operator} ${operation.secondVariable.name} `);
  35. break;
  36. }
  37. case mainCodeTypes.operator: {
  38. console.log(operations[i].previousElementSibling.value);
  39. stringToBeCompiled += `${getOperationByValue(operations[i].previousElementSibling.value).operator} `;
  40. compiledJavascriptCode.append(`${getOperationByValue(operations[i].previousElementSibling.value).operator} `);
  41. break;
  42. }
  43. }
  44. }
  45. compiledJavascriptCode.append(`<br/><br/>//Exibindo variáveis`);
  46. for (let i = 0; i < variables.length; i++) {
  47. stringToBeCompiled += `\nalert("${variables[i].name} = " + ${variables[i].name});`;
  48. compiledJavascriptCode.append(`<br/>alert("${variables[i].name} = " + ${variables[i].name});`);
  49. }
  50. eval(stringToBeCompiled);
  51. }