UNPKG

pxt-core

Version:

Microsoft MakeCode provides Blocks / JavaScript / Python tools and editors

1 lines • 254 kB
let iface;var pxt;!function(e){!function(t){function i(t,i){return e.worker.getWorker(e.webConfig.workerjs).opAsync(t,i)}t.workerOpAsync=i;let o={};function s(e,t){e.push.apply(e,t)}function l(e){if(!e)throw new Error("Assertion failure")}class n{constructor(e,t,i,o,s){this.link=e,this.type=t,this.parentType=i,this.childType=o,this.isArrayType=s}}let r;function a(e){return e.link?a(e.link):e}function c(e,t){let i=a(e),o=a(t);if(l(null==i.link&&null==o.link),i==o)return;if(i.childType&&o.childType){const e=i.childType;i.childType=null,c(e,o.childType)}else i.childType&&!o.childType&&(o.childType=i.childType);if(i.parentType&&o.parentType){const e=i.parentType;i.parentType=null,c(e,o.parentType)}else!i.parentType||o.parentType||o.type||(o.parentType=i.parentType);let s=function(e,t){if(null==e||"Array"===e&&y(t))return t;if(null==t||"Array"===t&&y(e))return e;if(e==t)return e;throw new Error("cannot mix "+e+" with "+t)}(i.type,o.type);e.link=o,i.link=o,i.isArrayType=o.isArrayType,e.type=null,t.type=s}function u(e,t=!1){return new n(null,e,null,null,t)}t.Point=n,function(e){e[e.None=0]="None",e[e.Argument=1]="Argument",e[e.Assigned=2]="Assigned",e[e.Implicit=3]="Implicit"}(r=t.BlockDeclarationType||(t.BlockDeclarationType={}));const d=u("number"),p=u("boolean"),h=u("string"),m=u("void");function f(e){if(!e)return u(e);switch(e.toLowerCase()){case"number":return d;case"boolean":return p;case"string":return h;case"void":return m;default:return u(e)}}function g(e,t){if(l(null!=t),function(e){return"placeholder"==e.type||e.type===pxtc.TS_OUTPUT_TYPE}(t))return t.p||(t.p=u(null)),a(t.p);if("variables_get"==t.type)return a(V(e,t,t.getField("VAR").getText()).type);if("function_call_output"==t.type)return function(e,t){const i=t.getField("function_name").getText();return k(e,i)}(e,t);if(!t.outputConnection)return f(m.type);const i=t.outputConnection.check_&&t.outputConnection.check_.length?t.outputConnection.check_[0]:"T";if("Array"===i){if(t.outputConnection.check_.length>1)return f(t.outputConnection.check_[1]);let i;if("lists_create_with"==t.type){if(t.inputList&&t.inputList.length)for(const o of t.inputList)if(o.connection&&o.connection.targetBlock()){let t=a(g(e,o.connection.targetBlock()));if(t){if(t.parentType)return t.parentType;i=t.type?f(t.type+"[]"):u(null),C(i,t);break}}}else"argument_reporter_array"==t.type&&(i||(i=V(e,t,t.getFieldValue("VALUE")).type));return i&&(i.isArrayType=!0),i||u(null,!0)}if("T"===i){const i=e.stdCallTable[t.type],o="lists_index_get"===t.type;if(o||i&&i.comp.thisParameter){let s;if(s=o?t.inputList.find((e=>"LIST"===e.name)):t.inputList.find((e=>e.name===i.comp.thisParameter.definitionName)),s.connection&&s.connection.targetBlock()){const t=g(e,s.connection.targetBlock());if(t.childType)return t.childType;const i=y(t.type)&&"Array"!==t.type?u(t.type.substr(0,t.type.length-2)):u(null);return C(t,i),i}}return u(null)}return f(i)}function k(t,i){if(!t.userFunctionReturnValues[i]){const o=Blockly.Functions.getDefinition(i,t.workspace);let s=u("void");if(be(o,!0))s=u("any");else{const l=[];for(const e of o.getDescendants(!1))"function_return"===e.type&&(b(t,e,"RETURN_VALUE"),l.push(g(t,T(e,"RETURN_VALUE"))));if(l.length)try{const e=u(null);for(const t of l)c(e,t);s=e}catch(l){t.diagnostics.push({blockId:o.id,message:e.Util.lf("Function '{0}' has an invalid return type",i)}),s=u("any")}}t.userFunctionReturnValues[i]=s}return t.userFunctionReturnValues[i]}function y(e){return e&&(-1!==e.indexOf("[]")||"Array"==e)}function b(e,t,i,s){const l=t.getInputTargetBlock(i);l?l.type!==pxtc.TS_OUTPUT_TYPE||l.p||(l.p=u(null)):(o[t.id]||(o[t.id]={}),o[t.id][i]||(o[t.id][i]=function(e,t,i){return{type:"placeholder",p:u(i||null),workspace:e.workspace,parentBlock_:t}}(e,t,s)))}function _(e){return"pxt_controls_for"==e.type||"pxt_controls_for_of"==e.type?T(e,"VAR"):e}function T(e,t){const i=e.getInputTargetBlock(t);return i||o[e.id]&&o[e.id][t]}function x(){o={}}function E(e,t,i,o){b(e,t,i);try{c(g(e,T(t,i)),o)}catch(e){}}function B(e,t,i){function o(e){return e.name?e.connection&&e.connection.check_&&e.connection.check_.length?e.connection.check_[0]:"T":void 0}function s(e,i){let s=e.inputList.filter((e=>"T"===o(e)));if(s.length){const o=T(e,s[0].name);if(o){const s=g(t,o),l=s.type?f(g(t,o).type+"[]"):f(null);return C(l,s),E(t,e,i,l),!0}}return!1}e&&e.filter((e=>e.isEnabled())).forEach((e=>{try{switch(e.type){case"math_op2":E(t,e,"x",f(d.type)),E(t,e,"y",f(d.type));break;case"math_op3":E(t,e,"x",f(d.type));break;case"math_arithmetic":case"logic_compare":switch(e.getFieldValue("OP")){case"ADD":case"MINUS":case"MULTIPLY":case"DIVIDE":case"LT":case"LTE":case"GT":case"GTE":case"POWER":E(t,e,"A",f(d.type)),E(t,e,"B",f(d.type));break;case"AND":case"OR":b(t,e,"A",p.type),b(t,e,"B",p.type);break;case"EQ":case"NEQ":b(t,e,"A"),b(t,e,"B");let i=g(t,T(e,"A")),o=g(t,T(e,"B"));try{c(i,o)}catch(e){}}break;case"logic_operation":b(t,e,"A",p.type),b(t,e,"B",p.type);break;case"logic_negate":b(t,e,"BOOL",p.type);break;case"controls_if":for(let i=0;i<=e.elseifCount_;++i)b(t,e,"IF"+i,p.type);break;case"pxt_controls_for":case"controls_simple_for":E(t,e,"TO",f(d.type));break;case"pxt_controls_for_of":case"controls_for_of":const i=g(t,T(e,"LIST"));C(i,V(t,e,_(e).getField("VAR").getText()).type);break;case"variables_set":case"variables_change":let l=V(t,e,e.getField("VAR").getText()).type;b(t,e,"VALUE");let n=T(e,"VALUE");if(n){let e=function(e,t){var i,o;return(null===(o=null===(i=t.outputConnection)||void 0===i?void 0:i.check_)||void 0===o?void 0:o.length)&&"Array"!==t.outputConnection.check_[0]&&"T"!==t.outputConnection.check_[0]?t.outputConnection.check_.map((e=>f(e))):[g(e,t)]}(t,n);const i=a(l);if(i.type&&e.slice(1).some((e=>e.type===i.type)))l.link=a(e[0]);else try{c(l,e[0])}catch(e){}}break;case"controls_repeat_ext":E(t,e,"TIMES",f(d.type));break;case"device_while":b(t,e,"COND",p.type);break;case"lists_index_get":E(t,e,"LIST",f("Array")),E(t,e,"INDEX",f(d.type));const r=g(t,T(e,"LIST"));C(r,g(t,e));break;case"lists_index_set":E(t,e,"LIST",f("Array")),b(t,e,"VALUE"),s(e,"LIST"),E(t,e,"INDEX",f(d.type));break;case"function_definition":k(t,e.getField("function_name").getText());break;case"function_call":case"function_call_output":e.getArguments().forEach((i=>{E(t,e,i.id,f(i.type))}));break;case pxtc.TS_RETURN_STATEMENT_TYPE:b(t,e,"RETURN_VALUE");break;case pxtc.PAUSE_UNTIL_TYPE:E(t,e,"PREDICATE",p);break;default:if(e.type in t.stdCallTable){const i=t.stdCallTable[e.type];if("ENUM_GET"===i.attrs.shim||"KIND_GET"===i.attrs.shim)return;re(i,ne(e,i)).forEach(((l,n)=>{const r=i.isExtensionMethod&&0===n;if(l.definitionName&&!e.getFieldValue(l.definitionName)){let i=e.inputList.find((e=>e.name==l.definitionName));if(i&&i.connection&&i.connection.check_){if(r&&"Array"===o(i)){if(s(e,l.definitionName))return}for(let o=0;o<i.connection.check_.length;o++)try{let s=i.connection.check_[o];E(t,e,l.definitionName,f(s));break}catch(e){}}}}))}}}catch(i){const o=i.block||e;o.setWarningText(i+""),t.errors.push(o)}})),t.allVariables.forEach((e=>{null==v(e.type).type&&(e.isFunctionParameter?e.type.isArrayType&&(e.type.type="any[]"):c(e.type,f(e.type.isArrayType?"number[]":d.type)))}))}function C(e,t){const i=a(e),o=a(t);i.childType?c(i.childType,o):i.type||(i.childType=o),o.parentType?c(o.parentType,i):o.type||(o.parentType=i),y(i.type)&&(i.isArrayType=!0)}function v(e,t=[]){const i=a(e);if(-1===t.indexOf(i)&&(t.push(i),!i.type||"Array"===i.type)){if(i.parentType){const e=v(i.parentType,t);if(e.type&&"Array"!==e.type)return y(e.type)?i.type=e.type.substr(0,e.type.length-2):i.type=e.type,i}if(i.childType){const e=v(i.childType,t);if(e.type)return i.type=e.type+"[]",i}}return i}function A(e){let t=e.getFieldValue("math_number_minmax"===e.type?"SLIDER":"NUM");const i=parseFloat(t);return function(e,t){isFinite(e)&&!isNaN(e)||function(e,t){let i=new Error(e);throw i.block=t,i}(lf("Number entered is either too large or too small"),t)}(i,e),i}function I(e,i,o){return t.H.mkNumberLiteral(A(i))}function N(e,t){if(!t)return!1;if("math_number"===t.type||"math_integer"===t.type||"math_number_minmax"===t.type||"math_whole_number"===t.type)return!0;const i=e.stdCallTable[t.type];if(!i)return!1;const{comp:o}=i;if("TD_ID"===i.attrs.shim&&1===o.parameters.length){const i=t.getFieldValue(o.parameters[0].definitionName);return i?!isNaN(parseInt(i)):N(e,T(t,o.parameters[0].definitionName))}return!1}function w(e,t){return N(e,t)||"logic_boolean"===t.type||"text"===t.type}let S={ADD:"+",MINUS:"-",MULTIPLY:"*",DIVIDE:"/",LT:"<",LTE:"<=",GT:">",GTE:">=",AND:"&&",OR:"||",EQ:"==",NEQ:"!=",POWER:"**"};function D(e,i,o){const s=P(i.getFieldValue("NAME"),e,!0);return t.mkStmt(t.mkText(s+"()"))}function L(e,i,o,s){const l=P(i.getField("function_name").getText(),e,!0),n=!i.getInputsInline(),r=i.getArguments().map((e=>({actualName:e.name,definitionName:e.id}))).map((t=>W(e,i,t,o))),a=t.H.stdCall(l,r,n);return s?t.mkStmt(a):a}function R(e,i,o){const s=T(i,"RETURN_VALUE");return s&&"placeholder"!=s.type?t.mkStmt(t.mkText("return "),M(e,s,o)):t.mkStmt(t.mkText("return"))}function O(e){const i=e.getContent();return t.Helpers.mkMultiComment(i.trim())}function F(e){if(null==e.type&&(c(e,f(d.type)),e=a(e)),y(e.type)||e.isArrayType)return t.mkText("[]");switch(e.type){case"boolean":return t.H.mkBooleanLiteral(!1);case"number":return t.H.mkNumberLiteral(0);case"string":return t.H.mkStringLiteral("");default:return t.mkText("null")}}function M(i,o,s){let n;if(l(null!=o),i.stats[o.type]=(i.stats[o.type]||0)+1,se(o,s),"placeholder"!=o.type&&o.isEnabled&&o.isEnabled())switch(o.type){case"math_number":case"math_integer":case"math_whole_number":case"math_number_minmax":n=I(0,o);break;case"math_op2":n=function(e,i,o){let s=i.getFieldValue("op"),l=M(e,T(i,"x"),o),n=M(e,T(i,"y"),o);return t.H.mathCall(s,[l,n])}(i,o,s);break;case"math_op3":n=function(e,i,o){let s=M(e,T(i,"x"),o);return t.H.mathCall("abs",[s])}(i,o,s);break;case"math_arithmetic":case"logic_compare":case"logic_operation":n=function(e,i,o){let s=i.getFieldValue("OP"),n=T(i,"A"),r=T(i,"B"),a=[M(e,n,o),M(e,r,o)];-1!==["LT","LTE","GT","GTE","EQ","NEQ"].indexOf(s)&&w(e,n)&&w(e,r)&&t.flattenNode([a[0]]).output!==t.flattenNode([a[1]]).output&&(a=a.map((e=>t.H.mkParenthesizedExpression(t.mkGroup([e,t.mkText(" as any")])))));let c=g(e,n).type;if(c==h.type){if("EQ"==s)return t.H.mkSimpleCall("==",a);if("NEQ"==s)return t.H.mkSimpleCall("!=",a)}else if(c==p.type)return t.H.mkSimpleCall(S[s],a);return l(s in S),t.H.mkSimpleCall(S[s],a)}(i,o,s);break;case"math_modulo":n=function(e,i,o){let s=T(i,"DIVIDEND"),l=T(i,"DIVISOR"),n=[M(e,s,o),M(e,l,o)];return t.H.mkSimpleCall("%",n)}(i,o,s);break;case"logic_boolean":n=function(e,i,o){return t.H.mkBooleanLiteral("TRUE"==i.getFieldValue("BOOL"))}(0,o);break;case"logic_negate":n=function(e,i,o){let s=M(e,T(i,"BOOL"),o);return t.mkPrefix("!",[t.H.mkParenthesizedExpression(s)])}(i,o,s);break;case"variables_get":n=function(e,i){const o=i.getField("VAR").getText();let s=V(e,i,o);if(!s)return t.mkText(o);s.firstReference||(s.firstReference=i);return l(null!=s&&null!=s.type),t.mkText(s.escapedName)}(i,o);break;case"text":n=function(e,i,o){return t.H.mkStringLiteral(i.getFieldValue("TEXT"))}(0,o);break;case"text_join":n=function(e,i,o){let s,l=0;for(;;){const n=T(i,"ADD"+l);if(l++,!n){if(l<i.inputList.length)continue;break}const r=M(e,n,o);s=s?t.H.mkSimpleCall("+",[s,r]):0===n.type.indexOf("text")?r:t.H.mkSimpleCall("+",[t.H.mkStringLiteral(""),r])}return s||t.H.mkStringLiteral("")}(i,o,s);break;case"lists_create_with":n=function(e,i,o){let s=i.inputList.map((t=>t.connection&&t.connection.targetBlock()?M(e,t.connection.targetBlock(),o):void 0)).filter((e=>!!e));return t.H.mkArrayLiteral(s,!i.getInputsInline())}(i,o,s);break;case"lists_index_get":n=function(e,i,o){const s=M(e,T(i,"LIST"),o),l=M(e,T(i,"INDEX"),o);return t.mkGroup([s,t.mkText("["),l,t.mkText("]")])}(i,o,s);break;case"lists_index_set":n=function(e,i,o){const s=T(i,"LIST"),l=M(e,s,o),n=M(e,T(i,"INDEX"),o),r=M(e,T(i,"VALUE"),o),a=t.mkGroup([l,t.mkText("["),n,t.mkText("] = "),r]);return"lists_create_with"===s.type?Y(a):a}(i,o,s);break;case"math_js_op":case"math_js_round":n=function(e,i,o){const s=i.getFieldValue("OP"),l=[M(e,T(i,"ARG0"),o)];return i.getInput("ARG1")&&l.push(M(e,T(i,"ARG1"),o)),t.H.mathCall(s,l)}(i,o,s);break;case pxtc.TS_OUTPUT_TYPE:n=function(e,i,o){return t.mkText(i.getFieldValue("EXPRESSION").trim())}(0,o);break;case"argument_reporter_boolean":case"argument_reporter_number":case"argument_reporter_string":case"argument_reporter_array":case"argument_reporter_custom":n=function(e,i,o){const s=P(i.getFieldValue("VALUE"),e);return t.mkText(s)}(i,o);break;case"function_call_output":n=L(i,o,s,!1);break;default:let r=i.stdCallTable[o.type];r?n=r.imageLiteral?X(i,o,r.imageLiteral,r.imageLiteralColumns,r.imageLiteralRows,r.namespace,r.f,re(r,ne(o,r)).map((e=>W(i,o,e,s)))):$(i,o,r,s):(e.reportError("blocks","unable to compile expression",{details:o.type}),n=F(g(i,o)))}else{if("Array"===a(g(i,o)).type){let e="lists_index_get"===o.parentBlock_.type;if(!e){const t=i.stdCallTable[o.parentBlock_.type];e=t&&t.isExpression}const s=t.mkText("[0]");n=e?s:Y(s)}else n=F(g(i,o))}return n.id=o.id,n}function V(e,t,i){return he(i,e.idToScope[t.id])}function P(e,t,i=!1){if(!e)return"_";if(i){if(t.renames.oldToNewFunctions[e])return t.renames.oldToNewFunctions[e]}else if(t.renames.oldToNew[e])return t.renames.oldToNew[e];let o=ts.pxtc.escapeIdentifier(e);if(t.renames.takenNames[o]){let e=2;for(;t.renames.takenNames[o+e];)e++;o+=e}return i?(t.renames.oldToNewFunctions[e]=o,t.renames.takenNames[o]=!0):t.renames.oldToNew[e]=o,o}function U(e,i,o){let s=T(i,"VALUE"),l=V(e,i,i.getField("VAR").getText());let n=e.idToScope[i.id].declaredVars[l.name]===l&&!l.firstReference&&!l.alreadyDeclared;n&&me(i,(t=>{if("variables_get"===t.type){V(e,t,t.getField("VAR").getText())===l&&(n=!1)}}),!0);let a=M(e,s,o),c=l.escapedName+" = ";if(l.isAssigned=!0,n){l.alreadyDeclared=r.Assigned;const t=v(l.type);if(c=`let ${l.escapedName} = `,t){const i=v(g(e,s));t.type!==i.type&&(c=`let ${l.escapedName}: ${t.type} = `)}}else l.firstReference||(l.firstReference=i);return t.mkStmt(t.mkText(c),a)}function H(e,i,o){let s=T(i,"VALUE"),l=V(e,i,i.getField("VAR").getText()),n=M(e,s,o),r=t.mkText(l.escapedName);return t.mkStmt(t.mkInfix(r,"+=",n))}function G(i,o,s){const l=i.stdCallTable[o.type];return l.imageLiteral?t.mkStmt(X(i,o,l.imageLiteral,l.imageLiteralColumns,l.imageLiteralRows,l.namespace,l.f,re(l,ne(o,l)).map((e=>W(i,o,e,s))))):l.hasHandler?function(i,o,s,l,n,r){const a=l.map((e=>W(i,o,e,r))),c=T(o,"HANDLER"),u=K(i,c);e.appTarget.compile&&e.appTarget.compile.emptyEventHandlerComments&&0===u.children.length&&u.children.unshift(t.mkStmt(t.mkText(`// ${pxtc.HANDLER_COMMENT}`)));let d;if(j(o)&&o.mutation.getMutationType()===t.MutatorTypes.ObjectDestructuringMutator)d=o.mutation.compileMutation(i,r);else if(s.comp.handlerArgs.length){let e=function(e,t,i){return ae(e,t).map((t=>V(i,e,t.name).escapedName))}(o,s,i);d=t.mkText(`function (${e.join(", ")})`)}return q(i,n,s.f,a,u,d,s.isExtensionMethod)}(i,o,l,function(e,t){return re(e,ne(t,e)).filter((e=>!!e.definitionName))}(l,o),l.namespace,s):t.mkStmt($(i,o,l,s))}function W(i,o,s,l,n=!1){let r=o.getFieldValue(s.definitionName);if(null!=r){const l=o.getField(s.definitionName);if(l instanceof pxtblockly.FieldTextInput)return t.H.mkStringLiteral(r);if(l instanceof pxtblockly.FieldTilemap&&!l.isGreyBlock){const o=e.react.getTilemapProject(),s=l.getValue();if(s.startsWith("tilemap`"))return t.mkText(s);if(i.options.emitTilemapLiterals)try{const i=e.sprite.decodeTilemap(s,"typescript",o);if(i){const[e]=o.createNewTilemapFromData(i);return t.mkText(`tilemap\`${e}\``)}}catch(i){}}const n=i.blocksInfo.apis.byQName[s.type];if(n&&n.attributes.emitAsConstant)for(const e of Object.keys(i.blocksInfo.apis.byQName)){const o=i.blocksInfo.apis.byQName[e];if(o&&o.attributes&&o.attributes.enumIdentity===r)return t.mkText(e)}let a=t.mkText(r);return a.canIndentInside="string"==typeof r&&r.indexOf("\n")>=0,a}{b(i,o,s.definitionName);const e=T(o,s.definitionName);return n&&"lists_create_with"===e.type?Y(M(i,e,l)):s.shadowOptions&&s.shadowOptions.toString&&g(i,e)!==h?t.H.mkSimpleCall("+",[t.H.mkStringLiteral(""),t.H.mkParenthesizedExpression(M(i,e,l))]):M(i,e,l)}}function $(e,i,o,s){let l;if(j(i)&&i.mutation.getMutationType()===t.MutatorTypes.RestParameterMutator)l=i.mutation.compileMutation(e,s).children;else{if("ENUM_GET"===o.attrs.shim){const e=o.attrs.enumName,s=i.getFieldValue("MEMBER").replace(/^\d+/,"");return t.H.mkPropertyAccess(s,t.mkText(e))}if("KIND_GET"===o.attrs.shim){const s=e.kinds.filter((e=>e.blockId===o.attrs.blockId))[0];return t.H.mkPropertyAccess(i.getFieldValue("MEMBER"),t.mkText(s.name))}l=re(o,ne(i,o)).map(((t,l)=>W(e,i,t,s,o.isExtensionMethod&&0===l&&!o.isExpression)))}let n=o.namespace,r=o.f;if(o.attrs.blockAliasFor){const t=e.blocksInfo.apis.byQName[o.attrs.blockAliasFor];t&&(r=t.name,n=t.namespace)}const a=!i.getInputsInline();if(o.isIdentity)return l[0];if(o.property)return t.H.mkPropertyAccess(r,l[0]);if("@get@"==r)return t.H.mkPropertyAccess(l[1].op.replace(/.*\./,""),l[0]);if("@set@"==r)return t.H.mkAssign(t.H.mkPropertyAccess(l[1].op.replace(/.*\./,"").replace(/@set/,""),l[0]),l[2]);if("@change@"==r)return t.H.mkSimpleCall("+=",[t.H.mkPropertyAccess(l[1].op.replace(/.*\./,"").replace(/@set/,""),l[0]),l[2]]);if(o.isExtensionMethod){if(o.attrs.defaultInstance){let n;j(i)&&i.mutation.getMutationType()===t.MutatorTypes.DefaultInstanceMutator&&(n=i.mutation.compileMutation(e,s)),n?l.unshift(n):l.unshift(t.mkText(o.attrs.defaultInstance))}return t.H.extensionCall(r,l,a)}return n?t.H.namespaceCall(n,r,l,a):t.H.stdCall(r,l,a)}function q(e,i,o,s,l,n,r=!1){let a;return l.noFinalNewline=!0,a=n?t.mkGroup([n,l]):t.mkGroup([t.mkText("function ()"),l]),r?t.mkStmt(t.H.extensionCall(o,s.concat([a]),!1)):i?t.mkStmt(t.H.namespaceCall(i,o,s.concat([a]),!1)):t.mkStmt(t.H.mkCall(o,s.concat([a]),!1))}function j(e){return!!e.mutation}function X(e,i,o,s,l,n,r,a){a=void 0===a?[]:a;let c="\n";l=l||5,s=(s||5)*o;let u=i.getFieldValue("LEDS");u=u.replace(/[ `\n]+/g,"");for(let e=0;e<l;++e){for(let t=0;t<s;++t)t>0&&(c+=" "),c+="#"===u[e*s+t]?"#":".";c+="\n"}let d=t.H.mkStringLiteral(c);return d.canIndentInside=!0,t.H.namespaceCall(n,r,[d].concat(a),!1)}function z(i,o){let l;const n=[];switch(i.stats[o.type]=(i.stats[o.type]||0)+1,se(o,n),o.type){case"controls_if":l=function(e,i,o){let l=[];for(let n=0;n<=i.elseifCount_;++n){let r=M(e,T(i,"IF"+n),o),a=K(e,T(i,"DO"+n)),c=t.mkText("if (");n>0&&(c=t.mkText("else if ("),c.glueToBlock=t.GlueMode.WithSpace),s(l,[c,r,t.mkText(")"),a])}if(i.elseCount_){let o=t.mkText("else");o.glueToBlock=t.GlueMode.WithSpace,s(l,[o,K(e,T(i,"ELSE"))])}return l}(i,o,n);break;case"pxt_controls_for":case"controls_for":case"controls_simple_for":l=function(e,i,o){let s=T(i,"TO"),l=T(i,"DO"),n=T(i,"BY"),r=T(i,"FROM"),a=!n||n.type.match(/^math_number/)&&1==A(n),c=V(e,i,_(i).getField("VAR").getText());return[t.mkText("for (let "+c.escapedName+" = "),r?M(e,r,o):t.mkText("0"),t.mkText("; "),t.mkInfix(t.mkText(c.escapedName),"<=",M(e,s,o)),t.mkText("; "),a?t.mkText(c.escapedName+"++"):t.mkInfix(t.mkText(c.escapedName),"+=",M(e,n,o)),t.mkText(")"),K(e,l)]}(i,o,n);break;case"pxt_controls_for_of":case"controls_for_of":l=function(e,i,o){let s=T(i,"LIST"),l=T(i,"DO"),n=V(e,i,_(i).getField("VAR").getText());return[t.mkText("for (let "+n.escapedName+" of "),M(e,s,o),t.mkText(")"),K(e,l)]}(i,o,n);break;case"variables_set":l=[U(i,o,n)];break;case"variables_change":l=[H(i,o,n)];break;case"controls_repeat_ext":l=function(e,i,o){let s=M(e,T(i,"TIMES"),o),l=K(e,T(i,"DO")),n="index";for(let t=2;V(e,i,n);t++)n="index"+t;return[t.mkText("for (let "+n+" = 0; "),t.mkInfix(t.mkText(n),"<",s),t.mkText("; "+n+"++)"),l]}(i,o,n);break;case"device_while":l=function(e,i,o){let s=M(e,T(i,"COND"),o),l=K(e,T(i,"DO"));return[t.mkText("while ("),s,t.mkText(")"),l]}(i,o,n);break;case"procedures_defnoreturn":l=function(e,i,o){const s=P(i.getFieldValue("NAME"),e,!0),l=T(i,"STACK");return[t.mkText("function "+s+"() "),K(e,l)]}(i,o);break;case"function_definition":l=function(e,i,o){const s=P(i.getField("function_name").getText(),e,!0),l=T(i,"STACK"),n=i.getArguments().map((t=>{if("Array"==t.type){const o=v(V(e,i,t.name).type),s=(null==o?void 0:o.type)&&"Array"!==o.type?o.type:"any[]";return`${P(t.name,e)}: ${s}`}return`${P(t.name,e)}: ${t.type}`})),r=be(i,!1);return[t.mkText(`function ${s} (${n.join(", ")})${r?": any":""}`),K(e,l)]}(i,o);break;case"procedures_callnoreturn":l=[D(i,o)];break;case"function_call":l=[L(i,o,n,!0)];break;case pxtc.TS_RETURN_STATEMENT_TYPE:l=[R(i,o,n)];break;case ts.pxtc.ON_START_TYPE:l=function(i,o){const s=K(i,T(o,"HANDLER"));return e.appTarget.compile&&e.appTarget.compile.onStartText&&s&&s.children&&s.children.unshift(t.mkStmt(t.mkText(`// ${pxtc.ON_START_COMMENT}\n`))),s}(i,o).children;break;case pxtc.TS_STATEMENT_TYPE:l=function(e,i){return i.getLines().map((e=>t.mkText(e+"\n")))}(0,o);break;case pxtc.PAUSE_UNTIL_TYPE:l=function(i,o,s){const l=e.appTarget.runtime&&e.appTarget.runtime.pauseUntilBlock;e.Util.assert(!!l,"target has block enabled");const n=l.namespace,r=l.callName||"pauseUntil",a=W(i,o,{definitionName:"PREDICATE",actualName:"PREDICATE"},s),c=[t.mkGroup([t.mkText("() => "),a])];return n?[t.mkStmt(t.H.namespaceCall(n,r,c,!1))]:[t.mkStmt(t.H.mkCall(r,c,!1,!1))]}(i,o,n);break;case pxtc.TS_DEBUGGER_TYPE:l=function(e,i){if("1"==i.getFieldValue("ON_OFF"))return[t.mkText("debugger;\n")];return[]}(0,o);break;case pxtc.TS_BREAK_TYPE:l=[t.mkText("break;\n")];break;case pxtc.TS_CONTINUE_TYPE:l=[t.mkText("continue;\n")];break;default:l=i.stdCallTable[o.type]?[G(i,o,n)]:[t.mkStmt(M(i,o,n))]}let r=l[l.length-1];return r&&!r.id&&(r.id=o.id),n.length&&function(e,i){const o=[];for(const i of e)for(const e of i.split("\n"))o.push(t.mkText(`// ${e}`)),o.push(t.mkNewLine());for(const e of o.reverse())i.unshift(e)}(n,l),l.forEach((i=>{!(i.type===t.NT.Block||i.type===t.NT.Prefix&&e.Util.startsWith(i.op,"//"))||o.type==pxtc.ON_START_TYPE&&i.id||(i.id=o.id)})),l}function K(e,i){let o=[],l=i;for(;i;)i.isEnabled()&&s(o,z(e,i)),i=i.getNextBlock();return l&&e.blockDeclarations[l.id]&&e.blockDeclarations[l.id].filter((e=>!e.alreadyDeclared)).forEach((t=>{o.unshift(le(t,e.blocksInfo)),t.alreadyDeclared=r.Implicit})),t.mkBlock(o)}function Y(e){const i=t.mkStmt(t.mkText(";"));return i.glueToBlock=t.GlueMode.NoSpace,t.mkGroup([i,e])}function Q(t,i,o={}){let s=function(e,t){return{workspace:e,options:t,stdCallTable:{},userFunctionReturnValues:{},diagnostics:[],errors:[],renames:{oldToNew:{},takenNames:{},oldToNewFunctions:{}},stats:{},enums:[],kinds:[],idToScope:{},blockDeclarations:{},allVariables:[],blocksInfo:null}}(t,o);return s.blocksInfo=i,i&&(Object.keys(i.apis.byQName).forEach((e=>{const t=i.apis.byQName[e];!t.pkg||6!==t.kind&&3!==t.kind&&5!==t.kind&&4!==t.kind||(s.renames.takenNames[t.qName]=!0)})),i.enumsByName&&Object.keys(i.enumsByName).forEach((e=>s.enums.push(i.enumsByName[e]))),i.kindsByName&&Object.keys(i.kindsByName).forEach((e=>s.kinds.push(i.kindsByName[e]))),i.blocks.forEach((t=>{if(s.stdCallTable[t.attributes.blockId])return void e.reportError("blocks","function already defined",{details:t.attributes.blockId,qualifiedName:t.qName,packageName:t.pkg});s.renames.takenNames[t.namespace]=!0;const i=e.blocks.compileInfo(t),o=!!i.thisParameter;s.stdCallTable[t.attributes.blockId]={namespace:t.namespace,f:t.name,comp:i,attrs:t.attributes,isExtensionMethod:o,isExpression:t.retType&&"void"!==t.retType,imageLiteral:t.attributes.imageLiteral,imageLiteralColumns:t.attributes.imageLiteralColumns,imageLiteralRows:t.attributes.imageLiteralRows,hasHandler:e.blocks.hasHandler(t),property:!t.parameters,isIdentity:"TD_ID"==t.attributes.shim}})),t.getTopBlocks(!1).filter(ke).forEach((e=>{P("procedures_defnoreturn"===e.type?e.getFieldValue("NAME"):e.getField("function_name").getText(),s,!0)}))),s}function J(e,t){if(e.type===ts.pxtc.ON_START_TYPE)return 0;const i=t.stdCallTable[e.type],o=ee(t,e),s=1+ts.pxtc.Util.codalHash16(o);return i&&i.attrs.afterOnStart?s:-s}function Z(i,o,l){try{let n=o.getAllBlocks(!1);e.react.getTilemapProject&&e.react.getTilemapProject().removeInactiveBlockAssets(n.map((e=>e.id)));let a=o.getTopBlocks(!0);a=a.sort(((e,t)=>J(e,i)-J(t,i))),function(e,t,i){t.forEach((e=>e.setEnabled(!0)));const o={};function s(e,t){o[e]?ie(t,!1):(ie(t,!0),o[e]=t)}i.forEach((t=>{const i=e.stdCallTable[t.type];if(t.type==ts.pxtc.ON_START_TYPE)s(ts.pxtc.ON_START_TYPE,t);else{if(ke(t)||i&&i.attrs.blockAllowMultiple&&!i.attrs.handlerStatement)return;if(i&&i.hasHandler&&!i.attrs.handlerStatement){s(i.attrs.blockHandlerKey||ee(e,t),t)}else{let e=t;for(;e;)ie(t,!1),e=e.getNextBlock()}}}))}(i,n,a),n=n.filter((e=>e.isEnabled())),a=a.filter((e=>e.isEnabled())),function(e,t){let i,o=1;e.forEach((e=>{if(e.type===ts.pxtc.ON_START_TYPE){const o=e.getInputTargetBlock("HANDLER");o&&(i={firstStatement:o,declaredVars:{},referencedVars:[],children:[],assignedVars:[]},s(o,i,t))}})),i||(i={firstStatement:null,declaredVars:{},referencedVars:[],children:[],assignedVars:[]});return e.forEach((e=>{e.type!==ts.pxtc.ON_START_TYPE&&s(e,i,t)})),Object.keys(i.declaredVars).forEach((e=>{const t=i.declaredVars[e];delete i.declaredVars[e];(pe(i,t.id)||i).declaredVars[e]=t})),fe(i,t),de(i,t),i;function s(e,t,i){if(i.idToScope[e.id]=t,"variables_get"===e.type){const i=l(e.getField("VAR").getText(),t);t.referencedVars.push(i.id)}else if("variables_set"===e.type||"variables_change"===e.type){const i=l(e.getField("VAR").getText(),t);t.assignedVars.push(i.id),t.referencedVars.push(i.id)}else if(e.type===pxtc.TS_STATEMENT_TYPE){const i=e.declaredVariables;if(i){i.split(",").forEach((e=>{l(e,t).alreadyDeclared=r.Argument}))}}if(function(e){return e.inputList.some((e=>e.type===Blockly.NEXT_STATEMENT))}(e)){const l=function(e,t){switch(e.type){case"pxt_controls_for":case"controls_simple_for":return[{name:_(e).getField("VAR").getText(),type:d}];case"pxt_controls_for_of":case"controls_for_of":return[{name:_(e).getField("VAR").getText(),type:u(null)}];case"function_definition":return e.getArguments().filter((e=>"Array"===e.type)).map((e=>{const t=u(null);return t.isArrayType=!0,{name:e.name,type:t,isFunctionParameter:!0}}))}if(j(e)){const t=e.mutation.getDeclaredVariables();if(t)return Object.keys(t).map((e=>({name:e,type:u(t[e])})))}let i=t.stdCallTable[e.type];if(i&&i.comp.handlerArgs.length)return ae(e,i);return[]}(e,i).map((e=>Object.assign(Object.assign({},e),{id:o++})));let n=t;l.length&&(n={parent:t,firstStatement:e,declaredVars:{},referencedVars:[],assignedVars:[],children:[]},l.forEach((e=>{e.alreadyDeclared=r.Assigned,n.declaredVars[e.name]=e})),i.idToScope[e.id]=n),t!==n&&t.children.push(n),me(e,(e=>{s(e,n,i)})),function(e,t){e.inputList.filter((e=>e.type===Blockly.NEXT_STATEMENT)).forEach((e=>{e.connection&&e.connection.targetBlock()&&t(e.connection.targetBlock())}))}(e,(e=>{const t={parent:n,firstStatement:e,declaredVars:{},referencedVars:[],assignedVars:[],children:[]};n.children.push(t),s(e,t,i)}))}else me(e,(e=>{s(e,t,i)}));e.nextConnection&&e.nextConnection.targetBlock()&&s(e.nextConnection.targetBlock(),t,i)}function l(e,t){return t.declaredVars[e]?t.declaredVars[e]:t.parent?l(e,t.parent):(t.declaredVars[e]={name:e,type:u(null),id:o++},t.declaredVars[e])}}(a,i),B(n,i);const c=[],p=function(e,t){if(!e.length||e.some((e=>!e.rendered)))return{orphans:t,idToComments:{}};const i=e.map((e=>{const t=e.getBoundingRectangle(),i=e.getHeightWidth();return{id:e.id,x:t.left,y:t.top,width:i.width,height:i.height}})),o={orphans:[],idToComments:{}},s=20;for(const e of t){const t=e.getBoundingRectangle(),l=e.getHeightWidth(),n=t.left,r=t.top;let a;for(const e of i)(ge(n,r,l.width,l.height,e)||!a&&ge(n-s,r-s,l.width+2*s,l.height+2*s,e))&&(a=e);a?(o.idToComments[a.id]||(o.idToComments[a.id]=[]),o.idToComments[a.id].push(e)):o.orphans.push(e)}return o}(a,o.getTopComments(!0));p.orphans.forEach((e=>s(c,O(e).children))),a.forEach((e=>{if(p.idToComments[e.id]&&p.idToComments[e.id].forEach((e=>{s(c,O(e).children)})),e.type==ts.pxtc.ON_START_TYPE)s(c,z(i,e));else{const o=t.mkBlock(z(i,e));o.type==t.NT.Block?s(c,o.children):c.push(o)}}));const h=[];i.enums.forEach((e=>{const i=o.getVariablesOfType(e.name);if(i&&i.length){const o=i.map((e=>{const t=/^(\d+)([^0-9].*)$/.exec(e.name);return t?[t[2],parseInt(t[1])]:[e.name,-1]}));o.sort(((e,t)=>e[1]-t[1]));const s=[];let l=-1;o.forEach((([i,o],n)=>{let r;if(e.isBitMask){const e=Math.log2(o);e>=0&&Math.floor(e)===e&&(r=t.H.mkAssign(t.mkText(i),t.H.mkSimpleCall("<<",[t.H.mkNumberLiteral(1),t.H.mkNumberLiteral(e)])))}else if(e.isHash){const e=ts.pxtc.Util.codalHash16(i.toLowerCase());r=t.H.mkAssign(t.mkText(i),t.H.mkNumberLiteral(e))}r||(r=o===l+1?t.mkText(i):t.H.mkAssign(t.mkText(i),t.H.mkNumberLiteral(o))),s.push(r),l=o}));const n=t.mkCommaSep(s,!0);n.glueToBlock=t.GlueMode.NoSpace,h.push(t.mkGroup([t.mkText(`enum ${e.name}`),t.mkBlock([n])]))}})),i.kinds.forEach((e=>{const i=o.getVariablesOfType("KIND_"+e.name);if(i&&i.length){const o=i.map((e=>e.name)).filter((t=>-1===e.initialMembers.indexOf(t)));o.length&&h.push(t.mkGroup([t.mkText(`namespace ${e.name}`),t.mkBlock(o.map((i=>t.mkStmt(t.mkText(`export const ${i} = ${e.name}.${e.createFunctionName}()`)))))]))}}));const m=i.allVariables.filter((e=>!e.alreadyDeclared)).map((e=>le(e,l)));return i.allVariables.filter((e=>e.alreadyDeclared===r.Implicit&&!e.isAssigned)).forEach((e=>{const t=v(e.type);"string"===t.type||"number"===t.type||"boolean"===t.type||y(t.type)||i.diagnostics.push({blockId:e.firstReference&&e.firstReference.id,message:lf("Variable '{0}' is never assigned",e.name)})})),[h.concat(m.concat(c)),i.diagnostics]}catch(e){let t=e.block;if(!t)throw e;t.setWarningText(e+""),i.errors.push(t)}finally{x()}return[null,null]}function ee(e,t){if(t.type==ts.pxtc.ON_START_TYPE)return JSON.stringify({name:ts.pxtc.ON_START_TYPE});if(t.type==ts.pxtc.FUNCTION_DEFINITION_TYPE)return JSON.stringify({type:"function",name:t.getFieldValue("function_name")});return JSON.stringify(te(t)).replace(/"id"\s*:\s*"[^"]+"/g,"")}function te(e){const t=[],i=[];for(const o of e.inputList){for(const e of o.fieldRow)e.name&&t.push(e.getText());o.type===Blockly.INPUT_VALUE&&(o.connection.targetBlock()?i.push(te(o.connection.targetBlock())):i.push(null))}return{type:e.type,fields:t,inputs:i}}function ie(e,t){e.setEnabled(t);const i=e.getDescendants(!1);for(const e of i)e.setEnabled(t)}function oe(e,o,s){let l=t.flattenNode(o);return i("format",{format:{input:l.output,pos:1}}).then((()=>({source:l.output,sourceMap:l.sourceMap,stats:e.stats,diagnostics:s||[]})))}function se(e,t){var i;const o=null===(i=e.getCommentText)||void 0===i?void 0:i.call(e);o&&t.push(o)}function le(e,i){const o=v(e.type);let s;s="Array"===o.type?t.mkText("[]"):F(o);let l="";if("null"==s.op||"[]"==s.op){let e=o.type;"Array"!==e&&"null[]"!==e||(e="number[]");let n=i.apis.byQName[e];n&&n.attributes.autoCreate?s=t.mkText(n.attributes.autoCreate+"()"):l=": "+e}return t.mkStmt(t.mkText("let "+e.escapedName+l+" = "),s)}function ne(e,t){if(t.attrs.compileHiddenArguments)return t.comp.parameters.reduce(((e,t)=>(t.isOptional&&e++,e)),0);if(e.mutationToDom){const t=e.mutationToDom();if(t.hasAttribute("_expanded")){const e=parseInt(t.getAttribute("_expanded"));return isNaN(e)?0:Math.max(e,0)}}return 0}function re({comp:e},t){const i=[];return e.thisParameter&&i.push(e.thisParameter),e.parameters.forEach((e=>{e.isOptional&&t>0?(i.push(e),--t):e.isOptional||i.push(e)})),i}function ae(e,t){let i=[];if(t.attrs.draggableParameters)for(let o=0;o<t.comp.handlerArgs.length;o++){const s=t.comp.handlerArgs[o];let l;const n=T(e,"HANDLER_DRAG_PARAM_"+s.name);if(l="reporter"===t.attrs.draggableParameters?n&&n.getFieldValue("VALUE"):n&&n.getField("VAR").getText(),null===l)break;i.push({name:l,type:u(s.type)})}else for(let o=0;o<t.comp.handlerArgs.length;o++){const s=t.comp.handlerArgs[o],l=e.getField("HANDLER_"+s.name),n=l&&l.getText();if(null===n)break;i.push({name:n,type:u(s.type)})}return i}function ce(e,t){if(-1!==e.referencedVars.indexOf(t))return!0;for(const i of e.children)if(ce(i,t))return!0;return!1}function ue(e,t){if(-1!==e.assignedVars.indexOf(t))return!0;for(const i of e.children)if(ue(i,t))return!0;return!1}function de(e,t){for(const t of Object.keys(e.declaredVars)){const o=e.declaredVars[t];o.escapedName||(o.escapedName=i(t))}function i(i){if(!i)return"_";let s=ts.pxtc.escapeIdentifier(i);if(t.renames.takenNames[s]||o(s,e,i)){let l=2;for(;t.renames.takenNames[s+l]||o(s+l,e,i);)l++;s+=l}return s}function o(e,t,i){if(t){for(const o of Object.keys(t.declaredVars)){const s=t.declaredVars[o];if((i!==s.name||s.name!==s.escapedName)&&s.escapedName===e)return!0}return o(e,t.parent,i)}return!1}e.children.forEach((e=>de(e,t)))}function pe(e,t){let i;if(-1!==e.referencedVars.indexOf(t))return e;for(const o of e.children)if(ce(o,t)){if(ue(o,t))return e;if(i)return e;i=o}return i?pe(i,t):void 0}function he(e,t){return t&&t.declaredVars[e]?t.declaredVars[e]:t&&t.parent?he(e,t.parent):null}function me(e,t,i=!1){e.inputList.filter((e=>e.type===Blockly.INPUT_VALUE)).forEach((e=>{e.connection&&e.connection.targetBlock()&&(t(e.connection.targetBlock()),i&&me(e.connection.targetBlock(),t,i))}))}function fe(e,t){const i=Object.keys(e.declaredVars);if(i.length){const o=i.map((t=>e.declaredVars[t]));e.firstStatement&&(t.blockDeclarations[e.firstStatement.id]=o.concat(t.blockDeclarations[e.firstStatement.id]||[])),o.forEach((e=>t.allVariables.push(e)))}e.children.forEach((e=>fe(e,t)))}function ge(e,t,i,o,s){const l=r(e,s.x,s.x+s.width)||r(s.x,e,e+i),n=r(t,s.y,s.y+s.height)||r(s.y,t,t+o);return l&&n;function r(e,t,i){return e>=t&&e<=i}}function ke(e){return"procedures_defnoreturn"===e.type||"function_definition"===e.type}function ye(e){return e.getField("function_name").getText()}function be(e,t){const i=ye(e),o={};return function e(s){let l;l=t?s.getDescendants(!1).filter((e=>"function_return"==e.type)).map((e=>T(e,"RETURN_VALUE"))).filter((e=>e&&"function_call_output"===e.type)):s.getDescendants(!1).filter((e=>"function_call_output"==e.type));for(const t of l){const s=ye(t);if(s===i)return!0;if(!o[s]&&(o[s]=!0,e(Blockly.Functions.getDefinition(s,t.workspace))))return!0}return!1}(e)}t.compileExpression=M,t.escapeVarName=P,t.mkEnv=Q,t.compileBlockAsync=function(e,t){const i=e.workspace,o=Q(i,t);B(i&&i.getAllBlocks(!1),o);const s=z(o,e);return x(),oe(o,s)},t.callKey=ee,t.findBlockIdByPosition=function(e,t){if(!t)return;let i,o;for(let s=0;s<e.length;++s){let l=e[s];l.startPos<=t.start&&l.endPos>=t.start+t.length&&(!i||o>l.endPos-l.startPos)&&(i=l,o=l.endPos-l.startPos)}return i?i.id:void 0},t.findBlockIdByLine=function(e,t){if(!t)return;let i,o;for(let s=0;s<e.length;++s){let l=e[s];l.startLine<=t.start&&l.endLine>t.start+t.length&&(!i||o>l.endLine-l.startLine)&&(i=l,o=l.endLine-l.startLine)}return i?i.id:void 0},t.compileAsync=function(e,t,i={}){const o=Q(e,t,i),[s,l]=Z(o,e,t);return oe(o,s,l)}}(e.blocks||(e.blocks={}))}(pxt||(pxt={})),function(e){!function(t){let i={};function o(e,t,o){null==i[e]&&(i[e]={field:t,validator:o})}t.initFieldEditors=function(){var t;o("text",pxtblockly.FieldTextInput),o("note",pxtblockly.FieldNote),o("gridpicker",pxtblockly.FieldGridPicker),o("textdropdown",pxtblockly.FieldTextDropdown),o("numberdropdown",pxtblockly.FieldNumberDropdown),o("imagedropdown",pxtblockly.FieldImageDropdown),o("colorwheel",pxtblockly.FieldColorWheel),o("toggle",pxtblockly.FieldToggle),o("toggleonoff",pxtblockly.FieldToggleOnOff),o("toggleyesno",pxtblockly.FieldToggleYesNo),o("toggleupdown",pxtblockly.FieldToggleUpDown),o("toggledownup",pxtblockly.FieldToggleDownUp),o("togglehighlow",pxtblockly.FieldToggleHighLow),o("togglewinlose",pxtblockly.FieldToggleWinLose),o("colornumber",pxtblockly.FieldColorNumber),o("images",pxtblockly.FieldImages),o("sprite",pxtblockly.FieldSpriteEditor),o("animation",pxtblockly.FieldAnimationEditor),o("tilemap",pxtblockly.FieldTilemap),o("tileset",pxtblockly.FieldTileset),o("speed",pxtblockly.FieldSpeed),o("turnratio",pxtblockly.FieldTurnRatio),o("protractor",pxtblockly.FieldProtractor),o("position",pxtblockly.FieldPosition),o("melody",pxtblockly.FieldCustomMelody),o("soundeffect",pxtblockly.FieldSoundEffect),o("autocomplete",pxtblockly.FieldAutoComplete),(null===(t=e.appTarget.appTheme)||void 0===t?void 0:t.songEditor)&&o("musiceditor",pxtblockly.FieldMusicEditor)},t.registerFieldEditor=o,t.createFieldEditor=function(t,o,s){if(null==i[t])return console.error(`Field editor ${t} not registered`),null;s||(s={}),e.Util.assert(null==s.lightMode,"lightMode is a reserved parameter for custom fields"),s.lightMode=e.options.light;let l=i[t];return new l.field(o,s,l.validator)}}(e.blocks||(e.blocks={}))}(pxt||(pxt={})),function(e){!function(t){t.needsDecompiledDiff=function(e,t){if(!e||!t)return!1;const i={};if(e.replace(/id="([^"]+)"/g,((e,t)=>(i[t]=!0,""))),!Object.keys(i).length)return!1;let o=0,s=0;return t.replace(/id="([^"]+)"/g,((e,t)=>(o++,i[t]&&s++,""))),o>0&&0==s},t.diffXml=function(t,i,s){return o(e.blocks.loadWorkspaceXml(t,!0),e.blocks.loadWorkspaceXml(i,!0),s)};const i="#d0d0d0";function o(o,n,r){try{return Blockly.Events.disable(),function(o,n,r){e.tickEvent("blocks.diff",{started:1}),r=r||{};const a=s();if(!o)return{ws:void 0,message:lf("All blocks are new."),added:0,deleted:0,modified:1};if(!n)return{ws:void 0,message:lf("The current blocks seem corrupted."),added:0,deleted:0,modified:1};const c=e.Util.toDictionary(o.getTopBlocks(!1),(e=>l(e,!0)));n.getTopBlocks(!1).forEach((e=>{const t=l(e,!0),i=o.getBlockById(e.id)||c[t];if(i){t==l(i,!0)&&(a("fast unmodified top ",e.id),e.dispose(!1),i.dispose(!1))}}));const u=o.getAllBlocks(!1).filter((e=>e.isEnabled())),d=o.getTopBlocks(!1).filter((e=>e.isEnabled())),p=n.getAllBlocks(!1).filter((e=>e.isEnabled()));if(a("blocks",p.map((e=>e.toDevString()))),a(p),0==u.length&&0==p.length)return e.tickEvent("blocks.diff",{moves:1}),{ws:void 0,message:lf("Some blocks were moved or changed."),added:0,deleted:0,modified:1};const h=d.filter((e=>!n.getBlockById(e.id))),m=u.filter((e=>!n.getBlockById(e.id))),f=p.filter((e=>!o.getBlockById(e.id))),g=e.blocks.initRenderingWorkspace(),k=e.blocks.saveWorkspaceXml(n,!0);e.blocks.domToWorkspaceNoEvents(Blockly.Xml.textToDom(k),g),g.getAllBlocks(!1).filter((e=>!e.isEnabled())).forEach((e=>{a("disabled ",e.toDevString()),e.dispose(!1)}));const y=e.Util.toDictionary(g.getAllBlocks(!1),(e=>e.id));a("todo blocks",y),w("start"),r.hideDeletedTopBlocks||(h.forEach((e=>{a(`deleted top ${e.toDevString()}`),I(e);const t=v(e);I(t),t.setEnabled(!1)})),w("deleted top"));f.map((e=>g.getBlockById(e.id))).filter((e=>!!e)).forEach((e=>{a(`added ${e.toDevString()}`),I(e)})),w("added");const b={};if(!r.hideDeletedBlocks){const e=m.filter((e=>!(y[e.id]||C(e)||e.outputConnection&&e.outputConnection.isConnected())));e.forEach((e=>{const t=v(e);b[e.id]=t.id,a(`deleted block ${e.toDevString()}->${t.toDevString()}`)})),e.forEach((e=>E(e)))}let _=0;if(e.Util.values(y).filter((e=>S(e))).forEach((e=>{a(`moved ${e.toDevString()}`),delete y[e.id],B(e),_++})),w("moved"),e.Util.values(y).filter((e=>D(e))).forEach((e=>{a(`changed ${e.toDevString()}`),delete y[e.id],B(e),_++})),w("changed"),g.getTopBlocks(!1).forEach((e=>{N(e)||(a(`unmodified top ${e.toDevString()}`),delete y[e.id],e.dispose(!1))})),w("cleaned"),e.Util.values(y).filter((e=>!!g.getBlockById(e.id))).forEach((e=>{L(e)})),w("unmodified"),!g.getAllBlocks(!1).length)return e.tickEvent("blocks.diff",{missed:1}),{ws:g,message:lf("Some blocks were changed."),deleted:m.length,added:f.length,modified:_};g.resize(),Blockly.svgResize(g);const T=e.blocks.renderWorkspace(r.renderOptions||{emPixels:20,layout:t.BlockLayout.Flow,aspectRatio:.5,useViewWidth:!0}),x={ws:g,svg:T,deleted:m.length,added:f.length,modified:_};return e.tickEvent("blocks.diff",{deleted:x.deleted,added:x.added,modified:x.modified}),x;function E(e){a(`stitching ${e.toDevString()}->${b[e.id]}`);const t=g.getBlockById(b[e.id]);t.setEnabled(!1),B(t),I(t);const i=e.getPreviousBlock();if(i){const o=g.getBlockById(b[i.id])||g.getBlockById(i.id);if(a(`previous ${e.id}->${t.toDevString()}: ${o.toDevString()}`),o)if(o.nextConnection)t.previousConnection.connect(o.nextConnection);else{const e=o.inputList.slice().reverse().find((e=>e.connection&&e.connection.type==Blockly.NEXT_STATEMENT));e&&t.previousConnection.connect(e.connection)}}const o=e.getNextBlock();if(o){const i=g.getBlockById(b[o.id])||g.getBlockById(o.id);i&&(a(`next ${e.id}->${t.toDevString()}: ${i.toDevString()}`),t.nextConnection.connect(i.previousConnection))}}function B(e){e.__pxt_used=!0}function C(e){return!!e.__pxt_used}function v(e){const t=Blockly.Xml.blockToDom(e,!1),i=Blockly.Xml.domToBlock(t,g);return i.nextConnection&&i.nextConnection.targetConnection&&i.nextConnection.disconnect(),i.previousConnection&&i.previousConnection.targetConnection&&i.previousConnection.disconnect(),i}function A(e){e.rendered=!1,e.inputList.forEach((t=>t.fieldRow.forEach((t=>{t.init(),t.borderRect_&&(t.borderRect_.setAttribute("fill",e.getColour()),t.borderRect_.setAttribute("stroke",e.getColourTertiary()))}))))}function I(e){e.getDescendants(!1).forEach((e=>{delete y[e.id],B(e)}))}function N(e){return!!e.getDescendants(!1).find((e=>C(e)))}function w(t){a(`${t}:`,e.Util.values(y).map((e=>e.toDevString())))}function S(e){const t=o.getBlockById(e.id);if(!t)return!1;const i=e.getPreviousBlock();if(i&&!y[i.id])return!1;const s=e.getNextBlock();if(s&&!y[s.id])return!1;const l=t.getPreviousBlock();if(!l&&!i)return!1;if(!!l!=!!i||l.id!=i.id)return!0;const n=t.getNextBlock();return!(!n&&!s)&&(!!n!=!!s||n.id!=s.id)}function D(e){let t=o.getBlockById(e.id);if(!t)return!1;const i=l(t),s=l(e);return i!=s&&(a(`old ${t.toDevString()}`,i),a(`new ${e.toDevString()}`,s),!0)}function L(e){e.setColour(i),A(e),r.statementsOnly&&(e.inputList||[]).map((e=>e.type==Blockly.INPUT_VALUE&&e.connection&&e.connection.targetBlock())).filter((e=>!!e)).forEach((e=>L(e)))}}(o,n,r)}catch(t){return e.reportException(t),{ws:void 0,message:lf("Oops, we could not diff those blocks."),error:t,deleted:0,added:0,modified:0}}finally{Blockly.Events.enable()}}function s(){return e.options.debug||window&&/diffdbg=1/.test(window.location.href)?console.log:(e,...t)=>{}}function l(e,t){const i=Blockly.Xml.blockToDom(e,!0);return n(i),r(i,(e=>{n(e),t||("next"==e.localName||"statement"==e.localName||"shadow"==e.localName)&&e.remove()})),Blockly.Xml.domToText(i)}function n(e){e.removeAttribute("id"),e.removeAttribute("x"),e.removeAttribute("y"),e.removeAttribute("deletable"),e.removeAttribute("editable"),e.removeAttribute("movable")}function r(t,i){if(t){i(t);for(const o of e.Util.toArray(t.children))r(o,i)}}t.mergeXml=function(e,t,i){return e==t?i:i==t?e:void 0},t.decompiledDiffAsync=function(t,i,l,n,r={}){const a=s(),c=i.outfiles[e.MAIN_BLOCKS];let u=n.outfiles[e.MAIN_BLOCKS];a(c),a(u);const d=e.diff.compute(t,l,{ignoreWhitespace:!0,full:!0});a(d);const p={};let h=0,m=0;d.forEach(((t,o)=>{const s=t[0],l=t.substr(2);let r=l.length;switch(s){case"-":h+=r+1;break;case"+":m+=r+1;break;default:const o=/^\s+/.exec(l);if(o){const e=o[0].length;h+=e,m+=e,r-=e}const s=e.blocks.findBlockIdByPosition(n.blockSourceMap,{start:m,length:r});if(s&&!p[s]){const o=e.blocks.findBlockIdByPosition(i.blockSourceMap,{start:h,length:r});o&&(a(t),a(`id ${h}:${l.length}>${o} ==> ${m}:${l.length}>${s}`),p[s]=o,u=u.replace(s,o))}h+=r+1,m+=r+1}}));const f=e.blocks.loadWorkspaceXml(c,!0),g=e.blocks.loadWorkspaceXml(u,!0);return r.statementsOnly=!0,o(f,g,r)}}(e.blocks||(e.blocks={}))}(pxt||(pxt={})),function(e){!function(t){function i(e,t){const i=[];for(let o=0;o<e.childNodes.length;o++){const s=e.childNodes.item(o);s.tagName===t&&i.push(s)}return i}function o(e,t){return s(e,"block","type",t).concat(s(e,"shadow","type",t))}function s(t,i,o,s){return e.Util.toArray(t.getElementsByTagName(i)).filter((e=>e.getAttribute(o)===s))}function l(e,t,i,o){const l=s(e,t,i,o);return l.length?l[0]:void 0}function n(e,i,o){var s;let n=o.getAttribute("type"),r=Blockly.Blocks[n],a=t.blockSymbol(n);if(!a||!r)return;let c=t.compileInfo(a);null===(s=a.parameters)||void 0===s||s.forEach(((t,s)=>{let n=e.apis.byQName[t.type];if(n&&6==n.kind){let e=l(o,"field","name",c.actualNameToParam[t.name].definitionName);if(e){let t=i[n.name+"."+e.textContent];t&&(e.textContent=t)}}}))}t.domToWorkspaceNoEvents=function(t,i,o){e.tickEvent("blocks.domtow");let s=[];try{Blockly.Events.disable(),s=Blockly.Xml.domToWorkspace(t,i),function(e,t){e.getAllBlocks(!1).filter((e=>!!e.getCommentText())).forEach((i=>{var o,s;const l=i.getCommentText();if(/@hide/.test(l)&&(null==t?void 0:t.applyHideMetaComment))return void i.dispose(!0);let n=l;/@highlight/.test(n)&&(n=n.replace(/@highlight/g,"").trim(),null===(s=(o=e).highlightBlock)||void 0===s||s.call(o,i.id,!0)),/@collapsed/.test(n)&&!i.getParent()&&(n=n.replace(/@collapsed/g,"").trim(),i.setCollapsed(!0)),l===n||(null==t?void 0:t.keepMetaComments)||i.setCommentText(n||null)}))}(i,o)}catch(t){e.reportException(t)}finally{Blockly.Events.enable()}return s.filter((e=>!!i.getBlockById(e)))},t.clearWithoutEvents=function(t){if(e.tickEvent("blocks.clear"),t)try{Blockly.Events.disable(),t.clear(),t.clearUndo()}finally{Blockly.Events.enable()}},t.saveWorkspaceXml=function(e,t){const i=Blockly.Xml.workspaceToDom(e,!t);return Blockly.Xml.domToText(i)},t.saveBlocksXml=function(e,t){return e.getTopBlocks(!1).map((e=>Blockly.Xml.domToText(Blockly.Xml.blockToDom(e,!t))))},t.getDirectChildren=i,t.getBlocksWithType=o,t.getChildrenWithAttr=s,t.getFirstChildWithAttr=l,t.loadBlocksXml=function(e,t){let i=Blockly.Xml.textToDom(t),o=Blockly.Xml.domToBlock(i,e);if(e.getMetrics){let t=e.getMetrics(),i=o.getHeightWidth();o.moveBy(t.viewLeft+t.viewWidth/2-i.width/2,t.viewTop+t.viewHeight/2-i.height/2)}},t.loadWorkspaceXml=function(t,i=!1,o){const s=new Blockly.Workspace;try{const i=Blockly.Xml.textToDom(t);return e.blocks.domToWorkspaceNoEvents(i,s,o),s}catch(t){return i||e.reportException(t),null}},t.importXml=function(l,r,a,c=!1){try{e.blocks.initializeAndInject(a);const c=(new DOMParser).parseFromString(r,"application/xml"),d=e.patching.computePatches(l);d&&(d.filter((e=>"blockId"==e.type)).forEach((t=>Object.keys(t.map).forEach((i=>{o(c,i).forEach((o=>{o.setAttribute("type",t.map[i]),e.debug(`patched block ${i} -> ${t.map[i]}`)}))})))),d.filter((e=>"blockValue"==e.type)).forEach((t=>Object.keys(t.map).forEach((s=>{const l=s.split("."),n=l[0];l[1];o(c,n).reduce(((e,t)=>e.concat(i(t,"value"))),[]).forEach((i=>{i.setAttribute("name",t.map[s]),e.debug(`patched block value ${s} -> ${t.map[s]}`)}))})))),d.filter((e=>"userenum"==e.type)).forEach((t=>Object.keys(t.map).forEach((i=>{s(c,"variable","type",i).forEach((o=>{o.setAttribute("type",t.map[i]),e.debug(`patched enum variable type ${i} -> ${t.map[i]}`)}))})))));const p=i(c.children.item(0),"shadow");for(const e of p){const t=c.createElement("block");e.getAttributeNames().forEach((i=>t.setAttribute(i,e.getAttribute(i))));for(let i=0;i<e.childNodes.length;i++)t.appendChild(e.childNodes.item(i));e.replaceWith(t)}const h={};Object.keys(a.apis.byQName).forEach((e=>{let t=a.apis.byQName[e];7==t.kind&&(h[t.namespace+"."+(t.attributes.blockImportId||t.attributes.block||t.attributes.blockId||t.name)]=t.namespace+"."+t.name)}));const m=c.getElementsByTagName("block");for(let e=0;e<m.length;++e)n(a,h,m[e]);return function(i,s){const l=o(i,ts.pxtc.ON_START_TYPE);let n=l.length?l[0]:void 0;if(n)return void n.removeAttribute("deletable");let r=[];const a=s.blocksById;let c,u=i.firstElementChild;for(;u;){const o=u.nextElementSibling,s=u.getAttribute("type");if(!u.getAttribute("disabled")&&!u.getElementsByTagName("statement").length&&(e.blocks.buildinBlockStatements[s]||a[s]&&"void"==a[s].retType&&!t.hasArrowFunction(a[s])))if(c){const e=i.ownerDocument.createElement("next");e.appendChild(u),c.appendChild(e),u.removeAttribute("x"),u.removeAttribute("y"),c=u}else c=i.ownerDocument.createElement("statement"),c.setAttribute("name","HANDLER"),n||(n=i.ownerDocument.createElement("block"),n.setAttribute("type",ts.pxtc.ON_START_TYPE),r.push(n)),n.appendChild(c),c.appendChild(u),u.removeAttribute("x"),u.removeAttribute("y"),c=u;u=o}r.forEach((e=>i.appendChild(e)))}(c.documentElement,a),u=c.documentElement,e.U.toArray(u.querySelectorAll("block[type=procedures_defnoreturn]")).forEach((e=>{e.setAttribute("type","function_definition"),e.querySelector("field[name=NAME]").setAttribute("name","function_name")})),e.U.toArray(u.querySelectorAll("block[type=procedures_callnoreturn]")).forEach((e=>{e.setAttribute("type","function_call"),e.querySelector("field[name=NAME]").setAttribute("name","function_name")})),e.blocks.extensionBlocklyPatch&&e.blocks.extensionBlocklyPatch(l,c.documentElement),(new XMLSerializer).serializeToString(c)}catch(t){return c||e.reportException(t),r}var u},t.validateAllReferencedBlocksExist=function(t){e.U.assert(!!(null===Blockly||void 0===Blockly?void 0:Blockly.Blocks),"Called validateAllReferencedBlocksExist before initializing Blockly");const i=Blockly.Xml.textToDom(t),o=i.querySelectorAll("block");for(let e=0;e<o.length;e++)if(!Blockly.Blocks[o.item(e).getAttribute("type")])return!1;const s=i.querySelectorAll("shadow");for(let e=0;e<s.length;e++)if(!Blockly.Blocks[s.item(e).getAttribute("type")])return!1;return!0}}(e.blocks||(e.blocks={}))}(pxt||(pxt={})),function(e){var t;(function(i){i.patchBlocksFromOldWorkspace=function(t,i,o){const s=e.blocks.loadWorkspaceXml(o,!0);return function(t,i,o){let s,l;i.getTopBlocks(!1).filter((e=>e.isEnabled())).forEach((n=>{const r=n.xy_;if(r&&0!=r.x&&0!=r.y){s||(s=e.blocks.mkEnv(i,t),l={},o.getTopBlocks(!1).forEach((t=>{const i=e.blocks.callKey(s,t),o=l[i]||[];o.push(t),l[i]=o})));const a=e.blocks.callKey(s,n),c=(l[a]||[]).shift();c&&(c.xy_=r.clone())}}))}(t,i,s),function(t,i){const o=Blockly.Xml.workspaceToD