mirror of
https://github.com/jjrobots/B-ROBOT_EVO2.git
synced 2026-03-09 19:36:50 +01:00
New version of BROBOT EVO2
This is the first oficial version of BROBOT EVO2. Enjoy!
This commit is contained in:
317
Blockly/brobot/generators/javascript.js
Normal file
317
Blockly/brobot/generators/javascript.js
Normal file
@@ -0,0 +1,317 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Helper functions for generating JavaScript for blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript');
|
||||
|
||||
goog.require('Blockly.Generator');
|
||||
|
||||
|
||||
/**
|
||||
* JavaScript code generator.
|
||||
* @type {!Blockly.Generator}
|
||||
*/
|
||||
Blockly.JavaScript = new Blockly.Generator('JavaScript');
|
||||
|
||||
/**
|
||||
* List of illegal variable names.
|
||||
* This is not intended to be a security feature. Blockly is 100% client-side,
|
||||
* so bypassing this list is trivial. This is intended to prevent users from
|
||||
* accidentally clobbering a built-in object or function.
|
||||
* @private
|
||||
*/
|
||||
Blockly.JavaScript.addReservedWords(
|
||||
'Blockly,' + // In case JS is evaled in the current window.
|
||||
// https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words
|
||||
'break,case,catch,continue,debugger,default,delete,do,else,finally,for,function,if,in,instanceof,new,return,switch,this,throw,try,typeof,var,void,while,with,' +
|
||||
'class,enum,export,extends,import,super,implements,interface,let,package,private,protected,public,static,yield,' +
|
||||
'const,null,true,false,' +
|
||||
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects
|
||||
'Array,ArrayBuffer,Boolean,Date,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,Error,eval,EvalError,Float32Array,Float64Array,Function,Infinity,Int16Array,Int32Array,Int8Array,isFinite,isNaN,Iterator,JSON,Math,NaN,Number,Object,parseFloat,parseInt,RangeError,ReferenceError,RegExp,StopIteration,String,SyntaxError,TypeError,Uint16Array,Uint32Array,Uint8Array,Uint8ClampedArray,undefined,uneval,URIError,' +
|
||||
// https://developer.mozilla.org/en/DOM/window
|
||||
'applicationCache,closed,Components,content,_content,controllers,crypto,defaultStatus,dialogArguments,directories,document,frameElement,frames,fullScreen,globalStorage,history,innerHeight,innerWidth,length,location,locationbar,localStorage,menubar,messageManager,mozAnimationStartTime,mozInnerScreenX,mozInnerScreenY,mozPaintCount,name,navigator,opener,outerHeight,outerWidth,pageXOffset,pageYOffset,parent,performance,personalbar,pkcs11,returnValue,screen,screenX,screenY,scrollbars,scrollMaxX,scrollMaxY,scrollX,scrollY,self,sessionStorage,sidebar,status,statusbar,toolbar,top,URL,window,' +
|
||||
'addEventListener,alert,atob,back,blur,btoa,captureEvents,clearImmediate,clearInterval,clearTimeout,close,confirm,disableExternalCapture,dispatchEvent,dump,enableExternalCapture,escape,find,focus,forward,GeckoActiveXObject,getAttention,getAttentionWithCycleCount,getComputedStyle,getSelection,home,matchMedia,maximize,minimize,moveBy,moveTo,mozRequestAnimationFrame,open,openDialog,postMessage,print,prompt,QueryInterface,releaseEvents,removeEventListener,resizeBy,resizeTo,restore,routeEvent,scroll,scrollBy,scrollByLines,scrollByPages,scrollTo,setCursor,setImmediate,setInterval,setResizable,setTimeout,showModalDialog,sizeToContent,stop,unescape,updateCommands,XPCNativeWrapper,XPCSafeJSObjectWrapper,' +
|
||||
'onabort,onbeforeunload,onblur,onchange,onclick,onclose,oncontextmenu,ondevicemotion,ondeviceorientation,ondragdrop,onerror,onfocus,onhashchange,onkeydown,onkeypress,onkeyup,onload,onmousedown,onmousemove,onmouseout,onmouseover,onmouseup,onmozbeforepaint,onpaint,onpopstate,onreset,onresize,onscroll,onselect,onsubmit,onunload,onpageshow,onpagehide,' +
|
||||
'Image,Option,Worker,' +
|
||||
// https://developer.mozilla.org/en/Gecko_DOM_Reference
|
||||
'Event,Range,File,FileReader,Blob,BlobBuilder,' +
|
||||
'Attr,CDATASection,CharacterData,Comment,console,DocumentFragment,DocumentType,DomConfiguration,DOMError,DOMErrorHandler,DOMException,DOMImplementation,DOMImplementationList,DOMImplementationRegistry,DOMImplementationSource,DOMLocator,DOMObject,DOMString,DOMStringList,DOMTimeStamp,DOMUserData,Entity,EntityReference,MediaQueryList,MediaQueryListListener,NameList,NamedNodeMap,Node,NodeFilter,NodeIterator,NodeList,Notation,Plugin,PluginArray,ProcessingInstruction,SharedWorker,Text,TimeRanges,Treewalker,TypeInfo,UserDataHandler,Worker,WorkerGlobalScope,' +
|
||||
'HTMLDocument,HTMLElement,HTMLAnchorElement,HTMLAppletElement,HTMLAudioElement,HTMLAreaElement,HTMLBaseElement,HTMLBaseFontElement,HTMLBodyElement,HTMLBRElement,HTMLButtonElement,HTMLCanvasElement,HTMLDirectoryElement,HTMLDivElement,HTMLDListElement,HTMLEmbedElement,HTMLFieldSetElement,HTMLFontElement,HTMLFormElement,HTMLFrameElement,HTMLFrameSetElement,HTMLHeadElement,HTMLHeadingElement,HTMLHtmlElement,HTMLHRElement,HTMLIFrameElement,HTMLImageElement,HTMLInputElement,HTMLKeygenElement,HTMLLabelElement,HTMLLIElement,HTMLLinkElement,HTMLMapElement,HTMLMenuElement,HTMLMetaElement,HTMLModElement,HTMLObjectElement,HTMLOListElement,HTMLOptGroupElement,HTMLOptionElement,HTMLOutputElement,HTMLParagraphElement,HTMLParamElement,HTMLPreElement,HTMLQuoteElement,HTMLScriptElement,HTMLSelectElement,HTMLSourceElement,HTMLSpanElement,HTMLStyleElement,HTMLTableElement,HTMLTableCaptionElement,HTMLTableCellElement,HTMLTableDataCellElement,HTMLTableHeaderCellElement,HTMLTableColElement,HTMLTableRowElement,HTMLTableSectionElement,HTMLTextAreaElement,HTMLTimeElement,HTMLTitleElement,HTMLTrackElement,HTMLUListElement,HTMLUnknownElement,HTMLVideoElement,' +
|
||||
'HTMLCanvasElement,CanvasRenderingContext2D,CanvasGradient,CanvasPattern,TextMetrics,ImageData,CanvasPixelArray,HTMLAudioElement,HTMLVideoElement,NotifyAudioAvailableEvent,HTMLCollection,HTMLAllCollection,HTMLFormControlsCollection,HTMLOptionsCollection,HTMLPropertiesCollection,DOMTokenList,DOMSettableTokenList,DOMStringMap,RadioNodeList,' +
|
||||
'SVGDocument,SVGElement,SVGAElement,SVGAltGlyphElement,SVGAltGlyphDefElement,SVGAltGlyphItemElement,SVGAnimationElement,SVGAnimateElement,SVGAnimateColorElement,SVGAnimateMotionElement,SVGAnimateTransformElement,SVGSetElement,SVGCircleElement,SVGClipPathElement,SVGColorProfileElement,SVGCursorElement,SVGDefsElement,SVGDescElement,SVGEllipseElement,SVGFilterElement,SVGFilterPrimitiveStandardAttributes,SVGFEBlendElement,SVGFEColorMatrixElement,SVGFEComponentTransferElement,SVGFECompositeElement,SVGFEConvolveMatrixElement,SVGFEDiffuseLightingElement,SVGFEDisplacementMapElement,SVGFEDistantLightElement,SVGFEFloodElement,SVGFEGaussianBlurElement,SVGFEImageElement,SVGFEMergeElement,SVGFEMergeNodeElement,SVGFEMorphologyElement,SVGFEOffsetElement,SVGFEPointLightElement,SVGFESpecularLightingElement,SVGFESpotLightElement,SVGFETileElement,SVGFETurbulenceElement,SVGComponentTransferFunctionElement,SVGFEFuncRElement,SVGFEFuncGElement,SVGFEFuncBElement,SVGFEFuncAElement,SVGFontElement,SVGFontFaceElement,SVGFontFaceFormatElement,SVGFontFaceNameElement,SVGFontFaceSrcElement,SVGFontFaceUriElement,SVGForeignObjectElement,SVGGElement,SVGGlyphElement,SVGGlyphRefElement,SVGGradientElement,SVGLinearGradientElement,SVGRadialGradientElement,SVGHKernElement,SVGImageElement,SVGLineElement,SVGMarkerElement,SVGMaskElement,SVGMetadataElement,SVGMissingGlyphElement,SVGMPathElement,SVGPathElement,SVGPatternElement,SVGPolylineElement,SVGPolygonElement,SVGRectElement,SVGScriptElement,SVGStopElement,SVGStyleElement,SVGSVGElement,SVGSwitchElement,SVGSymbolElement,SVGTextElement,SVGTextPathElement,SVGTitleElement,SVGTRefElement,SVGTSpanElement,SVGUseElement,SVGViewElement,SVGVKernElement,' +
|
||||
'SVGAngle,SVGColor,SVGICCColor,SVGElementInstance,SVGElementInstanceList,SVGLength,SVGLengthList,SVGMatrix,SVGNumber,SVGNumberList,SVGPaint,SVGPoint,SVGPointList,SVGPreserveAspectRatio,SVGRect,SVGStringList,SVGTransform,SVGTransformList,' +
|
||||
'SVGAnimatedAngle,SVGAnimatedBoolean,SVGAnimatedEnumeration,SVGAnimatedInteger,SVGAnimatedLength,SVGAnimatedLengthList,SVGAnimatedNumber,SVGAnimatedNumberList,SVGAnimatedPreserveAspectRatio,SVGAnimatedRect,SVGAnimatedString,SVGAnimatedTransformList,' +
|
||||
'SVGPathSegList,SVGPathSeg,SVGPathSegArcAbs,SVGPathSegArcRel,SVGPathSegClosePath,SVGPathSegCurvetoCubicAbs,SVGPathSegCurvetoCubicRel,SVGPathSegCurvetoCubicSmoothAbs,SVGPathSegCurvetoCubicSmoothRel,SVGPathSegCurvetoQuadraticAbs,SVGPathSegCurvetoQuadraticRel,SVGPathSegCurvetoQuadraticSmoothAbs,SVGPathSegCurvetoQuadraticSmoothRel,SVGPathSegLinetoAbs,SVGPathSegLinetoHorizontalAbs,SVGPathSegLinetoHorizontalRel,SVGPathSegLinetoRel,SVGPathSegLinetoVerticalAbs,SVGPathSegLinetoVerticalRel,SVGPathSegMovetoAbs,SVGPathSegMovetoRel,ElementTimeControl,TimeEvent,SVGAnimatedPathData,' +
|
||||
'SVGAnimatedPoints,SVGColorProfileRule,SVGCSSRule,SVGExternalResourcesRequired,SVGFitToViewBox,SVGLangSpace,SVGLocatable,SVGRenderingIntent,SVGStylable,SVGTests,SVGTextContentElement,SVGTextPositioningElement,SVGTransformable,SVGUnitTypes,SVGURIReference,SVGViewSpec,SVGZoomAndPan');
|
||||
|
||||
/**
|
||||
* Order of operation ENUMs.
|
||||
* https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence
|
||||
*/
|
||||
Blockly.JavaScript.ORDER_ATOMIC = 0; // 0 "" ...
|
||||
Blockly.JavaScript.ORDER_NEW = 1.1; // new
|
||||
Blockly.JavaScript.ORDER_MEMBER = 1.2; // . []
|
||||
Blockly.JavaScript.ORDER_FUNCTION_CALL = 2; // ()
|
||||
Blockly.JavaScript.ORDER_INCREMENT = 3; // ++
|
||||
Blockly.JavaScript.ORDER_DECREMENT = 3; // --
|
||||
Blockly.JavaScript.ORDER_BITWISE_NOT = 4.1; // ~
|
||||
Blockly.JavaScript.ORDER_UNARY_PLUS = 4.2; // +
|
||||
Blockly.JavaScript.ORDER_UNARY_NEGATION = 4.3; // -
|
||||
Blockly.JavaScript.ORDER_LOGICAL_NOT = 4.4; // !
|
||||
Blockly.JavaScript.ORDER_TYPEOF = 4.5; // typeof
|
||||
Blockly.JavaScript.ORDER_VOID = 4.6; // void
|
||||
Blockly.JavaScript.ORDER_DELETE = 4.7; // delete
|
||||
Blockly.JavaScript.ORDER_DIVISION = 5.1; // /
|
||||
Blockly.JavaScript.ORDER_MULTIPLICATION = 5.2; // *
|
||||
Blockly.JavaScript.ORDER_MODULUS = 5.3; // %
|
||||
Blockly.JavaScript.ORDER_SUBTRACTION = 6.1; // -
|
||||
Blockly.JavaScript.ORDER_ADDITION = 6.2; // +
|
||||
Blockly.JavaScript.ORDER_BITWISE_SHIFT = 7; // << >> >>>
|
||||
Blockly.JavaScript.ORDER_RELATIONAL = 8; // < <= > >=
|
||||
Blockly.JavaScript.ORDER_IN = 8; // in
|
||||
Blockly.JavaScript.ORDER_INSTANCEOF = 8; // instanceof
|
||||
Blockly.JavaScript.ORDER_EQUALITY = 9; // == != === !==
|
||||
Blockly.JavaScript.ORDER_BITWISE_AND = 10; // &
|
||||
Blockly.JavaScript.ORDER_BITWISE_XOR = 11; // ^
|
||||
Blockly.JavaScript.ORDER_BITWISE_OR = 12; // |
|
||||
Blockly.JavaScript.ORDER_LOGICAL_AND = 13; // &&
|
||||
Blockly.JavaScript.ORDER_LOGICAL_OR = 14; // ||
|
||||
Blockly.JavaScript.ORDER_CONDITIONAL = 15; // ?:
|
||||
Blockly.JavaScript.ORDER_ASSIGNMENT = 16; // = += -= *= /= %= <<= >>= ...
|
||||
Blockly.JavaScript.ORDER_COMMA = 17; // ,
|
||||
Blockly.JavaScript.ORDER_NONE = 99; // (...)
|
||||
|
||||
/**
|
||||
* List of outer-inner pairings that do NOT require parentheses.
|
||||
* @type {!Array.<!Array.<number>>}
|
||||
*/
|
||||
Blockly.JavaScript.ORDER_OVERRIDES = [
|
||||
// (foo()).bar -> foo().bar
|
||||
// (foo())[0] -> foo()[0]
|
||||
[Blockly.JavaScript.ORDER_FUNCTION_CALL, Blockly.JavaScript.ORDER_MEMBER],
|
||||
// (foo())() -> foo()()
|
||||
[Blockly.JavaScript.ORDER_FUNCTION_CALL, Blockly.JavaScript.ORDER_FUNCTION_CALL],
|
||||
// (foo.bar).baz -> foo.bar.baz
|
||||
// (foo.bar)[0] -> foo.bar[0]
|
||||
// (foo[0]).bar -> foo[0].bar
|
||||
// (foo[0])[1] -> foo[0][1]
|
||||
[Blockly.JavaScript.ORDER_MEMBER, Blockly.JavaScript.ORDER_MEMBER],
|
||||
// (foo.bar)() -> foo.bar()
|
||||
// (foo[0])() -> foo[0]()
|
||||
[Blockly.JavaScript.ORDER_MEMBER, Blockly.JavaScript.ORDER_FUNCTION_CALL],
|
||||
|
||||
// !(!foo) -> !!foo
|
||||
[Blockly.JavaScript.ORDER_LOGICAL_NOT, Blockly.JavaScript.ORDER_LOGICAL_NOT],
|
||||
// a * (b * c) -> a * b * c
|
||||
[Blockly.JavaScript.ORDER_MULTIPLICATION, Blockly.JavaScript.ORDER_MULTIPLICATION],
|
||||
// a + (b + c) -> a + b + c
|
||||
[Blockly.JavaScript.ORDER_ADDITION, Blockly.JavaScript.ORDER_ADDITION],
|
||||
// a && (b && c) -> a && b && c
|
||||
[Blockly.JavaScript.ORDER_LOGICAL_AND, Blockly.JavaScript.ORDER_LOGICAL_AND],
|
||||
// a || (b || c) -> a || b || c
|
||||
[Blockly.JavaScript.ORDER_LOGICAL_OR, Blockly.JavaScript.ORDER_LOGICAL_OR]
|
||||
];
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
Blockly.JavaScript.init = function(workspace) {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly.JavaScript.definitions_ = Object.create(null);
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
// to actual function names (to avoid collisions with user functions).
|
||||
Blockly.JavaScript.functionNames_ = Object.create(null);
|
||||
|
||||
if (!Blockly.JavaScript.variableDB_) {
|
||||
Blockly.JavaScript.variableDB_ =
|
||||
new Blockly.Names(Blockly.JavaScript.RESERVED_WORDS_);
|
||||
} else {
|
||||
Blockly.JavaScript.variableDB_.reset();
|
||||
}
|
||||
|
||||
var defvars = [];
|
||||
var variables = workspace.variableList;
|
||||
if (variables.length) {
|
||||
for (var i = 0; i < variables.length; i++) {
|
||||
defvars[i] = Blockly.JavaScript.variableDB_.getName(variables[i],
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
}
|
||||
Blockly.JavaScript.definitions_['variables'] =
|
||||
'var ' + defvars.join(', ') + ';';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepend the generated code with the variable definitions.
|
||||
* @param {string} code Generated code.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
Blockly.JavaScript.finish = function(code) {
|
||||
// Convert the definitions dictionary into a list.
|
||||
var definitions = [];
|
||||
for (var name in Blockly.JavaScript.definitions_) {
|
||||
definitions.push(Blockly.JavaScript.definitions_[name]);
|
||||
}
|
||||
// Clean up temporary data.
|
||||
delete Blockly.JavaScript.definitions_;
|
||||
delete Blockly.JavaScript.functionNames_;
|
||||
Blockly.JavaScript.variableDB_.reset();
|
||||
return definitions.join('\n\n') + '\n\n\n' + code;
|
||||
};
|
||||
|
||||
/**
|
||||
* Naked values are top-level blocks with outputs that aren't plugged into
|
||||
* anything. A trailing semicolon is needed to make this legal.
|
||||
* @param {string} line Line of generated code.
|
||||
* @return {string} Legal line of code.
|
||||
*/
|
||||
Blockly.JavaScript.scrubNakedValue = function(line) {
|
||||
return line + ';\n';
|
||||
};
|
||||
|
||||
/**
|
||||
* Encode a string as a properly escaped JavaScript string, complete with
|
||||
* quotes.
|
||||
* @param {string} string Text to encode.
|
||||
* @return {string} JavaScript string.
|
||||
* @private
|
||||
*/
|
||||
Blockly.JavaScript.quote_ = function(string) {
|
||||
// Can't use goog.string.quote since Google's style guide recommends
|
||||
// JS string literals use single quotes.
|
||||
string = string.replace(/\\/g, '\\\\')
|
||||
.replace(/\n/g, '\\\n')
|
||||
.replace(/'/g, '\\\'');
|
||||
return '\'' + string + '\'';
|
||||
};
|
||||
|
||||
/**
|
||||
* Common tasks for generating JavaScript from blocks.
|
||||
* Handles comments for the specified block and any connected value blocks.
|
||||
* Calls any statements following this block.
|
||||
* @param {!Blockly.Block} block The current block.
|
||||
* @param {string} code The JavaScript code created for this block.
|
||||
* @return {string} JavaScript code with comments and subsequent blocks added.
|
||||
* @private
|
||||
*/
|
||||
Blockly.JavaScript.scrub_ = function(block, code) {
|
||||
var commentCode = '';
|
||||
// Only collect comments for blocks that aren't inline.
|
||||
if (!block.outputConnection || !block.outputConnection.targetConnection) {
|
||||
// Collect comment for this block.
|
||||
var comment = block.getCommentText();
|
||||
comment = Blockly.utils.wrap(comment, Blockly.JavaScript.COMMENT_WRAP - 3);
|
||||
if (comment) {
|
||||
if (block.getProcedureDef) {
|
||||
// Use a comment block for function comments.
|
||||
commentCode += '/**\n' +
|
||||
Blockly.JavaScript.prefixLines(comment + '\n', ' * ') +
|
||||
' */\n';
|
||||
} else {
|
||||
commentCode += Blockly.JavaScript.prefixLines(comment + '\n', '// ');
|
||||
}
|
||||
}
|
||||
// Collect comments for all value arguments.
|
||||
// Don't collect comments for nested statements.
|
||||
for (var i = 0; i < block.inputList.length; i++) {
|
||||
if (block.inputList[i].type == Blockly.INPUT_VALUE) {
|
||||
var childBlock = block.inputList[i].connection.targetBlock();
|
||||
if (childBlock) {
|
||||
var comment = Blockly.JavaScript.allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += Blockly.JavaScript.prefixLines(comment, '// ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
|
||||
var nextCode = Blockly.JavaScript.blockToCode(nextBlock);
|
||||
return commentCode + code + nextCode;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a property and adjusts the value while taking into account indexing.
|
||||
* @param {!Blockly.Block} block The block.
|
||||
* @param {string} atId The property ID of the element to get.
|
||||
* @param {number=} opt_delta Value to add.
|
||||
* @param {boolean=} opt_negate Whether to negate the value.
|
||||
* @param {number=} opt_order The highest order acting on this value.
|
||||
* @return {string|number}
|
||||
*/
|
||||
Blockly.JavaScript.getAdjusted = function(block, atId, opt_delta, opt_negate,
|
||||
opt_order) {
|
||||
var delta = opt_delta || 0;
|
||||
var order = opt_order || Blockly.JavaScript.ORDER_NONE;
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
delta--;
|
||||
}
|
||||
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
|
||||
if (delta > 0) {
|
||||
var at = Blockly.JavaScript.valueToCode(block, atId,
|
||||
Blockly.JavaScript.ORDER_ADDITION) || defaultAtIndex;
|
||||
} else if (delta < 0) {
|
||||
var at = Blockly.JavaScript.valueToCode(block, atId,
|
||||
Blockly.JavaScript.ORDER_SUBTRACTION) || defaultAtIndex;
|
||||
} else if (opt_negate) {
|
||||
var at = Blockly.JavaScript.valueToCode(block, atId,
|
||||
Blockly.JavaScript.ORDER_UNARY_NEGATION) || defaultAtIndex;
|
||||
} else {
|
||||
var at = Blockly.JavaScript.valueToCode(block, atId, order) ||
|
||||
defaultAtIndex;
|
||||
}
|
||||
|
||||
if (Blockly.isNumber(at)) {
|
||||
// If the index is a naked number, adjust it right now.
|
||||
at = parseFloat(at) + delta;
|
||||
if (opt_negate) {
|
||||
at = -at;
|
||||
}
|
||||
} else {
|
||||
// If the index is dynamic, adjust it in code.
|
||||
if (delta > 0) {
|
||||
at = at + ' + ' + delta;
|
||||
var innerOrder = Blockly.JavaScript.ORDER_ADDITION;
|
||||
} else if (delta < 0) {
|
||||
at = at + ' - ' + -delta;
|
||||
var innerOrder = Blockly.JavaScript.ORDER_SUBTRACTION;
|
||||
}
|
||||
if (opt_negate) {
|
||||
if (delta) {
|
||||
at = '-(' + at + ')';
|
||||
} else {
|
||||
at = '-' + at;
|
||||
}
|
||||
var innerOrder = Blockly.JavaScript.ORDER_UNARY_NEGATION;
|
||||
}
|
||||
innerOrder = Math.floor(innerOrder);
|
||||
order = Math.floor(order);
|
||||
if (innerOrder && order >= innerOrder) {
|
||||
at = '(' + at + ')';
|
||||
}
|
||||
}
|
||||
return at;
|
||||
};
|
||||
161
Blockly/brobot/generators/javascript/brobot.js
Normal file
161
Blockly/brobot/generators/javascript/brobot.js
Normal file
@@ -0,0 +1,161 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating JavaScript for colour blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript.brobot');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
|
||||
|
||||
Blockly.JavaScript['moveforward'] = function(block) {
|
||||
var code = "var datavalue='MV=50,'+(Math.round(STEPSMETER*0.4))+','+(Math.round(STEPSMETER*0.4));\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
code += 'var dt = new Date();while ((new Date()) - dt <= 3700) {};\n'; // delay
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['movebackward'] = function(block) {
|
||||
var code = "var datavalue='MV=50,'+(-Math.round(STEPSMETER*0.4))+','+(-Math.round(STEPSMETER*0.4));\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
code += 'var dt = new Date();while ((new Date()) - dt <= 3700) {};\n'; // delay
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['turnleft'] = function(block) {
|
||||
var code = "var datavalue='MV=50,'+(-Math.round(STEPSTURN/4))+','+(Math.round(STEPSTURN/4));\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
code += 'var dt = new Date();while ((new Date()) - dt <= 2200) {};\n'; // delay
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['turnright'] = function(block) {
|
||||
var code = "var datavalue='MV=50,'+(Math.round(STEPSTURN/4))+','+(-Math.round(STEPSTURN/4));\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
code += 'var dt = new Date();while ((new Date()) - dt <= 2200) {};\n'; // delay
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['turn180'] = function(block) {
|
||||
var value = block.getFieldValue('turn');
|
||||
var code;
|
||||
if (value==1)
|
||||
code = "var datavalue='MV=50,'+(-Math.round(STEPSTURN/2))+','+(Math.round(STEPSTURN/2));\n";
|
||||
else
|
||||
code = "var datavalue='MV=50,'+(Math.round(STEPSTURN/2))+','+(-Math.round(STEPSTURN/2));\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
code += 'var dt = new Date();while ((new Date()) - dt <= 3600) {};\n'; // delay
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['spin360'] = function(block) {
|
||||
var value = block.getFieldValue('spin');
|
||||
var code;
|
||||
if (value==1)
|
||||
code = "var datavalue='MV=50,'+(-Math.round(STEPSTURN))+','+(Math.round(STEPSTURN));\n";
|
||||
else
|
||||
code = "var datavalue='MV=50,'+(Math.round(STEPSTURN))+','+(-Math.round(STEPSTURN));\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
code += 'var dt = new Date();while ((new Date()) - dt <= 5400) {};\n'; // delay
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['movesimple'] = function(block) {
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'cm', Blockly.JavaScript.ORDER_ATOMIC);
|
||||
var value2 = block.getFieldValue('move');
|
||||
var code;
|
||||
if (value2 == 0)
|
||||
code = "var datavalue='MV=50,"+(Math.round(value*STEPSMETER/100))+","+(Math.round(value*STEPSMETER/100))+"';\n";
|
||||
else
|
||||
code = "var datavalue='MV=50,"+(-Math.round(value*STEPSMETER/100))+","+(-Math.round(value*STEPSMETER/100))+"';\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
var delay = 51*Math.abs(value) + 1200;
|
||||
code += 'var dt = new Date();while ((new Date()) - dt <= '+delay+') {};\n'; // delay
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['turndegrees'] = function(block) {
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'degrees', Blockly.JavaScript.ORDER_ATOMIC);
|
||||
var value2 = block.getFieldValue('turn');
|
||||
var code;
|
||||
if (value2 == 1)
|
||||
code = "var datavalue='MV=50,"+(-Math.round(value*STEPSTURN/360))+","+(Math.round(value*STEPSTURN/360))+"';\n";
|
||||
else
|
||||
code = "var datavalue='MV=50,"+(Math.round(value*STEPSTURN/360))+","+(-Math.round(value*STEPSTURN/360))+"';\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
var delay = 16*Math.abs(value)+700;
|
||||
code += 'var dt = new Date();while ((new Date()) - dt <= '+delay+') {};\n'; // delay
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['robotconfig'] = function(block) {
|
||||
var IPvalue = Blockly.JavaScript.valueToCode(block, 'IPvalue', Blockly.JavaScript.ORDER_ATOMIC).replace(/'/g, "");
|
||||
//var PORTvalue = Blockly.JavaScript.valueToCode(block, 'PORTvalue', Blockly.JavaScript.ORDER_ATOMIC).replace(/'/g, "");
|
||||
var PORTvalue = "2222"
|
||||
var code = '//Robot Initialization\n\n';
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data:\"IP="+ IPvalue+"&PORT="+PORTvalue+"\"});\n";
|
||||
return code;
|
||||
}
|
||||
|
||||
Blockly.JavaScript['throttle'] = function(block) {
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'TValue', Blockly.JavaScript.ORDER_ATOMIC);
|
||||
var code = "var datavalue='TH='+"+value+";\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['steering'] = function(block) {
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'SValue', Blockly.JavaScript.ORDER_ATOMIC);
|
||||
var code = "var datavalue='ST='+"+value+";\n";
|
||||
code += "$.ajax({url:'http://127.0.0.1:8008', data: datavalue});\n";
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['servo'] = function(block) {
|
||||
//var value = Blockly.JavaScript.valueToCode(block, 'SValue', Blockly.JavaScript.ORDER_ATOMIC);//
|
||||
var value = block.getFieldValue('Servo');
|
||||
// TODO: Assemble JavaScript into code variable.
|
||||
var code = "$.ajax({url:'http://127.0.0.1:8008', data:'SE="+ value+"'});\n";
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['mode'] = function(block) {
|
||||
//var value = Blockly.JavaScript.valueToCode(block, 'SValue', Blockly.JavaScript.ORDER_ATOMIC);//
|
||||
var value = block.getFieldValue('Mode');
|
||||
// TODO: Assemble JavaScript into code variable.
|
||||
var code = "$.ajax({url:'http://127.0.0.1:8008', data:'MO="+ value+"'});\n";
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['delay'] = function(block) {
|
||||
//var value = Blockly.JavaScript.valueToCode(block, 'SValue', Blockly.JavaScript.ORDER_ATOMIC);//
|
||||
var value = block.getFieldValue('DValue')*1000;
|
||||
// TODO: Assemble JavaScript into code variable.
|
||||
//var code = 'window.alert(delay'+value+');\n';
|
||||
var end = 0;
|
||||
var code = 'var dt = new Date();while ((new Date()) - dt <= '+value+') {};\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
|
||||
283
Blockly/brobot/generators/python.js
Normal file
283
Blockly/brobot/generators/python.js
Normal file
@@ -0,0 +1,283 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Helper functions for generating Python for blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python');
|
||||
|
||||
goog.require('Blockly.Generator');
|
||||
|
||||
|
||||
/**
|
||||
* Python code generator.
|
||||
* @type {!Blockly.Generator}
|
||||
*/
|
||||
Blockly.Python = new Blockly.Generator('Python');
|
||||
|
||||
/**
|
||||
* List of illegal variable names.
|
||||
* This is not intended to be a security feature. Blockly is 100% client-side,
|
||||
* so bypassing this list is trivial. This is intended to prevent users from
|
||||
* accidentally clobbering a built-in object or function.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Python.addReservedWords(
|
||||
// import keyword
|
||||
// print ','.join(keyword.kwlist)
|
||||
// http://docs.python.org/reference/lexical_analysis.html#keywords
|
||||
'and,as,assert,break,class,continue,def,del,elif,else,except,exec,' +
|
||||
'finally,for,from,global,if,import,in,is,lambda,not,or,pass,print,raise,' +
|
||||
'return,try,while,with,yield,' +
|
||||
//http://docs.python.org/library/constants.html
|
||||
'True,False,None,NotImplemented,Ellipsis,__debug__,quit,exit,copyright,' +
|
||||
'license,credits,' +
|
||||
// http://docs.python.org/library/functions.html
|
||||
'abs,divmod,input,open,staticmethod,all,enumerate,int,ord,str,any,eval,' +
|
||||
'isinstance,pow,sum,basestring,execfile,issubclass,print,super,bin,file,' +
|
||||
'iter,property,tuple,bool,filter,len,range,type,bytearray,float,list,' +
|
||||
'raw_input,unichr,callable,format,locals,reduce,unicode,chr,frozenset,' +
|
||||
'long,reload,vars,classmethod,getattr,map,repr,xrange,cmp,globals,max,' +
|
||||
'reversed,zip,compile,hasattr,memoryview,round,__import__,complex,hash,' +
|
||||
'min,set,apply,delattr,help,next,setattr,buffer,dict,hex,object,slice,' +
|
||||
'coerce,dir,id,oct,sorted,intern'
|
||||
);
|
||||
|
||||
/**
|
||||
* Order of operation ENUMs.
|
||||
* http://docs.python.org/reference/expressions.html#summary
|
||||
*/
|
||||
Blockly.Python.ORDER_ATOMIC = 0; // 0 "" ...
|
||||
Blockly.Python.ORDER_COLLECTION = 1; // tuples, lists, dictionaries
|
||||
Blockly.Python.ORDER_STRING_CONVERSION = 1; // `expression...`
|
||||
Blockly.Python.ORDER_MEMBER = 2.1; // . []
|
||||
Blockly.Python.ORDER_FUNCTION_CALL = 2.2; // ()
|
||||
Blockly.Python.ORDER_EXPONENTIATION = 3; // **
|
||||
Blockly.Python.ORDER_UNARY_SIGN = 4; // + -
|
||||
Blockly.Python.ORDER_BITWISE_NOT = 4; // ~
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE = 5; // * / // %
|
||||
Blockly.Python.ORDER_ADDITIVE = 6; // + -
|
||||
Blockly.Python.ORDER_BITWISE_SHIFT = 7; // << >>
|
||||
Blockly.Python.ORDER_BITWISE_AND = 8; // &
|
||||
Blockly.Python.ORDER_BITWISE_XOR = 9; // ^
|
||||
Blockly.Python.ORDER_BITWISE_OR = 10; // |
|
||||
Blockly.Python.ORDER_RELATIONAL = 11; // in, not in, is, is not,
|
||||
// <, <=, >, >=, <>, !=, ==
|
||||
Blockly.Python.ORDER_LOGICAL_NOT = 12; // not
|
||||
Blockly.Python.ORDER_LOGICAL_AND = 13; // and
|
||||
Blockly.Python.ORDER_LOGICAL_OR = 14; // or
|
||||
Blockly.Python.ORDER_CONDITIONAL = 15; // if else
|
||||
Blockly.Python.ORDER_LAMBDA = 16; // lambda
|
||||
Blockly.Python.ORDER_NONE = 99; // (...)
|
||||
|
||||
/**
|
||||
* List of outer-inner pairings that do NOT require parentheses.
|
||||
* @type {!Array.<!Array.<number>>}
|
||||
*/
|
||||
Blockly.Python.ORDER_OVERRIDES = [
|
||||
// (foo()).bar -> foo().bar
|
||||
// (foo())[0] -> foo()[0]
|
||||
[Blockly.Python.ORDER_FUNCTION_CALL, Blockly.Python.ORDER_MEMBER],
|
||||
// (foo())() -> foo()()
|
||||
[Blockly.Python.ORDER_FUNCTION_CALL, Blockly.Python.ORDER_FUNCTION_CALL],
|
||||
// (foo.bar).baz -> foo.bar.baz
|
||||
// (foo.bar)[0] -> foo.bar[0]
|
||||
// (foo[0]).bar -> foo[0].bar
|
||||
// (foo[0])[1] -> foo[0][1]
|
||||
[Blockly.Python.ORDER_MEMBER, Blockly.Python.ORDER_MEMBER],
|
||||
// (foo.bar)() -> foo.bar()
|
||||
// (foo[0])() -> foo[0]()
|
||||
[Blockly.Python.ORDER_MEMBER, Blockly.Python.ORDER_FUNCTION_CALL],
|
||||
|
||||
// not (not foo) -> not not foo
|
||||
[Blockly.Python.ORDER_LOGICAL_NOT, Blockly.Python.ORDER_LOGICAL_NOT],
|
||||
// a and (b and c) -> a and b and c
|
||||
[Blockly.Python.ORDER_LOGICAL_AND, Blockly.Python.ORDER_LOGICAL_AND],
|
||||
// a or (b or c) -> a or b or c
|
||||
[Blockly.Python.ORDER_LOGICAL_OR, Blockly.Python.ORDER_LOGICAL_OR]
|
||||
];
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
Blockly.Python.init = function(workspace) {
|
||||
/**
|
||||
* Empty loops or conditionals are not allowed in Python.
|
||||
*/
|
||||
Blockly.Python.PASS = this.INDENT + 'pass\n';
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly.Python.definitions_ = Object.create(null);
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
// to actual function names (to avoid collisions with user functions).
|
||||
Blockly.Python.functionNames_ = Object.create(null);
|
||||
|
||||
if (!Blockly.Python.variableDB_) {
|
||||
Blockly.Python.variableDB_ =
|
||||
new Blockly.Names(Blockly.Python.RESERVED_WORDS_);
|
||||
} else {
|
||||
Blockly.Python.variableDB_.reset();
|
||||
}
|
||||
|
||||
var defvars = [];
|
||||
var variables = workspace.variableList;
|
||||
for (var i = 0; i < variables.length; i++) {
|
||||
defvars[i] = Blockly.Python.variableDB_.getName(variables[i],
|
||||
Blockly.Variables.NAME_TYPE) + ' = None';
|
||||
}
|
||||
Blockly.Python.definitions_['variables'] = defvars.join('\n');
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepend the generated code with the variable definitions.
|
||||
* @param {string} code Generated code.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
Blockly.Python.finish = function(code) {
|
||||
// Convert the definitions dictionary into a list.
|
||||
var imports = [];
|
||||
var definitions = [];
|
||||
for (var name in Blockly.Python.definitions_) {
|
||||
var def = Blockly.Python.definitions_[name];
|
||||
if (def.match(/^(from\s+\S+\s+)?import\s+\S+/)) {
|
||||
imports.push(def);
|
||||
} else {
|
||||
definitions.push(def);
|
||||
}
|
||||
}
|
||||
// Clean up temporary data.
|
||||
delete Blockly.Python.definitions_;
|
||||
delete Blockly.Python.functionNames_;
|
||||
Blockly.Python.variableDB_.reset();
|
||||
var allDefs = imports.join('\n') + '\n\n' + definitions.join('\n\n');
|
||||
return allDefs.replace(/\n\n+/g, '\n\n').replace(/\n*$/, '\n\n\n') + code;
|
||||
};
|
||||
|
||||
/**
|
||||
* Naked values are top-level blocks with outputs that aren't plugged into
|
||||
* anything.
|
||||
* @param {string} line Line of generated code.
|
||||
* @return {string} Legal line of code.
|
||||
*/
|
||||
Blockly.Python.scrubNakedValue = function(line) {
|
||||
return line + '\n';
|
||||
};
|
||||
|
||||
/**
|
||||
* Encode a string as a properly escaped Python string, complete with quotes.
|
||||
* @param {string} string Text to encode.
|
||||
* @return {string} Python string.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Python.quote_ = function(string) {
|
||||
// Can't use goog.string.quote since % must also be escaped.
|
||||
string = string.replace(/\\/g, '\\\\')
|
||||
.replace(/\n/g, '\\\n')
|
||||
.replace(/\%/g, '\\%')
|
||||
.replace(/'/g, '\\\'');
|
||||
return '\'' + string + '\'';
|
||||
};
|
||||
|
||||
/**
|
||||
* Common tasks for generating Python from blocks.
|
||||
* Handles comments for the specified block and any connected value blocks.
|
||||
* Calls any statements following this block.
|
||||
* @param {!Blockly.Block} block The current block.
|
||||
* @param {string} code The Python code created for this block.
|
||||
* @return {string} Python code with comments and subsequent blocks added.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Python.scrub_ = function(block, code) {
|
||||
var commentCode = '';
|
||||
// Only collect comments for blocks that aren't inline.
|
||||
if (!block.outputConnection || !block.outputConnection.targetConnection) {
|
||||
// Collect comment for this block.
|
||||
var comment = block.getCommentText();
|
||||
comment = Blockly.utils.wrap(comment, Blockly.Python.COMMENT_WRAP - 3);
|
||||
if (comment) {
|
||||
if (block.getProcedureDef) {
|
||||
// Use a comment block for function comments.
|
||||
commentCode += '"""' + comment + '\n"""\n';
|
||||
} else {
|
||||
commentCode += Blockly.Python.prefixLines(comment + '\n', '# ');
|
||||
}
|
||||
}
|
||||
// Collect comments for all value arguments.
|
||||
// Don't collect comments for nested statements.
|
||||
for (var i = 0; i < block.inputList.length; i++) {
|
||||
if (block.inputList[i].type == Blockly.INPUT_VALUE) {
|
||||
var childBlock = block.inputList[i].connection.targetBlock();
|
||||
if (childBlock) {
|
||||
var comment = Blockly.Python.allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += Blockly.Python.prefixLines(comment, '# ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
|
||||
var nextCode = Blockly.Python.blockToCode(nextBlock);
|
||||
return commentCode + code + nextCode;
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a property and adjusts the value, taking into account indexing, and
|
||||
* casts to an integer.
|
||||
* @param {!Blockly.Block} block The block.
|
||||
* @param {string} atId The property ID of the element to get.
|
||||
* @param {number=} opt_delta Value to add.
|
||||
* @param {boolean=} opt_negate Whether to negate the value.
|
||||
* @return {string|number}
|
||||
*/
|
||||
Blockly.Python.getAdjustedInt = function(block, atId, opt_delta, opt_negate) {
|
||||
var delta = opt_delta || 0;
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
delta--;
|
||||
}
|
||||
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
|
||||
var atOrder = delta ? Blockly.Python.ORDER_ADDITIVE :
|
||||
Blockly.Python.ORDER_NONE;
|
||||
var at = Blockly.Python.valueToCode(block, atId, atOrder) || defaultAtIndex;
|
||||
|
||||
if (Blockly.isNumber(at)) {
|
||||
// If the index is a naked number, adjust it right now.
|
||||
at = parseInt(at, 10) + delta;
|
||||
if (opt_negate) {
|
||||
at = -at;
|
||||
}
|
||||
} else {
|
||||
// If the index is dynamic, adjust it in code.
|
||||
if (delta > 0) {
|
||||
at = 'int(' + at + ' + ' + delta + ')';
|
||||
} else if (delta < 0) {
|
||||
at = 'int(' + at + ' - ' + -delta + ')';
|
||||
} else {
|
||||
at = 'int(' + at + ')';
|
||||
}
|
||||
if (opt_negate) {
|
||||
at = '-' + at;
|
||||
}
|
||||
}
|
||||
return at;
|
||||
};
|
||||
83
Blockly/brobot/generators/python/brobot.js
Normal file
83
Blockly/brobot/generators/python/brobot.js
Normal file
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating JavaScript for colour blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.brobot');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
|
||||
Blockly.Python['robotconfig'] = function(block) {
|
||||
var IPvalue = Blockly.JavaScript.valueToCode(block, 'IPvalue', Blockly.JavaScript.ORDER_ATOMIC).replace(/'/g, "");
|
||||
var PORTvalue = Blockly.JavaScript.valueToCode(block, 'PORTvalue', Blockly.JavaScript.ORDER_ATOMIC).replace(/'/g, "");
|
||||
var code = 'myRobot.UDP_IP=\"'+IPvalue+'\"\n';
|
||||
code += 'myRobot.UDP_PORT=\"'+PORTvalue+'\"\n';
|
||||
//var code = '';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['throttle'] = function(block) {
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'TValue', Blockly.JavaScript.ORDER_ATOMIC);//
|
||||
//var value = block.getFieldValue('TValue');
|
||||
// TODO: Assemble JavaScript into code variable.
|
||||
//var code = 'window.alert("T'+value+'");\n';
|
||||
// Create the Socket
|
||||
var code = 'myRobot.throttle('+value+')\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['steering'] = function(block) {
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'SValue', Blockly.JavaScript.ORDER_ATOMIC);//
|
||||
//var value = block.getFieldValue('SValue');
|
||||
// TODO: Assemble JavaScript into code variable.
|
||||
var code = 'myRobot.steering('+value+')\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['servo'] = function(block) {
|
||||
//var value = Blockly.JavaScript.valueToCode(block, 'SValue', Blockly.JavaScript.ORDER_ATOMIC);//
|
||||
var value = block.getFieldValue('Servo');
|
||||
// TODO: Assemble JavaScript into code variable.
|
||||
var code = 'myRobot.servo('+value+')\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['mode'] = function(block) {
|
||||
//var value = Blockly.JavaScript.valueToCode(block, 'SValue', Blockly.JavaScript.ORDER_ATOMIC);//
|
||||
var value = block.getFieldValue('Mode');
|
||||
// TODO: Assemble JavaScript into code variable.
|
||||
var code = 'myRobot.mode('+value+')\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['delay'] = function(block) {
|
||||
//var value = Blockly.JavaScript.valueToCode(block, 'SValue', Blockly.JavaScript.ORDER_ATOMIC);//
|
||||
var value = block.getFieldValue('DValue');
|
||||
// TODO: Assemble JavaScript into code variable.
|
||||
//var code = 'window.alert(delay'+value+');\n';
|
||||
var code = 'time.sleep('+value+')\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
|
||||
86
Blockly/brobot/generators/python/colour.js
Normal file
86
Blockly/brobot/generators/python/colour.js
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for colour blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.colour');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
|
||||
|
||||
Blockly.Python['colour_picker'] = function(block) {
|
||||
// Colour picker.
|
||||
var code = '\'' + block.getFieldValue('COLOUR') + '\'';
|
||||
return [code, Blockly.Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Python['colour_random'] = function(block) {
|
||||
// Generate a random colour.
|
||||
Blockly.Python.definitions_['import_random'] = 'import random';
|
||||
var code = '\'#%06x\' % random.randint(0, 2**24 - 1)';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['colour_rgb'] = function(block) {
|
||||
// Compose a colour from RGB components expressed as percentages.
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'colour_rgb',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b):',
|
||||
' r = round(min(100, max(0, r)) * 2.55)',
|
||||
' g = round(min(100, max(0, g)) * 2.55)',
|
||||
' b = round(min(100, max(0, b)) * 2.55)',
|
||||
' return \'#%02x%02x%02x\' % (r, g, b)']);
|
||||
var r = Blockly.Python.valueToCode(block, 'RED',
|
||||
Blockly.Python.ORDER_NONE) || 0;
|
||||
var g = Blockly.Python.valueToCode(block, 'GREEN',
|
||||
Blockly.Python.ORDER_NONE) || 0;
|
||||
var b = Blockly.Python.valueToCode(block, 'BLUE',
|
||||
Blockly.Python.ORDER_NONE) || 0;
|
||||
var code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['colour_blend'] = function(block) {
|
||||
// Blend two colours together.
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'colour_blend',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(colour1, colour2, ratio):',
|
||||
' r1, r2 = int(colour1[1:3], 16), int(colour2[1:3], 16)',
|
||||
' g1, g2 = int(colour1[3:5], 16), int(colour2[3:5], 16)',
|
||||
' b1, b2 = int(colour1[5:7], 16), int(colour2[5:7], 16)',
|
||||
' ratio = min(1, max(0, ratio))',
|
||||
' r = round(r1 * (1 - ratio) + r2 * ratio)',
|
||||
' g = round(g1 * (1 - ratio) + g2 * ratio)',
|
||||
' b = round(b1 * (1 - ratio) + b2 * ratio)',
|
||||
' return \'#%02x%02x%02x\' % (r, g, b)']);
|
||||
var colour1 = Blockly.Python.valueToCode(block, 'COLOUR1',
|
||||
Blockly.Python.ORDER_NONE) || '\'#000000\'';
|
||||
var colour2 = Blockly.Python.valueToCode(block, 'COLOUR2',
|
||||
Blockly.Python.ORDER_NONE) || '\'#000000\'';
|
||||
var ratio = Blockly.Python.valueToCode(block, 'RATIO',
|
||||
Blockly.Python.ORDER_NONE) || 0;
|
||||
var code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
355
Blockly/brobot/generators/python/lists.js
Normal file
355
Blockly/brobot/generators/python/lists.js
Normal file
@@ -0,0 +1,355 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for list blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.lists');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
|
||||
|
||||
Blockly.Python['lists_create_empty'] = function(block) {
|
||||
// Create an empty list.
|
||||
return ['[]', Blockly.Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_create_with'] = function(block) {
|
||||
// Create a list with any number of elements of any type.
|
||||
var elements = new Array(block.itemCount_);
|
||||
for (var i = 0; i < block.itemCount_; i++) {
|
||||
elements[i] = Blockly.Python.valueToCode(block, 'ADD' + i,
|
||||
Blockly.Python.ORDER_NONE) || 'None';
|
||||
}
|
||||
var code = '[' + elements.join(', ') + ']';
|
||||
return [code, Blockly.Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_repeat'] = function(block) {
|
||||
// Create a list with one element repeated.
|
||||
var item = Blockly.Python.valueToCode(block, 'ITEM',
|
||||
Blockly.Python.ORDER_NONE) || 'None';
|
||||
var times = Blockly.Python.valueToCode(block, 'NUM',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
|
||||
var code = '[' + item + '] * ' + times;
|
||||
return [code, Blockly.Python.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_length'] = function(block) {
|
||||
// String or array length.
|
||||
var list = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || '[]';
|
||||
return ['len(' + list + ')', Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var list = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || '[]';
|
||||
var code = 'not len(' + list + ')';
|
||||
return [code, Blockly.Python.ORDER_LOGICAL_NOT];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_indexOf'] = function(block) {
|
||||
// Find an item in the list.
|
||||
var item = Blockly.Python.valueToCode(block, 'FIND',
|
||||
Blockly.Python.ORDER_NONE) || '[]';
|
||||
var list = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
var errorIndex = ' 0';
|
||||
var firstIndexAdjustment = ' + 1';
|
||||
var lastIndexAdjustment = '';
|
||||
} else {
|
||||
var errorIndex = ' -1';
|
||||
var firstIndexAdjustment = '';
|
||||
var lastIndexAdjustment = ' - 1';
|
||||
}
|
||||
if (block.getFieldValue('END') == 'FIRST') {
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'first_index',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(my_list, elem):',
|
||||
' try: index = my_list.index(elem)' + firstIndexAdjustment,
|
||||
' except: index =' + errorIndex,
|
||||
' return index']);
|
||||
var code = functionName + '(' + list + ', ' + item + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'last_index',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(my_list, elem):',
|
||||
' try: index = len(my_list) - my_list[::-1].index(elem)' +
|
||||
lastIndexAdjustment,
|
||||
' except: index =' + errorIndex,
|
||||
' return index']);
|
||||
var code = functionName + '(' + list + ', ' + item + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_getIndex'] = function(block) {
|
||||
// Get element at index.
|
||||
// Note: Until January 2013 this block did not have MODE or WHERE inputs.
|
||||
var mode = block.getFieldValue('MODE') || 'GET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var listOrder = (where == 'RANDOM') ? Blockly.Python.ORDER_NONE :
|
||||
Blockly.Python.ORDER_MEMBER;
|
||||
var list = Blockly.Python.valueToCode(block, 'VALUE', listOrder) || '[]';
|
||||
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
if (mode == 'GET') {
|
||||
var code = list + '[0]';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.pop(0)';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.pop(0)\n';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
if (mode == 'GET') {
|
||||
var code = list + '[-1]';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.pop()';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.pop()\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_START':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT');
|
||||
if (mode == 'GET') {
|
||||
var code = list + '[' + at + ']';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.pop(' + at + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.pop(' + at + ')\n';
|
||||
}
|
||||
break;
|
||||
case'FROM_END':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT', 1, true);
|
||||
if (mode == 'GET') {
|
||||
var code = list + '[' + at + ']';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.pop(' + at + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.pop(' + at + ')\n';
|
||||
}
|
||||
break;
|
||||
case 'RANDOM':
|
||||
Blockly.Python.definitions_['import_random'] = 'import random';
|
||||
if (mode == 'GET') {
|
||||
code = 'random.choice(' + list + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else {
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'lists_remove_random_item',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):',
|
||||
' x = int(random.random() * len(myList))',
|
||||
' return myList.pop(x)']);
|
||||
code = functionName + '(' + list + ')';
|
||||
if (mode == 'GET_REMOVE') {
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return code + '\n';
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
throw 'Unhandled combination (lists_getIndex).';
|
||||
};
|
||||
|
||||
Blockly.Python['lists_setIndex'] = function(block) {
|
||||
// Set element at index.
|
||||
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
||||
var list = Blockly.Python.valueToCode(block, 'LIST',
|
||||
Blockly.Python.ORDER_MEMBER) || '[]';
|
||||
var mode = block.getFieldValue('MODE') || 'GET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var value = Blockly.Python.valueToCode(block, 'TO',
|
||||
Blockly.Python.ORDER_NONE) || 'None';
|
||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||
// Closure, which accesses and modifies 'list'.
|
||||
function cacheList() {
|
||||
if (list.match(/^\w+$/)) {
|
||||
return '';
|
||||
}
|
||||
var listVar = Blockly.Python.variableDB_.getDistinctName(
|
||||
'tmp_list', Blockly.Variables.NAME_TYPE);
|
||||
var code = listVar + ' = ' + list + '\n';
|
||||
list = listVar;
|
||||
return code;
|
||||
}
|
||||
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
if (mode == 'SET') {
|
||||
return list + '[0] = ' + value + '\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.insert(0, ' + value + ')\n';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
if (mode == 'SET') {
|
||||
return list + '[-1] = ' + value + '\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.append(' + value + ')\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_START':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT');
|
||||
if (mode == 'SET') {
|
||||
return list + '[' + at + '] = ' + value + '\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.insert(' + at + ', ' + value + ')\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT', 1, true);
|
||||
if (mode == 'SET') {
|
||||
return list + '[' + at + '] = ' + value + '\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.insert(' + at + ', ' + value + ')\n';
|
||||
}
|
||||
break;
|
||||
case 'RANDOM':
|
||||
Blockly.Python.definitions_['import_random'] = 'import random';
|
||||
var code = cacheList();
|
||||
var xVar = Blockly.Python.variableDB_.getDistinctName(
|
||||
'tmp_x', Blockly.Variables.NAME_TYPE);
|
||||
code += xVar + ' = int(random.random() * len(' + list + '))\n';
|
||||
if (mode == 'SET') {
|
||||
code += list + '[' + xVar + '] = ' + value + '\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
code += list + '.insert(' + xVar + ', ' + value + ')\n';
|
||||
return code;
|
||||
}
|
||||
break;
|
||||
}
|
||||
throw 'Unhandled combination (lists_setIndex).';
|
||||
};
|
||||
|
||||
Blockly.Python['lists_getSublist'] = function(block) {
|
||||
// Get sublist.
|
||||
var list = Blockly.Python.valueToCode(block, 'LIST',
|
||||
Blockly.Python.ORDER_MEMBER) || '[]';
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
var at1 = Blockly.Python.getAdjustedInt(block, 'AT1');
|
||||
if (at1 == '0') {
|
||||
at1 = '';
|
||||
}
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at1 = Blockly.Python.getAdjustedInt(block, 'AT1', 1, true);
|
||||
break;
|
||||
case 'FIRST':
|
||||
var at1 = '';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (lists_getSublist)';
|
||||
}
|
||||
switch (where2) {
|
||||
case 'FROM_START':
|
||||
var at2 = Blockly.Python.getAdjustedInt(block, 'AT2', 1);
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at2 = Blockly.Python.getAdjustedInt(block, 'AT2', 0, true);
|
||||
// Ensure that if the result calculated is 0 that sub-sequence will
|
||||
// include all elements as expected.
|
||||
if (!Blockly.isNumber(String(at2))) {
|
||||
Blockly.Python.definitions_['import_sys'] = 'import sys';
|
||||
at2 += ' or sys.maxsize';
|
||||
} else if (at2 == '0') {
|
||||
at2 = '';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
var at2 = '';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (lists_getSublist)';
|
||||
}
|
||||
var code = list + '[' + at1 + ' : ' + at2 + ']';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_sort'] = function(block) {
|
||||
// Block for sorting a list.
|
||||
var list = (Blockly.Python.valueToCode(block, 'LIST',
|
||||
Blockly.Python.ORDER_NONE) || '[]');
|
||||
var type = block.getFieldValue('TYPE');
|
||||
var reverse = block.getFieldValue('DIRECTION') === '1' ? 'False' : 'True';
|
||||
var sortFunctionName = Blockly.Python.provideFunction_('lists_sort',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(my_list, type, reverse):',
|
||||
' def try_float(s):',
|
||||
' try:',
|
||||
' return float(s)',
|
||||
' except:',
|
||||
' return 0',
|
||||
' key_funcs = {',
|
||||
' "NUMERIC": try_float,',
|
||||
' "TEXT": str,',
|
||||
' "IGNORE_CASE": lambda s: str(s).lower()',
|
||||
' }',
|
||||
' key_func = key_funcs[type]',
|
||||
' list_cpy = list(my_list)', // Clone the list.
|
||||
' return sorted(list_cpy, key=key_func, reverse=reverse)'
|
||||
]);
|
||||
|
||||
var code = sortFunctionName +
|
||||
'(' + list + ', "' + type + '", ' + reverse + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_split'] = function(block) {
|
||||
// Block for splitting text into a list, or joining a list into text.
|
||||
var mode = block.getFieldValue('MODE');
|
||||
if (mode == 'SPLIT') {
|
||||
var value_input = Blockly.Python.valueToCode(block, 'INPUT',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var value_delim = Blockly.Python.valueToCode(block, 'DELIM',
|
||||
Blockly.Python.ORDER_NONE);
|
||||
var code = value_input + '.split(' + value_delim + ')';
|
||||
} else if (mode == 'JOIN') {
|
||||
var value_input = Blockly.Python.valueToCode(block, 'INPUT',
|
||||
Blockly.Python.ORDER_NONE) || '[]';
|
||||
var value_delim = Blockly.Python.valueToCode(block, 'DELIM',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var code = value_delim + '.join(' + value_input + ')';
|
||||
} else {
|
||||
throw 'Unknown mode: ' + mode;
|
||||
}
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
127
Blockly/brobot/generators/python/logic.js
Normal file
127
Blockly/brobot/generators/python/logic.js
Normal file
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for logic blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.logic');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
|
||||
|
||||
Blockly.Python['controls_if'] = function(block) {
|
||||
// If/elseif/else condition.
|
||||
var n = 0;
|
||||
var argument = Blockly.Python.valueToCode(block, 'IF' + n,
|
||||
Blockly.Python.ORDER_NONE) || 'False';
|
||||
var branch = Blockly.Python.statementToCode(block, 'DO' + n) ||
|
||||
Blockly.Python.PASS;
|
||||
var code = 'if ' + argument + ':\n' + branch;
|
||||
for (n = 1; n <= block.elseifCount_; n++) {
|
||||
argument = Blockly.Python.valueToCode(block, 'IF' + n,
|
||||
Blockly.Python.ORDER_NONE) || 'False';
|
||||
branch = Blockly.Python.statementToCode(block, 'DO' + n) ||
|
||||
Blockly.Python.PASS;
|
||||
code += 'elif ' + argument + ':\n' + branch;
|
||||
}
|
||||
if (block.elseCount_) {
|
||||
branch = Blockly.Python.statementToCode(block, 'ELSE') ||
|
||||
Blockly.Python.PASS;
|
||||
code += 'else:\n' + branch;
|
||||
}
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['logic_compare'] = function(block) {
|
||||
// Comparison operator.
|
||||
var OPERATORS = {
|
||||
'EQ': '==',
|
||||
'NEQ': '!=',
|
||||
'LT': '<',
|
||||
'LTE': '<=',
|
||||
'GT': '>',
|
||||
'GTE': '>='
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('OP')];
|
||||
var order = Blockly.Python.ORDER_RELATIONAL;
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.Python.valueToCode(block, 'B', order) || '0';
|
||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
var operator = (block.getFieldValue('OP') == 'AND') ? 'and' : 'or';
|
||||
var order = (operator == 'and') ? Blockly.Python.ORDER_LOGICAL_AND :
|
||||
Blockly.Python.ORDER_LOGICAL_OR;
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'A', order);
|
||||
var argument1 = Blockly.Python.valueToCode(block, 'B', order);
|
||||
if (!argument0 && !argument1) {
|
||||
// If there are no arguments, then the return value is false.
|
||||
argument0 = 'False';
|
||||
argument1 = 'False';
|
||||
} else {
|
||||
// Single missing arguments have no effect on the return value.
|
||||
var defaultArgument = (operator == 'and') ? 'True' : 'False';
|
||||
if (!argument0) {
|
||||
argument0 = defaultArgument;
|
||||
}
|
||||
if (!argument1) {
|
||||
argument1 = defaultArgument;
|
||||
}
|
||||
}
|
||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_negate'] = function(block) {
|
||||
// Negation.
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'BOOL',
|
||||
Blockly.Python.ORDER_LOGICAL_NOT) || 'True';
|
||||
var code = 'not ' + argument0;
|
||||
return [code, Blockly.Python.ORDER_LOGICAL_NOT];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'True' : 'False';
|
||||
return [code, Blockly.Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_null'] = function(block) {
|
||||
// Null data type.
|
||||
return ['None', Blockly.Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Python['logic_ternary'] = function(block) {
|
||||
// Ternary operator.
|
||||
var value_if = Blockly.Python.valueToCode(block, 'IF',
|
||||
Blockly.Python.ORDER_CONDITIONAL) || 'False';
|
||||
var value_then = Blockly.Python.valueToCode(block, 'THEN',
|
||||
Blockly.Python.ORDER_CONDITIONAL) || 'None';
|
||||
var value_else = Blockly.Python.valueToCode(block, 'ELSE',
|
||||
Blockly.Python.ORDER_CONDITIONAL) || 'None';
|
||||
var code = value_then + ' if ' + value_if + ' else ' + value_else;
|
||||
return [code, Blockly.Python.ORDER_CONDITIONAL];
|
||||
};
|
||||
211
Blockly/brobot/generators/python/loops.js
Normal file
211
Blockly/brobot/generators/python/loops.js
Normal file
@@ -0,0 +1,211 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for loop blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.loops');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
|
||||
|
||||
Blockly.Python['controls_repeat_ext'] = function(block) {
|
||||
// Repeat n times.
|
||||
if (block.getField('TIMES')) {
|
||||
// Internal number.
|
||||
var repeats = String(parseInt(block.getFieldValue('TIMES'), 10));
|
||||
} else {
|
||||
// External number.
|
||||
var repeats = Blockly.Python.valueToCode(block, 'TIMES',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
}
|
||||
if (Blockly.isNumber(repeats)) {
|
||||
repeats = parseInt(repeats, 10);
|
||||
} else {
|
||||
repeats = 'int(' + repeats + ')';
|
||||
}
|
||||
var branch = Blockly.Python.statementToCode(block, 'DO');
|
||||
branch = Blockly.Python.addLoopTrap(branch, block.id) ||
|
||||
Blockly.Python.PASS;
|
||||
var loopVar = Blockly.Python.variableDB_.getDistinctName(
|
||||
'count', Blockly.Variables.NAME_TYPE);
|
||||
var code = 'for ' + loopVar + ' in range(' + repeats + '):\n' + branch;
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['controls_repeat'] = Blockly.Python['controls_repeat_ext'];
|
||||
|
||||
Blockly.Python['controls_whileUntil'] = function(block) {
|
||||
// Do while/until loop.
|
||||
var until = block.getFieldValue('MODE') == 'UNTIL';
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'BOOL',
|
||||
until ? Blockly.Python.ORDER_LOGICAL_NOT :
|
||||
Blockly.Python.ORDER_NONE) || 'False';
|
||||
var branch = Blockly.Python.statementToCode(block, 'DO');
|
||||
branch = Blockly.Python.addLoopTrap(branch, block.id) ||
|
||||
Blockly.Python.PASS;
|
||||
if (until) {
|
||||
argument0 = 'not ' + argument0;
|
||||
}
|
||||
return 'while ' + argument0 + ':\n' + branch;
|
||||
};
|
||||
|
||||
Blockly.Python['controls_for'] = function(block) {
|
||||
// For loop.
|
||||
var variable0 = Blockly.Python.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'FROM',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Python.valueToCode(block, 'TO',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
var increment = Blockly.Python.valueToCode(block, 'BY',
|
||||
Blockly.Python.ORDER_NONE) || '1';
|
||||
var branch = Blockly.Python.statementToCode(block, 'DO');
|
||||
branch = Blockly.Python.addLoopTrap(branch, block.id) ||
|
||||
Blockly.Python.PASS;
|
||||
|
||||
var code = '';
|
||||
var range;
|
||||
|
||||
// Helper functions.
|
||||
var defineUpRange = function() {
|
||||
return Blockly.Python.provideFunction_(
|
||||
'upRange',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(start, stop, step):',
|
||||
' while start <= stop:',
|
||||
' yield start',
|
||||
' start += abs(step)']);
|
||||
};
|
||||
var defineDownRange = function() {
|
||||
return Blockly.Python.provideFunction_(
|
||||
'downRange',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(start, stop, step):',
|
||||
' while start >= stop:',
|
||||
' yield start',
|
||||
' start -= abs(step)']);
|
||||
};
|
||||
// Arguments are legal Python code (numbers or strings returned by scrub()).
|
||||
var generateUpDownRange = function(start, end, inc) {
|
||||
return '(' + start + ' <= ' + end + ') and ' +
|
||||
defineUpRange() + '(' + start + ', ' + end + ', ' + inc + ') or ' +
|
||||
defineDownRange() + '(' + start + ', ' + end + ', ' + inc + ')';
|
||||
};
|
||||
|
||||
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
|
||||
Blockly.isNumber(increment)) {
|
||||
// All parameters are simple numbers.
|
||||
argument0 = parseFloat(argument0);
|
||||
argument1 = parseFloat(argument1);
|
||||
increment = Math.abs(parseFloat(increment));
|
||||
if (argument0 % 1 === 0 && argument1 % 1 === 0 && increment % 1 === 0) {
|
||||
// All parameters are integers.
|
||||
if (argument0 <= argument1) {
|
||||
// Count up.
|
||||
argument1++;
|
||||
if (argument0 == 0 && increment == 1) {
|
||||
// If starting index is 0, omit it.
|
||||
range = argument1;
|
||||
} else {
|
||||
range = argument0 + ', ' + argument1;
|
||||
}
|
||||
// If increment isn't 1, it must be explicit.
|
||||
if (increment != 1) {
|
||||
range += ', ' + increment;
|
||||
}
|
||||
} else {
|
||||
// Count down.
|
||||
argument1--;
|
||||
range = argument0 + ', ' + argument1 + ', -' + increment;
|
||||
}
|
||||
range = 'range(' + range + ')';
|
||||
} else {
|
||||
// At least one of the parameters is not an integer.
|
||||
if (argument0 < argument1) {
|
||||
range = defineUpRange();
|
||||
} else {
|
||||
range = defineDownRange();
|
||||
}
|
||||
range += '(' + argument0 + ', ' + argument1 + ', ' + increment + ')';
|
||||
}
|
||||
} else {
|
||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||
var scrub = function(arg, suffix) {
|
||||
if (Blockly.isNumber(arg)) {
|
||||
// Simple number.
|
||||
arg = parseFloat(arg);
|
||||
} else if (arg.match(/^\w+$/)) {
|
||||
// Variable.
|
||||
arg = 'float(' + arg + ')';
|
||||
} else {
|
||||
// It's complicated.
|
||||
var varName = Blockly.Python.variableDB_.getDistinctName(
|
||||
variable0 + suffix, Blockly.Variables.NAME_TYPE);
|
||||
code += varName + ' = float(' + arg + ')\n';
|
||||
arg = varName;
|
||||
}
|
||||
return arg;
|
||||
};
|
||||
var startVar = scrub(argument0, '_start');
|
||||
var endVar = scrub(argument1, '_end');
|
||||
var incVar = scrub(increment, '_inc');
|
||||
|
||||
if (typeof startVar == 'number' && typeof endVar == 'number') {
|
||||
if (startVar < endVar) {
|
||||
range = defineUpRange(startVar, endVar, increment);
|
||||
} else {
|
||||
range = defineDownRange(startVar, endVar, increment);
|
||||
}
|
||||
} else {
|
||||
// We cannot determine direction statically.
|
||||
range = generateUpDownRange(startVar, endVar, increment);
|
||||
}
|
||||
}
|
||||
code += 'for ' + variable0 + ' in ' + range + ':\n' + branch;
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['controls_forEach'] = function(block) {
|
||||
// For each loop.
|
||||
var variable0 = Blockly.Python.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'LIST',
|
||||
Blockly.Python.ORDER_RELATIONAL) || '[]';
|
||||
var branch = Blockly.Python.statementToCode(block, 'DO');
|
||||
branch = Blockly.Python.addLoopTrap(branch, block.id) ||
|
||||
Blockly.Python.PASS;
|
||||
var code = 'for ' + variable0 + ' in ' + argument0 + ':\n' + branch;
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['controls_flow_statements'] = function(block) {
|
||||
// Flow statements: continue, break.
|
||||
switch (block.getFieldValue('FLOW')) {
|
||||
case 'BREAK':
|
||||
return 'break\n';
|
||||
case 'CONTINUE':
|
||||
return 'continue\n';
|
||||
}
|
||||
throw 'Unknown flow statement.';
|
||||
};
|
||||
388
Blockly/brobot/generators/python/math.js
Normal file
388
Blockly/brobot/generators/python/math.js
Normal file
@@ -0,0 +1,388 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for math blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.math');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
|
||||
|
||||
// If any new block imports any library, add that library name here.
|
||||
Blockly.Python.addReservedWords('math,random,Number');
|
||||
|
||||
Blockly.Python['math_number'] = function(block) {
|
||||
// Numeric value.
|
||||
var code = parseFloat(block.getFieldValue('NUM'));
|
||||
var order;
|
||||
if (code == Infinity) {
|
||||
code = 'float("inf")';
|
||||
order = Blockly.Python.ORDER_FUNCTION_CALL;
|
||||
} else if (code == -Infinity) {
|
||||
code = '-float("inf")';
|
||||
order = Blockly.Python.ORDER_UNARY_SIGN;
|
||||
} else {
|
||||
order = code < 0 ? Blockly.Python.ORDER_UNARY_SIGN :
|
||||
Blockly.Python.ORDER_ATOMIC;
|
||||
}
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Python['math_arithmetic'] = function(block) {
|
||||
// Basic arithmetic operators, and power.
|
||||
var OPERATORS = {
|
||||
'ADD': [' + ', Blockly.Python.ORDER_ADDITIVE],
|
||||
'MINUS': [' - ', Blockly.Python.ORDER_ADDITIVE],
|
||||
'MULTIPLY': [' * ', Blockly.Python.ORDER_MULTIPLICATIVE],
|
||||
'DIVIDE': [' / ', Blockly.Python.ORDER_MULTIPLICATIVE],
|
||||
'POWER': [' ** ', Blockly.Python.ORDER_EXPONENTIATION]
|
||||
};
|
||||
var tuple = OPERATORS[block.getFieldValue('OP')];
|
||||
var operator = tuple[0];
|
||||
var order = tuple[1];
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.Python.valueToCode(block, 'B', order) || '0';
|
||||
var code = argument0 + operator + argument1;
|
||||
return [code, order];
|
||||
// In case of 'DIVIDE', division between integers returns different results
|
||||
// in Python 2 and 3. However, is not an issue since Blockly does not
|
||||
// guarantee identical results in all languages. To do otherwise would
|
||||
// require every operator to be wrapped in a function call. This would kill
|
||||
// legibility of the generated code.
|
||||
};
|
||||
|
||||
Blockly.Python['math_single'] = function(block) {
|
||||
// Math operators with single operand.
|
||||
var operator = block.getFieldValue('OP');
|
||||
var code;
|
||||
var arg;
|
||||
if (operator == 'NEG') {
|
||||
// Negation is a special case given its different operator precedence.
|
||||
var code = Blockly.Python.valueToCode(block, 'NUM',
|
||||
Blockly.Python.ORDER_UNARY_SIGN) || '0';
|
||||
return ['-' + code, Blockly.Python.ORDER_UNARY_SIGN];
|
||||
}
|
||||
Blockly.Python.definitions_['import_math'] = 'import math';
|
||||
if (operator == 'SIN' || operator == 'COS' || operator == 'TAN') {
|
||||
arg = Blockly.Python.valueToCode(block, 'NUM',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
|
||||
} else {
|
||||
arg = Blockly.Python.valueToCode(block, 'NUM',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
}
|
||||
// First, handle cases which generate values that don't need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'ABS':
|
||||
code = 'math.fabs(' + arg + ')';
|
||||
break;
|
||||
case 'ROOT':
|
||||
code = 'math.sqrt(' + arg + ')';
|
||||
break;
|
||||
case 'LN':
|
||||
code = 'math.log(' + arg + ')';
|
||||
break;
|
||||
case 'LOG10':
|
||||
code = 'math.log10(' + arg + ')';
|
||||
break;
|
||||
case 'EXP':
|
||||
code = 'math.exp(' + arg + ')';
|
||||
break;
|
||||
case 'POW10':
|
||||
code = 'math.pow(10,' + arg + ')';
|
||||
break;
|
||||
case 'ROUND':
|
||||
code = 'round(' + arg + ')';
|
||||
break;
|
||||
case 'ROUNDUP':
|
||||
code = 'math.ceil(' + arg + ')';
|
||||
break;
|
||||
case 'ROUNDDOWN':
|
||||
code = 'math.floor(' + arg + ')';
|
||||
break;
|
||||
case 'SIN':
|
||||
code = 'math.sin(' + arg + ' / 180.0 * math.pi)';
|
||||
break;
|
||||
case 'COS':
|
||||
code = 'math.cos(' + arg + ' / 180.0 * math.pi)';
|
||||
break;
|
||||
case 'TAN':
|
||||
code = 'math.tan(' + arg + ' / 180.0 * math.pi)';
|
||||
break;
|
||||
}
|
||||
if (code) {
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
// Second, handle cases which generate values that may need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'ASIN':
|
||||
code = 'math.asin(' + arg + ') / math.pi * 180';
|
||||
break;
|
||||
case 'ACOS':
|
||||
code = 'math.acos(' + arg + ') / math.pi * 180';
|
||||
break;
|
||||
case 'ATAN':
|
||||
code = 'math.atan(' + arg + ') / math.pi * 180';
|
||||
break;
|
||||
default:
|
||||
throw 'Unknown math operator: ' + operator;
|
||||
}
|
||||
return [code, Blockly.Python.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
Blockly.Python['math_constant'] = function(block) {
|
||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||
var CONSTANTS = {
|
||||
'PI': ['math.pi', Blockly.Python.ORDER_MEMBER],
|
||||
'E': ['math.e', Blockly.Python.ORDER_MEMBER],
|
||||
'GOLDEN_RATIO': ['(1 + math.sqrt(5)) / 2',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE],
|
||||
'SQRT2': ['math.sqrt(2)', Blockly.Python.ORDER_MEMBER],
|
||||
'SQRT1_2': ['math.sqrt(1.0 / 2)', Blockly.Python.ORDER_MEMBER],
|
||||
'INFINITY': ['float(\'inf\')', Blockly.Python.ORDER_ATOMIC]
|
||||
};
|
||||
var constant = block.getFieldValue('CONSTANT');
|
||||
if (constant != 'INFINITY') {
|
||||
Blockly.Python.definitions_['import_math'] = 'import math';
|
||||
}
|
||||
return CONSTANTS[constant];
|
||||
};
|
||||
|
||||
Blockly.Python['math_number_property'] = function(block) {
|
||||
// Check if a number is even, odd, prime, whole, positive, or negative
|
||||
// or if it is divisible by certain number. Returns true or false.
|
||||
var number_to_check = Blockly.Python.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
|
||||
var dropdown_property = block.getFieldValue('PROPERTY');
|
||||
var code;
|
||||
if (dropdown_property == 'PRIME') {
|
||||
Blockly.Python.definitions_['import_math'] = 'import math';
|
||||
Blockly.Python.definitions_['from_numbers_import_Number'] =
|
||||
'from numbers import Number';
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'math_isPrime',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(n):',
|
||||
' # https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' # If n is not a number but a string, try parsing it.',
|
||||
' if not isinstance(n, Number):',
|
||||
' try:',
|
||||
' n = float(n)',
|
||||
' except:',
|
||||
' return False',
|
||||
' if n == 2 or n == 3:',
|
||||
' return True',
|
||||
' # False if n is negative, is 1, or not whole,' +
|
||||
' or if n is divisible by 2 or 3.',
|
||||
' if n <= 1 or n % 1 != 0 or n % 2 == 0 or n % 3 == 0:',
|
||||
' return False',
|
||||
' # Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for x in range(6, int(math.sqrt(n)) + 2, 6):',
|
||||
' if n % (x - 1) == 0 or n % (x + 1) == 0:',
|
||||
' return False',
|
||||
' return True']);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
switch (dropdown_property) {
|
||||
case 'EVEN':
|
||||
code = number_to_check + ' % 2 == 0';
|
||||
break;
|
||||
case 'ODD':
|
||||
code = number_to_check + ' % 2 == 1';
|
||||
break;
|
||||
case 'WHOLE':
|
||||
code = number_to_check + ' % 1 == 0';
|
||||
break;
|
||||
case 'POSITIVE':
|
||||
code = number_to_check + ' > 0';
|
||||
break;
|
||||
case 'NEGATIVE':
|
||||
code = number_to_check + ' < 0';
|
||||
break;
|
||||
case 'DIVISIBLE_BY':
|
||||
var divisor = Blockly.Python.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE);
|
||||
// If 'divisor' is some code that evals to 0, Python will raise an error.
|
||||
if (!divisor || divisor == '0') {
|
||||
return ['False', Blockly.Python.ORDER_ATOMIC];
|
||||
}
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
}
|
||||
return [code, Blockly.Python.ORDER_RELATIONAL];
|
||||
};
|
||||
|
||||
Blockly.Python['math_change'] = function(block) {
|
||||
// Add to a variable in place.
|
||||
Blockly.Python.definitions_['from_numbers_import_Number'] =
|
||||
'from numbers import Number';
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'DELTA',
|
||||
Blockly.Python.ORDER_ADDITIVE) || '0';
|
||||
var varName = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = (' + varName + ' if isinstance(' + varName +
|
||||
', Number) else 0) + ' + argument0 + '\n';
|
||||
};
|
||||
|
||||
// Rounding functions have a single operand.
|
||||
Blockly.Python['math_round'] = Blockly.Python['math_single'];
|
||||
// Trigonometry functions have a single operand.
|
||||
Blockly.Python['math_trig'] = Blockly.Python['math_single'];
|
||||
|
||||
Blockly.Python['math_on_list'] = function(block) {
|
||||
// Math functions for lists.
|
||||
var func = block.getFieldValue('OP');
|
||||
var list = Blockly.Python.valueToCode(block, 'LIST',
|
||||
Blockly.Python.ORDER_NONE) || '[]';
|
||||
var code;
|
||||
switch (func) {
|
||||
case 'SUM':
|
||||
code = 'sum(' + list + ')';
|
||||
break;
|
||||
case 'MIN':
|
||||
code = 'min(' + list + ')';
|
||||
break;
|
||||
case 'MAX':
|
||||
code = 'max(' + list + ')';
|
||||
break;
|
||||
case 'AVERAGE':
|
||||
Blockly.Python.definitions_['from_numbers_import_Number'] =
|
||||
'from numbers import Number';
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'math_mean',
|
||||
// This operation excludes null and values that aren't int or float:',
|
||||
// math_mean([null, null, "aString", 1, 9]) == 5.0.',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):',
|
||||
' localList = [e for e in myList if isinstance(e, Number)]',
|
||||
' if not localList: return',
|
||||
' return float(sum(localList)) / len(localList)']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MEDIAN':
|
||||
Blockly.Python.definitions_['from_numbers_import_Number'] =
|
||||
'from numbers import Number';
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'math_median',
|
||||
// This operation excludes null values:
|
||||
// math_median([null, null, 1, 3]) == 2.0.
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(myList):',
|
||||
' localList = sorted([e for e in myList if isinstance(e, Number)])',
|
||||
' if not localList: return',
|
||||
' if len(localList) % 2 == 0:',
|
||||
' return (localList[len(localList) // 2 - 1] + ' +
|
||||
'localList[len(localList) // 2]) / 2.0',
|
||||
' else:',
|
||||
' return localList[(len(localList) - 1) // 2]']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MODE':
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'math_modes',
|
||||
// As a list of numbers can contain more than one mode,
|
||||
// the returned result is provided as an array.
|
||||
// Mode of [3, 'x', 'x', 1, 1, 2, '3'] -> ['x', 1].
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(some_list):',
|
||||
' modes = []',
|
||||
' # Using a lists of [item, count] to keep count rather than dict',
|
||||
' # to avoid "unhashable" errors when the counted item is ' +
|
||||
'itself a list or dict.',
|
||||
' counts = []',
|
||||
' maxCount = 1',
|
||||
' for item in some_list:',
|
||||
' found = False',
|
||||
' for count in counts:',
|
||||
' if count[0] == item:',
|
||||
' count[1] += 1',
|
||||
' maxCount = max(maxCount, count[1])',
|
||||
' found = True',
|
||||
' if not found:',
|
||||
' counts.append([item, 1])',
|
||||
' for counted_item, item_count in counts:',
|
||||
' if item_count == maxCount:',
|
||||
' modes.append(counted_item)',
|
||||
' return modes']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'STD_DEV':
|
||||
Blockly.Python.definitions_['import_math'] = 'import math';
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'math_standard_deviation',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(numbers):',
|
||||
' n = len(numbers)',
|
||||
' if n == 0: return',
|
||||
' mean = float(sum(numbers)) / n',
|
||||
' variance = sum((x - mean) ** 2 for x in numbers) / n',
|
||||
' return math.sqrt(variance)']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'RANDOM':
|
||||
Blockly.Python.definitions_['import_random'] = 'import random';
|
||||
code = 'random.choice(' + list + ')';
|
||||
break;
|
||||
default:
|
||||
throw 'Unknown operator: ' + func;
|
||||
}
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['math_modulo'] = function(block) {
|
||||
// Remainder computation.
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'DIVIDEND',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
|
||||
var argument1 = Blockly.Python.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Python.ORDER_MULTIPLICATIVE) || '0';
|
||||
var code = argument0 + ' % ' + argument1;
|
||||
return [code, Blockly.Python.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
Blockly.Python['math_constrain'] = function(block) {
|
||||
// Constrain a number between two limits.
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Python.valueToCode(block, 'LOW',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
var argument2 = Blockly.Python.valueToCode(block, 'HIGH',
|
||||
Blockly.Python.ORDER_NONE) || 'float(\'inf\')';
|
||||
var code = 'min(max(' + argument0 + ', ' + argument1 + '), ' +
|
||||
argument2 + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['math_random_int'] = function(block) {
|
||||
// Random integer between [X] and [Y].
|
||||
Blockly.Python.definitions_['import_random'] = 'import random';
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'FROM',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Python.valueToCode(block, 'TO',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
var code = 'random.randint(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['math_random_float'] = function(block) {
|
||||
// Random fraction between 0 and 1.
|
||||
Blockly.Python.definitions_['import_random'] = 'import random';
|
||||
return ['random.random()', Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
120
Blockly/brobot/generators/python/procedures.js
Normal file
120
Blockly/brobot/generators/python/procedures.js
Normal file
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for procedure blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.procedures');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
|
||||
|
||||
Blockly.Python['procedures_defreturn'] = function(block) {
|
||||
// Define a procedure with a return value.
|
||||
// First, add a 'global' statement for every variable that is not shadowed by
|
||||
// a local parameter.
|
||||
var globals = [];
|
||||
for (var i = 0, varName; varName = block.workspace.variableList[i]; i++) {
|
||||
if (block.arguments_.indexOf(varName) == -1) {
|
||||
globals.push(Blockly.Python.variableDB_.getName(varName,
|
||||
Blockly.Variables.NAME_TYPE));
|
||||
}
|
||||
}
|
||||
globals = globals.length ? ' global ' + globals.join(', ') + '\n' : '';
|
||||
var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE);
|
||||
var branch = Blockly.Python.statementToCode(block, 'STACK');
|
||||
if (Blockly.Python.STATEMENT_PREFIX) {
|
||||
branch = Blockly.Python.prefixLines(
|
||||
Blockly.Python.STATEMENT_PREFIX.replace(/%1/g,
|
||||
'\'' + block.id + '\''), Blockly.Python.INDENT) + branch;
|
||||
}
|
||||
if (Blockly.Python.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'"' + block.id + '"') + branch;
|
||||
}
|
||||
var returnValue = Blockly.Python.valueToCode(block, 'RETURN',
|
||||
Blockly.Python.ORDER_NONE) || '';
|
||||
if (returnValue) {
|
||||
returnValue = ' return ' + returnValue + '\n';
|
||||
} else if (!branch) {
|
||||
branch = Blockly.Python.PASS;
|
||||
}
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.Python.variableDB_.getName(block.arguments_[i],
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
}
|
||||
var code = 'def ' + funcName + '(' + args.join(', ') + '):\n' +
|
||||
globals + branch + returnValue;
|
||||
code = Blockly.Python.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
Blockly.Python.definitions_['%' + funcName] = code;
|
||||
return null;
|
||||
};
|
||||
|
||||
// Defining a procedure without a return value uses the same generator as
|
||||
// a procedure with a return value.
|
||||
Blockly.Python['procedures_defnoreturn'] =
|
||||
Blockly.Python['procedures_defreturn'];
|
||||
|
||||
Blockly.Python['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.Python.valueToCode(block, 'ARG' + i,
|
||||
Blockly.Python.ORDER_NONE) || 'None';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['procedures_callnoreturn'] = function(block) {
|
||||
// Call a procedure with no return value.
|
||||
var funcName = Blockly.Python.variableDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.Python.valueToCode(block, 'ARG' + i,
|
||||
Blockly.Python.ORDER_NONE) || 'None';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ')\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
var condition = Blockly.Python.valueToCode(block, 'CONDITION',
|
||||
Blockly.Python.ORDER_NONE) || 'False';
|
||||
var code = 'if ' + condition + ':\n';
|
||||
if (block.hasReturnValue_) {
|
||||
var value = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || 'None';
|
||||
code += ' return ' + value + '\n';
|
||||
} else {
|
||||
code += ' return\n';
|
||||
}
|
||||
return code;
|
||||
};
|
||||
253
Blockly/brobot/generators/python/text.js
Normal file
253
Blockly/brobot/generators/python/text.js
Normal file
@@ -0,0 +1,253 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for text blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.texts');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
|
||||
|
||||
Blockly.Python['text'] = function(block) {
|
||||
// Text value.
|
||||
var code = Blockly.Python.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Blockly.Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Python['text_join'] = function(block) {
|
||||
// Create a string made up of any number of elements of any type.
|
||||
//Should we allow joining by '-' or ',' or any other characters?
|
||||
switch (block.itemCount_) {
|
||||
case 0:
|
||||
return ['\'\'', Blockly.Python.ORDER_ATOMIC];
|
||||
break;
|
||||
case 1:
|
||||
var element = Blockly.Python.valueToCode(block, 'ADD0',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
var code = 'str(' + element + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
break;
|
||||
case 2:
|
||||
var element0 = Blockly.Python.valueToCode(block, 'ADD0',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
var element1 = Blockly.Python.valueToCode(block, 'ADD1',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
var code = 'str(' + element0 + ') + str(' + element1 + ')';
|
||||
return [code, Blockly.Python.ORDER_ADDITIVE];
|
||||
break;
|
||||
default:
|
||||
var elements = [];
|
||||
for (var i = 0; i < block.itemCount_; i++) {
|
||||
elements[i] = Blockly.Python.valueToCode(block, 'ADD' + i,
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var tempVar = Blockly.Python.variableDB_.getDistinctName('x',
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
var code = '\'\'.join([str(' + tempVar + ') for ' + tempVar + ' in [' +
|
||||
elements.join(', ') + ']])';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Python['text_append'] = function(block) {
|
||||
// Append to a variable in place.
|
||||
var varName = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
var value = Blockly.Python.valueToCode(block, 'TEXT',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
return varName + ' = str(' + varName + ') + str(' + value + ')\n';
|
||||
};
|
||||
|
||||
Blockly.Python['text_length'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var text = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
return ['len(' + text + ')', Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['text_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var text = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
var code = 'not len(' + text + ')';
|
||||
return [code, Blockly.Python.ORDER_LOGICAL_NOT];
|
||||
};
|
||||
|
||||
Blockly.Python['text_indexOf'] = function(block) {
|
||||
// Search the text for a substring.
|
||||
// Should we allow for non-case sensitive???
|
||||
var operator = block.getFieldValue('END') == 'FIRST' ? 'find' : 'rfind';
|
||||
var substring = Blockly.Python.valueToCode(block, 'FIND',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
var text = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var code = text + '.' + operator + '(' + substring + ')';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.Python.ORDER_ADDITIVE];
|
||||
}
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['text_charAt'] = function(block) {
|
||||
// Get letter at index.
|
||||
// Note: Until January 2013 this block did not have the WHERE input.
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var text = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
var code = text + '[0]';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
case 'LAST':
|
||||
var code = text + '[-1]';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
case 'FROM_START':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT');
|
||||
var code = text + '[' + at + ']';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
case 'FROM_END':
|
||||
var at = Blockly.Python.getAdjustedInt(block, 'AT', 1, true);
|
||||
var code = text + '[' + at + ']';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
case 'RANDOM':
|
||||
Blockly.Python.definitions_['import_random'] = 'import random';
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'text_random_letter',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(text):',
|
||||
' x = int(random.random() * len(text))',
|
||||
' return text[x];']);
|
||||
code = functionName + '(' + text + ')';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
throw 'Unhandled option (text_charAt).';
|
||||
};
|
||||
|
||||
Blockly.Python['text_getSubstring'] = function(block) {
|
||||
// Get substring.
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
var text = Blockly.Python.valueToCode(block, 'STRING',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
var at1 = Blockly.Python.getAdjustedInt(block, 'AT1');
|
||||
if (at1 == '0') {
|
||||
at1 = '';
|
||||
}
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at1 = Blockly.Python.getAdjustedInt(block, 'AT1', 1, true);
|
||||
break;
|
||||
case 'FIRST':
|
||||
var at1 = '';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (text_getSubstring)';
|
||||
}
|
||||
switch (where2) {
|
||||
case 'FROM_START':
|
||||
var at2 = Blockly.Python.getAdjustedInt(block, 'AT2', 1);
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at2 = Blockly.Python.getAdjustedInt(block, 'AT2', 0, true);
|
||||
// Ensure that if the result calculated is 0 that sub-sequence will
|
||||
// include all elements as expected.
|
||||
if (!Blockly.isNumber(String(at2))) {
|
||||
Blockly.Python.definitions_['import_sys'] = 'import sys';
|
||||
at2 += ' or sys.maxsize';
|
||||
} else if (at2 == '0') {
|
||||
at2 = '';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
var at2 = '';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (text_getSubstring)';
|
||||
}
|
||||
var code = text + '[' + at1 + ' : ' + at2 + ']';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
Blockly.Python['text_changeCase'] = function(block) {
|
||||
// Change capitalization.
|
||||
var OPERATORS = {
|
||||
'UPPERCASE': '.upper()',
|
||||
'LOWERCASE': '.lower()',
|
||||
'TITLECASE': '.title()'
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('CASE')];
|
||||
var text = Blockly.Python.valueToCode(block, 'TEXT',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var code = text + operator;
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['text_trim'] = function(block) {
|
||||
// Trim spaces.
|
||||
var OPERATORS = {
|
||||
'LEFT': '.lstrip()',
|
||||
'RIGHT': '.rstrip()',
|
||||
'BOTH': '.strip()'
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
var text = Blockly.Python.valueToCode(block, 'TEXT',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var code = text + operator;
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['text_print'] = function(block) {
|
||||
// Print statement.
|
||||
var msg = Blockly.Python.valueToCode(block, 'TEXT',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
return 'print(' + msg + ')\n';
|
||||
};
|
||||
|
||||
Blockly.Python['text_prompt_ext'] = function(block) {
|
||||
// Prompt function.
|
||||
var functionName = Blockly.Python.provideFunction_(
|
||||
'text_prompt',
|
||||
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(msg):',
|
||||
' try:',
|
||||
' return raw_input(msg)',
|
||||
' except NameError:',
|
||||
' return input(msg)']);
|
||||
if (block.getField('TEXT')) {
|
||||
// Internal message.
|
||||
var msg = Blockly.Python.quote_(block.getFieldValue('TEXT'));
|
||||
} else {
|
||||
// External message.
|
||||
var msg = Blockly.Python.valueToCode(block, 'TEXT',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var code = functionName + '(' + msg + ')';
|
||||
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
|
||||
if (toNumber) {
|
||||
code = 'float(' + code + ')';
|
||||
}
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.Python['text_prompt'] = Blockly.Python['text_prompt_ext'];
|
||||
46
Blockly/brobot/generators/python/variables.js
Normal file
46
Blockly/brobot/generators/python/variables.js
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* https://developers.google.com/blockly/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Python for variable blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.variables');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
|
||||
|
||||
Blockly.Python['variables_get'] = function(block) {
|
||||
// Variable getter.
|
||||
var code = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return [code, Blockly.Python.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Python['variables_set'] = function(block) {
|
||||
// Variable setter.
|
||||
var argument0 = Blockly.Python.valueToCode(block, 'VALUE',
|
||||
Blockly.Python.ORDER_NONE) || '0';
|
||||
var varName = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = ' + argument0 + '\n';
|
||||
};
|
||||
Reference in New Issue
Block a user