Routine recompile

This commit is contained in:
Neil Fraser
2016-06-14 11:04:26 -07:00
parent c81e3b1d33
commit 873e987f85
9 changed files with 47 additions and 47 deletions

View File

@@ -19,10 +19,10 @@ Blockly.Python.lists_repeat=function(a){var b=Blockly.Python.valueToCode(a,"ITEM
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_MEMBER)||"''";return[("FIRST"==a.getFieldValue("END")?Blockly.Python.provideFunction_("first_index",["def "+Blockly.Python.FUNCTION_NAME_PLACEHOLDER_+"(myList, elem):"," try: theIndex = myList.index(elem) + 1"," except: theIndex = 0"," return theIndex"]):Blockly.Python.provideFunction_("last_index",["def "+Blockly.Python.FUNCTION_NAME_PLACEHOLDER_+
"(myList, elem):"," try: theIndex = len(myList) - myList[::-1].index(elem)"," except: theIndex = 0"," return theIndex"]))+"("+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,"AT",Blockly.Python.ORDER_UNARY_SIGN)||"1";a=Blockly.Python.valueToCode(a,"VALUE",Blockly.Python.ORDER_MEMBER)||"[]";if("FIRST"==c){if("GET"==b)return[a+"[0]",Blockly.Python.ORDER_MEMBER];c=a+".pop(0)";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==b)return c+"\n"}else if("LAST"==c){if("GET"==b)return[a+"[-1]",Blockly.Python.ORDER_MEMBER];
c=a+".pop()";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==b)return c+"\n"}else if("FROM_START"==c){d=Blockly.isNumber(d)?parseInt(d,10)-1:"int("+d+" - 1)";if("GET"==b)return[a+"["+d+"]",Blockly.Python.ORDER_MEMBER];c=a+".pop("+d+")";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==b)return c+"\n"}else if("FROM_END"==c){if("GET"==b)return[a+"[-"+d+"]",Blockly.Python.ORDER_MEMBER];c=a+".pop(-"+d+")";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];
if("REMOVE"==b)return c+"\n"}else if("RANDOM"==c){Blockly.Python.definitions_.import_random="import random";if("GET"==b)return["random.choice("+a+")",Blockly.Python.ORDER_FUNCTION_CALL];c=Blockly.Python.provideFunction_("lists_remove_random_item",["def "+Blockly.Python.FUNCTION_NAME_PLACEHOLDER_+"(myList):"," x = int(random.random() * len(myList))"," return myList.pop(x)"])+"("+a+")";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==b)return c+"\n"}throw"Unhandled combination (lists_getIndex).";
};
Blockly.Python.lists_getIndex=function(a){var b=a.getFieldValue("MODE")||"GET",c=a.getFieldValue("WHERE")||"FROM_START",d=Blockly.Python.valueToCode(a,"AT",Blockly.Python.ORDER_UNARY_SIGN)||"1",e=this.getInputTargetBlock("VALUE");a=Blockly.Python.valueToCode(a,"VALUE",e&&"procedures_callreturn"==e.type?Blockly.Python.ORDER_NONE:Blockly.Python.ORDER_MEMBER)||"[]";if("FIRST"==c){if("GET"==b)return[a+"[0]",Blockly.Python.ORDER_MEMBER];c=a+".pop(0)";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];
if("REMOVE"==b)return c+"\n"}else if("LAST"==c){if("GET"==b)return[a+"[-1]",Blockly.Python.ORDER_MEMBER];c=a+".pop()";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==b)return c+"\n"}else if("FROM_START"==c){d=Blockly.isNumber(d)?parseInt(d,10)-1:"int("+d+" - 1)";if("GET"==b)return[a+"["+d+"]",Blockly.Python.ORDER_MEMBER];c=a+".pop("+d+")";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==b)return c+"\n"}else if("FROM_END"==c){if("GET"==b)return[a+
"[-"+d+"]",Blockly.Python.ORDER_MEMBER];c=a+".pop(-"+d+")";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==b)return c+"\n"}else if("RANDOM"==c){Blockly.Python.definitions_.import_random="import random";if("GET"==b)return["random.choice("+a+")",Blockly.Python.ORDER_FUNCTION_CALL];c=Blockly.Python.provideFunction_("lists_remove_random_item",["def "+Blockly.Python.FUNCTION_NAME_PLACEHOLDER_+"(myList):"," x = int(random.random() * len(myList))"," return myList.pop(x)"])+
"("+a+")";if("GET_REMOVE"==b)return[c,Blockly.Python.ORDER_FUNCTION_CALL];if("REMOVE"==b)return c+"\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,"AT",Blockly.Python.ORDER_NONE)||"1";a=Blockly.Python.valueToCode(a,"TO",Blockly.Python.ORDER_NONE)||"None";if("FIRST"==d){if("SET"==c)return b+"[0] = "+a+"\n";if("INSERT"==c)return b+".insert(0, "+a+")\n"}else if("LAST"==d){if("SET"==c)return b+"[-1] = "+a+"\n";if("INSERT"==c)return b+
".append("+a+")\n"}else if("FROM_START"==d){e=Blockly.isNumber(e)?parseInt(e,10)-1:"int("+e+" - 1)";if("SET"==c)return b+"["+e+"] = "+a+"\n";if("INSERT"==c)return b+".insert("+e+", "+a+")\n"}else if("FROM_END"==d){if("SET"==c)return b+"[-"+e+"] = "+a+"\n";if("INSERT"==c)return b+".insert(-"+e+", "+a+")\n"}else if("RANDOM"==d){Blockly.Python.definitions_.import_random="import random";b.match(/^\w+$/)?d="":(d=Blockly.Python.variableDB_.getDistinctName("tmp_list",Blockly.Variables.NAME_TYPE),e=d+" = "+
b+"\n",b=d,d=e);e=Blockly.Python.variableDB_.getDistinctName("tmp_x",Blockly.Variables.NAME_TYPE);d+=e+" = int(random.random() * len("+b+"))\n";if("SET"==c)return d+(b+"["+e+"] = "+a+"\n");if("INSERT"==c)return d+(b+".insert("+e+", "+a+")\n")}throw"Unhandled combination (lists_setIndex).";};