lists.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @license Licensed under the Apache License, Version 2.0 (the "License"):
  3. * http://www.apache.org/licenses/LICENSE-2.0
  4. */
  5. /**
  6. * @fileoverview Generating Arduino code for list blocks.
  7. *
  8. * TODO: A lot of this can be converted to arrays code by creating functions to
  9. * replicate this kind of behavior.
  10. */
  11. 'use strict';
  12. goog.provide('Blockly.Python.lists');
  13. goog.require('Blockly.Python');
  14. Blockly.Python['lists_create_with'] = function(block) {
  15. // Create a list with any number of elements of any type.
  16. var size = block.itemCount_;
  17. var code = new Array(this.itemCount_);
  18. for (var n = 0; n < this.itemCount_; n++) {
  19. code[n] = Blockly.Python.valueToCode(this, 'ADD' + n,
  20. Blockly.Python.ORDER_NONE) || '0';
  21. }
  22. var res_code = '{' + code.join(', ') + '}';
  23. return [res_code,Blockly.Python.ORDER_ATOMIC];
  24. };
  25. Blockly.Python['lists_create_with2'] = function(block) {
  26. // Create a list with any number of elements of any type.
  27. var size = block.itemCount_;
  28. var code = new Array(this.itemCount_);
  29. for (var n = 0; n < this.itemCount_; n++) {
  30. code[n] = Blockly.Python.valueToCode(this, 'ADD' + n,
  31. Blockly.Python.ORDER_NONE) || '"string"' ;
  32. }
  33. var res_code = '{' + code.join(', ') + '}';
  34. return [res_code,Blockly.Python.ORDER_ATOMIC];
  35. };
  36. //Blockly.Python['lists_repeat'] = Blockly.Python.noGeneratorCodeInline;
  37. Blockly.Python['lists_length'] = function(block) {
  38. var varName = Blockly.Python.valueToCode(this, 'VAR',
  39. Blockly.Python.ORDER_ATOMIC) || 'list';
  40. let code = 'sizeof('+varName+')/sizeof('+varName+'[0])';
  41. return [code, Blockly.Python.ORDER_ATOMIC];
  42. };
  43. //Blockly.Python['lists_isEmpty'] = Blockly.Python.noGeneratorCodeInline;
  44. //Blockly.Python['lists_indexOf'] = Blockly.Python.noGeneratorCodeInline;
  45. Blockly.Python['lists_getIndex'] = function(block) {
  46. // Indexing into a list is the same as indexing into a string.
  47. var varName = Blockly.Python.valueToCode(this, 'VAR',
  48. Blockly.Python.ORDER_ATOMIC) || 'list';
  49. var argument0 = Blockly.Python.valueToCode(this, 'AT',
  50. Blockly.Python.ORDER_ADDITIVE) || '0';
  51. if (argument0.match(/^\d+$/)) {
  52. // If the index is a naked number, decrement it right now.
  53. argument0 = parseInt(argument0, 10);
  54. } else {
  55. // If the index is dynamic, decrement it in code.
  56. //argument0 += ' - 1';
  57. }
  58. // console.log();
  59. var code = varName+'[(int)('+argument0+')]';
  60. return [code,Blockly.Python.ORDER_ATOMIC];
  61. };
  62. Blockly.Python['lists_setIndex'] = function(block) {
  63. // Set element at index.
  64. //checktip1 -- if can't success .check Blockly.Variables.NAME_TYPE
  65. var varName = Blockly.Python.valueToCode(this, 'VAR',
  66. Blockly.Python.ORDER_ATOMIC) || 'list';
  67. var argument0 = Blockly.Python.valueToCode(this, 'AT',
  68. Blockly.Python.ORDER_ADDITIVE) || '1';
  69. var argument1 = Blockly.Python.valueToCode(this, 'TO',
  70. Blockly.Python.ORDER_ASSIGNMENT) || '0';
  71. // Blockly uses one-based indicies.
  72. if (argument0.match(/^\d+$/)) {
  73. // If the index is a naked number, decrement it right now.
  74. argument0 = parseInt(argument0, 10);
  75. } else {
  76. // If the index is dynamic, decrement it in code.
  77. //argument0 += ' - 1';
  78. }
  79. return varName + '[(int)(' + argument0 + ')] = ' + argument1 + ';\n';
  80. };
  81. Blockly.Python['lists_variable_get'] = function(block) {
  82. var code = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
  83. Blockly.Variables.NAME_TYPE);
  84. return [code, Blockly.Python.ORDER_ATOMIC];
  85. };
  86. /**
  87. * Code generator for array variable (X) setter (Y).
  88. * Arduino code: type X;
  89. * loop { X = Y; }
  90. * @param {Blockly.Block} block Block to generate the code from.
  91. * @return {string} Completed code.
  92. */
  93. Blockly.Python['lists_variable_set'] = function(block) {
  94. var argument0 = Blockly.Python.valueToCode(block, 'VALUE',
  95. Blockly.Python.ORDER_ASSIGNMENT) || '{}';
  96. // console.log("generator_list -122- ");
  97. // console.log(block);
  98. // console.log("generator_list -122-end ");
  99. if (block.childBlocks_.length) {
  100. if (block.childBlocks_[0].type == "lists_create_with") {
  101. // console.log("test1");
  102. var varName = block.getFieldValue('VAR');
  103. var declName = 'create_int_list_' + varName;
  104. var decl = 'int '+ varName + '['+ this.getChildren()[0].itemCount_ +'] = ' + argument0 + ';\n';
  105. Blockly.Python.addDeclaration(declName, decl);
  106. return '';
  107. } else if (block.childBlocks_[0].type == "lists_create_with2"){
  108. // console.log("test2");
  109. var varName = block.getFieldValue('VAR');
  110. var declName = 'create_int_list_' + varName;
  111. var decl = 'String '+ varName + '['+ this.getChildren()[0].itemCount_ +'] = ' + argument0 + ';\n';
  112. Blockly.Python.addDeclaration(declName, decl);
  113. return '';
  114. }
  115. }
  116. var varName = Blockly.Python.variableDB_.getName(
  117. block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  118. var decl = 'String '+ varName + '[] = ' + argument0 + ';\n';
  119. Blockly.Python.addDeclaration('create_string_list', decl);
  120. return '';
  121. };