mirror of
https://github.com/google/blockly.git
synced 2026-01-05 08:00:09 +01:00
Automatic commit Wed Jan 15 03:00:09 PST 2014
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* Copyright 2014 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -25,9 +25,10 @@
|
||||
|
||||
goog.provide('Blockly.Dart');
|
||||
|
||||
goog.require('Blockly.CodeGenerator');
|
||||
goog.require('Blockly.Generator');
|
||||
|
||||
Blockly.Dart = Blockly.Generator.get('Dart');
|
||||
|
||||
Blockly.Dart = new Blockly.Generator('Dart');
|
||||
|
||||
/**
|
||||
* List of illegal variable names.
|
||||
@@ -37,14 +38,15 @@ Blockly.Dart = Blockly.Generator.get('Dart');
|
||||
* @private
|
||||
*/
|
||||
Blockly.Dart.addReservedWords(
|
||||
// http://www.dartlang.org/docs/spec/latest/dart-language-specification.pdf
|
||||
// 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,extends,false,final,finally,for,if,in,is,new,null,return,super,switch,this,throw,true,try,var,void,while,with,' +
|
||||
// http://api.dartlang.org/dart_core.html
|
||||
'Collection,Comparable,Completer,Date,double,Function,Future,Hashable,HashMap,HashSet,int,InvocationMirror,Iterable,Iterator,LinkedHashMap,List,Map,Match,num,Options,Pattern,Queue,RegExp,Sequence,SequenceCollection,Set,Stopwatch,String,StringBuffer,Strings,Type,bool,DoubleLinkedQueue,DoubleLinkedQueueEntry,Duration,Expando,Expect,Futures,Object,SequenceIterator,SequenceList,Comparator,AbstractClassInstantiationError,ArgumentError,AssertionError,CastError,Error,Exception,ExpectException,FallThroughError,FormatException,FutureAlreadyCompleteException,FutureNotCompleteException,FutureUnhandledException,IllegalJSRegExpException,IntegerDivisionByZeroException,NoSuchMethodError,NullThrownError,OutOfMemoryError,RangeError,RuntimeError,StackOverflowError,StateError,TypeError,UnimplementedError,UnsupportedError');
|
||||
'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.
|
||||
* http://www.dartlang.org/docs/dart-up-and-running/ch02.html#operator_table
|
||||
* 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-- () [] .
|
||||
@@ -52,15 +54,16 @@ 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_RELATIONAL = 6; // is is! >= > <= <
|
||||
Blockly.Dart.ORDER_EQUALITY = 7; // == != === !==
|
||||
Blockly.Dart.ORDER_BITWISE_AND = 8; // &
|
||||
Blockly.Dart.ORDER_BITWISE_XOR = 9; // ^
|
||||
Blockly.Dart.ORDER_BITWISE_OR = 10; // |
|
||||
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_CONDITIONAL = 13; // expr ? expr : expr
|
||||
Blockly.Dart.ORDER_ASSIGNMENT = 14; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
|
||||
Blockly.Dart.ORDER_CASCADE = 14; // ..
|
||||
Blockly.Dart.ORDER_ASSIGNMENT = 15; // = *= /= ~/= %= += -= <<= >>= &= ^= |=
|
||||
Blockly.Dart.ORDER_NONE = 99; // (...)
|
||||
|
||||
/**
|
||||
@@ -76,7 +79,10 @@ Blockly.Dart.INFINITE_LOOP_TRAP = null;
|
||||
*/
|
||||
Blockly.Dart.init = function() {
|
||||
// Create a dictionary of definitions to be printed before the code.
|
||||
Blockly.Dart.definitions_ = {};
|
||||
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.Variables) {
|
||||
if (!Blockly.Dart.variableDB_) {
|
||||
@@ -169,7 +175,7 @@ Blockly.Dart.scrub_ = function(block, code) {
|
||||
// Collect comment for this block.
|
||||
var comment = block.getCommentText();
|
||||
if (comment) {
|
||||
commentCode += Blockly.Generator.prefixLines(comment, '// ') + '\n';
|
||||
commentCode += this.prefixLines(comment, '// ') + '\n';
|
||||
}
|
||||
// Collect comments for all value arguments.
|
||||
// Don't collect comments for nested statements.
|
||||
@@ -177,9 +183,9 @@ Blockly.Dart.scrub_ = function(block, code) {
|
||||
if (block.inputList[x].type == Blockly.INPUT_VALUE) {
|
||||
var childBlock = block.inputList[x].connection.targetBlock();
|
||||
if (childBlock) {
|
||||
var comment = Blockly.Generator.allNestedComments(childBlock);
|
||||
var comment = this.allNestedComments(childBlock);
|
||||
if (comment) {
|
||||
commentCode += Blockly.Generator.prefixLines(comment, '// ');
|
||||
commentCode += this.prefixLines(comment, '// ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* Copyright 2014 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -27,93 +27,101 @@ goog.provide('Blockly.Dart.colour');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart.addReservedWords('Math');
|
||||
|
||||
Blockly.Dart.colour_picker = function() {
|
||||
Blockly.Dart['colour_picker'] = function(block) {
|
||||
// Colour picker.
|
||||
var code = '\'' + this.getTitleValue('COLOUR') + '\'';
|
||||
var code = '\'' + block.getFieldValue('COLOUR') + '\'';
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart.colour_rgb = function() {
|
||||
// Compose a colour from RGB components.
|
||||
var red = Blockly.Dart.valueToCode(this, 'RED',
|
||||
Blockly.Dart.ORDER_NONE) || 0;
|
||||
var green = Blockly.Dart.valueToCode(this, 'GREEN',
|
||||
Blockly.Dart.ORDER_NONE) || 0;
|
||||
var blue = Blockly.Dart.valueToCode(this, 'BLUE',
|
||||
Blockly.Dart.ORDER_NONE) || 0;
|
||||
|
||||
if (!Blockly.Dart.definitions_['colour_rgb']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'colour_rgb', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.colour_rgb.functionName = functionName;
|
||||
var func = [];
|
||||
func.push('String ' + functionName + '(num r, num g, num b) {');
|
||||
func.push(' num rn = (Math.max(Math.min(r, 1), 0) * 255).round();');
|
||||
func.push(' String rs = rn.toInt().toRadixString(16);');
|
||||
func.push(' rs = \'0$rs\';');
|
||||
func.push(' rs = rs.substring(rs.length - 2);');
|
||||
func.push(' num gn = (Math.max(Math.min(g, 1), 0) * 255).round();');
|
||||
func.push(' String gs = gn.toInt().toRadixString(16);');
|
||||
func.push(' gs = \'0$gs\';');
|
||||
func.push(' gs = gs.substring(gs.length - 2);');
|
||||
func.push(' num bn = (Math.max(Math.min(b, 1), 0) * 255).round();');
|
||||
func.push(' String bs = bn.toInt().toRadixString(16);');
|
||||
func.push(' bs = \'0$bs\';');
|
||||
func.push(' bs = bs.substring(bs.length - 2);');
|
||||
func.push(' return \'#$rs$gs$bs\';');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['colour_rgb'] = func.join('\n');
|
||||
}
|
||||
var code = Blockly.Dart.colour_rgb.functionName +
|
||||
'(' + red + ', ' + green + ', ' + blue + ')';
|
||||
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_blend = function() {
|
||||
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, 1), 0) * 255).round();',
|
||||
' String rs = rn.toInt().toRadixString(16);',
|
||||
' rs = \'0$rs\';',
|
||||
' rs = rs.substring(rs.length - 2);',
|
||||
' num gn = (Math.max(Math.min(g, 1), 0) * 255).round();',
|
||||
' String gs = gn.toInt().toRadixString(16);',
|
||||
' gs = \'0$gs\';',
|
||||
' gs = gs.substring(gs.length - 2);',
|
||||
' num bn = (Math.max(Math.min(b, 1), 0) * 255).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(this, 'COLOUR1',
|
||||
var c1 = Blockly.Dart.valueToCode(block, 'COLOUR1',
|
||||
Blockly.Dart.ORDER_NONE) || '\'#000000\'';
|
||||
var c2 = Blockly.Dart.valueToCode(this, 'COLOUR2',
|
||||
var c2 = Blockly.Dart.valueToCode(block, 'COLOUR2',
|
||||
Blockly.Dart.ORDER_NONE) || '\'#000000\'';
|
||||
var ratio = Blockly.Dart.valueToCode(this, 'RATIO',
|
||||
var ratio = Blockly.Dart.valueToCode(block, 'RATIO',
|
||||
Blockly.Dart.ORDER_NONE) || 0.5;
|
||||
|
||||
if (!Blockly.Dart.definitions_['colour_blend']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'colour_blend', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.colour_blend.functionName = functionName;
|
||||
var func = [];
|
||||
func.push('String ' + functionName + '(String c1, String c2, num ratio) {');
|
||||
func.push(' ratio = Math.max(Math.min(ratio, 1), 0);');
|
||||
func.push(' int r1 = int.parse(\'0x${c1.substring(1, 3)}\');');
|
||||
func.push(' int g1 = int.parse(\'0x${c1.substring(3, 5)}\');');
|
||||
func.push(' int b1 = int.parse(\'0x${c1.substring(5, 7)}\');');
|
||||
func.push(' int r2 = int.parse(\'0x${c2.substring(1, 3)}\');');
|
||||
func.push(' int g2 = int.parse(\'0x${c2.substring(3, 5)}\');');
|
||||
func.push(' int b2 = int.parse(\'0x${c2.substring(5, 7)}\');');
|
||||
func.push(' num rn = (r1 * (1 - ratio) + r2 * ratio).round();');
|
||||
func.push(' String rs = rn.toInt().toRadixString(16);');
|
||||
func.push(' num gn = (g1 * (1 - ratio) + g2 * ratio).round();');
|
||||
func.push(' String gs = gn.toInt().toRadixString(16);');
|
||||
func.push(' num bn = (b1 * (1 - ratio) + b2 * ratio).round();');
|
||||
func.push(' String bs = bn.toInt().toRadixString(16);');
|
||||
func.push(' rs = \'0$rs\';');
|
||||
func.push(' rs = rs.substring(rs.length - 2);');
|
||||
func.push(' gs = \'0$gs\';');
|
||||
func.push(' gs = gs.substring(gs.length - 2);');
|
||||
func.push(' bs = \'0$bs\';');
|
||||
func.push(' bs = bs.substring(bs.length - 2);');
|
||||
func.push(' return \'#$rs$gs$bs\';');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['colour_blend'] = func.join('\n');
|
||||
}
|
||||
var code = Blockly.Dart.colour_blend.functionName +
|
||||
'(' + c1 + ', ' + c2 + ', ' + ratio + ')';
|
||||
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];
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* Copyright 2014 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -27,84 +27,69 @@ goog.provide('Blockly.Dart.lists');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart.addReservedWords('Math');
|
||||
|
||||
Blockly.Dart.lists_create_empty = function() {
|
||||
Blockly.Dart['lists_create_empty'] = function(block) {
|
||||
// Create an empty list.
|
||||
return ['[]', Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart.lists_create_with = function() {
|
||||
Blockly.Dart['lists_create_with'] = function(block) {
|
||||
// Create a list with any number of elements of any type.
|
||||
var code = new Array(this.itemCount_);
|
||||
for (var n = 0; n < this.itemCount_; n++) {
|
||||
code[n] = Blockly.Dart.valueToCode(this, 'ADD' + n,
|
||||
var code = new Array(block.itemCount_);
|
||||
for (var n = 0; n < block.itemCount_; n++) {
|
||||
code[n] = Blockly.Dart.valueToCode(block, 'ADD' + n,
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
}
|
||||
var code = '[' + code.join(', ') + ']';
|
||||
code = '[' + code.join(', ') + ']';
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart.lists_repeat = function() {
|
||||
Blockly.Dart['lists_repeat'] = function(block) {
|
||||
// Create a list with one element repeated.
|
||||
if (!Blockly.Dart.definitions_['lists_repeat']) {
|
||||
// Function adapted from Closure's goog.array.repeat.
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName('lists_repeat',
|
||||
Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.lists_repeat.repeat = functionName;
|
||||
var func = [];
|
||||
func.push('List ' + functionName + '(value, n) {');
|
||||
func.push(' var array = new List(n);');
|
||||
func.push(' for (int i = 0; i < n; i++) {');
|
||||
func.push(' array[i] = value;');
|
||||
func.push(' }');
|
||||
func.push(' return array;');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['lists_repeat'] = func.join('\n');
|
||||
}
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'ITEM',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'ITEM',
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'NUM',
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
var code = Blockly.Dart.lists_repeat.repeat +
|
||||
'(' + argument0 + ', ' + argument1 + ')';
|
||||
var code = 'new List.filled(' + argument1 + ', ' + argument0 + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.lists_length = function() {
|
||||
Blockly.Dart['lists_length'] = function(block) {
|
||||
// List length.
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
return [argument0 + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.lists_isEmpty = function() {
|
||||
Blockly.Dart['lists_isEmpty'] = function(block) {
|
||||
// Is the list empty?
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
return [argument0 + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.lists_indexOf = function() {
|
||||
Blockly.Dart['lists_indexOf'] = function(block) {
|
||||
// Find an item in the list.
|
||||
var operator = this.getTitleValue('END') == 'FIRST' ?
|
||||
var operator = block.getFieldValue('END') == 'FIRST' ?
|
||||
'indexOf' : 'lastIndexOf';
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'FIND',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'FIND',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
var code = argument1 + '.' + operator + '(' + argument0 + ') + 1';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.lists_getIndex = function() {
|
||||
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 = this.getTitleValue('MODE') || 'GET';
|
||||
var where = this.getTitleValue('WHERE') || 'FROM_START';
|
||||
var at = Blockly.Dart.valueToCode(this, 'AT',
|
||||
var mode = block.getFieldValue('MODE') || 'GET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var at = Blockly.Dart.valueToCode(block, 'AT',
|
||||
Blockly.Dart.ORDER_UNARY_PREFIX) || '1';
|
||||
var list = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
var list = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
|
||||
if (where == 'FIRST') {
|
||||
@@ -129,7 +114,7 @@ Blockly.Dart.lists_getIndex = function() {
|
||||
}
|
||||
} else if (where == 'FROM_START') {
|
||||
// Blockly uses one-based indicies.
|
||||
if (at.match(/^-?\d+$/)) {
|
||||
if (Blockly.isNumber(at)) {
|
||||
// If the index is a naked number, decrement it right now.
|
||||
at = parseInt(at, 10) - 1;
|
||||
} else {
|
||||
@@ -147,34 +132,24 @@ Blockly.Dart.lists_getIndex = function() {
|
||||
}
|
||||
} else if (where == 'FROM_END') {
|
||||
if (mode == 'GET') {
|
||||
if (!Blockly.Dart.definitions_['lists_get_from_end']) {
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'lists_get_from_end', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.lists_getIndex.lists_get_from_end = functionName;
|
||||
var func = [];
|
||||
func.push('dynamic ' + functionName + '(List myList, num x) {');
|
||||
func.push(' x = myList.length - x;');
|
||||
func.push(' return myList.removeAt(x);');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['lists_get_from_end'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.lists_getIndex.lists_get_from_end +
|
||||
'(' + list + ', ' + at + ')';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'lists_get_from_end',
|
||||
[ 'dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList, num x) {',
|
||||
' x = myList.length - x;',
|
||||
' return myList.removeAt(x);',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ', ' + at + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'GET_REMOVE' || mode == 'REMOVE') {
|
||||
if (!Blockly.Dart.definitions_['lists_remove_from_end']) {
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'lists_remove_from_end', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.lists_getIndex.lists_remove_from_end = functionName;
|
||||
var func = [];
|
||||
func.push('dynamic ' + functionName + '(List myList, num x) {');
|
||||
func.push(' x = myList.length - x;');
|
||||
func.push(' return myList.removeAt(x);');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['lists_remove_from_end'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.lists_getIndex.lists_remove_from_end +
|
||||
'(' + list + ', ' + at + ')';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'lists_remove_from_end',
|
||||
[ 'dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList, num x) {',
|
||||
' x = myList.length - x;',
|
||||
' return myList.removeAt(x);',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ', ' + at + ')';
|
||||
if (mode == 'GET_REMOVE') {
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'REMOVE') {
|
||||
@@ -182,25 +157,20 @@ Blockly.Dart.lists_getIndex = function() {
|
||||
}
|
||||
}
|
||||
} else if (where == 'RANDOM') {
|
||||
if (!Blockly.Dart.definitions_['lists_get_random_item']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'lists_get_random_item', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.lists_getIndex.random = functionName;
|
||||
var func = [];
|
||||
func.push('dynamic ' + functionName + '(List myList, bool remove) {');
|
||||
func.push(' int x = new Math.Random().nextInt(myList.length);');
|
||||
func.push(' if (remove) {');
|
||||
func.push(' return myList.removeAt(x);');
|
||||
func.push(' } else {');
|
||||
func.push(' return myList[x];');
|
||||
func.push(' }');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['lists_get_random_item'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.lists_getIndex.random +
|
||||
'(' + list + ', ' + (mode != 'GET') + ')';
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'lists_get_random_item',
|
||||
[ 'dynamic ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList, bool remove) {',
|
||||
' int x = new Math.Random().nextInt(myList.length);',
|
||||
' if (remove) {',
|
||||
' return myList.removeAt(x);',
|
||||
' } else {',
|
||||
' return myList[x];',
|
||||
' }',
|
||||
'}']);
|
||||
code = functionName + '(' + list + ', ' + (mode != 'GET') + ')';
|
||||
if (mode == 'GET' || mode == 'GET_REMOVE') {
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else if (mode == 'REMOVE') {
|
||||
@@ -210,16 +180,16 @@ Blockly.Dart.lists_getIndex = function() {
|
||||
throw 'Unhandled combination (lists_getIndex).';
|
||||
};
|
||||
|
||||
Blockly.Dart.lists_setIndex = function() {
|
||||
Blockly.Dart['lists_setIndex'] = function(block) {
|
||||
// Set element at index.
|
||||
// Note: Until February 2013 this block did not have MODE or WHERE inputs.
|
||||
var list = Blockly.Dart.valueToCode(this, 'LIST',
|
||||
var list = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '[]';
|
||||
var mode = this.getTitleValue('MODE') || 'GET';
|
||||
var where = this.getTitleValue('WHERE') || 'FROM_START';
|
||||
var at = Blockly.Dart.valueToCode(this, 'AT',
|
||||
var mode = block.getFieldValue('MODE') || 'GET';
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var at = Blockly.Dart.valueToCode(block, 'AT',
|
||||
Blockly.Dart.ORDER_ADDITIVE) || '1';
|
||||
var value = Blockly.Dart.valueToCode(this, 'TO',
|
||||
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'.
|
||||
@@ -237,7 +207,7 @@ Blockly.Dart.lists_setIndex = function() {
|
||||
if (mode == 'SET') {
|
||||
return list + '[0] = ' + value + ';\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.insertRange(0, 1, ' + value + ');\n';
|
||||
return list + '.insert(0, ' + value + ');\n';
|
||||
}
|
||||
} else if (where == 'LAST') {
|
||||
if (mode == 'SET') {
|
||||
@@ -245,11 +215,11 @@ Blockly.Dart.lists_setIndex = function() {
|
||||
code += list + '[' + list + '.length - 1] = ' + value + ';\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.addLast(' + value + ');\n';
|
||||
return list + '.add(' + value + ');\n';
|
||||
}
|
||||
} else if (where == 'FROM_START') {
|
||||
// Blockly uses one-based indicies.
|
||||
if (at.match(/^\d+$/)) {
|
||||
if (Blockly.isNumber(at)) {
|
||||
// If the index is a naked number, decrement it right now.
|
||||
at = parseInt(at, 10) - 1;
|
||||
} else {
|
||||
@@ -259,7 +229,7 @@ Blockly.Dart.lists_setIndex = function() {
|
||||
if (mode == 'SET') {
|
||||
return list + '[' + at + '] = ' + value + ';\n';
|
||||
} else if (mode == 'INSERT') {
|
||||
return list + '.insertRange(' + at + ', 1, ' + value + ');\n';
|
||||
return list + '.insert(' + at + ', ' + value + ');\n';
|
||||
}
|
||||
} else if (where == 'FROM_END') {
|
||||
var code = cacheList();
|
||||
@@ -267,7 +237,7 @@ Blockly.Dart.lists_setIndex = function() {
|
||||
code += list + '[' + list + '.length - ' + at + '] = ' + value + ';\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
code += list + '.insertRange(' + list + '.length - ' + at + ', 1, ' +
|
||||
code += list + '.insert(' + list + '.length - ' + at + ', ' +
|
||||
value + ');\n';
|
||||
return code;
|
||||
}
|
||||
@@ -282,9 +252,58 @@ Blockly.Dart.lists_setIndex = function() {
|
||||
code += list + '[' + xVar + '] = ' + value + ';\n';
|
||||
return code;
|
||||
} else if (mode == 'INSERT') {
|
||||
code += list + '.insertRange(' + xVar + ', 1, ' + value + ');\n';
|
||||
code += list + '.insert(' + xVar + ', ' + value + ');\n';
|
||||
return code;
|
||||
}
|
||||
}
|
||||
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');
|
||||
var at1 = Blockly.Dart.valueToCode(block, 'AT1',
|
||||
Blockly.Dart.ORDER_NONE) || '1';
|
||||
var at2 = Blockly.Dart.valueToCode(block, 'AT2',
|
||||
Blockly.Dart.ORDER_NONE) || '1';
|
||||
if ((where1 == 'FIRST' || where1 == 'FROM_START' && Blockly.isNumber(at1)) &&
|
||||
(where2 == 'LAST' || where2 == 'FROM_START' && Blockly.isNumber(at2))) {
|
||||
// Simple case that can be done inline.
|
||||
at1 = where1 == 'FIRST' ? 0 : parseInt(at1, 10) - 1;
|
||||
if (where2 == 'LAST') {
|
||||
code = list + '.sublist(' + at1 + ')';
|
||||
} else {
|
||||
at2 = parseInt(at2, 10);
|
||||
code = list + '.sublist(' + at1 + ', ' + at2 + ')';
|
||||
}
|
||||
} else {
|
||||
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_START\') {',
|
||||
' at--;',
|
||||
' } else if (where == \'FROM_END\') {',
|
||||
' at = list.length - at;',
|
||||
' } else if (where == \'FIRST\') {',
|
||||
' at = 0;',
|
||||
' } else if (where == \'LAST\') {',
|
||||
' at = list.length - 1;',
|
||||
' } else {',
|
||||
' 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];
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* Copyright 2014 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -27,64 +27,97 @@ goog.provide('Blockly.Dart.logic');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
Blockly.Dart.logic_compare = function() {
|
||||
|
||||
Blockly.Dart['controls_if'] = function(block) {
|
||||
// If/elseif/else condition.
|
||||
var n = 0;
|
||||
var argument = Blockly.Dart.valueToCode(block, 'IF' + n,
|
||||
Blockly.Dart.ORDER_NONE) || 'false';
|
||||
var branch = Blockly.Dart.statementToCode(block, 'DO' + n);
|
||||
var code = 'if (' + argument + ') {\n' + branch + '}';
|
||||
for (n = 1; n <= block.elseifCount_; n++) {
|
||||
argument = Blockly.Dart.valueToCode(block, 'IF' + n,
|
||||
Blockly.Dart.ORDER_NONE) || 'false';
|
||||
branch = Blockly.Dart.statementToCode(block, 'DO' + n);
|
||||
code += ' else if (' + argument + ') {\n' + branch + '}';
|
||||
}
|
||||
if (block.elseCount_) {
|
||||
branch = Blockly.Dart.statementToCode(block, 'ELSE');
|
||||
code += ' else {\n' + branch + '}';
|
||||
}
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
Blockly.Dart['logic_compare'] = function(block) {
|
||||
// Comparison operator.
|
||||
var mode = this.getTitleValue('OP');
|
||||
var operator = Blockly.Dart.logic_compare.OPERATORS[mode];
|
||||
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(this, 'A', order) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'B', order) || '0';
|
||||
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_compare.OPERATORS = {
|
||||
EQ: '==',
|
||||
NEQ: '!=',
|
||||
LT: '<',
|
||||
LTE: '<=',
|
||||
GT: '>',
|
||||
GTE: '>='
|
||||
};
|
||||
|
||||
Blockly.Dart.logic_operation = function() {
|
||||
Blockly.Dart['logic_operation'] = function(block) {
|
||||
// Operations 'and', 'or'.
|
||||
var operator = (this.getTitleValue('OP') == 'AND') ? '&&' : '||';
|
||||
var operator = (block.getFieldValue('OP') == 'AND') ? '&&' : '||';
|
||||
var order = (operator == '&&') ? Blockly.Dart.ORDER_LOGICAL_AND :
|
||||
Blockly.Dart.ORDER_LOGICAL_OR;
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'A', order) || 'false';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'B', order) || 'false';
|
||||
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() {
|
||||
Blockly.Dart['logic_negate'] = function(block) {
|
||||
// Negation.
|
||||
var order = Blockly.Dart.ORDER_UNARY_PREFIX;
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'BOOL', order) || 'false';
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'BOOL', order) || 'true';
|
||||
var code = '!' + argument0;
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Dart.logic_boolean = function() {
|
||||
Blockly.Dart['logic_boolean'] = function(block) {
|
||||
// Boolean values true and false.
|
||||
var code = (this.getTitleValue('BOOL') == 'TRUE') ? 'true' : 'false';
|
||||
var code = (block.getFieldValue('BOOL') == 'TRUE') ? 'true' : 'false';
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart.logic_null = function() {
|
||||
Blockly.Dart['logic_null'] = function(block) {
|
||||
// Null data type.
|
||||
return ['null', Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart.logic_ternary = function() {
|
||||
Blockly.Dart['logic_ternary'] = function(block) {
|
||||
// Ternary operator.
|
||||
var value_if = Blockly.Dart.valueToCode(this, 'IF',
|
||||
var value_if = Blockly.Dart.valueToCode(block, 'IF',
|
||||
Blockly.Dart.ORDER_CONDITIONAL) || 'false';
|
||||
var value_then = Blockly.Dart.valueToCode(this, 'THEN',
|
||||
var value_then = Blockly.Dart.valueToCode(block, 'THEN',
|
||||
Blockly.Dart.ORDER_CONDITIONAL) || 'null';
|
||||
var value_else = Blockly.Dart.valueToCode(this, 'ELSE',
|
||||
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];
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* Copyright 2014 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -18,42 +18,23 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Dart for control blocks.
|
||||
* @fileoverview Generating Dart for loop blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Blockly.Dart.control');
|
||||
goog.provide('Blockly.Dart.loops');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
Blockly.Dart.controls_if = function() {
|
||||
// If/elseif/else condition.
|
||||
var n = 0;
|
||||
var argument = Blockly.Dart.valueToCode(this, 'IF' + n,
|
||||
Blockly.Dart.ORDER_NONE) || 'false';
|
||||
var branch = Blockly.Dart.statementToCode(this, 'DO' + n);
|
||||
var code = 'if (' + argument + ') {\n' + branch + '}';
|
||||
for (n = 1; n <= this.elseifCount_; n++) {
|
||||
argument = Blockly.Dart.valueToCode(this, 'IF' + n,
|
||||
Blockly.Dart.ORDER_NONE) || 'false';
|
||||
branch = Blockly.Dart.statementToCode(this, 'DO' + n);
|
||||
code += ' else if (' + argument + ') {\n' + branch + '}';
|
||||
}
|
||||
if (this.elseCount_) {
|
||||
branch = Blockly.Dart.statementToCode(this, 'ELSE');
|
||||
code += ' else {\n' + branch + '}';
|
||||
}
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
Blockly.Dart.controls_repeat = function() {
|
||||
// Repeat n times.
|
||||
var repeats = Number(this.getTitleValue('TIMES'));
|
||||
var branch = Blockly.Dart.statementToCode(this, 'DO');
|
||||
Blockly.Dart['controls_repeat'] = function(block) {
|
||||
// Repeat n times (internal number).
|
||||
var repeats = Number(block.getFieldValue('TIMES'));
|
||||
var branch = Blockly.Dart.statementToCode(block, 'DO');
|
||||
if (Blockly.Dart.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.Dart.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + this.id + '\'') + branch;
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
var loopVar = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'count', Blockly.Variables.NAME_TYPE);
|
||||
@@ -64,91 +45,135 @@ Blockly.Dart.controls_repeat = function() {
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Dart.controls_whileUntil = function() {
|
||||
// Do while/until loop.
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'BOOL',
|
||||
Blockly.Dart.ORDER_NONE) || 'false';
|
||||
var branch = Blockly.Dart.statementToCode(this, 'DO');
|
||||
Blockly.Dart['controls_repeat_ext'] = function(block) {
|
||||
// Repeat n times (external number).
|
||||
var repeats = Blockly.Dart.valueToCode(block, 'TIMES',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
||||
var branch = Blockly.Dart.statementToCode(block, 'DO');
|
||||
if (Blockly.Dart.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.Dart.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + this.id + '\'') + branch;
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
if (this.getTitleValue('MODE') == 'UNTIL') {
|
||||
if (!argument0.match(/^\w+$/)) {
|
||||
argument0 = '(' + argument0 + ')';
|
||||
}
|
||||
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_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');
|
||||
if (Blockly.Dart.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.Dart.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
if (until) {
|
||||
argument0 = '!' + argument0;
|
||||
}
|
||||
return 'while (' + argument0 + ') {\n' + branch + '}\n';
|
||||
};
|
||||
|
||||
Blockly.Dart.controls_for = function() {
|
||||
Blockly.Dart['controls_for'] = function(block) {
|
||||
// For loop.
|
||||
var variable0 = Blockly.Dart.variableDB_.getName(
|
||||
this.getTitleValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'FROM',
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'FROM',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'TO',
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'TO',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
||||
var branch = Blockly.Dart.statementToCode(this, 'DO');
|
||||
var increment = Blockly.Dart.valueToCode(block, 'BY',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '1';
|
||||
var branch = Blockly.Dart.statementToCode(block, 'DO');
|
||||
if (Blockly.Dart.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.Dart.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + this.id + '\'') + branch;
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
var code;
|
||||
if (argument0.match(/^-?\d+(\.\d+)?$/) &&
|
||||
argument1.match(/^-?\d+(\.\d+)?$/)) {
|
||||
// Both arguments are simple numbers.
|
||||
if (Blockly.isNumber(argument0) && Blockly.isNumber(argument1) &&
|
||||
Blockly.isNumber(increment)) {
|
||||
// All arguments are simple numbers.
|
||||
var up = parseFloat(argument0) <= parseFloat(argument1);
|
||||
code = 'for (num ' + variable0 + ' = ' + argument0 + '; ' +
|
||||
code = 'for (' + variable0 + ' = ' + argument0 + '; ' +
|
||||
variable0 + (up ? ' <= ' : ' >= ') + argument1 + '; ' +
|
||||
variable0 + (up ? '++' : '--') + ') {\n' +
|
||||
branch + '}\n';
|
||||
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+$/) && !argument0.match(/^-?\d+(\.\d+)?$/)) {
|
||||
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+$/) && !argument1.match(/^-?\d+(\.\d+)?$/)) {
|
||||
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 += ' ' + incVar + ' = -' + incVar +';\n';
|
||||
code += '}\n';
|
||||
code += 'for (' + variable0 + ' = ' + startVar + ';\n' +
|
||||
' (' + startVar + ' <= ' + endVar + ') ? ' +
|
||||
' ' + incVar + ' >= 0 ? ' +
|
||||
variable0 + ' <= ' + endVar + ' : ' +
|
||||
variable0 + ' >= ' + endVar + ';\n' +
|
||||
' ' + variable0 + ' += (' + startVar + ' <= ' + endVar +
|
||||
') ? 1 : -1) {\n' +
|
||||
' ' + variable0 + ' += ' + incVar + ') {\n' +
|
||||
branch + '}\n';
|
||||
}
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Dart.controls_forEach = function() {
|
||||
Blockly.Dart['controls_forEach'] = function(block) {
|
||||
// For each loop.
|
||||
var variable0 = Blockly.Dart.variableDB_.getName(
|
||||
this.getTitleValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'LIST',
|
||||
block.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '[]';
|
||||
var branch = Blockly.Dart.statementToCode(this, 'DO');
|
||||
var branch = Blockly.Dart.statementToCode(block, 'DO');
|
||||
if (Blockly.Dart.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.Dart.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + this.id + '\'') + branch;
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
var code = 'for (var ' + variable0 + ' in ' + argument0 + ') {\n' +
|
||||
branch + '}\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Dart.controls_flow_statements = function() {
|
||||
Blockly.Dart['controls_flow_statements'] = function(block) {
|
||||
// Flow statements: continue, break.
|
||||
switch (this.getTitleValue('FLOW')) {
|
||||
switch (block.getFieldValue('FLOW')) {
|
||||
case 'BREAK':
|
||||
return 'break;\n';
|
||||
case 'CONTINUE':
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* Copyright 2014 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -27,11 +27,12 @@ goog.provide('Blockly.Dart.math');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart.addReservedWords('Math');
|
||||
|
||||
Blockly.Dart.math_number = function() {
|
||||
Blockly.Dart['math_number'] = function(block) {
|
||||
// Numeric value.
|
||||
var code = window.parseFloat(this.getTitleValue('NUM'));
|
||||
var code = window.parseFloat(block.getFieldValue('NUM'));
|
||||
// -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.
|
||||
var order = code < 0 ?
|
||||
@@ -39,14 +40,20 @@ Blockly.Dart.math_number = function() {
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Dart.math_arithmetic = function() {
|
||||
Blockly.Dart['math_arithmetic'] = function(block) {
|
||||
// Basic arithmetic operators, and power.
|
||||
var mode = this.getTitleValue('OP');
|
||||
var tuple = Blockly.Dart.math_arithmetic.OPERATORS[mode];
|
||||
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(this, 'A', order) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'B', order) || '0';
|
||||
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) {
|
||||
@@ -59,22 +66,14 @@ Blockly.Dart.math_arithmetic = function() {
|
||||
return [code, order];
|
||||
};
|
||||
|
||||
Blockly.Dart.math_arithmetic.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.
|
||||
};
|
||||
|
||||
Blockly.Dart.math_single = function() {
|
||||
Blockly.Dart['math_single'] = function(block) {
|
||||
// Math operators with single operand.
|
||||
var operator = this.getTitleValue('OP');
|
||||
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(this, 'NUM',
|
||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_UNARY_PREFIX) || '0';
|
||||
if (arg[0] == '-') {
|
||||
// --3 is not legal in Dart.
|
||||
@@ -86,16 +85,17 @@ Blockly.Dart.math_single = function() {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
if (operator == 'ABS' || operator.substring(0, 5) == 'ROUND') {
|
||||
arg = Blockly.Dart.valueToCode(this, 'NUM',
|
||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '0';
|
||||
} else if (operator == 'SIN' || operator == 'COS' || operator == 'TAN') {
|
||||
arg = Blockly.Dart.valueToCode(this, 'NUM',
|
||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE) || '0';
|
||||
} else {
|
||||
arg = Blockly.Dart.valueToCode(this, 'NUM',
|
||||
arg = Blockly.Dart.valueToCode(block, 'NUM',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
}
|
||||
// First, handle cases which generate values that don't need parentheses.
|
||||
// First, handle cases which generate values that don't need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'ABS':
|
||||
code = arg + '.abs()';
|
||||
@@ -134,7 +134,8 @@ Blockly.Dart.math_single = function() {
|
||||
if (code) {
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
}
|
||||
// Second, handle cases which generate values that may need parentheses.
|
||||
// Second, handle cases which generate values that may need parentheses
|
||||
// wrapping the code.
|
||||
switch (operator) {
|
||||
case 'LOG10':
|
||||
code = 'Math.log(' + arg + ') / Math.log(10)';
|
||||
@@ -154,66 +155,60 @@ Blockly.Dart.math_single = function() {
|
||||
return [code, Blockly.Dart.ORDER_MULTIPLICATIVE];
|
||||
};
|
||||
|
||||
Blockly.Dart.math_constant = function() {
|
||||
Blockly.Dart['math_constant'] = function(block) {
|
||||
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
|
||||
var constant = this.getTitleValue('CONSTANT');
|
||||
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 Blockly.Dart.math_constant.CONSTANTS[constant];
|
||||
return CONSTANTS[constant];
|
||||
};
|
||||
|
||||
Blockly.Dart.math_constant.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]
|
||||
};
|
||||
|
||||
Blockly.Dart.math_number_property = function() {
|
||||
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(this, 'NUMBER_TO_CHECK',
|
||||
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 = this.getTitleValue('PROPERTY');
|
||||
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.
|
||||
if (!Blockly.Dart.definitions_['isPrime']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'isPrime', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.logic_prime= functionName;
|
||||
var func = [];
|
||||
func.push('bool ' + functionName + '(n) {');
|
||||
func.push(' // http://en.wikipedia.org/wiki/Primality_test#Naive_methods');
|
||||
func.push(' if (n == 2 || n == 3) {');
|
||||
func.push(' return true;');
|
||||
func.push(' }');
|
||||
func.push(' // False if n is null, negative, is 1, or not whole.');
|
||||
func.push(' // And false if n is divisible by 2 or 3.');
|
||||
func.push(' if (n == null || n <= 1 || n % 1 != 0 || n % 2 == 0 ||' +
|
||||
' n % 3 == 0) {');
|
||||
func.push(' return false;');
|
||||
func.push(' }');
|
||||
func.push(' // Check all the numbers of form 6k +/- 1, up to sqrt(n).');
|
||||
func.push(' for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {');
|
||||
func.push(' if (n % (x - 1) == 0 || n % (x + 1) == 0) {');
|
||||
func.push(' return false;');
|
||||
func.push(' }');
|
||||
func.push(' }');
|
||||
func.push(' return true;');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['isPrime'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.logic_prime + '(' + number_to_check + ')';
|
||||
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) {
|
||||
@@ -233,7 +228,7 @@ Blockly.Dart.math_number_property = function() {
|
||||
code = number_to_check + ' < 0';
|
||||
break;
|
||||
case 'DIVISIBLE_BY':
|
||||
var divisor = Blockly.Dart.valueToCode(this, 'DIVISOR',
|
||||
var divisor = Blockly.Dart.valueToCode(block, 'DIVISOR',
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE);
|
||||
if (!divisor) {
|
||||
return ['false', Blockly.Python.ORDER_ATOMIC];
|
||||
@@ -244,215 +239,180 @@ Blockly.Dart.math_number_property = function() {
|
||||
return [code, Blockly.Dart.ORDER_EQUALITY];
|
||||
};
|
||||
|
||||
Blockly.Dart.math_change = function() {
|
||||
Blockly.Dart['math_change'] = function(block) {
|
||||
// Add to a variable in place.
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'DELTA',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'DELTA',
|
||||
Blockly.Dart.ORDER_ADDITIVE) || '0';
|
||||
var varName = Blockly.Dart.variableDB_.getName(this.getTitleValue('VAR'),
|
||||
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;
|
||||
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_trig'] = Blockly.Dart['math_single'];
|
||||
|
||||
Blockly.Dart.math_on_list = function() {
|
||||
Blockly.Dart['math_on_list'] = function(block) {
|
||||
// Math functions for lists.
|
||||
var func = this.getTitleValue('OP');
|
||||
var list = Blockly.Dart.valueToCode(this, 'LIST',
|
||||
var func = block.getFieldValue('OP');
|
||||
var list = Blockly.Dart.valueToCode(block, 'LIST',
|
||||
Blockly.Dart.ORDER_NONE) || '[]';
|
||||
var code;
|
||||
switch (func) {
|
||||
case 'SUM':
|
||||
if (!Blockly.Dart.definitions_['math_sum']) {
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'math_sum', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.math_on_list.math_sum = functionName;
|
||||
var func = [];
|
||||
func.push('num ' + functionName + '(List myList) {');
|
||||
func.push(' num sumVal = 0;');
|
||||
func.push(' myList.forEach((num entry) {sumVal += entry;});');
|
||||
func.push(' return sumVal;');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['math_sum'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.math_on_list.math_sum + '(' + list + ')';
|
||||
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':
|
||||
if (!Blockly.Dart.definitions_['math_min']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'math_min', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.math_on_list.math_min = functionName;
|
||||
var func = [];
|
||||
func.push('num ' + functionName + '(List myList) {');
|
||||
func.push(' if (myList.isEmpty) return null;');
|
||||
func.push(' num minVal = myList[0];');
|
||||
func.push(' myList.forEach((num entry) ' +
|
||||
'{minVal = Math.min(minVal, entry);});');
|
||||
func.push(' return minVal;');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['math_min'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.math_on_list.math_min + '(' + list + ')';
|
||||
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':
|
||||
if (!Blockly.Dart.definitions_['math_max']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'math_max', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.math_on_list.math_max = functionName;
|
||||
var func = [];
|
||||
func.push('num ' + functionName + '(List myList) {');
|
||||
func.push(' if (myList.isEmpty) return null;');
|
||||
func.push(' num maxVal = myList[0];');
|
||||
func.push(' myList.forEach((num entry) ' +
|
||||
'{maxVal = Math.max(maxVal, entry);});');
|
||||
func.push(' return maxVal;');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['math_max'] = func.join('\n');
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
}
|
||||
code = Blockly.Dart.math_on_list.math_max + '(' + list + ')';
|
||||
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.
|
||||
if (!Blockly.Dart.definitions_['math_average']) {
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'math_average', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.math_on_list.math_average = functionName;
|
||||
var func = [];
|
||||
func.push('num ' + functionName + '(List myList) {');
|
||||
func.push(' // First filter list for numbers only.');
|
||||
func.push(' List localList = new List.from(myList);');
|
||||
func.push(' localList.removeMatching((a) => a is! num);');
|
||||
func.push(' if (localList.isEmpty) return null;');
|
||||
func.push(' num sumVal = 0;');
|
||||
func.push(' localList.forEach((num entry) {sumVal += entry;});');
|
||||
func.push(' return sumVal / localList.length;');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['math_average'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.math_on_list.math_average + '(' + list + ')';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'math_average',
|
||||
[ 'num ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(List myList) {',
|
||||
' // First filter list for numbers only.',
|
||||
' List localList = new List.from(myList);',
|
||||
' localList.removeMatching((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':
|
||||
if (!Blockly.Dart.definitions_['math_median']) {
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'math_median', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.math_on_list.math_median = functionName;
|
||||
var func = [];
|
||||
func.push('num ' + functionName + '(List myList) {');
|
||||
func.push(' // First filter list for numbers only, then sort, ' +
|
||||
'then return middle value');
|
||||
func.push(' // or the average of two middle values if list has an ' +
|
||||
'even number of elements.');
|
||||
func.push(' List localList = new List.from(myList);');
|
||||
func.push(' localList.removeMatching((a) => a is! num);');
|
||||
func.push(' if (localList.isEmpty) return null;');
|
||||
func.push(' localList.sort((a, b) => (a - b));');
|
||||
func.push(' int index = localList.length ~/ 2;');
|
||||
func.push(' if (localList.length % 2 == 1) {');
|
||||
func.push(' return localList[index];');
|
||||
func.push(' } else {');
|
||||
func.push(' return (localList[index - 1] + localList[index]) / 2;');
|
||||
func.push(' }');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['math_median'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.math_on_list.math_median + '(' + list + ')';
|
||||
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.removeMatching((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':
|
||||
if (!Blockly.Dart.definitions_['math_modes']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'math_modes', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.math_on_list.math_modes = functionName;
|
||||
// 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 func = [];
|
||||
func.push('List ' + functionName + '(values) {');
|
||||
func.push(' List modes = [];');
|
||||
func.push(' List counts = [];');
|
||||
func.push(' int maxCount = 0;');
|
||||
func.push(' for (int i = 0; i < values.length; i++) {');
|
||||
func.push(' var value = values[i];');
|
||||
func.push(' bool found = false;');
|
||||
func.push(' int thisCount;');
|
||||
func.push(' for (int j = 0; j < counts.length; j++) {');
|
||||
func.push(' if (counts[j][0] == value) {');
|
||||
func.push(' thisCount = ++counts[j][1];');
|
||||
func.push(' found = true;');
|
||||
func.push(' break;');
|
||||
func.push(' }');
|
||||
func.push(' }');
|
||||
func.push(' if (!found) {');
|
||||
func.push(' counts.add([value, 1]);');
|
||||
func.push(' thisCount = 1;');
|
||||
func.push(' }');
|
||||
func.push(' maxCount = Math.max(thisCount, maxCount);');
|
||||
func.push(' }');
|
||||
func.push(' for (int j = 0; j < counts.length; j++) {');
|
||||
func.push(' if (counts[j][1] == maxCount) {');
|
||||
func.push(' modes.add(counts[j][0]);');
|
||||
func.push(' }');
|
||||
func.push(' }');
|
||||
func.push(' return modes;');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['math_modes'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.math_on_list.math_modes + '(' + list + ')';
|
||||
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':
|
||||
if (!Blockly.Dart.definitions_['math_standard_deviation']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'math_standard_deviation', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.math_on_list.math_standard_deviation = functionName;
|
||||
var func = [];
|
||||
func.push('num ' + functionName + '(List myList) {');
|
||||
func.push(' // First filter list for numbers only.');
|
||||
func.push(' List numbers = new List.from(myList);');
|
||||
func.push(' numbers.removeMatching((a) => a is! num);');
|
||||
func.push(' if (numbers.isEmpty) return null;');
|
||||
func.push(' num n = numbers.length;');
|
||||
func.push(' num sum = 0;');
|
||||
func.push(' numbers.forEach((x) => sum += x);');
|
||||
func.push(' num mean = sum / n;');
|
||||
func.push(' num sumSquare = 0;');
|
||||
func.push(' numbers.forEach((x) => sumSquare += ' +
|
||||
'Math.pow(x - mean, 2));');
|
||||
func.push(' return Math.sqrt(sumSquare / n);');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['math_standard_deviation'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.math_on_list.math_standard_deviation +
|
||||
'(' + list + ')';
|
||||
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.removeMatching((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':
|
||||
if (!Blockly.Dart.definitions_['math_random_item']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'math_random_item', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.math_on_list.math_random_item = functionName;
|
||||
var func = [];
|
||||
func.push('dynamic ' + functionName + '(List myList) {');
|
||||
func.push(' int x = new Math.Random().nextInt(myList.length);');
|
||||
func.push(' return myList[x];');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['math_random_item'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.math_on_list.math_random_item + '(' + list + ')';
|
||||
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;
|
||||
@@ -460,57 +420,57 @@ Blockly.Dart.math_on_list = function() {
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.math_modulo = function() {
|
||||
Blockly.Dart['math_modulo'] = function(block) {
|
||||
// Remainder computation.
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'DIVIDEND',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'DIVIDEND',
|
||||
Blockly.Dart.ORDER_MULTIPLICATIVE) || '0';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'DIVISOR',
|
||||
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() {
|
||||
Blockly.Dart['math_constrain'] = function(block) {
|
||||
// Constrain a number between two limits.
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
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(this, 'LOW',
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'LOW',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
var argument2 = Blockly.Dart.valueToCode(this, 'HIGH',
|
||||
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() {
|
||||
Blockly.Dart['math_random_int'] = function(block) {
|
||||
// Random integer between [X] and [Y].
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'FROM',
|
||||
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(this, 'TO',
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'TO',
|
||||
Blockly.Dart.ORDER_NONE) || '0';
|
||||
if (!Blockly.Dart.definitions_['math_random_int']) {
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'math_random_int', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.math_random_int.random_function = functionName;
|
||||
var func = [];
|
||||
func.push('int ' + functionName + '(num a, num b) {');
|
||||
func.push(' if (a > b) {');
|
||||
func.push(' // Swap a and b to ensure a is smaller.');
|
||||
func.push(' num c = a;');
|
||||
func.push(' a = b;');
|
||||
func.push(' b = c;');
|
||||
func.push(' }');
|
||||
func.push(' return new Math.Random().nextInt(b - a + 1) + a;');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['math_random_int'] = func.join('\n');
|
||||
}
|
||||
var code = Blockly.Dart.math_random_int.random_function +
|
||||
'(' + argument0 + ', ' + argument1 + ')';
|
||||
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() {
|
||||
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];
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* Copyright 2014 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -18,7 +18,7 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Generating Dart for variable blocks.
|
||||
* @fileoverview Generating Dart for procedure blocks.
|
||||
* @author fraser@google.com (Neil Fraser)
|
||||
*/
|
||||
'use strict';
|
||||
@@ -27,70 +27,71 @@ goog.provide('Blockly.Dart.procedures');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
Blockly.Dart.procedures_defreturn = function() {
|
||||
|
||||
Blockly.Dart['procedures_defreturn'] = function(block) {
|
||||
// Define a procedure with a return value.
|
||||
var funcName = Blockly.Dart.variableDB_.getName(this.getTitleValue('NAME'),
|
||||
var funcName = Blockly.Dart.variableDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE);
|
||||
var branch = Blockly.Dart.statementToCode(this, 'STACK');
|
||||
var branch = Blockly.Dart.statementToCode(block, 'STACK');
|
||||
if (Blockly.Dart.INFINITE_LOOP_TRAP) {
|
||||
branch = Blockly.Dart.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + this.id + '\'') + branch;
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
var returnValue = Blockly.Dart.valueToCode(this, 'RETURN',
|
||||
var returnValue = Blockly.Dart.valueToCode(block, 'RETURN',
|
||||
Blockly.Dart.ORDER_NONE) || '';
|
||||
if (returnValue) {
|
||||
returnValue = ' return ' + returnValue + ';\n';
|
||||
}
|
||||
var returnType = returnValue ? 'dynamic' : 'void';
|
||||
var args = [];
|
||||
for (var x = 0; x < this.arguments_.length; x++) {
|
||||
args[x] = Blockly.Dart.variableDB_.getName(this.arguments_[x],
|
||||
for (var x = 0; x < block.arguments_.length; x++) {
|
||||
args[x] = Blockly.Dart.variableDB_.getName(block.arguments_[x],
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
}
|
||||
var code = returnType + ' ' + funcName + '(' + args.join(', ') + ') {\n' +
|
||||
branch + returnValue + '}';
|
||||
code = Blockly.Dart.scrub_(this, code);
|
||||
code = Blockly.Dart.scrub_(block, code);
|
||||
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_defnoreturn'] = Blockly.Dart['procedures_defreturn'];
|
||||
|
||||
Blockly.Dart.procedures_callreturn = function() {
|
||||
Blockly.Dart['procedures_callreturn'] = function(block) {
|
||||
// Call a procedure with a return value.
|
||||
var funcName = Blockly.Dart.variableDB_.getName(this.getTitleValue('NAME'),
|
||||
var funcName = Blockly.Dart.variableDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var x = 0; x < this.arguments_.length; x++) {
|
||||
args[x] = Blockly.Dart.valueToCode(this, 'ARG' + x,
|
||||
for (var x = 0; x < block.arguments_.length; x++) {
|
||||
args[x] = Blockly.Dart.valueToCode(block, 'ARG' + x,
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ')';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.procedures_callnoreturn = function() {
|
||||
Blockly.Dart['procedures_callnoreturn'] = function(block) {
|
||||
// Call a procedure with no return value.
|
||||
var funcName = Blockly.Dart.variableDB_.getName(this.getTitleValue('NAME'),
|
||||
var funcName = Blockly.Dart.variableDB_.getName(block.getFieldValue('NAME'),
|
||||
Blockly.Procedures.NAME_TYPE);
|
||||
var args = [];
|
||||
for (var x = 0; x < this.arguments_.length; x++) {
|
||||
args[x] = Blockly.Dart.valueToCode(this, 'ARG' + x,
|
||||
for (var x = 0; x < block.arguments_.length; x++) {
|
||||
args[x] = Blockly.Dart.valueToCode(block, 'ARG' + x,
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
}
|
||||
var code = funcName + '(' + args.join(', ') + ');\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
Blockly.Dart.procedures_ifreturn = function() {
|
||||
Blockly.Dart['procedures_ifreturn'] = function(block) {
|
||||
// Conditionally return value from a procedure.
|
||||
var condition = Blockly.Dart.valueToCode(this, 'CONDITION',
|
||||
var condition = Blockly.Dart.valueToCode(block, 'CONDITION',
|
||||
Blockly.Dart.ORDER_NONE) || 'false';
|
||||
var code = 'if (' + condition + ') {\n';
|
||||
if (this.hasReturnValue_) {
|
||||
var value = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
if (block.hasReturnValue_) {
|
||||
var value = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_NONE) || 'null';
|
||||
code += ' return ' + value + ';\n';
|
||||
} else {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* Copyright 2014 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -27,28 +27,29 @@ goog.provide('Blockly.Dart.text');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
|
||||
Blockly.Dart.addReservedWords('Html,Math');
|
||||
|
||||
Blockly.Dart.text = function() {
|
||||
Blockly.Dart['text'] = function(block) {
|
||||
// Text value.
|
||||
var code = Blockly.Dart.quote_(this.getTitleValue('TEXT'));
|
||||
var code = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart.text_join = function() {
|
||||
Blockly.Dart['text_join'] = function(block) {
|
||||
// Create a string made up of any number of elements of any type.
|
||||
var code;
|
||||
if (this.itemCount_ == 0) {
|
||||
if (block.itemCount_ == 0) {
|
||||
return ['\'\'', Blockly.Dart.ORDER_ATOMIC];
|
||||
} else if (this.itemCount_ == 1) {
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'ADD0',
|
||||
} else if (block.itemCount_ == 1) {
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'ADD0',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
code = argument0 + '.toString()';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
} else {
|
||||
code = new Array(this.itemCount_);
|
||||
for (var n = 0; n < this.itemCount_; n++) {
|
||||
code[n] = Blockly.Dart.valueToCode(this, 'ADD' + n,
|
||||
code = new Array(block.itemCount_);
|
||||
for (var n = 0; n < block.itemCount_; n++) {
|
||||
code[n] = Blockly.Dart.valueToCode(block, 'ADD' + n,
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
}
|
||||
code = '[' + code.join(',') + '].join()';
|
||||
@@ -56,80 +57,48 @@ Blockly.Dart.text_join = function() {
|
||||
}
|
||||
};
|
||||
|
||||
Blockly.Dart.text_append = function() {
|
||||
Blockly.Dart['text_append'] = function(block) {
|
||||
// Append to a variable in place.
|
||||
var varName = Blockly.Dart.variableDB_.getName(this.getTitleValue('VAR'),
|
||||
var varName = Blockly.Dart.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'TEXT',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
return varName + ' = [' + varName + ', ' + argument0 + '].join();\n';
|
||||
};
|
||||
|
||||
Blockly.Dart.text_length = function() {
|
||||
Blockly.Dart['text_length'] = function(block) {
|
||||
// String length.
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
return [argument0 + '.length', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.text_isEmpty = function() {
|
||||
Blockly.Dart['text_isEmpty'] = function(block) {
|
||||
// Is the string null?
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
return [argument0 + '.isEmpty', Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.text_endString = function() {
|
||||
// Return a leading or trailing substring.
|
||||
var first = this.getTitleValue('END') == 'FIRST';
|
||||
var code;
|
||||
if (first) {
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'NUM',
|
||||
Blockly.Dart.ORDER_NONE) || '1';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
code = argument1 + '.substring(0, ' + argument0 + ')';
|
||||
} else {
|
||||
if (!Blockly.Dart.definitions_['text_tailString']) {
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'text_tailString', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.text_endString.text_tailString = functionName;
|
||||
var func = [];
|
||||
func.push('String ' + functionName + '(n, myString) {');
|
||||
func.push(' // Return a trailing substring of n characters.');
|
||||
func.push(' return myString.substring(myString.length - n);');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['text_tailString'] = func.join('\n');
|
||||
}
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'NUM',
|
||||
Blockly.Dart.ORDER_NONE) || '1';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
code = Blockly.Dart.text_endString.text_tailString +
|
||||
'(' + argument0 + ', ' + argument1 + ')';
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.text_indexOf = function() {
|
||||
Blockly.Dart['text_indexOf'] = function(block) {
|
||||
// Search the text for a substring.
|
||||
var operator = this.getTitleValue('END') == 'FIRST' ?
|
||||
var operator = block.getFieldValue('END') == 'FIRST' ?
|
||||
'indexOf' : 'lastIndexOf';
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'FIND',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'FIND',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
var argument1 = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
var argument1 = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
var code = argument1 + '.' + operator + '(' + argument0 + ') + 1';
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.text_charAt = function() {
|
||||
Blockly.Dart['text_charAt'] = function(block) {
|
||||
// Get letter at index.
|
||||
// Note: Until January 2013 this block did not have the WHERE input.
|
||||
var where = this.getTitleValue('WHERE') || 'FROM_START';
|
||||
var at = Blockly.Dart.valueToCode(this, 'AT',
|
||||
var where = block.getFieldValue('WHERE') || 'FROM_START';
|
||||
var at = Blockly.Dart.valueToCode(block, 'AT',
|
||||
Blockly.Dart.ORDER_NONE) || '1';
|
||||
var text = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
var text = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
switch (where) {
|
||||
case 'FIRST':
|
||||
@@ -150,115 +119,138 @@ Blockly.Dart.text_charAt = function() {
|
||||
at = 1;
|
||||
// Fall through.
|
||||
case 'FROM_END':
|
||||
if (!Blockly.Dart.definitions_['text_get_from_end']) {
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'text_get_from_end', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.text_charAt.text_get_from_end = functionName;
|
||||
var func = [];
|
||||
func.push('String ' + functionName + '(String text, num x) {');
|
||||
func.push(' return text[text.length - x];');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['text_get_from_end'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.text_charAt.text_get_from_end +
|
||||
'(' + text + ', ' + at + ')';
|
||||
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':
|
||||
if (!Blockly.Dart.definitions_['text_random_letter']) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'text_random_letter', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.text_charAt.text_random_letter = functionName;
|
||||
var func = [];
|
||||
func.push('String ' + functionName + '(String text) {');
|
||||
func.push(' int x = new Math.Random().nextInt(text.length);');
|
||||
func.push(' return text[x];');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['text_random_letter'] = func.join('\n');
|
||||
}
|
||||
code = Blockly.Dart.text_charAt.text_random_letter +
|
||||
'(' + text + ')';
|
||||
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_changeCase = function() {
|
||||
// Change capitalization.
|
||||
var mode = this.getTitleValue('CASE');
|
||||
var operator = Blockly.Dart.text_changeCase.OPERATORS[mode];
|
||||
var code;
|
||||
if (operator) {
|
||||
// Upper and lower case are functions built into Dart.
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
code = argument0 + operator;
|
||||
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');
|
||||
var at1 = Blockly.Dart.valueToCode(block, 'AT1',
|
||||
Blockly.Dart.ORDER_NONE) || '1';
|
||||
var at2 = Blockly.Dart.valueToCode(block, 'AT2',
|
||||
Blockly.Dart.ORDER_NONE) || '1';
|
||||
if (where1 == 'FIRST' && where2 == 'LAST') {
|
||||
var code = text;
|
||||
} else {
|
||||
if (!Blockly.Dart.definitions_['toTitleCase']) {
|
||||
// Title case is not a native Dart function. Define one.
|
||||
var functionName = Blockly.Dart.variableDB_.getDistinctName(
|
||||
'text_toTitleCase', Blockly.Generator.NAME_TYPE);
|
||||
Blockly.Dart.text_changeCase.toTitleCase = functionName;
|
||||
var func = [];
|
||||
func.push('String ' + functionName + '(str) {');
|
||||
func.push(' RegExp exp = new RegExp(r\'\\b\');');
|
||||
func.push(' List<String> list = str.split(exp);');
|
||||
func.push(' final title = new StringBuffer();');
|
||||
func.push(' for (String part in list) {');
|
||||
func.push(' if (part.length > 0) {');
|
||||
func.push(' title.write(part[0].toUpperCase());');
|
||||
func.push(' if (part.length > 0) {');
|
||||
func.push(' title.write(part.substring(1).toLowerCase());');
|
||||
func.push(' }');
|
||||
func.push(' }');
|
||||
func.push(' }');
|
||||
func.push(' return title.toString();');
|
||||
func.push('}');
|
||||
Blockly.Dart.definitions_['toTitleCase'] = func.join('\n');
|
||||
}
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
code = Blockly.Dart.text_changeCase.toTitleCase + '(' + argument0 + ')';
|
||||
var functionName = Blockly.Dart.provideFunction_(
|
||||
'text_get_substring',
|
||||
[ 'function ' + Blockly.Dart.FUNCTION_NAME_PLACEHOLDER_ +
|
||||
'(text, where1, at1, where2, at2) {',
|
||||
' function getAt(where, at) {',
|
||||
' if (where == \'FROM_START\') {',
|
||||
' at--;',
|
||||
' } else if (where == \'FROM_END\') {',
|
||||
' at = text.length - at;',
|
||||
' } else if (where == \'FIRST\') {',
|
||||
' at = 0;',
|
||||
' } else if (where == \'LAST\') {',
|
||||
' at = text.length - 1;',
|
||||
' } else {',
|
||||
' 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.OPERATORS = {
|
||||
UPPERCASE: '.toUpperCase()',
|
||||
LOWERCASE: '.toLowerCase()',
|
||||
TITLECASE: null
|
||||
Blockly.Dart['text_changeCase'] = function(block) {
|
||||
// Change capitalization.
|
||||
var OPERATORS = {
|
||||
UPPERCASE: '.toUpperCase()',
|
||||
LOWERCASE: '.toLowerCase()',
|
||||
TITLECASE: null
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('CASE')];
|
||||
var code;
|
||||
if (operator) {
|
||||
// Upper and lower case are functions built into Dart.
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
code = argument0 + 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 argument0 = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
code = functionName + '(' + argument0 + ')';
|
||||
}
|
||||
return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.text_trim = function() {
|
||||
Blockly.Dart['text_trim'] = function(block) {
|
||||
// Trim spaces.
|
||||
var mode = this.getTitleValue('MODE');
|
||||
var operator = Blockly.Dart.text_trim.OPERATORS[mode];
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'TEXT',
|
||||
var OPERATORS = {
|
||||
LEFT: '.replaceFirst(new RegExp(r\'^\\s+\'), \'\')',
|
||||
RIGHT: '.replaceFirst(new RegExp(r\'\\s+$\'), \'\')',
|
||||
BOTH: '.trim()'
|
||||
};
|
||||
var operator = OPERATORS[block.getFieldValue('MODE')];
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
|
||||
return [argument0 + operator, Blockly.Dart.ORDER_UNARY_POSTFIX];
|
||||
};
|
||||
|
||||
Blockly.Dart.text_trim.OPERATORS = {
|
||||
LEFT: '.replaceFirst(new RegExp(r\'^\\s+\'), \'\')',
|
||||
RIGHT: '.replaceFirst(new RegExp(r\'\\s+$\'), \'\')',
|
||||
BOTH: '.trim()'
|
||||
};
|
||||
|
||||
Blockly.Dart.text_print = function() {
|
||||
Blockly.Dart['text_print'] = function(block) {
|
||||
// Print statement.
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'TEXT',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'TEXT',
|
||||
Blockly.Dart.ORDER_NONE) || '\'\'';
|
||||
return 'print(' + argument0 + ');\n';
|
||||
};
|
||||
|
||||
Blockly.Dart.text_prompt = function() {
|
||||
Blockly.Dart['text_prompt'] = function(block) {
|
||||
// Prompt function.
|
||||
Blockly.Dart.definitions_['import_dart_html'] =
|
||||
'import \'dart:html\' as Html;';
|
||||
var msg = Blockly.Dart.quote_(this.getTitleValue('TEXT'));
|
||||
var msg = Blockly.Dart.quote_(block.getFieldValue('TEXT'));
|
||||
var code = 'Html.window.prompt(' + msg + ', \'\')';
|
||||
var toNumber = this.getTitleValue('TYPE') == 'NUMBER';
|
||||
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
|
||||
if (toNumber) {
|
||||
Blockly.Dart.definitions_['import_dart_math'] =
|
||||
'import \'dart:math\' as Math;';
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Visual Blocks Language
|
||||
*
|
||||
* Copyright 2012 Google Inc.
|
||||
* Copyright 2014 Google Inc.
|
||||
* http://blockly.googlecode.com/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -27,18 +27,19 @@ goog.provide('Blockly.Dart.variables');
|
||||
|
||||
goog.require('Blockly.Dart');
|
||||
|
||||
Blockly.Dart.variables_get = function() {
|
||||
|
||||
Blockly.Dart['variables_get'] = function(block) {
|
||||
// Variable getter.
|
||||
var code = Blockly.Dart.variableDB_.getName(this.getTitleValue('VAR'),
|
||||
var code = Blockly.Dart.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return [code, Blockly.Dart.ORDER_ATOMIC];
|
||||
};
|
||||
|
||||
Blockly.Dart.variables_set = function() {
|
||||
Blockly.Dart['variables_set'] = function(block) {
|
||||
// Variable setter.
|
||||
var argument0 = Blockly.Dart.valueToCode(this, 'VALUE',
|
||||
var argument0 = Blockly.Dart.valueToCode(block, 'VALUE',
|
||||
Blockly.Dart.ORDER_ASSIGNMENT) || '0';
|
||||
var varName = Blockly.Dart.variableDB_.getName(this.getTitleValue('VAR'),
|
||||
var varName = Blockly.Dart.variableDB_.getName(block.getFieldValue('VAR'),
|
||||
Blockly.Variables.NAME_TYPE);
|
||||
return varName + ' = ' + argument0 + ';\n';
|
||||
};
|
||||
|
||||
@@ -67,7 +67,7 @@ Blockly.JavaScript['lists_repeat'] = function(block) {
|
||||
Blockly.JavaScript['lists_length'] = function(block) {
|
||||
// List length.
|
||||
var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
|
||||
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '\'\'';
|
||||
Blockly.JavaScript.ORDER_FUNCTION_CALL) || '[]';
|
||||
return [argument0 + '.length', Blockly.JavaScript.ORDER_MEMBER];
|
||||
};
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ Blockly.JavaScript['controls_if'] = function(block) {
|
||||
}
|
||||
if (block.elseCount_) {
|
||||
branch = Blockly.JavaScript.statementToCode(block, 'ELSE');
|
||||
code += ' else {\n' + branch + '}\n';
|
||||
code += ' else {\n' + branch + '}';
|
||||
}
|
||||
return code + '\n';
|
||||
};
|
||||
|
||||
@@ -166,25 +166,12 @@ Blockly.JavaScript['controls_forEach'] = function(block) {
|
||||
branch = Blockly.JavaScript.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'\'' + block.id + '\'') + branch;
|
||||
}
|
||||
var code;
|
||||
var indexVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
variable0 + '_index', Blockly.Variables.NAME_TYPE);
|
||||
if (argument0.match(/^\w+$/)) {
|
||||
branch = ' ' + variable0 + ' = ' + argument0 + '[' + indexVar + '];\n' +
|
||||
branch;
|
||||
code = 'for (var ' + indexVar + ' in ' + argument0 + ') {\n' +
|
||||
branch + '}\n';
|
||||
} else {
|
||||
// The list appears to be more complicated than a simple variable.
|
||||
// Cache it to a variable to prevent repeated look-ups.
|
||||
var listVar = Blockly.JavaScript.variableDB_.getDistinctName(
|
||||
variable0 + '_list', Blockly.Variables.NAME_TYPE);
|
||||
branch = ' ' + variable0 + ' = ' + listVar + '[' + indexVar + '];\n' +
|
||||
branch;
|
||||
code = 'var ' + listVar + ' = ' + argument0 + ';\n' +
|
||||
'for (var ' + indexVar + ' in ' + listVar + ') {\n' +
|
||||
branch + '}\n';
|
||||
}
|
||||
branch = ' ' + variable0 + ' = ' + argument0 + '[' + indexVar + '];\n' +
|
||||
branch;
|
||||
var code = 'for (var ' + indexVar + ' in ' + argument0 + ') {\n' +
|
||||
branch + '}\n';
|
||||
return code;
|
||||
};
|
||||
|
||||
|
||||
@@ -143,7 +143,7 @@ Blockly.JavaScript['text_charAt'] = function(block) {
|
||||
Blockly.JavaScript['text_getSubstring'] = function(block) {
|
||||
// Get substring.
|
||||
var text = Blockly.JavaScript.valueToCode(block, 'STRING',
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '[]';
|
||||
Blockly.JavaScript.ORDER_MEMBER) || '\'\'';
|
||||
var where1 = block.getFieldValue('WHERE1');
|
||||
var where2 = block.getFieldValue('WHERE2');
|
||||
var at1 = Blockly.JavaScript.valueToCode(block, 'AT1',
|
||||
|
||||
@@ -73,10 +73,7 @@ Blockly.Python['controls_whileUntil'] = function(block) {
|
||||
branch = Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g,
|
||||
'"' + block.id + '"') + branch;
|
||||
}
|
||||
if (block.getFieldValue('MODE') == 'UNTIL') {
|
||||
if (!argument0.match(/^\w+$/)) {
|
||||
argument0 = '(' + argument0 + ')';
|
||||
}
|
||||
if (until) {
|
||||
argument0 = 'not ' + argument0;
|
||||
}
|
||||
return 'while ' + argument0 + ':\n' + branch;
|
||||
|
||||
Reference in New Issue
Block a user