python_compressed.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. // Do not edit this file; automatically generated by build.py.
  2. 'use strict';
  3. Blockly.Python = new Blockly.Generator("Python");
  4. Blockly.Python.addReservedWords("and,as,assert,break,class,continue,def,del,elif,else,except,exec,finally,for,from,global,if,import,in,is,lambda,not,or,pass,print,raise,return,try,while,with,yield,True,False,None,NotImplemented,Ellipsis,__debug__,quit,exit,copyright,license,credits,abs,divmod,input,open,staticmethod,all,enumerate,int,ord,str,any,eval,isinstance,pow,sum,basestring,execfile,issubclass,print,super,bin,file,iter,property,tuple,bool,filter,len,range,type,bytearray,float,list,raw_input,unichr,callable,format,locals,reduce,unicode,chr,frozenset,long,reload,vars,classmethod,getattr,map,repr,xrange,cmp,globals,max,reversed,zip,compile,hasattr,memoryview,round,__import__,complex,hash,min,set,apply,delattr,help,next,setattr,buffer,dict,hex,object,slice,coerce,dir,id,oct,sorted,intern");
  5. Blockly.Python.ORDER_ATOMIC = 0;
  6. Blockly.Python.ORDER_COLLECTION = 1;
  7. Blockly.Python.ORDER_STRING_CONVERSION = 1;
  8. Blockly.Python.ORDER_MEMBER = 2.1;
  9. Blockly.Python.ORDER_FUNCTION_CALL = 2.2;
  10. Blockly.Python.ORDER_EXPONENTIATION = 3;
  11. Blockly.Python.ORDER_UNARY_SIGN = 4;
  12. Blockly.Python.ORDER_BITWISE_NOT = 4;
  13. Blockly.Python.ORDER_MULTIPLICATIVE = 5;
  14. Blockly.Python.ORDER_ADDITIVE = 6;
  15. Blockly.Python.ORDER_BITWISE_SHIFT = 7;
  16. Blockly.Python.ORDER_BITWISE_AND = 8;
  17. Blockly.Python.ORDER_BITWISE_XOR = 9;
  18. Blockly.Python.ORDER_BITWISE_OR = 10;
  19. Blockly.Python.ORDER_RELATIONAL = 11;
  20. Blockly.Python.ORDER_LOGICAL_NOT = 12;
  21. Blockly.Python.ORDER_LOGICAL_AND = 13;
  22. Blockly.Python.ORDER_LOGICAL_OR = 14;
  23. Blockly.Python.ORDER_CONDITIONAL = 15;
  24. Blockly.Python.ORDER_LAMBDA = 16;
  25. Blockly.Python.ORDER_NONE = 99;
  26. Blockly.Python.ORDER_OVERRIDES = [[Blockly.Python.ORDER_FUNCTION_CALL, Blockly.Python.ORDER_MEMBER], [Blockly.Python.ORDER_FUNCTION_CALL, Blockly.Python.ORDER_FUNCTION_CALL], [Blockly.Python.ORDER_MEMBER, Blockly.Python.ORDER_MEMBER], [Blockly.Python.ORDER_MEMBER, Blockly.Python.ORDER_FUNCTION_CALL], [Blockly.Python.ORDER_LOGICAL_NOT, Blockly.Python.ORDER_LOGICAL_NOT], [Blockly.Python.ORDER_LOGICAL_AND, Blockly.Python.ORDER_LOGICAL_AND], [Blockly.Python.ORDER_LOGICAL_OR, Blockly.Python.ORDER_LOGICAL_OR]];
  27. Blockly.Python.init = function (a) {
  28. Blockly.Python.PASS = this.INDENT + "pass\n";
  29. Blockly.Python.tops_ = Object.create(null);
  30. Blockly.Python.definitions_ = Object.create(null);
  31. Blockly.Python.codeFunctions_ = Object.create(null);
  32. Blockly.Python.functionNames_ = Object.create(null);
  33. Blockly.Python.variableDB_ ? Blockly.Python.variableDB_.reset() : Blockly.Python.variableDB_ = new Blockly.Names(Blockly.Python.RESERVED_WORDS_)
  34. }
  35. ;
  36. Blockly.Python.finish = function (a) {
  37. var b = []
  38. , c = []
  39. , d = []
  40. , e = [];
  41. Blockly.Python.tops_.import_machine && delete Blockly.Python.definitions_.import_machine;
  42. for (var f in Blockly.Python.tops_) {
  43. var g = Blockly.Python.tops_[f];
  44. g.match(/^(from\s+\S+\s+)?import\s+\S+/) ? b.push(g) : c.push(g)
  45. }
  46. for (f in Blockly.Python.definitions_)
  47. g = Blockly.Python.definitions_[f],
  48. g.match(/^(from\s+\S+\s+)?import\s+\S+/) ? b.push(g) : d.push(g);
  49. for (f in Blockly.Python.codeFunctions_)
  50. g = Blockly.Python.codeFunctions_[f],
  51. e.push(g);
  52. delete Blockly.Python.tops_;
  53. delete Blockly.Python.definitions_;
  54. delete Blockly.Python.codeFunctions_;
  55. delete Blockly.Python.functionNames_;
  56. Blockly.Python.variableDB_.reset();
  57. return (b.join("\n") + "\n\n" + c.join("\n\n") + d.join("\n\n") + "\n\n" + e.join("\n\n")).replace(/\n\n+/g, "\n\n").replace(/\n*$/, "\n\n\n") + a
  58. }
  59. ;
  60. Blockly.Python.scrubNakedValue = function (a) {
  61. return a + "\n"
  62. }
  63. ;
  64. Blockly.Python.quote_ = function (a) {
  65. a = a.replace(/\\/g, "\\\\").replace(/\n/g, "\\\n").replace(/"/g, '\\"');
  66. return '"' + a + '"'
  67. }
  68. ;
  69. Blockly.Python.quote_empty = function (a) {
  70. return a
  71. }
  72. ;
  73. Blockly.Python.scrub_ = function (a, b) {
  74. var c = "";
  75. if (!a.outputConnection || !a.outputConnection.targetConnection) {
  76. var d = a.getCommentText();
  77. (d = Blockly.utils.wrap(d, Blockly.Python.COMMENT_WRAP - 3)) && (c = a.getProcedureDef ? c + ('"""' + d + '\n"""\n') : c + Blockly.Python.prefixLines(d + "\n", "# "));
  78. for (var e = 0; e < a.inputList.length; e++)
  79. a.inputList[e].type == Blockly.INPUT_VALUE && (d = a.inputList[e].connection.targetBlock()) && (d = Blockly.Python.allNestedComments(d)) && (c += Blockly.Python.prefixLines(d, "# "))
  80. }
  81. e = a.nextConnection && a.nextConnection.targetBlock();
  82. e = Blockly.Python.blockToCode(e);
  83. return c + b + e
  84. }
  85. ;
  86. Blockly.Python.getAdjustedInt = function (a, b, c, d) {
  87. c = c || 0;
  88. a.workspace.options.oneBasedIndex && c--;
  89. var e = a.workspace.options.oneBasedIndex ? "1" : "0";
  90. a = Blockly.Python.valueToCode(a, b, c ? Blockly.Python.ORDER_ADDITIVE : Blockly.Python.ORDER_NONE) || e;
  91. Blockly.isNumber(a) ? (a = parseInt(a, 10) + c,
  92. d && (a = -a)) : (a = 0 < c ? "int(" + a + " + " + c + ")" : 0 > c ? "int(" + a + " - " + -c + ")" : "int(" + a + ")",
  93. d && (a = "-" + a));
  94. return a
  95. }
  96. ;
  97. Blockly.Python.colour = {};
  98. Blockly.Python.colour_picker = function (a) {
  99. return ["'" + a.getFieldValue("COLOUR") + "'", Blockly.Python.ORDER_ATOMIC]
  100. }
  101. ;
  102. Blockly.Python.colour_random = function (a) {
  103. Blockly.Python.definitions_.import_random = "import random";
  104. return ["'#%06x' % random.randint(0, 2**24 - 1)", Blockly.Python.ORDER_FUNCTION_CALL]
  105. }
  106. ;
  107. Blockly.Python.colour_rgb = function (a) {
  108. var b = Blockly.Python.provideFunction_("colour_rgb", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(r, g, b):", " r = round(min(100, max(0, r)) * 2.55)", " g = round(min(100, max(0, g)) * 2.55)", " b = round(min(100, max(0, b)) * 2.55)", " return '#%02x%02x%02x' % (r, g, b)"])
  109. , c = Blockly.Python.valueToCode(a, "RED", Blockly.Python.ORDER_NONE) || 0
  110. , d = Blockly.Python.valueToCode(a, "GREEN", Blockly.Python.ORDER_NONE) || 0;
  111. a = Blockly.Python.valueToCode(a, "BLUE", Blockly.Python.ORDER_NONE) || 0;
  112. return [b + "(" + c + ", " + d + ", " + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  113. }
  114. ;
  115. Blockly.Python.colour_blend = function (a) {
  116. var b = Blockly.Python.provideFunction_("colour_blend", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(colour1, colour2, ratio):", " r1, r2 = int(colour1[1:3], 16), int(colour2[1:3], 16)", " g1, g2 = int(colour1[3:5], 16), int(colour2[3:5], 16)", " b1, b2 = int(colour1[5:7], 16), int(colour2[5:7], 16)", " ratio = min(1, max(0, ratio))", " r = round(r1 * (1 - ratio) + r2 * ratio)", " g = round(g1 * (1 - ratio) + g2 * ratio)", " b = round(b1 * (1 - ratio) + b2 * ratio)", " return '#%02x%02x%02x' % (r, g, b)"])
  117. , c = Blockly.Python.valueToCode(a, "COLOUR1", Blockly.Python.ORDER_NONE) || "'#000000'"
  118. , d = Blockly.Python.valueToCode(a, "COLOUR2", Blockly.Python.ORDER_NONE) || "'#000000'";
  119. a = Blockly.Python.valueToCode(a, "RATIO", Blockly.Python.ORDER_NONE) || 0;
  120. return [b + "(" + c + ", " + d + ", " + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  121. }
  122. ;
  123. Blockly.Python.lists = {};
  124. Blockly.Python.lists_create_empty = function (a) {
  125. return ["[]", Blockly.Python.ORDER_ATOMIC]
  126. }
  127. ;
  128. Blockly.Python.lists_create_with = function (a) {
  129. for (var b = Array(a.itemCount_), c = 0; c < a.itemCount_; c++)
  130. b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None";
  131. return ["[" + b.join(", ") + "]", Blockly.Python.ORDER_ATOMIC]
  132. }
  133. ;
  134. Blockly.Python.lists_create_with_object = function (a) {
  135. for (var b = Array(a.itemCount_), c = 0; c < a.itemCount_; c++)
  136. b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None";
  137. return ["[" + b.join(", ") + "]", Blockly.Python.ORDER_ATOMIC]
  138. }
  139. ;
  140. Blockly.Python.lists_repeat = function (a) {
  141. var b = Blockly.Python.valueToCode(a, "ITEM", Blockly.Python.ORDER_NONE) || "None";
  142. a = Blockly.Python.valueToCode(a, "NUM", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  143. return ["[" + b + "] * " + a, Blockly.Python.ORDER_MULTIPLICATIVE]
  144. }
  145. ;
  146. Blockly.Python.lists_length = function (a) {
  147. return ["len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "[]") + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  148. }
  149. ;
  150. Blockly.Python.lists_isEmpty = function (a) {
  151. return ["not len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "[]") + ")", Blockly.Python.ORDER_LOGICAL_NOT]
  152. }
  153. ;
  154. Blockly.Python.lists_indexOf = function (a) {
  155. var b = Blockly.Python.valueToCode(a, "FIND", Blockly.Python.ORDER_NONE) || "[]"
  156. , c = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "''";
  157. if (a.workspace.options.oneBasedIndex)
  158. var d = " 0"
  159. , e = " + 1"
  160. , f = "";
  161. else
  162. d = " -1",
  163. e = "",
  164. f = " - 1";
  165. if ("FIRST" == a.getFieldValue("END"))
  166. return a = Blockly.Python.provideFunction_("first_index", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(my_list, elem):", " try: index = my_list.index(elem)" + e, " except: index =" + d, " return index"]),
  167. [a + "(" + c + ", " + b + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  168. a = Blockly.Python.provideFunction_("last_index", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(my_list, elem):", " try: index = len(my_list) - my_list[::-1].index(elem)" + f, " except: index =" + d, " return index"]);
  169. return [a + "(" + c + ", " + b + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  170. }
  171. ;
  172. Blockly.Python.lists_getIndex = function (a) {
  173. var b = a.getFieldValue("MODE") || "GET"
  174. , c = a.getFieldValue("WHERE") || "FROM_START"
  175. , d = Blockly.Python.valueToCode(a, "VALUE", "RANDOM" == c ? Blockly.Python.ORDER_NONE : Blockly.Python.ORDER_MEMBER) || "[]";
  176. switch (c) {
  177. case "FIRST":
  178. if ("GET" == b)
  179. return [d + "[0]", Blockly.Python.ORDER_MEMBER];
  180. if ("GET_REMOVE" == b)
  181. return [d + ".pop(0)", Blockly.Python.ORDER_FUNCTION_CALL];
  182. if ("REMOVE" == b)
  183. return d + ".pop(0)\n";
  184. break;
  185. case "LAST":
  186. if ("GET" == b)
  187. return [d + "[-1]", Blockly.Python.ORDER_MEMBER];
  188. if ("GET_REMOVE" == b)
  189. return [d + ".pop()", Blockly.Python.ORDER_FUNCTION_CALL];
  190. if ("REMOVE" == b)
  191. return d + ".pop()\n";
  192. break;
  193. case "FROM_START":
  194. a = Blockly.Python.valueToCode(a, "AT", Blockly.Python.ORDER_ATOMIC);
  195. if ("GET" == b)
  196. return [d + "[" + a + "]", Blockly.Python.ORDER_MEMBER];
  197. if ("GET_REMOVE" == b)
  198. return [d + ".pop(" + a + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  199. if ("REMOVE" == b)
  200. return d + ".pop(" + a + ")\n";
  201. break;
  202. case "FROM_END":
  203. a = Blockly.Python.getAdjustedInt(a, "AT", 1, !0);
  204. if ("GET" == b)
  205. return [d + "[" + a + "]", Blockly.Python.ORDER_MEMBER];
  206. if ("GET_REMOVE" == b)
  207. return [d + ".pop(" + a + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  208. if ("REMOVE" == b)
  209. return d + ".pop(" + a + ")\n";
  210. break;
  211. case "RANDOM":
  212. Blockly.Python.definitions_.import_random = "import random";
  213. if ("GET" == b)
  214. return ["random.choice(" + d + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  215. d = Blockly.Python.provideFunction_("lists_remove_random_item", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(myList):", " x = int(random.random() * len(myList))", " return myList.pop(x)"]) + "(" + d + ")";
  216. if ("GET_REMOVE" == b)
  217. return [d, Blockly.Python.ORDER_FUNCTION_CALL];
  218. if ("REMOVE" == b)
  219. return d + "\n"
  220. }
  221. throw "Unhandled combination (lists_getIndex).";
  222. }
  223. ;
  224. Blockly.Python.lists_setIndex = function (a) {
  225. var b = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_MEMBER) || "[]"
  226. , c = a.getFieldValue("MODE") || "GET"
  227. , d = a.getFieldValue("WHERE") || "FROM_START"
  228. , e = Blockly.Python.valueToCode(a, "TO", Blockly.Python.ORDER_NONE) || "None";
  229. switch (d) {
  230. case "FIRST":
  231. if ("SET" == c)
  232. return b + "[0] = " + e + "\n";
  233. if ("INSERT" == c)
  234. return b + ".insert(0, " + e + ")\n";
  235. break;
  236. case "LAST":
  237. if ("SET" == c)
  238. return b + "[-1] = " + e + "\n";
  239. if ("INSERT" == c)
  240. return b + ".append(" + e + ")\n";
  241. break;
  242. case "FROM_START":
  243. a = Blockly.Python.getAdjustedInt(a, "AT");
  244. if ("SET" == c)
  245. return b + "[" + a + "] = " + e + "\n";
  246. if ("INSERT" == c)
  247. return b + ".insert(" + a + ", " + e + ")\n";
  248. break;
  249. case "FROM_END":
  250. a = Blockly.Python.getAdjustedInt(a, "AT", 1, !0);
  251. if ("SET" == c)
  252. return b + "[" + a + "] = " + e + "\n";
  253. if ("INSERT" == c)
  254. return b + ".insert(" + a + ", " + e + ")\n";
  255. break;
  256. case "RANDOM":
  257. Blockly.Python.definitions_.import_random = "import random";
  258. b.match(/^\w+$/) ? a = "" : (a = Blockly.Python.variableDB_.getDistinctName("tmp_list", Blockly.Variables.NAME_TYPE),
  259. d = a + " = " + b + "\n",
  260. b = a,
  261. a = d);
  262. d = Blockly.Python.variableDB_.getDistinctName("tmp_x", Blockly.Variables.NAME_TYPE);
  263. a += d + " = int(random.random() * len(" + b + "))\n";
  264. if ("SET" == c)
  265. return a + (b + "[" + d + "] = " + e + "\n");
  266. if ("INSERT" == c)
  267. return a + (b + ".insert(" + d + ", " + e + ")\n")
  268. }
  269. throw "Unhandled combination (lists_setIndex).";
  270. }
  271. ;
  272. Blockly.Python.lists_getSublist = function (a) {
  273. var b = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_MEMBER) || "[]"
  274. , c = a.getFieldValue("WHERE1")
  275. , d = a.getFieldValue("WHERE2");
  276. switch (c) {
  277. case "FROM_START":
  278. c = Blockly.Python.getAdjustedInt(a, "AT1");
  279. "0" == c && (c = "");
  280. break;
  281. case "FROM_END":
  282. c = Blockly.Python.getAdjustedInt(a, "AT1", 1, !0);
  283. break;
  284. case "FIRST":
  285. c = "";
  286. break;
  287. default:
  288. throw "Unhandled option (lists_getSublist)";
  289. }
  290. switch (d) {
  291. case "FROM_START":
  292. a = Blockly.Python.getAdjustedInt(a, "AT2", 1);
  293. break;
  294. case "FROM_END":
  295. a = Blockly.Python.getAdjustedInt(a, "AT2", 0, !0);
  296. Blockly.isNumber(String(a)) ? "0" == a && (a = "") : (Blockly.Python.definitions_.import_sys = "import sys",
  297. a += " or sys.maxsize");
  298. break;
  299. case "LAST":
  300. a = "";
  301. break;
  302. default:
  303. throw "Unhandled option (lists_getSublist)";
  304. }
  305. return [b + "[" + c + " : " + a + "]", Blockly.Python.ORDER_MEMBER]
  306. }
  307. ;
  308. Blockly.Python.lists_sort = function (a) {
  309. var b = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_NONE) || "[]"
  310. , c = a.getFieldValue("TYPE");
  311. a = "1" === a.getFieldValue("DIRECTION") ? "False" : "True";
  312. return [Blockly.Python.provideFunction_("lists_sort", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(my_list, type, reverse):", " def try_float(s):", " try:", " return float(s)", " except:", " return 0", " key_funcs = {", ' "NUMERIC": try_float,', ' "TEXT": str,', ' "IGNORE_CASE": lambda s: str(s).lower()', " }", " key_func = key_funcs[type]", " list_cpy = list(my_list)", " return sorted(list_cpy, key=key_func, reverse=reverse)"]) + "(" + b + ', "' + c + '", ' + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  313. }
  314. ;
  315. Blockly.Python.lists_split = function (a) {
  316. var b = a.getFieldValue("MODE");
  317. if ("SPLIT" == b)
  318. b = Blockly.Python.valueToCode(a, "INPUT", Blockly.Python.ORDER_MEMBER) || "''",
  319. a = Blockly.Python.valueToCode(a, "DELIM", Blockly.Python.ORDER_NONE),
  320. a = b + ".split(" + a + ")";
  321. else if ("JOIN" == b)
  322. b = Blockly.Python.valueToCode(a, "INPUT", Blockly.Python.ORDER_NONE) || "[]",
  323. a = Blockly.Python.valueToCode(a, "DELIM", Blockly.Python.ORDER_MEMBER) || "''",
  324. a = a + ".join(" + b + ")";
  325. else
  326. throw "Unknown mode: " + b;
  327. return [a, Blockly.Python.ORDER_FUNCTION_CALL]
  328. }
  329. ;
  330. Blockly.Python.logic = {};
  331. Blockly.Python.controls_if = function (a) {
  332. var b = 0
  333. , c = "";
  334. do {
  335. var d = Blockly.Python.valueToCode(a, "IF" + b, Blockly.Python.ORDER_NONE) || "False";
  336. var e = Blockly.Python.statementToCode(a, "DO" + b) || Blockly.Python.PASS;
  337. c += (0 == b ? "if " : "elif ") + d + ":\n" + e;
  338. ++b
  339. } while (a.getInput("IF" + b)); a.getInput("ELSE") && (e = Blockly.Python.statementToCode(a, "ELSE") || Blockly.Python.PASS,
  340. c += "else:\n" + e);
  341. return c
  342. }
  343. ;
  344. Blockly.Python.logic_compare = function (a) {
  345. var b = {
  346. EQ: "==",
  347. NEQ: "!=",
  348. LT: "<",
  349. LTE: "<=",
  350. GT: ">",
  351. GTE: ">="
  352. }[a.getFieldValue("OP")]
  353. , c = Blockly.Python.ORDER_RELATIONAL
  354. , d = Blockly.Python.valueToCode(a, "A", c) || "0";
  355. a = Blockly.Python.valueToCode(a, "B", c) || "0";
  356. return [d + " " + b + " " + a, c]
  357. }
  358. ;
  359. Blockly.Python.logic_operation = function (a) {
  360. var b = "AND" == a.getFieldValue("OP") ? "and" : "or"
  361. , c = "and" == b ? Blockly.Python.ORDER_LOGICAL_AND : Blockly.Python.ORDER_LOGICAL_OR
  362. , d = Blockly.Python.valueToCode(a, "A", c);
  363. a = Blockly.Python.valueToCode(a, "B", c);
  364. if (d || a) {
  365. var e = "and" == b ? "True" : "False";
  366. d || (d = e);
  367. a || (a = e)
  368. } else
  369. a = d = "False";
  370. return [d + " " + b + " " + a, c]
  371. }
  372. ;
  373. Blockly.Python.logic_negate = function (a) {
  374. return ["not " + (Blockly.Python.valueToCode(a, "BOOL", Blockly.Python.ORDER_LOGICAL_NOT) || "True"), Blockly.Python.ORDER_LOGICAL_NOT]
  375. }
  376. ;
  377. Blockly.Python.logic_boolean = function (a) {
  378. return ["TRUE" == a.getFieldValue("BOOL") ? "True" : "False", Blockly.Python.ORDER_ATOMIC]
  379. }
  380. ;
  381. Blockly.Python.logic_null = function (a) {
  382. return ["None", Blockly.Python.ORDER_ATOMIC]
  383. }
  384. ;
  385. Blockly.Python.logic_ternary = function (a) {
  386. var b = Blockly.Python.valueToCode(a, "IF", Blockly.Python.ORDER_CONDITIONAL) || "False"
  387. , c = Blockly.Python.valueToCode(a, "THEN", Blockly.Python.ORDER_CONDITIONAL) || "None";
  388. a = Blockly.Python.valueToCode(a, "ELSE", Blockly.Python.ORDER_CONDITIONAL) || "None";
  389. return [c + " if " + b + " else " + a, Blockly.Python.ORDER_CONDITIONAL]
  390. }
  391. ;
  392. Blockly.Python.loops = {};
  393. Blockly.Python.controls_repeat_forever = function (a) {
  394. var b = Blockly.Python.statementToCode(a, "DO");
  395. b = Blockly.Python.addLoopTrap(b, a.id) || Blockly.Python.PASS;
  396. return "while True:\n" + b
  397. }
  398. ;
  399. Blockly.Python.controls_repeat_ext = function (a) {
  400. var b = a.getField("TIMES") ? String(parseInt(a.getFieldValue("TIMES"), 10)) : Blockly.Python.valueToCode(a, "TIMES", Blockly.Python.ORDER_NONE) || "0";
  401. b = Blockly.isNumber(b) ? parseInt(b, 10) : "int(" + b + ")";
  402. var c = Blockly.Python.statementToCode(a, "DO");
  403. c = Blockly.Python.addLoopTrap(c, a.id) || Blockly.Python.PASS;
  404. Blockly.Python.addVariable('count', "", true);
  405. return "for " + Blockly.Python.variableDB_.getDistinctName("count", Blockly.Variables.NAME_TYPE) + " in range(" + b + "):\n" + c
  406. }
  407. ;
  408. Blockly.Python.controls_repeat = Blockly.Python.controls_repeat_ext;
  409. Blockly.Python.controls_whileUntil = function (a) {
  410. var b = "UNTIL" == a.getFieldValue("MODE")
  411. , c = Blockly.Python.valueToCode(a, "BOOL", b ? Blockly.Python.ORDER_LOGICAL_NOT : Blockly.Python.ORDER_NONE) || "False"
  412. , d = Blockly.Python.statementToCode(a, "DO");
  413. d = Blockly.Python.addLoopTrap(d, a.id) || Blockly.Python.PASS;
  414. b && (c = "not " + c);
  415. return "while " + c + ":\n" + d
  416. }
  417. ;
  418. Blockly.Python.controls_for = function (a) {
  419. var b = Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE)
  420. , c = Blockly.Python.valueToCode(a, "FROM", Blockly.Python.ORDER_NONE) || "0"
  421. , d = Blockly.Python.valueToCode(a, "TO", Blockly.Python.ORDER_NONE) || "0"
  422. , e = Blockly.Python.valueToCode(a, "BY", Blockly.Python.ORDER_NONE) || "1"
  423. , f = Blockly.Python.statementToCode(a, "DO");
  424. f = Blockly.Python.addLoopTrap(f, a.id) || Blockly.Python.PASS;
  425. var g = ""
  426. , h = function () {
  427. return Blockly.Python.provideFunction_("upRange", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(start, stop, step):", " while start <= stop:", " yield start", " start += abs(step)"])
  428. }
  429. , k = function () {
  430. return Blockly.Python.provideFunction_("downRange", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(start, stop, step):", " while start >= stop:", " yield start", " start -= abs(step)"])
  431. };
  432. a = function (a, b, c) {
  433. return "(" + a + " <= " + b + ") and " + h() + "(" + a + ", " + b + ", " + c + ") or " + k() + "(" + a + ", " + b + ", " + c + ")"
  434. }
  435. ;
  436. if (Blockly.isNumber(c) && Blockly.isNumber(d) && Blockly.isNumber(e))
  437. c = parseFloat(c),
  438. d = parseFloat(d),
  439. e = Math.abs(parseFloat(e)),
  440. 0 === c % 1 && 0 === d % 1 && 0 === e % 1 ? (c <= d ? (d++,
  441. a = 0 == c && 1 == e ? d : c + ", " + d,
  442. 1 != e && (a += ", " + e)) : (d--,
  443. a = c + ", " + d + ", -" + e),
  444. a = "range(" + a + ")") : (a = c < d ? h() : k(),
  445. a += "(" + c + ", " + d + ", " + e + ")");
  446. else {
  447. var l = function (a, c) {
  448. if (Blockly.isNumber(a))
  449. a = parseFloat(a);
  450. else if (a.match(/^\w+$/))
  451. a = "int(" + a + ")";
  452. else {
  453. var d = Blockly.Python.variableDB_.getDistinctName(b + c, Blockly.Variables.NAME_TYPE);
  454. g += d + " = int(" + a + ")\n";
  455. a = d
  456. }
  457. return a
  458. };
  459. c = l(c, "_start");
  460. d = l(d, "_end");
  461. l(e, "_inc");
  462. a = "number" == typeof c && "number" == typeof d ? c < d ? h(c, d, e) : k(c, d, e) : a(c, d, e)
  463. }
  464. return g += "for " + b + " in " + a + ":\n" + f
  465. }
  466. ;
  467. Blockly.Python.controls_forEach = function (a) {
  468. var b = Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE)
  469. , c = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_RELATIONAL) || "[]"
  470. , d = Blockly.Python.statementToCode(a, "DO");
  471. d = Blockly.Python.addLoopTrap(d, a.id) || Blockly.Python.PASS;
  472. return "for " + b + " in " + c + ":\n" + d
  473. }
  474. ;
  475. Blockly.Python.controls_flow_statements = function (a) {
  476. switch (a.getFieldValue("FLOW")) {
  477. case "BREAK":
  478. return "break\n";
  479. case "CONTINUE":
  480. return "continue\n"
  481. }
  482. throw "Unknown flow statement.";
  483. }
  484. ;
  485. Blockly.Python.math = {};
  486. Blockly.Python.addReservedWords("math,random,Number");
  487. Blockly.Python.math_number = function (a) {
  488. a = parseFloat(a.getFieldValue("NUM"));
  489. if (Infinity == a) {
  490. a = 'float("inf")';
  491. var b = Blockly.Python.ORDER_FUNCTION_CALL
  492. } else
  493. -Infinity == a ? (a = '-float("inf")',
  494. b = Blockly.Python.ORDER_UNARY_SIGN) : b = 0 > a ? Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  495. return [a, b]
  496. }
  497. ;
  498. Blockly.Python.math_arithmetic = function (a) {
  499. var b = {
  500. ADD: [" + ", Blockly.Python.ORDER_ADDITIVE],
  501. MINUS: [" - ", Blockly.Python.ORDER_ADDITIVE],
  502. MULTIPLY: [" * ", Blockly.Python.ORDER_MULTIPLICATIVE],
  503. DIVIDE: [" / ", Blockly.Python.ORDER_MULTIPLICATIVE],
  504. POWER: [" ** ", Blockly.Python.ORDER_EXPONENTIATION]
  505. }[a.getFieldValue("OP")]
  506. , c = b[0];
  507. b = b[1];
  508. var d = Blockly.Python.valueToCode(a, "A", b) || "0";
  509. a = Blockly.Python.valueToCode(a, "B", b) || "0";
  510. return [d + c + a, b]
  511. }
  512. ;
  513. Blockly.Python.math_number_bits_ops = function (a) {
  514. var b = {
  515. " | ": [" | ", Blockly.Python.ORDER_BITWISE_OR],
  516. " & ": [" & ", Blockly.Python.ORDER_BITWISE_AND],
  517. " ^ ": [" ^ ", Blockly.Python.ORDER_BITWISE_XOR],
  518. " >> ": [" >> ", Blockly.Python.ORDER_BITWISE_SHIFT],
  519. " << ": [" << ", Blockly.Python.ORDER_BITWISE_SHIFT]
  520. }[a.getFieldValue("OP")]
  521. , c = b[0];
  522. b = b[1];
  523. var d = Blockly.Python.valueToCode(a, "A", b) || "0";
  524. a = Blockly.Python.valueToCode(a, "B", b) || "0";
  525. return [d + c + a, b]
  526. }
  527. ;
  528. Blockly.Python.math_single = function (a) {
  529. var b = a.getFieldValue("OP");
  530. if ("NEG" == b) {
  531. var c = Blockly.Python.valueToCode(a, "NUM", Blockly.Python.ORDER_UNARY_SIGN) || "0";
  532. return ["-" + c, Blockly.Python.ORDER_UNARY_SIGN]
  533. }
  534. "ROUND" != b && (Blockly.Python.definitions_.import_math = "import math");
  535. a = "SIN" == b || "COS" == b || "TAN" == b ? Blockly.Python.valueToCode(a, "NUM", Blockly.Python.ORDER_MULTIPLICATIVE) || "0" : Blockly.Python.valueToCode(a, "NUM", Blockly.Python.ORDER_NONE) || "0";
  536. switch (b) {
  537. case "ABS":
  538. c = "math.fabs(" + a + ")";
  539. break;
  540. case "ROOT":
  541. c = "math.sqrt(" + a + ")";
  542. break;
  543. case "LN":
  544. c = "math.log(" + a + ")";
  545. break;
  546. case "LOG10":
  547. c = "math.log10(" + a + ")";
  548. break;
  549. case "EXP":
  550. c = "math.exp(" + a + ")";
  551. break;
  552. case "POW10":
  553. c = "math.pow(10," + a + ")";
  554. break;
  555. case "ROUND":
  556. c = "round(" + a + ")";
  557. break;
  558. case "ROUNDUP":
  559. c = "math.ceil(" + a + ")";
  560. break;
  561. case "ROUNDDOWN":
  562. c = "math.floor(" + a + ")";
  563. break;
  564. case "SIN":
  565. c = "math.sin(math.radians(" + a + "))";
  566. break;
  567. case "COS":
  568. c = "math.cos(math.radians(" + a + "))";
  569. break;
  570. case "TAN":
  571. c = "math.tan(math.radians(" + a + "))"
  572. }
  573. if (c)
  574. return [c, Blockly.Python.ORDER_FUNCTION_CALL];
  575. switch (b) {
  576. case "ASIN":
  577. // c = "math.asin(" + a + ") / math.pi * 180";
  578. c = "math.asin(" + a + ")";
  579. break;
  580. case "ACOS":
  581. // c = "math.acos(" + a + ") / math.pi * 180";
  582. c = "math.acos(" + a + ")";
  583. break;
  584. case "ATAN":
  585. // c = "math.atan(" + a + ") / math.pi * 180";
  586. c = "math.atan(" + a + ")";
  587. break;
  588. default:
  589. throw "Unknown math operator: " + b;
  590. }
  591. return [c, Blockly.Python.ORDER_MULTIPLICATIVE]
  592. }
  593. ;
  594. Blockly.Python.math_constant = function (a) {
  595. var b = {
  596. PI: ["math.pi", Blockly.Python.ORDER_MEMBER],
  597. E: ["math.e", Blockly.Python.ORDER_MEMBER],
  598. GOLDEN_RATIO: ["(1 + math.sqrt(5)) / 2", Blockly.Python.ORDER_MULTIPLICATIVE],
  599. SQRT2: ["math.sqrt(2)", Blockly.Python.ORDER_MEMBER],
  600. SQRT1_2: ["math.sqrt(1.0 / 2)", Blockly.Python.ORDER_MEMBER],
  601. INFINITY: ["float('inf')", Blockly.Python.ORDER_ATOMIC]
  602. };
  603. a = a.getFieldValue("CONSTANT");
  604. "INFINITY" != a && (Blockly.Python.definitions_.import_math = "import math");
  605. return b[a]
  606. }
  607. ;
  608. Blockly.Python.math_number_property = function (a) {
  609. var b = Blockly.Python.valueToCode(a, "NUMBER_TO_CHECK", Blockly.Python.ORDER_MULTIPLICATIVE) || "0"
  610. , c = a.getFieldValue("PROPERTY");
  611. if ("PRIME" == c)
  612. return Blockly.Python.definitions_.import_math = "import math",
  613. Blockly.Python.definitions_.from_numbers_import_Number = "from numbers import Number",
  614. [Blockly.Python.provideFunction_("math_isPrime", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(n):", " # https://en.wikipedia.org/wiki/Primality_test#Naive_methods", " # If n is not a number but a string, try parsing it.", " if not isinstance(n, Number):", " try:", " n = float(n)", " except:", " return False", " if n == 2 or n == 3:", " return True", " # False if n is negative, is 1, or not whole, or if n is divisible by 2 or 3.", " if n <= 1 or n % 1 != 0 or n % 2 == 0 or n % 3 == 0:", " return False", " # Check all the numbers of form 6k +/- 1, up to sqrt(n).", " for x in range(6, int(math.sqrt(n)) + 2, 6):", " if n % (x - 1) == 0 or n % (x + 1) == 0:", " return False", " return True"]) + "(" + b + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  615. switch (c) {
  616. case "EVEN":
  617. var d = b + " % 2 == 0";
  618. break;
  619. case "ODD":
  620. d = b + " % 2 == 1";
  621. break;
  622. case "WHOLE":
  623. d = b + " % 1 == 0";
  624. break;
  625. case "POSITIVE":
  626. d = b + " > 0";
  627. break;
  628. case "NEGATIVE":
  629. d = b + " < 0";
  630. break;
  631. case "DIVISIBLE_BY":
  632. a = Blockly.Python.valueToCode(a, "DIVISOR", Blockly.Python.ORDER_MULTIPLICATIVE);
  633. if (!a || "0" == a)
  634. return ["False", Blockly.Python.ORDER_ATOMIC];
  635. d = b + " % " + a + " == 0"
  636. }
  637. return [d, Blockly.Python.ORDER_RELATIONAL]
  638. }
  639. ;
  640. Blockly.Python.math_change = function (a) {
  641. var b = Blockly.Python.valueToCode(a, "DELTA", Blockly.Python.ORDER_ADDITIVE) || "0";
  642. a = Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE);
  643. return a + " = " + a + " + " + b + "\n"
  644. }
  645. ;
  646. Blockly.Python.math_round = Blockly.Python.math_single;
  647. Blockly.Python.math_trig = Blockly.Python.math_single;
  648. Blockly.Python.math_on_list = function (a) {
  649. var b = a.getFieldValue("OP");
  650. a = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_NONE) || "[]";
  651. switch (b) {
  652. case "SUM":
  653. b = "sum(" + a + ")";
  654. break;
  655. case "MIN":
  656. b = "min(" + a + ")";
  657. break;
  658. case "MAX":
  659. b = "max(" + a + ")";
  660. break;
  661. case "AVERAGE":
  662. b = Blockly.Python.provideFunction_("math_mean", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(myList):", " return float(sum(myList)) / len(myList)"]);
  663. b = b + "(" + a + ")";
  664. break;
  665. case "MEDIAN":
  666. b = Blockly.Python.provideFunction_("math_median", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(myList):", " localList = sorted(myList)", " if not localList: return", " if len(localList) % 2 == 0:", " return (localList[len(localList) // 2 - 1] + localList[len(localList) // 2]) / 2.0", " else:", " return localList[(len(localList) - 1) // 2]"]);
  667. b = b + "(" + a + ")";
  668. break;
  669. case "MODE":
  670. b = Blockly.Python.provideFunction_("math_modes", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(some_list):", " modes = []", " # Using a lists of [item, count] to keep count rather than dict", ' # to avoid "unhashable" errors when the counted item is itself a list or dict.', " counts = []", " maxCount = 1", " for item in some_list:", " found = False", " for count in counts:", " if count[0] == item:", " count[1] += 1", " maxCount = max(maxCount, count[1])", " found = True", " if not found:", " counts.append([item, 1])", " for counted_item, item_count in counts:", " if item_count == maxCount:", " modes.append(counted_item)", " return modes"]);
  671. b = b + "(" + a + ")";
  672. break;
  673. case "STD_DEV":
  674. Blockly.Python.definitions_.import_math = "import math";
  675. b = Blockly.Python.provideFunction_("math_standard_deviation", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(numbers):", " n = len(numbers)", " if n == 0: return", " mean = float(sum(numbers)) / n", " variance = sum((x - mean) ** 2 for x in numbers) / n", " return math.sqrt(variance)"]);
  676. b = b + "(" + a + ")";
  677. break;
  678. case "RANDOM":
  679. Blockly.Python.definitions_.import_random = "import random";
  680. b = "random.choice(" + a + ")";
  681. break;
  682. default:
  683. throw "Unknown operator: " + b;
  684. }
  685. return [b, Blockly.Python.ORDER_FUNCTION_CALL]
  686. }
  687. ;
  688. Blockly.Python.math_modulo = function (a) {
  689. var b = Blockly.Python.valueToCode(a, "DIVIDEND", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  690. a = Blockly.Python.valueToCode(a, "DIVISOR", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  691. return [b + " % " + a, Blockly.Python.ORDER_MULTIPLICATIVE]
  692. }
  693. ;
  694. Blockly.Python.math_division_consult = function (a) {
  695. var b = Blockly.Python.valueToCode(a, "DIVIDEND", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  696. a = Blockly.Python.valueToCode(a, "DIVISOR", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  697. return [b + " // " + a, Blockly.Python.ORDER_MULTIPLICATIVE]
  698. }
  699. ;
  700. Blockly.Python.math_constrain = function (a) {
  701. var b = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "0"
  702. , c = Blockly.Python.valueToCode(a, "LOW", Blockly.Python.ORDER_NONE) || "0";
  703. a = Blockly.Python.valueToCode(a, "HIGH", Blockly.Python.ORDER_NONE) || "float('inf')";
  704. return ["min(max(" + b + ", " + c + "), " + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  705. }
  706. ;
  707. Blockly.Python.math_random_int = function (a) {
  708. // Blockly.Python.definitions_.import_time = "import time";
  709. // Blockly.Python.codeFunctions_.random_seed = "random.seed(time.ticks_cpu())";
  710. Blockly.Python.definitions_.import_random = "import random";
  711. var b = Blockly.Python.valueToCode(a, "FROM", Blockly.Python.ORDER_NONE) || "0";
  712. a = Blockly.Python.valueToCode(a, "TO", Blockly.Python.ORDER_NONE) || "0";
  713. return ["random.randint(" + b + ", " + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  714. }
  715. ;
  716. Blockly.Python.math_random_float = function (a) {
  717. // Blockly.Python.definitions_.import_time = "import time";
  718. // Blockly.Python.codeFunctions_.random_seed = "random.seed(time.ticks_cpu())";
  719. Blockly.Python.definitions_.import_random = "import random";
  720. return ["random.random()", Blockly.Python.ORDER_FUNCTION_CALL]
  721. }
  722. ;
  723. Blockly.Python.math_convert = function (a) {
  724. var b = a.getFieldValue("TYPE0");
  725. a = Blockly.Python.valueToCode(a, "INPUT0", Blockly.Python.ORDER_ATOMIC);
  726. return [b + "(" + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  727. }
  728. ;
  729. Blockly.Python.procedures = {};
  730. Blockly.Python.procedures_defreturn = function (a) {
  731. for (var b = [], c = 0, d; d = a.workspace.variableList[c]; c++)
  732. -1 == a.arguments_.indexOf(d) && b.push(Blockly.Python.variableDB_.getName(d, Blockly.Variables.NAME_TYPE));
  733. b = b.length ? " global " + b.join(", ") + "\n" : "";
  734. d = Blockly.Python.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE);
  735. var e = Blockly.Python.statementToCode(a, "STACK");
  736. Blockly.Python.STATEMENT_PREFIX && (e = Blockly.Python.prefixLines(Blockly.Python.STATEMENT_PREFIX.replace(/%1/g, "'" + a.id + "'"), Blockly.Python.INDENT) + e);
  737. Blockly.Python.INFINITE_LOOP_TRAP && (e = Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g, '"' + a.id + '"') + e);
  738. var f = Blockly.Python.valueToCode(a, "RETURN", Blockly.Python.ORDER_NONE) || "";
  739. f ? f = " return " + f + "\n" : e || (e = Blockly.Python.PASS);
  740. var g = [];
  741. for (c = 0; c < a.arguments_.length; c++)
  742. g[c] = Blockly.Python.variableDB_.getName(a.arguments_[c], Blockly.Variables.NAME_TYPE);
  743. b = "def " + d + "(" + g.join(", ") + "):\n" + b + e + f;
  744. b = Blockly.Python.scrub_(a, b);
  745. Blockly.Python.definitions_["%" + d] = b;
  746. return null
  747. }
  748. ;
  749. Blockly.Python.procedures_defthirdreturn = function (a) {
  750. for (var b = [], c = 0, d; d = a.workspace.variableList[c]; c++)
  751. -1 == a.arguments_.indexOf(d) && b.push(Blockly.Python.variableDB_.getName(d, Blockly.Variables.NAME_TYPE));
  752. b = b.length ? " global " + b.join(", ") + "\n" : "";
  753. d = Blockly.Python.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE);
  754. var e = Blockly.Python.statementToCode(a, "STACK");
  755. Blockly.Python.STATEMENT_PREFIX && (e = Blockly.Python.prefixLines(Blockly.Python.STATEMENT_PREFIX.replace(/%1/g, "'" + a.id + "'"), Blockly.Python.INDENT) + e);
  756. Blockly.Python.INFINITE_LOOP_TRAP && (e = Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g, '"' + a.id + '"') + e);
  757. var f = Blockly.Python.valueToCode(a, "RETURN", Blockly.Python.ORDER_NONE) || "";
  758. f ? f = " return " + f + "\n" : e || (e = Blockly.Python.PASS);
  759. var g = [];
  760. for (c = 0; c < a.arguments_.length; c++)
  761. g[c] = Blockly.Python.variableDB_.getName(a.arguments_[c], Blockly.Variables.NAME_TYPE);
  762. b = "def " + d + "(" + g.join(", ") + "):\n" + b + e + f;
  763. b = Blockly.Python.scrub_(a, b);
  764. Blockly.Python.definitions_["%" + d] = b;
  765. return null
  766. }
  767. ;
  768. Blockly.Python.procedures_defnoreturn = Blockly.Python.procedures_defreturn;
  769. Blockly.Python.procedures_callreturn = function (a) {
  770. for (var b = Blockly.Python.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE), c = [], d = 0; d < a.arguments_.length; d++)
  771. c[d] = Blockly.Python.valueToCode(a, "ARG" + d, Blockly.Python.ORDER_NONE) || "None";
  772. return [b + "(" + c.join(", ") + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  773. }
  774. ;
  775. Blockly.Python.procedures_callnoreturn = function (a) {
  776. for (var b = Blockly.Python.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE), c = [], d = 0; d < a.arguments_.length; d++)
  777. c[d] = Blockly.Python.valueToCode(a, "ARG" + d, Blockly.Python.ORDER_NONE) || "None";
  778. return b + "(" + c.join(", ") + ")\n"
  779. }
  780. ;
  781. Blockly.Python.procedures_callNothirdreturn = function (a) {
  782. for (var b = Blockly.Python.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE), c = [], d = 0; d < a.arguments_.length; d++)
  783. c[d] = Blockly.Python.valueToCode(a, "ARG" + d, Blockly.Python.ORDER_NONE) || "None";
  784. return [b + "()", Blockly.Python.ORDER_FUNCTION_CALL]
  785. }
  786. Blockly.Python.procedures_ifreturn = function (a) {
  787. var b = "if " + (Blockly.Python.valueToCode(a, "CONDITION", Blockly.Python.ORDER_NONE) || "False") + ":\n";
  788. a.hasReturnValue_ ? (a = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "None",
  789. b += " return " + a + "\n") : b += " return\n";
  790. return b
  791. }
  792. ;
  793. Blockly.Python.texts = {};
  794. Blockly.Python.text = function (a) {
  795. return [Blockly.Python.quote_(a.getFieldValue("TEXT")), Blockly.Python.ORDER_ATOMIC]
  796. }
  797. ;
  798. Blockly.Python.text_list = function (a) {
  799. return ["[" + Blockly.Python.quote_empty(a.getFieldValue("TEXT")) + "]", Blockly.Python.ORDER_ATOMIC]
  800. }
  801. ;
  802. Blockly.Python.text_tuple = function (a) {
  803. return ["(" + Blockly.Python.quote_empty(a.getFieldValue("TEXT")) + ")", Blockly.Python.ORDER_ATOMIC]
  804. }
  805. ;
  806. Blockly.Python.text_dict = function (a) {
  807. return ["{" + Blockly.Python.quote_empty(a.getFieldValue("TEXT")) + "}", Blockly.Python.ORDER_ATOMIC]
  808. }
  809. ;
  810. Blockly.Python.text_join = function (a) {
  811. switch (a.itemCount_) {
  812. case 0:
  813. return ['""', Blockly.Python.ORDER_ATOMIC];
  814. case 1:
  815. return ["str(" + (Blockly.Python.valueToCode(a, "ADD0", Blockly.Python.ORDER_NONE) || "''") + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  816. case 2:
  817. var b = Blockly.Python.valueToCode(a, "ADD0", Blockly.Python.ORDER_NONE) || "''";
  818. a = Blockly.Python.valueToCode(a, "ADD1", Blockly.Python.ORDER_NONE) || "''";
  819. return ["str(" + b + ") + str(" + a + ")", Blockly.Python.ORDER_ADDITIVE];
  820. default:
  821. b = [];
  822. for (var c = 0; c < a.itemCount_; c++)
  823. b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "''";
  824. a = Blockly.Python.variableDB_.getDistinctName("x", Blockly.Variables.NAME_TYPE);
  825. a = '"".join([str(' + a + ') for ' + a + ' in [' + b.join(", ") + ']])';
  826. return [a, Blockly.Python.ORDER_FUNCTION_CALL]
  827. }
  828. }
  829. ;
  830. Blockly.Python.text_append = function (a) {
  831. var b = Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE);
  832. a = Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_NONE) || "''";
  833. return b + " = str(" + b + ") + str(" + a + ")\n"
  834. }
  835. ;
  836. Blockly.Python.text_length = function (a) {
  837. return ["len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "''") + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  838. }
  839. ;
  840. Blockly.Python.text_isEmpty = function (a) {
  841. return ["not len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "''") + ")", Blockly.Python.ORDER_LOGICAL_NOT]
  842. }
  843. ;
  844. Blockly.Python.text_indexOf = function (a) {
  845. var b = "FIRST" == a.getFieldValue("END") ? "find" : "rfind"
  846. , c = Blockly.Python.valueToCode(a, "FIND", Blockly.Python.ORDER_NONE) || "''";
  847. b = (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_MEMBER) || "''") + "." + b + "(" + c + ")";
  848. return a.workspace.options.oneBasedIndex ? [b + " + 1", Blockly.Python.ORDER_ADDITIVE] : [b, Blockly.Python.ORDER_FUNCTION_CALL]
  849. }
  850. ;
  851. Blockly.Python.text_charAt = function (a) {
  852. var b = a.getFieldValue("WHERE") || "FROM_START"
  853. , c = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_MEMBER) || "''";
  854. switch (b) {
  855. case "FIRST":
  856. return [c + "[0]", Blockly.Python.ORDER_MEMBER];
  857. case "LAST":
  858. return [c + "[-1]", Blockly.Python.ORDER_MEMBER];
  859. case "FROM_START":
  860. return a = Blockly.Python.getAdjustedInt(a, "AT"),
  861. [c + "[" + a + "]", Blockly.Python.ORDER_MEMBER];
  862. case "FROM_END":
  863. return a = Blockly.Python.getAdjustedInt(a, "AT", 1, !0),
  864. [c + "[" + a + "]", Blockly.Python.ORDER_MEMBER];
  865. case "RANDOM":
  866. return Blockly.Python.definitions_.import_random = "import random",
  867. [Blockly.Python.provideFunction_("text_random_letter", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(text):", " x = int(random.random() * len(text))", " return text[x];"]) + "(" + c + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  868. }
  869. throw "Unhandled option (text_charAt).";
  870. }
  871. ;
  872. Blockly.Python.text_getSubstring = function (a) {
  873. var b = a.getFieldValue("WHERE1")
  874. , c = a.getFieldValue("WHERE2")
  875. , d = Blockly.Python.valueToCode(a, "STRING", Blockly.Python.ORDER_MEMBER) || "''";
  876. switch (b) {
  877. case "FROM_START":
  878. b = Blockly.Python.getAdjustedInt(a, "AT1");
  879. "0" == b && (b = "");
  880. break;
  881. case "FROM_END":
  882. b = Blockly.Python.getAdjustedInt(a, "AT1", 1, !0);
  883. break;
  884. case "FIRST":
  885. b = "";
  886. break;
  887. default:
  888. throw "Unhandled option (text_getSubstring)";
  889. }
  890. switch (c) {
  891. case "FROM_START":
  892. a = Blockly.Python.getAdjustedInt(a, "AT2", 1);
  893. break;
  894. case "FROM_END":
  895. a = Blockly.Python.getAdjustedInt(a, "AT2", 0, !0);
  896. Blockly.isNumber(String(a)) ? "0" == a && (a = "") : (Blockly.Python.definitions_.import_sys = "import sys",
  897. a += " or sys.maxsize");
  898. break;
  899. case "LAST":
  900. a = "";
  901. break;
  902. default:
  903. throw "Unhandled option (text_getSubstring)";
  904. }
  905. return [d + "[" + b + " : " + a + "]", Blockly.Python.ORDER_MEMBER]
  906. }
  907. ;
  908. Blockly.Python.text_changeCase = function (a) {
  909. var b = {
  910. UPPERCASE: ".upper()",
  911. LOWERCASE: ".lower()",
  912. TITLECASE: ".title()"
  913. }[a.getFieldValue("CASE")];
  914. return [(Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_MEMBER) || "''") + b, Blockly.Python.ORDER_FUNCTION_CALL]
  915. }
  916. ;
  917. Blockly.Python.text_trim = function (a) {
  918. var b = {
  919. LEFT: ".lstrip()",
  920. RIGHT: ".rstrip()",
  921. BOTH: ".strip()"
  922. }[a.getFieldValue("MODE")];
  923. return [(Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_MEMBER) || "''") + b, Blockly.Python.ORDER_FUNCTION_CALL]
  924. }
  925. ;
  926. Blockly.Python.text_print = function (a) {
  927. return "print(" + (Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_NONE) || "''") + ")\n"
  928. }
  929. ;
  930. Blockly.Python.text_prompt_ext = function (a) {
  931. var b = Blockly.Python.provideFunction_("text_prompt", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(msg):", " try:", " return raw_input(msg)", " except NameError:", " return input(msg)"])
  932. , c = a.getField("TEXT") ? Blockly.Python.quote_(a.getFieldValue("TEXT")) : Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_NONE) || "''";
  933. b = b + "(" + c + ")";
  934. "NUMBER" == a.getFieldValue("TYPE") && (b = "float(" + b + ")");
  935. return [b, Blockly.Python.ORDER_FUNCTION_CALL]
  936. }
  937. ;
  938. Blockly.Python.text_prompt = Blockly.Python.text_prompt_ext;
  939. Blockly.Python.variables = {};
  940. Blockly.Python.variables_get = function (a) {
  941. return [Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE), Blockly.Python.ORDER_ATOMIC]
  942. }
  943. ;
  944. Blockly.Python.variables_set = function (a) {
  945. var b = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "0";
  946. return Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE) + " = " + b + "\n"
  947. }
  948. ;