compiler.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. switch (operations[i].type) {
  32. case 'ASSIGN':
  33. code += ` ${operations[i].assignedVariable.name} <- `;
  34. for (let j = 0; j < operations[i].operators.length; j++) {
  35. switch (operations[i].operators[j].type.value) {
  36. case 'VARIABLE':
  37. code += operations[i].operators[j].variable.name;
  38. break;
  39. case 'VALUE':
  40. code += operations[i].operators[j].value;
  41. break;
  42. case 'OPERATOR':
  43. code += operations[i].operators[j].operator.name;
  44. break;
  45. }
  46. }
  47. break;
  48. case 'PRINT':
  49. code += ` escreva ("${operations[i].assignedVariable.name}: " + ${operations[i].assignedVariable.name} + "\\n")`;
  50. break;
  51. }
  52. code += `\n`;
  53. }
  54. code += ` }\n`;
  55. code += `}`;
  56. generatedCode.innerText = code;
  57. return code;
  58. }
  59. export function runCode () {
  60. if (isRunning) {
  61. return;
  62. }
  63. let strCode = generateCode();
  64. if (strCode == null) {
  65. return;
  66. }
  67. // toggleConsole(true);
  68. // if(domConsole == null)
  69. // domConsole = new DOMConsole("#ivprog-term");
  70. //$("#ivprog-term").slideDown(500);
  71. try {
  72. const data = SemanticAnalyser.analyseFromSource(strCode);
  73. proc = new IVProgProcessor(data);
  74. proc.registerInput(domConsole);
  75. proc.registerOutput(domConsole);
  76. $("#ivprog-term").addClass('ivprog-term-active');
  77. isRunning = true;
  78. proc.interpretAST().then(_ => {
  79. scheduleCall(() => {
  80. if (domConsole.pending_writes.length == 0) {
  81. if (proc.mode === Modes.ABORT) {
  82. domConsole.info(LocalizedStrings.getMessage("aborted_execution"));
  83. } else {
  84. domConsole.info(LocalizedStrings.getMessage("success_execution"));
  85. }
  86. $("#ivprog-term").removeClass('ivprog-term-active');
  87. isRunning = false;
  88. proc = null;
  89. showRunButton();
  90. return true;
  91. }
  92. return false;
  93. }, 100);
  94. }).catch(err => {
  95. scheduleCall(() => {
  96. if (domConsole.pending_writes.length == 0) {
  97. if (err instanceof Error) {
  98. domConsole.err(err.message);
  99. } else {
  100. domConsole.err(err);
  101. }
  102. // $("#ivprog-term").removeClass('ivprog-term-active');
  103. isRunning = false;
  104. proc = null;
  105. showRunButton();
  106. return true;
  107. }
  108. return false;
  109. }, 100);
  110. })
  111. } catch (error) {
  112. scheduleCall(() => {
  113. if (domConsole.pending_writes.length == 0) {
  114. isRunning = false;
  115. proc = null;
  116. showRunButton();
  117. domConsole.err(error.message);
  118. console.log(error);
  119. return true;
  120. }
  121. return false;
  122. }, 100);
  123. }
  124. }
  125. // ***********************************************************************
  126. function scheduleCall (callback, time) {
  127. const scheduled_call = () => {
  128. const id = setInterval(() => {
  129. if (callback()) {
  130. clearInterval(id);
  131. }
  132. }, time)
  133. }
  134. scheduled_call();
  135. }
  136. // ***********************************************************************
  137. function showRunButton () {
  138. runCodePlayIcon.style.display = 'inline';
  139. runCodeStopIcon.style.display = 'none';
  140. }
  141. function showStopButton () {
  142. runCodePlayIcon.style.display = 'none';
  143. runCodeStopIcon.style.display = 'inline';
  144. }
  145. // ***********************************************************************