compiler.js 5.5 KB

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