variables.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @license
  3. * Visual Blocks Language
  4. *
  5. * Copyright 2012 Google Inc.
  6. * https://blockly.googlecode.com/
  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 OpenSCAD for variable blocks.
  22. * @author fraser@google.com (Neil Fraser)
  23. */
  24. 'use strict';
  25. goog.provide('Blockly.OpenSCAD.variables');
  26. goog.require('Blockly.OpenSCAD');
  27. Blockscad = Blockscad || {};
  28. Blockly.OpenSCAD['variables_get'] = function(block) {
  29. // Variable getter.
  30. var code = Blockly.OpenSCAD.variableDB_.getName(block.getFieldValue('VAR'),
  31. Blockly.Variables.NAME_TYPE);
  32. var commented_code = '//' + code;
  33. if (block.getParent())
  34. return [code, Blockly.OpenSCAD.ORDER_ATOMIC];
  35. else return [commented_code, Blockly.OpenSCAD.ORDER_ATOMIC];
  36. };
  37. Blockly.OpenSCAD['variables_set'] = function(block) {
  38. // Variable setter.
  39. var argument0 = Blockly.OpenSCAD.valueToCode(block, 'VALUE',
  40. Blockly.OpenSCAD.ORDER_ASSIGNMENT) || '0';
  41. var varName = Blockly.OpenSCAD.variableDB_.getName(
  42. block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
  43. // if the parent stack has a block that isn't a variables_set or a module definition,
  44. // then this is a hacked variable that needs to act like an assign.
  45. // it will be detected by its parent and will be encoded there.
  46. // this isn't right. I need to check to the nearest scope. if the first scope I hit is a transform or set-op,
  47. // instead of a module definition or nothing, then I want to kick out of this code.
  48. if (Blockscad.doVariableHack(block))
  49. return '';
  50. // this is a regular variable, just hanging out. Code it.
  51. return varName + ' = ' + argument0 + ';\n';
  52. };