forked from google/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aseba_compressed.js
90 lines (88 loc) · 31 KB
/
aseba_compressed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Do not edit this file; automatically generated by build.py.
"use strict";
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.ASEBA=new Blockly.Generator("ASEBA");Blockly.ASEBA.addReservedWords("abs, call, callsub, do, else, elseif, end, for, if, in, onevent, return, step, sub, then, var, when, while");Blockly.ASEBA.ORDER_ATOMIC=0;Blockly.ASEBA.ORDER_MEMBER=1;Blockly.ASEBA.ORDER_NEW=1;Blockly.ASEBA.ORDER_FUNCTION_CALL=2;Blockly.ASEBA.ORDER_INCREMENT=3;Blockly.ASEBA.ORDER_DECREMENT=3;Blockly.ASEBA.ORDER_LOGICAL_NOT=4;Blockly.ASEBA.ORDER_BITWISE_NOT=4;Blockly.ASEBA.ORDER_UNARY_PLUS=4;
Blockly.ASEBA.ORDER_UNARY_NEGATION=4;Blockly.ASEBA.ORDER_TYPEOF=4;Blockly.ASEBA.ORDER_VOID=4;Blockly.ASEBA.ORDER_DELETE=4;Blockly.ASEBA.ORDER_MULTIPLICATION=5;Blockly.ASEBA.ORDER_DIVISION=5;Blockly.ASEBA.ORDER_MODULUS=5;Blockly.ASEBA.ORDER_ADDITION=6;Blockly.ASEBA.ORDER_SUBTRACTION=6;Blockly.ASEBA.ORDER_BITWISE_SHIFT=7;Blockly.ASEBA.ORDER_RELATIONAL=8;Blockly.ASEBA.ORDER_IN=8;Blockly.ASEBA.ORDER_INSTANCEOF=8;Blockly.ASEBA.ORDER_EQUALITY=9;Blockly.ASEBA.ORDER_BITWISE_AND=10;
Blockly.ASEBA.ORDER_BITWISE_XOR=11;Blockly.ASEBA.ORDER_BITWISE_OR=12;Blockly.ASEBA.ORDER_LOGICAL_AND=13;Blockly.ASEBA.ORDER_LOGICAL_OR=14;Blockly.ASEBA.ORDER_CONDITIONAL=15;Blockly.ASEBA.ORDER_ASSIGNMENT=16;Blockly.ASEBA.ORDER_COMMA=17;Blockly.ASEBA.ORDER_NONE=99;
Blockly.ASEBA.init=function(){Blockly.ASEBA.definitions_=Object.create(null);Blockly.ASEBA.functionNames_=Object.create(null);Blockly.ASEBA.internalVariables_={};Blockly.ASEBA.variableDB_?Blockly.ASEBA.variableDB_.reset():Blockly.ASEBA.variableDB_=new Blockly.Names(Blockly.ASEBA.RESERVED_WORDS_);for(var a=[],b=Blockly.Variables.allVariables(),c=0;c<b.length;c++)a[c]="var "+Blockly.ASEBA.variableDB_.getName(b[c],Blockly.Variables.NAME_TYPE);Blockly.ASEBA.definitions_.variables=a.join("\n")};
Blockly.ASEBA.finish=function(a){var b=[],c;for(c in Blockly.ASEBA.internalVariables_)b.push("var "+c);Blockly.ASEBA.definitions_.aseba_int_variables=b.join("\n");var b=[],d;for(d in Blockly.ASEBA.definitions_)b.push(Blockly.ASEBA.definitions_[d]);return b.join("\n\n")+"\n\n\n"+a};Blockly.ASEBA.scrubNakedValue=function(a){return a+";\n"};Blockly.ASEBA.quote_=function(a){a=a.replace(/\\/g,"\\\\").replace(/\n/g,"\\\n").replace(/'/g,"\\'");return"'"+a+"'"};
Blockly.ASEBA.scrub_=function(a,b){var c="";if(!a.outputConnection||!a.outputConnection.targetConnection){var d=a.getCommentText();d&&(c+=Blockly.ASEBA.prefixLines(d,"# ")+"\n");for(var e=0;e<a.inputList.length;e++)a.inputList[e].type==Blockly.INPUT_VALUE&&(d=a.inputList[e].connection.targetBlock())&&(d=Blockly.ASEBA.allNestedComments(d))&&(c+=Blockly.ASEBA.prefixLines(d,"# "))}e=a.nextConnection&&a.nextConnection.targetBlock();e=Blockly.ASEBA.blockToCode(e);return c+b+e};
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.ASEBA.colour={};Blockly.ASEBA.colour_picker=function(a){return["'"+a.getFieldValue("COLOUR")+"'",Blockly.ASEBA.ORDER_ATOMIC]};Blockly.ASEBA.colour_random=function(a){return[Blockly.ASEBA.provideFunction_("colour_random",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"() {"," var num = Math.floor(Math.random() * Math.pow(2, 24));"," return '#' + ('00000' + num.toString(16)).substr(-6);","}"])+"()",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
Blockly.ASEBA.colour_rgb=function(a){var b=Blockly.ASEBA.valueToCode(a,"RED",Blockly.ASEBA.ORDER_COMMA)||0,c=Blockly.ASEBA.valueToCode(a,"GREEN",Blockly.ASEBA.ORDER_COMMA)||0;a=Blockly.ASEBA.valueToCode(a,"BLUE",Blockly.ASEBA.ORDER_COMMA)||0;return[Blockly.ASEBA.provideFunction_("colour_rgb",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(r, g, b) {"," r = Math.max(Math.min(Number(r), 100), 0) * 2.55;"," g = Math.max(Math.min(Number(g), 100), 0) * 2.55;"," b = Math.max(Math.min(Number(b), 100), 0) * 2.55;",
" r = ('0' + (Math.round(r) || 0).toString(16)).slice(-2);"," g = ('0' + (Math.round(g) || 0).toString(16)).slice(-2);"," b = ('0' + (Math.round(b) || 0).toString(16)).slice(-2);"," return '#' + r + g + b;","}"])+"("+b+", "+c+", "+a+")",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
Blockly.ASEBA.colour_blend=function(a){var b=Blockly.ASEBA.valueToCode(a,"COLOUR1",Blockly.ASEBA.ORDER_COMMA)||"'#000000'",c=Blockly.ASEBA.valueToCode(a,"COLOUR2",Blockly.ASEBA.ORDER_COMMA)||"'#000000'";a=Blockly.ASEBA.valueToCode(a,"RATIO",Blockly.ASEBA.ORDER_COMMA)||.5;return[Blockly.ASEBA.provideFunction_("colour_blend",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(c1, c2, ratio) {"," ratio = Math.max(Math.min(Number(ratio), 1), 0);"," var r1 = parseInt(c1.substring(1, 3), 16);"," var g1 = parseInt(c1.substring(3, 5), 16);",
" var b1 = parseInt(c1.substring(5, 7), 16);"," var r2 = parseInt(c2.substring(1, 3), 16);"," var g2 = parseInt(c2.substring(3, 5), 16);"," var b2 = parseInt(c2.substring(5, 7), 16);"," var r = Math.round(r1 * (1 - ratio) + r2 * ratio);"," var g = Math.round(g1 * (1 - ratio) + g2 * ratio);"," var b = Math.round(b1 * (1 - ratio) + b2 * ratio);"," r = ('0' + (r || 0).toString(16)).slice(-2);"," g = ('0' + (g || 0).toString(16)).slice(-2);"," b = ('0' + (b || 0).toString(16)).slice(-2);"," return '#' + r + g + b;",
"}"])+"("+b+", "+c+", "+a+")",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.ASEBA.lists={};Blockly.ASEBA.lists_create_empty=function(a){return["[]",Blockly.ASEBA.ORDER_ATOMIC]};Blockly.ASEBA.lists_create_with=function(a){for(var b=Array(a.itemCount_),c=0;c<a.itemCount_;c++)b[c]=Blockly.ASEBA.valueToCode(a,"ADD"+c,Blockly.ASEBA.ORDER_COMMA)||"null";b="["+b.join(", ")+"]";return[b,Blockly.ASEBA.ORDER_ATOMIC]};
Blockly.ASEBA.lists_repeat=function(a){var b=Blockly.ASEBA.provideFunction_("lists_repeat",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(value, n) {"," var array = [];"," for (var i = 0; i < n; i++) {"," array[i] = value;"," }"," return array;","}"]),c=Blockly.ASEBA.valueToCode(a,"ITEM",Blockly.ASEBA.ORDER_COMMA)||"null";a=Blockly.ASEBA.valueToCode(a,"NUM",Blockly.ASEBA.ORDER_COMMA)||"0";return[b+"("+c+", "+a+")",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
Blockly.ASEBA.lists_length=function(a){return[(Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_FUNCTION_CALL)||"[]")+".length",Blockly.ASEBA.ORDER_MEMBER]};Blockly.ASEBA.lists_isEmpty=function(a){return["!"+(Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_MEMBER)||"[]")+".length",Blockly.ASEBA.ORDER_LOGICAL_NOT]};
Blockly.ASEBA.lists_indexOf=function(a){var b="FIRST"==a.getFieldValue("END")?"indexOf":"lastIndexOf",c=Blockly.ASEBA.valueToCode(a,"FIND",Blockly.ASEBA.ORDER_NONE)||"''";return[(Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_MEMBER)||"[]")+"."+b+"("+c+") + 1",Blockly.ASEBA.ORDER_MEMBER]};
Blockly.ASEBA.lists_getIndex=function(a){var b=a.getFieldValue("MODE")||"GET",c=a.getFieldValue("WHERE")||"FROM_START",d=Blockly.ASEBA.valueToCode(a,"AT",Blockly.ASEBA.ORDER_UNARY_NEGATION)||"1";a=Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_MEMBER)||"[]";if("FIRST"==c){if("GET"==b)return[a+"[0]",Blockly.ASEBA.ORDER_MEMBER];if("GET_REMOVE"==b)return[a+".shift()",Blockly.ASEBA.ORDER_MEMBER];if("REMOVE"==b)return a+".shift();\n"}else if("LAST"==c){if("GET"==b)return[a+".slice(-1)[0]",Blockly.ASEBA.ORDER_MEMBER];
if("GET_REMOVE"==b)return[a+".pop()",Blockly.ASEBA.ORDER_MEMBER];if("REMOVE"==b)return a+".pop();\n"}else if("FROM_START"==c){d=Blockly.isNumber(d)?parseFloat(d)-1:d+" - 1";if("GET"==b)return[a+"["+d+"]",Blockly.ASEBA.ORDER_MEMBER];if("GET_REMOVE"==b)return[a+".splice("+d+", 1)[0]",Blockly.ASEBA.ORDER_FUNCTION_CALL];if("REMOVE"==b)return a+".splice("+d+", 1);\n"}else if("FROM_END"==c){if("GET"==b)return[a+".slice(-"+d+")[0]",Blockly.ASEBA.ORDER_FUNCTION_CALL];if("GET_REMOVE"==b||"REMOVE"==b){c=Blockly.ASEBA.provideFunction_("lists_remove_from_end",
["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(list, x) {"," x = list.length - x;"," return list.splice(x, 1)[0];","}"]);d=c+"("+a+", "+d+")";if("GET_REMOVE"==b)return[d,Blockly.ASEBA.ORDER_FUNCTION_CALL];if("REMOVE"==b)return d+";\n"}}else if("RANDOM"==c){c=Blockly.ASEBA.provideFunction_("lists_get_random_item",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(list, remove) {"," var x = Math.floor(Math.random() * list.length);"," if (remove) {"," return list.splice(x, 1)[0];",
" } else {"," return list[x];"," }","}"]);d=c+"("+a+", "+("GET"!=b)+")";if("GET"==b||"GET_REMOVE"==b)return[d,Blockly.ASEBA.ORDER_FUNCTION_CALL];if("REMOVE"==b)return d+";\n"}throw"Unhandled combination (lists_getIndex).";};
Blockly.ASEBA.lists_setIndex=function(a){function b(){if(c.match(/^\w+$/))return"";var a=Blockly.ASEBA.variableDB_.getDistinctName("tmp_list",Blockly.Variables.NAME_TYPE),b="var "+a+" = "+c+";\n";c=a;return b}var c=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_MEMBER)||"[]",d=a.getFieldValue("MODE")||"GET",e=a.getFieldValue("WHERE")||"FROM_START",f=Blockly.ASEBA.valueToCode(a,"AT",Blockly.ASEBA.ORDER_NONE)||"1";a=Blockly.ASEBA.valueToCode(a,"TO",Blockly.ASEBA.ORDER_ASSIGNMENT)||"null";if("FIRST"==
e){if("SET"==d)return c+"[0] = "+a+";\n";if("INSERT"==d)return c+".unshift("+a+");\n"}else if("LAST"==e){if("SET"==d)return e=b(),e+(c+"["+c+".length - 1] = "+a+";\n");if("INSERT"==d)return c+".push("+a+");\n"}else if("FROM_START"==e){f=Blockly.isNumber(f)?parseFloat(f)-1:f+" - 1";if("SET"==d)return c+"["+f+"] = "+a+";\n";if("INSERT"==d)return c+".splice("+f+", 0, "+a+");\n"}else if("FROM_END"==e){e=b();if("SET"==d)return e+=c+"["+c+".length - "+f+"] = "+a+";\n";if("INSERT"==d)return e+=c+".splice("+
c+".length - "+f+", 0, "+a+");\n"}else if("RANDOM"==e){e=b();f=Blockly.ASEBA.variableDB_.getDistinctName("tmp_x",Blockly.Variables.NAME_TYPE);e+="var "+f+" = Math.floor(Math.random() * "+c+".length);\n";if("SET"==d)return e+=c+"["+f+"] = "+a+";\n";if("INSERT"==d)return e+=c+".splice("+f+", 0, "+a+");\n"}throw"Unhandled combination (lists_setIndex).";};
Blockly.ASEBA.lists_getSublist=function(a){var b=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_MEMBER)||"[]",c=a.getFieldValue("WHERE1"),d=a.getFieldValue("WHERE2"),e=Blockly.ASEBA.valueToCode(a,"AT1",Blockly.ASEBA.ORDER_NONE)||"1";a=Blockly.ASEBA.valueToCode(a,"AT2",Blockly.ASEBA.ORDER_NONE)||"1";return["FIRST"==c&&"LAST"==d?b+".concat()":Blockly.ASEBA.provideFunction_("lists_get_sublist",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(list, where1, at1, where2, at2) {"," function 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.slice(at1, at2);","}"])+"("+b+", '"+c+"', "+e+", '"+d+"', "+a+")",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.ASEBA.logic={};Blockly.ASEBA.controls_if=function(a){for(var b=0,c=Blockly.ASEBA.valueToCode(a,"IF"+b,Blockly.ASEBA.ORDER_NONE)||"false",d=Blockly.ASEBA.statementToCode(a,"DO"+b),e="if ("+c+") {\n"+d+"}",b=1;b<=a.elseifCount_;b++)c=Blockly.ASEBA.valueToCode(a,"IF"+b,Blockly.ASEBA.ORDER_NONE)||"false",d=Blockly.ASEBA.statementToCode(a,"DO"+b),e+=" else if ("+c+") {\n"+d+"}";a.elseCount_&&(d=Blockly.ASEBA.statementToCode(a,"ELSE"),e+=" else {\n"+d+"}");return e+"\n"};
Blockly.ASEBA.logic_compare=function(a){var b={EQ:"==",NEQ:"!=",LT:"<",LTE:"<=",GT:">",GTE:">="}[a.getFieldValue("OP")],c="=="==b||"!="==b?Blockly.ASEBA.ORDER_EQUALITY:Blockly.ASEBA.ORDER_RELATIONAL,d=Blockly.ASEBA.valueToCode(a,"A",c)||"0";a=Blockly.ASEBA.valueToCode(a,"B",c)||"0";return[d+" "+b+" "+a,c]};
Blockly.ASEBA.logic_operation=function(a){var b="AND"==a.getFieldValue("OP")?"&&":"||",c="&&"==b?Blockly.ASEBA.ORDER_LOGICAL_AND:Blockly.ASEBA.ORDER_LOGICAL_OR,d=Blockly.ASEBA.valueToCode(a,"A",c);a=Blockly.ASEBA.valueToCode(a,"B",c);if(d||a){var e="&&"==b?"true":"false";d||(d=e);a||(a=e)}else a=d="false";return[d+" "+b+" "+a,c]};Blockly.ASEBA.logic_negate=function(a){var b=Blockly.ASEBA.ORDER_LOGICAL_NOT;return["!"+(Blockly.ASEBA.valueToCode(a,"BOOL",b)||"true"),b]};
Blockly.ASEBA.logic_boolean=function(a){return["TRUE"==a.getFieldValue("BOOL")?"true":"false",Blockly.ASEBA.ORDER_ATOMIC]};Blockly.ASEBA.logic_null=function(a){return["null",Blockly.ASEBA.ORDER_ATOMIC]};
Blockly.ASEBA.logic_ternary=function(a){var b=Blockly.ASEBA.valueToCode(a,"IF",Blockly.ASEBA.ORDER_CONDITIONAL)||"false",c=Blockly.ASEBA.valueToCode(a,"THEN",Blockly.ASEBA.ORDER_CONDITIONAL)||"null";a=Blockly.ASEBA.valueToCode(a,"ELSE",Blockly.ASEBA.ORDER_CONDITIONAL)||"null";return[b+" ? "+c+" : "+a,Blockly.ASEBA.ORDER_CONDITIONAL]};
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.ASEBA.loops={};Blockly.ASEBA.controls_repeat=function(a){var b=Number(a.getFieldValue("TIMES")),c=Blockly.ASEBA.statementToCode(a,"DO"),c=Blockly.ASEBA.addLoopTrap(c,a.id);a=Blockly.ASEBA.variableDB_.getDistinctName("count",Blockly.Variables.NAME_TYPE);Blockly.ASEBA.internalVariables_[a]=a;return"for "+a+" in 1:"+b+" do\n"+c+"end\n"};
Blockly.ASEBA.controls_repeat_ext=function(a){var b=Blockly.ASEBA.valueToCode(a,"TIMES",Blockly.ASEBA.ORDER_ASSIGNMENT)||"0",c=Blockly.ASEBA.statementToCode(a,"DO"),c=Blockly.ASEBA.addLoopTrap(c,a.id);a="";var d=Blockly.ASEBA.variableDB_.getDistinctName("count",Blockly.Variables.NAME_TYPE),e=b;Blockly.ASEBA.internalVariables_[d]=d;Blockly.isNumber(b)?a+="for "+d+" in 1:"+e+" do\n"+c+"end\n":(b.match(/^\w+$/)||(e=Blockly.ASEBA.variableDB_.getDistinctName("repeat_end",Blockly.Variables.NAME_TYPE),Blockly.ASEBA.internalVariables_[e]=
e,a+=e+" = "+b+"\n"),a+=d+" = 0\nwhile "+d+" < "+e+"\n"+c+" "+d+" += 1\nend\n");return a};Blockly.ASEBA.controls_whileUntil=function(a){var b="UNTIL"==a.getFieldValue("MODE"),c=Blockly.ASEBA.valueToCode(a,"BOOL",b?Blockly.ASEBA.ORDER_LOGICAL_NOT:Blockly.ASEBA.ORDER_NONE)||"false",d=Blockly.ASEBA.statementToCode(a,"DO"),d=Blockly.ASEBA.addLoopTrap(d,a.id);b&&(c="not "+c);return"while "+c+"\n"+d+"end\n"};
Blockly.ASEBA.controls_for=function(a){var b=Blockly.ASEBA.variableDB_.getName(a.getFieldValue("VAR"),Blockly.Variables.NAME_TYPE),c=Blockly.ASEBA.valueToCode(a,"FROM",Blockly.ASEBA.ORDER_ASSIGNMENT)||"0",d=Blockly.ASEBA.valueToCode(a,"TO",Blockly.ASEBA.ORDER_ASSIGNMENT)||"0",e=Blockly.ASEBA.valueToCode(a,"BY",Blockly.ASEBA.ORDER_ASSIGNMENT)||"1",f=Blockly.ASEBA.statementToCode(a,"DO"),f=Blockly.ASEBA.addLoopTrap(f,a.id);if(Blockly.isNumber(c)&&Blockly.isNumber(d)&&Blockly.isNumber(e)){var g=parseFloat(c)<=
parseFloat(d);a="for ("+b+" = "+c+"; "+b+(g?" <= ":" >= ")+d+"; "+b;b=Math.abs(parseFloat(e));a=(1==b?a+(g?"++":"--"):a+((g?" += ":" -= ")+b))+(") {\n"+f+"}\n")}else a="",g=c,c.match(/^\w+$/)||Blockly.isNumber(c)||(g=Blockly.ASEBA.variableDB_.getDistinctName(b+"_start",Blockly.Variables.NAME_TYPE),a+="var "+g+" = "+c+";\n"),c=d,d.match(/^\w+$/)||Blockly.isNumber(d)||(c=Blockly.ASEBA.variableDB_.getDistinctName(b+"_end",Blockly.Variables.NAME_TYPE),a+="var "+c+" = "+d+";\n"),d=Blockly.ASEBA.variableDB_.getDistinctName(b+
"_inc",Blockly.Variables.NAME_TYPE),a+="var "+d+" = ",a=Blockly.isNumber(e)?a+(Math.abs(e)+";\n"):a+("Math.abs("+e+");\n"),a+="if ("+g+" > "+c+") {\n",a+=Blockly.ASEBA.INDENT+d+" = -"+d+";\n",a+="}\n",a+="for ("+b+" = "+g+";\n "+d+" >= 0 ? "+b+" <= "+c+" : "+b+" >= "+c+";\n "+b+" += "+d+") {\n"+f+"}\n";return a};
Blockly.ASEBA.controls_forEach=function(a){var b=Blockly.ASEBA.variableDB_.getName(a.getFieldValue("VAR"),Blockly.Variables.NAME_TYPE),c=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_ASSIGNMENT)||"[]",d=Blockly.ASEBA.statementToCode(a,"DO"),d=Blockly.ASEBA.addLoopTrap(d,a.id);a=Blockly.ASEBA.variableDB_.getDistinctName(b+"_index",Blockly.Variables.NAME_TYPE);d=Blockly.ASEBA.INDENT+b+" = "+c+"["+a+"];\n"+d;return"for (var "+a+" in "+c+") {\n"+d+"}\n"};
Blockly.ASEBA.controls_flow_statements=function(a){throw"ASEBA does not support flow statement.";};
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.ASEBA.math={};Blockly.ASEBA.math_number=function(a){return[parseFloat(a.getFieldValue("NUM")),Blockly.ASEBA.ORDER_ATOMIC]};
Blockly.ASEBA.math_arithmetic=function(a){var b={ADD:[" + ",Blockly.ASEBA.ORDER_ADDITION],MINUS:[" - ",Blockly.ASEBA.ORDER_SUBTRACTION],MULTIPLY:[" * ",Blockly.ASEBA.ORDER_MULTIPLICATION],DIVIDE:[" / ",Blockly.ASEBA.ORDER_DIVISION],POWER:[null,Blockly.ASEBA.ORDER_COMMA]}[a.getFieldValue("OP")],c=b[0],b=b[1],d=Blockly.ASEBA.valueToCode(a,"A",b)||"0";a=Blockly.ASEBA.valueToCode(a,"B",b)||"0";return c?[d+c+a,b]:["Math.pow("+d+", "+a+")",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
Blockly.ASEBA.math_single=function(a){var b=a.getFieldValue("OP"),c;if("NEG"==b)return a=Blockly.ASEBA.valueToCode(a,"NUM",Blockly.ASEBA.ORDER_UNARY_NEGATION)||"0","-"==a[0]&&(a=" "+a),["-"+a,Blockly.ASEBA.ORDER_UNARY_NEGATION];a="SIN"==b||"COS"==b||"TAN"==b?Blockly.ASEBA.valueToCode(a,"NUM",Blockly.ASEBA.ORDER_DIVISION)||"0":Blockly.ASEBA.valueToCode(a,"NUM",Blockly.ASEBA.ORDER_NONE)||"0";switch(b){case "ABS":c="Math.abs("+a+")";break;case "ROOT":c="Math.sqrt("+a+")";break;case "LN":c="Math.log("+
a+")";break;case "EXP":c="Math.exp("+a+")";break;case "POW10":c="Math.pow(10,"+a+")";break;case "ROUND":c="Math.round("+a+")";break;case "ROUNDUP":c="Math.ceil("+a+")";break;case "ROUNDDOWN":c="Math.floor("+a+")";break;case "SIN":c="Math.sin("+a+" / 180 * Math.PI)";break;case "COS":c="Math.cos("+a+" / 180 * Math.PI)";break;case "TAN":c="Math.tan("+a+" / 180 * Math.PI)"}if(c)return[c,Blockly.ASEBA.ORDER_FUNCTION_CALL];switch(b){case "LOG10":c="Math.log("+a+") / Math.log(10)";break;case "ASIN":c="Math.asin("+
a+") / Math.PI * 180";break;case "ACOS":c="Math.acos("+a+") / Math.PI * 180";break;case "ATAN":c="Math.atan("+a+") / Math.PI * 180";break;default:throw"Unknown math operator: "+b;}return[c,Blockly.ASEBA.ORDER_DIVISION]};
Blockly.ASEBA.math_constant=function(a){return{PI:["Math.PI",Blockly.ASEBA.ORDER_MEMBER],E:["Math.E",Blockly.ASEBA.ORDER_MEMBER],GOLDEN_RATIO:["(1 + Math.sqrt(5)) / 2",Blockly.ASEBA.ORDER_DIVISION],SQRT2:["Math.SQRT2",Blockly.ASEBA.ORDER_MEMBER],SQRT1_2:["Math.SQRT1_2",Blockly.ASEBA.ORDER_MEMBER],INFINITY:["Infinity",Blockly.ASEBA.ORDER_ATOMIC]}[a.getFieldValue("CONSTANT")]};
Blockly.ASEBA.math_number_property=function(a){var b=Blockly.ASEBA.valueToCode(a,"NUMBER_TO_CHECK",Blockly.ASEBA.ORDER_MODULUS)||"0",c=a.getFieldValue("PROPERTY"),d;if("PRIME"==c)return d=Blockly.ASEBA.provideFunction_("math_isPrime",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(n) {"," // https://en.wikipedia.org/wiki/Primality_test#Naive_methods"," if (n == 2 || n == 3) {"," return true;"," }"," // False if n is NaN, negative, is 1, or not whole."," // And false if n is divisible by 2 or 3.",
" if (isNaN(n) || n <= 1 || n % 1 != 0 || n % 2 == 0 || n % 3 == 0) {"," return false;"," }"," // Check all the numbers of form 6k +/- 1, up to sqrt(n)."," for (var x = 6; x <= Math.sqrt(n) + 1; x += 6) {"," if (n % (x - 1) == 0 || n % (x + 1) == 0) {"," return false;"," }"," }"," return true;","}"])+"("+b+")",[d,Blockly.ASEBA.ORDER_FUNCTION_CALL];switch(c){case "EVEN":d=b+" % 2 == 0";break;case "ODD":d=b+" % 2 == 1";break;case "WHOLE":d=b+" % 1 == 0";break;case "POSITIVE":d=b+
" > 0";break;case "NEGATIVE":d=b+" < 0";break;case "DIVISIBLE_BY":a=Blockly.ASEBA.valueToCode(a,"DIVISOR",Blockly.ASEBA.ORDER_MODULUS)||"0",d=b+" % "+a+" == 0"}return[d,Blockly.ASEBA.ORDER_EQUALITY]};Blockly.ASEBA.math_change=function(a){var b=Blockly.ASEBA.valueToCode(a,"DELTA",Blockly.ASEBA.ORDER_ADDITION)||"0";a=Blockly.ASEBA.variableDB_.getName(a.getFieldValue("VAR"),Blockly.Variables.NAME_TYPE);return a+" = (typeof "+a+" == 'number' ? "+a+" : 0) + "+b+";\n"};Blockly.ASEBA.math_round=Blockly.ASEBA.math_single;
Blockly.ASEBA.math_trig=Blockly.ASEBA.math_single;
Blockly.ASEBA.math_on_list=function(a){var b=a.getFieldValue("OP");switch(b){case "SUM":a=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_MEMBER)||"[]";a+=".reduce(function(x, y) {return x + y;})";break;case "MIN":a=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_COMMA)||"[]";a="Math.min.apply(null, "+a+")";break;case "MAX":a=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_COMMA)||"[]";a="Math.max.apply(null, "+a+")";break;case "AVERAGE":b=Blockly.ASEBA.provideFunction_("math_mean",
["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(myList) {"," return myList.reduce(function(x, y) {return x + y;}) / myList.length;","}"]);a=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_NONE)||"[]";a=b+"("+a+")";break;case "MEDIAN":b=Blockly.ASEBA.provideFunction_("math_median",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(myList) {"," var localList = myList.filter(function (x) {return typeof x == 'number';});"," if (!localList.length) return null;"," localList.sort(function(a, b) {return b - a;});",
" if (localList.length % 2 == 0) {"," return (localList[localList.length / 2 - 1] + localList[localList.length / 2]) / 2;"," } else {"," return localList[(localList.length - 1) / 2];"," }","}"]);a=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_NONE)||"[]";a=b+"("+a+")";break;case "MODE":b=Blockly.ASEBA.provideFunction_("math_modes",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(values) {"," var modes = [];"," var counts = [];"," var maxCount = 0;"," for (var i = 0; i < values.length; i++) {",
" var value = values[i];"," var found = false;"," var thisCount;"," for (var j = 0; j < counts.length; j++) {"," if (counts[j][0] === value) {"," thisCount = ++counts[j][1];"," found = true;"," break;"," }"," }"," if (!found) {"," counts.push([value, 1]);"," thisCount = 1;"," }"," maxCount = Math.max(thisCount, maxCount);"," }"," for (var j = 0; j < counts.length; j++) {"," if (counts[j][1] == maxCount) {"," modes.push(counts[j][0]);",
" }"," }"," return modes;","}"]);a=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_NONE)||"[]";a=b+"("+a+")";break;case "STD_DEV":b=Blockly.ASEBA.provideFunction_("math_standard_deviation",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(numbers) {"," var n = numbers.length;"," if (!n) return null;"," var mean = numbers.reduce(function(x, y) {return x + y;}) / n;"," var variance = 0;"," for (var j = 0; j < n; j++) {"," variance += Math.pow(numbers[j] - mean, 2);"," }",
" variance = variance / n;"," return Math.sqrt(variance);","}"]);a=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_NONE)||"[]";a=b+"("+a+")";break;case "RANDOM":b=Blockly.ASEBA.provideFunction_("math_random_list",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(list) {"," var x = Math.floor(Math.random() * list.length);"," return list[x];","}"]);a=Blockly.ASEBA.valueToCode(a,"LIST",Blockly.ASEBA.ORDER_NONE)||"[]";a=b+"("+a+")";break;default:throw"Unknown operator: "+b;}return[a,
Blockly.ASEBA.ORDER_FUNCTION_CALL]};Blockly.ASEBA.math_modulo=function(a){var b=Blockly.ASEBA.valueToCode(a,"DIVIDEND",Blockly.ASEBA.ORDER_MODULUS)||"0";a=Blockly.ASEBA.valueToCode(a,"DIVISOR",Blockly.ASEBA.ORDER_MODULUS)||"0";return[b+" % "+a,Blockly.ASEBA.ORDER_MODULUS]};
Blockly.ASEBA.math_constrain=function(a){var b=Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_COMMA)||"0",c=Blockly.ASEBA.valueToCode(a,"LOW",Blockly.ASEBA.ORDER_COMMA)||"0";a=Blockly.ASEBA.valueToCode(a,"HIGH",Blockly.ASEBA.ORDER_COMMA)||"Infinity";return["Math.min(Math.max("+b+", "+c+"), "+a+")",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
Blockly.ASEBA.math_random_int=function(a){var b=Blockly.ASEBA.valueToCode(a,"FROM",Blockly.ASEBA.ORDER_COMMA)||"0";a=Blockly.ASEBA.valueToCode(a,"TO",Blockly.ASEBA.ORDER_COMMA)||"0";return[Blockly.ASEBA.provideFunction_("math_random_int",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(a, b) {"," if (a > b) {"," // Swap a and b to ensure a is smaller."," var c = a;"," a = b;"," b = c;"," }"," return Math.floor(Math.random() * (b - a + 1) + a);","}"])+"("+b+", "+a+")",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
Blockly.ASEBA.math_random_float=function(a){return["Math.random()",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.ASEBA.procedures={};
Blockly.ASEBA.procedures_defreturn=function(a){var b=Blockly.ASEBA.variableDB_.getName(a.getFieldValue("NAME"),Blockly.Procedures.NAME_TYPE),c=Blockly.ASEBA.statementToCode(a,"STACK");Blockly.ASEBA.STATEMENT_PREFIX&&(c=Blockly.ASEBA.prefixLines(Blockly.ASEBA.STATEMENT_PREFIX.replace(/%1/g,"'"+a.id+"'"),Blockly.ASEBA.INDENT)+c);Blockly.ASEBA.INFINITE_LOOP_TRAP&&(c=Blockly.ASEBA.INFINITE_LOOP_TRAP.replace(/%1/g,"'"+a.id+"'")+c);var d=Blockly.ASEBA.valueToCode(a,"RETURN",Blockly.ASEBA.ORDER_NONE)||"";
d&&(d=" return "+d+";\n");for(var e=[],f=0;f<a.arguments_.length;f++)e[f]=Blockly.ASEBA.variableDB_.getName(a.arguments_[f],Blockly.Variables.NAME_TYPE);c="function "+b+"("+e.join(", ")+") {\n"+c+d+"}";c=Blockly.ASEBA.scrub_(a,c);Blockly.ASEBA.definitions_[b]=c;return null};Blockly.ASEBA.procedures_defnoreturn=Blockly.ASEBA.procedures_defreturn;
Blockly.ASEBA.procedures_callreturn=function(a){for(var b=Blockly.ASEBA.variableDB_.getName(a.getFieldValue("NAME"),Blockly.Procedures.NAME_TYPE),c=[],d=0;d<a.arguments_.length;d++)c[d]=Blockly.ASEBA.valueToCode(a,"ARG"+d,Blockly.ASEBA.ORDER_COMMA)||"null";return[b+"("+c.join(", ")+")",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
Blockly.ASEBA.procedures_callnoreturn=function(a){for(var b=Blockly.ASEBA.variableDB_.getName(a.getFieldValue("NAME"),Blockly.Procedures.NAME_TYPE),c=[],d=0;d<a.arguments_.length;d++)c[d]=Blockly.ASEBA.valueToCode(a,"ARG"+d,Blockly.ASEBA.ORDER_COMMA)||"null";return b+"("+c.join(", ")+");\n"};
Blockly.ASEBA.procedures_ifreturn=function(a){var b="if ("+(Blockly.ASEBA.valueToCode(a,"CONDITION",Blockly.ASEBA.ORDER_NONE)||"false")+") {\n";a.hasReturnValue_?(a=Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_NONE)||"null",b+=" return "+a+";\n"):b+=" return;\n";return b+"}\n"};
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.ASEBA.text={};Blockly.ASEBA.text=function(a){return[Blockly.ASEBA.quote_(a.getFieldValue("TEXT")),Blockly.ASEBA.ORDER_ATOMIC]};
Blockly.ASEBA.text_join=function(a){var b;if(0==a.itemCount_)return["''",Blockly.ASEBA.ORDER_ATOMIC];if(1==a.itemCount_)return b=Blockly.ASEBA.valueToCode(a,"ADD0",Blockly.ASEBA.ORDER_NONE)||"''",["String("+b+")",Blockly.ASEBA.ORDER_FUNCTION_CALL];if(2==a.itemCount_)return b=Blockly.ASEBA.valueToCode(a,"ADD0",Blockly.ASEBA.ORDER_NONE)||"''",a=Blockly.ASEBA.valueToCode(a,"ADD1",Blockly.ASEBA.ORDER_NONE)||"''",["String("+b+") + String("+a+")",Blockly.ASEBA.ORDER_ADDITION];b=Array(a.itemCount_);for(var c=
0;c<a.itemCount_;c++)b[c]=Blockly.ASEBA.valueToCode(a,"ADD"+c,Blockly.ASEBA.ORDER_COMMA)||"''";b="["+b.join(",")+"].join('')";return[b,Blockly.ASEBA.ORDER_FUNCTION_CALL]};Blockly.ASEBA.text_append=function(a){var b=Blockly.ASEBA.variableDB_.getName(a.getFieldValue("VAR"),Blockly.Variables.NAME_TYPE);a=Blockly.ASEBA.valueToCode(a,"TEXT",Blockly.ASEBA.ORDER_NONE)||"''";return b+" = String("+b+") + String("+a+");\n"};
Blockly.ASEBA.text_length=function(a){return[(Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_FUNCTION_CALL)||"''")+".length",Blockly.ASEBA.ORDER_MEMBER]};Blockly.ASEBA.text_isEmpty=function(a){return["!"+(Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_MEMBER)||"''"),Blockly.ASEBA.ORDER_LOGICAL_NOT]};
Blockly.ASEBA.text_indexOf=function(a){var b="FIRST"==a.getFieldValue("END")?"indexOf":"lastIndexOf",c=Blockly.ASEBA.valueToCode(a,"FIND",Blockly.ASEBA.ORDER_NONE)||"''";return[(Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_MEMBER)||"''")+"."+b+"("+c+") + 1",Blockly.ASEBA.ORDER_MEMBER]};
Blockly.ASEBA.text_charAt=function(a){var b=a.getFieldValue("WHERE")||"FROM_START",c=Blockly.ASEBA.valueToCode(a,"AT",Blockly.ASEBA.ORDER_UNARY_NEGATION)||"1";a=Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_MEMBER)||"''";switch(b){case "FIRST":return[a+".charAt(0)",Blockly.ASEBA.ORDER_FUNCTION_CALL];case "LAST":return[a+".slice(-1)",Blockly.ASEBA.ORDER_FUNCTION_CALL];case "FROM_START":return c=Blockly.isNumber(c)?parseFloat(c)-1:c+" - 1",[a+".charAt("+c+")",Blockly.ASEBA.ORDER_FUNCTION_CALL];
case "FROM_END":return[a+".slice(-"+c+").charAt(0)",Blockly.ASEBA.ORDER_FUNCTION_CALL];case "RANDOM":return b=Blockly.ASEBA.provideFunction_("text_random_letter",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(text) {"," var x = Math.floor(Math.random() * text.length);"," return text[x];","}"])+"("+a+")",[b,Blockly.ASEBA.ORDER_FUNCTION_CALL]}throw"Unhandled option (text_charAt).";};
Blockly.ASEBA.text_getSubstring=function(a){var b=Blockly.ASEBA.valueToCode(a,"STRING",Blockly.ASEBA.ORDER_MEMBER)||"''",c=a.getFieldValue("WHERE1"),d=a.getFieldValue("WHERE2"),e=Blockly.ASEBA.valueToCode(a,"AT1",Blockly.ASEBA.ORDER_NONE)||"1";a=Blockly.ASEBA.valueToCode(a,"AT2",Blockly.ASEBA.ORDER_NONE)||"1";return["FIRST"==c&&"LAST"==d?b:Blockly.ASEBA.provideFunction_("text_get_substring",["function "+Blockly.ASEBA.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.slice(at1, at2);","}"])+"("+b+", '"+c+"', "+e+", '"+d+"', "+a+")",Blockly.ASEBA.ORDER_FUNCTION_CALL]};
Blockly.ASEBA.text_changeCase=function(a){var b={UPPERCASE:".toUpperCase()",LOWERCASE:".toLowerCase()",TITLECASE:null}[a.getFieldValue("CASE")];b?(a=Blockly.ASEBA.valueToCode(a,"TEXT",Blockly.ASEBA.ORDER_MEMBER)||"''",a+=b):(b=Blockly.ASEBA.provideFunction_("text_toTitleCase",["function "+Blockly.ASEBA.FUNCTION_NAME_PLACEHOLDER_+"(str) {"," return str.replace(/\\S+/g,"," function(txt) {return txt[0].toUpperCase() + txt.substring(1).toLowerCase();});","}"]),a=Blockly.ASEBA.valueToCode(a,"TEXT",
Blockly.ASEBA.ORDER_NONE)||"''",a=b+"("+a+")");return[a,Blockly.ASEBA.ORDER_FUNCTION_CALL]};Blockly.ASEBA.text_trim=function(a){var b={LEFT:".trimLeft()",RIGHT:".trimRight()",BOTH:".trim()"}[a.getFieldValue("MODE")];return[(Blockly.ASEBA.valueToCode(a,"TEXT",Blockly.ASEBA.ORDER_MEMBER)||"''")+b,Blockly.ASEBA.ORDER_FUNCTION_CALL]};Blockly.ASEBA.text_print=function(a){return"window.alert("+(Blockly.ASEBA.valueToCode(a,"TEXT",Blockly.ASEBA.ORDER_NONE)||"''")+");\n"};
Blockly.ASEBA.text_prompt=function(a){var b="window.prompt("+Blockly.ASEBA.quote_(a.getFieldValue("TEXT"))+")";"NUMBER"==a.getFieldValue("TYPE")&&(b="parseFloat("+b+")");return[b,Blockly.ASEBA.ORDER_FUNCTION_CALL]};Blockly.ASEBA.text_prompt_ext=function(a){var b="window.prompt("+(Blockly.ASEBA.valueToCode(a,"TEXT",Blockly.ASEBA.ORDER_NONE)||"''")+")";"NUMBER"==a.getFieldValue("TYPE")&&(b="parseFloat("+b+")");return[b,Blockly.ASEBA.ORDER_FUNCTION_CALL]};
// Copyright 2012 Google Inc. Apache License 2.0
Blockly.ASEBA.variables={};Blockly.ASEBA.variables_get=function(a){return[Blockly.ASEBA.variableDB_.getName(a.getFieldValue("VAR"),Blockly.Variables.NAME_TYPE),Blockly.ASEBA.ORDER_ATOMIC]};Blockly.ASEBA.variables_set=function(a){var b=Blockly.ASEBA.valueToCode(a,"VALUE",Blockly.ASEBA.ORDER_ASSIGNMENT)||"0";return Blockly.ASEBA.variableDB_.getName(a.getFieldValue("VAR"),Blockly.Variables.NAME_TYPE)+" = "+b+"\n"};