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

//* comment *//
Copyright (C) 2015 Ignacio Rodríguez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
//* globals *//
turtleHeaps = {};
//* flow:showHeap *//
var block = logo.blocks.blockList[blk];
logo.textMsg(JSON.stringify(turtleHeaps[turtle]));
//* flow:emptyHeap *//
turtleHeaps[turtle] = [];
//* flow:push *//
var block = logo.blocks.blockList[blk];
var conns = block.connections;
var arg = logo.parseArg(logo, turtle, conns[1]);
if (!(turtle in turtleHeaps)) {
turtleHeaps[turtle] = [];
}
turtleHeaps[turtle].push(arg);
//* arg:pop *//
var block = logo.blocks.blockList[blk];
if (turtle in turtleHeaps && turtleHeaps[turtle].length > 0) {
block.value = turtleHeaps[turtle].pop();
} else {
logo.errorMsg(_('empty heap'));
block.value = null;
}
//* arg:heapEmpty *//
var block = logo.blocks.blockList[blk];
if (turtle in turtleHeaps) {
block.value = (turtleHeaps[turtle].length == 0);
} else {
block.value = true;
}
//* flow:saveHeap *//
function downloadFile(filename, mimetype, content) {
var download = document.createElement('a');
download.setAttribute('href', 'data:' + mimetype + ';charset=utf-8,' + content);
download.setAttribute('download', filename);
document.body.appendChild(download);
download.click();
document.body.removeChild(download);
};
var block = logo.blocks.blockList[blk];
var conns = block.connections;
var arg = logo.parseArg(logo, turtle, conns[1]);
if (arg && turtle in turtleHeaps) {
downloadFile(arg, 'text/json', JSON.stringify(turtleHeaps[turtle]));
};
//* flow:loadHeap *//
var block = logo.blocks.blockList[blk];
var conns = block.connections;
var arg = logo.parseArg(logo, turtle, conns[1]);
if (turtle in turtleHeaps) {
var oldHeap = turtleHeaps[turtle];
} else {
var oldHeap = [];
}
if (blocks.blockList[conns[1]].name == 'loadFile') {
if (!arg) {
logo.errorMsg(_('You need to select a file.'));
}
else {
if (arg.length != 2) {
logo.errorMsg(_('You need to select a file.'));
}
else {
try {
turtleHeaps[turtle] = JSON.parse(arg[1]);
if (!Array.isArray(turtleHeaps[turtle])) {
throw 'is not array';
}
}
catch (e) {
turtleHeaps[turtle] = oldHeap;
logo.errorMsg(_('The file you selected does not contain a valid heap.'));
}
}
}
}
else {
logo.errorMsg(_('The loadHeap block needs a loadFile block.'))
};
//* block:push *//
var pushBlk = new ProtoBlock('push');
pushBlk.palette = palettes.dict['extras'];
blocks.protoBlockDict['push'] = pushBlk;
pushBlk.staticLabels.push(_('push'));
pushBlk.adjustWidthToLabel();
pushBlk.oneArgBlock();
pushBlk.dockTypes[1] = 'anyin';
//* block:pop *//
var popBlk = new ProtoBlock('pop');
popBlk.palette = palettes.dict['extras'];
blocks.protoBlockDict['pop'] = popBlk;
popBlk.staticLabels.push(_('pop'));
popBlk.adjustWidthToLabel();
popBlk.parameterBlock();
//* block:showHeap *//
var showHeap = new ProtoBlock('showHeap');
showHeap.palette = palettes.dict['extras'];
blocks.protoBlockDict['showHeap'] = showHeap;
showHeap.staticLabels.push(_('show heap'));
showHeap.adjustWidthToLabel();
showHeap.zeroArgBlock();
//* block:heapEmpty *//
var heapEmpty = new ProtoBlock('heapEmpty');
heapEmpty.palette = palettes.dict['extras'];
blocks.protoBlockDict['heapEmpty'] = heapEmpty;
heapEmpty.staticLabels.push(_('heap empty?'));
heapEmpty.adjustWidthToLabel();
heapEmpty.booleanZeroArgBlock();
//* block:emptyHeap *//
var emptyHeap = new ProtoBlock('emptyHeap');
emptyHeap.palette = palettes.dict['extras'];
blocks.protoBlockDict['emptyHeap'] = emptyHeap;
emptyHeap.staticLabels.push(_('empty heap'));
emptyHeap.adjustWidthToLabel();
emptyHeap.zeroArgBlock();
//* block:saveHeap *//
var saveHeap = new ProtoBlock('saveHeap');
saveHeap.palette = palettes.dict['extras'];
blocks.protoBlockDict['saveHeap'] = saveHeap;
saveHeap.staticLabels.push(_('save heap'));
saveHeap.adjustWidthToLabel();
saveHeap.oneArgBlock();
saveHeap.defaults.push('heap.json');
saveHeap.dockTypes[1] = 'textin';
//* block:loadHeap *//
var loadHeap = new ProtoBlock('loadHeap');
loadHeap.palette = palettes.dict['extras'];
blocks.protoBlockDict['loadHeap'] = loadHeap;
loadHeap.staticLabels.push(_('load heap'));
loadHeap.adjustWidthToLabel();
loadHeap.oneArgBlock();
loadHeap.dockTypes[1] = 'filein';
loadHeap.defaults = [[null, null]];