not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.9 KiB

  1. //* comment *//
  2. Copyright (C) 2015 Ignacio Rodríguez
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. //* globals *//
  14. turtleHeaps = {};
  15. //* flow:showHeap *//
  16. var block = logo.blocks.blockList[blk];
  17. logo.textMsg(JSON.stringify(turtleHeaps[turtle]));
  18. //* flow:emptyHeap *//
  19. turtleHeaps[turtle] = [];
  20. //* flow:push *//
  21. var block = logo.blocks.blockList[blk];
  22. var conns = block.connections;
  23. var arg = logo.parseArg(logo, turtle, conns[1]);
  24. if (!(turtle in turtleHeaps)) {
  25. turtleHeaps[turtle] = [];
  26. }
  27. turtleHeaps[turtle].push(arg);
  28. //* arg:pop *//
  29. var block = logo.blocks.blockList[blk];
  30. if (turtle in turtleHeaps && turtleHeaps[turtle].length > 0) {
  31. block.value = turtleHeaps[turtle].pop();
  32. } else {
  33. logo.errorMsg(_('empty heap'));
  34. block.value = null;
  35. }
  36. //* arg:heapEmpty *//
  37. var block = logo.blocks.blockList[blk];
  38. if (turtle in turtleHeaps) {
  39. block.value = (turtleHeaps[turtle].length == 0);
  40. } else {
  41. block.value = true;
  42. }
  43. //* flow:saveHeap *//
  44. function downloadFile(filename, mimetype, content) {
  45. var download = document.createElement('a');
  46. download.setAttribute('href', 'data:' + mimetype + ';charset=utf-8,' + content);
  47. download.setAttribute('download', filename);
  48. document.body.appendChild(download);
  49. download.click();
  50. document.body.removeChild(download);
  51. };
  52. var block = logo.blocks.blockList[blk];
  53. var conns = block.connections;
  54. var arg = logo.parseArg(logo, turtle, conns[1]);
  55. if (arg && turtle in turtleHeaps) {
  56. downloadFile(arg, 'text/json', JSON.stringify(turtleHeaps[turtle]));
  57. };
  58. //* flow:loadHeap *//
  59. var block = logo.blocks.blockList[blk];
  60. var conns = block.connections;
  61. var arg = logo.parseArg(logo, turtle, conns[1]);
  62. if (turtle in turtleHeaps) {
  63. var oldHeap = turtleHeaps[turtle];
  64. } else {
  65. var oldHeap = [];
  66. }
  67. if (blocks.blockList[conns[1]].name == 'loadFile') {
  68. if (!arg) {
  69. logo.errorMsg(_('You need to select a file.'));
  70. }
  71. else {
  72. if (arg.length != 2) {
  73. logo.errorMsg(_('You need to select a file.'));
  74. }
  75. else {
  76. try {
  77. turtleHeaps[turtle] = JSON.parse(arg[1]);
  78. if (!Array.isArray(turtleHeaps[turtle])) {
  79. throw 'is not array';
  80. }
  81. }
  82. catch (e) {
  83. turtleHeaps[turtle] = oldHeap;
  84. logo.errorMsg(_('The file you selected does not contain a valid heap.'));
  85. }
  86. }
  87. }
  88. }
  89. else {
  90. logo.errorMsg(_('The loadHeap block needs a loadFile block.'))
  91. };
  92. //* block:push *//
  93. var pushBlk = new ProtoBlock('push');
  94. pushBlk.palette = palettes.dict['extras'];
  95. blocks.protoBlockDict['push'] = pushBlk;
  96. pushBlk.staticLabels.push(_('push'));
  97. pushBlk.adjustWidthToLabel();
  98. pushBlk.oneArgBlock();
  99. pushBlk.dockTypes[1] = 'anyin';
  100. //* block:pop *//
  101. var popBlk = new ProtoBlock('pop');
  102. popBlk.palette = palettes.dict['extras'];
  103. blocks.protoBlockDict['pop'] = popBlk;
  104. popBlk.staticLabels.push(_('pop'));
  105. popBlk.adjustWidthToLabel();
  106. popBlk.parameterBlock();
  107. //* block:showHeap *//
  108. var showHeap = new ProtoBlock('showHeap');
  109. showHeap.palette = palettes.dict['extras'];
  110. blocks.protoBlockDict['showHeap'] = showHeap;
  111. showHeap.staticLabels.push(_('show heap'));
  112. showHeap.adjustWidthToLabel();
  113. showHeap.zeroArgBlock();
  114. //* block:heapEmpty *//
  115. var heapEmpty = new ProtoBlock('heapEmpty');
  116. heapEmpty.palette = palettes.dict['extras'];
  117. blocks.protoBlockDict['heapEmpty'] = heapEmpty;
  118. heapEmpty.staticLabels.push(_('heap empty?'));
  119. heapEmpty.adjustWidthToLabel();
  120. heapEmpty.booleanZeroArgBlock();
  121. //* block:emptyHeap *//
  122. var emptyHeap = new ProtoBlock('emptyHeap');
  123. emptyHeap.palette = palettes.dict['extras'];
  124. blocks.protoBlockDict['emptyHeap'] = emptyHeap;
  125. emptyHeap.staticLabels.push(_('empty heap'));
  126. emptyHeap.adjustWidthToLabel();
  127. emptyHeap.zeroArgBlock();
  128. //* block:saveHeap *//
  129. var saveHeap = new ProtoBlock('saveHeap');
  130. saveHeap.palette = palettes.dict['extras'];
  131. blocks.protoBlockDict['saveHeap'] = saveHeap;
  132. saveHeap.staticLabels.push(_('save heap'));
  133. saveHeap.adjustWidthToLabel();
  134. saveHeap.oneArgBlock();
  135. saveHeap.defaults.push('heap.json');
  136. saveHeap.dockTypes[1] = 'textin';
  137. //* block:loadHeap *//
  138. var loadHeap = new ProtoBlock('loadHeap');
  139. loadHeap.palette = palettes.dict['extras'];
  140. blocks.protoBlockDict['loadHeap'] = loadHeap;
  141. loadHeap.staticLabels.push(_('load heap'));
  142. loadHeap.adjustWidthToLabel();
  143. loadHeap.oneArgBlock();
  144. loadHeap.dockTypes[1] = 'filein';
  145. loadHeap.defaults = [[null, null]];