<commit_before>/*
 *
 * Instituto Politécnico de Tomar
 * Fluxogram Executor - <NAME>
 *
 *
 */
"use strict";
class Executor {
    constructor(fluxogramManager) {
        this.auxDeclaredVaribales = [];
        this.fluxogramManager = fluxogramManager;
    }
    /**
     * Validates all fluxogram blocks according to their individual scope
     */
    validateFluxogram() {
        let finalState = true;
        this.fluxogramManager.blocks.forEach(element => {
            let isExpressionValid = true;
            if (element instanceof WriteBlock)
                isExpressionValid = this.validateWriteBlockExpression(element.previousConnector, element.instruction);
            if (element instanceof DefineBlock)
                isExpressionValid = this.validateVariablesForDefineBlock(element.previousConnector, element.type, element.instruction, element.value);
            if (element instanceof ExecuteBlock)
                isExpressionValid = this.validateExecuteBlockExpression(element.previousConnector, element.instruction);
            if (element instanceof WhileBlock || element instanceof DoWhileBlock)
                isExpressionValid = this.validateExpressionForContainerBlock(element.previousConnector, element.instruction);
            if (element instanceof IfBlock)
                isExpressionValid = this.validateExecuteBlockExpression(element.previousConnector, element.instruction);
            if (element instanceof ReadBlock)
                isExpressionValid = this.validateReadBlockVariable(element.previousConnector, element.instruction);
            if (!isExpressionValid) {
                finalState = false;
                element.executionState = ExecutionStateEnum.invalidBlock;
            }
            if (isExpressionValid) {
                element.executionState = "";
            }
        });
        this.fluxogramManager.shapeObjectManager.draw();
        return finalState;
    }
    /**
     * Method that validates write block expression
     */
    validateReadBlockVariable(line, instruction) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            eval(instruction);
            isExpressionValid = true;
        }
        catch (err) {
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err);
            isExpressionValid = false;
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    /**
     * Method that validates write block expression
     */
    validateWriteBlockExpression(line, instruction) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            //validate the write block expression
            var instanceType = typeof (eval(instruction));
            isExpressionValid = ('string' == instanceType ? true : false);
            if (!isExpressionValid) {
                //If it is a string 
                new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, "Invalid content for write block");
            }
        }
        catch (err) {
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err);
            isExpressionValid = false;
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    /**
     * Method that validates execute block expression
     */
    validateExecuteBlockExpression(line, instruction) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            //If the execution of the expression succeeds
            eval(instruction);
            isExpressionValid = true;
        }
        catch (err) {
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err);
            isExpressionValid = false;
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    /**
     * Method that validates define block expression
     */
    validateVariablesForDefineBlock(line, typeVar, name, value) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            eval(name);
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, "A variable with this name already exists!");
        }
        catch (err) {
            if (err.name == "ReferenceError") {
                isExpressionValid = this.declareNewVariable(typeVar, name, value);
                tempDeclaredVariables.push(name);
            }
            else {
                new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err);
            }
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    /**
     * Method that validates container block expression - While, If, DoWhile
     */
    validateExpressionForContainerBlock(line, originalInstruction) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            //Check if expression is boolean type
            isExpressionValid = typeof (eval(originalInstruction)) === "boolean";
            if (!isExpressionValid) {
                new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, "Invalid expression");
            }
        }
        catch (err) {
            isExpressionValid = false;
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err.message);
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    execute(line, velocity, declaredVariables) {
        if (this.outputConsole == undefined) {
            this.outputConsole = document.getElementById("internalOutputContentDiv");
            this.memoryConsole = document.getElementById("internalMemoryWidgetDiv");
        }
        this.auxDeclaredVaribales = declaredVariables;
        me = this;
        this.globalTimeOutId = window.setTimeout(this.executeFluxogram, velocity, line, velocity, declaredVariables);
    }
    stopExecution() {
        if (this.outputConsole == undefined) {
            this.outputConsole = document.getElementById("internalOutputContentDiv");
            this.memoryConsole = document.getElementById("internalMemoryWidgetDiv");
        }
        if (document.body.contains(document.getElementById("divPopInstructionContainer"))) {
            document.getElementById("divPopInstructionContainer").parentNode.removeChild(document.getElementById("divPopInstructionContainer"));
        }
        this.fluxogramManager.blocks.forEach(element => {
            element.executionState = "";
        });
        if (this.globalTimeOutId != null) {
            window.clearTimeout(this.globalTimeOutId);
            me.outputConsole.innerHTML += '<br><br> <span style="color:red;"> End Program <span>';
            this.globalTimeOutId = null;
        }
        this.fluxogramManager.shapeObjectManager.draw();
        this.destroyVariables(this.auxDeclaredVaribales);
        this.memoryConsole.innerHTML = '';
    }
    isInExecution() {
        if (this.globalTimeOutId != null) {
            return true;
        }
        else {
            return false;
        }
    }
    readFromReadBlock(variableName, value) {
        try {
            eval(variableName + " = " + value);
        }
        catch (err) {
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Heigth, err.message);
        }
    }
    /*
    *Private recursive method that executes the fluxogram
    */
    executeFluxogram(line, velocity, declaredVariables) {
        if (line.next == null) {<commit_msg>fixed bug with write blovk in executor.js<commit_after>/*
 *
 * Instituto Politécnico de Tomar
 * Fluxogram Executor - <NAME>
 *
 *
 */
"use strict";
class Executor {
    constructor(fluxogramManager) {
        this.auxDeclaredVaribales = [];
        this.fluxogramManager = fluxogramManager;
    }
    /**
     * Validates all fluxogram blocks according to their individual scope
     */
    validateFluxogram() {
        let finalState = true;
        this.fluxogramManager.blocks.forEach(element => {
            let isExpressionValid = true;
            if (element instanceof WriteBlock)
                isExpressionValid = this.validateWriteBlockExpression(element.previousConnector, element.instruction);
            if (element instanceof DefineBlock)
                isExpressionValid = this.validateVariablesForDefineBlock(element.previousConnector, element.type, element.instruction, element.value);
            if (element instanceof ExecuteBlock)
                isExpressionValid = this.validateExecuteBlockExpression(element.previousConnector, element.instruction);
            if (element instanceof WhileBlock || element instanceof DoWhileBlock)
                isExpressionValid = this.validateExpressionForContainerBlock(element.previousConnector, element.instruction);
            if (element instanceof IfBlock)
                isExpressionValid = this.validateExecuteBlockExpression(element.previousConnector, element.instruction);
            if (element instanceof ReadBlock)
                isExpressionValid = this.validateReadBlockVariable(element.previousConnector, element.instruction);
            if (!isExpressionValid) {
                finalState = false;
                element.executionState = ExecutionStateEnum.invalidBlock;
            }
            if (isExpressionValid) {
                element.executionState = "";
            }
        });
        this.fluxogramManager.shapeObjectManager.draw();
        return finalState;
    }
    /**
     * Method that validates write block expression
     */
    validateReadBlockVariable(line, instruction) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            eval(instruction);
            isExpressionValid = true;
        }
        catch (err) {
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err);
            isExpressionValid = false;
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    /**
     * Method that validates write block expression
     */
    validateWriteBlockExpression(line, instruction) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            //validate the write block expression
            var instanceType = typeof (eval(instruction));
            isExpressionValid = ('string' == instanceType ? true : false);
            if (!isExpressionValid) {
                //If it is a string 
                new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, "Invalid content for write block");
            }
        }
        catch (err) {
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err);
            isExpressionValid = false;
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    /**
     * Method that validates execute block expression
     */
    validateExecuteBlockExpression(line, instruction) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            //If the execution of the expression succeeds
            eval(instruction);
            isExpressionValid = true;
        }
        catch (err) {
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err);
            isExpressionValid = false;
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    /**
     * Method that validates define block expression
     */
    validateVariablesForDefineBlock(line, typeVar, name, value) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            eval(name);
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, "A variable with this name already exists!");
        }
        catch (err) {
            if (err.name == "ReferenceError") {
                isExpressionValid = this.declareNewVariable(typeVar, name, value);
                tempDeclaredVariables.push(name);
            }
            else {
                new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err);
            }
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    /**
     * Method that validates container block expression - While, If, DoWhile
     */
    validateExpressionForContainerBlock(line, originalInstruction) {
        var isExpressionValid = false;
        var tempDeclaredVariables = [];
        //Initialize all variables in scope
        tempDeclaredVariables = this.initializeVariablesInScope(line, tempDeclaredVariables);
        try {
            //Check if expression is boolean type
            isExpressionValid = typeof (eval(originalInstruction)) === "boolean";
            if (!isExpressionValid) {
                new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, "Invalid expression");
            }
        }
        catch (err) {
            isExpressionValid = false;
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Height, err.message);
        }
        finally {
            //Destroy all variables in scope
            this.destroyVariables(tempDeclaredVariables);
        }
        return isExpressionValid;
    }
    execute(line, velocity, declaredVariables) {
        if (this.outputConsole == undefined) {
            this.outputConsole = document.getElementById("internalOutputContentDiv");
            this.memoryConsole = document.getElementById("internalMemoryWidgetDiv");
        }
        this.auxDeclaredVaribales = declaredVariables;
        me = this;
        this.globalTimeOutId = window.setTimeout(this.executeFluxogram, velocity, line, velocity, declaredVariables);
    }
    stopExecution() {
        if (this.outputConsole == undefined) {
            this.outputConsole = document.getElementById("internalOutputContentDiv");
            this.memoryConsole = document.getElementById("internalMemoryWidgetDiv");
        }
        if (document.body.contains(document.getElementById("divPopInstructionContainer"))) {
            document.getElementById("divPopInstructionContainer").parentNode.removeChild(document.getElementById("divPopInstructionContainer"));
        }
        this.fluxogramManager.blocks.forEach(element => {
            element.executionState = "";
        });
        if (this.globalTimeOutId != null) {
            window.clearTimeout(this.globalTimeOutId);
            me.outputConsole.innerHTML += '<br><br> <span style="color:red;"> End Program <span>';
            this.globalTimeOutId = null;
        }
        this.fluxogramManager.shapeObjectManager.draw();
        this.destroyVariables(this.auxDeclaredVaribales);
        this.memoryConsole.innerHTML = '';
    }
    isInExecution() {
        if (this.globalTimeOutId != null) {
            return true;
        }
        else {
            return false;
        }
    }
    readFromReadBlock(variableName, value) {
        try {
            eval(variableName + " = " + value);
        }
        catch (err) {
            new ErrorPopUp(ErrorSuccessWarningPopUpSize.Width, ErrorSuccessWarningPopUpSize.Heigth, err.message);
        }
    }
    /*
    *Private recursive method that executes the fluxogram
    */
    executeFluxogram(line, velocity, declaredVariables) {
        debugger;
        if (line.next == null) {
{'fraction_non_alphanumeric': 0.08161214692419337, 'fraction_numerical': 0.0, 'mean_word_length': 2.3492241540474854, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1320053', 'n_tokens_mistral': 4219, 'n_tokens_neox': 4180, 'n_words': 909}