blocklyでobjectを動かす。 その8


概要

blocklyでobjectを動かして見る。
真理値表、やってみた。

写真

ステージを作る。

<div id="outp"></div>

ブロックを書く。

Blockly.Blocks.table = {
    init: function() {
        this.jsonInit({
        message0: "table %1 times",
        args0:[{
            type: "input_value",
            name: "TIMES",
            check: "Number"
        }],
        message1: "%{BKY_CONTROLS_REPEAT_INPUT_DO} %1",
        args1:[{
            type: "input_statement",
            name: "DO"
        }],
        previousStatement: null,
        nextStatement: null,
        style: "loop_blocks",
        tooltip: "%{BKY_CONTROLS_REPEAT_TOOLTIP}",
        helpUrl: "%{BKY_CONTROLS_REPEAT_HELPURL}"
      })
  }
};

Blockly.Blocks.ini = {
    init: function() {
        this.jsonInit({
        message0: "ini %1",
        args0: [{
            type: "input_value",
            name: "TEXT"
        }],
        previousStatement: null,
        nextStatement: null,
        style: "text_blocks",
        tooltip: Blockly.Msg.TEXT_PRINT_TOOLTIP,
        helpUrl: Blockly.Msg.TEXT_PRINT_HELPURL
    })
}};

Blockly.Blocks.nand = {
    init: function() {
        this.jsonInit({
        message0: "nand %1",
        args0: [{
            type: "input_value",
            name: "TEXT"
        }],
        previousStatement: null,
        nextStatement: null,
        style: "text_blocks",
        tooltip: Blockly.Msg.TEXT_PRINT_TOOLTIP,
        helpUrl: Blockly.Msg.TEXT_PRINT_HELPURL
    })
}};

Blockly.Blocks.and = {
    init: function() {
        this.jsonInit({
        message0: "and %1",
        args0: [{
            type: "input_value",
            name: "TEXT"
        }],
        previousStatement: null,
        nextStatement: null,
        style: "text_blocks",
        tooltip: Blockly.Msg.TEXT_PRINT_TOOLTIP,
        helpUrl: Blockly.Msg.TEXT_PRINT_HELPURL
    })
}};

Blockly.Blocks.xor = {
    init: function() {
        this.jsonInit({
        message0: "xor %1",
        args0: [{
            type: "input_value",
            name: "TEXT"
        }],
        previousStatement: null,
        nextStatement: null,
        style: "text_blocks",
        tooltip: Blockly.Msg.TEXT_PRINT_TOOLTIP,
        helpUrl: Blockly.Msg.TEXT_PRINT_HELPURL
    })
}};

フローを書く。

objectを書く。




Blockly.JavaScript.table = function(a) {
    var b = a.getField("TIMES") ? String(Number(a.getFieldValue("TIMES"))) : Blockly.JavaScript.valueToCode(a,"TIMES",Blockly.JavaScript.ORDER_ASSIGNMENT)||"0",c=Blockly.JavaScript.statementToCode(a,"DO");
    c = Blockly.JavaScript.addLoopTrap(c, a);
    a = "";
    var d = Blockly.JavaScript.variableDB_.getDistinctName("count", Blockly.VARIABLE_CATEGORY_NAME),
        e = b;
    b.match(/^\w+$/) || Blockly.isNumber(b) || (e=Blockly.JavaScript.variableDB_.getDistinctName("repeat_end",Blockly.VARIABLE_CATEGORY_NAME),
    a += "var " + e + " = " + b + ";\n");
    return a + ("c0 = new C(" + e + ");\n" + c + "c0.table();\n")
};



Blockly.JavaScript.ini = function(a) {
    return "c0.init(" + (Blockly.JavaScript.valueToCode(a, "TEXT", Blockly.JavaScript.ORDER_NONE) || "''") + ");\n"
};


Blockly.JavaScript.nand = function(a) {
    return "c0.nand(" + (Blockly.JavaScript.valueToCode(a, "TEXT", Blockly.JavaScript.ORDER_NONE) || "0") + ");\n"
};



Blockly.JavaScript.and = function(a) {
    return "c0.and(" + (Blockly.JavaScript.valueToCode(a, "TEXT", Blockly.JavaScript.ORDER_NONE) || "0") + ");\n"
};


Blockly.JavaScript.xor = function(a) {
    return "c0.xor(" + (Blockly.JavaScript.valueToCode(a, "TEXT", Blockly.JavaScript.ORDER_NONE) || "0") + ");\n"
};

var c0;

class C {
    constructor(n) {
      this.h = "";
      this.n = n;
        this.s = [];
        var t = new Array(n);
        for (var i = 0; i < n; i++)
        {
          t[i] = 0;
        }
        this.s.push(t);
        return this;
    }
    init(a, b) {
      b = b + 1;
      for (var i = a; i < b; i++)
        {
          var t = [];
          for (var j = 0; j < this.s.length; j++)
          {
            var u = this.s[j];
            u[i] = 0;
            t.push(u.slice());
            u[i] = 1;
            t.push(u);
          }
          this.s = t;
        }
      return this;
    }
    xor(a, b, c) {
      for (var j = 0; j < this.s.length; j++)
      {
        this.s[j][c] = this.s[j][a] ^ this.s[j][b];
      }
      return this;
    }
    nand(a, b, c) {
      for (var j = 0; j < this.s.length; j++)
      {
         var s = this.s[j][a] & this.s[j][b];
         if (s == 0)
         {
           this.s[j][c] = 1;
         }
         else
         {
           this.s[j][c] = 0;
         }
      }
      return this;
    }
    nor(a, b, c) {
      for (var j = 0; j < this.s.length; j++)
      {
         var s = this.s[j][a] | this.s[j][b];
         if (s == 0)
         {
           this.s[j][c] = 1;
         }
         else
         {
           this.s[j][c] = 0;
         }
      }
      return this;
    }
    not(a, b) {
      for (var j = 0; j < this.s.length; j++)
      {
         var s = !this.s[j][a];
         if (s == true)
         {
           this.s[j][b] = 1;
         }
         else
         {
           this.s[j][b] = 0;
         }
      }
      return this;
    }
    and(a, b, c) {
      for (var j = 0; j < this.s.length; j++)
      {
        this.s[j][c] = this.s[j][a] & this.s[j][b];
      }
      return this;
    }
    or(a, b, c) {
      for (var j = 0; j < this.s.length; j++)
      {
        this.s[j][c] = this.s[j][a] | this.s[j][b];
      }
      return this;
    }
    get() {
      return this.s;
    }
    table() {
    this.h += '<table>';
    for (var row = 0; row < this.s.length + 1; row++)
    {
        this.h +='<tr>';
        for (var col = 0; col < this.n + 1; col++)
        {
            if (col === 0 && row === 0)
            {
                this.h += '<th>&nbsp;<\/th>';
        }
            else if (col === 0 && row !== 0)
            {
                this.h += '<th>' + row + '<\/th>';
            }
            else if (row === 0)
            {
                this.h += '<th>' + (col - 1) + '<\/th>'; 
            }
            else
            {
                var v = this.s[row - 1][col- 1];
                this.h += '<td>' + v + '<\/td>';
            }
        }
        this.h += '<\/tr>';
    }
    this.h += '<\/table>';
    document.getElementById('outp').innerHTML = this.h;
    }
}


成果物

以上。