dicts.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 Pseudo for dictionary blocks.
  22. * @author acbart@vt.edu (Austin Cory Bart)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.Pseudo.dicts');
  26. goog.require('Blockly.Pseudo');
  27. Blockly.Pseudo['dict_get'] = function(block) {
  28. var dict = Blockly.Pseudo.valueToCode(block, 'DICT',
  29. Blockly.Pseudo.ORDER_MEMBER) || '___';
  30. var value = Blockly.Pseudo.valueToCode(block, 'ITEM',
  31. Blockly.Pseudo.ORDER_NONE) || '___';
  32. var code = " the value of the key " + value + " of " +dict;
  33. return [code, Blockly.Pseudo.ORDER_ATOMIC];
  34. };
  35. Blockly.Pseudo['dict_get_literal'] = function(block) {
  36. var dict = Blockly.Pseudo.valueToCode(block, 'DICT',
  37. Blockly.Pseudo.ORDER_MEMBER) || '___';
  38. var value = Blockly.Pseudo.quote_(block.getFieldValue('ITEM'));
  39. var code = " the value of the key " + value + " of " +dict;
  40. return [code, Blockly.Pseudo.ORDER_ATOMIC];
  41. };
  42. Blockly.Pseudo['dicts_create_with'] = function(block) {
  43. var value_keys = Blockly.Pseudo.valueToCode(block, 'keys', Blockly. Pseudo.ORDER_ATOMIC);
  44. // TODO: Assemble Pseudo into code variable.
  45. var code = new Array(block.itemCount_);
  46. for (var n = 0; n < block.itemCount_; n++) {
  47. var key = Blockly.Pseudo.quote_(block.getFieldValue('KEY' + n));
  48. var value = Blockly.Pseudo.valueToCode(block, 'VALUE' + n,
  49. Blockly.Pseudo.ORDER_NONE) || '___';
  50. code[n] = key +": "+ value;
  51. }
  52. code = '{' + code.join(', ') + '}';
  53. return [code, Blockly.Pseudo.ORDER_ATOMIC];
  54. };
  55. Blockly.Pseudo['dict_keys'] = function(block) {
  56. var dict = Blockly.Pseudo.valueToCode(block, 'DICT',
  57. Blockly.Pseudo.ORDER_MEMBER) || '___';
  58. var code = dict + '.keys()';
  59. return [code, Blockly.Pseudo.ORDER_ATOMIC];
  60. };