<commit_before>/*
    blockly_blocks:
        Block generation and configuration logic for the Blockly mode
*/

// Blockly to Scratch translations
var blocklyToScratch = {
   singleBlocks: {
      'controls_if': ['control_if'],
      'controls_if_else': ['control_if_else'],
      'controls_repeat': ['control_repeat'],
      'controls_repeat_ext': ['control_repeat'],
      'controls_whileUntil': ['control_repeat_until'],
      'controls_untilWhile': ['control_repeat_until'],
      'lists_repeat': ['data_listrepeat'],
      'lists_create_with_empty': [], // Scratch logic is not to initialize
      'lists_getIndex': ['data_itemoflist'],
      'lists_setIndex': ['data_replaceitemoflist'],
      'logic_negate': ['operator_not'],
      'logic_boolean': [],
      'logic_compare': ['operator_equals', 'operator_gt', 'operator_lt'],
      'logic_operation': ['operator_and', 'operator_or'],
      'text_join': ['operator_join'],
      'math_arithmetic': ['operator_add', 'operator_subtract', 'operator_multiply', 'operator_divide'],
      'math_change': ['data_changevariableby'],
      'math_number': [],
      'variables_get': ['data_variable'],
      'variables_set': ['data_setvariableto'],
    }
};

function getBlocklyBlockFunctions(maxBlocks, nbTestCases) {
   // TODO :: completely split the logic so it can be a separate object

   return {
      allBlocksAllowed: [],
      getCodeFromXml: function(xmlText, language) {
         try {
           var xml = Blockly.Xml.textToDom(xmlText)
         } catch (e) {
           alert(e);
           return;
         }

         // Remove statement prefix (highlightBlock)
         var statementPrefix = Blockly.JavaScript.STATEMENT_PREFIX;
         Blockly.JavaScript.STATEMENT_PREFIX = '';

         // New workspaces need options, else they can give unpredictable results
         var tmpOptions = new Blockly.Options({});
         var tmpWorkspace = new Blockly.Workspace(tmpOptions);
         if(this.scratchMode) {
            // Make sure it has the right information from this blocklyHelper
            tmpWorkspace.maxBlocks = function () { return maxBlocks; };
         }
         Blockly.Xml.domToWorkspace(xml, tmpWorkspace);
         var code = this.getCode(language, tmpWorkspace);

         Blockly.JavaScript.STATEMENT_PREFIX = statementPrefix;
         return code;
      },

      getCode: function(language, codeWorkspace) {
         if (codeWorkspace == undefined) {
            codeWorkspace = this.workspace;
         }
         if(codeWorkspace.remainingCapacity(this.maxBlocks) < 0) {
            // Safeguard: avoid generating code when we use too many blocks
            return 'throw "'+this.strings.tooManyBlocks+'";';
         }
         var blocks = codeWorkspace.getTopBlocks(true);
         var languageObj = null;
         if (language == "javascript") {
            languageObj = Blockly.JavaScript;
         }
         if (language == "python") {
            languageObj = Blockly.Python;
         }
         languageObj.init(codeWorkspace);

         var code = [];
         var comments = [];
         for (var b = 0; b < blocks.length; b++) {
            var block = blocks[b];
            var blockCode = languageObj.blockToCode(block);
            if (arrayContains(["procedures_defnoreturn", "procedures_defreturn"], block.type)) {
               // For function blocks, the code is stored in languageObj.definitions_
            } else {
               if (block.type == "robot_start" || !this.startingBlock) {
                  comments.push(blockCode);
               }
            }
         }

         for (var def in languageObj.definitions_) {
            code.push(languageObj.definitions_[def]);
         }

         var code = code.join("\n");
         code += comments.join("\n");
         return code;
      },

      completeBlockHandler: function(block, objectName, context) {
         if (typeof block.handler == "undefined") {
            block.handler = context[objectName][block.name];
         }


         if (typeof block.handler == "undefined") {
            block.handler = (function(oName, bName) {<commit_msg>quickAlgo-Blockly: fix newline in code generation<commit_after>/*
    blockly_blocks:
        Block generation and configuration logic for the Blockly mode
*/

// Blockly to Scratch translations
var blocklyToScratch = {
   singleBlocks: {
      'controls_if': ['control_if'],
      'controls_if_else': ['control_if_else'],
      'controls_repeat': ['control_repeat'],
      'controls_repeat_ext': ['control_repeat'],
      'controls_whileUntil': ['control_repeat_until'],
      'controls_untilWhile': ['control_repeat_until'],
      'lists_repeat': ['data_listrepeat'],
      'lists_create_with_empty': [], // Scratch logic is not to initialize
      'lists_getIndex': ['data_itemoflist'],
      'lists_setIndex': ['data_replaceitemoflist'],
      'logic_negate': ['operator_not'],
      'logic_boolean': [],
      'logic_compare': ['operator_equals', 'operator_gt', 'operator_lt'],
      'logic_operation': ['operator_and', 'operator_or'],
      'text_join': ['operator_join'],
      'math_arithmetic': ['operator_add', 'operator_subtract', 'operator_multiply', 'operator_divide'],
      'math_change': ['data_changevariableby'],
      'math_number': [],
      'variables_get': ['data_variable'],
      'variables_set': ['data_setvariableto'],
    }
};

function getBlocklyBlockFunctions(maxBlocks, nbTestCases) {
   // TODO :: completely split the logic so it can be a separate object

   return {
      allBlocksAllowed: [],
      getCodeFromXml: function(xmlText, language) {
         try {
           var xml = Blockly.Xml.textToDom(xmlText)
         } catch (e) {
           alert(e);
           return;
         }

         // Remove statement prefix (highlightBlock)
         var statementPrefix = Blockly.JavaScript.STATEMENT_PREFIX;
         Blockly.JavaScript.STATEMENT_PREFIX = '';

         // New workspaces need options, else they can give unpredictable results
         var tmpOptions = new Blockly.Options({});
         var tmpWorkspace = new Blockly.Workspace(tmpOptions);
         if(this.scratchMode) {
            // Make sure it has the right information from this blocklyHelper
            tmpWorkspace.maxBlocks = function () { return maxBlocks; };
         }
         Blockly.Xml.domToWorkspace(xml, tmpWorkspace);
         var code = this.getCode(language, tmpWorkspace);

         Blockly.JavaScript.STATEMENT_PREFIX = statementPrefix;
         return code;
      },

      getCode: function(language, codeWorkspace) {
         if (codeWorkspace == undefined) {
            codeWorkspace = this.workspace;
         }
         if(codeWorkspace.remainingCapacity(this.maxBlocks) < 0) {
            // Safeguard: avoid generating code when we use too many blocks
            return 'throw "'+this.strings.tooManyBlocks+'";';
         }
         var blocks = codeWorkspace.getTopBlocks(true);
         var languageObj = null;
         if (language == "javascript") {
            languageObj = Blockly.JavaScript;
         }
         if (language == "python") {
            languageObj = Blockly.Python;
         }
         languageObj.init(codeWorkspace);

         var code = [];
         var comments = [];
         for (var b = 0; b < blocks.length; b++) {
            var block = blocks[b];
            var blockCode = languageObj.blockToCode(block);
            if (arrayContains(["procedures_defnoreturn", "procedures_defreturn"], block.type)) {
               // For function blocks, the code is stored in languageObj.definitions_
            } else {
               if (block.type == "robot_start" || !this.startingBlock) {
                  comments.push(blockCode);
               }
            }
         }

         for (var def in languageObj.definitions_) {
            code.push(languageObj.definitions_[def]);
         }

         var code = code.join("\n");
         code += "\n";
         code += comments.join("\n");
         return code;
      },

      completeBlockHandler: function(block, objectName, context) {
         if (typeof block.handler == "undefined") {
            block.handler = context[objectName][block.name];
         }


         if (typeof block.handler == "undefined") {
            block.handler = (function(oName, bName) {
{'fraction_non_alphanumeric': 0.11972592859718716, 'fraction_numerical': 0.0004808270224786633, 'mean_word_length': 2.3959183673469386, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1062940', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2241, 'n_words': 511}