compiler.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Imports
  2. import {terminal} from "./main";
  3. import {variables} from "./modules/variables/variables";
  4. import {operations} from "./modules/operations/operations";
  5. import * as TextEditor from "../visualUI/text_editor";
  6. import * as CodeManagement from "../visualUI/code_generator";
  7. import {SemanticAnalyser} from "../processor/semantic/semanticAnalyser";
  8. import {IVProgProcessor} from "../processor/ivprogProcessor";
  9. import {Modes} from "../processor/modes";
  10. import {LocalizedStrings} from "../services/localizedStringsService";
  11. import {DOMConsole} from "../io/domConsole";
  12. // Imports
  13. // iVProg variables
  14. let proc = null;
  15. let isRunning = false;
  16. let domConsole = new DOMConsole("terminal");
  17. //
  18. const runCodePlayIcon = document.getElementById('runCodePlayIcon');
  19. const runCodeStopIcon = document.getElementById('runCodeStopIcon');
  20. //Debug
  21. const generatedCode = document.getElementById('generatedCode');
  22. function generateCode() {
  23. console.log(variables);
  24. console.log(operations);
  25. let code = `programa {\n`;
  26. for (let i = 0; i < variables.length; i++) {
  27. code += ` ${variables[i].type.name} ${variables[i].name} <- ${variables[i].type.barrier}${variables[i].value}${variables[i].type.barrier}\n`;
  28. }
  29. code += ` funcao vazio inicio () {\n`;
  30. for (let i = 0; i < operations.length; i++) {
  31. code += ` ${operations[i].assignedVariable.name} <- `;
  32. for (let j = 0; j < operations[i].operators.length; j++) {
  33. switch (operations[i].operators[j].type.value) {
  34. case 'VARIABLE':
  35. code += operations[i].operators[j].variable.name;
  36. break;
  37. case 'VALUE':
  38. code += operations[i].operators[j].value;
  39. break;
  40. case 'OPERATOR':
  41. code += operations[i].operators[j].operator.name;
  42. break;
  43. }
  44. }
  45. code += `\n`;
  46. }
  47. for (let i = 0; i < variables.length; i++) {
  48. code += ` escreva ("${variables[i].name}: " + ${variables[i].name} + "\\n")\n`;
  49. }
  50. code += ` }\n`;
  51. code += `}`;
  52. generatedCode.innerText = code;
  53. return code;
  54. }
  55. export function runCode() {
  56. if (isRunning) {
  57. return;
  58. }
  59. let strCode = generateCode();
  60. if (strCode == null) {
  61. return;
  62. }
  63. // toggleConsole(true);
  64. // if(domConsole == null)
  65. // domConsole = new DOMConsole("#ivprog-term");
  66. //$("#ivprog-term").slideDown(500);
  67. try {
  68. const data = SemanticAnalyser.analyseFromSource(strCode);
  69. proc = new IVProgProcessor(data);
  70. proc.registerInput(domConsole);
  71. proc.registerOutput(domConsole);
  72. $("#ivprog-term").addClass('ivprog-term-active');
  73. isRunning = true;
  74. proc.interpretAST().then(_ => {
  75. scheduleCall(() => {
  76. if (domConsole.pending_writes.length == 0) {
  77. if (proc.mode === Modes.ABORT) {
  78. domConsole.info(LocalizedStrings.getMessage("aborted_execution"));
  79. } else {
  80. domConsole.info(LocalizedStrings.getMessage("success_execution"));
  81. }
  82. $("#ivprog-term").removeClass('ivprog-term-active');
  83. isRunning = false;
  84. proc = null;
  85. showRunButton();
  86. return true;
  87. }
  88. return false;
  89. }, 100);
  90. }).catch(err => {
  91. scheduleCall(() => {
  92. if (domConsole.pending_writes.length == 0) {
  93. if (err instanceof Error) {
  94. domConsole.err(err.message);
  95. } else {
  96. domConsole.err(err);
  97. }
  98. // $("#ivprog-term").removeClass('ivprog-term-active');
  99. isRunning = false;
  100. proc = null;
  101. showRunButton();
  102. return true;
  103. }
  104. return false;
  105. }, 100);
  106. })
  107. } catch (error) {
  108. scheduleCall(() => {
  109. if (domConsole.pending_writes.length == 0) {
  110. isRunning = false;
  111. proc = null;
  112. showRunButton();
  113. domConsole.err(error.message);
  114. console.log(error);
  115. return true;
  116. }
  117. return false;
  118. }, 100);
  119. }
  120. }
  121. // ***********************************************************************
  122. function scheduleCall(callback, time) {
  123. const scheduled_call = () => {
  124. const id = setInterval(() => {
  125. if (callback()) {
  126. clearInterval(id);
  127. }
  128. }, time)
  129. }
  130. scheduled_call();
  131. }
  132. // ***********************************************************************
  133. function showRunButton() {
  134. runCodePlayIcon.style.display = 'inline';
  135. runCodeStopIcon.style.display = 'none';
  136. }
  137. function showStopButton() {
  138. runCodePlayIcon.style.display = 'none';
  139. runCodeStopIcon.style.display = 'inline';
  140. }
  141. // ***********************************************************************