python_compressed.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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_repeat = function (a) {
  135. var b = Blockly.Python.valueToCode(a, "ITEM", Blockly.Python.ORDER_NONE) || "None";
  136. a = Blockly.Python.valueToCode(a, "NUM", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  137. return ["[" + b + "] * " + a, Blockly.Python.ORDER_MULTIPLICATIVE]
  138. }
  139. ;
  140. Blockly.Python.lists_length = function (a) {
  141. return ["len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "[]") + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  142. }
  143. ;
  144. Blockly.Python.lists_isEmpty = function (a) {
  145. return ["not len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "[]") + ")", Blockly.Python.ORDER_LOGICAL_NOT]
  146. }
  147. ;
  148. Blockly.Python.lists_indexOf = function (a) {
  149. var b = Blockly.Python.valueToCode(a, "FIND", Blockly.Python.ORDER_NONE) || "[]"
  150. , c = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "''";
  151. if (a.workspace.options.oneBasedIndex)
  152. var d = " 0"
  153. , e = " + 1"
  154. , f = "";
  155. else
  156. d = " -1",
  157. e = "",
  158. f = " - 1";
  159. if ("FIRST" == a.getFieldValue("END"))
  160. 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"]),
  161. [a + "(" + c + ", " + b + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  162. 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"]);
  163. return [a + "(" + c + ", " + b + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  164. }
  165. ;
  166. Blockly.Python.lists_getIndex = function (a) {
  167. var b = a.getFieldValue("MODE") || "GET"
  168. , c = a.getFieldValue("WHERE") || "FROM_START"
  169. , d = Blockly.Python.valueToCode(a, "VALUE", "RANDOM" == c ? Blockly.Python.ORDER_NONE : Blockly.Python.ORDER_MEMBER) || "[]";
  170. switch (c) {
  171. case "FIRST":
  172. if ("GET" == b)
  173. return [d + "[0]", Blockly.Python.ORDER_MEMBER];
  174. if ("GET_REMOVE" == b)
  175. return [d + ".pop(0)", Blockly.Python.ORDER_FUNCTION_CALL];
  176. if ("REMOVE" == b)
  177. return d + ".pop(0)\n";
  178. break;
  179. case "LAST":
  180. if ("GET" == b)
  181. return [d + "[-1]", Blockly.Python.ORDER_MEMBER];
  182. if ("GET_REMOVE" == b)
  183. return [d + ".pop()", Blockly.Python.ORDER_FUNCTION_CALL];
  184. if ("REMOVE" == b)
  185. return d + ".pop()\n";
  186. break;
  187. case "FROM_START":
  188. a = Blockly.Python.valueToCode(a, "AT", Blockly.Python.ORDER_ATOMIC);
  189. if ("GET" == b)
  190. return [d + "[" + a + "]", Blockly.Python.ORDER_MEMBER];
  191. if ("GET_REMOVE" == b)
  192. return [d + ".pop(" + a + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  193. if ("REMOVE" == b)
  194. return d + ".pop(" + a + ")\n";
  195. break;
  196. case "FROM_END":
  197. a = Blockly.Python.getAdjustedInt(a, "AT", 1, !0);
  198. if ("GET" == b)
  199. return [d + "[" + a + "]", Blockly.Python.ORDER_MEMBER];
  200. if ("GET_REMOVE" == b)
  201. return [d + ".pop(" + a + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  202. if ("REMOVE" == b)
  203. return d + ".pop(" + a + ")\n";
  204. break;
  205. case "RANDOM":
  206. Blockly.Python.definitions_.import_random = "import random";
  207. if ("GET" == b)
  208. return ["random.choice(" + d + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  209. 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 + ")";
  210. if ("GET_REMOVE" == b)
  211. return [d, Blockly.Python.ORDER_FUNCTION_CALL];
  212. if ("REMOVE" == b)
  213. return d + "\n"
  214. }
  215. throw "Unhandled combination (lists_getIndex).";
  216. }
  217. ;
  218. Blockly.Python.lists_setIndex = function (a) {
  219. var b = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_MEMBER) || "[]"
  220. , c = a.getFieldValue("MODE") || "GET"
  221. , d = a.getFieldValue("WHERE") || "FROM_START"
  222. , e = Blockly.Python.valueToCode(a, "TO", Blockly.Python.ORDER_NONE) || "None";
  223. switch (d) {
  224. case "FIRST":
  225. if ("SET" == c)
  226. return b + "[0] = " + e + "\n";
  227. if ("INSERT" == c)
  228. return b + ".insert(0, " + e + ")\n";
  229. break;
  230. case "LAST":
  231. if ("SET" == c)
  232. return b + "[-1] = " + e + "\n";
  233. if ("INSERT" == c)
  234. return b + ".append(" + e + ")\n";
  235. break;
  236. case "FROM_START":
  237. a = Blockly.Python.getAdjustedInt(a, "AT");
  238. if ("SET" == c)
  239. return b + "[" + a + "] = " + e + "\n";
  240. if ("INSERT" == c)
  241. return b + ".insert(" + a + ", " + e + ")\n";
  242. break;
  243. case "FROM_END":
  244. a = Blockly.Python.getAdjustedInt(a, "AT", 1, !0);
  245. if ("SET" == c)
  246. return b + "[" + a + "] = " + e + "\n";
  247. if ("INSERT" == c)
  248. return b + ".insert(" + a + ", " + e + ")\n";
  249. break;
  250. case "RANDOM":
  251. Blockly.Python.definitions_.import_random = "import random";
  252. b.match(/^\w+$/) ? a = "" : (a = Blockly.Python.variableDB_.getDistinctName("tmp_list", Blockly.Variables.NAME_TYPE),
  253. d = a + " = " + b + "\n",
  254. b = a,
  255. a = d);
  256. d = Blockly.Python.variableDB_.getDistinctName("tmp_x", Blockly.Variables.NAME_TYPE);
  257. a += d + " = int(random.random() * len(" + b + "))\n";
  258. if ("SET" == c)
  259. return a + (b + "[" + d + "] = " + e + "\n");
  260. if ("INSERT" == c)
  261. return a + (b + ".insert(" + d + ", " + e + ")\n")
  262. }
  263. throw "Unhandled combination (lists_setIndex).";
  264. }
  265. ;
  266. Blockly.Python.lists_getSublist = function (a) {
  267. var b = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_MEMBER) || "[]"
  268. , c = a.getFieldValue("WHERE1")
  269. , d = a.getFieldValue("WHERE2");
  270. switch (c) {
  271. case "FROM_START":
  272. c = Blockly.Python.getAdjustedInt(a, "AT1");
  273. "0" == c && (c = "");
  274. break;
  275. case "FROM_END":
  276. c = Blockly.Python.getAdjustedInt(a, "AT1", 1, !0);
  277. break;
  278. case "FIRST":
  279. c = "";
  280. break;
  281. default:
  282. throw "Unhandled option (lists_getSublist)";
  283. }
  284. switch (d) {
  285. case "FROM_START":
  286. a = Blockly.Python.getAdjustedInt(a, "AT2", 1);
  287. break;
  288. case "FROM_END":
  289. a = Blockly.Python.getAdjustedInt(a, "AT2", 0, !0);
  290. Blockly.isNumber(String(a)) ? "0" == a && (a = "") : (Blockly.Python.definitions_.import_sys = "import sys",
  291. a += " or sys.maxsize");
  292. break;
  293. case "LAST":
  294. a = "";
  295. break;
  296. default:
  297. throw "Unhandled option (lists_getSublist)";
  298. }
  299. return [b + "[" + c + " : " + a + "]", Blockly.Python.ORDER_MEMBER]
  300. }
  301. ;
  302. Blockly.Python.lists_sort = function (a) {
  303. var b = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_NONE) || "[]"
  304. , c = a.getFieldValue("TYPE");
  305. a = "1" === a.getFieldValue("DIRECTION") ? "False" : "True";
  306. 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]
  307. }
  308. ;
  309. Blockly.Python.lists_split = function (a) {
  310. var b = a.getFieldValue("MODE");
  311. if ("SPLIT" == b)
  312. b = Blockly.Python.valueToCode(a, "INPUT", Blockly.Python.ORDER_MEMBER) || "''",
  313. a = Blockly.Python.valueToCode(a, "DELIM", Blockly.Python.ORDER_NONE),
  314. a = b + ".split(" + a + ")";
  315. else if ("JOIN" == b)
  316. b = Blockly.Python.valueToCode(a, "INPUT", Blockly.Python.ORDER_NONE) || "[]",
  317. a = Blockly.Python.valueToCode(a, "DELIM", Blockly.Python.ORDER_MEMBER) || "''",
  318. a = a + ".join(" + b + ")";
  319. else
  320. throw "Unknown mode: " + b;
  321. return [a, Blockly.Python.ORDER_FUNCTION_CALL]
  322. }
  323. ;
  324. Blockly.Python.logic = {};
  325. Blockly.Python.controls_if = function (a) {
  326. var b = 0
  327. , c = "";
  328. do {
  329. var d = Blockly.Python.valueToCode(a, "IF" + b, Blockly.Python.ORDER_NONE) || "False";
  330. var e = Blockly.Python.statementToCode(a, "DO" + b) || Blockly.Python.PASS;
  331. c += (0 == b ? "if " : "elif ") + d + ":\n" + e;
  332. ++b
  333. } while (a.getInput("IF" + b)); a.getInput("ELSE") && (e = Blockly.Python.statementToCode(a, "ELSE") || Blockly.Python.PASS,
  334. c += "else:\n" + e);
  335. return c
  336. }
  337. ;
  338. Blockly.Python.logic_compare = function (a) {
  339. var b = {
  340. EQ: "==",
  341. NEQ: "!=",
  342. LT: "<",
  343. LTE: "<=",
  344. GT: ">",
  345. GTE: ">="
  346. }[a.getFieldValue("OP")]
  347. , c = Blockly.Python.ORDER_RELATIONAL
  348. , d = Blockly.Python.valueToCode(a, "A", c) || "0";
  349. a = Blockly.Python.valueToCode(a, "B", c) || "0";
  350. return [d + " " + b + " " + a, c]
  351. }
  352. ;
  353. Blockly.Python.logic_operation = function (a) {
  354. var b = "AND" == a.getFieldValue("OP") ? "and" : "or"
  355. , c = "and" == b ? Blockly.Python.ORDER_LOGICAL_AND : Blockly.Python.ORDER_LOGICAL_OR
  356. , d = Blockly.Python.valueToCode(a, "A", c);
  357. a = Blockly.Python.valueToCode(a, "B", c);
  358. if (d || a) {
  359. var e = "and" == b ? "True" : "False";
  360. d || (d = e);
  361. a || (a = e)
  362. } else
  363. a = d = "False";
  364. return [d + " " + b + " " + a, c]
  365. }
  366. ;
  367. Blockly.Python.logic_negate = function (a) {
  368. return ["not " + (Blockly.Python.valueToCode(a, "BOOL", Blockly.Python.ORDER_LOGICAL_NOT) || "True"), Blockly.Python.ORDER_LOGICAL_NOT]
  369. }
  370. ;
  371. Blockly.Python.logic_boolean = function (a) {
  372. return ["TRUE" == a.getFieldValue("BOOL") ? "True" : "False", Blockly.Python.ORDER_ATOMIC]
  373. }
  374. ;
  375. Blockly.Python.logic_null = function (a) {
  376. return ["None", Blockly.Python.ORDER_ATOMIC]
  377. }
  378. ;
  379. Blockly.Python.logic_ternary = function (a) {
  380. var b = Blockly.Python.valueToCode(a, "IF", Blockly.Python.ORDER_CONDITIONAL) || "False"
  381. , c = Blockly.Python.valueToCode(a, "THEN", Blockly.Python.ORDER_CONDITIONAL) || "None";
  382. a = Blockly.Python.valueToCode(a, "ELSE", Blockly.Python.ORDER_CONDITIONAL) || "None";
  383. return [c + " if " + b + " else " + a, Blockly.Python.ORDER_CONDITIONAL]
  384. }
  385. ;
  386. Blockly.Python.loops = {};
  387. Blockly.Python.controls_repeat_forever = function (a) {
  388. var b = Blockly.Python.statementToCode(a, "DO");
  389. b = Blockly.Python.addLoopTrap(b, a.id) || Blockly.Python.PASS;
  390. return "while True:\n" + b
  391. }
  392. ;
  393. Blockly.Python.controls_repeat_ext = function (a) {
  394. var b = a.getField("TIMES") ? String(parseInt(a.getFieldValue("TIMES"), 10)) : Blockly.Python.valueToCode(a, "TIMES", Blockly.Python.ORDER_NONE) || "0";
  395. b = Blockly.isNumber(b) ? parseInt(b, 10) : "int(" + b + ")";
  396. var c = Blockly.Python.statementToCode(a, "DO");
  397. c = Blockly.Python.addLoopTrap(c, a.id) || Blockly.Python.PASS;
  398. return "for " + Blockly.Python.variableDB_.getDistinctName("count", Blockly.Variables.NAME_TYPE) + " in range(" + b + "):\n" + c
  399. }
  400. ;
  401. Blockly.Python.controls_repeat = Blockly.Python.controls_repeat_ext;
  402. Blockly.Python.controls_whileUntil = function (a) {
  403. var b = "UNTIL" == a.getFieldValue("MODE")
  404. , c = Blockly.Python.valueToCode(a, "BOOL", b ? Blockly.Python.ORDER_LOGICAL_NOT : Blockly.Python.ORDER_NONE) || "False"
  405. , d = Blockly.Python.statementToCode(a, "DO");
  406. d = Blockly.Python.addLoopTrap(d, a.id) || Blockly.Python.PASS;
  407. b && (c = "not " + c);
  408. return "while " + c + ":\n" + d
  409. }
  410. ;
  411. Blockly.Python.controls_for = function (a) {
  412. var b = Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE)
  413. , c = Blockly.Python.valueToCode(a, "FROM", Blockly.Python.ORDER_NONE) || "0"
  414. , d = Blockly.Python.valueToCode(a, "TO", Blockly.Python.ORDER_NONE) || "0"
  415. , e = Blockly.Python.valueToCode(a, "BY", Blockly.Python.ORDER_NONE) || "1"
  416. , f = Blockly.Python.statementToCode(a, "DO");
  417. f = Blockly.Python.addLoopTrap(f, a.id) || Blockly.Python.PASS;
  418. var g = ""
  419. , h = function () {
  420. return Blockly.Python.provideFunction_("upRange", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(start, stop, step):", " while start <= stop:", " yield start", " start += abs(step)"])
  421. }
  422. , k = function () {
  423. return Blockly.Python.provideFunction_("downRange", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(start, stop, step):", " while start >= stop:", " yield start", " start -= abs(step)"])
  424. };
  425. a = function (a, b, c) {
  426. return "(" + a + " <= " + b + ") and " + h() + "(" + a + ", " + b + ", " + c + ") or " + k() + "(" + a + ", " + b + ", " + c + ")"
  427. }
  428. ;
  429. if (Blockly.isNumber(c) && Blockly.isNumber(d) && Blockly.isNumber(e))
  430. c = parseFloat(c),
  431. d = parseFloat(d),
  432. e = Math.abs(parseFloat(e)),
  433. 0 === c % 1 && 0 === d % 1 && 0 === e % 1 ? (c <= d ? (d++ ,
  434. a = 0 == c && 1 == e ? d : c + ", " + d,
  435. 1 != e && (a += ", " + e)) : (d-- ,
  436. a = c + ", " + d + ", -" + e),
  437. a = "range(" + a + ")") : (a = c < d ? h() : k(),
  438. a += "(" + c + ", " + d + ", " + e + ")");
  439. else {
  440. var l = function (a, c) {
  441. if (Blockly.isNumber(a))
  442. a = parseFloat(a);
  443. else if (a.match(/^\w+$/))
  444. a = "int(" + a + ")";
  445. else {
  446. var d = Blockly.Python.variableDB_.getDistinctName(b + c, Blockly.Variables.NAME_TYPE);
  447. g += d + " = int(" + a + ")\n";
  448. a = d
  449. }
  450. return a
  451. };
  452. c = l(c, "_start");
  453. d = l(d, "_end");
  454. l(e, "_inc");
  455. a = "number" == typeof c && "number" == typeof d ? c < d ? h(c, d, e) : k(c, d, e) : a(c, d, e)
  456. }
  457. return g += "for " + b + " in " + a + ":\n" + f
  458. }
  459. ;
  460. Blockly.Python.controls_forEach = function (a) {
  461. var b = Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE)
  462. , c = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_RELATIONAL) || "[]"
  463. , d = Blockly.Python.statementToCode(a, "DO");
  464. d = Blockly.Python.addLoopTrap(d, a.id) || Blockly.Python.PASS;
  465. return "for " + b + " in " + c + ":\n" + d
  466. }
  467. ;
  468. Blockly.Python.controls_flow_statements = function (a) {
  469. switch (a.getFieldValue("FLOW")) {
  470. case "BREAK":
  471. return "break\n";
  472. case "CONTINUE":
  473. return "continue\n"
  474. }
  475. throw "Unknown flow statement.";
  476. }
  477. ;
  478. Blockly.Python.math = {};
  479. Blockly.Python.addReservedWords("math,random,Number");
  480. Blockly.Python.math_number = function (a) {
  481. a = parseFloat(a.getFieldValue("NUM"));
  482. if (Infinity == a) {
  483. a = 'float("inf")';
  484. var b = Blockly.Python.ORDER_FUNCTION_CALL
  485. } else
  486. -Infinity == a ? (a = '-float("inf")',
  487. b = Blockly.Python.ORDER_UNARY_SIGN) : b = 0 > a ? Blockly.Python.ORDER_UNARY_SIGN : Blockly.Python.ORDER_ATOMIC;
  488. return [a, b]
  489. }
  490. ;
  491. Blockly.Python.math_arithmetic = function (a) {
  492. var b = {
  493. ADD: [" + ", Blockly.Python.ORDER_ADDITIVE],
  494. MINUS: [" - ", Blockly.Python.ORDER_ADDITIVE],
  495. MULTIPLY: [" * ", Blockly.Python.ORDER_MULTIPLICATIVE],
  496. DIVIDE: [" / ", Blockly.Python.ORDER_MULTIPLICATIVE],
  497. POWER: [" ** ", Blockly.Python.ORDER_EXPONENTIATION]
  498. }[a.getFieldValue("OP")]
  499. , c = b[0];
  500. b = b[1];
  501. var d = Blockly.Python.valueToCode(a, "A", b) || "0";
  502. a = Blockly.Python.valueToCode(a, "B", b) || "0";
  503. return [d + c + a, b]
  504. }
  505. ;
  506. Blockly.Python.math_number_bits_ops = function (a) {
  507. var b = {
  508. " | ": [" | ", Blockly.Python.ORDER_BITWISE_OR],
  509. " & ": [" & ", Blockly.Python.ORDER_BITWISE_AND],
  510. " ^ ": [" ^ ", Blockly.Python.ORDER_BITWISE_XOR],
  511. " >> ": [" >> ", Blockly.Python.ORDER_BITWISE_SHIFT],
  512. " << ": [" << ", Blockly.Python.ORDER_BITWISE_SHIFT]
  513. }[a.getFieldValue("OP")]
  514. , c = b[0];
  515. b = b[1];
  516. var d = Blockly.Python.valueToCode(a, "A", b) || "0";
  517. a = Blockly.Python.valueToCode(a, "B", b) || "0";
  518. return [d + c + a, b]
  519. }
  520. ;
  521. Blockly.Python.math_single = function (a) {
  522. var b = a.getFieldValue("OP");
  523. if ("NEG" == b) {
  524. var c = Blockly.Python.valueToCode(a, "NUM", Blockly.Python.ORDER_UNARY_SIGN) || "0";
  525. return ["-" + c, Blockly.Python.ORDER_UNARY_SIGN]
  526. }
  527. "ROUND" != b && (Blockly.Python.definitions_.import_math = "import math");
  528. 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";
  529. switch (b) {
  530. case "ABS":
  531. c = "math.fabs(" + a + ")";
  532. break;
  533. case "ROOT":
  534. c = "math.sqrt(" + a + ")";
  535. break;
  536. case "LN":
  537. c = "math.log(" + a + ")";
  538. break;
  539. case "LOG10":
  540. c = "math.log10(" + a + ")";
  541. break;
  542. case "EXP":
  543. c = "math.exp(" + a + ")";
  544. break;
  545. case "POW10":
  546. c = "math.pow(10," + a + ")";
  547. break;
  548. case "ROUND":
  549. c = "round(" + a + ")";
  550. break;
  551. case "ROUNDUP":
  552. c = "math.ceil(" + a + ")";
  553. break;
  554. case "ROUNDDOWN":
  555. c = "math.floor(" + a + ")";
  556. break;
  557. case "SIN":
  558. c = "math.sin(math.radians(" + a + "))";
  559. break;
  560. case "COS":
  561. c = "math.cos(math.radians(" + a + "))";
  562. break;
  563. case "TAN":
  564. c = "math.tan(math.radians(" + a + "))"
  565. }
  566. if (c)
  567. return [c, Blockly.Python.ORDER_FUNCTION_CALL];
  568. switch (b) {
  569. case "ASIN":
  570. // c = "math.asin(" + a + ") / math.pi * 180";
  571. c = "math.asin(" + a + ")";
  572. break;
  573. case "ACOS":
  574. // c = "math.acos(" + a + ") / math.pi * 180";
  575. c = "math.acos(" + a + ")";
  576. break;
  577. case "ATAN":
  578. // c = "math.atan(" + a + ") / math.pi * 180";
  579. c = "math.atan(" + a + ")";
  580. break;
  581. default:
  582. throw "Unknown math operator: " + b;
  583. }
  584. return [c, Blockly.Python.ORDER_MULTIPLICATIVE]
  585. }
  586. ;
  587. Blockly.Python.math_constant = function (a) {
  588. var b = {
  589. PI: ["math.pi", Blockly.Python.ORDER_MEMBER],
  590. E: ["math.e", Blockly.Python.ORDER_MEMBER],
  591. GOLDEN_RATIO: ["(1 + math.sqrt(5)) / 2", Blockly.Python.ORDER_MULTIPLICATIVE],
  592. SQRT2: ["math.sqrt(2)", Blockly.Python.ORDER_MEMBER],
  593. SQRT1_2: ["math.sqrt(1.0 / 2)", Blockly.Python.ORDER_MEMBER],
  594. INFINITY: ["float('inf')", Blockly.Python.ORDER_ATOMIC]
  595. };
  596. a = a.getFieldValue("CONSTANT");
  597. "INFINITY" != a && (Blockly.Python.definitions_.import_math = "import math");
  598. return b[a]
  599. }
  600. ;
  601. Blockly.Python.math_number_property = function (a) {
  602. var b = Blockly.Python.valueToCode(a, "NUMBER_TO_CHECK", Blockly.Python.ORDER_MULTIPLICATIVE) || "0"
  603. , c = a.getFieldValue("PROPERTY");
  604. if ("PRIME" == c)
  605. return Blockly.Python.definitions_.import_math = "import math",
  606. Blockly.Python.definitions_.from_numbers_import_Number = "from numbers import Number",
  607. [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];
  608. switch (c) {
  609. case "EVEN":
  610. var d = b + " % 2 == 0";
  611. break;
  612. case "ODD":
  613. d = b + " % 2 == 1";
  614. break;
  615. case "WHOLE":
  616. d = b + " % 1 == 0";
  617. break;
  618. case "POSITIVE":
  619. d = b + " > 0";
  620. break;
  621. case "NEGATIVE":
  622. d = b + " < 0";
  623. break;
  624. case "DIVISIBLE_BY":
  625. a = Blockly.Python.valueToCode(a, "DIVISOR", Blockly.Python.ORDER_MULTIPLICATIVE);
  626. if (!a || "0" == a)
  627. return ["False", Blockly.Python.ORDER_ATOMIC];
  628. d = b + " % " + a + " == 0"
  629. }
  630. return [d, Blockly.Python.ORDER_RELATIONAL]
  631. }
  632. ;
  633. Blockly.Python.math_change = function (a) {
  634. var b = Blockly.Python.valueToCode(a, "DELTA", Blockly.Python.ORDER_ADDITIVE) || "0";
  635. a = Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE);
  636. return a + " = " + a + " + " + b + "\n"
  637. }
  638. ;
  639. Blockly.Python.math_round = Blockly.Python.math_single;
  640. Blockly.Python.math_trig = Blockly.Python.math_single;
  641. Blockly.Python.math_on_list = function (a) {
  642. var b = a.getFieldValue("OP");
  643. a = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_NONE) || "[]";
  644. switch (b) {
  645. case "SUM":
  646. b = "sum(" + a + ")";
  647. break;
  648. case "MIN":
  649. b = "min(" + a + ")";
  650. break;
  651. case "MAX":
  652. b = "max(" + a + ")";
  653. break;
  654. case "AVERAGE":
  655. b = Blockly.Python.provideFunction_("math_mean", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(myList):", " return float(sum(myList)) / len(myList)"]);
  656. b = b + "(" + a + ")";
  657. break;
  658. case "MEDIAN":
  659. 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]"]);
  660. b = b + "(" + a + ")";
  661. break;
  662. case "MODE":
  663. 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"]);
  664. b = b + "(" + a + ")";
  665. break;
  666. case "STD_DEV":
  667. Blockly.Python.definitions_.import_math = "import math";
  668. 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)"]);
  669. b = b + "(" + a + ")";
  670. break;
  671. case "RANDOM":
  672. Blockly.Python.definitions_.import_random = "import random";
  673. b = "random.choice(" + a + ")";
  674. break;
  675. default:
  676. throw "Unknown operator: " + b;
  677. }
  678. return [b, Blockly.Python.ORDER_FUNCTION_CALL]
  679. }
  680. ;
  681. Blockly.Python.math_modulo = function (a) {
  682. var b = Blockly.Python.valueToCode(a, "DIVIDEND", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  683. a = Blockly.Python.valueToCode(a, "DIVISOR", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  684. return [b + " % " + a, Blockly.Python.ORDER_MULTIPLICATIVE]
  685. }
  686. ;
  687. Blockly.Python.math_division_consult = function (a) {
  688. var b = Blockly.Python.valueToCode(a, "DIVIDEND", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  689. a = Blockly.Python.valueToCode(a, "DIVISOR", Blockly.Python.ORDER_MULTIPLICATIVE) || "0";
  690. return [b + " // " + a, Blockly.Python.ORDER_MULTIPLICATIVE]
  691. }
  692. ;
  693. Blockly.Python.math_constrain = function (a) {
  694. var b = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "0"
  695. , c = Blockly.Python.valueToCode(a, "LOW", Blockly.Python.ORDER_NONE) || "0";
  696. a = Blockly.Python.valueToCode(a, "HIGH", Blockly.Python.ORDER_NONE) || "float('inf')";
  697. return ["min(max(" + b + ", " + c + "), " + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  698. }
  699. ;
  700. Blockly.Python.math_random_int = function (a) {
  701. Blockly.Python.definitions_.import_time = "import time";
  702. Blockly.Python.codeFunctions_.random_seed = "random.seed(time.ticks_cpu())";
  703. Blockly.Python.definitions_.import_random = "import random";
  704. var b = Blockly.Python.valueToCode(a, "FROM", Blockly.Python.ORDER_NONE) || "0";
  705. a = Blockly.Python.valueToCode(a, "TO", Blockly.Python.ORDER_NONE) || "0";
  706. return ["random.randint(" + b + ", " + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  707. }
  708. ;
  709. Blockly.Python.math_random_float = function (a) {
  710. Blockly.Python.definitions_.import_time = "import time";
  711. Blockly.Python.codeFunctions_.random_seed = "random.seed(time.ticks_cpu())";
  712. Blockly.Python.definitions_.import_random = "import random";
  713. return ["random.random()", Blockly.Python.ORDER_FUNCTION_CALL]
  714. }
  715. ;
  716. Blockly.Python.math_convert = function (a) {
  717. var b = a.getFieldValue("TYPE0");
  718. a = Blockly.Python.valueToCode(a, "INPUT0", Blockly.Python.ORDER_ATOMIC);
  719. return [b + "(" + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  720. }
  721. ;
  722. Blockly.Python.procedures = {};
  723. Blockly.Python.procedures_defreturn = function (a) {
  724. for (var b = [], c = 0, d; d = a.workspace.variableList[c]; c++)
  725. -1 == a.arguments_.indexOf(d) && b.push(Blockly.Python.variableDB_.getName(d, Blockly.Variables.NAME_TYPE));
  726. b = b.length ? " global " + b.join(", ") + "\n" : "";
  727. d = Blockly.Python.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE);
  728. var e = Blockly.Python.statementToCode(a, "STACK");
  729. Blockly.Python.STATEMENT_PREFIX && (e = Blockly.Python.prefixLines(Blockly.Python.STATEMENT_PREFIX.replace(/%1/g, "'" + a.id + "'"), Blockly.Python.INDENT) + e);
  730. Blockly.Python.INFINITE_LOOP_TRAP && (e = Blockly.Python.INFINITE_LOOP_TRAP.replace(/%1/g, '"' + a.id + '"') + e);
  731. var f = Blockly.Python.valueToCode(a, "RETURN", Blockly.Python.ORDER_NONE) || "";
  732. f ? f = " return " + f + "\n" : e || (e = Blockly.Python.PASS);
  733. var g = [];
  734. for (c = 0; c < a.arguments_.length; c++)
  735. g[c] = Blockly.Python.variableDB_.getName(a.arguments_[c], Blockly.Variables.NAME_TYPE);
  736. b = "def " + d + "(" + g.join(", ") + "):\n" + b + e + f;
  737. b = Blockly.Python.scrub_(a, b);
  738. Blockly.Python.definitions_["%" + d] = b;
  739. return null
  740. }
  741. ;
  742. Blockly.Python.procedures_defnoreturn = Blockly.Python.procedures_defreturn;
  743. Blockly.Python.procedures_callreturn = function (a) {
  744. for (var b = Blockly.Python.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE), c = [], d = 0; d < a.arguments_.length; d++)
  745. c[d] = Blockly.Python.valueToCode(a, "ARG" + d, Blockly.Python.ORDER_NONE) || "None";
  746. return [b + "(" + c.join(", ") + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  747. }
  748. ;
  749. Blockly.Python.procedures_callnoreturn = function (a) {
  750. for (var b = Blockly.Python.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE), c = [], d = 0; d < a.arguments_.length; d++)
  751. c[d] = Blockly.Python.valueToCode(a, "ARG" + d, Blockly.Python.ORDER_NONE) || "None";
  752. return b + "(" + c.join(", ") + ")\n"
  753. }
  754. ;
  755. Blockly.Python.procedures_ifreturn = function (a) {
  756. var b = "if " + (Blockly.Python.valueToCode(a, "CONDITION", Blockly.Python.ORDER_NONE) || "False") + ":\n";
  757. a.hasReturnValue_ ? (a = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "None",
  758. b += " return " + a + "\n") : b += " return\n";
  759. return b
  760. }
  761. ;
  762. Blockly.Python.texts = {};
  763. Blockly.Python.text = function (a) {
  764. return [Blockly.Python.quote_(a.getFieldValue("TEXT")), Blockly.Python.ORDER_ATOMIC]
  765. }
  766. ;
  767. Blockly.Python.text_list = function (a) {
  768. return ["[" + Blockly.Python.quote_empty(a.getFieldValue("TEXT")) + "]", Blockly.Python.ORDER_ATOMIC]
  769. }
  770. ;
  771. Blockly.Python.text_tuple = function (a) {
  772. return ["(" + Blockly.Python.quote_empty(a.getFieldValue("TEXT")) + ")", Blockly.Python.ORDER_ATOMIC]
  773. }
  774. ;
  775. Blockly.Python.text_dict = function (a) {
  776. return ["{" + Blockly.Python.quote_empty(a.getFieldValue("TEXT")) + "}", Blockly.Python.ORDER_ATOMIC]
  777. }
  778. ;
  779. Blockly.Python.text_join = function (a) {
  780. switch (a.itemCount_) {
  781. case 0:
  782. return ["''", Blockly.Python.ORDER_ATOMIC];
  783. case 1:
  784. return ["str(" + (Blockly.Python.valueToCode(a, "ADD0", Blockly.Python.ORDER_NONE) || "''") + ")", Blockly.Python.ORDER_FUNCTION_CALL];
  785. case 2:
  786. var b = Blockly.Python.valueToCode(a, "ADD0", Blockly.Python.ORDER_NONE) || "''";
  787. a = Blockly.Python.valueToCode(a, "ADD1", Blockly.Python.ORDER_NONE) || "''";
  788. return ["str(" + b + ") + str(" + a + ")", Blockly.Python.ORDER_ADDITIVE];
  789. default:
  790. b = [];
  791. for (var c = 0; c < a.itemCount_; c++)
  792. b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "''";
  793. a = Blockly.Python.variableDB_.getDistinctName("x", Blockly.Variables.NAME_TYPE);
  794. a = "''.join([str(" + a + ") for " + a + " in [" + b.join(", ") + "]])";
  795. return [a, Blockly.Python.ORDER_FUNCTION_CALL]
  796. }
  797. }
  798. ;
  799. Blockly.Python.text_append = function (a) {
  800. var b = Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE);
  801. a = Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_NONE) || "''";
  802. return b + " = str(" + b + ") + str(" + a + ")\n"
  803. }
  804. ;
  805. Blockly.Python.text_length = function (a) {
  806. return ["len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "''") + ")", Blockly.Python.ORDER_FUNCTION_CALL]
  807. }
  808. ;
  809. Blockly.Python.text_isEmpty = function (a) {
  810. return ["not len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "''") + ")", Blockly.Python.ORDER_LOGICAL_NOT]
  811. }
  812. ;
  813. Blockly.Python.text_indexOf = function (a) {
  814. var b = "FIRST" == a.getFieldValue("END") ? "find" : "rfind"
  815. , c = Blockly.Python.valueToCode(a, "FIND", Blockly.Python.ORDER_NONE) || "''";
  816. b = (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_MEMBER) || "''") + "." + b + "(" + c + ")";
  817. return a.workspace.options.oneBasedIndex ? [b + " + 1", Blockly.Python.ORDER_ADDITIVE] : [b, Blockly.Python.ORDER_FUNCTION_CALL]
  818. }
  819. ;
  820. Blockly.Python.text_charAt = function (a) {
  821. var b = a.getFieldValue("WHERE") || "FROM_START"
  822. , c = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_MEMBER) || "''";
  823. switch (b) {
  824. case "FIRST":
  825. return [c + "[0]", Blockly.Python.ORDER_MEMBER];
  826. case "LAST":
  827. return [c + "[-1]", Blockly.Python.ORDER_MEMBER];
  828. case "FROM_START":
  829. return a = Blockly.Python.getAdjustedInt(a, "AT"),
  830. [c + "[" + a + "]", Blockly.Python.ORDER_MEMBER];
  831. case "FROM_END":
  832. return a = Blockly.Python.getAdjustedInt(a, "AT", 1, !0),
  833. [c + "[" + a + "]", Blockly.Python.ORDER_MEMBER];
  834. case "RANDOM":
  835. return Blockly.Python.definitions_.import_random = "import random",
  836. [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]
  837. }
  838. throw "Unhandled option (text_charAt).";
  839. }
  840. ;
  841. Blockly.Python.text_getSubstring = function (a) {
  842. var b = a.getFieldValue("WHERE1")
  843. , c = a.getFieldValue("WHERE2")
  844. , d = Blockly.Python.valueToCode(a, "STRING", Blockly.Python.ORDER_MEMBER) || "''";
  845. switch (b) {
  846. case "FROM_START":
  847. b = Blockly.Python.getAdjustedInt(a, "AT1");
  848. "0" == b && (b = "");
  849. break;
  850. case "FROM_END":
  851. b = Blockly.Python.getAdjustedInt(a, "AT1", 1, !0);
  852. break;
  853. case "FIRST":
  854. b = "";
  855. break;
  856. default:
  857. throw "Unhandled option (text_getSubstring)";
  858. }
  859. switch (c) {
  860. case "FROM_START":
  861. a = Blockly.Python.getAdjustedInt(a, "AT2", 1);
  862. break;
  863. case "FROM_END":
  864. a = Blockly.Python.getAdjustedInt(a, "AT2", 0, !0);
  865. Blockly.isNumber(String(a)) ? "0" == a && (a = "") : (Blockly.Python.definitions_.import_sys = "import sys",
  866. a += " or sys.maxsize");
  867. break;
  868. case "LAST":
  869. a = "";
  870. break;
  871. default:
  872. throw "Unhandled option (text_getSubstring)";
  873. }
  874. return [d + "[" + b + " : " + a + "]", Blockly.Python.ORDER_MEMBER]
  875. }
  876. ;
  877. Blockly.Python.text_changeCase = function (a) {
  878. var b = {
  879. UPPERCASE: ".upper()",
  880. LOWERCASE: ".lower()",
  881. TITLECASE: ".title()"
  882. }[a.getFieldValue("CASE")];
  883. return [(Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_MEMBER) || "''") + b, Blockly.Python.ORDER_FUNCTION_CALL]
  884. }
  885. ;
  886. Blockly.Python.text_trim = function (a) {
  887. var b = {
  888. LEFT: ".lstrip()",
  889. RIGHT: ".rstrip()",
  890. BOTH: ".strip()"
  891. }[a.getFieldValue("MODE")];
  892. return [(Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_MEMBER) || "''") + b, Blockly.Python.ORDER_FUNCTION_CALL]
  893. }
  894. ;
  895. Blockly.Python.text_print = function (a) {
  896. return "print(" + (Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_NONE) || "''") + ")\n"
  897. }
  898. ;
  899. Blockly.Python.text_prompt_ext = function (a) {
  900. var b = Blockly.Python.provideFunction_("text_prompt", ["def " + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + "(msg):", " try:", " return raw_input(msg)", " except NameError:", " return input(msg)"])
  901. , c = a.getField("TEXT") ? Blockly.Python.quote_(a.getFieldValue("TEXT")) : Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_NONE) || "''";
  902. b = b + "(" + c + ")";
  903. "NUMBER" == a.getFieldValue("TYPE") && (b = "float(" + b + ")");
  904. return [b, Blockly.Python.ORDER_FUNCTION_CALL]
  905. }
  906. ;
  907. Blockly.Python.text_prompt = Blockly.Python.text_prompt_ext;
  908. Blockly.Python.variables = {};
  909. Blockly.Python.variables_get = function (a) {
  910. return [Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE), Blockly.Python.ORDER_ATOMIC]
  911. }
  912. ;
  913. Blockly.Python.variables_set = function (a) {
  914. var b = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "0";
  915. return Blockly.Python.variableDB_.getName(a.getFieldValue("VAR"), Blockly.Variables.NAME_TYPE) + " = " + b + "\n"
  916. }
  917. ;