123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- // Imports
- import {terminal} from "./main";
- import {variables} from "./modules/variables/variables";
- import {operations} from "./modules/operations/operations";
- import * as TextEditor from "../visualUI/text_editor";
- import * as CodeManagement from "../visualUI/code_generator";
- import {SemanticAnalyser} from "../processor/semantic/semanticAnalyser";
- import {IVProgProcessor} from "../processor/ivprogProcessor";
- import {Modes} from "../processor/modes";
- import {LocalizedStrings} from "../services/localizedStringsService";
- import {DOMConsole} from "../io/domConsole";
- // Imports
- // iVProg variables
- let proc = null;
- let isRunning = false;
- let domConsole = new DOMConsole("terminal");
- //
- const ivprogConsoleTitleSpan = document.getElementById('ivprog-console-title-span');
- const runCodePlayIcon = document.getElementById('runCodePlayIcon');
- const runCodeStopIcon = document.getElementById('runCodeStopIcon');
- //Debug
- const generatedCode = document.getElementById('generatedCode');
- function generateCode () {
- console.log(variables);
- console.log(operations);
- let code = `programa {\n`;
- for (let i = 0; i < variables.length; i++) {
- code += ` ${variables[i].type.name} ${variables[i].name} <- ${variables[i].type.barrier}${variables[i].value}${variables[i].type.barrier}\n`;
- }
- code += ` funcao vazio inicio () {\n`;
- for (let i = 0; i < operations.length; i++) {
- switch (operations[i].type) {
- case 'ASSIGN':
- code += ` ${operations[i].assignedVariable.name} <- `;
- for (let j = 0; j < operations[i].operators.length; j++) {
- switch (operations[i].operators[j].type.value) {
- case 'VARIABLE':
- code += operations[i].operators[j].variable.name;
- break;
- case 'VALUE':
- code += operations[i].operators[j].value;
- break;
- case 'OPERATOR':
- code += operations[i].operators[j].operator.name;
- break;
- }
- }
- break;
- case 'PRINT':
- code += ` escreva ("${operations[i].assignedVariable.name}: " + ${operations[i].assignedVariable.name} + "\\n")`;
- break;
- }
- code += `\n`;
- }
- code += ` }\n`;
- code += `}`;
- generatedCode.innerText = code;
- return code;
- }
- export function runCode () {
- if (isRunning) {
- return;
- }
- let strCode = generateCode();
- if (strCode == null) {
- return;
- }
- // toggleConsole(true);
- // if(domConsole == null)
- // domConsole = new DOMConsole("#ivprog-term");
- //$("#ivprog-term").slideDown(500);
- try {
- const data = SemanticAnalyser.analyseFromSource(strCode);
- proc = new IVProgProcessor(data);
- proc.registerInput(domConsole);
- proc.registerOutput(domConsole);
- $("#ivprog-term").addClass('ivprog-term-active');
- isRunning = true;
- proc.interpretAST().then(_ => {
- scheduleCall(() => {
- if (domConsole.pending_writes.length == 0) {
- if (proc.mode === Modes.ABORT) {
- domConsole.info(LocalizedStrings.getMessage("aborted_execution"));
- } else {
- domConsole.info(LocalizedStrings.getMessage("success_execution"));
- }
- $("#ivprog-term").removeClass('ivprog-term-active');
- isRunning = false;
- proc = null;
- showRunButton();
- ivprogConsoleTitleSpan.focus();
- return true;
- }
- return false;
- }, 100);
- }).catch(err => {
- scheduleCall(() => {
- if (domConsole.pending_writes.length == 0) {
- if (err instanceof Error) {
- domConsole.err(err.message);
- } else {
- domConsole.err(err);
- }
- // $("#ivprog-term").removeClass('ivprog-term-active');
- isRunning = false;
- proc = null;
- showRunButton();
- return true;
- }
- return false;
- }, 100);
- })
- } catch (error) {
- scheduleCall(() => {
- if (domConsole.pending_writes.length == 0) {
- isRunning = false;
- proc = null;
- showRunButton();
- domConsole.err(error.message);
- console.log(error);
- return true;
- }
- return false;
- }, 100);
- }
- }
- // ***********************************************************************
- function scheduleCall (callback, time) {
- const scheduled_call = () => {
- const id = setInterval(() => {
- if (callback()) {
- clearInterval(id);
- }
- }, time)
- }
- scheduled_call();
- }
- // ***********************************************************************
- function showRunButton () {
- runCodePlayIcon.style.display = 'inline';
- runCodeStopIcon.style.display = 'none';
- }
- function showStopButton () {
- runCodePlayIcon.style.display = 'none';
- runCodeStopIcon.style.display = 'inline';
- }
- // ***********************************************************************
|