From a95d871e9705d879abc726d6f96eeccec58c2a66 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Mon, 13 Aug 2018 12:51:26 -0700 Subject: [PATCH] Convert division to idiv (#1072) * Convert division to idiv * Reflect changes in pxt-core to the idiv XML --- editor/extension.ts | 94 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 14 deletions(-) diff --git a/editor/extension.ts b/editor/extension.ts index dea0fa39..866407ef 100644 --- a/editor/extension.ts +++ b/editor/extension.ts @@ -598,24 +598,60 @@ namespace pxt.editor { // device_random now refers to randomRange() so we need to add the missing lower bound argument U.toArray(dom.querySelectorAll("block[type=device_random]")) .forEach(node => { - for (let i = 0; i < node.children.length; i++) { - const child = node.children.item(i); - if (child.tagName === "value" && child.getAttribute("name") === "min") { - return; - } - } + if (getValue(node, "min")) return; const v = node.ownerDocument.createElement("value"); - const s = node.ownerDocument.createElement("shadow"); - const f = node.ownerDocument.createElement("field"); - v.setAttribute("name", "min"); - v.appendChild(s); - s.setAttribute("type", "math_number"); - s.appendChild(f); - f.setAttribute("name", "NUM"); - f.textContent = "0"; + addNumberShadow(v); node.appendChild(v); }); + + /* + + DIVIDE + + 0 + 2 + + + 1 + 3 + + + */ + U.toArray(dom.querySelectorAll("block[type=math_arithmetic]")) + .forEach(node => { + const op = getField(node, "OP"); + if (!op || op.textContent.trim() !== "DIVIDE") return; + + // Convert to integer division + /* + + + idiv + + 0 + + + 0 + + + */ + + node.setAttribute("type", "math_js_op"); + op.textContent = "idiv"; + + const mutation = node.ownerDocument.createElement("mutation"); + mutation.setAttribute("op-type", "infix"); + // mutation has to be first or Blockly will drop the second argument + node.insertBefore(mutation, node.firstChild); + + const a = getValue(node, "A"); + if (a) a.setAttribute("name", "ARG0"); + + const b = getValue(node, "B"); + if (b) b.setAttribute("name", "ARG1"); + }) + } initExtensionsAsync = function (opts: pxt.editor.ExtensionOptions): Promise { @@ -679,4 +715,34 @@ namespace pxt.editor { return Promise.resolve(res); } + function getField(parent: Element, name: string) { + return getFieldOrValue(parent, name, true); + } + + function getValue(parent: Element, name: string) { + return getFieldOrValue(parent, name, false); + } + + function getFieldOrValue(parent: Element, name: string, isField: boolean) { + const nodeType = isField ? "field" : "value"; + for (let i = 0; i < parent.children.length; i++) { + const child = parent.children.item(i); + if (child.tagName === nodeType && child.getAttribute("name") === name) { + return child; + } + } + return undefined; + } + + function addNumberShadow(valueNode: Element) { + const s = valueNode.ownerDocument.createElement("shadow"); + s.setAttribute("type", "math_number"); + + const f = valueNode.ownerDocument.createElement("field"); + f.setAttribute("name", "NUM"); + f.textContent = "0"; + + s.appendChild(f); + valueNode.appendChild(s); + } }