Initial commit
This commit is contained in:
286
node_modules/node-blockly/blockly/generators/dart.js
generated
vendored
Normal file
286
node_modules/node-blockly/blockly/generators/dart.js
generated
vendored
Normal file
@@ -0,0 +1,286 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2014 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 Dart for blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart');
|
||||
|
||||
goog.require('Blockly.Generator');
|
||||
|
||||
|
||||
/**
|
||||
* Dart code generator.
|
||||
* @type {!Blockly.Generator}
|
||||
*/
|
||||
Blockly.Dart = new Blockly.Generator('Dart');
|
||||
|
||||
/**
|
||||
* 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.Dart.addReservedWords(
|
||||
// https://www.dartlang.org/docs/spec/latest/dart-language-specification.pdf
|
||||
// Section 16.1.1
|
||||
'assert,break,case,catch,class,const,continue,default,do,else,enum,' +
|
||||
'extends,false,final,finally,for,if,in,is,new,null,rethrow,return,super,' +
|
||||
'switch,this,throw,true,try,var,void,while,with,' +
|
||||
// https://api.dartlang.org/dart_core.html
|
||||
'print,identityHashCode,identical,BidirectionalIterator,Comparable,' +
|
||||
'double,Function,int,Invocation,Iterable,Iterator,List,Map,Match,num,' +
|
||||
'Pattern,RegExp,Set,StackTrace,String,StringSink,Type,bool,DateTime,' +
|
||||
'Deprecated,Duration,Expando,Null,Object,RuneIterator,Runes,Stopwatch,' +
|
||||
'StringBuffer,Symbol,Uri,Comparator,AbstractClassInstantiationError,' +
|
||||
'ArgumentError,AssertionError,CastError,ConcurrentModificationError,' +
|
||||
'CyclicInitializationError,Error,Exception,FallThroughError,' +
|
||||
'FormatException,IntegerDivisionByZeroException,NoSuchMethodError,' +
|
||||
'NullThrownError,OutOfMemoryError,RangeError,StackOverflowError,' +
|
||||
'StateError,TypeError,UnimplementedError,UnsupportedError'
|
||||
);
|
||||
|
||||
/**
|
||||
* Order of operation ENUMs.
|
||||
* https://www.dartlang.org/docs/dart-up-and-running/ch02.html#operator_table
|
||||
*/
|
||||
Blockly.Dart.ORDER_ATOMIC = 0; // 0 "" ...
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX = 1; // expr++ expr-- () [] . ?.
|
||||
Blockly.Dart.ORDER_UNARY_PREFIX = 2; // -expr !expr ~expr ++expr --expr
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE = 3; // * / % ~/
|
||||
Blockly.Dart.ORDER_ADDITIVE = 4; // + -
|
||||
Blockly.Dart.ORDER_SHIFT = 5; // << >>
|
||||
Blockly.Dart.ORDER_BITWISE_AND = 6; // &
|
||||
Blockly.Dart.ORDER_BITWISE_XOR = 7; // ^
|
||||
Blockly.Dart.ORDER_BITWISE_OR = 8; // |
|
||||
Blockly.Dart.ORDER_RELATIONAL = 9; // >= > <= < as is is!
|
||||
Blockly.Dart.ORDER_EQUALITY = 10; // == !=
|
||||
Blockly.Dart.ORDER_LOGICAL_AND = 11; // &&
|
||||
Blockly.Dart.ORDER_LOGICAL_OR = 12; // ||
|
||||
Blockly.Dart.ORDER_IF_NULL = 13; // ??
|
||||
Blockly.Dart.ORDER_CONDITIONAL = 14; // expr ? expr : expr
|
||||
Blockly.Dart.ORDER_CASCADE = 15; // ..
|
||||
Blockly.Dart.ORDER_ASSIGNMENT = 16; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
|
||||
Blockly.Dart.ORDER_NONE = 99; // (...)
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
Blockly.Dart.init = function(workspace) {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly.Dart.definitions_ = Object.create(null);
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
// to actual function names (to avoid collisions with user functions).
|
||||
Blockly.Dart.functionNames_ = Object.create(null);
|
||||
|
||||
if (!Blockly.Dart.variableDB_) {
|
||||
Blockly.Dart.variableDB_ =
|
||||
new Blockly.Names(Blockly.Dart.RESERVED_WORDS_);
|
||||
} else {
|
||||
Blockly.Dart.variableDB_.reset();
|
||||
}
|
||||
|
||||
Blockly.Dart.variableDB_.setVariableMap(workspace.getVariableMap());
|
||||
|
||||
var defvars = [];
|
||||
// Add developer variables (not created or named by the user).
|
||||
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
for (var i = 0; i < devVarList.length; i++) {
|
||||
defvars.push(Blockly.Dart.variableDB_.getName(devVarList[i],
|
||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
|
||||
}
|
||||
|
||||
// Add user variables, but only ones that are being used.
|
||||
var variables = Blockly.Variables.allUsedVarModels(workspace);
|
||||
for (var i = 0; i < variables.length; i++) {
|
||||
defvars.push(Blockly.Dart.variableDB_.getName(variables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE));
|
||||
}
|
||||
|
||||
// Declare all of the variables.
|
||||
if (defvars.length) {
|
||||
Blockly.Dart.definitions_['variables'] =
|
||||
'var ' + defvars.join(', ') + ';';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepend the generated code with the variable definitions.
|
||||
* @param {string} code Generated code.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
Blockly.Dart.finish = function(code) {
|
||||
// Indent every line.
|
||||
if (code) {
|
||||
code = Blockly.Dart.prefixLines(code, Blockly.Dart.INDENT);
|
||||
}
|
||||
code = 'main() {\n' + code + '}';
|
||||
|
||||
// Convert the definitions dictionary into a list.
|
||||
var imports = [];
|
||||
var definitions = [];
|
||||
for (var name in Blockly.Dart.definitions_) {
|
||||
var def = Blockly.Dart.definitions_[name];
|
||||
if (def.match(/^import\s/)) {
|
||||
imports.push(def);
|
||||
} else {
|
||||
definitions.push(def);
|
||||
}
|
||||
}
|
||||
// Clean up temporary data.
|
||||
delete Blockly.Dart.definitions_;
|
||||
delete Blockly.Dart.functionNames_;
|
||||
Blockly.Dart.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. A trailing semicolon is needed to make this legal.
|
||||
* @param {string} line Line of generated code.
|
||||
* @return {string} Legal line of code.
|
||||
*/
|
||||
Blockly.Dart.scrubNakedValue = function(line) {
|
||||
return line + ';\n';
|
||||
};
|
||||
|
||||
/**
|
||||
* Encode a string as a properly escaped Dart string, complete with quotes.
|
||||
* @param {string} string Text to encode.
|
||||
* @return {string} Dart string.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Dart.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 Dart 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 Dart code created for this block.
|
||||
* @return {string} Dart code with comments and subsequent blocks added.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Dart.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.Dart.COMMENT_WRAP - 3);
|
||||
if (comment) {
|
||||
if (block.getProcedureDef) {
|
||||
// Use documentation comment for function comments.
|
||||
commentCode += Blockly.Dart.prefixLines(comment + '\n', '/// ');
|
||||
} else {
|
||||
commentCode += Blockly.Dart.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.Dart.allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += Blockly.Dart.prefixLines(comment, '// ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
|
||||
var nextCode = Blockly.Dart.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.Dart.getAdjusted = function(block, atId, opt_delta, opt_negate,
|
||||
opt_order) {
|
||||
var delta = opt_delta || 0;
|
||||
var order = opt_order || Blockly.Dart.ORDER_NONE;
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
delta--;
|
||||
}
|
||||
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
|
||||
if (delta) {
|
||||
var at = Blockly.Dart.valueToCode(block, atId,
|
||||
Blockly.Dart.ORDER_ADDITIVE) || defaultAtIndex;
|
||||
} else if (opt_negate) {
|
||||
var at = Blockly.Dart.valueToCode(block, atId,
|
||||
Blockly.Dart.ORDER_UNARY_PREFIX) || defaultAtIndex;
|
||||
} else {
|
||||
var at = Blockly.Dart.valueToCode(block, atId, order) ||
|
||||
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 = at + ' + ' + delta;
|
||||
var innerOrder = Blockly.Dart.ORDER_ADDITIVE;
|
||||
} else if (delta < 0) {
|
||||
at = at + ' - ' + -delta;
|
||||
var innerOrder = Blockly.Dart.ORDER_ADDITIVE;
|
||||
}
|
||||
if (opt_negate) {
|
||||
if (delta) {
|
||||
at = '-(' + at + ')';
|
||||
} else {
|
||||
at = '-' + at;
|
||||
}
|
||||
var innerOrder = Blockly.Dart.ORDER_UNARY_PREFIX;
|
||||
}
|
||||
innerOrder = Math.floor(innerOrder);
|
||||
order = Math.floor(order);
|
||||
if (innerOrder && order >= innerOrder) {
|
||||
at = '(' + at + ')';
|
||||
}
|
||||
}
|
||||
return at;
|
||||
};
|
||||
128
node_modules/node-blockly/blockly/generators/dart/colour.js
generated
vendored
Normal file
128
node_modules/node-blockly/blockly/generators/dart/colour.js
generated
vendored
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2014 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 Dart for colour blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.colour');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart.addReservedWords('Math');
|
||||
|
||||
Blockly.Dart['colour_picker'] = function(block) {
|
||||
// Colour picker.
|
||||
var code = '\'' + block.getFieldValue('COLOUR') + '\'';
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart['colour_random'] = function(block) {
|
||||
// Generate a random colour.
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'colour_random',
|
||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '() {',
|
||||
' String hex = \'0123456789abcdef\';',
|
||||
' var rnd = new Math.Random();',
|
||||
' return \'#${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\'',
|
||||
' \'${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\'',
|
||||
' \'${hex[rnd.nextInt(16)]}${hex[rnd.nextInt(16)]}\';',
|
||||
'}']);
|
||||
var code = functionName + '()';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['colour_rgb'] = function(block) {
|
||||
// Compose a colour from RGB components expressed as percentages.
|
||||
var red = Blockly.Dart.valueToCode(block, 'RED',
|
||||
Blockly.Dart.ORDER_NONE) || 0;
|
||||
var green = Blockly.Dart.valueToCode(block, 'GREEN',
|
||||
Blockly.Dart.ORDER_NONE) || 0;
|
||||
var blue = Blockly.Dart.valueToCode(block, 'BLUE',
|
||||
Blockly.Dart.ORDER_NONE) || 0;
|
||||
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'colour_rgb',
|
||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(num r, num g, num b) {',
|
||||
' num rn = (Math.max(Math.min(r, 100), 0) * 2.55).round();',
|
||||
' String rs = rn.toInt().toRadixString(16);',
|
||||
' rs = \'0$rs\';',
|
||||
' rs = rs.substring(rs.length - 2);',
|
||||
' num gn = (Math.max(Math.min(g, 100), 0) * 2.55).round();',
|
||||
' String gs = gn.toInt().toRadixString(16);',
|
||||
' gs = \'0$gs\';',
|
||||
' gs = gs.substring(gs.length - 2);',
|
||||
' num bn = (Math.max(Math.min(b, 100), 0) * 2.55).round();',
|
||||
' String bs = bn.toInt().toRadixString(16);',
|
||||
' bs = \'0$bs\';',
|
||||
' bs = bs.substring(bs.length - 2);',
|
||||
' return \'#$rs$gs$bs\';',
|
||||
'}']);
|
||||
var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['colour_blend'] = function(block) {
|
||||
// Blend two colours together.
|
||||
var c1 = Blockly.Dart.valueToCode(block, 'COLOUR1',
|
||||
Blockly.Dart.ORDER_NONE) || '\'#000000\'';
|
||||
var c2 = Blockly.Dart.valueToCode(block, 'COLOUR2',
|
||||
Blockly.Dart.ORDER_NONE) || '\'#000000\'';
|
||||
var ratio = Blockly.Dart.valueToCode(block, 'RATIO',
|
||||
Blockly.Dart.ORDER_NONE) || 0.5;
|
||||
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'colour_blend',
|
||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(String c1, String c2, num ratio) {',
|
||||
' ratio = Math.max(Math.min(ratio, 1), 0);',
|
||||
' int r1 = int.parse(\'0x${c1.substring(1, 3)}\');',
|
||||
' int g1 = int.parse(\'0x${c1.substring(3, 5)}\');',
|
||||
' int b1 = int.parse(\'0x${c1.substring(5, 7)}\');',
|
||||
' int r2 = int.parse(\'0x${c2.substring(1, 3)}\');',
|
||||
' int g2 = int.parse(\'0x${c2.substring(3, 5)}\');',
|
||||
' int b2 = int.parse(\'0x${c2.substring(5, 7)}\');',
|
||||
' num rn = (r1 * (1 - ratio) + r2 * ratio).round();',
|
||||
' String rs = rn.toInt().toRadixString(16);',
|
||||
' num gn = (g1 * (1 - ratio) + g2 * ratio).round();',
|
||||
' String gs = gn.toInt().toRadixString(16);',
|
||||
' num bn = (b1 * (1 - ratio) + b2 * ratio).round();',
|
||||
' String bs = bn.toInt().toRadixString(16);',
|
||||
' rs = \'0$rs\';',
|
||||
' rs = rs.substring(rs.length - 2);',
|
||||
' gs = \'0$gs\';',
|
||||
' gs = gs.substring(gs.length - 2);',
|
||||
' bs = \'0$bs\';',
|
||||
' bs = bs.substring(bs.length - 2);',
|
||||
' return \'#$rs$gs$bs\';',
|
||||
'}']);
|
||||
var code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
461
node_modules/node-blockly/blockly/generators/dart/lists.js
generated
vendored
Normal file
461
node_modules/node-blockly/blockly/generators/dart/lists.js
generated
vendored
Normal file
@@ -0,0 +1,461 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2014 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 Dart for list blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.lists');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart.addReservedWords('Math');
|
||||
|
||||
Blockly.Dart['lists_create_empty'] = function(block) {
|
||||
// Create an empty list.
|
||||
return ['[]', Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart['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.Dart.valueToCode(block, 'ADD' + i,
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
}
|
||||
var code = '[' + elements.join(', ') + ']';
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart['lists_repeat'] = function(block) {
|
||||
// Create a list with one element repeated.
|
||||
var element = Blockly.Dart.valueToCode(block, 'ITEM',
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
var repeatCount = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
var code = 'new List.filled(' + repeatCount + ', ' + element + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['lists_length'] = function(block) {
|
||||
// String or array length.
|
||||
var list = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
return [list + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['lists_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var list = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
return [list + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['lists_indexOf'] = function(block) {
|
||||
// Find an item in the list.
|
||||
var operator = block.getFieldValue('END') == 'FIRST' ?
|
||||
'indexOf' : 'lastIndexOf';
|
||||
var item = Blockly.Dart.valueToCode(block, 'FIND',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var list = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
var code = list + '.' + operator + '(' + item + ')';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE];
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['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' || where == 'FROM_END') ?
|
||||
Blockly.Dart.ORDER_NONE : Blockly.Dart.ORDER_UNARY_POSTFIX;
|
||||
var list = Blockly.Dart.valueToCode(block, 'VALUE', listOrder) || '[]';
|
||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||
// Closure, which accesses and modifies 'list'.
|
||||
function cacheList() {
|
||||
var listVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'tmp_list', Blockly.Variables.NAME_TYPE);
|
||||
var code = 'List ' + listVar + ' = ' + list + ';\n';
|
||||
list = listVar;
|
||||
return code;
|
||||
}
|
||||
// If `list` would be evaluated more than once (which is the case for
|
||||
// RANDOM REMOVE and FROM_END) and is non-trivial, make sure to access it
|
||||
// only once.
|
||||
if (((where == 'RANDOM' && mode == 'REMOVE') || where == 'FROM_END') &&
|
||||
!list.match(/^\w+$/)) {
|
||||
// `list` is an expression, so we may not evaluate it more than once.
|
||||
if (where == 'RANDOM') {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
// We can use multiple statements.
|
||||
var code = cacheList();
|
||||
var xVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'tmp_x', Blockly.Variables.NAME_TYPE);
|
||||
code += 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
|
||||
'.length);\n';
|
||||
code += list + '.removeAt(' + xVar + ');\n';
|
||||
return code;
|
||||
} else { // where == 'FROM_END'
|
||||
if (mode == 'REMOVE') {
|
||||
// We can use multiple statements.
|
||||
var at = Blockly.Dart.getAdjusted(block, 'AT', 1, false,
|
||||
Blockly.Dart.ORDER_ADDITIVE);
|
||||
var code = cacheList();
|
||||
code += list + '.removeAt(' + list + '.length' + ' - ' + at + ');\n';
|
||||
return code;
|
||||
|
||||
} else if (mode == 'GET') {
|
||||
var at = Blockly.Dart.getAdjusted(block, 'AT', 1);
|
||||
// We need to create a procedure to avoid reevaluating values.
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'lists_get_from_end',
|
||||
['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List my_list, num x) {',
|
||||
' x = my_list.length - x;',
|
||||
' return my_list[x];',
|
||||
'}']);
|
||||
var code = functionName + '(' + list + ', ' + at + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var at = Blockly.Dart.getAdjusted(block, 'AT', 1);
|
||||
// We need to create a procedure to avoid reevaluating values.
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'lists_remove_from_end',
|
||||
['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List my_list, num x) {',
|
||||
' x = my_list.length - x;',
|
||||
' return my_list.removeAt(x);',
|
||||
'}']);
|
||||
var code = functionName + '(' + list + ', ' + at + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Either `list` is a simple variable, or we only need to refer to `list`
|
||||
// once.
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
if (mode == 'GET') {
|
||||
var code = list + '.first';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.removeAt(0)';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.removeAt(0);\n';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
if (mode == 'GET') {
|
||||
var code = list + '.last';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.removeLast()';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.removeLast();\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_START':
|
||||
var at = Blockly.Dart.getAdjusted(block, 'AT');
|
||||
if (mode == 'GET') {
|
||||
var code = list + '[' + at + ']';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.removeAt(' + at + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.removeAt(' + at + ');\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at = Blockly.Dart.getAdjusted(block, 'AT', 1, false,
|
||||
Blockly.Dart.ORDER_ADDITIVE);
|
||||
if (mode == 'GET') {
|
||||
var code = list + '[' + list + '.length - ' + at + ']';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'GET_REMOVE' || mode == 'REMOVE') {
|
||||
var code = list + '.removeAt(' + list + '.length - ' + at + ')';
|
||||
if (mode == 'GET_REMOVE') {
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return code + ';\n';
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'RANDOM':
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
if (mode == 'REMOVE') {
|
||||
// We can use multiple statements.
|
||||
var xVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'tmp_x', Blockly.Variables.NAME_TYPE);
|
||||
var code = 'int ' + xVar + ' = new Math.Random().nextInt(' + list +
|
||||
'.length);\n';
|
||||
code += list + '.removeAt(' + xVar + ');\n';
|
||||
return code;
|
||||
} else if (mode == 'GET') {
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'lists_get_random_item',
|
||||
['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List my_list) {',
|
||||
' int x = new Math.Random().nextInt(my_list.length);',
|
||||
' return my_list[x];',
|
||||
'}']);
|
||||
var code = functionName + '(' + list + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'lists_remove_random_item',
|
||||
['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List my_list) {',
|
||||
' int x = new Math.Random().nextInt(my_list.length);',
|
||||
' return my_list.removeAt(x);',
|
||||
'}']);
|
||||
var code = functionName + '(' + list + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw 'Unhandled combination (lists_getIndex).';
|
||||
};
|
||||
|
||||
Blockly.Dart['lists_setIndex'] = function(block) {
|
||||
// Set element at index.
|
||||
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
||||
var mode = block.getFieldValue('MODE') || 'GET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var list = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
var value = Blockly.Dart.valueToCode(block, 'TO',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || 'null';
|
||||
// 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.Dart.variableDB_.getDistinctName(
|
||||
'tmp_list', Blockly.Variables.NAME_TYPE);
|
||||
var code = 'List ' + 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') {
|
||||
var code = cacheList();
|
||||
code += list + '[' + list + '.length - 1] = ' + value + ';\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.add(' + value + ');\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_START':
|
||||
var at = Blockly.Dart.getAdjusted(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.Dart.getAdjusted(block, 'AT', 1, false,
|
||||
Blockly.Dart.ORDER_ADDITIVE);
|
||||
var code = cacheList();
|
||||
if (mode == 'SET') {
|
||||
code += list + '[' + list + '.length - ' + at + '] = ' + value +
|
||||
';\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
code += list + '.insert(' + list + '.length - ' + at + ', ' +
|
||||
value + ');\n';
|
||||
return code;
|
||||
}
|
||||
break;
|
||||
case 'RANDOM':
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var code = cacheList();
|
||||
var xVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'tmp_x', Blockly.Variables.NAME_TYPE);
|
||||
code += 'int ' + xVar +
|
||||
' = new Math.Random().nextInt(' + list + '.length);\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.Dart['lists_getSublist'] = function(block) {
|
||||
// Get sublist.
|
||||
var list = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
if (list.match(/^\w+$/) || (where1 != 'FROM_END' && where2 == 'FROM_START')) {
|
||||
// If the list is a is a variable or doesn't require a call for length,
|
||||
// don't generate a helper function.
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
var at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at1 = Blockly.Dart.getAdjusted(block, 'AT1', 1, false,
|
||||
Blockly.Dart.ORDER_ADDITIVE);
|
||||
at1 = list + '.length - ' + at1;
|
||||
break;
|
||||
case 'FIRST':
|
||||
var at1 = '0';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (lists_getSublist).';
|
||||
}
|
||||
switch (where2) {
|
||||
case 'FROM_START':
|
||||
var at2 = Blockly.Dart.getAdjusted(block, 'AT2', 1);
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at2 = Blockly.Dart.getAdjusted(block, 'AT2', 0, false,
|
||||
Blockly.Dart.ORDER_ADDITIVE);
|
||||
at2 = list + '.length - ' + at2;
|
||||
break;
|
||||
case 'LAST':
|
||||
// There is no second index if LAST option is chosen.
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (lists_getSublist).';
|
||||
}
|
||||
if (where2 == 'LAST') {
|
||||
var code = list + '.sublist(' + at1 + ')';
|
||||
} else {
|
||||
var code = list + '.sublist(' + at1 + ', ' + at2 + ')';
|
||||
}
|
||||
} else {
|
||||
var at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
||||
var at2 = Blockly.Dart.getAdjusted(block, 'AT2');
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'lists_get_sublist',
|
||||
['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(list, where1, at1, where2, at2) {',
|
||||
' int getAt(where, at) {',
|
||||
' if (where == \'FROM_END\') {',
|
||||
' at = list.length - 1 - at;',
|
||||
' } else if (where == \'FIRST\') {',
|
||||
' at = 0;',
|
||||
' } else if (where == \'LAST\') {',
|
||||
' at = list.length - 1;',
|
||||
' } else if (where != \'FROM_START\') {',
|
||||
' throw \'Unhandled option (lists_getSublist).\';',
|
||||
' }',
|
||||
' return at;',
|
||||
' }',
|
||||
' at1 = getAt(where1, at1);',
|
||||
' at2 = getAt(where2, at2) + 1;',
|
||||
' return list.sublist(at1, at2);',
|
||||
'}']);
|
||||
var code = functionName + '(' + list + ', \'' +
|
||||
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['lists_sort'] = function(block) {
|
||||
// Block for sorting a list.
|
||||
var list = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_NONE) || '[]';
|
||||
var direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
||||
var type = block.getFieldValue('TYPE');
|
||||
var sortFunctionName = Blockly.Dart.provideFunction_(
|
||||
'lists_sort',
|
||||
['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(list, type, direction) {',
|
||||
' var compareFuncs = {',
|
||||
' "NUMERIC": (a, b) => direction * a.compareTo(b),',
|
||||
' "TEXT": (a, b) => direction * ' +
|
||||
'a.toString().compareTo(b.toString()),',
|
||||
' "IGNORE_CASE": ',
|
||||
' (a, b) => direction * ',
|
||||
' a.toString().toLowerCase().compareTo(b.toString().toLowerCase())',
|
||||
' };',
|
||||
' list = new List.from(list);', // Clone the list.
|
||||
' var compare = compareFuncs[type];',
|
||||
' list.sort(compare);',
|
||||
' return list;',
|
||||
'}']);
|
||||
return [sortFunctionName + '(' + list + ', ' +
|
||||
'"' + type + '", ' + direction + ')',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['lists_split'] = function(block) {
|
||||
// Block for splitting text into a list, or joining a list into text.
|
||||
var input = Blockly.Dart.valueToCode(block, 'INPUT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX);
|
||||
var delimiter = Blockly.Dart.valueToCode(block, 'DELIM',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var mode = block.getFieldValue('MODE');
|
||||
if (mode == 'SPLIT') {
|
||||
if (!input) {
|
||||
input = '\'\'';
|
||||
}
|
||||
var functionName = 'split';
|
||||
} else if (mode == 'JOIN') {
|
||||
if (!input) {
|
||||
input = '[]';
|
||||
}
|
||||
var functionName = 'join';
|
||||
} else {
|
||||
throw 'Unknown mode: ' + mode;
|
||||
}
|
||||
var code = input + '.' + functionName + '(' + delimiter + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['lists_reverse'] = function(block) {
|
||||
// Block for reversing a list.
|
||||
var list = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_NONE) || '[]';
|
||||
// XXX What should the operator precedence be for a `new`?
|
||||
var code = 'new List.from(' + list + '.reversed)';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
128
node_modules/node-blockly/blockly/generators/dart/logic.js
generated
vendored
Normal file
128
node_modules/node-blockly/blockly/generators/dart/logic.js
generated
vendored
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2014 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 Dart for logic blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.logic');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart['controls_if'] = function(block) {
|
||||
// If/elseif/else condition.
|
||||
var n = 0;
|
||||
var code = '', branchCode, conditionCode;
|
||||
do {
|
||||
conditionCode = Blockly.Dart.valueToCode(block, 'IF' + n,
|
||||
Blockly.Dart.ORDER_NONE) || 'false';
|
||||
branchCode = Blockly.Dart.statementToCode(block, 'DO' + n);
|
||||
code += (n > 0 ? 'else ' : '') +
|
||||
'if (' + conditionCode + ') {\n' + branchCode + '}';
|
||||
|
||||
++n;
|
||||
} while (block.getInput('IF' + n));
|
||||
|
||||
if (block.getInput('ELSE')) {
|
||||
branchCode = Blockly.Dart.statementToCode(block, 'ELSE');
|
||||
code += ' else {\n' + branchCode + '}';
|
||||
}
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
Blockly.Dart['controls_ifelse'] = Blockly.Dart['controls_if'];
|
||||
|
||||
Blockly.Dart['logic_compare'] = function(block) {
|
||||
// Comparison operator.
|
||||
var OPERATORS = {
|
||||
'EQ': '==',
|
||||
'NEQ': '!=',
|
||||
'LT': '<',
|
||||
'LTE': '<=',
|
||||
'GT': '>',
|
||||
'GTE': '>='
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('OP')];
|
||||
var order = (operator == '==' || operator == '!=') ?
|
||||
Blockly.Dart.ORDER_EQUALITY : Blockly.Dart.ORDER_RELATIONAL;
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'B', order) || '0';
|
||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Dart['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
var operator = (block.getFieldValue('OP') == 'AND') ? '&&' : '||';
|
||||
var order = (operator == '&&') ? Blockly.Dart.ORDER_LOGICAL_AND :
|
||||
Blockly.Dart.ORDER_LOGICAL_OR;
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'A', order);
|
||||
var argument1 = Blockly.Dart.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 == '&&') ? 'true' : 'false';
|
||||
if (!argument0) {
|
||||
argument0 = defaultArgument;
|
||||
}
|
||||
if (!argument1) {
|
||||
argument1 = defaultArgument;
|
||||
}
|
||||
}
|
||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Dart['logic_negate'] = function(block) {
|
||||
// Negation.
|
||||
var order = Blockly.Dart.ORDER_UNARY_PREFIX;
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'BOOL', order) || 'true';
|
||||
var code = '!' + argument0;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Dart['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart['logic_null'] = function(block) {
|
||||
// Null data type.
|
||||
return ['null', Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart['logic_ternary'] = function(block) {
|
||||
// Ternary operator.
|
||||
var value_if = Blockly.Dart.valueToCode(block, 'IF',
|
||||
Blockly.Dart.ORDER_CONDITIONAL) || 'false';
|
||||
var value_then = Blockly.Dart.valueToCode(block, 'THEN',
|
||||
Blockly.Dart.ORDER_CONDITIONAL) || 'null';
|
||||
var value_else = Blockly.Dart.valueToCode(block, 'ELSE',
|
||||
Blockly.Dart.ORDER_CONDITIONAL) || 'null';
|
||||
var code = value_if + ' ? ' + value_then + ' : ' + value_else;
|
||||
return [code, Blockly.Dart.ORDER_CONDITIONAL];
|
||||
};
|
||||
163
node_modules/node-blockly/blockly/generators/dart/loops.js
generated
vendored
Normal file
163
node_modules/node-blockly/blockly/generators/dart/loops.js
generated
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2014 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 Dart for loop blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.loops');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart['controls_repeat_ext'] = function(block) {
|
||||
// Repeat n times.
|
||||
if (block.getField('TIMES')) {
|
||||
// Internal number.
|
||||
var repeats = String(Number(block.getFieldValue('TIMES')));
|
||||
} else {
|
||||
// External number.
|
||||
var repeats = Blockly.Dart.valueToCode(block, 'TIMES',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
||||
}
|
||||
var branch = Blockly.Dart.statementToCode(block, 'DO');
|
||||
branch = Blockly.Dart.addLoopTrap(branch, block.id);
|
||||
var code = '';
|
||||
var loopVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'count', Blockly.Variables.NAME_TYPE);
|
||||
var endVar = repeats;
|
||||
if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) {
|
||||
var endVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'repeat_end', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + endVar + ' = ' + repeats + ';\n';
|
||||
}
|
||||
code += 'for (int ' + loopVar + ' = 0; ' +
|
||||
loopVar + ' < ' + endVar + '; ' +
|
||||
loopVar + '++) {\n' +
|
||||
branch + '}\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Dart['controls_repeat'] = Blockly.Dart['controls_repeat_ext'];
|
||||
|
||||
Blockly.Dart['controls_whileUntil'] = function(block) {
|
||||
// Do while/until loop.
|
||||
var until = block.getFieldValue('MODE') == 'UNTIL';
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'BOOL',
|
||||
until ? Blockly.Dart.ORDER_UNARY_PREFIX :
|
||||
Blockly.Dart.ORDER_NONE) || 'false';
|
||||
var branch = Blockly.Dart.statementToCode(block, 'DO');
|
||||
branch = Blockly.Dart.addLoopTrap(branch, block.id);
|
||||
if (until) {
|
||||
argument0 = '!' + argument0;
|
||||
}
|
||||
return 'while (' + argument0 + ') {\n' + branch + '}\n';
|
||||
};
|
||||
|
||||
Blockly.Dart['controls_for'] = function(block) {
|
||||
// For loop.
|
||||
var variable0 = Blockly.Dart.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'FROM',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'TO',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
||||
var increment = Blockly.Dart.valueToCode(block, 'BY',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '1';
|
||||
var branch = Blockly.Dart.statementToCode(block, 'DO');
|
||||
branch = Blockly.Dart.addLoopTrap(branch, block.id);
|
||||
var code;
|
||||
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
|
||||
Blockly.isNumber(increment)) {
|
||||
// All arguments are simple numbers.
|
||||
var up = parseFloat(argument0) <= parseFloat(argument1);
|
||||
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
|
||||
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
|
||||
variable0;
|
||||
var step = Math.abs(parseFloat(increment));
|
||||
if (step == 1) {
|
||||
code += up ? '++' : '--';
|
||||
} else {
|
||||
code += (up ? ' += ' : ' -= ') + step;
|
||||
}
|
||||
code += ') {\n' + branch + '}\n';
|
||||
} else {
|
||||
code = '';
|
||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||
var startVar = argument0;
|
||||
if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) {
|
||||
var startVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
variable0 + '_start', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + startVar + ' = ' + argument0 + ';\n';
|
||||
}
|
||||
var endVar = argument1;
|
||||
if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) {
|
||||
var endVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
variable0 + '_end', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + endVar + ' = ' + argument1 + ';\n';
|
||||
}
|
||||
// Determine loop direction at start, in case one of the bounds
|
||||
// changes during loop execution.
|
||||
var incVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
|
||||
code += 'num ' + incVar + ' = ';
|
||||
if (Blockly.isNumber(increment)) {
|
||||
code += Math.abs(increment) + ';\n';
|
||||
} else {
|
||||
code += '(' + increment + ').abs();\n';
|
||||
}
|
||||
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
|
||||
code += Blockly.Dart.INDENT + incVar + ' = -' + incVar + ';\n';
|
||||
code += '}\n';
|
||||
code += 'for (' + variable0 + ' = ' + startVar + '; ' +
|
||||
incVar + ' >= 0 ? ' +
|
||||
variable0 + ' <= ' + endVar + ' : ' +
|
||||
variable0 + ' >= ' + endVar + '; ' +
|
||||
variable0 + ' += ' + incVar + ') {\n' +
|
||||
branch + '}\n';
|
||||
}
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Dart['controls_forEach'] = function(block) {
|
||||
// For each loop.
|
||||
var variable0 = Blockly.Dart.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '[]';
|
||||
var branch = Blockly.Dart.statementToCode(block, 'DO');
|
||||
branch = Blockly.Dart.addLoopTrap(branch, block.id);
|
||||
var code = 'for (var ' + variable0 + ' in ' + argument0 + ') {\n' +
|
||||
branch + '}\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Dart['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.';
|
||||
};
|
||||
487
node_modules/node-blockly/blockly/generators/dart/math.js
generated
vendored
Normal file
487
node_modules/node-blockly/blockly/generators/dart/math.js
generated
vendored
Normal file
@@ -0,0 +1,487 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2014 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 Dart for math blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.math');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart.addReservedWords('Math');
|
||||
|
||||
Blockly.Dart['math_number'] = function(block) {
|
||||
// Numeric value.
|
||||
var code = parseFloat(block.getFieldValue('NUM'));
|
||||
var order;
|
||||
if (code == Infinity) {
|
||||
code = 'double.INFINITY';
|
||||
order = Blockly.Dart.ORDER_UNARY_POSTFIX;
|
||||
} else if (code == -Infinity) {
|
||||
code = '-double.INFINITY';
|
||||
order = Blockly.Dart.ORDER_UNARY_PREFIX;
|
||||
} else {
|
||||
// -4.abs() returns -4 in Dart due to strange order of operation choices.
|
||||
// -4 is actually an operator and a number. Reflect this in the order.
|
||||
order = code < 0 ?
|
||||
Blockly.Dart.ORDER_UNARY_PREFIX : Blockly.Dart.ORDER_ATOMIC;
|
||||
}
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Dart['math_arithmetic'] = function(block) {
|
||||
// Basic arithmetic operators, and power.
|
||||
var OPERATORS = {
|
||||
'ADD': [' + ', Blockly.Dart.ORDER_ADDITIVE],
|
||||
'MINUS': [' - ', Blockly.Dart.ORDER_ADDITIVE],
|
||||
'MULTIPLY': [' * ', Blockly.Dart.ORDER_MULTIPLICATIVE],
|
||||
'DIVIDE': [' / ', Blockly.Dart.ORDER_MULTIPLICATIVE],
|
||||
'POWER': [null, Blockly.Dart.ORDER_NONE] // Handle power separately.
|
||||
};
|
||||
var tuple = OPERATORS[block.getFieldValue('OP')];
|
||||
var operator = tuple[0];
|
||||
var order = tuple[1];
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'B', order) || '0';
|
||||
var code;
|
||||
// Power in Dart requires a special case since it has no operator.
|
||||
if (!operator) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
code = 'Math.pow(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
code = argument0 + operator + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Dart['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.
|
||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_UNARY_PREFIX) || '0';
|
||||
if (arg[0] == '-') {
|
||||
// --3 is not legal in Dart.
|
||||
arg = ' ' + arg;
|
||||
}
|
||||
code = '-' + arg;
|
||||
return [code, Blockly.Dart.ORDER_UNARY_PREFIX];
|
||||
}
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
if (operator == 'ABS' || operator.substring(0, 5) == 'ROUND') {
|
||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '0';
|
||||
} else if (operator == 'SIN' || operator == 'COS' || operator == 'TAN') {
|
||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE) || '0';
|
||||
} else {
|
||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
}
|
||||
// First, handle cases which generate values that don't need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'ABS':
|
||||
code = arg + '.abs()';
|
||||
break;
|
||||
case 'ROOT':
|
||||
code = 'Math.sqrt(' + arg + ')';
|
||||
break;
|
||||
case 'LN':
|
||||
code = 'Math.log(' + arg + ')';
|
||||
break;
|
||||
case 'EXP':
|
||||
code = 'Math.exp(' + arg + ')';
|
||||
break;
|
||||
case 'POW10':
|
||||
code = 'Math.pow(10,' + arg + ')';
|
||||
break;
|
||||
case 'ROUND':
|
||||
code = arg + '.round()';
|
||||
break;
|
||||
case 'ROUNDUP':
|
||||
code = arg + '.ceil()';
|
||||
break;
|
||||
case 'ROUNDDOWN':
|
||||
code = arg + '.floor()';
|
||||
break;
|
||||
case 'SIN':
|
||||
code = 'Math.sin(' + arg + ' / 180 * Math.PI)';
|
||||
break;
|
||||
case 'COS':
|
||||
code = 'Math.cos(' + arg + ' / 180 * Math.PI)';
|
||||
break;
|
||||
case 'TAN':
|
||||
code = 'Math.tan(' + arg + ' / 180 * Math.PI)';
|
||||
break;
|
||||
}
|
||||
if (code) {
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
// Second, handle cases which generate values that may need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'LOG10':
|
||||
code = 'Math.log(' + arg + ') / Math.log(10)';
|
||||
break;
|
||||
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.Dart.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
Blockly.Dart['math_constant'] = function(block) {
|
||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||
var CONSTANTS = {
|
||||
'PI': ['Math.PI', Blockly.Dart.ORDER_UNARY_POSTFIX],
|
||||
'E': ['Math.E', Blockly.Dart.ORDER_UNARY_POSTFIX],
|
||||
'GOLDEN_RATIO':
|
||||
['(1 + Math.sqrt(5)) / 2', Blockly.Dart.ORDER_MULTIPLICATIVE],
|
||||
'SQRT2': ['Math.SQRT2', Blockly.Dart.ORDER_UNARY_POSTFIX],
|
||||
'SQRT1_2': ['Math.SQRT1_2', Blockly.Dart.ORDER_UNARY_POSTFIX],
|
||||
'INFINITY': ['double.INFINITY', Blockly.Dart.ORDER_ATOMIC]
|
||||
};
|
||||
var constant = block.getFieldValue('CONSTANT');
|
||||
if (constant != 'INFINITY') {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
}
|
||||
return CONSTANTS[constant];
|
||||
};
|
||||
|
||||
Blockly.Dart['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.Dart.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE);
|
||||
if (!number_to_check) {
|
||||
return ['false', Blockly.Python.ORDER_ATOMIC];
|
||||
}
|
||||
var dropdown_property = block.getFieldValue('PROPERTY');
|
||||
var code;
|
||||
if (dropdown_property == 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_isPrime',
|
||||
['bool ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '(n) {',
|
||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if (n == 2 || n == 3) {',
|
||||
' return true;',
|
||||
' }',
|
||||
' // False if n is null, negative, is 1, or not whole.',
|
||||
' // And false if n is divisible by 2 or 3.',
|
||||
' if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' +
|
||||
' n % 3 == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {',
|
||||
' if (n % (x - 1) == 0 || n % (x + 1) == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' }',
|
||||
' return true;',
|
||||
'}']);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
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.Dart.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE);
|
||||
if (!divisor) {
|
||||
return ['false', Blockly.Python.ORDER_ATOMIC];
|
||||
}
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_EQUALITY];
|
||||
};
|
||||
|
||||
Blockly.Dart['math_change'] = function(block) {
|
||||
// Add to a variable in place.
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'DELTA',
|
||||
Blockly.Dart.ORDER_ADDITIVE) || '0';
|
||||
var varName = Blockly.Dart.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = (' + varName + ' is num ? ' + varName + ' : 0) + ' +
|
||||
argument0 + ';\n';
|
||||
};
|
||||
|
||||
// Rounding functions have a single operand.
|
||||
Blockly.Dart['math_round'] = Blockly.Dart['math_single'];
|
||||
// Trigonometry functions have a single operand.
|
||||
Blockly.Dart['math_trig'] = Blockly.Dart['math_single'];
|
||||
|
||||
Blockly.Dart['math_on_list'] = function(block) {
|
||||
// Math functions for lists.
|
||||
var func = block.getFieldValue('OP');
|
||||
var list = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_NONE) || '[]';
|
||||
var code;
|
||||
switch (func) {
|
||||
case 'SUM':
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_sum',
|
||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList) {',
|
||||
' num sumVal = 0;',
|
||||
' myList.forEach((num entry) {sumVal += entry;});',
|
||||
' return sumVal;',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MIN':
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_min',
|
||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList) {',
|
||||
' if (myList.isEmpty) return null;',
|
||||
' num minVal = myList[0];',
|
||||
' myList.forEach((num entry) ' +
|
||||
'{minVal = Math.min(minVal, entry);});',
|
||||
' return minVal;',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MAX':
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_max',
|
||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList) {',
|
||||
' if (myList.isEmpty) return null;',
|
||||
' num maxVal = myList[0];',
|
||||
' myList.forEach((num entry) ' +
|
||||
'{maxVal = Math.max(maxVal, entry);});',
|
||||
' return maxVal;',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'AVERAGE':
|
||||
// This operation exclude null and values that are not int or float:
|
||||
// math_mean([null,null,"aString",1,9]) == 5.0.
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_mean',
|
||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList) {',
|
||||
' // First filter list for numbers only.',
|
||||
' List localList = new List.from(myList);',
|
||||
' localList.removeWhere((a) => a is! num);',
|
||||
' if (localList.isEmpty) return null;',
|
||||
' num sumVal = 0;',
|
||||
' localList.forEach((num entry) {sumVal += entry;});',
|
||||
' return sumVal / localList.length;',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MEDIAN':
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_median',
|
||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList) {',
|
||||
' // First filter list for numbers only, then sort, ' +
|
||||
'then return middle value',
|
||||
' // or the average of two middle values if list has an ' +
|
||||
'even number of elements.',
|
||||
' List localList = new List.from(myList);',
|
||||
' localList.removeWhere((a) => a is! num);',
|
||||
' if (localList.isEmpty) return null;',
|
||||
' localList.sort((a, b) => (a - b));',
|
||||
' int index = localList.length ~/ 2;',
|
||||
' if (localList.length % 2 == 1) {',
|
||||
' return localList[index];',
|
||||
' } else {',
|
||||
' return (localList[index - 1] + localList[index]) / 2;',
|
||||
' }',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MODE':
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
// 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].
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_modes',
|
||||
['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List values) {',
|
||||
' List modes = [];',
|
||||
' List counts = [];',
|
||||
' int maxCount = 0;',
|
||||
' for (int i = 0; i < values.length; i++) {',
|
||||
' var value = values[i];',
|
||||
' bool found = false;',
|
||||
' int thisCount;',
|
||||
' for (int j = 0; j < counts.length; j++) {',
|
||||
' if (counts[j][0] == value) {',
|
||||
' thisCount = ++counts[j][1];',
|
||||
' found = true;',
|
||||
' break;',
|
||||
' }',
|
||||
' }',
|
||||
' if (!found) {',
|
||||
' counts.add([value, 1]);',
|
||||
' thisCount = 1;',
|
||||
' }',
|
||||
' maxCount = Math.max(thisCount, maxCount);',
|
||||
' }',
|
||||
' for (int j = 0; j < counts.length; j++) {',
|
||||
' if (counts[j][1] == maxCount) {',
|
||||
' modes.add(counts[j][0]);',
|
||||
' }',
|
||||
' }',
|
||||
' return modes;',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'STD_DEV':
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_standard_deviation',
|
||||
['num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList) {',
|
||||
' // First filter list for numbers only.',
|
||||
' List numbers = new List.from(myList);',
|
||||
' numbers.removeWhere((a) => a is! num);',
|
||||
' if (numbers.isEmpty) return null;',
|
||||
' num n = numbers.length;',
|
||||
' num sum = 0;',
|
||||
' numbers.forEach((x) => sum += x);',
|
||||
' num mean = sum / n;',
|
||||
' num sumSquare = 0;',
|
||||
' numbers.forEach((x) => sumSquare += ' +
|
||||
'Math.pow(x - mean, 2));',
|
||||
' return Math.sqrt(sumSquare / n);',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'RANDOM':
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_random_item',
|
||||
['dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList) {',
|
||||
' int x = new Math.Random().nextInt(myList.length);',
|
||||
' return myList[x];',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
default:
|
||||
throw 'Unknown operator: ' + func;
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['math_modulo'] = function(block) {
|
||||
// Remainder computation.
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'DIVIDEND',
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE) || '0';
|
||||
var code = argument0 + ' % ' + argument1;
|
||||
return [code, Blockly.Dart.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
Blockly.Dart['math_constrain'] = function(block) {
|
||||
// Constrain a number between two limits.
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'LOW',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
var argument2 = Blockly.Dart.valueToCode(block, 'HIGH',
|
||||
Blockly.Dart.ORDER_NONE) || 'double.INFINITY';
|
||||
var code = 'Math.min(Math.max(' + argument0 + ', ' + argument1 + '), ' +
|
||||
argument2 + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['math_random_int'] = function(block) {
|
||||
// Random integer between [X] and [Y].
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'FROM',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'TO',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_random_int',
|
||||
['int ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ + '(num a, num b) {',
|
||||
' if (a > b) {',
|
||||
' // Swap a and b to ensure a is smaller.',
|
||||
' num c = a;',
|
||||
' a = b;',
|
||||
' b = c;',
|
||||
' }',
|
||||
' return new Math.Random().nextInt(b - a + 1) + a;',
|
||||
'}']);
|
||||
var code = functionName + '(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['math_random_float'] = function(block) {
|
||||
// Random fraction between 0 and 1.
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
return ['new Math.Random().nextDouble()', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
110
node_modules/node-blockly/blockly/generators/dart/procedures.js
generated
vendored
Normal file
110
node_modules/node-blockly/blockly/generators/dart/procedures.js
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2014 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 Dart for procedure blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.procedures');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart['procedures_defreturn'] = function(block) {
|
||||
// Define a procedure with a return value.
|
||||
var funcName = Blockly.Dart.variableDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE);
|
||||
var branch = Blockly.Dart.statementToCode(block, 'STACK');
|
||||
if (Blockly.Dart.STATEMENT_PREFIX) {
|
||||
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
|
||||
branch = Blockly.Dart.prefixLines(
|
||||
Blockly.Dart.STATEMENT_PREFIX.replace(/%1/g,
|
||||
'\'' + id + '\''), Blockly.Dart.INDENT) + branch;
|
||||
}
|
||||
if (Blockly.Dart.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.Dart.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
var returnValue = Blockly.Dart.valueToCode(block, 'RETURN',
|
||||
Blockly.Dart.ORDER_NONE) || '';
|
||||
if (returnValue) {
|
||||
returnValue = Blockly.Dart.INDENT + 'return ' + returnValue + ';\n';
|
||||
}
|
||||
var returnType = returnValue ? 'dynamic' : 'void';
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.Dart.variableDB_.getName(block.arguments_[i],
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
}
|
||||
var code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
|
||||
branch + returnValue + '}';
|
||||
code = Blockly.Dart.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
Blockly.Dart.definitions_['%' + funcName] = code;
|
||||
return null;
|
||||
};
|
||||
|
||||
// Defining a procedure without a return value uses the same generator as
|
||||
// a procedure with a return value.
|
||||
Blockly.Dart['procedures_defnoreturn'] = Blockly.Dart['procedures_defreturn'];
|
||||
|
||||
Blockly.Dart['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
var funcName = Blockly.Dart.variableDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.Dart.valueToCode(block, 'ARG' + i,
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['procedures_callnoreturn'] = function(block) {
|
||||
// Call a procedure with no return value.
|
||||
var funcName = Blockly.Dart.variableDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.Dart.valueToCode(block, 'ARG' + i,
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ');\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Dart['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
var condition = Blockly.Dart.valueToCode(block, 'CONDITION',
|
||||
Blockly.Dart.ORDER_NONE) || 'false';
|
||||
var code = 'if (' + condition + ') {\n';
|
||||
if (block.hasReturnValue_) {
|
||||
var value = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
code += Blockly.Dart.INDENT + 'return ' + value + ';\n';
|
||||
} else {
|
||||
code += Blockly.Dart.INDENT + 'return;\n';
|
||||
}
|
||||
code += '}\n';
|
||||
return code;
|
||||
};
|
||||
347
node_modules/node-blockly/blockly/generators/dart/text.js
generated
vendored
Normal file
347
node_modules/node-blockly/blockly/generators/dart/text.js
generated
vendored
Normal file
@@ -0,0 +1,347 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2014 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 Dart for text blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.texts');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart.addReservedWords('Html,Math');
|
||||
|
||||
Blockly.Dart['text'] = function(block) {
|
||||
// Text value.
|
||||
var code = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_join'] = function(block) {
|
||||
// Create a string made up of any number of elements of any type.
|
||||
switch (block.itemCount_) {
|
||||
case 0:
|
||||
return ['\'\'', Blockly.Dart.ORDER_ATOMIC];
|
||||
case 1:
|
||||
var element = Blockly.Dart.valueToCode(block, 'ADD0',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var code = element + '.toString()';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
default:
|
||||
var elements = new Array(block.itemCount_);
|
||||
for (var i = 0; i < block.itemCount_; i++) {
|
||||
elements[i] = Blockly.Dart.valueToCode(block, 'ADD' + i,
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var code = '[' + elements.join(',') + '].join()';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Dart['text_append'] = function(block) {
|
||||
// Append to a variable in place.
|
||||
var varName = Blockly.Dart.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
var value = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
return varName + ' = [' + varName + ', ' + value + '].join();\n';
|
||||
};
|
||||
|
||||
Blockly.Dart['text_length'] = function(block) {
|
||||
// String or array length.
|
||||
var text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
return [text + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
return [text + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_indexOf'] = function(block) {
|
||||
// Search the text for a substring.
|
||||
var operator = block.getFieldValue('END') == 'FIRST' ?
|
||||
'indexOf' : 'lastIndexOf';
|
||||
var substring = Blockly.Dart.valueToCode(block, 'FIND',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var code = text + '.' + operator + '(' + substring + ')';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.Dart.ORDER_ADDITIVE];
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['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.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
var code = text + '[0]';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
case 'FROM_START':
|
||||
var at = Blockly.Dart.getAdjusted(block, 'AT');
|
||||
var code = text + '[' + at + ']';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
case 'LAST':
|
||||
at = 1;
|
||||
// Fall through.
|
||||
case 'FROM_END':
|
||||
var at = Blockly.Dart.getAdjusted(block, 'AT', 1);
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'text_get_from_end',
|
||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(String text, num x) {',
|
||||
' return text[text.length - x];',
|
||||
'}']);
|
||||
code = functionName + '(' + text + ', ' + at + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
case 'RANDOM':
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'text_random_letter',
|
||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(String text) {',
|
||||
' int x = new Math.Random().nextInt(text.length);',
|
||||
' return text[x];',
|
||||
'}']);
|
||||
code = functionName + '(' + text + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
throw 'Unhandled option (text_charAt).';
|
||||
};
|
||||
|
||||
Blockly.Dart['text_getSubstring'] = function(block) {
|
||||
// Get substring.
|
||||
var text = Blockly.Dart.valueToCode(block, 'STRING',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
if (where1 == 'FIRST' && where2 == 'LAST') {
|
||||
var code = text;
|
||||
} else if (text.match(/^'?\w+'?$/) ||
|
||||
(where1 != 'FROM_END' && where2 == 'FROM_START')) {
|
||||
// If the text is a variable or literal or doesn't require a call for
|
||||
// length, don't generate a helper function.
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
var at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at1 = Blockly.Dart.getAdjusted(block, 'AT1', 1, false,
|
||||
Blockly.Dart.ORDER_ADDITIVE);
|
||||
at1 = text + '.length - ' + at1;
|
||||
break;
|
||||
case 'FIRST':
|
||||
var at1 = '0';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (text_getSubstring).';
|
||||
}
|
||||
switch (where2) {
|
||||
case 'FROM_START':
|
||||
var at2 = Blockly.Dart.getAdjusted(block, 'AT2', 1);
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at2 = Blockly.Dart.getAdjusted(block, 'AT2', 0, false,
|
||||
Blockly.Dart.ORDER_ADDITIVE);
|
||||
at2 = text + '.length - ' + at2;
|
||||
break;
|
||||
case 'LAST':
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (text_getSubstring).';
|
||||
}
|
||||
if (where2 == 'LAST') {
|
||||
var code = text + '.substring(' + at1 + ')';
|
||||
} else {
|
||||
var code = text + '.substring(' + at1 + ', ' + at2 + ')';
|
||||
}
|
||||
} else {
|
||||
var at1 = Blockly.Dart.getAdjusted(block, 'AT1');
|
||||
var at2 = Blockly.Dart.getAdjusted(block, 'AT2');
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'text_get_substring',
|
||||
['List ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(text, where1, at1, where2, at2) {',
|
||||
' int getAt(where, at) {',
|
||||
' if (where == \'FROM_END\') {',
|
||||
' at = text.length - 1 - at;',
|
||||
' } else if (where == \'FIRST\') {',
|
||||
' at = 0;',
|
||||
' } else if (where == \'LAST\') {',
|
||||
' at = text.length - 1;',
|
||||
' } else if (where != \'FROM_START\') {',
|
||||
' throw \'Unhandled option (text_getSubstring).\';',
|
||||
' }',
|
||||
' return at;',
|
||||
' }',
|
||||
' at1 = getAt(where1, at1);',
|
||||
' at2 = getAt(where2, at2) + 1;',
|
||||
' return text.substring(at1, at2);',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ', \'' +
|
||||
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_changeCase'] = function(block) {
|
||||
// Change capitalization.
|
||||
var OPERATORS = {
|
||||
'UPPERCASE': '.toUpperCase()',
|
||||
'LOWERCASE': '.toLowerCase()',
|
||||
'TITLECASE': null
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('CASE')];
|
||||
var textOrder = operator ? Blockly.Dart.ORDER_UNARY_POSTFIX :
|
||||
Blockly.Dart.ORDER_NONE;
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT', textOrder) || '\'\'';
|
||||
if (operator) {
|
||||
// Upper and lower case are functions built into Dart.
|
||||
var code = text + operator;
|
||||
} else {
|
||||
// Title case is not a native Dart function. Define one.
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'text_toTitleCase',
|
||||
['String ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(String str) {',
|
||||
' RegExp exp = new RegExp(r\'\\b\');',
|
||||
' List<String> list = str.split(exp);',
|
||||
' final title = new StringBuffer();',
|
||||
' for (String part in list) {',
|
||||
' if (part.length > 0) {',
|
||||
' title.write(part[0].toUpperCase());',
|
||||
' if (part.length > 0) {',
|
||||
' title.write(part.substring(1).toLowerCase());',
|
||||
' }',
|
||||
' }',
|
||||
' }',
|
||||
' return title.toString();',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ')';
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_trim'] = function(block) {
|
||||
// Trim spaces.
|
||||
var OPERATORS = {
|
||||
'LEFT': '.replaceFirst(new RegExp(r\'^\\s+\'), \'\')',
|
||||
'RIGHT': '.replaceFirst(new RegExp(r\'\\s+$\'), \'\')',
|
||||
'BOTH': '.trim()'
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
return [text + operator, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_print'] = function(block) {
|
||||
// Print statement.
|
||||
var msg = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
return 'print(' + msg + ');\n';
|
||||
};
|
||||
|
||||
Blockly.Dart['text_prompt_ext'] = function(block) {
|
||||
// Prompt function.
|
||||
Blockly.Dart.definitions_['import_dart_html'] =
|
||||
'import \'dart:html\' as Html;';
|
||||
if (block.getField('TEXT')) {
|
||||
// Internal message.
|
||||
var msg = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
||||
} else {
|
||||
// External message.
|
||||
var msg = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var code = 'Html.window.prompt(' + msg + ', \'\')';
|
||||
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
|
||||
if (toNumber) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
code = 'Math.parseDouble(' + code + ')';
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_prompt'] = Blockly.Dart['text_prompt_ext'];
|
||||
|
||||
Blockly.Dart['text_count'] = function(block) {
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var sub = Blockly.Dart.valueToCode(block, 'SUB',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
// Substring count is not a native Dart function. Define one.
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'text_count',
|
||||
['int ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(String haystack, String needle) {',
|
||||
' if (needle.length == 0) {',
|
||||
' return haystack.length + 1;',
|
||||
' }',
|
||||
' int index = 0;',
|
||||
' int count = 0;',
|
||||
' while (index != -1) {',
|
||||
' index = haystack.indexOf(needle, index);',
|
||||
' if (index != -1) {',
|
||||
' count++;',
|
||||
' index += needle.length;',
|
||||
' }',
|
||||
' }',
|
||||
' return count;',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ', ' + sub + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_replace'] = function(block) {
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var from = Blockly.Dart.valueToCode(block, 'FROM',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var to = Blockly.Dart.valueToCode(block, 'TO',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var code = text + '.replaceAll(' + from + ', ' + to + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart['text_reverse'] = function(block) {
|
||||
// There isn't a sensible way to do this in Dart. See:
|
||||
// http://stackoverflow.com/a/21613700/3529104
|
||||
// Implementing something is possibly better than not implementing anything?
|
||||
var text = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var code = 'new String.fromCharCodes(' + text + '.runes.toList().reversed)';
|
||||
// XXX What should the operator precedence be for a `new`?
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
46
node_modules/node-blockly/blockly/generators/dart/variables.js
generated
vendored
Normal file
46
node_modules/node-blockly/blockly/generators/dart/variables.js
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2014 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 Dart for variable blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.variables');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart['variables_get'] = function(block) {
|
||||
// Variable getter.
|
||||
var code = Blockly.Dart.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart['variables_set'] = function(block) {
|
||||
// Variable setter.
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
||||
var varName = Blockly.Dart.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = ' + argument0 + ';\n';
|
||||
};
|
||||
35
node_modules/node-blockly/blockly/generators/dart/variables_dynamic.js
generated
vendored
Normal file
35
node_modules/node-blockly/blockly/generators/dart/variables_dynamic.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2018 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 Dart for dynamic variable blocks.
|
||||
* @author fenichel@google.com (Rachel Fenichel)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.variablesDynamic');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
goog.require('Blockly.Dart.variables');
|
||||
|
||||
|
||||
// Dart is dynamically typed.
|
||||
Blockly.Dart['variables_get_dynamic'] = Blockly.Dart['variables_get'];
|
||||
Blockly.Dart['variables_set_dynamic'] = Blockly.Dart['variables_set'];
|
||||
332
node_modules/node-blockly/blockly/generators/javascript.js
generated
vendored
Normal file
332
node_modules/node-blockly/blockly/generators/javascript.js
generated
vendored
Normal file
@@ -0,0 +1,332 @@
|
||||
/**
|
||||
* @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_AWAIT = 4.8; // await
|
||||
Blockly.JavaScript.ORDER_EXPONENTIATION = 5.0; // **
|
||||
Blockly.JavaScript.ORDER_MULTIPLICATION = 5.1; // *
|
||||
Blockly.JavaScript.ORDER_DIVISION = 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_YIELD = 17; // yield
|
||||
Blockly.JavaScript.ORDER_COMMA = 18; // ,
|
||||
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();
|
||||
}
|
||||
|
||||
Blockly.JavaScript.variableDB_.setVariableMap(workspace.getVariableMap());
|
||||
|
||||
var defvars = [];
|
||||
// Add developer variables (not created or named by the user).
|
||||
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
for (var i = 0; i < devVarList.length; i++) {
|
||||
defvars.push(Blockly.JavaScript.variableDB_.getName(devVarList[i],
|
||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
|
||||
}
|
||||
|
||||
// Add user variables, but only ones that are being used.
|
||||
var variables = Blockly.Variables.allUsedVarModels(workspace);
|
||||
for (var i = 0; i < variables.length; i++) {
|
||||
defvars.push(Blockly.JavaScript.variableDB_.getName(variables[i].getId(),
|
||||
Blockly.Variables.NAME_TYPE));
|
||||
}
|
||||
|
||||
// Declare all of the variables.
|
||||
if (defvars.length) {
|
||||
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;
|
||||
};
|
||||
103
node_modules/node-blockly/blockly/generators/javascript/colour.js
generated
vendored
Normal file
103
node_modules/node-blockly/blockly/generators/javascript/colour.js
generated
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* @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.colour');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
|
||||
|
||||
Blockly.JavaScript['colour_picker'] = function(block) {
|
||||
// Colour picker.
|
||||
var code = '\'' + block.getFieldValue('COLOUR') + '\'';
|
||||
return [code, Blockly.JavaScript.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['colour_random'] = function(block) {
|
||||
// Generate a random colour.
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'colourRandom',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '() {',
|
||||
' var num = Math.floor(Math.random() * Math.pow(2, 24));',
|
||||
' return \'#\' + (\'00000\' + num.toString(16)).substr(-6);',
|
||||
'}']);
|
||||
var code = functionName + '()';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['colour_rgb'] = function(block) {
|
||||
// Compose a colour from RGB components expressed as percentages.
|
||||
var red = Blockly.JavaScript.valueToCode(block, 'RED',
|
||||
Blockly.JavaScript.ORDER_COMMA) || 0;
|
||||
var green = Blockly.JavaScript.valueToCode(block, 'GREEN',
|
||||
Blockly.JavaScript.ORDER_COMMA) || 0;
|
||||
var blue = Blockly.JavaScript.valueToCode(block, 'BLUE',
|
||||
Blockly.JavaScript.ORDER_COMMA) || 0;
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'colourRgb',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(r, g, b) {',
|
||||
' r = Math.max(Math.min(Number(r), 100), 0) * 2.55;',
|
||||
' g = Math.max(Math.min(Number(g), 100), 0) * 2.55;',
|
||||
' b = Math.max(Math.min(Number(b), 100), 0) * 2.55;',
|
||||
' r = (\'0\' + (Math.round(r) || 0).toString(16)).slice(-2);',
|
||||
' g = (\'0\' + (Math.round(g) || 0).toString(16)).slice(-2);',
|
||||
' b = (\'0\' + (Math.round(b) || 0).toString(16)).slice(-2);',
|
||||
' return \'#\' + r + g + b;',
|
||||
'}']);
|
||||
var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['colour_blend'] = function(block) {
|
||||
// Blend two colours together.
|
||||
var c1 = Blockly.JavaScript.valueToCode(block, 'COLOUR1',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '\'#000000\'';
|
||||
var c2 = Blockly.JavaScript.valueToCode(block, 'COLOUR2',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '\'#000000\'';
|
||||
var ratio = Blockly.JavaScript.valueToCode(block, 'RATIO',
|
||||
Blockly.JavaScript.ORDER_COMMA) || 0.5;
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'colourBlend',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(c1, c2, ratio) {',
|
||||
' ratio = Math.max(Math.min(Number(ratio), 1), 0);',
|
||||
' var r1 = parseInt(c1.substring(1, 3), 16);',
|
||||
' var g1 = parseInt(c1.substring(3, 5), 16);',
|
||||
' var b1 = parseInt(c1.substring(5, 7), 16);',
|
||||
' var r2 = parseInt(c2.substring(1, 3), 16);',
|
||||
' var g2 = parseInt(c2.substring(3, 5), 16);',
|
||||
' var b2 = parseInt(c2.substring(5, 7), 16);',
|
||||
' var r = Math.round(r1 * (1 - ratio) + r2 * ratio);',
|
||||
' var g = Math.round(g1 * (1 - ratio) + g2 * ratio);',
|
||||
' var b = Math.round(b1 * (1 - ratio) + b2 * ratio);',
|
||||
' r = (\'0\' + (r || 0).toString(16)).slice(-2);',
|
||||
' g = (\'0\' + (g || 0).toString(16)).slice(-2);',
|
||||
' b = (\'0\' + (b || 0).toString(16)).slice(-2);',
|
||||
' return \'#\' + r + g + b;',
|
||||
'}']);
|
||||
var code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
402
node_modules/node-blockly/blockly/generators/javascript/lists.js
generated
vendored
Normal file
402
node_modules/node-blockly/blockly/generators/javascript/lists.js
generated
vendored
Normal file
@@ -0,0 +1,402 @@
|
||||
/**
|
||||
* @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 list blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript.lists');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
|
||||
|
||||
Blockly.JavaScript['lists_create_empty'] = function(block) {
|
||||
// Create an empty list.
|
||||
return ['[]', Blockly.JavaScript.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['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.JavaScript.valueToCode(block, 'ADD' + i,
|
||||
Blockly.JavaScript.ORDER_COMMA) || 'null';
|
||||
}
|
||||
var code = '[' + elements.join(', ') + ']';
|
||||
return [code, Blockly.JavaScript.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_repeat'] = function(block) {
|
||||
// Create a list with one element repeated.
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'listsRepeat',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(value, n) {',
|
||||
' var array = [];',
|
||||
' for (var i = 0; i < n; i++) {',
|
||||
' array[i] = value;',
|
||||
' }',
|
||||
' return array;',
|
||||
'}']);
|
||||
var element = Blockly.JavaScript.valueToCode(block, 'ITEM',
|
||||
Blockly.JavaScript.ORDER_COMMA) || 'null';
|
||||
var repeatCount = Blockly.JavaScript.valueToCode(block, 'NUM',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '0';
|
||||
var code = functionName + '(' + element + ', ' + repeatCount + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_length'] = function(block) {
|
||||
// String or array length.
|
||||
var list = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '[]';
|
||||
return [list + '.length', Blockly.JavaScript.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var list = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '[]';
|
||||
return ['!' + list + '.length', Blockly.JavaScript.ORDER_LOGICAL_NOT];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_indexOf'] = function(block) {
|
||||
// Find an item in the list.
|
||||
var operator = block.getFieldValue('END') == 'FIRST' ?
|
||||
'indexOf' : 'lastIndexOf';
|
||||
var item = Blockly.JavaScript.valueToCode(block, 'FIND',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var list = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '[]';
|
||||
var code = list + '.' + operator + '(' + item + ')';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.JavaScript.ORDER_ADDITION];
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['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.JavaScript.ORDER_COMMA :
|
||||
Blockly.JavaScript.ORDER_MEMBER;
|
||||
var list = Blockly.JavaScript.valueToCode(block, 'VALUE', listOrder) || '[]';
|
||||
|
||||
switch (where) {
|
||||
case ('FIRST'):
|
||||
if (mode == 'GET') {
|
||||
var code = list + '[0]';
|
||||
return [code, Blockly.JavaScript.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.shift()';
|
||||
return [code, Blockly.JavaScript.ORDER_MEMBER];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.shift();\n';
|
||||
}
|
||||
break;
|
||||
case ('LAST'):
|
||||
if (mode == 'GET') {
|
||||
var code = list + '.slice(-1)[0]';
|
||||
return [code, Blockly.JavaScript.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.pop()';
|
||||
return [code, Blockly.JavaScript.ORDER_MEMBER];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.pop();\n';
|
||||
}
|
||||
break;
|
||||
case ('FROM_START'):
|
||||
var at = Blockly.JavaScript.getAdjusted(block, 'AT');
|
||||
if (mode == 'GET') {
|
||||
var code = list + '[' + at + ']';
|
||||
return [code, Blockly.JavaScript.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.splice(' + at + ', 1)[0]';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.splice(' + at + ', 1);\n';
|
||||
}
|
||||
break;
|
||||
case ('FROM_END'):
|
||||
var at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, true);
|
||||
if (mode == 'GET') {
|
||||
var code = list + '.slice(' + at + ')[0]';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var code = list + '.splice(' + at + ', 1)[0]';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return list + '.splice(' + at + ', 1);';
|
||||
}
|
||||
break;
|
||||
case ('RANDOM'):
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'listsGetRandomItem',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(list, remove) {',
|
||||
' var x = Math.floor(Math.random() * list.length);',
|
||||
' if (remove) {',
|
||||
' return list.splice(x, 1)[0];',
|
||||
' } else {',
|
||||
' return list[x];',
|
||||
' }',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ', ' + (mode != 'GET') + ')';
|
||||
if (mode == 'GET' || mode == 'GET_REMOVE') {
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return code + ';\n';
|
||||
}
|
||||
break;
|
||||
}
|
||||
throw 'Unhandled combination (lists_getIndex).';
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_setIndex'] = function(block) {
|
||||
// Set element at index.
|
||||
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
||||
var list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '[]';
|
||||
var mode = block.getFieldValue('MODE') || 'GET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'TO',
|
||||
Blockly.JavaScript.ORDER_ASSIGNMENT) || 'null';
|
||||
// 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.JavaScript.variableDB_.getDistinctName(
|
||||
'tmpList', Blockly.Variables.NAME_TYPE);
|
||||
var code = 'var ' + 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 + '.unshift(' + value + ');\n';
|
||||
}
|
||||
break;
|
||||
case ('LAST'):
|
||||
if (mode == 'SET') {
|
||||
var code = cacheList();
|
||||
code += list + '[' + list + '.length - 1] = ' + value + ';\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.push(' + value + ');\n';
|
||||
}
|
||||
break;
|
||||
case ('FROM_START'):
|
||||
var at = Blockly.JavaScript.getAdjusted(block, 'AT');
|
||||
if (mode == 'SET') {
|
||||
return list + '[' + at + '] = ' + value + ';\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.splice(' + at + ', 0, ' + value + ');\n';
|
||||
}
|
||||
break;
|
||||
case ('FROM_END'):
|
||||
var at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, false,
|
||||
Blockly.JavaScript.ORDER_SUBTRACTION);
|
||||
var code = cacheList();
|
||||
if (mode == 'SET') {
|
||||
code += list + '[' + list + '.length - ' + at + '] = ' + value + ';\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
code += list + '.splice(' + list + '.length - ' + at + ', 0, ' + value +
|
||||
');\n';
|
||||
return code;
|
||||
}
|
||||
break;
|
||||
case ('RANDOM'):
|
||||
var code = cacheList();
|
||||
var xVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
'tmpX', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + xVar + ' = Math.floor(Math.random() * ' + list +
|
||||
'.length);\n';
|
||||
if (mode == 'SET') {
|
||||
code += list + '[' + xVar + '] = ' + value + ';\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
code += list + '.splice(' + xVar + ', 0, ' + value + ');\n';
|
||||
return code;
|
||||
}
|
||||
break;
|
||||
}
|
||||
throw 'Unhandled combination (lists_setIndex).';
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an expression calculating the index into a list.
|
||||
* @private
|
||||
* @param {string} listName Name of the list, used to calculate length.
|
||||
* @param {string} where The method of indexing, selected by dropdown in Blockly
|
||||
* @param {string=} opt_at The optional offset when indexing from start/end.
|
||||
* @return {string} Index expression.
|
||||
*/
|
||||
Blockly.JavaScript.lists.getIndex_ = function(listName, where, opt_at) {
|
||||
if (where == 'FIRST') {
|
||||
return '0';
|
||||
} else if (where == 'FROM_END') {
|
||||
return listName + '.length - 1 - ' + opt_at;
|
||||
} else if (where == 'LAST') {
|
||||
return listName + '.length - 1';
|
||||
} else {
|
||||
return opt_at;
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_getSublist'] = function(block) {
|
||||
// Get sublist.
|
||||
var list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '[]';
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
if (where1 == 'FIRST' && where2 == 'LAST') {
|
||||
var code = list + '.slice(0)';
|
||||
} else if (list.match(/^\w+$/) ||
|
||||
(where1 != 'FROM_END' && where2 == 'FROM_START')) {
|
||||
// If the list is a variable or doesn't require a call for length, don't
|
||||
// generate a helper function.
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1');
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1', 1, false,
|
||||
Blockly.JavaScript.ORDER_SUBTRACTION);
|
||||
at1 = list + '.length - ' + at1;
|
||||
break;
|
||||
case 'FIRST':
|
||||
var at1 = '0';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (lists_getSublist).';
|
||||
}
|
||||
switch (where2) {
|
||||
case 'FROM_START':
|
||||
var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 1);
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 0, false,
|
||||
Blockly.JavaScript.ORDER_SUBTRACTION);
|
||||
at2 = list + '.length - ' + at2;
|
||||
break;
|
||||
case 'LAST':
|
||||
var at2 = list + '.length';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (lists_getSublist).';
|
||||
}
|
||||
code = list + '.slice(' + at1 + ', ' + at2 + ')';
|
||||
} else {
|
||||
var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1');
|
||||
var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2');
|
||||
var getIndex_ = Blockly.JavaScript.lists.getIndex_;
|
||||
var wherePascalCase = {'FIRST': 'First', 'LAST': 'Last',
|
||||
'FROM_START': 'FromStart', 'FROM_END': 'FromEnd'};
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'subsequence' + wherePascalCase[where1] + wherePascalCase[where2],
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(sequence' +
|
||||
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
||||
// we add it as a parameter.
|
||||
((where1 == 'FROM_END' || where1 == 'FROM_START') ? ', at1' : '') +
|
||||
((where2 == 'FROM_END' || where2 == 'FROM_START') ? ', at2' : '') +
|
||||
') {',
|
||||
' var start = ' + getIndex_('sequence', where1, 'at1') + ';',
|
||||
' var end = ' + getIndex_('sequence', where2, 'at2') + ' + 1;',
|
||||
' return sequence.slice(start, end);',
|
||||
'}']);
|
||||
var code = functionName + '(' + list +
|
||||
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
|
||||
// pass it.
|
||||
((where1 == 'FROM_END' || where1 == 'FROM_START') ? ', ' + at1 : '') +
|
||||
((where2 == 'FROM_END' || where2 == 'FROM_START') ? ', ' + at2 : '') +
|
||||
')';
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_sort'] = function(block) {
|
||||
// Block for sorting a list.
|
||||
var list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]';
|
||||
var direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
||||
var type = block.getFieldValue('TYPE');
|
||||
var getCompareFunctionName = Blockly.JavaScript.provideFunction_(
|
||||
'listsGetSortCompare',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(type, direction) {',
|
||||
' var compareFuncs = {',
|
||||
' "NUMERIC": function(a, b) {',
|
||||
' return parseFloat(a) - parseFloat(b); },',
|
||||
' "TEXT": function(a, b) {',
|
||||
' return a.toString() > b.toString() ? 1 : -1; },',
|
||||
' "IGNORE_CASE": function(a, b) {',
|
||||
' return a.toString().toLowerCase() > ' +
|
||||
'b.toString().toLowerCase() ? 1 : -1; },',
|
||||
' };',
|
||||
' var compare = compareFuncs[type];',
|
||||
' return function(a, b) { return compare(a, b) * direction; }',
|
||||
'}']);
|
||||
return [list + '.slice().sort(' +
|
||||
getCompareFunctionName + '("' + type + '", ' + direction + '))',
|
||||
Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_split'] = function(block) {
|
||||
// Block for splitting text into a list, or joining a list into text.
|
||||
var input = Blockly.JavaScript.valueToCode(block, 'INPUT',
|
||||
Blockly.JavaScript.ORDER_MEMBER);
|
||||
var delimiter = Blockly.JavaScript.valueToCode(block, 'DELIM',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var mode = block.getFieldValue('MODE');
|
||||
if (mode == 'SPLIT') {
|
||||
if (!input) {
|
||||
input = '\'\'';
|
||||
}
|
||||
var functionName = 'split';
|
||||
} else if (mode == 'JOIN') {
|
||||
if (!input) {
|
||||
input = '[]';
|
||||
}
|
||||
var functionName = 'join';
|
||||
} else {
|
||||
throw 'Unknown mode: ' + mode;
|
||||
}
|
||||
var code = input + '.' + functionName + '(' + delimiter + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['lists_reverse'] = function(block) {
|
||||
// Block for reversing a list.
|
||||
var list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]';
|
||||
var code = list + '.slice().reverse()';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
129
node_modules/node-blockly/blockly/generators/javascript/logic.js
generated
vendored
Normal file
129
node_modules/node-blockly/blockly/generators/javascript/logic.js
generated
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* @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 logic blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript.logic');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
|
||||
|
||||
Blockly.JavaScript['controls_if'] = function(block) {
|
||||
// If/elseif/else condition.
|
||||
var n = 0;
|
||||
var code = '', branchCode, conditionCode;
|
||||
do {
|
||||
conditionCode = Blockly.JavaScript.valueToCode(block, 'IF' + n,
|
||||
Blockly.JavaScript.ORDER_NONE) || 'false';
|
||||
branchCode = Blockly.JavaScript.statementToCode(block, 'DO' + n);
|
||||
code += (n > 0 ? ' else ' : '') +
|
||||
'if (' + conditionCode + ') {\n' + branchCode + '}';
|
||||
|
||||
++n;
|
||||
} while (block.getInput('IF' + n));
|
||||
|
||||
if (block.getInput('ELSE')) {
|
||||
branchCode = Blockly.JavaScript.statementToCode(block, 'ELSE');
|
||||
code += ' else {\n' + branchCode + '}';
|
||||
}
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
Blockly.JavaScript['controls_ifelse'] = Blockly.JavaScript['controls_if'];
|
||||
|
||||
Blockly.JavaScript['logic_compare'] = function(block) {
|
||||
// Comparison operator.
|
||||
var OPERATORS = {
|
||||
'EQ': '==',
|
||||
'NEQ': '!=',
|
||||
'LT': '<',
|
||||
'LTE': '<=',
|
||||
'GT': '>',
|
||||
'GTE': '>='
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('OP')];
|
||||
var order = (operator == '==' || operator == '!=') ?
|
||||
Blockly.JavaScript.ORDER_EQUALITY : Blockly.JavaScript.ORDER_RELATIONAL;
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.JavaScript.valueToCode(block, 'B', order) || '0';
|
||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
var operator = (block.getFieldValue('OP') == 'AND') ? '&&' : '||';
|
||||
var order = (operator == '&&') ? Blockly.JavaScript.ORDER_LOGICAL_AND :
|
||||
Blockly.JavaScript.ORDER_LOGICAL_OR;
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'A', order);
|
||||
var argument1 = Blockly.JavaScript.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 == '&&') ? 'true' : 'false';
|
||||
if (!argument0) {
|
||||
argument0 = defaultArgument;
|
||||
}
|
||||
if (!argument1) {
|
||||
argument1 = defaultArgument;
|
||||
}
|
||||
}
|
||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['logic_negate'] = function(block) {
|
||||
// Negation.
|
||||
var order = Blockly.JavaScript.ORDER_LOGICAL_NOT;
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'BOOL', order) ||
|
||||
'true';
|
||||
var code = '!' + argument0;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
|
||||
return [code, Blockly.JavaScript.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['logic_null'] = function(block) {
|
||||
// Null data type.
|
||||
return ['null', Blockly.JavaScript.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['logic_ternary'] = function(block) {
|
||||
// Ternary operator.
|
||||
var value_if = Blockly.JavaScript.valueToCode(block, 'IF',
|
||||
Blockly.JavaScript.ORDER_CONDITIONAL) || 'false';
|
||||
var value_then = Blockly.JavaScript.valueToCode(block, 'THEN',
|
||||
Blockly.JavaScript.ORDER_CONDITIONAL) || 'null';
|
||||
var value_else = Blockly.JavaScript.valueToCode(block, 'ELSE',
|
||||
Blockly.JavaScript.ORDER_CONDITIONAL) || 'null';
|
||||
var code = value_if + ' ? ' + value_then + ' : ' + value_else;
|
||||
return [code, Blockly.JavaScript.ORDER_CONDITIONAL];
|
||||
};
|
||||
175
node_modules/node-blockly/blockly/generators/javascript/loops.js
generated
vendored
Normal file
175
node_modules/node-blockly/blockly/generators/javascript/loops.js
generated
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
/**
|
||||
* @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 loop blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript.loops');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
|
||||
|
||||
Blockly.JavaScript['controls_repeat_ext'] = function(block) {
|
||||
// Repeat n times.
|
||||
if (block.getField('TIMES')) {
|
||||
// Internal number.
|
||||
var repeats = String(Number(block.getFieldValue('TIMES')));
|
||||
} else {
|
||||
// External number.
|
||||
var repeats = Blockly.JavaScript.valueToCode(block, 'TIMES',
|
||||
Blockly.JavaScript.ORDER_ASSIGNMENT) || '0';
|
||||
}
|
||||
var branch = Blockly.JavaScript.statementToCode(block, 'DO');
|
||||
branch = Blockly.JavaScript.addLoopTrap(branch, block.id);
|
||||
var code = '';
|
||||
var loopVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
'count', Blockly.Variables.NAME_TYPE);
|
||||
var endVar = repeats;
|
||||
if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) {
|
||||
var endVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
'repeat_end', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + endVar + ' = ' + repeats + ';\n';
|
||||
}
|
||||
code += 'for (var ' + loopVar + ' = 0; ' +
|
||||
loopVar + ' < ' + endVar + '; ' +
|
||||
loopVar + '++) {\n' +
|
||||
branch + '}\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['controls_repeat'] =
|
||||
Blockly.JavaScript['controls_repeat_ext'];
|
||||
|
||||
Blockly.JavaScript['controls_whileUntil'] = function(block) {
|
||||
// Do while/until loop.
|
||||
var until = block.getFieldValue('MODE') == 'UNTIL';
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'BOOL',
|
||||
until ? Blockly.JavaScript.ORDER_LOGICAL_NOT :
|
||||
Blockly.JavaScript.ORDER_NONE) || 'false';
|
||||
var branch = Blockly.JavaScript.statementToCode(block, 'DO');
|
||||
branch = Blockly.JavaScript.addLoopTrap(branch, block.id);
|
||||
if (until) {
|
||||
argument0 = '!' + argument0;
|
||||
}
|
||||
return 'while (' + argument0 + ') {\n' + branch + '}\n';
|
||||
};
|
||||
|
||||
Blockly.JavaScript['controls_for'] = function(block) {
|
||||
// For loop.
|
||||
var variable0 = Blockly.JavaScript.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'FROM',
|
||||
Blockly.JavaScript.ORDER_ASSIGNMENT) || '0';
|
||||
var argument1 = Blockly.JavaScript.valueToCode(block, 'TO',
|
||||
Blockly.JavaScript.ORDER_ASSIGNMENT) || '0';
|
||||
var increment = Blockly.JavaScript.valueToCode(block, 'BY',
|
||||
Blockly.JavaScript.ORDER_ASSIGNMENT) || '1';
|
||||
var branch = Blockly.JavaScript.statementToCode(block, 'DO');
|
||||
branch = Blockly.JavaScript.addLoopTrap(branch, block.id);
|
||||
var code;
|
||||
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
|
||||
Blockly.isNumber(increment)) {
|
||||
// All arguments are simple numbers.
|
||||
var up = parseFloat(argument0) <= parseFloat(argument1);
|
||||
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
|
||||
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
|
||||
variable0;
|
||||
var step = Math.abs(parseFloat(increment));
|
||||
if (step == 1) {
|
||||
code += up ? '++' : '--';
|
||||
} else {
|
||||
code += (up ? ' += ' : ' -= ') + step;
|
||||
}
|
||||
code += ') {\n' + branch + '}\n';
|
||||
} else {
|
||||
code = '';
|
||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||
var startVar = argument0;
|
||||
if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) {
|
||||
startVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
variable0 + '_start', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + startVar + ' = ' + argument0 + ';\n';
|
||||
}
|
||||
var endVar = argument1;
|
||||
if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) {
|
||||
var endVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
variable0 + '_end', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + endVar + ' = ' + argument1 + ';\n';
|
||||
}
|
||||
// Determine loop direction at start, in case one of the bounds
|
||||
// changes during loop execution.
|
||||
var incVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + incVar + ' = ';
|
||||
if (Blockly.isNumber(increment)) {
|
||||
code += Math.abs(increment) + ';\n';
|
||||
} else {
|
||||
code += 'Math.abs(' + increment + ');\n';
|
||||
}
|
||||
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
|
||||
code += Blockly.JavaScript.INDENT + incVar + ' = -' + incVar + ';\n';
|
||||
code += '}\n';
|
||||
code += 'for (' + variable0 + ' = ' + startVar + '; ' +
|
||||
incVar + ' >= 0 ? ' +
|
||||
variable0 + ' <= ' + endVar + ' : ' +
|
||||
variable0 + ' >= ' + endVar + '; ' +
|
||||
variable0 + ' += ' + incVar + ') {\n' +
|
||||
branch + '}\n';
|
||||
}
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['controls_forEach'] = function(block) {
|
||||
// For each loop.
|
||||
var variable0 = Blockly.JavaScript.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_ASSIGNMENT) || '[]';
|
||||
var branch = Blockly.JavaScript.statementToCode(block, 'DO');
|
||||
branch = Blockly.JavaScript.addLoopTrap(branch, block.id);
|
||||
var code = '';
|
||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||
var listVar = argument0;
|
||||
if (!argument0.match(/^\w+$/)) {
|
||||
listVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
variable0 + '_list', Blockly.Variables.NAME_TYPE);
|
||||
code += 'var ' + listVar + ' = ' + argument0 + ';\n';
|
||||
}
|
||||
var indexVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
variable0 + '_index', Blockly.Variables.NAME_TYPE);
|
||||
branch = Blockly.JavaScript.INDENT + variable0 + ' = ' +
|
||||
listVar + '[' + indexVar + '];\n' + branch;
|
||||
code += 'for (var ' + indexVar + ' in ' + listVar + ') {\n' + branch + '}\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['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.';
|
||||
};
|
||||
413
node_modules/node-blockly/blockly/generators/javascript/math.js
generated
vendored
Normal file
413
node_modules/node-blockly/blockly/generators/javascript/math.js
generated
vendored
Normal file
@@ -0,0 +1,413 @@
|
||||
/**
|
||||
* @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 math blocks.
|
||||
* @author q.neutron@gmail.com (Quynh Neutron)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript.math');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
|
||||
|
||||
Blockly.JavaScript['math_number'] = function(block) {
|
||||
// Numeric value.
|
||||
var code = parseFloat(block.getFieldValue('NUM'));
|
||||
var order = code >= 0 ? Blockly.JavaScript.ORDER_ATOMIC :
|
||||
Blockly.JavaScript.ORDER_UNARY_NEGATION;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['math_arithmetic'] = function(block) {
|
||||
// Basic arithmetic operators, and power.
|
||||
var OPERATORS = {
|
||||
'ADD': [' + ', Blockly.JavaScript.ORDER_ADDITION],
|
||||
'MINUS': [' - ', Blockly.JavaScript.ORDER_SUBTRACTION],
|
||||
'MULTIPLY': [' * ', Blockly.JavaScript.ORDER_MULTIPLICATION],
|
||||
'DIVIDE': [' / ', Blockly.JavaScript.ORDER_DIVISION],
|
||||
'POWER': [null, Blockly.JavaScript.ORDER_COMMA] // Handle power separately.
|
||||
};
|
||||
var tuple = OPERATORS[block.getFieldValue('OP')];
|
||||
var operator = tuple[0];
|
||||
var order = tuple[1];
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.JavaScript.valueToCode(block, 'B', order) || '0';
|
||||
var code;
|
||||
// Power in JavaScript requires a special case since it has no operator.
|
||||
if (!operator) {
|
||||
code = 'Math.pow(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
code = argument0 + operator + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['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.
|
||||
arg = Blockly.JavaScript.valueToCode(block, 'NUM',
|
||||
Blockly.JavaScript.ORDER_UNARY_NEGATION) || '0';
|
||||
if (arg[0] == '-') {
|
||||
// --3 is not legal in JS.
|
||||
arg = ' ' + arg;
|
||||
}
|
||||
code = '-' + arg;
|
||||
return [code, Blockly.JavaScript.ORDER_UNARY_NEGATION];
|
||||
}
|
||||
if (operator == 'SIN' || operator == 'COS' || operator == 'TAN') {
|
||||
arg = Blockly.JavaScript.valueToCode(block, 'NUM',
|
||||
Blockly.JavaScript.ORDER_DIVISION) || '0';
|
||||
} else {
|
||||
arg = Blockly.JavaScript.valueToCode(block, 'NUM',
|
||||
Blockly.JavaScript.ORDER_NONE) || '0';
|
||||
}
|
||||
// First, handle cases which generate values that don't need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'ABS':
|
||||
code = 'Math.abs(' + arg + ')';
|
||||
break;
|
||||
case 'ROOT':
|
||||
code = 'Math.sqrt(' + arg + ')';
|
||||
break;
|
||||
case 'LN':
|
||||
code = 'Math.log(' + arg + ')';
|
||||
break;
|
||||
case 'EXP':
|
||||
code = 'Math.exp(' + arg + ')';
|
||||
break;
|
||||
case 'POW10':
|
||||
code = 'Math.pow(10,' + arg + ')';
|
||||
break;
|
||||
case 'ROUND':
|
||||
code = 'Math.round(' + arg + ')';
|
||||
break;
|
||||
case 'ROUNDUP':
|
||||
code = 'Math.ceil(' + arg + ')';
|
||||
break;
|
||||
case 'ROUNDDOWN':
|
||||
code = 'Math.floor(' + arg + ')';
|
||||
break;
|
||||
case 'SIN':
|
||||
code = 'Math.sin(' + arg + ' / 180 * Math.PI)';
|
||||
break;
|
||||
case 'COS':
|
||||
code = 'Math.cos(' + arg + ' / 180 * Math.PI)';
|
||||
break;
|
||||
case 'TAN':
|
||||
code = 'Math.tan(' + arg + ' / 180 * Math.PI)';
|
||||
break;
|
||||
}
|
||||
if (code) {
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
// Second, handle cases which generate values that may need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'LOG10':
|
||||
code = 'Math.log(' + arg + ') / Math.log(10)';
|
||||
break;
|
||||
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.JavaScript.ORDER_DIVISION];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['math_constant'] = function(block) {
|
||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||
var CONSTANTS = {
|
||||
'PI': ['Math.PI', Blockly.JavaScript.ORDER_MEMBER],
|
||||
'E': ['Math.E', Blockly.JavaScript.ORDER_MEMBER],
|
||||
'GOLDEN_RATIO':
|
||||
['(1 + Math.sqrt(5)) / 2', Blockly.JavaScript.ORDER_DIVISION],
|
||||
'SQRT2': ['Math.SQRT2', Blockly.JavaScript.ORDER_MEMBER],
|
||||
'SQRT1_2': ['Math.SQRT1_2', Blockly.JavaScript.ORDER_MEMBER],
|
||||
'INFINITY': ['Infinity', Blockly.JavaScript.ORDER_ATOMIC]
|
||||
};
|
||||
return CONSTANTS[block.getFieldValue('CONSTANT')];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['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.JavaScript.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
Blockly.JavaScript.ORDER_MODULUS) || '0';
|
||||
var dropdown_property = block.getFieldValue('PROPERTY');
|
||||
var code;
|
||||
if (dropdown_property == 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'mathIsPrime',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ + '(n) {',
|
||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if (n == 2 || n == 3) {',
|
||||
' return true;',
|
||||
' }',
|
||||
' // False if n is NaN, negative, is 1, or not whole.',
|
||||
' // And false if n is divisible by 2 or 3.',
|
||||
' if (isNaN(n) || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' +
|
||||
' n % 3 == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {',
|
||||
' if (n % (x - 1) == 0 || n % (x + 1) == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' }',
|
||||
' return true;',
|
||||
'}']);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, Blockly.JavaScript.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.JavaScript.valueToCode(block, 'DIVISOR',
|
||||
Blockly.JavaScript.ORDER_MODULUS) || '0';
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_EQUALITY];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['math_change'] = function(block) {
|
||||
// Add to a variable in place.
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'DELTA',
|
||||
Blockly.JavaScript.ORDER_ADDITION) || '0';
|
||||
var varName = Blockly.JavaScript.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = (typeof ' + varName + ' == \'number\' ? ' + varName +
|
||||
' : 0) + ' + argument0 + ';\n';
|
||||
};
|
||||
|
||||
// Rounding functions have a single operand.
|
||||
Blockly.JavaScript['math_round'] = Blockly.JavaScript['math_single'];
|
||||
// Trigonometry functions have a single operand.
|
||||
Blockly.JavaScript['math_trig'] = Blockly.JavaScript['math_single'];
|
||||
|
||||
Blockly.JavaScript['math_on_list'] = function(block) {
|
||||
// Math functions for lists.
|
||||
var func = block.getFieldValue('OP');
|
||||
var list, code;
|
||||
switch (func) {
|
||||
case 'SUM':
|
||||
list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '[]';
|
||||
code = list + '.reduce(function(x, y) {return x + y;})';
|
||||
break;
|
||||
case 'MIN':
|
||||
list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '[]';
|
||||
code = 'Math.min.apply(null, ' + list + ')';
|
||||
break;
|
||||
case 'MAX':
|
||||
list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '[]';
|
||||
code = 'Math.max.apply(null, ' + list + ')';
|
||||
break;
|
||||
case 'AVERAGE':
|
||||
// mathMean([null,null,1,3]) == 2.0.
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'mathMean',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(myList) {',
|
||||
' return myList.reduce(function(x, y) {return x + y;}) / ' +
|
||||
'myList.length;',
|
||||
'}']);
|
||||
list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MEDIAN':
|
||||
// mathMedian([null,null,1,3]) == 2.0.
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'mathMedian',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(myList) {',
|
||||
' var localList = myList.filter(function (x) ' +
|
||||
'{return typeof x == \'number\';});',
|
||||
' if (!localList.length) return null;',
|
||||
' localList.sort(function(a, b) {return b - a;});',
|
||||
' if (localList.length % 2 == 0) {',
|
||||
' return (localList[localList.length / 2 - 1] + ' +
|
||||
'localList[localList.length / 2]) / 2;',
|
||||
' } else {',
|
||||
' return localList[(localList.length - 1) / 2];',
|
||||
' }',
|
||||
'}']);
|
||||
list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MODE':
|
||||
// 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].
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'mathModes',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(values) {',
|
||||
' var modes = [];',
|
||||
' var counts = [];',
|
||||
' var maxCount = 0;',
|
||||
' for (var i = 0; i < values.length; i++) {',
|
||||
' var value = values[i];',
|
||||
' var found = false;',
|
||||
' var thisCount;',
|
||||
' for (var j = 0; j < counts.length; j++) {',
|
||||
' if (counts[j][0] === value) {',
|
||||
' thisCount = ++counts[j][1];',
|
||||
' found = true;',
|
||||
' break;',
|
||||
' }',
|
||||
' }',
|
||||
' if (!found) {',
|
||||
' counts.push([value, 1]);',
|
||||
' thisCount = 1;',
|
||||
' }',
|
||||
' maxCount = Math.max(thisCount, maxCount);',
|
||||
' }',
|
||||
' for (var j = 0; j < counts.length; j++) {',
|
||||
' if (counts[j][1] == maxCount) {',
|
||||
' modes.push(counts[j][0]);',
|
||||
' }',
|
||||
' }',
|
||||
' return modes;',
|
||||
'}']);
|
||||
list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'STD_DEV':
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'mathStandardDeviation',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(numbers) {',
|
||||
' var n = numbers.length;',
|
||||
' if (!n) return null;',
|
||||
' var mean = numbers.reduce(function(x, y) {return x + y;}) / n;',
|
||||
' var variance = 0;',
|
||||
' for (var j = 0; j < n; j++) {',
|
||||
' variance += Math.pow(numbers[j] - mean, 2);',
|
||||
' }',
|
||||
' variance = variance / n;',
|
||||
' return Math.sqrt(variance);',
|
||||
'}']);
|
||||
list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'RANDOM':
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'mathRandomList',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(list) {',
|
||||
' var x = Math.floor(Math.random() * list.length);',
|
||||
' return list[x];',
|
||||
'}']);
|
||||
list = Blockly.JavaScript.valueToCode(block, 'LIST',
|
||||
Blockly.JavaScript.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
default:
|
||||
throw 'Unknown operator: ' + func;
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['math_modulo'] = function(block) {
|
||||
// Remainder computation.
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'DIVIDEND',
|
||||
Blockly.JavaScript.ORDER_MODULUS) || '0';
|
||||
var argument1 = Blockly.JavaScript.valueToCode(block, 'DIVISOR',
|
||||
Blockly.JavaScript.ORDER_MODULUS) || '0';
|
||||
var code = argument0 + ' % ' + argument1;
|
||||
return [code, Blockly.JavaScript.ORDER_MODULUS];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['math_constrain'] = function(block) {
|
||||
// Constrain a number between two limits.
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '0';
|
||||
var argument1 = Blockly.JavaScript.valueToCode(block, 'LOW',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '0';
|
||||
var argument2 = Blockly.JavaScript.valueToCode(block, 'HIGH',
|
||||
Blockly.JavaScript.ORDER_COMMA) || 'Infinity';
|
||||
var code = 'Math.min(Math.max(' + argument0 + ', ' + argument1 + '), ' +
|
||||
argument2 + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['math_random_int'] = function(block) {
|
||||
// Random integer between [X] and [Y].
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'FROM',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '0';
|
||||
var argument1 = Blockly.JavaScript.valueToCode(block, 'TO',
|
||||
Blockly.JavaScript.ORDER_COMMA) || '0';
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'mathRandomInt',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(a, b) {',
|
||||
' if (a > b) {',
|
||||
' // Swap a and b to ensure a is smaller.',
|
||||
' var c = a;',
|
||||
' a = b;',
|
||||
' b = c;',
|
||||
' }',
|
||||
' return Math.floor(Math.random() * (b - a + 1) + a);',
|
||||
'}']);
|
||||
var code = functionName + '(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['math_random_float'] = function(block) {
|
||||
// Random fraction between 0 and 1.
|
||||
return ['Math.random()', Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
110
node_modules/node-blockly/blockly/generators/javascript/procedures.js
generated
vendored
Normal file
110
node_modules/node-blockly/blockly/generators/javascript/procedures.js
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* @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 procedure blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript.procedures');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
|
||||
|
||||
Blockly.JavaScript['procedures_defreturn'] = function(block) {
|
||||
// Define a procedure with a return value.
|
||||
var funcName = Blockly.JavaScript.variableDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
|
||||
var branch = Blockly.JavaScript.statementToCode(block, 'STACK');
|
||||
if (Blockly.JavaScript.STATEMENT_PREFIX) {
|
||||
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
|
||||
branch = Blockly.JavaScript.prefixLines(
|
||||
Blockly.JavaScript.STATEMENT_PREFIX.replace(/%1/g,
|
||||
'\'' + id + '\''), Blockly.JavaScript.INDENT) + branch;
|
||||
}
|
||||
if (Blockly.JavaScript.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.JavaScript.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
var returnValue = Blockly.JavaScript.valueToCode(block, 'RETURN',
|
||||
Blockly.JavaScript.ORDER_NONE) || '';
|
||||
if (returnValue) {
|
||||
returnValue = Blockly.JavaScript.INDENT + 'return ' + returnValue + ';\n';
|
||||
}
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.JavaScript.variableDB_.getName(block.arguments_[i],
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
}
|
||||
var code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
|
||||
branch + returnValue + '}';
|
||||
code = Blockly.JavaScript.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
Blockly.JavaScript.definitions_['%' + funcName] = code;
|
||||
return null;
|
||||
};
|
||||
|
||||
// Defining a procedure without a return value uses the same generator as
|
||||
// a procedure with a return value.
|
||||
Blockly.JavaScript['procedures_defnoreturn'] =
|
||||
Blockly.JavaScript['procedures_defreturn'];
|
||||
|
||||
Blockly.JavaScript['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
var funcName = Blockly.JavaScript.variableDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.JavaScript.valueToCode(block, 'ARG' + i,
|
||||
Blockly.JavaScript.ORDER_COMMA) || 'null';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['procedures_callnoreturn'] = function(block) {
|
||||
// Call a procedure with no return value.
|
||||
var funcName = Blockly.JavaScript.variableDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.JavaScript.valueToCode(block, 'ARG' + i,
|
||||
Blockly.JavaScript.ORDER_COMMA) || 'null';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ');\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.JavaScript['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
var condition = Blockly.JavaScript.valueToCode(block, 'CONDITION',
|
||||
Blockly.JavaScript.ORDER_NONE) || 'false';
|
||||
var code = 'if (' + condition + ') {\n';
|
||||
if (block.hasReturnValue_) {
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_NONE) || 'null';
|
||||
code += Blockly.JavaScript.INDENT + 'return ' + value + ';\n';
|
||||
} else {
|
||||
code += Blockly.JavaScript.INDENT + 'return;\n';
|
||||
}
|
||||
code += '}\n';
|
||||
return code;
|
||||
};
|
||||
352
node_modules/node-blockly/blockly/generators/javascript/text.js
generated
vendored
Normal file
352
node_modules/node-blockly/blockly/generators/javascript/text.js
generated
vendored
Normal file
@@ -0,0 +1,352 @@
|
||||
/**
|
||||
* @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 text blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript.texts');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
|
||||
|
||||
Blockly.JavaScript['text'] = function(block) {
|
||||
// Text value.
|
||||
var code = Blockly.JavaScript.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Blockly.JavaScript.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_join'] = function(block) {
|
||||
// Create a string made up of any number of elements of any type.
|
||||
switch (block.itemCount_) {
|
||||
case 0:
|
||||
return ['\'\'', Blockly.JavaScript.ORDER_ATOMIC];
|
||||
case 1:
|
||||
var element = Blockly.JavaScript.valueToCode(block, 'ADD0',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var code = 'String(' + element + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
case 2:
|
||||
var element0 = Blockly.JavaScript.valueToCode(block, 'ADD0',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var element1 = Blockly.JavaScript.valueToCode(block, 'ADD1',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var code = 'String(' + element0 + ') + String(' + element1 + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_ADDITION];
|
||||
default:
|
||||
var elements = new Array(block.itemCount_);
|
||||
for (var i = 0; i < block.itemCount_; i++) {
|
||||
elements[i] = Blockly.JavaScript.valueToCode(block, 'ADD' + i,
|
||||
Blockly.JavaScript.ORDER_COMMA) || '\'\'';
|
||||
}
|
||||
var code = '[' + elements.join(',') + '].join(\'\')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_append'] = function(block) {
|
||||
// Append to a variable in place.
|
||||
var varName = Blockly.JavaScript.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var value = Blockly.JavaScript.valueToCode(block, 'TEXT',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
return varName + ' = String(' + varName + ') + String(' + value + ');\n';
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_length'] = function(block) {
|
||||
// String or array length.
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '\'\'';
|
||||
return [text + '.length', Blockly.JavaScript.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
|
||||
return ['!' + text + '.length', Blockly.JavaScript.ORDER_LOGICAL_NOT];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_indexOf'] = function(block) {
|
||||
// Search the text for a substring.
|
||||
var operator = block.getFieldValue('END') == 'FIRST' ?
|
||||
'indexOf' : 'lastIndexOf';
|
||||
var substring = Blockly.JavaScript.valueToCode(block, 'FIND',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
|
||||
var code = text + '.' + operator + '(' + substring + ')';
|
||||
// Adjust index if using one-based indices.
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
return [code + ' + 1', Blockly.JavaScript.ORDER_ADDITION];
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['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 textOrder = (where == 'RANDOM') ? Blockly.JavaScript.ORDER_NONE :
|
||||
Blockly.JavaScript.ORDER_MEMBER;
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
textOrder) || '\'\'';
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
var code = text + '.charAt(0)';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
case 'LAST':
|
||||
var code = text + '.slice(-1)';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
case 'FROM_START':
|
||||
var at = Blockly.JavaScript.getAdjusted(block, 'AT');
|
||||
// Adjust index if using one-based indices.
|
||||
var code = text + '.charAt(' + at + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
case 'FROM_END':
|
||||
var at = Blockly.JavaScript.getAdjusted(block, 'AT', 1, true);
|
||||
var code = text + '.slice(' + at + ').charAt(0)';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
case 'RANDOM':
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'textRandomLetter',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(text) {',
|
||||
' var x = Math.floor(Math.random() * text.length);',
|
||||
' return text[x];',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
throw 'Unhandled option (text_charAt).';
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an expression calculating the index into a string.
|
||||
* @private
|
||||
* @param {string} stringName Name of the string, used to calculate length.
|
||||
* @param {string} where The method of indexing, selected by dropdown in Blockly
|
||||
* @param {string=} opt_at The optional offset when indexing from start/end.
|
||||
* @return {string} Index expression.
|
||||
*/
|
||||
Blockly.JavaScript.text.getIndex_ = function(stringName, where, opt_at) {
|
||||
if (where == 'FIRST') {
|
||||
return '0';
|
||||
} else if (where == 'FROM_END') {
|
||||
return stringName + '.length - 1 - ' + opt_at;
|
||||
} else if (where == 'LAST') {
|
||||
return stringName + '.length - 1';
|
||||
} else {
|
||||
return opt_at;
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_getSubstring'] = function(block) {
|
||||
// Get substring.
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'STRING',
|
||||
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '\'\'';
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
if (where1 == 'FIRST' && where2 == 'LAST') {
|
||||
var code = text;
|
||||
} else if (text.match(/^'?\w+'?$/) ||
|
||||
(where1 != 'FROM_END' && where1 != 'LAST' &&
|
||||
where2 != 'FROM_END' && where2 != 'LAST')) {
|
||||
// If the text is a variable or literal or doesn't require a call for
|
||||
// length, don't generate a helper function.
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1');
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1', 1, false,
|
||||
Blockly.JavaScript.ORDER_SUBTRACTION);
|
||||
at1 = text + '.length - ' + at1;
|
||||
break;
|
||||
case 'FIRST':
|
||||
var at1 = '0';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (text_getSubstring).';
|
||||
}
|
||||
switch (where2) {
|
||||
case 'FROM_START':
|
||||
var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 1);
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2', 0, false,
|
||||
Blockly.JavaScript.ORDER_SUBTRACTION);
|
||||
at2 = text + '.length - ' + at2;
|
||||
break;
|
||||
case 'LAST':
|
||||
var at2 = text + '.length';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (text_getSubstring).';
|
||||
}
|
||||
code = text + '.slice(' + at1 + ', ' + at2 + ')';
|
||||
} else {
|
||||
var at1 = Blockly.JavaScript.getAdjusted(block, 'AT1');
|
||||
var at2 = Blockly.JavaScript.getAdjusted(block, 'AT2');
|
||||
var getIndex_ = Blockly.JavaScript.text.getIndex_;
|
||||
var wherePascalCase = {'FIRST': 'First', 'LAST': 'Last',
|
||||
'FROM_START': 'FromStart', 'FROM_END': 'FromEnd'};
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'subsequence' + wherePascalCase[where1] + wherePascalCase[where2],
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(sequence' +
|
||||
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
||||
// we add it as a parameter.
|
||||
((where1 == 'FROM_END' || where1 == 'FROM_START') ? ', at1' : '') +
|
||||
((where2 == 'FROM_END' || where2 == 'FROM_START') ? ', at2' : '') +
|
||||
') {',
|
||||
' var start = ' + getIndex_('sequence', where1, 'at1') + ';',
|
||||
' var end = ' + getIndex_('sequence', where2, 'at2') + ' + 1;',
|
||||
' return sequence.slice(start, end);',
|
||||
'}']);
|
||||
var code = functionName + '(' + text +
|
||||
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
|
||||
// pass it.
|
||||
((where1 == 'FROM_END' || where1 == 'FROM_START') ? ', ' + at1 : '') +
|
||||
((where2 == 'FROM_END' || where2 == 'FROM_START') ? ', ' + at2 : '') +
|
||||
')';
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_changeCase'] = function(block) {
|
||||
// Change capitalization.
|
||||
var OPERATORS = {
|
||||
'UPPERCASE': '.toUpperCase()',
|
||||
'LOWERCASE': '.toLowerCase()',
|
||||
'TITLECASE': null
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('CASE')];
|
||||
var textOrder = operator ? Blockly.JavaScript.ORDER_MEMBER :
|
||||
Blockly.JavaScript.ORDER_NONE;
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'TEXT',
|
||||
textOrder) || '\'\'';
|
||||
if (operator) {
|
||||
// Upper and lower case are functions built into JavaScript.
|
||||
var code = text + operator;
|
||||
} else {
|
||||
// Title case is not a native JavaScript function. Define one.
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'textToTitleCase',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(str) {',
|
||||
' return str.replace(/\\S+/g,',
|
||||
' function(txt) {return txt[0].toUpperCase() + ' +
|
||||
'txt.substring(1).toLowerCase();});',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ')';
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_trim'] = function(block) {
|
||||
// Trim spaces.
|
||||
var OPERATORS = {
|
||||
'LEFT': ".replace(/^[\\s\\xa0]+/, '')",
|
||||
'RIGHT': ".replace(/[\\s\\xa0]+$/, '')",
|
||||
'BOTH': '.trim()'
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'TEXT',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
|
||||
return [text + operator, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_print'] = function(block) {
|
||||
// Print statement.
|
||||
var msg = Blockly.JavaScript.valueToCode(block, 'TEXT',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
return 'window.alert(' + msg + ');\n';
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_prompt_ext'] = function(block) {
|
||||
// Prompt function.
|
||||
if (block.getField('TEXT')) {
|
||||
// Internal message.
|
||||
var msg = Blockly.JavaScript.quote_(block.getFieldValue('TEXT'));
|
||||
} else {
|
||||
// External message.
|
||||
var msg = Blockly.JavaScript.valueToCode(block, 'TEXT',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var code = 'window.prompt(' + msg + ')';
|
||||
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
|
||||
if (toNumber) {
|
||||
code = 'parseFloat(' + code + ')';
|
||||
}
|
||||
return [code, Blockly.JavaScript.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_prompt'] = Blockly.JavaScript['text_prompt_ext'];
|
||||
|
||||
Blockly.JavaScript['text_count'] = function(block) {
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'TEXT',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
|
||||
var sub = Blockly.JavaScript.valueToCode(block, 'SUB',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'textCount',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(haystack, needle) {',
|
||||
' if (needle.length === 0) {',
|
||||
' return haystack.length + 1;',
|
||||
' } else {',
|
||||
' return haystack.split(needle).length - 1;',
|
||||
' }',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ', ' + sub + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_SUBTRACTION];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_replace'] = function(block) {
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'TEXT',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
|
||||
var from = Blockly.JavaScript.valueToCode(block, 'FROM',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
var to = Blockly.JavaScript.valueToCode(block, 'TO',
|
||||
Blockly.JavaScript.ORDER_NONE) || '\'\'';
|
||||
// The regex escaping code below is taken from the implementation of
|
||||
// goog.string.regExpEscape.
|
||||
var functionName = Blockly.JavaScript.provideFunction_(
|
||||
'textReplace',
|
||||
['function ' + Blockly.JavaScript.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(haystack, needle, replacement) {',
|
||||
' needle = ' +
|
||||
'needle.replace(/([-()\\[\\]{}+?*.$\\^|,:#<!\\\\])/g,"\\\\$1")',
|
||||
' .replace(/\\x08/g,"\\\\x08");',
|
||||
' return haystack.replace(new RegExp(needle, \'g\'), replacement);',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ', ' + from + ', ' + to + ')';
|
||||
return [code, Blockly.JavaScript.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['text_reverse'] = function(block) {
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'TEXT',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
|
||||
var code = text + '.split(\'\').reverse().join(\'\')';
|
||||
return [code, Blockly.JavaScript.ORDER_MEMBER];
|
||||
};
|
||||
46
node_modules/node-blockly/blockly/generators/javascript/variables.js
generated
vendored
Normal file
46
node_modules/node-blockly/blockly/generators/javascript/variables.js
generated
vendored
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 JavaScript for variable blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript.variables');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
|
||||
|
||||
Blockly.JavaScript['variables_get'] = function(block) {
|
||||
// Variable getter.
|
||||
var code = Blockly.JavaScript.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return [code, Blockly.JavaScript.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.JavaScript['variables_set'] = function(block) {
|
||||
// Variable setter.
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_ASSIGNMENT) || '0';
|
||||
var varName = Blockly.JavaScript.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = ' + argument0 + ';\n';
|
||||
};
|
||||
37
node_modules/node-blockly/blockly/generators/javascript/variables_dynamic.js
generated
vendored
Normal file
37
node_modules/node-blockly/blockly/generators/javascript/variables_dynamic.js
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2018 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 dynamic variable blocks.
|
||||
* @author fenichel@google.com (Rachel Fenichel)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.JavaScript.variablesDynamic');
|
||||
|
||||
goog.require('Blockly.JavaScript');
|
||||
goog.require('Blockly.JavaScript.variables');
|
||||
|
||||
|
||||
// JavaScript is dynamically typed.
|
||||
Blockly.JavaScript['variables_get_dynamic'] =
|
||||
Blockly.JavaScript['variables_get'];
|
||||
Blockly.JavaScript['variables_set_dynamic'] =
|
||||
Blockly.JavaScript['variables_set'];
|
||||
195
node_modules/node-blockly/blockly/generators/lua.js
generated
vendored
Normal file
195
node_modules/node-blockly/blockly/generators/lua.js
generated
vendored
Normal file
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2016 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 Lua for blocks.
|
||||
* @author rodrigoq@google.com (Rodrigo Queiro)
|
||||
* Based on Ellen Spertus's blocky-lua project.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua');
|
||||
|
||||
goog.require('Blockly.Generator');
|
||||
|
||||
|
||||
/**
|
||||
* Lua code generator.
|
||||
* @type {!Blockly.Generator}
|
||||
*/
|
||||
Blockly.Lua = new Blockly.Generator('Lua');
|
||||
|
||||
/**
|
||||
* 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.Lua.addReservedWords(
|
||||
// Special character
|
||||
'_,' +
|
||||
// From theoriginalbit's script:
|
||||
// https://github.com/espertus/blockly-lua/issues/6
|
||||
'__inext,assert,bit,colors,colours,coroutine,disk,dofile,error,fs,' +
|
||||
'fetfenv,getmetatable,gps,help,io,ipairs,keys,loadfile,loadstring,math,' +
|
||||
'native,next,os,paintutils,pairs,parallel,pcall,peripheral,print,' +
|
||||
'printError,rawequal,rawget,rawset,read,rednet,redstone,rs,select,' +
|
||||
'setfenv,setmetatable,sleep,string,table,term,textutils,tonumber,' +
|
||||
'tostring,turtle,type,unpack,vector,write,xpcall,_VERSION,__indext,' +
|
||||
// Not included in the script, probably because it wasn't enabled:
|
||||
'HTTP,' +
|
||||
// Keywords (http://www.lua.org/pil/1.3.html).
|
||||
'and,break,do,else,elseif,end,false,for,function,if,in,local,nil,not,or,' +
|
||||
'repeat,return,then,true,until,while,' +
|
||||
// Metamethods (http://www.lua.org/manual/5.2/manual.html).
|
||||
'add,sub,mul,div,mod,pow,unm,concat,len,eq,lt,le,index,newindex,call,' +
|
||||
// Basic functions (http://www.lua.org/manual/5.2/manual.html, section 6.1).
|
||||
'assert,collectgarbage,dofile,error,_G,getmetatable,inpairs,load,' +
|
||||
'loadfile,next,pairs,pcall,print,rawequal,rawget,rawlen,rawset,select,' +
|
||||
'setmetatable,tonumber,tostring,type,_VERSION,xpcall,' +
|
||||
// Modules (http://www.lua.org/manual/5.2/manual.html, section 6.3).
|
||||
'require,package,string,table,math,bit32,io,file,os,debug'
|
||||
);
|
||||
|
||||
/**
|
||||
* Order of operation ENUMs.
|
||||
* http://www.lua.org/manual/5.3/manual.html#3.4.8
|
||||
*/
|
||||
Blockly.Lua.ORDER_ATOMIC = 0; // literals
|
||||
// The next level was not explicit in documentation and inferred by Ellen.
|
||||
Blockly.Lua.ORDER_HIGH = 1; // Function calls, tables[]
|
||||
Blockly.Lua.ORDER_EXPONENTIATION = 2; // ^
|
||||
Blockly.Lua.ORDER_UNARY = 3; // not # - ~
|
||||
Blockly.Lua.ORDER_MULTIPLICATIVE = 4; // * / %
|
||||
Blockly.Lua.ORDER_ADDITIVE = 5; // + -
|
||||
Blockly.Lua.ORDER_CONCATENATION = 6; // ..
|
||||
Blockly.Lua.ORDER_RELATIONAL = 7; // < > <= >= ~= ==
|
||||
Blockly.Lua.ORDER_AND = 8; // and
|
||||
Blockly.Lua.ORDER_OR = 9; // or
|
||||
Blockly.Lua.ORDER_NONE = 99;
|
||||
|
||||
/**
|
||||
* Note: Lua is not supporting zero-indexing since the language itself is
|
||||
* one-indexed, so the generator does not repoct the oneBasedIndex configuration
|
||||
* option used for lists and text.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
Blockly.Lua.init = function(workspace) {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly.Lua.definitions_ = Object.create(null);
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
// to actual function names (to avoid collisions with user functions).
|
||||
Blockly.Lua.functionNames_ = Object.create(null);
|
||||
|
||||
if (!Blockly.Lua.variableDB_) {
|
||||
Blockly.Lua.variableDB_ =
|
||||
new Blockly.Names(Blockly.Lua.RESERVED_WORDS_);
|
||||
} else {
|
||||
Blockly.Lua.variableDB_.reset();
|
||||
}
|
||||
Blockly.Lua.variableDB_.setVariableMap(workspace.getVariableMap());
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepend the generated code with the variable definitions.
|
||||
* @param {string} code Generated code.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
Blockly.Lua.finish = function(code) {
|
||||
// Convert the definitions dictionary into a list.
|
||||
var definitions = [];
|
||||
for (var name in Blockly.Lua.definitions_) {
|
||||
definitions.push(Blockly.Lua.definitions_[name]);
|
||||
}
|
||||
// Clean up temporary data.
|
||||
delete Blockly.Lua.definitions_;
|
||||
delete Blockly.Lua.functionNames_;
|
||||
Blockly.Lua.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. In Lua, an expression is not a legal statement, so we must assign
|
||||
* the value to the (conventionally ignored) _.
|
||||
* http://lua-users.org/wiki/ExpressionsAsStatements
|
||||
* @param {string} line Line of generated code.
|
||||
* @return {string} Legal line of code.
|
||||
*/
|
||||
Blockly.Lua.scrubNakedValue = function(line) {
|
||||
return 'local _ = ' + line + '\n';
|
||||
};
|
||||
|
||||
/**
|
||||
* Encode a string as a properly escaped Lua string, complete with
|
||||
* quotes.
|
||||
* @param {string} string Text to encode.
|
||||
* @return {string} Lua string.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Lua.quote_ = function(string) {
|
||||
string = string.replace(/\\/g, '\\\\')
|
||||
.replace(/\n/g, '\\\n')
|
||||
.replace(/'/g, '\\\'');
|
||||
return '\'' + string + '\'';
|
||||
};
|
||||
|
||||
/**
|
||||
* Common tasks for generating Lua 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 Lua code created for this block.
|
||||
* @return {string} Lua code with comments and subsequent blocks added.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Lua.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.Lua.COMMENT_WRAP - 3);
|
||||
if (comment) {
|
||||
commentCode += Blockly.Lua.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) {
|
||||
comment = Blockly.Lua.allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += Blockly.Lua.prefixLines(comment, '-- ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
|
||||
var nextCode = Blockly.Lua.blockToCode(nextBlock);
|
||||
return commentCode + code + nextCode;
|
||||
};
|
||||
90
node_modules/node-blockly/blockly/generators/lua/colour.js
generated
vendored
Normal file
90
node_modules/node-blockly/blockly/generators/lua/colour.js
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2016 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 Lua for colour blocks.
|
||||
* @author rodrigoq@google.com (Rodrigo Queiro)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.colour');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
|
||||
|
||||
Blockly.Lua['colour_picker'] = function(block) {
|
||||
// Colour picker.
|
||||
var code = '\'' + block.getFieldValue('COLOUR') + '\'';
|
||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Lua['colour_random'] = function(block) {
|
||||
// Generate a random colour.
|
||||
var code = 'string.format("#%06x", math.random(0, 2^24 - 1))';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['colour_rgb'] = function(block) {
|
||||
// Compose a colour from RGB components expressed as percentages.
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'colour_rgb',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(r, g, b)',
|
||||
' r = math.floor(math.min(100, math.max(0, r)) * 2.55 + .5)',
|
||||
' g = math.floor(math.min(100, math.max(0, g)) * 2.55 + .5)',
|
||||
' b = math.floor(math.min(100, math.max(0, b)) * 2.55 + .5)',
|
||||
' return string.format("#%02x%02x%02x", r, g, b)',
|
||||
'end']);
|
||||
var r = Blockly.Lua.valueToCode(block, 'RED',
|
||||
Blockly.Lua.ORDER_NONE) || 0;
|
||||
var g = Blockly.Lua.valueToCode(block, 'GREEN',
|
||||
Blockly.Lua.ORDER_NONE) || 0;
|
||||
var b = Blockly.Lua.valueToCode(block, 'BLUE',
|
||||
Blockly.Lua.ORDER_NONE) || 0;
|
||||
var code = functionName + '(' + r + ', ' + g + ', ' + b + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['colour_blend'] = function(block) {
|
||||
// Blend two colours together.
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'colour_blend',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(colour1, colour2, ratio)',
|
||||
' local r1 = tonumber(string.sub(colour1, 2, 3), 16)',
|
||||
' local r2 = tonumber(string.sub(colour2, 2, 3), 16)',
|
||||
' local g1 = tonumber(string.sub(colour1, 4, 5), 16)',
|
||||
' local g2 = tonumber(string.sub(colour2, 4, 5), 16)',
|
||||
' local b1 = tonumber(string.sub(colour1, 6, 7), 16)',
|
||||
' local b2 = tonumber(string.sub(colour2, 6, 7), 16)',
|
||||
' local ratio = math.min(1, math.max(0, ratio))',
|
||||
' local r = math.floor(r1 * (1 - ratio) + r2 * ratio + .5)',
|
||||
' local g = math.floor(g1 * (1 - ratio) + g2 * ratio + .5)',
|
||||
' local b = math.floor(b1 * (1 - ratio) + b2 * ratio + .5)',
|
||||
' return string.format("#%02x%02x%02x", r, g, b)',
|
||||
'end']);
|
||||
var colour1 = Blockly.Lua.valueToCode(block, 'COLOUR1',
|
||||
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
|
||||
var colour2 = Blockly.Lua.valueToCode(block, 'COLOUR2',
|
||||
Blockly.Lua.ORDER_NONE) || '\'#000000\'';
|
||||
var ratio = Blockly.Lua.valueToCode(block, 'RATIO',
|
||||
Blockly.Lua.ORDER_NONE) || 0;
|
||||
var code = functionName + '(' + colour1 + ', ' + colour2 + ', ' + ratio + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
382
node_modules/node-blockly/blockly/generators/lua/lists.js
generated
vendored
Normal file
382
node_modules/node-blockly/blockly/generators/lua/lists.js
generated
vendored
Normal file
@@ -0,0 +1,382 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2016 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 Lua for list blocks.
|
||||
* @author rodrigoq@google.com (Rodrigo Queiro)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.lists');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
|
||||
|
||||
Blockly.Lua['lists_create_empty'] = function(block) {
|
||||
// Create an empty list.
|
||||
return ['{}', Blockly.Lua.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Lua['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.Lua.valueToCode(block, 'ADD' + i,
|
||||
Blockly.Lua.ORDER_NONE) || 'None';
|
||||
}
|
||||
var code = '{' + elements.join(', ') + '}';
|
||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Lua['lists_repeat'] = function(block) {
|
||||
// Create a list with one element repeated.
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'create_list_repeated',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(item, count)',
|
||||
' local t = {}',
|
||||
' for i = 1, count do',
|
||||
' table.insert(t, item)',
|
||||
' end',
|
||||
' return t',
|
||||
'end']);
|
||||
var element = Blockly.Lua.valueToCode(block, 'ITEM',
|
||||
Blockly.Lua.ORDER_NONE) || 'None';
|
||||
var repeatCount = Blockly.Lua.valueToCode(block, 'NUM',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
var code = functionName + '(' + element + ', ' + repeatCount + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['lists_length'] = function(block) {
|
||||
// String or array length.
|
||||
var list = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_UNARY) || '{}';
|
||||
return ['#' + list, Blockly.Lua.ORDER_UNARY];
|
||||
};
|
||||
|
||||
Blockly.Lua['lists_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var list = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_UNARY) || '{}';
|
||||
var code = '#' + list + ' == 0';
|
||||
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
||||
};
|
||||
|
||||
Blockly.Lua['lists_indexOf'] = function(block) {
|
||||
// Find an item in the list.
|
||||
var item = Blockly.Lua.valueToCode(block, 'FIND',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var list = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_NONE) || '{}';
|
||||
if (block.getFieldValue('END') == 'FIRST') {
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'first_index',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t, elem)',
|
||||
' for k, v in ipairs(t) do',
|
||||
' if v == elem then',
|
||||
' return k',
|
||||
' end',
|
||||
' end',
|
||||
' return 0',
|
||||
'end']);
|
||||
} else {
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'last_index',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t, elem)',
|
||||
' for i = #t, 1, -1 do',
|
||||
' if t[i] == elem then',
|
||||
' return i',
|
||||
' end',
|
||||
' end',
|
||||
' return 0',
|
||||
'end']);
|
||||
}
|
||||
var code = functionName + '(' + list + ', ' + item + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns an expression calculating the index into a list.
|
||||
* @private
|
||||
* @param {string} listName Name of the list, used to calculate length.
|
||||
* @param {string} where The method of indexing, selected by dropdown in Blockly
|
||||
* @param {string=} opt_at The optional offset when indexing from start/end.
|
||||
* @return {string} Index expression.
|
||||
*/
|
||||
Blockly.Lua.lists.getIndex_ = function(listName, where, opt_at) {
|
||||
if (where == 'FIRST') {
|
||||
return '1';
|
||||
} else if (where == 'FROM_END') {
|
||||
return '#' + listName + ' + 1 - ' + opt_at;
|
||||
} else if (where == 'LAST') {
|
||||
return '#' + listName;
|
||||
} else if (where == 'RANDOM') {
|
||||
return 'math.random(#' + listName + ')';
|
||||
} else {
|
||||
return opt_at;
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Lua['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 list = Blockly.Lua.valueToCode(block, 'VALUE', Blockly.Lua.ORDER_HIGH) ||
|
||||
'{}';
|
||||
var getIndex_ = Blockly.Lua.lists.getIndex_;
|
||||
|
||||
// If `list` would be evaluated more than once (which is the case for LAST,
|
||||
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
|
||||
if ((where == 'LAST' || where == 'FROM_END' || where == 'RANDOM') &&
|
||||
!list.match(/^\w+$/)) {
|
||||
// `list` is an expression, so we may not evaluate it more than once.
|
||||
if (mode == 'REMOVE') {
|
||||
// We can use multiple statements.
|
||||
var atOrder = (where == 'FROM_END') ? Blockly.Lua.ORDER_ADDITIVE :
|
||||
Blockly.Lua.ORDER_NONE;
|
||||
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
|
||||
var listVar = Blockly.Lua.variableDB_.getDistinctName(
|
||||
'tmp_list', Blockly.Variables.NAME_TYPE);
|
||||
at = getIndex_(listVar, where, at);
|
||||
var code = listVar + ' = ' + list + '\n' +
|
||||
'table.remove(' + listVar + ', ' + at + ')\n';
|
||||
return code;
|
||||
} else {
|
||||
// We need to create a procedure to avoid reevaluating values.
|
||||
var at = Blockly.Lua.valueToCode(block, 'AT', Blockly.Lua.ORDER_NONE) ||
|
||||
'1';
|
||||
if (mode == 'GET') {
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'list_get_' + where.toLowerCase(),
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
|
||||
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
||||
// we add it as a parameter.
|
||||
((where == 'FROM_END' || where == 'FROM_START') ?
|
||||
', at)' : ')'),
|
||||
' return t[' + getIndex_('t', where, 'at') + ']',
|
||||
'end']);
|
||||
} else { // mode == 'GET_REMOVE'
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'list_remove_' + where.toLowerCase(),
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t' +
|
||||
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
||||
// we add it as a parameter.
|
||||
((where == 'FROM_END' || where == 'FROM_START') ?
|
||||
', at)' : ')'),
|
||||
' return table.remove(t, ' + getIndex_('t', where, 'at') + ')',
|
||||
'end']);
|
||||
}
|
||||
var code = functionName + '(' + list +
|
||||
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
|
||||
// pass it.
|
||||
((where == 'FROM_END' || where == 'FROM_START') ? ', ' + at : '') +
|
||||
')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
}
|
||||
} else {
|
||||
// Either `list` is a simple variable, or we only need to refer to `list`
|
||||
// once.
|
||||
var atOrder = (mode == 'GET' && where == 'FROM_END') ?
|
||||
Blockly.Lua.ORDER_ADDITIVE : Blockly.Lua.ORDER_NONE;
|
||||
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
|
||||
at = getIndex_(list, where, at);
|
||||
if (mode == 'GET') {
|
||||
var code = list + '[' + at + ']';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
} else {
|
||||
var code = 'table.remove(' + list + ', ' + at + ')';
|
||||
if (mode == 'GET_REMOVE') {
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
} else { // `mode` == 'REMOVE'
|
||||
return code + '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Lua['lists_setIndex'] = function(block) {
|
||||
// Set element at index.
|
||||
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
||||
var list = Blockly.Lua.valueToCode(block, 'LIST',
|
||||
Blockly.Lua.ORDER_HIGH) || '{}';
|
||||
var mode = block.getFieldValue('MODE') || 'SET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var at = Blockly.Lua.valueToCode(block, 'AT',
|
||||
Blockly.Lua.ORDER_ADDITIVE) || '1';
|
||||
var value = Blockly.Lua.valueToCode(block, 'TO',
|
||||
Blockly.Lua.ORDER_NONE) || 'None';
|
||||
var getIndex_ = Blockly.Lua.lists.getIndex_;
|
||||
|
||||
var code = '';
|
||||
// If `list` would be evaluated more than once (which is the case for LAST,
|
||||
// FROM_END, and RANDOM) and is non-trivial, make sure to access it only once.
|
||||
if ((where == 'LAST' || where == 'FROM_END' || where == 'RANDOM') &&
|
||||
!list.match(/^\w+$/)) {
|
||||
// `list` is an expression, so we may not evaluate it more than once.
|
||||
// We can use multiple statements.
|
||||
var listVar = Blockly.Lua.variableDB_.getDistinctName(
|
||||
'tmp_list', Blockly.Variables.NAME_TYPE);
|
||||
code = listVar + ' = ' + list + '\n';
|
||||
list = listVar;
|
||||
}
|
||||
if (mode == 'SET') {
|
||||
code += list + '[' + getIndex_(list, where, at) + '] = ' + value;
|
||||
} else { // `mode` == 'INSERT'
|
||||
// LAST is a special case, because we want to insert
|
||||
// *after* not *before*, the existing last element.
|
||||
code += 'table.insert(' + list + ', ' +
|
||||
(getIndex_(list, where, at) + (where == 'LAST' ? ' + 1' : '')) +
|
||||
', ' + value + ')';
|
||||
}
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
Blockly.Lua['lists_getSublist'] = function(block) {
|
||||
// Get sublist.
|
||||
var list = Blockly.Lua.valueToCode(block, 'LIST',
|
||||
Blockly.Lua.ORDER_NONE) || '{}';
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
var at1 = Blockly.Lua.valueToCode(block, 'AT1',
|
||||
Blockly.Lua.ORDER_NONE) || '1';
|
||||
var at2 = Blockly.Lua.valueToCode(block, 'AT2',
|
||||
Blockly.Lua.ORDER_NONE) || '1';
|
||||
var getIndex_ = Blockly.Lua.lists.getIndex_;
|
||||
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'list_sublist_' + where1.toLowerCase() + '_' + where2.toLowerCase(),
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(source' +
|
||||
// The value for 'FROM_END' and'FROM_START' depends on `at` so
|
||||
// we add it as a parameter.
|
||||
((where1 == 'FROM_END' || where1 == 'FROM_START') ? ', at1' : '') +
|
||||
((where2 == 'FROM_END' || where2 == 'FROM_START') ? ', at2' : '') +
|
||||
')',
|
||||
' local t = {}',
|
||||
' local start = ' + getIndex_('source', where1, 'at1'),
|
||||
' local finish = ' + getIndex_('source', where2, 'at2'),
|
||||
' for i = start, finish do',
|
||||
' table.insert(t, source[i])',
|
||||
' end',
|
||||
' return t',
|
||||
'end']);
|
||||
var code = functionName + '(' + list +
|
||||
// The value for 'FROM_END' and 'FROM_START' depends on `at` so we
|
||||
// pass it.
|
||||
((where1 == 'FROM_END' || where1 == 'FROM_START') ? ', ' + at1 : '') +
|
||||
((where2 == 'FROM_END' || where2 == 'FROM_START') ? ', ' + at2 : '') +
|
||||
')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['lists_sort'] = function(block) {
|
||||
// Block for sorting a list.
|
||||
var list = Blockly.Lua.valueToCode(
|
||||
block, 'LIST', Blockly.Lua.ORDER_NONE) || '{}';
|
||||
var direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
||||
var type = block.getFieldValue('TYPE');
|
||||
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'list_sort',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(list, typev, direction)',
|
||||
' local t = {}',
|
||||
' for n,v in pairs(list) do table.insert(t, v) end', // Shallow-copy.
|
||||
' local compareFuncs = {',
|
||||
' NUMERIC = function(a, b)',
|
||||
' return (tonumber(tostring(a)) or 0)',
|
||||
' < (tonumber(tostring(b)) or 0) end,',
|
||||
' TEXT = function(a, b)',
|
||||
' return tostring(a) < tostring(b) end,',
|
||||
' IGNORE_CASE = function(a, b)',
|
||||
' return string.lower(tostring(a)) < string.lower(tostring(b)) end',
|
||||
' }',
|
||||
' local compareTemp = compareFuncs[typev]',
|
||||
' local compare = compareTemp',
|
||||
' if direction == -1',
|
||||
' then compare = function(a, b) return compareTemp(b, a) end',
|
||||
' end',
|
||||
' table.sort(t, compare)',
|
||||
' return t',
|
||||
'end']);
|
||||
|
||||
var code = functionName +
|
||||
'(' + list + ',"' + type + '", ' + direction + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['lists_split'] = function(block) {
|
||||
// Block for splitting text into a list, or joining a list into text.
|
||||
var input = Blockly.Lua.valueToCode(block, 'INPUT',
|
||||
Blockly.Lua.ORDER_NONE);
|
||||
var delimiter = Blockly.Lua.valueToCode(block, 'DELIM',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var mode = block.getFieldValue('MODE');
|
||||
var functionName;
|
||||
if (mode == 'SPLIT') {
|
||||
if (!input) {
|
||||
input = '\'\'';
|
||||
}
|
||||
functionName = Blockly.Lua.provideFunction_(
|
||||
'list_string_split',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(input, delim)',
|
||||
' local t = {}',
|
||||
' local pos = 1',
|
||||
' while true do',
|
||||
' next_delim = string.find(input, delim, pos)',
|
||||
' if next_delim == nil then',
|
||||
' table.insert(t, string.sub(input, pos))',
|
||||
' break',
|
||||
' else',
|
||||
' table.insert(t, string.sub(input, pos, next_delim-1))',
|
||||
' pos = next_delim + #delim',
|
||||
' end',
|
||||
' end',
|
||||
' return t',
|
||||
'end']);
|
||||
} else if (mode == 'JOIN') {
|
||||
if (!input) {
|
||||
input = '{}';
|
||||
}
|
||||
functionName = 'table.concat';
|
||||
} else {
|
||||
throw 'Unknown mode: ' + mode;
|
||||
}
|
||||
var code = functionName + '(' + input + ', ' + delimiter + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['lists_reverse'] = function(block) {
|
||||
// Block for reversing a list.
|
||||
var list = Blockly.Lua.valueToCode(block, 'LIST',
|
||||
Blockly.Lua.ORDER_NONE) || '{}';
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'list_reverse',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(input)',
|
||||
' local reversed = {}',
|
||||
' for i = #input, 1, -1 do',
|
||||
' table.insert(reversed, input[i])',
|
||||
' end',
|
||||
' return reversed',
|
||||
'end']);
|
||||
var code = 'list_reverse(' + list + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
128
node_modules/node-blockly/blockly/generators/lua/logic.js
generated
vendored
Normal file
128
node_modules/node-blockly/blockly/generators/lua/logic.js
generated
vendored
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2016 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 Lua for logic blocks.
|
||||
* @author rodrigoq@google.com (Rodrigo Queiro)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.logic');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
|
||||
|
||||
Blockly.Lua['controls_if'] = function(block) {
|
||||
// If/elseif/else condition.
|
||||
var n = 0;
|
||||
var code = '', branchCode, conditionCode;
|
||||
do {
|
||||
conditionCode = Blockly.Lua.valueToCode(block, 'IF' + n,
|
||||
Blockly.Lua.ORDER_NONE) || 'false';
|
||||
branchCode = Blockly.Lua.statementToCode(block, 'DO' + n);
|
||||
code += (n > 0 ? 'else' : '') +
|
||||
'if ' + conditionCode + ' then\n' + branchCode;
|
||||
|
||||
++n;
|
||||
} while (block.getInput('IF' + n));
|
||||
|
||||
if (block.getInput('ELSE')) {
|
||||
branchCode = Blockly.Lua.statementToCode(block, 'ELSE');
|
||||
code += 'else\n' + branchCode;
|
||||
}
|
||||
return code + 'end\n';
|
||||
};
|
||||
|
||||
Blockly.Lua['controls_ifelse'] = Blockly.Lua['controls_if'];
|
||||
|
||||
Blockly.Lua['logic_compare'] = function(block) {
|
||||
// Comparison operator.
|
||||
var OPERATORS = {
|
||||
'EQ': '==',
|
||||
'NEQ': '~=',
|
||||
'LT': '<',
|
||||
'LTE': '<=',
|
||||
'GT': '>',
|
||||
'GTE': '>='
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('OP')];
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'A',
|
||||
Blockly.Lua.ORDER_RELATIONAL) || '0';
|
||||
var argument1 = Blockly.Lua.valueToCode(block, 'B',
|
||||
Blockly.Lua.ORDER_RELATIONAL) || '0';
|
||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
||||
};
|
||||
|
||||
Blockly.Lua['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
var operator = (block.getFieldValue('OP') == 'AND') ? 'and' : 'or';
|
||||
var order = (operator == 'and') ? Blockly.Lua.ORDER_AND :
|
||||
Blockly.Lua.ORDER_OR;
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'A', order);
|
||||
var argument1 = Blockly.Lua.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.Lua['logic_negate'] = function(block) {
|
||||
// Negation.
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
|
||||
Blockly.Lua.ORDER_UNARY) || 'true';
|
||||
var code = 'not ' + argument0;
|
||||
return [code, Blockly.Lua.ORDER_UNARY];
|
||||
};
|
||||
|
||||
Blockly.Lua['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
|
||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Lua['logic_null'] = function(block) {
|
||||
// Null data type.
|
||||
return ['nil', Blockly.Lua.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Lua['logic_ternary'] = function(block) {
|
||||
// Ternary operator.
|
||||
var value_if = Blockly.Lua.valueToCode(block, 'IF',
|
||||
Blockly.Lua.ORDER_AND) || 'false';
|
||||
var value_then = Blockly.Lua.valueToCode(block, 'THEN',
|
||||
Blockly.Lua.ORDER_AND) || 'nil';
|
||||
var value_else = Blockly.Lua.valueToCode(block, 'ELSE',
|
||||
Blockly.Lua.ORDER_OR) || 'nil';
|
||||
var code = value_if + ' and ' + value_then + ' or ' + value_else;
|
||||
return [code, Blockly.Lua.ORDER_OR];
|
||||
};
|
||||
166
node_modules/node-blockly/blockly/generators/lua/loops.js
generated
vendored
Normal file
166
node_modules/node-blockly/blockly/generators/lua/loops.js
generated
vendored
Normal file
@@ -0,0 +1,166 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2016 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 Lua for loop blocks.
|
||||
* @author rodrigoq@google.com (Rodrigo Queiro)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.loops');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
|
||||
|
||||
/**
|
||||
* This is the text used to implement a <pre>continue</pre>.
|
||||
* It is also used to recognise <pre>continue</pre>s in generated code so that
|
||||
* the appropriate label can be put at the end of the loop body.
|
||||
* @const {string}
|
||||
*/
|
||||
Blockly.Lua.CONTINUE_STATEMENT = 'goto continue\n';
|
||||
|
||||
/**
|
||||
* If the loop body contains a "goto continue" statement, add a continue label
|
||||
* to the loop body. Slightly inefficient, as continue labels will be generated
|
||||
* in all outer loops, but this is safer than duplicating the logic of
|
||||
* blockToCode.
|
||||
*
|
||||
* @param {string} branch Generated code of the loop body
|
||||
* @return {string} Generated label or '' if unnecessary
|
||||
*/
|
||||
Blockly.Lua.addContinueLabel = function(branch) {
|
||||
if (branch.indexOf(Blockly.Lua.CONTINUE_STATEMENT) > -1) {
|
||||
return branch + Blockly.Lua.INDENT + '::continue::\n';
|
||||
} else {
|
||||
return branch;
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Lua['controls_repeat'] = function(block) {
|
||||
// Repeat n times (internal number).
|
||||
var repeats = parseInt(block.getFieldValue('TIMES'), 10);
|
||||
var branch = Blockly.Lua.statementToCode(block, 'DO') || '';
|
||||
branch = Blockly.Lua.addContinueLabel(branch);
|
||||
var loopVar = Blockly.Lua.variableDB_.getDistinctName(
|
||||
'count', Blockly.Variables.NAME_TYPE);
|
||||
var code = 'for ' + loopVar + ' = 1, ' + repeats + ' do\n' + branch + 'end\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Lua['controls_repeat_ext'] = function(block) {
|
||||
// Repeat n times (external number).
|
||||
var repeats = Blockly.Lua.valueToCode(block, 'TIMES',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
if (Blockly.isNumber(repeats)) {
|
||||
repeats = parseInt(repeats, 10);
|
||||
} else {
|
||||
repeats = 'math.floor(' + repeats + ')';
|
||||
}
|
||||
var branch = Blockly.Lua.statementToCode(block, 'DO') || '\n';
|
||||
branch = Blockly.Lua.addContinueLabel(branch);
|
||||
var loopVar = Blockly.Lua.variableDB_.getDistinctName(
|
||||
'count', Blockly.Variables.NAME_TYPE);
|
||||
var code = 'for ' + loopVar + ' = 1, ' + repeats + ' do\n' +
|
||||
branch + 'end\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Lua['controls_whileUntil'] = function(block) {
|
||||
// Do while/until loop.
|
||||
var until = block.getFieldValue('MODE') == 'UNTIL';
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'BOOL',
|
||||
until ? Blockly.Lua.ORDER_UNARY :
|
||||
Blockly.Lua.ORDER_NONE) || 'false';
|
||||
var branch = Blockly.Lua.statementToCode(block, 'DO') || '\n';
|
||||
branch = Blockly.Lua.addLoopTrap(branch, block.id);
|
||||
branch = Blockly.Lua.addContinueLabel(branch);
|
||||
if (until) {
|
||||
argument0 = 'not ' + argument0;
|
||||
}
|
||||
return 'while ' + argument0 + ' do\n' + branch + 'end\n';
|
||||
};
|
||||
|
||||
Blockly.Lua['controls_for'] = function(block) {
|
||||
// For loop.
|
||||
var variable0 = Blockly.Lua.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var startVar = Blockly.Lua.valueToCode(block, 'FROM',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
var endVar = Blockly.Lua.valueToCode(block, 'TO',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
var increment = Blockly.Lua.valueToCode(block, 'BY',
|
||||
Blockly.Lua.ORDER_NONE) || '1';
|
||||
var branch = Blockly.Lua.statementToCode(block, 'DO') || '\n';
|
||||
branch = Blockly.Lua.addLoopTrap(branch, block.id);
|
||||
branch = Blockly.Lua.addContinueLabel(branch);
|
||||
var code = '';
|
||||
var incValue;
|
||||
if (Blockly.isNumber(startVar) && Blockly.isNumber(endVar) &&
|
||||
Blockly.isNumber(increment)) {
|
||||
// All arguments are simple numbers.
|
||||
var up = parseFloat(startVar) <= parseFloat(endVar);
|
||||
var step = Math.abs(parseFloat(increment));
|
||||
incValue = (up ? '' : '-') + step;
|
||||
} else {
|
||||
code = '';
|
||||
// Determine loop direction at start, in case one of the bounds
|
||||
// changes during loop execution.
|
||||
incValue = Blockly.Lua.variableDB_.getDistinctName(
|
||||
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
|
||||
code += incValue + ' = ';
|
||||
if (Blockly.isNumber(increment)) {
|
||||
code += Math.abs(increment) + '\n';
|
||||
} else {
|
||||
code += 'math.abs(' + increment + ')\n';
|
||||
}
|
||||
code += 'if (' + startVar + ') > (' + endVar + ') then\n';
|
||||
code += Blockly.Lua.INDENT + incValue + ' = -' + incValue + '\n';
|
||||
code += 'end\n';
|
||||
}
|
||||
code += 'for ' + variable0 + ' = ' + startVar + ', ' + endVar +
|
||||
', ' + incValue;
|
||||
code += ' do\n' + branch + 'end\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Lua['controls_forEach'] = function(block) {
|
||||
// For each loop.
|
||||
var variable0 = Blockly.Lua.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'LIST',
|
||||
Blockly.Lua.ORDER_NONE) || '{}';
|
||||
var branch = Blockly.Lua.statementToCode(block, 'DO') || '\n';
|
||||
branch = Blockly.Lua.addContinueLabel(branch);
|
||||
var code = 'for _, ' + variable0 + ' in ipairs(' + argument0 + ') do \n' +
|
||||
branch + 'end\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Lua['controls_flow_statements'] = function(block) {
|
||||
// Flow statements: continue, break.
|
||||
switch (block.getFieldValue('FLOW')) {
|
||||
case 'BREAK':
|
||||
return 'break\n';
|
||||
case 'CONTINUE':
|
||||
return Blockly.Lua.CONTINUE_STATEMENT;
|
||||
}
|
||||
throw 'Unknown flow statement.';
|
||||
};
|
||||
427
node_modules/node-blockly/blockly/generators/lua/math.js
generated
vendored
Normal file
427
node_modules/node-blockly/blockly/generators/lua/math.js
generated
vendored
Normal file
@@ -0,0 +1,427 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2016 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 Lua for math blocks.
|
||||
* @author rodrigoq@google.com (Rodrigo Queiro)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.math');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
|
||||
|
||||
Blockly.Lua['math_number'] = function(block) {
|
||||
// Numeric value.
|
||||
var code = parseFloat(block.getFieldValue('NUM'));
|
||||
var order = code < 0 ? Blockly.Lua.ORDER_UNARY :
|
||||
Blockly.Lua.ORDER_ATOMIC;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Lua['math_arithmetic'] = function(block) {
|
||||
// Basic arithmetic operators, and power.
|
||||
var OPERATORS = {
|
||||
ADD: [' + ', Blockly.Lua.ORDER_ADDITIVE],
|
||||
MINUS: [' - ', Blockly.Lua.ORDER_ADDITIVE],
|
||||
MULTIPLY: [' * ', Blockly.Lua.ORDER_MULTIPLICATIVE],
|
||||
DIVIDE: [' / ', Blockly.Lua.ORDER_MULTIPLICATIVE],
|
||||
POWER: [' ^ ', Blockly.Lua.ORDER_EXPONENTIATION]
|
||||
};
|
||||
var tuple = OPERATORS[block.getFieldValue('OP')];
|
||||
var operator = tuple[0];
|
||||
var order = tuple[1];
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.Lua.valueToCode(block, 'B', order) || '0';
|
||||
var code = argument0 + operator + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Lua['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.
|
||||
arg = Blockly.Lua.valueToCode(block, 'NUM',
|
||||
Blockly.Lua.ORDER_UNARY) || '0';
|
||||
return ['-' + arg, Blockly.Lua.ORDER_UNARY];
|
||||
}
|
||||
if (operator == 'POW10') {
|
||||
arg = Blockly.Lua.valueToCode(block, 'NUM',
|
||||
Blockly.Lua.ORDER_EXPONENTIATION) || '0';
|
||||
return ['10 ^ ' + arg, Blockly.Lua.ORDER_EXPONENTIATION];
|
||||
}
|
||||
if (operator == 'ROUND') {
|
||||
arg = Blockly.Lua.valueToCode(block, 'NUM',
|
||||
Blockly.Lua.ORDER_ADDITIVE) || '0';
|
||||
} else {
|
||||
arg = Blockly.Lua.valueToCode(block, 'NUM',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
}
|
||||
switch (operator) {
|
||||
case 'ABS':
|
||||
code = 'math.abs(' + arg + ')';
|
||||
break;
|
||||
case 'ROOT':
|
||||
code = 'math.sqrt(' + arg + ')';
|
||||
break;
|
||||
case 'LN':
|
||||
code = 'math.log(' + arg + ')';
|
||||
break;
|
||||
case 'LOG10':
|
||||
code = 'math.log(' + arg + ', 10)';
|
||||
break;
|
||||
case 'EXP':
|
||||
code = 'math.exp(' + arg + ')';
|
||||
break;
|
||||
case 'ROUND':
|
||||
// This rounds up. Blockly does not specify rounding direction.
|
||||
code = 'math.floor(' + arg + ' + .5)';
|
||||
break;
|
||||
case 'ROUNDUP':
|
||||
code = 'math.ceil(' + arg + ')';
|
||||
break;
|
||||
case 'ROUNDDOWN':
|
||||
code = 'math.floor(' + arg + ')';
|
||||
break;
|
||||
case 'SIN':
|
||||
code = 'math.sin(math.rad(' + arg + '))';
|
||||
break;
|
||||
case 'COS':
|
||||
code = 'math.cos(math.rad(' + arg + '))';
|
||||
break;
|
||||
case 'TAN':
|
||||
code = 'math.tan(math.rad(' + arg + '))';
|
||||
break;
|
||||
case 'ASIN':
|
||||
code = 'math.deg(math.asin(' + arg + '))';
|
||||
break;
|
||||
case 'ACOS':
|
||||
code = 'math.deg(math.acos(' + arg + '))';
|
||||
break;
|
||||
case 'ATAN':
|
||||
code = 'math.deg(math.atan(' + arg + '))';
|
||||
break;
|
||||
default:
|
||||
throw 'Unknown math operator: ' + operator;
|
||||
}
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['math_constant'] = function(block) {
|
||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||
var CONSTANTS = {
|
||||
PI: ['math.pi', Blockly.Lua.ORDER_HIGH],
|
||||
E: ['math.exp(1)', Blockly.Lua.ORDER_HIGH],
|
||||
GOLDEN_RATIO: ['(1 + math.sqrt(5)) / 2', Blockly.Lua.ORDER_MULTIPLICATIVE],
|
||||
SQRT2: ['math.sqrt(2)', Blockly.Lua.ORDER_HIGH],
|
||||
SQRT1_2: ['math.sqrt(1 / 2)', Blockly.Lua.ORDER_HIGH],
|
||||
INFINITY: ['math.huge', Blockly.Lua.ORDER_HIGH]
|
||||
};
|
||||
return CONSTANTS[block.getFieldValue('CONSTANT')];
|
||||
};
|
||||
|
||||
Blockly.Lua['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.Lua.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
|
||||
var dropdown_property = block.getFieldValue('PROPERTY');
|
||||
var code;
|
||||
if (dropdown_property == 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'math_isPrime',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(n)',
|
||||
' -- https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if n == 2 or n == 3 then',
|
||||
' return true',
|
||||
' end',
|
||||
' -- False if n is NaN, negative, is 1, or not whole.',
|
||||
' -- And false if n is divisible by 2 or 3.',
|
||||
' if not(n > 1) or n % 1 ~= 0 or n % 2 == 0 or n % 3 == 0 then',
|
||||
' return false',
|
||||
' end',
|
||||
' -- Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for x = 6, math.sqrt(n) + 1.5, 6 do',
|
||||
' if n % (x - 1) == 0 or n % (x + 1) == 0 then',
|
||||
' return false',
|
||||
' end',
|
||||
' end',
|
||||
' return true',
|
||||
'end']);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
}
|
||||
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.Lua.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Lua.ORDER_MULTIPLICATIVE);
|
||||
// If 'divisor' is some code that evals to 0, Lua will produce a nan.
|
||||
// Let's produce nil if we can determine this at compile-time.
|
||||
if (!divisor || divisor == '0') {
|
||||
return ['nil', Blockly.Lua.ORDER_ATOMIC];
|
||||
}
|
||||
// The normal trick to implement ?: with and/or doesn't work here:
|
||||
// divisor == 0 and nil or number_to_check % divisor == 0
|
||||
// because nil is false, so allow a runtime failure. :-(
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
}
|
||||
return [code, Blockly.Lua.ORDER_RELATIONAL];
|
||||
};
|
||||
|
||||
Blockly.Lua['math_change'] = function(block) {
|
||||
// Add to a variable in place.
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'DELTA',
|
||||
Blockly.Lua.ORDER_ADDITIVE) || '0';
|
||||
var varName = Blockly.Lua.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = ' + varName + ' + ' + argument0 + '\n';
|
||||
};
|
||||
|
||||
// Rounding functions have a single operand.
|
||||
Blockly.Lua['math_round'] = Blockly.Lua['math_single'];
|
||||
// Trigonometry functions have a single operand.
|
||||
Blockly.Lua['math_trig'] = Blockly.Lua['math_single'];
|
||||
|
||||
Blockly.Lua['math_on_list'] = function(block) {
|
||||
// Math functions for lists.
|
||||
var func = block.getFieldValue('OP');
|
||||
var list = Blockly.Lua.valueToCode(block, 'LIST',
|
||||
Blockly.Lua.ORDER_NONE) || '{}';
|
||||
var functionName;
|
||||
|
||||
// Functions needed in more than one case.
|
||||
function provideSum() {
|
||||
return Blockly.Lua.provideFunction_(
|
||||
'math_sum',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t)',
|
||||
' local result = 0',
|
||||
' for _, v in ipairs(t) do',
|
||||
' result = result + v',
|
||||
' end',
|
||||
' return result',
|
||||
'end']);
|
||||
}
|
||||
|
||||
switch (func) {
|
||||
case 'SUM':
|
||||
functionName = provideSum();
|
||||
break;
|
||||
|
||||
case 'MIN':
|
||||
// Returns 0 for the empty list.
|
||||
functionName = Blockly.Lua.provideFunction_(
|
||||
'math_min',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t)',
|
||||
' if #t == 0 then',
|
||||
' return 0',
|
||||
' end',
|
||||
' local result = math.huge',
|
||||
' for _, v in ipairs(t) do',
|
||||
' if v < result then',
|
||||
' result = v',
|
||||
' end',
|
||||
' end',
|
||||
' return result',
|
||||
'end']);
|
||||
break;
|
||||
|
||||
case 'AVERAGE':
|
||||
// Returns 0 for the empty list.
|
||||
functionName = Blockly.Lua.provideFunction_(
|
||||
'math_average',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t)',
|
||||
' if #t == 0 then',
|
||||
' return 0',
|
||||
' end',
|
||||
' return ' + provideSum() + '(t) / #t',
|
||||
'end']);
|
||||
break;
|
||||
|
||||
case 'MAX':
|
||||
// Returns 0 for the empty list.
|
||||
functionName = Blockly.Lua.provideFunction_(
|
||||
'math_max',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t)',
|
||||
' if #t == 0 then',
|
||||
' return 0',
|
||||
' end',
|
||||
' local result = -math.huge',
|
||||
' for _, v in ipairs(t) do',
|
||||
' if v > result then',
|
||||
' result = v',
|
||||
' end',
|
||||
' end',
|
||||
' return result',
|
||||
'end']);
|
||||
break;
|
||||
|
||||
case 'MEDIAN':
|
||||
functionName = Blockly.Lua.provideFunction_(
|
||||
'math_median',
|
||||
// This operation excludes non-numbers.
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t)',
|
||||
' -- Source: http://lua-users.org/wiki/SimpleStats',
|
||||
' if #t == 0 then',
|
||||
' return 0',
|
||||
' end',
|
||||
' local temp={}',
|
||||
' for _, v in ipairs(t) do',
|
||||
' if type(v) == "number" then',
|
||||
' table.insert(temp, v)',
|
||||
' end',
|
||||
' end',
|
||||
' table.sort(temp)',
|
||||
' if #temp % 2 == 0 then',
|
||||
' return (temp[#temp/2] + temp[(#temp/2)+1]) / 2',
|
||||
' else',
|
||||
' return temp[math.ceil(#temp/2)]',
|
||||
' end',
|
||||
'end']);
|
||||
break;
|
||||
|
||||
case 'MODE':
|
||||
functionName = Blockly.Lua.provideFunction_(
|
||||
'math_modes',
|
||||
// As a list of numbers can contain more than one mode,
|
||||
// the returned result is provided as an array.
|
||||
// The Lua version includes non-numbers.
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t)',
|
||||
' -- Source: http://lua-users.org/wiki/SimpleStats',
|
||||
' local counts={}',
|
||||
' for _, v in ipairs(t) do',
|
||||
' if counts[v] == nil then',
|
||||
' counts[v] = 1',
|
||||
' else',
|
||||
' counts[v] = counts[v] + 1',
|
||||
' end',
|
||||
' end',
|
||||
' local biggestCount = 0',
|
||||
' for _, v in pairs(counts) do',
|
||||
' if v > biggestCount then',
|
||||
' biggestCount = v',
|
||||
' end',
|
||||
' end',
|
||||
' local temp={}',
|
||||
' for k, v in pairs(counts) do',
|
||||
' if v == biggestCount then',
|
||||
' table.insert(temp, k)',
|
||||
' end',
|
||||
' end',
|
||||
' return temp',
|
||||
'end']);
|
||||
break;
|
||||
|
||||
case 'STD_DEV':
|
||||
functionName = Blockly.Lua.provideFunction_(
|
||||
'math_standard_deviation',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t)',
|
||||
' local m',
|
||||
' local vm',
|
||||
' local total = 0',
|
||||
' local count = 0',
|
||||
' local result',
|
||||
' m = #t == 0 and 0 or ' + provideSum() + '(t) / #t',
|
||||
' for _, v in ipairs(t) do',
|
||||
" if type(v) == 'number' then",
|
||||
' vm = v - m',
|
||||
' total = total + (vm * vm)',
|
||||
' count = count + 1',
|
||||
' end',
|
||||
' end',
|
||||
' result = math.sqrt(total / (count-1))',
|
||||
' return result',
|
||||
'end']);
|
||||
break;
|
||||
|
||||
case 'RANDOM':
|
||||
functionName = Blockly.Lua.provideFunction_(
|
||||
'math_random_list',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(t)',
|
||||
' if #t == 0 then',
|
||||
' return nil',
|
||||
' end',
|
||||
' return t[math.random(#t)]',
|
||||
'end']);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw 'Unknown operator: ' + func;
|
||||
}
|
||||
return [functionName + '(' + list + ')', Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['math_modulo'] = function(block) {
|
||||
// Remainder computation.
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'DIVIDEND',
|
||||
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
|
||||
var argument1 = Blockly.Lua.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Lua.ORDER_MULTIPLICATIVE) || '0';
|
||||
var code = argument0 + ' % ' + argument1;
|
||||
return [code, Blockly.Lua.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
Blockly.Lua['math_constrain'] = function(block) {
|
||||
// Constrain a number between two limits.
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Lua.valueToCode(block, 'LOW',
|
||||
Blockly.Lua.ORDER_NONE) || '-math.huge';
|
||||
var argument2 = Blockly.Lua.valueToCode(block, 'HIGH',
|
||||
Blockly.Lua.ORDER_NONE) || 'math.huge';
|
||||
var code = 'math.min(math.max(' + argument0 + ', ' + argument1 + '), ' +
|
||||
argument2 + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['math_random_int'] = function(block) {
|
||||
// Random integer between [X] and [Y].
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'FROM',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
var argument1 = Blockly.Lua.valueToCode(block, 'TO',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
var code = 'math.random(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['math_random_float'] = function(block) {
|
||||
// Random fraction between 0 and 1.
|
||||
return ['math.random()', Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
112
node_modules/node-blockly/blockly/generators/lua/procedures.js
generated
vendored
Normal file
112
node_modules/node-blockly/blockly/generators/lua/procedures.js
generated
vendored
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2016 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 Lua for procedure blocks.
|
||||
* @author rodrigoq@google.com (Rodrigo Queiro)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.procedures');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
|
||||
|
||||
Blockly.Lua['procedures_defreturn'] = function(block) {
|
||||
// Define a procedure with a return value.
|
||||
var funcName = Blockly.Lua.variableDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
|
||||
var branch = Blockly.Lua.statementToCode(block, 'STACK');
|
||||
if (Blockly.Lua.STATEMENT_PREFIX) {
|
||||
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
|
||||
branch = Blockly.Lua.prefixLines(
|
||||
Blockly.Lua.STATEMENT_PREFIX.replace(/%1/g,
|
||||
'\'' + id + '\''), Blockly.Lua.INDENT) + branch;
|
||||
}
|
||||
if (Blockly.Lua.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.Lua.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
var returnValue = Blockly.Lua.valueToCode(block, 'RETURN',
|
||||
Blockly.Lua.ORDER_NONE) || '';
|
||||
if (returnValue) {
|
||||
returnValue = Blockly.Lua.INDENT + 'return ' + returnValue + '\n';
|
||||
} else if (!branch) {
|
||||
branch = '';
|
||||
}
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.Lua.variableDB_.getName(block.arguments_[i],
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
}
|
||||
var code = 'function ' + funcName + '(' + args.join(', ') + ')\n' +
|
||||
branch + returnValue + 'end\n';
|
||||
code = Blockly.Lua.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
Blockly.Lua.definitions_['%' + funcName] = code;
|
||||
return null;
|
||||
};
|
||||
|
||||
// Defining a procedure without a return value uses the same generator as
|
||||
// a procedure with a return value.
|
||||
Blockly.Lua['procedures_defnoreturn'] =
|
||||
Blockly.Lua['procedures_defreturn'];
|
||||
|
||||
Blockly.Lua['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
var funcName = Blockly.Lua.variableDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.Lua.valueToCode(block, 'ARG' + i,
|
||||
Blockly.Lua.ORDER_NONE) || 'nil';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['procedures_callnoreturn'] = function(block) {
|
||||
// Call a procedure with no return value.
|
||||
var funcName = Blockly.Lua.variableDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.Lua.valueToCode(block, 'ARG' + i,
|
||||
Blockly.Lua.ORDER_NONE) || 'nil';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ')\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Lua['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
var condition = Blockly.Lua.valueToCode(block, 'CONDITION',
|
||||
Blockly.Lua.ORDER_NONE) || 'false';
|
||||
var code = 'if ' + condition + ' then\n';
|
||||
if (block.hasReturnValue_) {
|
||||
var value = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_NONE) || 'nil';
|
||||
code += Blockly.Lua.INDENT + 'return ' + value + '\n';
|
||||
} else {
|
||||
code += Blockly.Lua.INDENT + 'return\n';
|
||||
}
|
||||
code += 'end\n';
|
||||
return code;
|
||||
};
|
||||
361
node_modules/node-blockly/blockly/generators/lua/text.js
generated
vendored
Normal file
361
node_modules/node-blockly/blockly/generators/lua/text.js
generated
vendored
Normal file
@@ -0,0 +1,361 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2016 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 Lua for text blocks.
|
||||
* @author rodrigoq@google.com (Rodrigo Queiro)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.texts');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
|
||||
|
||||
Blockly.Lua['text'] = function(block) {
|
||||
// Text value.
|
||||
var code = Blockly.Lua.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_join'] = function(block) {
|
||||
// Create a string made up of any number of elements of any type.
|
||||
if (block.itemCount_ == 0) {
|
||||
return ['\'\'', Blockly.Lua.ORDER_ATOMIC];
|
||||
} else if (block.itemCount_ == 1) {
|
||||
var element = Blockly.Lua.valueToCode(block, 'ADD0',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var code = 'tostring(' + element + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
} else if (block.itemCount_ == 2) {
|
||||
var element0 = Blockly.Lua.valueToCode(block, 'ADD0',
|
||||
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
|
||||
var element1 = Blockly.Lua.valueToCode(block, 'ADD1',
|
||||
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
|
||||
var code = element0 + ' .. ' + element1;
|
||||
return [code, Blockly.Lua.ORDER_CONCATENATION];
|
||||
} else {
|
||||
var elements = [];
|
||||
for (var i = 0; i < block.itemCount_; i++) {
|
||||
elements[i] = Blockly.Lua.valueToCode(block, 'ADD' + i,
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var code = 'table.concat({' + elements.join(', ') + '})';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Lua['text_append'] = function(block) {
|
||||
// Append to a variable in place.
|
||||
var varName = Blockly.Lua.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var value = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||
Blockly.Lua.ORDER_CONCATENATION) || '\'\'';
|
||||
return varName + ' = ' + varName + ' .. ' + value + '\n';
|
||||
};
|
||||
|
||||
Blockly.Lua['text_length'] = function(block) {
|
||||
// String or array length.
|
||||
var text = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_UNARY) || '\'\'';
|
||||
return ['#' + text, Blockly.Lua.ORDER_UNARY];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var text = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_UNARY) || '\'\'';
|
||||
return ['#' + text + ' == 0', Blockly.Lua.ORDER_RELATIONAL];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_indexOf'] = function(block) {
|
||||
// Search the text for a substring.
|
||||
var substring = Blockly.Lua.valueToCode(block, 'FIND',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var text = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
if (block.getFieldValue('END') == 'FIRST') {
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'firstIndexOf',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(str, substr) ',
|
||||
' local i = string.find(str, substr, 1, true)',
|
||||
' if i == nil then',
|
||||
' return 0',
|
||||
' else',
|
||||
' return i',
|
||||
' end',
|
||||
'end']);
|
||||
} else {
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'lastIndexOf',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(str, substr)',
|
||||
' local i = string.find(string.reverse(str), ' +
|
||||
'string.reverse(substr), 1, true)',
|
||||
' if i then',
|
||||
' return #str + 2 - i - #substr',
|
||||
' end',
|
||||
' return 0',
|
||||
'end']);
|
||||
}
|
||||
var code = functionName + '(' + text + ', ' + substring + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['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 atOrder = (where == 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
|
||||
Blockly.Lua.ORDER_NONE;
|
||||
var at = Blockly.Lua.valueToCode(block, 'AT', atOrder) || '1';
|
||||
var text = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var code;
|
||||
if (where == 'RANDOM') {
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'text_random_letter',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(str)',
|
||||
' local index = math.random(string.len(str))',
|
||||
' return string.sub(str, index, index)',
|
||||
'end']);
|
||||
code = functionName + '(' + text + ')';
|
||||
} else {
|
||||
if (where == 'FIRST') {
|
||||
var start = '1';
|
||||
} else if (where == 'LAST') {
|
||||
var start = '-1';
|
||||
} else {
|
||||
if (where == 'FROM_START') {
|
||||
var start = at;
|
||||
} else if (where == 'FROM_END') {
|
||||
var start = '-' + at;
|
||||
} else {
|
||||
throw 'Unhandled option (text_charAt).';
|
||||
}
|
||||
}
|
||||
if (start.match(/^-?\w*$/)) {
|
||||
code = 'string.sub(' + text + ', ' + start + ', ' + start + ')';
|
||||
} else {
|
||||
// use function to avoid reevaluation
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'text_char_at',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(str, index)',
|
||||
' return string.sub(str, index, index)',
|
||||
'end']);
|
||||
code = functionName + '(' + text + ', ' + start + ')';
|
||||
}
|
||||
}
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_getSubstring'] = function(block) {
|
||||
// Get substring.
|
||||
var text = Blockly.Lua.valueToCode(block, 'STRING',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
|
||||
// Get start index.
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var at1Order = (where1 == 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
|
||||
Blockly.Lua.ORDER_NONE;
|
||||
var at1 = Blockly.Lua.valueToCode(block, 'AT1', at1Order) || '1';
|
||||
if (where1 == 'FIRST') {
|
||||
var start = 1;
|
||||
} else if (where1 == 'FROM_START') {
|
||||
var start = at1;
|
||||
} else if (where1 == 'FROM_END') {
|
||||
var start = '-' + at1;
|
||||
} else {
|
||||
throw 'Unhandled option (text_getSubstring)';
|
||||
}
|
||||
|
||||
// Get end index.
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
var at2Order = (where2 == 'FROM_END') ? Blockly.Lua.ORDER_UNARY :
|
||||
Blockly.Lua.ORDER_NONE;
|
||||
var at2 = Blockly.Lua.valueToCode(block, 'AT2', at2Order) || '1';
|
||||
if (where2 == 'LAST') {
|
||||
var end = -1;
|
||||
} else if (where2 == 'FROM_START') {
|
||||
var end = at2;
|
||||
} else if (where2 == 'FROM_END') {
|
||||
var end = '-' + at2;
|
||||
} else {
|
||||
throw 'Unhandled option (text_getSubstring)';
|
||||
}
|
||||
var code = 'string.sub(' + text + ', ' + start + ', ' + end + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_changeCase'] = function(block) {
|
||||
// Change capitalization.
|
||||
var operator = block.getFieldValue('CASE');
|
||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
if (operator == 'UPPERCASE') {
|
||||
var functionName = 'string.upper';
|
||||
} else if (operator == 'LOWERCASE') {
|
||||
var functionName = 'string.lower';
|
||||
} else if (operator == 'TITLECASE') {
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'text_titlecase',
|
||||
// There are shorter versions at
|
||||
// http://lua-users.org/wiki/SciteTitleCase
|
||||
// that do not preserve whitespace.
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(str)',
|
||||
' local buf = {}',
|
||||
' local inWord = false',
|
||||
' for i = 1, #str do',
|
||||
' local c = string.sub(str, i, i)',
|
||||
' if inWord then',
|
||||
' table.insert(buf, string.lower(c))',
|
||||
' if string.find(c, "%s") then',
|
||||
' inWord = false',
|
||||
' end',
|
||||
' else',
|
||||
' table.insert(buf, string.upper(c))',
|
||||
' inWord = true',
|
||||
' end',
|
||||
' end',
|
||||
' return table.concat(buf)',
|
||||
'end']);
|
||||
}
|
||||
var code = functionName + '(' + text + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_trim'] = function(block) {
|
||||
// Trim spaces.
|
||||
var OPERATORS = {
|
||||
LEFT: '^%s*(,-)',
|
||||
RIGHT: '(.-)%s*$',
|
||||
BOTH: '^%s*(.-)%s*$'
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var code = 'string.gsub(' + text + ', "' + operator + '", "%1")';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_print'] = function(block) {
|
||||
// Print statement.
|
||||
var msg = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
return 'print(' + msg + ')\n';
|
||||
};
|
||||
|
||||
Blockly.Lua['text_prompt_ext'] = function(block) {
|
||||
// Prompt function.
|
||||
if (block.getField('TEXT')) {
|
||||
// Internal message.
|
||||
var msg = Blockly.Lua.quote_(block.getFieldValue('TEXT'));
|
||||
} else {
|
||||
// External message.
|
||||
var msg = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'text_prompt',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_ + '(msg)',
|
||||
' io.write(msg)',
|
||||
' io.flush()',
|
||||
' return io.read()',
|
||||
'end']);
|
||||
var code = functionName + '(' + msg + ')';
|
||||
|
||||
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
|
||||
if (toNumber) {
|
||||
code = 'tonumber(' + code + ', 10)';
|
||||
}
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_prompt'] = Blockly.Lua['text_prompt_ext'];
|
||||
|
||||
Blockly.Lua['text_count'] = function(block) {
|
||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var sub = Blockly.Lua.valueToCode(block, 'SUB',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'text_count',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_
|
||||
+ '(haystack, needle)',
|
||||
' if #needle == 0 then',
|
||||
' return #haystack + 1',
|
||||
' end',
|
||||
' local i = 1',
|
||||
' local count = 0',
|
||||
' while true do',
|
||||
' i = string.find(haystack, needle, i, true)',
|
||||
' if i == nil then',
|
||||
' break',
|
||||
' end',
|
||||
' count = count + 1',
|
||||
' i = i + #needle',
|
||||
' end',
|
||||
' return count',
|
||||
'end',
|
||||
]);
|
||||
var code = functionName + '(' + text + ', ' + sub + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_replace'] = function(block) {
|
||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var from = Blockly.Lua.valueToCode(block, 'FROM',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var to = Blockly.Lua.valueToCode(block, 'TO',
|
||||
Blockly.Lua.ORDER_NONE) || '\'\'';
|
||||
var functionName = Blockly.Lua.provideFunction_(
|
||||
'text_replace',
|
||||
['function ' + Blockly.Lua.FUNCTION_NAME_PLACEHOLDER_
|
||||
+ '(haystack, needle, replacement)',
|
||||
' local buf = {}',
|
||||
' local i = 1',
|
||||
' while i <= #haystack do',
|
||||
' if string.sub(haystack, i, i + #needle - 1) == needle then',
|
||||
' for j = 1, #replacement do',
|
||||
' table.insert(buf, string.sub(replacement, j, j))',
|
||||
' end',
|
||||
' i = i + #needle',
|
||||
' else',
|
||||
' table.insert(buf, string.sub(haystack, i, i))',
|
||||
' i = i + 1',
|
||||
' end',
|
||||
' end',
|
||||
' return table.concat(buf)',
|
||||
'end',
|
||||
]);
|
||||
var code = functionName + '(' + text + ', ' + from + ', ' + to + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
|
||||
Blockly.Lua['text_reverse'] = function(block) {
|
||||
var text = Blockly.Lua.valueToCode(block, 'TEXT',
|
||||
Blockly.Lua.ORDER_HIGH) || '\'\'';
|
||||
var code = 'string.reverse(' + text + ')';
|
||||
return [code, Blockly.Lua.ORDER_HIGH];
|
||||
};
|
||||
46
node_modules/node-blockly/blockly/generators/lua/variables.js
generated
vendored
Normal file
46
node_modules/node-blockly/blockly/generators/lua/variables.js
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2016 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 Lua for variable blocks.
|
||||
* @author rodrigoq@google.com (Rodrigo Queiro)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.variables');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
|
||||
|
||||
Blockly.Lua['variables_get'] = function(block) {
|
||||
// Variable getter.
|
||||
var code = Blockly.Lua.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return [code, Blockly.Lua.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Lua['variables_set'] = function(block) {
|
||||
// Variable setter.
|
||||
var argument0 = Blockly.Lua.valueToCode(block, 'VALUE',
|
||||
Blockly.Lua.ORDER_NONE) || '0';
|
||||
var varName = Blockly.Lua.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = ' + argument0 + '\n';
|
||||
};
|
||||
35
node_modules/node-blockly/blockly/generators/lua/variables_dynamic.js
generated
vendored
Normal file
35
node_modules/node-blockly/blockly/generators/lua/variables_dynamic.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2018 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 Lua for dynamic variable blocks.
|
||||
* @author fenichel@google.com (Rachel Fenichel)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Lua.variablesDynamic');
|
||||
|
||||
goog.require('Blockly.Lua');
|
||||
goog.require('Blockly.Lua.variables');
|
||||
|
||||
|
||||
// Lua is dynamically typed.
|
||||
Blockly.Lua['variables_get_dynamic'] = Blockly.Lua['variables_get'];
|
||||
Blockly.Lua['variables_set_dynamic'] = Blockly.Lua['variables_set'];
|
||||
314
node_modules/node-blockly/blockly/generators/php.js
generated
vendored
Normal file
314
node_modules/node-blockly/blockly/generators/php.js
generated
vendored
Normal file
@@ -0,0 +1,314 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2015 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 PHP for blocks.
|
||||
* @author daarond@gmail.com (Daaron Dwyer)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP');
|
||||
|
||||
goog.require('Blockly.Generator');
|
||||
|
||||
|
||||
/**
|
||||
* PHP code generator.
|
||||
* @type {!Blockly.Generator}
|
||||
*/
|
||||
Blockly.PHP = new Blockly.Generator('PHP');
|
||||
|
||||
/**
|
||||
* 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.PHP.addReservedWords(
|
||||
// http://php.net/manual/en/reserved.keywords.php
|
||||
'__halt_compiler,abstract,and,array,as,break,callable,case,catch,class,' +
|
||||
'clone,const,continue,declare,default,die,do,echo,else,elseif,empty,' +
|
||||
'enddeclare,endfor,endforeach,endif,endswitch,endwhile,eval,exit,extends,' +
|
||||
'final,for,foreach,function,global,goto,if,implements,include,' +
|
||||
'include_once,instanceof,insteadof,interface,isset,list,namespace,new,or,' +
|
||||
'print,private,protected,public,require,require_once,return,static,' +
|
||||
'switch,throw,trait,try,unset,use,var,while,xor,' +
|
||||
// http://php.net/manual/en/reserved.constants.php
|
||||
'PHP_VERSION,PHP_MAJOR_VERSION,PHP_MINOR_VERSION,PHP_RELEASE_VERSION,' +
|
||||
'PHP_VERSION_ID,PHP_EXTRA_VERSION,PHP_ZTS,PHP_DEBUG,PHP_MAXPATHLEN,' +
|
||||
'PHP_OS,PHP_SAPI,PHP_EOL,PHP_INT_MAX,PHP_INT_SIZE,DEFAULT_INCLUDE_PATH,' +
|
||||
'PEAR_INSTALL_DIR,PEAR_EXTENSION_DIR,PHP_EXTENSION_DIR,PHP_PREFIX,' +
|
||||
'PHP_BINDIR,PHP_BINARY,PHP_MANDIR,PHP_LIBDIR,PHP_DATADIR,PHP_SYSCONFDIR,' +
|
||||
'PHP_LOCALSTATEDIR,PHP_CONFIG_FILE_PATH,PHP_CONFIG_FILE_SCAN_DIR,' +
|
||||
'PHP_SHLIB_SUFFIX,E_ERROR,E_WARNING,E_PARSE,E_NOTICE,E_CORE_ERROR,' +
|
||||
'E_CORE_WARNING,E_COMPILE_ERROR,E_COMPILE_WARNING,E_USER_ERROR,' +
|
||||
'E_USER_WARNING,E_USER_NOTICE,E_DEPRECATED,E_USER_DEPRECATED,E_ALL,' +
|
||||
'E_STRICT,__COMPILER_HALT_OFFSET__,TRUE,FALSE,NULL,__CLASS__,__DIR__,' +
|
||||
'__FILE__,__FUNCTION__,__LINE__,__METHOD__,__NAMESPACE__,__TRAIT__'
|
||||
);
|
||||
|
||||
/**
|
||||
* Order of operation ENUMs.
|
||||
* http://php.net/manual/en/language.operators.precedence.php
|
||||
*/
|
||||
Blockly.PHP.ORDER_ATOMIC = 0; // 0 "" ...
|
||||
Blockly.PHP.ORDER_CLONE = 1; // clone
|
||||
Blockly.PHP.ORDER_NEW = 1; // new
|
||||
Blockly.PHP.ORDER_MEMBER = 2.1; // []
|
||||
Blockly.PHP.ORDER_FUNCTION_CALL = 2.2; // ()
|
||||
Blockly.PHP.ORDER_POWER = 3; // **
|
||||
Blockly.PHP.ORDER_INCREMENT = 4; // ++
|
||||
Blockly.PHP.ORDER_DECREMENT = 4; // --
|
||||
Blockly.PHP.ORDER_BITWISE_NOT = 4; // ~
|
||||
Blockly.PHP.ORDER_CAST = 4; // (int) (float) (string) (array) ...
|
||||
Blockly.PHP.ORDER_SUPPRESS_ERROR = 4; // @
|
||||
Blockly.PHP.ORDER_INSTANCEOF = 5; // instanceof
|
||||
Blockly.PHP.ORDER_LOGICAL_NOT = 6; // !
|
||||
Blockly.PHP.ORDER_UNARY_PLUS = 7.1; // +
|
||||
Blockly.PHP.ORDER_UNARY_NEGATION = 7.2; // -
|
||||
Blockly.PHP.ORDER_MULTIPLICATION = 8.1; // *
|
||||
Blockly.PHP.ORDER_DIVISION = 8.2; // /
|
||||
Blockly.PHP.ORDER_MODULUS = 8.3; // %
|
||||
Blockly.PHP.ORDER_ADDITION = 9.1; // +
|
||||
Blockly.PHP.ORDER_SUBTRACTION = 9.2; // -
|
||||
Blockly.PHP.ORDER_STRING_CONCAT = 9.3; // .
|
||||
Blockly.PHP.ORDER_BITWISE_SHIFT = 10; // << >>
|
||||
Blockly.PHP.ORDER_RELATIONAL = 11; // < <= > >=
|
||||
Blockly.PHP.ORDER_EQUALITY = 12; // == != === !== <> <=>
|
||||
Blockly.PHP.ORDER_REFERENCE = 13; // &
|
||||
Blockly.PHP.ORDER_BITWISE_AND = 13; // &
|
||||
Blockly.PHP.ORDER_BITWISE_XOR = 14; // ^
|
||||
Blockly.PHP.ORDER_BITWISE_OR = 15; // |
|
||||
Blockly.PHP.ORDER_LOGICAL_AND = 16; // &&
|
||||
Blockly.PHP.ORDER_LOGICAL_OR = 17; // ||
|
||||
Blockly.PHP.ORDER_IF_NULL = 18; // ??
|
||||
Blockly.PHP.ORDER_CONDITIONAL = 19; // ?:
|
||||
Blockly.PHP.ORDER_ASSIGNMENT = 20; // = += -= *= /= %= <<= >>= ...
|
||||
Blockly.PHP.ORDER_LOGICAL_AND_WEAK = 21; // and
|
||||
Blockly.PHP.ORDER_LOGICAL_XOR = 22; // xor
|
||||
Blockly.PHP.ORDER_LOGICAL_OR_WEAK = 23; // or
|
||||
Blockly.PHP.ORDER_COMMA = 24; // ,
|
||||
Blockly.PHP.ORDER_NONE = 99; // (...)
|
||||
|
||||
/**
|
||||
* List of outer-inner pairings that do NOT require parentheses.
|
||||
* @type {!Array.<!Array.<number>>}
|
||||
*/
|
||||
Blockly.PHP.ORDER_OVERRIDES = [
|
||||
// (foo()).bar() -> foo().bar()
|
||||
// (foo())[0] -> foo()[0]
|
||||
[Blockly.PHP.ORDER_MEMBER, Blockly.PHP.ORDER_FUNCTION_CALL],
|
||||
// (foo[0])[1] -> foo[0][1]
|
||||
// (foo.bar).baz -> foo.bar.baz
|
||||
[Blockly.PHP.ORDER_MEMBER, Blockly.PHP.ORDER_MEMBER],
|
||||
// !(!foo) -> !!foo
|
||||
[Blockly.PHP.ORDER_LOGICAL_NOT, Blockly.PHP.ORDER_LOGICAL_NOT],
|
||||
// a * (b * c) -> a * b * c
|
||||
[Blockly.PHP.ORDER_MULTIPLICATION, Blockly.PHP.ORDER_MULTIPLICATION],
|
||||
// a + (b + c) -> a + b + c
|
||||
[Blockly.PHP.ORDER_ADDITION, Blockly.PHP.ORDER_ADDITION],
|
||||
// a && (b && c) -> a && b && c
|
||||
[Blockly.PHP.ORDER_LOGICAL_AND, Blockly.PHP.ORDER_LOGICAL_AND],
|
||||
// a || (b || c) -> a || b || c
|
||||
[Blockly.PHP.ORDER_LOGICAL_OR, Blockly.PHP.ORDER_LOGICAL_OR]
|
||||
];
|
||||
|
||||
/**
|
||||
* Initialise the database of variable names.
|
||||
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
|
||||
*/
|
||||
Blockly.PHP.init = function(workspace) {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly.PHP.definitions_ = Object.create(null);
|
||||
// Create a dictionary mapping desired function names in definitions_
|
||||
// to actual function names (to avoid collisions with user functions).
|
||||
Blockly.PHP.functionNames_ = Object.create(null);
|
||||
|
||||
if (!Blockly.PHP.variableDB_) {
|
||||
Blockly.PHP.variableDB_ =
|
||||
new Blockly.Names(Blockly.PHP.RESERVED_WORDS_, '$');
|
||||
} else {
|
||||
Blockly.PHP.variableDB_.reset();
|
||||
}
|
||||
|
||||
Blockly.PHP.variableDB_.setVariableMap(workspace.getVariableMap());
|
||||
|
||||
var defvars = [];
|
||||
// Add developer variables (not created or named by the user).
|
||||
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
for (var i = 0; i < devVarList.length; i++) {
|
||||
defvars.push(Blockly.PHP.variableDB_.getName(devVarList[i],
|
||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ';');
|
||||
}
|
||||
|
||||
// Add user variables, but only ones that are being used.
|
||||
var variables = Blockly.Variables.allUsedVarModels(workspace);
|
||||
for (var i = 0, variable; variable = variables[i]; i++) {
|
||||
defvars.push(Blockly.PHP.variableDB_.getName(variable.getId(),
|
||||
Blockly.Variables.NAME_TYPE) + ';');
|
||||
}
|
||||
|
||||
// Declare all of the variables.
|
||||
Blockly.PHP.definitions_['variables'] = defvars.join('\n');
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepend the generated code with the variable definitions.
|
||||
* @param {string} code Generated code.
|
||||
* @return {string} Completed code.
|
||||
*/
|
||||
Blockly.PHP.finish = function(code) {
|
||||
// Convert the definitions dictionary into a list.
|
||||
var definitions = [];
|
||||
for (var name in Blockly.PHP.definitions_) {
|
||||
definitions.push(Blockly.PHP.definitions_[name]);
|
||||
}
|
||||
// Clean up temporary data.
|
||||
delete Blockly.PHP.definitions_;
|
||||
delete Blockly.PHP.functionNames_;
|
||||
Blockly.PHP.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.PHP.scrubNakedValue = function(line) {
|
||||
return line + ';\n';
|
||||
};
|
||||
|
||||
/**
|
||||
* Encode a string as a properly escaped PHP string, complete with
|
||||
* quotes.
|
||||
* @param {string} string Text to encode.
|
||||
* @return {string} PHP string.
|
||||
* @private
|
||||
*/
|
||||
Blockly.PHP.quote_ = function(string) {
|
||||
string = string.replace(/\\/g, '\\\\')
|
||||
.replace(/\n/g, '\\\n')
|
||||
.replace(/'/g, '\\\'');
|
||||
return '\'' + string + '\'';
|
||||
};
|
||||
|
||||
/**
|
||||
* Common tasks for generating PHP 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 PHP code created for this block.
|
||||
* @return {string} PHP code with comments and subsequent blocks added.
|
||||
* @private
|
||||
*/
|
||||
Blockly.PHP.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.PHP.COMMENT_WRAP - 3);
|
||||
if (comment) {
|
||||
commentCode += Blockly.PHP.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.PHP.allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += Blockly.PHP.prefixLines(comment, '// ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var nextBlock = block.nextConnection && block.nextConnection.targetBlock();
|
||||
var nextCode = Blockly.PHP.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.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate,
|
||||
opt_order) {
|
||||
var delta = opt_delta || 0;
|
||||
var order = opt_order || Blockly.PHP.ORDER_NONE;
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
delta--;
|
||||
}
|
||||
var defaultAtIndex = block.workspace.options.oneBasedIndex ? '1' : '0';
|
||||
if (delta > 0) {
|
||||
var at = Blockly.PHP.valueToCode(block, atId,
|
||||
Blockly.PHP.ORDER_ADDITION) || defaultAtIndex;
|
||||
} else if (delta < 0) {
|
||||
var at = Blockly.PHP.valueToCode(block, atId,
|
||||
Blockly.PHP.ORDER_SUBTRACTION) || defaultAtIndex;
|
||||
} else if (opt_negate) {
|
||||
var at = Blockly.PHP.valueToCode(block, atId,
|
||||
Blockly.PHP.ORDER_UNARY_NEGATION) || defaultAtIndex;
|
||||
} else {
|
||||
var at = Blockly.PHP.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.PHP.ORDER_ADDITION;
|
||||
} else if (delta < 0) {
|
||||
at = at + ' - ' + -delta;
|
||||
var innerOrder = Blockly.PHP.ORDER_SUBTRACTION;
|
||||
}
|
||||
if (opt_negate) {
|
||||
if (delta) {
|
||||
at = '-(' + at + ')';
|
||||
} else {
|
||||
at = '-' + at;
|
||||
}
|
||||
var innerOrder = Blockly.PHP.ORDER_UNARY_NEGATION;
|
||||
}
|
||||
innerOrder = Math.floor(innerOrder);
|
||||
order = Math.floor(order);
|
||||
if (innerOrder && order >= innerOrder) {
|
||||
at = '(' + at + ')';
|
||||
}
|
||||
}
|
||||
return at;
|
||||
};
|
||||
105
node_modules/node-blockly/blockly/generators/php/colour.js
generated
vendored
Normal file
105
node_modules/node-blockly/blockly/generators/php/colour.js
generated
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2015 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 PHP for colour blocks.
|
||||
* @author daarond@gmail.com (Daaron Dwyer)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP.colour');
|
||||
|
||||
goog.require('Blockly.PHP');
|
||||
|
||||
|
||||
Blockly.PHP['colour_picker'] = function(block) {
|
||||
// Colour picker.
|
||||
var code = '\'' + block.getFieldValue('COLOUR') + '\'';
|
||||
return [code, Blockly.PHP.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.PHP['colour_random'] = function(block) {
|
||||
// Generate a random colour.
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'colour_random',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '() {',
|
||||
' return \'#\' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), ' +
|
||||
'6, \'0\', STR_PAD_LEFT);',
|
||||
'}']);
|
||||
var code = functionName + '()';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['colour_rgb'] = function(block) {
|
||||
// Compose a colour from RGB components expressed as percentages.
|
||||
var red = Blockly.PHP.valueToCode(block, 'RED',
|
||||
Blockly.PHP.ORDER_COMMA) || 0;
|
||||
var green = Blockly.PHP.valueToCode(block, 'GREEN',
|
||||
Blockly.PHP.ORDER_COMMA) || 0;
|
||||
var blue = Blockly.PHP.valueToCode(block, 'BLUE',
|
||||
Blockly.PHP.ORDER_COMMA) || 0;
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'colour_rgb',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($r, $g, $b) {',
|
||||
' $r = round(max(min($r, 100), 0) * 2.55);',
|
||||
' $g = round(max(min($g, 100), 0) * 2.55);',
|
||||
' $b = round(max(min($b, 100), 0) * 2.55);',
|
||||
' $hex = \'#\';',
|
||||
' $hex .= str_pad(dechex($r), 2, \'0\', STR_PAD_LEFT);',
|
||||
' $hex .= str_pad(dechex($g), 2, \'0\', STR_PAD_LEFT);',
|
||||
' $hex .= str_pad(dechex($b), 2, \'0\', STR_PAD_LEFT);',
|
||||
' return $hex;',
|
||||
'}']);
|
||||
var code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['colour_blend'] = function(block) {
|
||||
// Blend two colours together.
|
||||
var c1 = Blockly.PHP.valueToCode(block, 'COLOUR1',
|
||||
Blockly.PHP.ORDER_COMMA) || '\'#000000\'';
|
||||
var c2 = Blockly.PHP.valueToCode(block, 'COLOUR2',
|
||||
Blockly.PHP.ORDER_COMMA) || '\'#000000\'';
|
||||
var ratio = Blockly.PHP.valueToCode(block, 'RATIO',
|
||||
Blockly.PHP.ORDER_COMMA) || 0.5;
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'colour_blend',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($c1, $c2, $ratio) {',
|
||||
' $ratio = max(min($ratio, 1), 0);',
|
||||
' $r1 = hexdec(substr($c1, 1, 2));',
|
||||
' $g1 = hexdec(substr($c1, 3, 2));',
|
||||
' $b1 = hexdec(substr($c1, 5, 2));',
|
||||
' $r2 = hexdec(substr($c2, 1, 2));',
|
||||
' $g2 = hexdec(substr($c2, 3, 2));',
|
||||
' $b2 = hexdec(substr($c2, 5, 2));',
|
||||
' $r = round($r1 * (1 - $ratio) + $r2 * $ratio);',
|
||||
' $g = round($g1 * (1 - $ratio) + $g2 * $ratio);',
|
||||
' $b = round($b1 * (1 - $ratio) + $b2 * $ratio);',
|
||||
' $hex = \'#\';',
|
||||
' $hex .= str_pad(dechex($r), 2, \'0\', STR_PAD_LEFT);',
|
||||
' $hex .= str_pad(dechex($g), 2, \'0\', STR_PAD_LEFT);',
|
||||
' $hex .= str_pad(dechex($b), 2, \'0\', STR_PAD_LEFT);',
|
||||
' return $hex;',
|
||||
'}']);
|
||||
var code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
512
node_modules/node-blockly/blockly/generators/php/lists.js
generated
vendored
Normal file
512
node_modules/node-blockly/blockly/generators/php/lists.js
generated
vendored
Normal file
@@ -0,0 +1,512 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2015 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 PHP for list blocks.
|
||||
* @author daarond@gmail.com (Daaron Dwyer)
|
||||
*/
|
||||
|
||||
/*
|
||||
* Lists in PHP are known to break when non-variables are passed into blocks
|
||||
* that require a list. PHP, unlike other languages, passes arrays as reference
|
||||
* value instead of value so we are unable to support it to the extent we can
|
||||
* for the other languages.
|
||||
* For example, a ternary operator with two arrays will return the array by
|
||||
* value and that cannot be passed into any of the built-in array functions for
|
||||
* PHP (because only variables can be passed by reference).
|
||||
* ex: end(true ? list1 : list2)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP.lists');
|
||||
|
||||
goog.require('Blockly.PHP');
|
||||
|
||||
|
||||
Blockly.PHP['lists_create_empty'] = function(block) {
|
||||
// Create an empty list.
|
||||
return ['array()', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_create_with'] = function(block) {
|
||||
// Create a list with any number of elements of any type.
|
||||
var code = new Array(block.itemCount_);
|
||||
for (var i = 0; i < block.itemCount_; i++) {
|
||||
code[i] = Blockly.PHP.valueToCode(block, 'ADD' + i,
|
||||
Blockly.PHP.ORDER_COMMA) || 'null';
|
||||
}
|
||||
code = 'array(' + code.join(', ') + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_repeat'] = function(block) {
|
||||
// Create a list with one element repeated.
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lists_repeat',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($value, $count) {',
|
||||
' $array = array();',
|
||||
' for ($index = 0; $index < $count; $index++) {',
|
||||
' $array[] = $value;',
|
||||
' }',
|
||||
' return $array;',
|
||||
'}']);
|
||||
var element = Blockly.PHP.valueToCode(block, 'ITEM',
|
||||
Blockly.PHP.ORDER_COMMA) || 'null';
|
||||
var repeatCount = Blockly.PHP.valueToCode(block, 'NUM',
|
||||
Blockly.PHP.ORDER_COMMA) || '0';
|
||||
var code = functionName + '(' + element + ', ' + repeatCount + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_length'] = function(block) {
|
||||
// String or array length.
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'length',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {',
|
||||
' if (is_string($value)) {',
|
||||
' return strlen($value);',
|
||||
' } else {',
|
||||
' return count($value);',
|
||||
' }',
|
||||
'}']);
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
return [functionName + '(' + list + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
|
||||
return ['empty(' + argument0 + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_indexOf'] = function(block) {
|
||||
// Find an item in the list.
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'FIND',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var argument1 = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_MEMBER) || '[]';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
var errorIndex = ' 0';
|
||||
var indexAdjustment = ' + 1';
|
||||
} else {
|
||||
var errorIndex = ' -1';
|
||||
var indexAdjustment = '';
|
||||
}
|
||||
if (block.getFieldValue('END') == 'FIRST') {
|
||||
// indexOf
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'indexOf',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($haystack, $needle) {',
|
||||
' for ($index = 0; $index < count($haystack); $index++) {',
|
||||
' if ($haystack[$index] == $needle) return $index' +
|
||||
indexAdjustment + ';',
|
||||
' }',
|
||||
' return ' + errorIndex + ';',
|
||||
'}']);
|
||||
} else {
|
||||
// lastIndexOf
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lastIndexOf',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($haystack, $needle) {',
|
||||
' $last = ' + errorIndex + ';',
|
||||
' for ($index = 0; $index < count($haystack); $index++) {',
|
||||
' if ($haystack[$index] == $needle) $last = $index' +
|
||||
indexAdjustment + ';',
|
||||
' }',
|
||||
' return $last;',
|
||||
'}']);
|
||||
}
|
||||
|
||||
var code = functionName + '(' + argument1 + ', ' + argument0 + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_getIndex'] = function(block) {
|
||||
// Get element at index.
|
||||
var mode = block.getFieldValue('MODE') || 'GET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
if (mode == 'GET') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_MEMBER) || 'array()';
|
||||
var code = list + '[0]';
|
||||
return [code, Blockly.PHP.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || 'array()';
|
||||
var code = 'array_shift(' + list + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || 'array()';
|
||||
return 'array_shift(' + list + ');\n';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
if (mode == 'GET') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || 'array()';
|
||||
var code = 'end(' + list + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || 'array()';
|
||||
var code = 'array_pop(' + list + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || 'array()';
|
||||
return 'array_pop(' + list + ');\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_START':
|
||||
var at = Blockly.PHP.getAdjusted(block, 'AT');
|
||||
if (mode == 'GET') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_MEMBER) || 'array()';
|
||||
var code = list + '[' + at + ']';
|
||||
return [code, Blockly.PHP.ORDER_MEMBER];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_COMMA) || 'array()';
|
||||
var code = 'array_splice(' + list + ', ' + at + ', 1)[0]';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_COMMA) || 'array()';
|
||||
return 'array_splice(' + list + ', ' + at + ', 1);\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_END':
|
||||
if (mode == 'GET') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_COMMA) || 'array()';
|
||||
var at = Blockly.PHP.getAdjusted(block, 'AT', 1, true);
|
||||
var code = 'array_slice(' + list + ', ' + at + ', 1)[0]';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'GET_REMOVE' || mode == 'REMOVE') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || 'array()';
|
||||
var at = Blockly.PHP.getAdjusted(block, 'AT', 1, false,
|
||||
Blockly.PHP.ORDER_SUBTRACTION);
|
||||
code = 'array_splice(' + list +
|
||||
', count(' + list + ') - ' + at + ', 1)[0]';
|
||||
if (mode == 'GET_REMOVE') {
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
return code + ';\n';
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'RANDOM':
|
||||
var list = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || 'array()';
|
||||
if (mode == 'GET') {
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lists_get_random_item',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($list) {',
|
||||
' return $list[rand(0,count($list)-1)];',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'GET_REMOVE') {
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lists_get_remove_random_item',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(&$list) {',
|
||||
' $x = rand(0,count($list)-1);',
|
||||
' unset($list[$x]);',
|
||||
' return array_values($list);',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
} else if (mode == 'REMOVE') {
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lists_remove_random_item',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(&$list) {',
|
||||
' unset($list[rand(0,count($list)-1)]);',
|
||||
'}']);
|
||||
return functionName + '(' + list + ');\n';
|
||||
}
|
||||
break;
|
||||
}
|
||||
throw 'Unhandled combination (lists_getIndex).';
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_setIndex'] = function(block) {
|
||||
// Set element at index.
|
||||
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
||||
var mode = block.getFieldValue('MODE') || 'GET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var value = Blockly.PHP.valueToCode(block, 'TO',
|
||||
Blockly.PHP.ORDER_ASSIGNMENT) || 'null';
|
||||
// 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.PHP.variableDB_.getDistinctName(
|
||||
'tmp_list', Blockly.Variables.NAME_TYPE);
|
||||
var code = listVar + ' = &' + list + ';\n';
|
||||
list = listVar;
|
||||
return code;
|
||||
}
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
if (mode == 'SET') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_MEMBER) || 'array()';
|
||||
return list + '[0] = ' + value + ';\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_COMMA) || 'array()';
|
||||
return 'array_unshift(' + list + ', ' + value + ');\n';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_COMMA) || 'array()';
|
||||
if (mode == 'SET') {
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lists_set_last_item',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(&$list, $value) {',
|
||||
' $list[count($list) - 1] = $value;',
|
||||
'}']);
|
||||
return functionName + '(' + list + ', ' + value + ');\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
return 'array_push(' + list + ', ' + value + ');\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_START':
|
||||
var at = Blockly.PHP.getAdjusted(block, 'AT');
|
||||
if (mode == 'SET') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_MEMBER) || 'array()';
|
||||
return list + '[' + at + '] = ' + value + ';\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_COMMA) || 'array()';
|
||||
return 'array_splice(' + list + ', ' + at + ', 0, ' + value + ');\n';
|
||||
}
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_COMMA) || 'array()';
|
||||
var at = Blockly.PHP.getAdjusted(block, 'AT', 1);
|
||||
if (mode == 'SET') {
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lists_set_from_end',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(&$list, $at, $value) {',
|
||||
' $list[count($list) - $at] = $value;',
|
||||
'}']);
|
||||
return functionName + '(' + list + ', ' + at + ', ' + value + ');\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lists_insert_from_end',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(&$list, $at, $value) {',
|
||||
' return array_splice($list, count($list) - $at, 0, $value);',
|
||||
'}']);
|
||||
return functionName + '(' + list + ', ' + at + ', ' + value + ');\n';
|
||||
}
|
||||
break;
|
||||
case 'RANDOM':
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_REFERENCE) || 'array()';
|
||||
var code = cacheList();
|
||||
var xVar = Blockly.PHP.variableDB_.getDistinctName(
|
||||
'tmp_x', Blockly.Variables.NAME_TYPE);
|
||||
code += xVar + ' = rand(0, count(' + list + ')-1);\n';
|
||||
if (mode == 'SET') {
|
||||
code += list + '[' + xVar + '] = ' + value + ';\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
code += 'array_splice(' + list + ', ' + xVar + ', 0, ' + value +
|
||||
');\n';
|
||||
return code;
|
||||
}
|
||||
break;
|
||||
}
|
||||
throw 'Unhandled combination (lists_setIndex).';
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_getSublist'] = function(block) {
|
||||
// Get sublist.
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_COMMA) || 'array()';
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
if (where1 == 'FIRST' && where2 == 'LAST') {
|
||||
var code = list;
|
||||
} else if (list.match(/^\$\w+$/) ||
|
||||
(where1 != 'FROM_END' && where2 == 'FROM_START')) {
|
||||
// If the list is a simple value or doesn't require a call for length, don't
|
||||
// generate a helper function.
|
||||
switch (where1) {
|
||||
case 'FROM_START':
|
||||
var at1 = Blockly.PHP.getAdjusted(block, 'AT1');
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at1 = Blockly.PHP.getAdjusted(block, 'AT1', 1, false,
|
||||
Blockly.PHP.ORDER_SUBTRACTION);
|
||||
at1 = 'count(' + list + ') - ' + at1;
|
||||
break;
|
||||
case 'FIRST':
|
||||
var at1 = '0';
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (lists_getSublist).';
|
||||
}
|
||||
switch (where2) {
|
||||
case 'FROM_START':
|
||||
var at2 = Blockly.PHP.getAdjusted(block, 'AT2', 0, false,
|
||||
Blockly.PHP.ORDER_SUBTRACTION);
|
||||
var length = at2 + ' - ';
|
||||
if (Blockly.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) {
|
||||
length += at1;
|
||||
} else {
|
||||
length += '(' + at1 + ')';
|
||||
}
|
||||
length += ' + 1';
|
||||
break;
|
||||
case 'FROM_END':
|
||||
var at2 = Blockly.PHP.getAdjusted(block, 'AT2', 0, false,
|
||||
Blockly.PHP.ORDER_SUBTRACTION);
|
||||
var length = 'count(' + list + ') - ' + at2 + ' - ';
|
||||
if (Blockly.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) {
|
||||
length += at1;
|
||||
} else {
|
||||
length += '(' + at1 + ')';
|
||||
}
|
||||
break;
|
||||
case 'LAST':
|
||||
var length = 'count(' + list + ') - ';
|
||||
if (Blockly.isNumber(String(at1)) || String(at1).match(/^\(.+\)$/)) {
|
||||
length += at1;
|
||||
} else {
|
||||
length += '(' + at1 + ')';
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw 'Unhandled option (lists_getSublist).';
|
||||
}
|
||||
code = 'array_slice(' + list + ', ' + at1 + ', ' + length + ')';
|
||||
} else {
|
||||
var at1 = Blockly.PHP.getAdjusted(block, 'AT1');
|
||||
var at2 = Blockly.PHP.getAdjusted(block, 'AT2');
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lists_get_sublist',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($list, $where1, $at1, $where2, $at2) {',
|
||||
' if ($where1 == \'FROM_END\') {',
|
||||
' $at1 = count($list) - 1 - $at1;',
|
||||
' } else if ($where1 == \'FIRST\') {',
|
||||
' $at1 = 0;',
|
||||
' } else if ($where1 != \'FROM_START\'){',
|
||||
' throw new Exception(\'Unhandled option (lists_get_sublist).\');',
|
||||
' }',
|
||||
' $length = 0;',
|
||||
' if ($where2 == \'FROM_START\') {',
|
||||
' $length = $at2 - $at1 + 1;',
|
||||
' } else if ($where2 == \'FROM_END\') {',
|
||||
' $length = count($list) - $at1 - $at2;',
|
||||
' } else if ($where2 == \'LAST\') {',
|
||||
' $length = count($list) - $at1;',
|
||||
' } else {',
|
||||
' throw new Exception(\'Unhandled option (lists_get_sublist).\');',
|
||||
' }',
|
||||
' return array_slice($list, $at1, $length);',
|
||||
'}']);
|
||||
var code = functionName + '(' + list + ', \'' +
|
||||
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
|
||||
}
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_sort'] = function(block) {
|
||||
// Block for sorting a list.
|
||||
var listCode = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_COMMA) || 'array()';
|
||||
var direction = block.getFieldValue('DIRECTION') === '1' ? 1 : -1;
|
||||
var type = block.getFieldValue('TYPE');
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'lists_sort',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($list, $type, $direction) {',
|
||||
' $sortCmpFuncs = array(',
|
||||
' "NUMERIC" => "strnatcasecmp",',
|
||||
' "TEXT" => "strcmp",',
|
||||
' "IGNORE_CASE" => "strcasecmp"',
|
||||
' );',
|
||||
' $sortCmp = $sortCmpFuncs[$type];',
|
||||
' $list2 = $list;', // Clone list.
|
||||
' usort($list2, $sortCmp);',
|
||||
' if ($direction == -1) {',
|
||||
' $list2 = array_reverse($list2);',
|
||||
' }',
|
||||
' return $list2;',
|
||||
'}']);
|
||||
var sortCode = functionName +
|
||||
'(' + listCode + ', "' + type + '", ' + direction + ')';
|
||||
return [sortCode, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_split'] = function(block) {
|
||||
// Block for splitting text into a list, or joining a list into text.
|
||||
var value_input = Blockly.PHP.valueToCode(block, 'INPUT',
|
||||
Blockly.PHP.ORDER_COMMA);
|
||||
var value_delim = Blockly.PHP.valueToCode(block, 'DELIM',
|
||||
Blockly.PHP.ORDER_COMMA) || '\'\'';
|
||||
var mode = block.getFieldValue('MODE');
|
||||
if (mode == 'SPLIT') {
|
||||
if (!value_input) {
|
||||
value_input = '\'\'';
|
||||
}
|
||||
var functionName = 'explode';
|
||||
} else if (mode == 'JOIN') {
|
||||
if (!value_input) {
|
||||
value_input = 'array()';
|
||||
}
|
||||
var functionName = 'implode';
|
||||
} else {
|
||||
throw 'Unknown mode: ' + mode;
|
||||
}
|
||||
var code = functionName + '(' + value_delim + ', ' + value_input + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['lists_reverse'] = function(block) {
|
||||
// Block for reversing a list.
|
||||
var list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_COMMA) || '[]';
|
||||
var code = 'array_reverse(' + list + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
129
node_modules/node-blockly/blockly/generators/php/logic.js
generated
vendored
Normal file
129
node_modules/node-blockly/blockly/generators/php/logic.js
generated
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2015 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 PHP for logic blocks.
|
||||
* @author daarond@gmail.com (Daaron Dwyer)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP.logic');
|
||||
|
||||
goog.require('Blockly.PHP');
|
||||
|
||||
|
||||
Blockly.PHP['controls_if'] = function(block) {
|
||||
// If/elseif/else condition.
|
||||
var n = 0;
|
||||
var code = '', branchCode, conditionCode;
|
||||
do {
|
||||
conditionCode = Blockly.PHP.valueToCode(block, 'IF' + n,
|
||||
Blockly.PHP.ORDER_NONE) || 'false';
|
||||
branchCode = Blockly.PHP.statementToCode(block, 'DO' + n);
|
||||
code += (n > 0 ? ' else ' : '') +
|
||||
'if (' + conditionCode + ') {\n' + branchCode + '}';
|
||||
|
||||
++n;
|
||||
} while (block.getInput('IF' + n));
|
||||
|
||||
if (block.getInput('ELSE')) {
|
||||
branchCode = Blockly.PHP.statementToCode(block, 'ELSE');
|
||||
code += ' else {\n' + branchCode + '}';
|
||||
}
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
Blockly.PHP['controls_ifelse'] = Blockly.PHP['controls_if'];
|
||||
|
||||
Blockly.PHP['logic_compare'] = function(block) {
|
||||
// Comparison operator.
|
||||
var OPERATORS = {
|
||||
'EQ': '==',
|
||||
'NEQ': '!=',
|
||||
'LT': '<',
|
||||
'LTE': '<=',
|
||||
'GT': '>',
|
||||
'GTE': '>='
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('OP')];
|
||||
var order = (operator == '==' || operator == '!=') ?
|
||||
Blockly.PHP.ORDER_EQUALITY : Blockly.PHP.ORDER_RELATIONAL;
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.PHP.valueToCode(block, 'B', order) || '0';
|
||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.PHP['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
var operator = (block.getFieldValue('OP') == 'AND') ? '&&' : '||';
|
||||
var order = (operator == '&&') ? Blockly.PHP.ORDER_LOGICAL_AND :
|
||||
Blockly.PHP.ORDER_LOGICAL_OR;
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'A', order);
|
||||
var argument1 = Blockly.PHP.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 == '&&') ? 'true' : 'false';
|
||||
if (!argument0) {
|
||||
argument0 = defaultArgument;
|
||||
}
|
||||
if (!argument1) {
|
||||
argument1 = defaultArgument;
|
||||
}
|
||||
}
|
||||
var code = argument0 + ' ' + operator + ' ' + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.PHP['logic_negate'] = function(block) {
|
||||
// Negation.
|
||||
var order = Blockly.PHP.ORDER_LOGICAL_NOT;
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'BOOL', order) ||
|
||||
'true';
|
||||
var code = '!' + argument0;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.PHP['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
|
||||
return [code, Blockly.PHP.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.PHP['logic_null'] = function(block) {
|
||||
// Null data type.
|
||||
return ['null', Blockly.PHP.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.PHP['logic_ternary'] = function(block) {
|
||||
// Ternary operator.
|
||||
var value_if = Blockly.PHP.valueToCode(block, 'IF',
|
||||
Blockly.PHP.ORDER_CONDITIONAL) || 'false';
|
||||
var value_then = Blockly.PHP.valueToCode(block, 'THEN',
|
||||
Blockly.PHP.ORDER_CONDITIONAL) || 'null';
|
||||
var value_else = Blockly.PHP.valueToCode(block, 'ELSE',
|
||||
Blockly.PHP.ORDER_CONDITIONAL) || 'null';
|
||||
var code = value_if + ' ? ' + value_then + ' : ' + value_else;
|
||||
return [code, Blockly.PHP.ORDER_CONDITIONAL];
|
||||
};
|
||||
164
node_modules/node-blockly/blockly/generators/php/loops.js
generated
vendored
Normal file
164
node_modules/node-blockly/blockly/generators/php/loops.js
generated
vendored
Normal file
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2015 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 PHP for loop blocks.
|
||||
* @author daarond@gmail.com (Daaron Dwyer)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP.loops');
|
||||
|
||||
goog.require('Blockly.PHP');
|
||||
|
||||
|
||||
Blockly.PHP['controls_repeat_ext'] = function(block) {
|
||||
// Repeat n times.
|
||||
if (block.getField('TIMES')) {
|
||||
// Internal number.
|
||||
var repeats = String(Number(block.getFieldValue('TIMES')));
|
||||
} else {
|
||||
// External number.
|
||||
var repeats = Blockly.PHP.valueToCode(block, 'TIMES',
|
||||
Blockly.PHP.ORDER_ASSIGNMENT) || '0';
|
||||
}
|
||||
var branch = Blockly.PHP.statementToCode(block, 'DO');
|
||||
branch = Blockly.PHP.addLoopTrap(branch, block.id);
|
||||
var code = '';
|
||||
var loopVar = Blockly.PHP.variableDB_.getDistinctName(
|
||||
'count', Blockly.Variables.NAME_TYPE);
|
||||
var endVar = repeats;
|
||||
if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) {
|
||||
var endVar = Blockly.PHP.variableDB_.getDistinctName(
|
||||
'repeat_end', Blockly.Variables.NAME_TYPE);
|
||||
code += endVar + ' = ' + repeats + ';\n';
|
||||
}
|
||||
code += 'for (' + loopVar + ' = 0; ' +
|
||||
loopVar + ' < ' + endVar + '; ' +
|
||||
loopVar + '++) {\n' +
|
||||
branch + '}\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.PHP['controls_repeat'] = Blockly.PHP['controls_repeat_ext'];
|
||||
|
||||
Blockly.PHP['controls_whileUntil'] = function(block) {
|
||||
// Do while/until loop.
|
||||
var until = block.getFieldValue('MODE') == 'UNTIL';
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'BOOL',
|
||||
until ? Blockly.PHP.ORDER_LOGICAL_NOT :
|
||||
Blockly.PHP.ORDER_NONE) || 'false';
|
||||
var branch = Blockly.PHP.statementToCode(block, 'DO');
|
||||
branch = Blockly.PHP.addLoopTrap(branch, block.id);
|
||||
if (until) {
|
||||
argument0 = '!' + argument0;
|
||||
}
|
||||
return 'while (' + argument0 + ') {\n' + branch + '}\n';
|
||||
};
|
||||
|
||||
Blockly.PHP['controls_for'] = function(block) {
|
||||
// For loop.
|
||||
var variable0 = Blockly.PHP.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'FROM',
|
||||
Blockly.PHP.ORDER_ASSIGNMENT) || '0';
|
||||
var argument1 = Blockly.PHP.valueToCode(block, 'TO',
|
||||
Blockly.PHP.ORDER_ASSIGNMENT) || '0';
|
||||
var increment = Blockly.PHP.valueToCode(block, 'BY',
|
||||
Blockly.PHP.ORDER_ASSIGNMENT) || '1';
|
||||
var branch = Blockly.PHP.statementToCode(block, 'DO');
|
||||
branch = Blockly.PHP.addLoopTrap(branch, block.id);
|
||||
var code;
|
||||
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
|
||||
Blockly.isNumber(increment)) {
|
||||
// All arguments are simple numbers.
|
||||
var up = parseFloat(argument0) <= parseFloat(argument1);
|
||||
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
|
||||
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
|
||||
variable0;
|
||||
var step = Math.abs(parseFloat(increment));
|
||||
if (step == 1) {
|
||||
code += up ? '++' : '--';
|
||||
} else {
|
||||
code += (up ? ' += ' : ' -= ') + step;
|
||||
}
|
||||
code += ') {\n' + branch + '}\n';
|
||||
} else {
|
||||
code = '';
|
||||
// Cache non-trivial values to variables to prevent repeated look-ups.
|
||||
var startVar = argument0;
|
||||
if (!argument0.match(/^\w+$/) && !Blockly.isNumber(argument0)) {
|
||||
startVar = Blockly.PHP.variableDB_.getDistinctName(
|
||||
variable0 + '_start', Blockly.Variables.NAME_TYPE);
|
||||
code += startVar + ' = ' + argument0 + ';\n';
|
||||
}
|
||||
var endVar = argument1;
|
||||
if (!argument1.match(/^\w+$/) && !Blockly.isNumber(argument1)) {
|
||||
var endVar = Blockly.PHP.variableDB_.getDistinctName(
|
||||
variable0 + '_end', Blockly.Variables.NAME_TYPE);
|
||||
code += endVar + ' = ' + argument1 + ';\n';
|
||||
}
|
||||
// Determine loop direction at start, in case one of the bounds
|
||||
// changes during loop execution.
|
||||
var incVar = Blockly.PHP.variableDB_.getDistinctName(
|
||||
variable0 + '_inc', Blockly.Variables.NAME_TYPE);
|
||||
code += incVar + ' = ';
|
||||
if (Blockly.isNumber(increment)) {
|
||||
code += Math.abs(increment) + ';\n';
|
||||
} else {
|
||||
code += 'abs(' + increment + ');\n';
|
||||
}
|
||||
code += 'if (' + startVar + ' > ' + endVar + ') {\n';
|
||||
code += Blockly.PHP.INDENT + incVar + ' = -' + incVar + ';\n';
|
||||
code += '}\n';
|
||||
code += 'for (' + variable0 + ' = ' + startVar + '; ' +
|
||||
incVar + ' >= 0 ? ' +
|
||||
variable0 + ' <= ' + endVar + ' : ' +
|
||||
variable0 + ' >= ' + endVar + '; ' +
|
||||
variable0 + ' += ' + incVar + ') {\n' +
|
||||
branch + '}\n';
|
||||
}
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.PHP['controls_forEach'] = function(block) {
|
||||
// For each loop.
|
||||
var variable0 = Blockly.PHP.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_ASSIGNMENT) || '[]';
|
||||
var branch = Blockly.PHP.statementToCode(block, 'DO');
|
||||
branch = Blockly.PHP.addLoopTrap(branch, block.id);
|
||||
var code = '';
|
||||
code += 'foreach (' + argument0 + ' as ' + variable0 +
|
||||
') {\n' + branch + '}\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.PHP['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.';
|
||||
};
|
||||
374
node_modules/node-blockly/blockly/generators/php/math.js
generated
vendored
Normal file
374
node_modules/node-blockly/blockly/generators/php/math.js
generated
vendored
Normal file
@@ -0,0 +1,374 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2015 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 PHP for math blocks.
|
||||
* @author daarond@gmail.com (Daaron Dwyer)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP.math');
|
||||
|
||||
goog.require('Blockly.PHP');
|
||||
|
||||
|
||||
Blockly.PHP['math_number'] = function(block) {
|
||||
// Numeric value.
|
||||
var code = parseFloat(block.getFieldValue('NUM'));
|
||||
var order = code >= 0 ? Blockly.PHP.ORDER_ATOMIC :
|
||||
Blockly.PHP.ORDER_UNARY_NEGATION;
|
||||
if (code == Infinity) {
|
||||
code = 'INF';
|
||||
} else if (code == -Infinity) {
|
||||
code = '-INF';
|
||||
}
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.PHP['math_arithmetic'] = function(block) {
|
||||
// Basic arithmetic operators, and power.
|
||||
var OPERATORS = {
|
||||
'ADD': [' + ', Blockly.PHP.ORDER_ADDITION],
|
||||
'MINUS': [' - ', Blockly.PHP.ORDER_SUBTRACTION],
|
||||
'MULTIPLY': [' * ', Blockly.PHP.ORDER_MULTIPLICATION],
|
||||
'DIVIDE': [' / ', Blockly.PHP.ORDER_DIVISION],
|
||||
'POWER': [' ** ', Blockly.PHP.ORDER_POWER]
|
||||
};
|
||||
var tuple = OPERATORS[block.getFieldValue('OP')];
|
||||
var operator = tuple[0];
|
||||
var order = tuple[1];
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'A', order) || '0';
|
||||
var argument1 = Blockly.PHP.valueToCode(block, 'B', order) || '0';
|
||||
var code = argument0 + operator + argument1;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.PHP['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.
|
||||
arg = Blockly.PHP.valueToCode(block, 'NUM',
|
||||
Blockly.PHP.ORDER_UNARY_NEGATION) || '0';
|
||||
if (arg[0] == '-') {
|
||||
// --3 is not legal in JS.
|
||||
arg = ' ' + arg;
|
||||
}
|
||||
code = '-' + arg;
|
||||
return [code, Blockly.PHP.ORDER_UNARY_NEGATION];
|
||||
}
|
||||
if (operator == 'SIN' || operator == 'COS' || operator == 'TAN') {
|
||||
arg = Blockly.PHP.valueToCode(block, 'NUM',
|
||||
Blockly.PHP.ORDER_DIVISION) || '0';
|
||||
} else {
|
||||
arg = Blockly.PHP.valueToCode(block, 'NUM',
|
||||
Blockly.PHP.ORDER_NONE) || '0';
|
||||
}
|
||||
// First, handle cases which generate values that don't need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'ABS':
|
||||
code = 'abs(' + arg + ')';
|
||||
break;
|
||||
case 'ROOT':
|
||||
code = 'sqrt(' + arg + ')';
|
||||
break;
|
||||
case 'LN':
|
||||
code = 'log(' + arg + ')';
|
||||
break;
|
||||
case 'EXP':
|
||||
code = 'exp(' + arg + ')';
|
||||
break;
|
||||
case 'POW10':
|
||||
code = 'pow(10,' + arg + ')';
|
||||
break;
|
||||
case 'ROUND':
|
||||
code = 'round(' + arg + ')';
|
||||
break;
|
||||
case 'ROUNDUP':
|
||||
code = 'ceil(' + arg + ')';
|
||||
break;
|
||||
case 'ROUNDDOWN':
|
||||
code = 'floor(' + arg + ')';
|
||||
break;
|
||||
case 'SIN':
|
||||
code = 'sin(' + arg + ' / 180 * pi())';
|
||||
break;
|
||||
case 'COS':
|
||||
code = 'cos(' + arg + ' / 180 * pi())';
|
||||
break;
|
||||
case 'TAN':
|
||||
code = 'tan(' + arg + ' / 180 * pi())';
|
||||
break;
|
||||
}
|
||||
if (code) {
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
// Second, handle cases which generate values that may need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'LOG10':
|
||||
code = 'log(' + arg + ') / log(10)';
|
||||
break;
|
||||
case 'ASIN':
|
||||
code = 'asin(' + arg + ') / pi() * 180';
|
||||
break;
|
||||
case 'ACOS':
|
||||
code = 'acos(' + arg + ') / pi() * 180';
|
||||
break;
|
||||
case 'ATAN':
|
||||
code = 'atan(' + arg + ') / pi() * 180';
|
||||
break;
|
||||
default:
|
||||
throw 'Unknown math operator: ' + operator;
|
||||
}
|
||||
return [code, Blockly.PHP.ORDER_DIVISION];
|
||||
};
|
||||
|
||||
Blockly.PHP['math_constant'] = function(block) {
|
||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||
var CONSTANTS = {
|
||||
'PI': ['M_PI', Blockly.PHP.ORDER_ATOMIC],
|
||||
'E': ['M_E', Blockly.PHP.ORDER_ATOMIC],
|
||||
'GOLDEN_RATIO': ['(1 + sqrt(5)) / 2', Blockly.PHP.ORDER_DIVISION],
|
||||
'SQRT2': ['M_SQRT2', Blockly.PHP.ORDER_ATOMIC],
|
||||
'SQRT1_2': ['M_SQRT1_2', Blockly.PHP.ORDER_ATOMIC],
|
||||
'INFINITY': ['INF', Blockly.PHP.ORDER_ATOMIC]
|
||||
};
|
||||
return CONSTANTS[block.getFieldValue('CONSTANT')];
|
||||
};
|
||||
|
||||
Blockly.PHP['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.PHP.valueToCode(block, 'NUMBER_TO_CHECK',
|
||||
Blockly.PHP.ORDER_MODULUS) || '0';
|
||||
var dropdown_property = block.getFieldValue('PROPERTY');
|
||||
var code;
|
||||
if (dropdown_property == 'PRIME') {
|
||||
// Prime is a special case as it is not a one-liner test.
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'math_isPrime',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($n) {',
|
||||
' // https://en.wikipedia.org/wiki/Primality_test#Naive_methods',
|
||||
' if ($n == 2 || $n == 3) {',
|
||||
' return true;',
|
||||
' }',
|
||||
' // False if n is NaN, negative, is 1, or not whole.',
|
||||
' // And false if n is divisible by 2 or 3.',
|
||||
' if (!is_numeric($n) || $n <= 1 || $n % 1 != 0 || $n % 2 == 0 ||' +
|
||||
' $n % 3 == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' // Check all the numbers of form 6k +/- 1, up to sqrt(n).',
|
||||
' for ($x = 6; $x <= sqrt($n) + 1; $x += 6) {',
|
||||
' if ($n % ($x - 1) == 0 || $n % ($x + 1) == 0) {',
|
||||
' return false;',
|
||||
' }',
|
||||
' }',
|
||||
' return true;',
|
||||
'}']);
|
||||
code = functionName + '(' + number_to_check + ')';
|
||||
return [code, Blockly.JavaScript.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 = 'is_int(' + number_to_check + ')';
|
||||
break;
|
||||
case 'POSITIVE':
|
||||
code = number_to_check + ' > 0';
|
||||
break;
|
||||
case 'NEGATIVE':
|
||||
code = number_to_check + ' < 0';
|
||||
break;
|
||||
case 'DIVISIBLE_BY':
|
||||
var divisor = Blockly.PHP.valueToCode(block, 'DIVISOR',
|
||||
Blockly.PHP.ORDER_MODULUS) || '0';
|
||||
code = number_to_check + ' % ' + divisor + ' == 0';
|
||||
break;
|
||||
}
|
||||
return [code, Blockly.PHP.ORDER_EQUALITY];
|
||||
};
|
||||
|
||||
Blockly.PHP['math_change'] = function(block) {
|
||||
// Add to a variable in place.
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'DELTA',
|
||||
Blockly.PHP.ORDER_ADDITION) || '0';
|
||||
var varName = Blockly.PHP.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' += ' + argument0 + ';\n';
|
||||
};
|
||||
|
||||
// Rounding functions have a single operand.
|
||||
Blockly.PHP['math_round'] = Blockly.PHP['math_single'];
|
||||
// Trigonometry functions have a single operand.
|
||||
Blockly.PHP['math_trig'] = Blockly.PHP['math_single'];
|
||||
|
||||
Blockly.PHP['math_on_list'] = function(block) {
|
||||
// Math functions for lists.
|
||||
var func = block.getFieldValue('OP');
|
||||
var list, code;
|
||||
switch (func) {
|
||||
case 'SUM':
|
||||
list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
|
||||
code = 'array_sum(' + list + ')';
|
||||
break;
|
||||
case 'MIN':
|
||||
list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
|
||||
code = 'min(' + list + ')';
|
||||
break;
|
||||
case 'MAX':
|
||||
list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_FUNCTION_CALL) || 'array()';
|
||||
code = 'max(' + list + ')';
|
||||
break;
|
||||
case 'AVERAGE':
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'math_mean',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($myList) {',
|
||||
' return array_sum($myList) / count($myList);',
|
||||
'}']);
|
||||
list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_NONE) || 'array()';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MEDIAN':
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'math_median',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($arr) {',
|
||||
' sort($arr,SORT_NUMERIC);',
|
||||
' return (count($arr) % 2) ? $arr[floor(count($arr)/2)] : ',
|
||||
' ($arr[floor(count($arr)/2)] + $arr[floor(count($arr)/2)' +
|
||||
' - 1]) / 2;',
|
||||
'}']);
|
||||
list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'MODE':
|
||||
// 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].
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'math_modes',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($values) {',
|
||||
' if (empty($values)) return array();',
|
||||
' $counts = array_count_values($values);',
|
||||
' arsort($counts); // Sort counts in descending order',
|
||||
' $modes = array_keys($counts, current($counts), true);',
|
||||
' return $modes;',
|
||||
'}']);
|
||||
list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'STD_DEV':
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'math_standard_deviation',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($numbers) {',
|
||||
' $n = count($numbers);',
|
||||
' if (!$n) return null;',
|
||||
' $mean = array_sum($numbers) / count($numbers);',
|
||||
' foreach($numbers as $key => $num) $devs[$key] = ' +
|
||||
'pow($num - $mean, 2);',
|
||||
' return sqrt(array_sum($devs) / (count($devs) - 1));',
|
||||
'}']);
|
||||
list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
case 'RANDOM':
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'math_random_list',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($list) {',
|
||||
' $x = rand(0, count($list)-1);',
|
||||
' return $list[$x];',
|
||||
'}']);
|
||||
list = Blockly.PHP.valueToCode(block, 'LIST',
|
||||
Blockly.PHP.ORDER_NONE) || '[]';
|
||||
code = functionName + '(' + list + ')';
|
||||
break;
|
||||
default:
|
||||
throw 'Unknown operator: ' + func;
|
||||
}
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['math_modulo'] = function(block) {
|
||||
// Remainder computation.
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'DIVIDEND',
|
||||
Blockly.PHP.ORDER_MODULUS) || '0';
|
||||
var argument1 = Blockly.PHP.valueToCode(block, 'DIVISOR',
|
||||
Blockly.PHP.ORDER_MODULUS) || '0';
|
||||
var code = argument0 + ' % ' + argument1;
|
||||
return [code, Blockly.PHP.ORDER_MODULUS];
|
||||
};
|
||||
|
||||
Blockly.PHP['math_constrain'] = function(block) {
|
||||
// Constrain a number between two limits.
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_COMMA) || '0';
|
||||
var argument1 = Blockly.PHP.valueToCode(block, 'LOW',
|
||||
Blockly.PHP.ORDER_COMMA) || '0';
|
||||
var argument2 = Blockly.PHP.valueToCode(block, 'HIGH',
|
||||
Blockly.PHP.ORDER_COMMA) || 'Infinity';
|
||||
var code = 'min(max(' + argument0 + ', ' + argument1 + '), ' +
|
||||
argument2 + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['math_random_int'] = function(block) {
|
||||
// Random integer between [X] and [Y].
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'FROM',
|
||||
Blockly.PHP.ORDER_COMMA) || '0';
|
||||
var argument1 = Blockly.PHP.valueToCode(block, 'TO',
|
||||
Blockly.PHP.ORDER_COMMA) || '0';
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'math_random_int',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($a, $b) {',
|
||||
' if ($a > $b) {',
|
||||
' return rand($b, $a);',
|
||||
' }',
|
||||
' return rand($a, $b);',
|
||||
'}']);
|
||||
var code = functionName + '(' + argument0 + ', ' + argument1 + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['math_random_float'] = function(block) {
|
||||
// Random fraction between 0 and 1.
|
||||
return ['(float)rand()/(float)getrandmax()', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
131
node_modules/node-blockly/blockly/generators/php/procedures.js
generated
vendored
Normal file
131
node_modules/node-blockly/blockly/generators/php/procedures.js
generated
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2015 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 PHP for procedure blocks.
|
||||
* @author daarond@gmail.com (Daaron Dwyer)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP.procedures');
|
||||
|
||||
goog.require('Blockly.PHP');
|
||||
|
||||
Blockly.PHP['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 = [];
|
||||
var varName;
|
||||
var workspace = block.workspace;
|
||||
var variables = Blockly.Variables.allUsedVarModels(workspace) || [];
|
||||
for (var i = 0, variable; variable = variables[i]; i++) {
|
||||
varName = variable.name;
|
||||
if (block.arguments_.indexOf(varName) == -1) {
|
||||
globals.push(Blockly.PHP.variableDB_.getName(varName,
|
||||
Blockly.Variables.NAME_TYPE));
|
||||
}
|
||||
}
|
||||
// Add developer variables.
|
||||
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
for (var i = 0; i < devVarList.length; i++) {
|
||||
globals.push(Blockly.PHP.variableDB_.getName(devVarList[i],
|
||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
|
||||
}
|
||||
globals = globals.length ?
|
||||
Blockly.PHP.INDENT + 'global ' + globals.join(', ') + ';\n' : '';
|
||||
|
||||
var funcName = Blockly.PHP.variableDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
|
||||
var branch = Blockly.PHP.statementToCode(block, 'STACK');
|
||||
if (Blockly.PHP.STATEMENT_PREFIX) {
|
||||
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
|
||||
branch = Blockly.PHP.prefixLines(
|
||||
Blockly.PHP.STATEMENT_PREFIX.replace(
|
||||
/%1/g, '\'' + id + '\''), Blockly.PHP.INDENT) + branch;
|
||||
}
|
||||
if (Blockly.PHP.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.PHP.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
var returnValue = Blockly.PHP.valueToCode(block, 'RETURN',
|
||||
Blockly.PHP.ORDER_NONE) || '';
|
||||
if (returnValue) {
|
||||
returnValue = Blockly.PHP.INDENT + 'return ' + returnValue + ';\n';
|
||||
}
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.PHP.variableDB_.getName(block.arguments_[i],
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
}
|
||||
var code = 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
|
||||
globals + branch + returnValue + '}';
|
||||
code = Blockly.PHP.scrub_(block, code);
|
||||
// Add % so as not to collide with helper functions in definitions list.
|
||||
Blockly.PHP.definitions_['%' + funcName] = code;
|
||||
return null;
|
||||
};
|
||||
|
||||
// Defining a procedure without a return value uses the same generator as
|
||||
// a procedure with a return value.
|
||||
Blockly.PHP['procedures_defnoreturn'] =
|
||||
Blockly.PHP['procedures_defreturn'];
|
||||
|
||||
Blockly.PHP['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
var funcName = Blockly.PHP.variableDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.PHP.valueToCode(block, 'ARG' + i,
|
||||
Blockly.PHP.ORDER_COMMA) || 'null';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['procedures_callnoreturn'] = function(block) {
|
||||
// Call a procedure with no return value.
|
||||
var funcName = Blockly.PHP.variableDB_.getName(
|
||||
block.getFieldValue('NAME'), Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var i = 0; i < block.arguments_.length; i++) {
|
||||
args[i] = Blockly.PHP.valueToCode(block, 'ARG' + i,
|
||||
Blockly.PHP.ORDER_COMMA) || 'null';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ');\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.PHP['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
var condition = Blockly.PHP.valueToCode(block, 'CONDITION',
|
||||
Blockly.PHP.ORDER_NONE) || 'false';
|
||||
var code = 'if (' + condition + ') {\n';
|
||||
if (block.hasReturnValue_) {
|
||||
var value = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || 'null';
|
||||
code += Blockly.PHP.INDENT + 'return ' + value + ';\n';
|
||||
} else {
|
||||
code += Blockly.PHP.INDENT + 'return;\n';
|
||||
}
|
||||
code += '}\n';
|
||||
return code;
|
||||
};
|
||||
279
node_modules/node-blockly/blockly/generators/php/text.js
generated
vendored
Normal file
279
node_modules/node-blockly/blockly/generators/php/text.js
generated
vendored
Normal file
@@ -0,0 +1,279 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2015 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 PHP for text blocks.
|
||||
* @author daarond@gmail.com (Daaron Dwyer)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP.texts');
|
||||
|
||||
goog.require('Blockly.PHP');
|
||||
|
||||
|
||||
Blockly.PHP['text'] = function(block) {
|
||||
// Text value.
|
||||
var code = Blockly.PHP.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Blockly.PHP.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_join'] = function(block) {
|
||||
// Create a string made up of any number of elements of any type.
|
||||
if (block.itemCount_ == 0) {
|
||||
return ['\'\'', Blockly.PHP.ORDER_ATOMIC];
|
||||
} else if (block.itemCount_ == 1) {
|
||||
var element = Blockly.PHP.valueToCode(block, 'ADD0',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var code = element;
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
} else if (block.itemCount_ == 2) {
|
||||
var element0 = Blockly.PHP.valueToCode(block, 'ADD0',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var element1 = Blockly.PHP.valueToCode(block, 'ADD1',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var code = element0 + ' . ' + element1;
|
||||
return [code, Blockly.PHP.ORDER_ADDITION];
|
||||
} else {
|
||||
var elements = new Array(block.itemCount_);
|
||||
for (var i = 0; i < block.itemCount_; i++) {
|
||||
elements[i] = Blockly.PHP.valueToCode(block, 'ADD' + i,
|
||||
Blockly.PHP.ORDER_COMMA) || '\'\'';
|
||||
}
|
||||
var code = 'implode(\'\', array(' + elements.join(',') + '))';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.PHP['text_append'] = function(block) {
|
||||
// Append to a variable in place.
|
||||
var varName = Blockly.PHP.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var value = Blockly.PHP.valueToCode(block, 'TEXT',
|
||||
Blockly.PHP.ORDER_ASSIGNMENT) || '\'\'';
|
||||
return varName + ' .= ' + value + ';\n';
|
||||
};
|
||||
|
||||
Blockly.PHP['text_length'] = function(block) {
|
||||
// String or array length.
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'length',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($value) {',
|
||||
' if (is_string($value)) {',
|
||||
' return strlen($value);',
|
||||
' } else {',
|
||||
' return count($value);',
|
||||
' }',
|
||||
'}']);
|
||||
var text = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
return [functionName + '(' + text + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_isEmpty'] = function(block) {
|
||||
// Is the string null or array empty?
|
||||
var text = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
return ['empty(' + text + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_indexOf'] = function(block) {
|
||||
// Search the text for a substring.
|
||||
var operator = block.getFieldValue('END') == 'FIRST' ?
|
||||
'strpos' : 'strrpos';
|
||||
var substring = Blockly.PHP.valueToCode(block, 'FIND',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var text = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
if (block.workspace.options.oneBasedIndex) {
|
||||
var errorIndex = ' 0';
|
||||
var indexAdjustment = ' + 1';
|
||||
} else {
|
||||
var errorIndex = ' -1';
|
||||
var indexAdjustment = '';
|
||||
}
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
block.getFieldValue('END') == 'FIRST' ?
|
||||
'text_indexOf' : 'text_lastIndexOf',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($text, $search) {',
|
||||
' $pos = ' + operator + '($text, $search);',
|
||||
' return $pos === false ? ' + errorIndex + ' : $pos' +
|
||||
indexAdjustment + ';',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ', ' + substring + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_charAt'] = function(block) {
|
||||
// Get letter at index.
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var textOrder = (where == 'RANDOM') ? Blockly.PHP.ORDER_NONE :
|
||||
Blockly.PHP.ORDER_COMMA;
|
||||
var text = Blockly.PHP.valueToCode(block, 'VALUE', textOrder) || '\'\'';
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
var code = 'substr(' + text + ', 0, 1)';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
case 'LAST':
|
||||
var code = 'substr(' + text + ', -1)';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
case 'FROM_START':
|
||||
var at = Blockly.PHP.getAdjusted(block, 'AT');
|
||||
var code = 'substr(' + text + ', ' + at + ', 1)';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
case 'FROM_END':
|
||||
var at = Blockly.PHP.getAdjusted(block, 'AT', 1, true);
|
||||
var code = 'substr(' + text + ', ' + at + ', 1)';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
case 'RANDOM':
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'text_random_letter',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ + '($text) {',
|
||||
' return $text[rand(0, strlen($text) - 1)];',
|
||||
'}']);
|
||||
code = functionName + '(' + text + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
}
|
||||
throw 'Unhandled option (text_charAt).';
|
||||
};
|
||||
|
||||
Blockly.PHP['text_getSubstring'] = function(block) {
|
||||
// Get substring.
|
||||
var text = Blockly.PHP.valueToCode(block, 'STRING',
|
||||
Blockly.PHP.ORDER_FUNCTION_CALL) || '\'\'';
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
if (where1 == 'FIRST' && where2 == 'LAST') {
|
||||
var code = text;
|
||||
} else {
|
||||
var at1 = Blockly.PHP.getAdjusted(block, 'AT1');
|
||||
var at2 = Blockly.PHP.getAdjusted(block, 'AT2');
|
||||
var functionName = Blockly.PHP.provideFunction_(
|
||||
'text_get_substring',
|
||||
['function ' + Blockly.PHP.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'($text, $where1, $at1, $where2, $at2) {',
|
||||
' if ($where1 == \'FROM_END\') {',
|
||||
' $at1 = strlen($text) - 1 - $at1;',
|
||||
' } else if ($where1 == \'FIRST\') {',
|
||||
' $at1 = 0;',
|
||||
' } else if ($where1 != \'FROM_START\'){',
|
||||
' throw new Exception(\'Unhandled option (text_get_substring).\');',
|
||||
' }',
|
||||
' $length = 0;',
|
||||
' if ($where2 == \'FROM_START\') {',
|
||||
' $length = $at2 - $at1 + 1;',
|
||||
' } else if ($where2 == \'FROM_END\') {',
|
||||
' $length = strlen($text) - $at1 - $at2;',
|
||||
' } else if ($where2 == \'LAST\') {',
|
||||
' $length = strlen($text) - $at1;',
|
||||
' } else {',
|
||||
' throw new Exception(\'Unhandled option (text_get_substring).\');',
|
||||
' }',
|
||||
' return substr($text, $at1, $length);',
|
||||
'}']);
|
||||
var code = functionName + '(' + text + ', \'' +
|
||||
where1 + '\', ' + at1 + ', \'' + where2 + '\', ' + at2 + ')';
|
||||
}
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_changeCase'] = function(block) {
|
||||
// Change capitalization.
|
||||
var text = Blockly.PHP.valueToCode(block, 'TEXT',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
if (block.getFieldValue('CASE') == 'UPPERCASE') {
|
||||
var code = 'strtoupper(' + text + ')';
|
||||
} else if (block.getFieldValue('CASE') == 'LOWERCASE') {
|
||||
var code = 'strtolower(' + text + ')';
|
||||
} else if (block.getFieldValue('CASE') == 'TITLECASE') {
|
||||
var code = 'ucwords(strtolower(' + text + '))';
|
||||
}
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_trim'] = function(block) {
|
||||
// Trim spaces.
|
||||
var OPERATORS = {
|
||||
'LEFT': 'ltrim',
|
||||
'RIGHT': 'rtrim',
|
||||
'BOTH': 'trim'
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
var text = Blockly.PHP.valueToCode(block, 'TEXT',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
return [operator + '(' + text + ')', Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_print'] = function(block) {
|
||||
// Print statement.
|
||||
var msg = Blockly.PHP.valueToCode(block, 'TEXT',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
return 'print(' + msg + ');\n';
|
||||
};
|
||||
|
||||
Blockly.PHP['text_prompt_ext'] = function(block) {
|
||||
// Prompt function.
|
||||
if (block.getField('TEXT')) {
|
||||
// Internal message.
|
||||
var msg = Blockly.PHP.quote_(block.getFieldValue('TEXT'));
|
||||
} else {
|
||||
// External message.
|
||||
var msg = Blockly.PHP.valueToCode(block, 'TEXT',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
var code = 'readline(' + msg + ')';
|
||||
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
|
||||
if (toNumber) {
|
||||
code = 'floatval(' + code + ')';
|
||||
}
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_prompt'] = Blockly.PHP['text_prompt_ext'];
|
||||
|
||||
Blockly.PHP['text_count'] = function(block) {
|
||||
var text = Blockly.PHP.valueToCode(block, 'TEXT',
|
||||
Blockly.PHP.ORDER_MEMBER) || '\'\'';
|
||||
var sub = Blockly.PHP.valueToCode(block, 'SUB',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var code = 'strlen(' + sub + ') === 0'
|
||||
+ ' ? strlen(' + text + ') + 1'
|
||||
+ ' : substr_count(' + text + ', ' + sub + ')';
|
||||
return [code, Blockly.PHP.ORDER_CONDITIONAL];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_replace'] = function(block) {
|
||||
var text = Blockly.PHP.valueToCode(block, 'TEXT',
|
||||
Blockly.PHP.ORDER_MEMBER) || '\'\'';
|
||||
var from = Blockly.PHP.valueToCode(block, 'FROM',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var to = Blockly.PHP.valueToCode(block, 'TO',
|
||||
Blockly.PHP.ORDER_NONE) || '\'\'';
|
||||
var code = 'str_replace(' + from + ', ' + to + ', ' + text + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
|
||||
Blockly.PHP['text_reverse'] = function(block) {
|
||||
var text = Blockly.PHP.valueToCode(block, 'TEXT',
|
||||
Blockly.PHP.ORDER_MEMBER) || '\'\'';
|
||||
var code = 'strrev(' + text + ')';
|
||||
return [code, Blockly.PHP.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
46
node_modules/node-blockly/blockly/generators/php/variables.js
generated
vendored
Normal file
46
node_modules/node-blockly/blockly/generators/php/variables.js
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2015 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 PHP for variable blocks.
|
||||
* @author daarond@gmail.com (Daaron Dwyer)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP.variables');
|
||||
|
||||
goog.require('Blockly.PHP');
|
||||
|
||||
|
||||
Blockly.PHP['variables_get'] = function(block) {
|
||||
// Variable getter.
|
||||
var code = Blockly.PHP.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return [code, Blockly.PHP.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.PHP['variables_set'] = function(block) {
|
||||
// Variable setter.
|
||||
var argument0 = Blockly.PHP.valueToCode(block, 'VALUE',
|
||||
Blockly.PHP.ORDER_ASSIGNMENT) || '0';
|
||||
var varName = Blockly.PHP.variableDB_.getName(
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = ' + argument0 + ';\n';
|
||||
};
|
||||
35
node_modules/node-blockly/blockly/generators/php/variables_dynamic.js
generated
vendored
Normal file
35
node_modules/node-blockly/blockly/generators/php/variables_dynamic.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2018 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 PHP for dynamic variable blocks.
|
||||
* @author fenichel@google.com (Rachel Fenichel)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.PHP.variablesDynamic');
|
||||
|
||||
goog.require('Blockly.PHP');
|
||||
goog.require('Blockly.PHP.variables');
|
||||
|
||||
|
||||
// PHP is dynamically typed.
|
||||
Blockly.PHP['variables_get_dynamic'] = Blockly.PHP['variables_get'];
|
||||
Blockly.PHP['variables_set_dynamic'] = Blockly.PHP['variables_set'];
|
||||
324
node_modules/node-blockly/blockly/generators/python.js
generated
vendored
Normal file
324
node_modules/node-blockly/blockly/generators/python.js
generated
vendored
Normal file
@@ -0,0 +1,324 @@
|
||||
/**
|
||||
* @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(sorted(keyword.kwlist)))
|
||||
// https://docs.python.org/3/reference/lexical_analysis.html#keywords
|
||||
// https://docs.python.org/2/reference/lexical_analysis.html#keywords
|
||||
'False,None,True,and,as,assert,break,class,continue,def,del,elif,else,' +
|
||||
'except,exec,finally,for,from,global,if,import,in,is,lambda,nonlocal,not,' +
|
||||
'or,pass,print,raise,return,try,while,with,yield,' +
|
||||
// https://docs.python.org/3/library/constants.html
|
||||
// https://docs.python.org/2/library/constants.html
|
||||
'NotImplemented,Ellipsis,__debug__,quit,exit,copyright,license,credits,' +
|
||||
// >>> print(','.join(sorted(dir(__builtins__))))
|
||||
// https://docs.python.org/3/library/functions.html
|
||||
// https://docs.python.org/2/library/functions.html
|
||||
'ArithmeticError,AssertionError,AttributeError,BaseException,' +
|
||||
'BlockingIOError,BrokenPipeError,BufferError,BytesWarning,' +
|
||||
'ChildProcessError,ConnectionAbortedError,ConnectionError,' +
|
||||
'ConnectionRefusedError,ConnectionResetError,DeprecationWarning,EOFError,' +
|
||||
'Ellipsis,EnvironmentError,Exception,FileExistsError,FileNotFoundError,' +
|
||||
'FloatingPointError,FutureWarning,GeneratorExit,IOError,ImportError,' +
|
||||
'ImportWarning,IndentationError,IndexError,InterruptedError,' +
|
||||
'IsADirectoryError,KeyError,KeyboardInterrupt,LookupError,MemoryError,' +
|
||||
'ModuleNotFoundError,NameError,NotADirectoryError,NotImplemented,' +
|
||||
'NotImplementedError,OSError,OverflowError,PendingDeprecationWarning,' +
|
||||
'PermissionError,ProcessLookupError,RecursionError,ReferenceError,' +
|
||||
'ResourceWarning,RuntimeError,RuntimeWarning,StandardError,' +
|
||||
'StopAsyncIteration,StopIteration,SyntaxError,SyntaxWarning,SystemError,' +
|
||||
'SystemExit,TabError,TimeoutError,TypeError,UnboundLocalError,' +
|
||||
'UnicodeDecodeError,UnicodeEncodeError,UnicodeError,' +
|
||||
'UnicodeTranslateError,UnicodeWarning,UserWarning,ValueError,Warning,' +
|
||||
'ZeroDivisionError,_,__build_class__,__debug__,__doc__,__import__,' +
|
||||
'__loader__,__name__,__package__,__spec__,abs,all,any,apply,ascii,' +
|
||||
'basestring,bin,bool,buffer,bytearray,bytes,callable,chr,classmethod,cmp,' +
|
||||
'coerce,compile,complex,copyright,credits,delattr,dict,dir,divmod,' +
|
||||
'enumerate,eval,exec,execfile,exit,file,filter,float,format,frozenset,' +
|
||||
'getattr,globals,hasattr,hash,help,hex,id,input,int,intern,isinstance,' +
|
||||
'issubclass,iter,len,license,list,locals,long,map,max,memoryview,min,' +
|
||||
'next,object,oct,open,ord,pow,print,property,quit,range,raw_input,reduce,' +
|
||||
'reload,repr,reversed,round,set,setattr,slice,sorted,staticmethod,str,' +
|
||||
'sum,super,tuple,type,unichr,unicode,vars,xrange,zip'
|
||||
);
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
Blockly.Python.variableDB_.setVariableMap(workspace.getVariableMap());
|
||||
|
||||
var defvars = [];
|
||||
// Add developer variables (not created or named by the user).
|
||||
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
for (var i = 0; i < devVarList.length; i++) {
|
||||
defvars.push(Blockly.Python.variableDB_.getName(devVarList[i],
|
||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE) + ' = None');
|
||||
}
|
||||
|
||||
// Add user variables, but only ones that are being used.
|
||||
var variables = Blockly.Variables.allUsedVarModels(workspace);
|
||||
for (var i = 0; i < variables.length; i++) {
|
||||
defvars.push(Blockly.Python.variableDB_.getName(variables[i].getId(),
|
||||
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, '\\%');
|
||||
|
||||
// Follow the CPython behaviour of repr() for a non-byte string.
|
||||
var quote = '\'';
|
||||
if (string.indexOf('\'') !== -1) {
|
||||
if (string.indexOf('"') === -1) {
|
||||
quote = '"';
|
||||
} else {
|
||||
string = string.replace(/'/g, '\\\'');
|
||||
}
|
||||
};
|
||||
return quote + string + quote;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
};
|
||||
86
node_modules/node-blockly/blockly/generators/python/colour.js
generated
vendored
Normal file
86
node_modules/node-blockly/blockly/generators/python/colour.js
generated
vendored
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];
|
||||
};
|
||||
363
node_modules/node-blockly/blockly/generators/python/lists.js
generated
vendored
Normal file
363
node_modules/node-blockly/blockly/generators/python/lists.js
generated
vendored
Normal file
@@ -0,0 +1,363 @@
|
||||
/**
|
||||
* @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];
|
||||
};
|
||||
|
||||
Blockly.Python['lists_reverse'] = function(block) {
|
||||
// Block for reversing a list.
|
||||
var list = Blockly.Python.valueToCode(block, 'LIST',
|
||||
Blockly.Python.ORDER_NONE) || '[]';
|
||||
var code = 'list(reversed(' + list + '))';
|
||||
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
|
||||
};
|
||||
128
node_modules/node-blockly/blockly/generators/python/logic.js
generated
vendored
Normal file
128
node_modules/node-blockly/blockly/generators/python/logic.js
generated
vendored
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* @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 code = '', branchCode, conditionCode;
|
||||
do {
|
||||
conditionCode = Blockly.Python.valueToCode(block, 'IF' + n,
|
||||
Blockly.Python.ORDER_NONE) || 'False';
|
||||
branchCode = Blockly.Python.statementToCode(block, 'DO' + n) ||
|
||||
Blockly.Python.PASS;
|
||||
code += (n == 0 ? 'if ' : 'elif ' ) + conditionCode + ':\n' + branchCode;
|
||||
|
||||
++n;
|
||||
} while (block.getInput('IF' + n));
|
||||
|
||||
if (block.getInput('ELSE')) {
|
||||
branchCode = Blockly.Python.statementToCode(block, 'ELSE') ||
|
||||
Blockly.Python.PASS;
|
||||
code += 'else:\n' + branchCode;
|
||||
}
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Python['controls_ifelse'] = Blockly.Python['controls_if'];
|
||||
|
||||
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
node_modules/node-blockly/blockly/generators/python/loops.js
generated
vendored
Normal file
211
node_modules/node-blockly/blockly/generators/python/loops.js
generated
vendored
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
node_modules/node-blockly/blockly/generators/python/math.js
generated
vendored
Normal file
388
node_modules/node-blockly/blockly/generators/python/math.js
generated
vendored
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];
|
||||
};
|
||||
133
node_modules/node-blockly/blockly/generators/python/procedures.js
generated
vendored
Normal file
133
node_modules/node-blockly/blockly/generators/python/procedures.js
generated
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* @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 = [];
|
||||
var varName;
|
||||
var workspace = block.workspace;
|
||||
var variables = Blockly.Variables.allUsedVarModels(workspace) || [];
|
||||
for (var i = 0, variable; variable = variables[i]; i++) {
|
||||
varName = variable.name;
|
||||
if (block.arguments_.indexOf(varName) == -1) {
|
||||
globals.push(Blockly.Python.variableDB_.getName(varName,
|
||||
Blockly.Variables.NAME_TYPE));
|
||||
}
|
||||
}
|
||||
// Add developer variables.
|
||||
var devVarList = Blockly.Variables.allDeveloperVariables(workspace);
|
||||
for (var i = 0; i < devVarList.length; i++) {
|
||||
globals.push(Blockly.Python.variableDB_.getName(devVarList[i],
|
||||
Blockly.Names.DEVELOPER_VARIABLE_TYPE));
|
||||
}
|
||||
|
||||
globals = globals.length ?
|
||||
Blockly.Python.INDENT + '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) {
|
||||
var id = block.id.replace(/\$/g, '$$$$'); // Issue 251.
|
||||
branch = Blockly.Python.prefixLines(
|
||||
Blockly.Python.STATEMENT_PREFIX.replace(
|
||||
/%1/g, '\'' + 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 = Blockly.Python.INDENT + '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 += Blockly.Python.INDENT + 'return ' + value + '\n';
|
||||
} else {
|
||||
code += Blockly.Python.INDENT + 'return\n';
|
||||
}
|
||||
return code;
|
||||
};
|
||||
280
node_modules/node-blockly/blockly/generators/python/text.js
generated
vendored
Normal file
280
node_modules/node-blockly/blockly/generators/python/text.js
generated
vendored
Normal file
@@ -0,0 +1,280 @@
|
||||
/**
|
||||
* @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'];
|
||||
|
||||
Blockly.Python['text_count'] = function(block) {
|
||||
var text = Blockly.Python.valueToCode(block, 'TEXT',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var sub = Blockly.Python.valueToCode(block, 'SUB',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
var code = text + '.count(' + sub + ')';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
Blockly.Python['text_replace'] = function(block) {
|
||||
var text = Blockly.Python.valueToCode(block, 'TEXT',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var from = Blockly.Python.valueToCode(block, 'FROM',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
var to = Blockly.Python.valueToCode(block, 'TO',
|
||||
Blockly.Python.ORDER_NONE) || '\'\'';
|
||||
var code = text + '.replace(' + from + ', ' + to + ')';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
Blockly.Python['text_reverse'] = function(block) {
|
||||
var text = Blockly.Python.valueToCode(block, 'TEXT',
|
||||
Blockly.Python.ORDER_MEMBER) || '\'\'';
|
||||
var code = text + '[::-1]';
|
||||
return [code, Blockly.Python.ORDER_MEMBER];
|
||||
};
|
||||
46
node_modules/node-blockly/blockly/generators/python/variables.js
generated
vendored
Normal file
46
node_modules/node-blockly/blockly/generators/python/variables.js
generated
vendored
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';
|
||||
};
|
||||
35
node_modules/node-blockly/blockly/generators/python/variables_dynamic.js
generated
vendored
Normal file
35
node_modules/node-blockly/blockly/generators/python/variables_dynamic.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @license
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2018 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 dynamic variable blocks.
|
||||
* @author fenichel@google.com (Rachel Fenichel)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Python.variablesDynamic');
|
||||
|
||||
goog.require('Blockly.Python');
|
||||
goog.require('Blockly.Python.variables');
|
||||
|
||||
|
||||
// Python is dynamically typed.
|
||||
Blockly.Python['variables_get_dynamic'] = Blockly.Python['variables_get'];
|
||||
Blockly.Python['variables_set_dynamic'] = Blockly.Python['variables_set'];
|
||||
Reference in New Issue
Block a user