dicts.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /**
  21. * @fileoverview Generating Python for dictionary blocks.
  22. * @author acbart@vt.edu (Austin Cory Bart)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Python.dicts');
  26. goog.require('Blockly.Python');
  27. Blockly.Python['dict_get'] = function(block) {
  28. var dict = Blockly.Python.valueToCode(block, 'DICT',
  29. Blockly.Python.ORDER_MEMBER) || '___';
  30. var value = Blockly.Python.valueToCode(block, 'ITEM',
  31. Blockly.Python.ORDER_NONE) || '___';
  32. var code = dict + '[' + value + ']';
  33. return [code, Blockly.Python.ORDER_ATOMIC];
  34. };
  35. Blockly.Python['dict_get_literal'] = function(block) {
  36. var dict = Blockly.Python.valueToCode(block, 'DICT',
  37. Blockly.Python.ORDER_MEMBER) || '___';
  38. var value = Blockly.Python.quote_(block.getFieldValue('ITEM'));
  39. var code = dict + '[' + value + ']';
  40. return [code, Blockly.Python.ORDER_ATOMIC];
  41. };
  42. Blockly.Python['dicts_create_with'] = function(block) {
  43. var value_keys = Blockly.Python.valueToCode(block, 'keys', Blockly. Python.ORDER_ATOMIC);
  44. // TODO: Assemble Python into code variable.
  45. var code = new Array(block.itemCount_);
  46. for (var n = 0; n < block.itemCount_; n++) {
  47. var key = Blockly.Python.quote_(block.getFieldValue('KEY' + n));
  48. var value = Blockly.Python.valueToCode(block, 'VALUE' + n,
  49. Blockly.Python.ORDER_NONE) || '___';
  50. code[n] = key +": "+ value;
  51. }
  52. code = '{' + code.join(', ') + '}';
  53. return [code, Blockly.Python.ORDER_ATOMIC];
  54. };
  55. Blockly.Python['dict_keys'] = function(block) {
  56. var dict = Blockly.Python.valueToCode(block, 'DICT',
  57. Blockly.Python.ORDER_MEMBER) || '___';
  58. var code = dict + '.keys()';
  59. return [code, Blockly.Python.ORDER_ATOMIC];
  60. };