/* eslint-disable no-unused-expressions */ import { pythonGenerator } from "blockly/python"; const Blockly = { Python: pythonGenerator || { Msg: Object.create(null) } }; Blockly.Python.lists_create_with = function (a) { for (var b = Array(a.itemCount_), c = 0; c < a.itemCount_; c++) b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None"; return ["[" + b.join(", ") + "]", Blockly.Python.ORDER_ATOMIC] }; Blockly.Python.text_list = function (a) { return ["[" + Blockly.Python.quote_empty(a.getFieldValue("TEXT")) + "]", Blockly.Python.ORDER_ATOMIC] }; Blockly.Python.lists_create_with_object = function (a) { for (var b = Array(a.itemCount_), c = 0; c < a.itemCount_; c++) b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None"; return ["[" + b.join(", ") + "]", Blockly.Python.ORDER_ATOMIC] }; Blockly.Python.CocoRobo_return_list = function (a) { var b = Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC); a = Blockly.Python.valueToCode(a, "list_items", Blockly.Python.ORDER_ATOMIC); return b + " \x3d " + a + "\n" }; Blockly.Python.lists_append = function (a) { var b = Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC); a = Blockly.Python.valueToCode(a, "last_item", Blockly.Python.ORDER_ATOMIC); return b + ".append(" + a + ")\n" }; Blockly.Python.lists_extend = function (a) { var b = Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC); a = Blockly.Python.valueToCode(a, "extend_list", Blockly.Python.ORDER_ATOMIC); return b + ".extend(" + a + ")\n" }; Blockly.Python.lists_clear = function (a) { return Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC) + ".clear()\n" }; Blockly.Python.list_order_item = function (a) { var b = Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC), c = a.getFieldValue("list_order"); a = Blockly.Python.valueToCode(a, "list_order_item", Blockly.Python.ORDER_ATOMIC); return [b + c + a + "]", Blockly.Python.ORDER_ATOMIC] }; Blockly.Python.lists_clear = function (a) { return Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC) + ".clear()\n" }; Blockly.Python.parts_of_list = function (a) { var b = Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC), c = a.getFieldValue("list_start_order"), d = a.getFieldValue("list_end_order"), e = Blockly.Python.valueToCode(a, "start_item", Blockly.Python.ORDER_ATOMIC); a = Blockly.Python.valueToCode(a, "end_item", Blockly.Python.ORDER_ATOMIC); switch (d) { case ":": var f = parseInt(a) + 1; break; case ":-": f = a - 1 } return [b + c + e + d + f + "]", Blockly.Python.ORDER_ATOMIC] }; Blockly.Python.list_to_tuple = function (a) { return ["tuple(" + Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC] }; Blockly.Python.list_order_item = function (a) { var b = Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC), c = a.getFieldValue("list_order"); a = Blockly.Python.valueToCode(a, "list_order_item", Blockly.Python.ORDER_ATOMIC); return [b + c + a + "]", Blockly.Python.ORDER_ATOMIC] }; Blockly.Python.set_list_order_item = function (a) { var b = Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC), c = a.getFieldValue("list_order"), d = Blockly.Python.valueToCode(a, "list_order_item", Blockly.Python.ORDER_ATOMIC); a = Blockly.Python.valueToCode(a, "set_value", Blockly.Python.ORDER_ATOMIC); return b + c + d + "] \x3d " + a + "\n" }; Blockly.Python.insert_list_order_item = function (a) { var b = Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC), c = a.getFieldValue("list_order"), d = Blockly.Python.valueToCode(a, "list_order_item", Blockly.Python.ORDER_ATOMIC); a = Blockly.Python.valueToCode(a, "set_value", Blockly.Python.ORDER_ATOMIC); return b + ".insert" + c + d + ", " + a + ")\n" }; Blockly.Python.list_item_exist = function (a) { var b = Blockly.Python.valueToCode(a, "list_name", Blockly.Python.ORDER_ATOMIC); return [Blockly.Python.valueToCode(a, "list_item", Blockly.Python.ORDER_ATOMIC) + " in " + b, Blockly.Python.ORDER_ATOMIC] }; Blockly.Python.lists_repeat = function (a) { var b = Blockly.Python.valueToCode(a, "ITEM", Blockly.Python.ORDER_NONE) || "None"; a = Blockly.Python.valueToCode(a, "NUM", Blockly.Python.ORDER_MULTIPLICATIVE) || "0"; return ["[" + b + "] * " + a, Blockly.Python.ORDER_MULTIPLICATIVE] }; Blockly.Python.lists_length = function (a) { return ["len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "[]") + ")", Blockly.Python.ORDER_FUNCTION_CALL] }; Blockly.Python.lists_isEmpty = function (a) { return ["not len(" + (Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "[]") + ")", Blockly.Python.ORDER_LOGICAL_NOT] }; Blockly.Python.lists_indexOf = function (a) { var b = Blockly.Python.valueToCode(a, "FIND", Blockly.Python.ORDER_NONE) || "[]" , c = Blockly.Python.valueToCode(a, "VALUE", Blockly.Python.ORDER_NONE) || "''"; if (a.workspace.options.oneBasedIndex) var d = " 0" , e = " + 1" , f = ""; else d = " -1", e = "", f = " - 1"; if ("FIRST" == a.getFieldValue("END")) 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"]), [a + "(" + c + ", " + b + ")", Blockly.Python.ORDER_FUNCTION_CALL]; 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"]); return [a + "(" + c + ", " + b + ")", Blockly.Python.ORDER_FUNCTION_CALL] }; Blockly.Python.lists_getIndex = function (a) { var b = a.getFieldValue("MODE") || "GET" , c = a.getFieldValue("WHERE") || "FROM_START" , d = Blockly.Python.valueToCode(a, "VALUE", "RANDOM" == c ? Blockly.Python.ORDER_NONE : Blockly.Python.ORDER_MEMBER) || "[]"; switch (c) { case "FIRST": if ("GET" == b) return [d + "[0]", Blockly.Python.ORDER_MEMBER]; if ("GET_REMOVE" == b) return [d + ".pop(0)", Blockly.Python.ORDER_FUNCTION_CALL]; if ("REMOVE" == b) return d + ".pop(0)\n"; break; case "LAST": if ("GET" == b) return [d + "[-1]", Blockly.Python.ORDER_MEMBER]; if ("GET_REMOVE" == b) return [d + ".pop()", Blockly.Python.ORDER_FUNCTION_CALL]; if ("REMOVE" == b) return d + ".pop()\n"; break; case "FROM_START": a = Blockly.Python.valueToCode(a, "AT", Blockly.Python.ORDER_ATOMIC); if ("GET" == b) return [d + "[" + a + "]", Blockly.Python.ORDER_MEMBER]; if ("GET_REMOVE" == b) return [d + ".pop(" + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]; if ("REMOVE" == b) return d + ".pop(" + a + ")\n"; break; case "FROM_END": a = Blockly.Python.getAdjustedInt(a, "AT", 1, !0); if ("GET" == b) return [d + "[" + a + "]", Blockly.Python.ORDER_MEMBER]; if ("GET_REMOVE" == b) return [d + ".pop(" + a + ")", Blockly.Python.ORDER_FUNCTION_CALL]; if ("REMOVE" == b) return d + ".pop(" + a + ")\n"; break; case "RANDOM": Blockly.Python.definitions_.import_random = "import random"; if ("GET" == b) return ["random.choice(" + d + ")", Blockly.Python.ORDER_FUNCTION_CALL]; 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 + ")"; if ("GET_REMOVE" == b) return [d, Blockly.Python.ORDER_FUNCTION_CALL]; if ("REMOVE" == b) return d + "\n" } throw "Unhandled combination (lists_getIndex)."; }; Blockly.Python.lists_setIndex = function (a) { var b = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_MEMBER) || "[]" , c = a.getFieldValue("MODE") || "GET" , d = a.getFieldValue("WHERE") || "FROM_START" , e = Blockly.Python.valueToCode(a, "TO", Blockly.Python.ORDER_NONE) || "None"; switch (d) { case "FIRST": if ("SET" == c) return b + "[0] = " + e + "\n"; if ("INSERT" == c) return b + ".insert(0, " + e + ")\n"; break; case "LAST": if ("SET" == c) return b + "[-1] = " + e + "\n"; if ("INSERT" == c) return b + ".append(" + e + ")\n"; break; case "FROM_START": a = Blockly.Python.getAdjustedInt(a, "AT"); if ("SET" == c) return b + "[" + a + "] = " + e + "\n"; if ("INSERT" == c) return b + ".insert(" + a + ", " + e + ")\n"; break; case "FROM_END": a = Blockly.Python.getAdjustedInt(a, "AT", 1, !0); if ("SET" == c) return b + "[" + a + "] = " + e + "\n"; if ("INSERT" == c) return b + ".insert(" + a + ", " + e + ")\n"; break; case "RANDOM": Blockly.Python.definitions_.import_random = "import random"; b.match(/^\w+$/) ? a = "" : (a = Blockly.Python.variableDB_.getDistinctName("tmp_list", Blockly.Variables.NAME_TYPE), d = a + " = " + b + "\n", b = a, a = d); d = Blockly.Python.variableDB_.getDistinctName("tmp_x", Blockly.Variables.NAME_TYPE); a += d + " = int(random.random() * len(" + b + "))\n"; if ("SET" == c) return a + (b + "[" + d + "] = " + e + "\n"); if ("INSERT" == c) return a + (b + ".insert(" + d + ", " + e + ")\n") } throw "Unhandled combination (lists_setIndex)."; }; Blockly.Python.lists_getSublist = function (a) { var b = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_MEMBER) || "[]" , c = a.getFieldValue("WHERE1") , d = a.getFieldValue("WHERE2"); switch (c) { case "FROM_START": c = Blockly.Python.getAdjustedInt(a, "AT1"); "0" == c && (c = ""); break; case "FROM_END": c = Blockly.Python.getAdjustedInt(a, "AT1", 1, !0); break; case "FIRST": c = ""; break; default: throw "Unhandled option (lists_getSublist)"; } switch (d) { case "FROM_START": a = Blockly.Python.getAdjustedInt(a, "AT2", 1); break; case "FROM_END": a = Blockly.Python.getAdjustedInt(a, "AT2", 0, !0); Blockly.isNumber(String(a)) ? "0" == a && (a = "") : (Blockly.Python.definitions_.import_sys = "import sys", a += " or sys.maxsize"); break; case "LAST": a = ""; break; default: throw "Unhandled option (lists_getSublist)"; } return [b + "[" + c + " : " + a + "]", Blockly.Python.ORDER_MEMBER] }; Blockly.Python.lists_sort = function (a) { var b = Blockly.Python.valueToCode(a, "LIST", Blockly.Python.ORDER_NONE) || "[]" , c = a.getFieldValue("TYPE"); a = "1" === a.getFieldValue("DIRECTION") ? "False" : "True"; 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] }; Blockly.Python.lists_split = function (a) { var b = a.getFieldValue("MODE"); if ("SPLIT" == b) b = Blockly.Python.valueToCode(a, "INPUT", Blockly.Python.ORDER_MEMBER) || "''", a = Blockly.Python.valueToCode(a, "DELIM", Blockly.Python.ORDER_NONE), a = b + ".split(" + a + ")"; else if ("JOIN" == b) b = Blockly.Python.valueToCode(a, "INPUT", Blockly.Python.ORDER_NONE) || "[]", a = Blockly.Python.valueToCode(a, "DELIM", Blockly.Python.ORDER_MEMBER) || "''", a = a + ".join(" + b + ")"; else throw "Unknown mode: " + b; return [a, Blockly.Python.ORDER_FUNCTION_CALL] }; Blockly.Python.list_first_index = function(a) { var b = Blockly.Python.valueToCode(a, "elem", Blockly.Python.ORDER_ATOMIC), c = Blockly.Python.valueToCode(a, "my_list", Blockly.Python.ORDER_ATOMIC); a = a.getFieldValue("last_or_first"); Blockly.Python.codeFunctions_.list_first_index = "def first_index(my_list, elem):\n try: index \x3d my_list.index(elem)\n except: index \x3d 0\n return index"; Blockly.Python.codeFunctions_.list_last_index = "def last_index(my_list, elem):\n try: index \x3d len(my_list) - my_list[::-1].index(elem) - 1\n except: index \x3d 0\n return index"; return [a + "_index(" + c + ", " + b + ")", Blockly.Python.ORDER_ATOMIC] }; export default Blockly;