variables.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 variables blocks.
  7. */
  8. 'use strict';
  9. goog.provide('Blockly.Python.variables');
  10. goog.require('Blockly.Python');
  11. /**
  12. * Code generator for variable (X) getter.
  13. * Arduino code: loop { X }
  14. * @param {Blockly.Block} block Block to generate the code from.
  15. * @return {array} Completed code with order of operation.
  16. */
  17. Blockly.Python['variables_get'] = function(block) {
  18. var code = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
  19. Blockly.Variables.NAME_TYPE);
  20. return [code, Blockly.Python.ORDER_ATOMIC];
  21. };
  22. /**
  23. * Code generator for variable (X) setter (Y).
  24. * Arduino code: type X;
  25. * loop { X = Y; }
  26. * @param {Blockly.Block} block Block to generate the code from.
  27. * @return {string} Completed code.
  28. */
  29. Blockly.Python['variables_set'] = function(block) {
  30. var argument0 = Blockly.Python.valueToCode(block, 'VALUE',
  31. Blockly.Python.ORDER_ASSIGNMENT) || '0';
  32. //console.log(block.getBlockType());
  33. //console.log(block.childBlocks_[0].type);
  34. if (block.childBlocks_.length) {
  35. if (block.childBlocks_[0].type == "lists_create_with") {
  36. // console.log("test1");
  37. var varName = block.getFieldValue('VAR');
  38. var decl = 'int '+ varName + '[] = ' + argument0 + ';\n';
  39. Blockly.Python.addDeclaration('create_int_list', decl);
  40. return '';
  41. } else if (block.childBlocks_[0].type == "lists_create_with2"){
  42. // console.log("test2");
  43. var varName = block.getFieldValue('VAR');
  44. var decl = 'String '+ varName + '[] = ' + argument0 + ';\n';
  45. Blockly.Python.addDeclaration('create_string_list', decl);
  46. return '';
  47. } else {
  48. // console.log('test3');
  49. var varName = Blockly.Python.variableDB_.getName(
  50. block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  51. //var varName = block.getFieldValue('VAR');
  52. return varName + ' = ' + argument0 + ';\n';
  53. }
  54. }
  55. // var argument0_Type = block.getBlockType();
  56. var varName = Blockly.Python.variableDB_.getName(
  57. block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  58. //var varName = block.getFieldValue('VAR');
  59. return varName + ' = ' + argument0 + ';\n';
  60. };
  61. /**
  62. * Code generator for variable (X) csasting (Y).
  63. * Arduino code: loop { (Y)X }
  64. * @param {Blockly.Block} block Block to generate the code from.
  65. * @return {array} Completed code with order of operation.
  66. */
  67. Blockly.Python['variables_set_type'] = function(block) {
  68. var argument0 = Blockly.Python.valueToCode(block, 'VARIABLE_SETTYPE_INPUT',
  69. Blockly.Python.ORDER_ASSIGNMENT) || '0';
  70. var varType = Blockly.Python.getArduinoType_(
  71. Blockly.Types[block.getFieldValue('VARIABLE_SETTYPE_TYPE')]);
  72. var code = '(' + varType + ')(' + argument0 + ')';
  73. return [code, Blockly.Python.ORDER_ATOMIC];
  74. };