高位合成言語アセンブラを作る。 その25

# 概要
高位合成言語アセンブラを作る。
練習問題、やってみる。

# 練習問題
3 の正の倍数検出回路を作れ。

# 回路図を描く

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/bc49a1a1-67a6-b8dd-1a78-287a1aef6ffa.png)

# ソース

このソースでsimcirjsで描く。

```
make 10
in 0 2
wire 3 8
not 2 3
and 0 1 4
and 1 2 5
not 0 6
and 3 4 7
and 5 6 8
or 7 8 9
out 9 9
```

# サンプルコード

```

var c0;
function run() {
  var src = document.getElementById("src");
	var str = src.value;
  var codes = str.split("\n");  
  var len = codes.length;
  var i;
  for (i = 0; i < len; i++)
  {
    var code = codes[i].split(" ");
    switch (code[0])
    {
    case "":
    break;
    case "out":
			var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      c0.out(a, b);
    break;
    case "wire":
    break;
    case "make":
      var a = parseInt(code[1]);
      c0 = new J(a);
    break;
    case "in":
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      c0.in(a, b);
    break;
    case "nand":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.nand(a, b, c);
    break;
    case "xor":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.xor(a, b, c);
    break;
    case "and":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.and(a, b, c);
    break;
    case "or":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.or(a, b, c);
    break;
    case "nor":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.nor(a, b, c);
    break;
    case "not":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      c0.not(a, b);
    break;
    default:
      alert(code[0]);
    break;
    }
  }
  c0.disp('koko');
}
class J {
	constructor(n) {
	  var str = '{"width": 600,	"height": 600, "showToolbox": false, "toolbox": [], "devices": [], "connectors": []}';
    this.json = JSON.parse(str);
    this.dev = [];
    this.con = [];
    this.id = 0;
    this.y0 = 40;
    this.y1 = 40;
    this.y2 = 40;
	  this.n = n;
		this.s = [];
		return this;
	}
	in(a, b) {
	  b += 1;
	  for(var i = a; i < b; i++)
	  {
	    var d = {
	      "type": "In",
	      "id": "dev" + this.id,
	      "x": 40,
	      "y": this.y0,
	      "label": "In"
	   };
	   this.json.devices.push(d);
	   this.s[i] = this.id;
	   this.y0 += 50;
	   this.id += 1;
	  }
	  return this;
	}
	wire(a, b) {
	  b += 1;
	  for(var i = a; i < b; i++)
	  {
	    this.s[i] = i;
	  }
	  return this;
	}
	out(a, b) {
	  b += 1;
	  for(var i = a; i < b; i++)
	  {
	    var d = {
	      "type": "Out",
	      "id": "dev" + this.id,
	      "x": 400,
	      "y": this.y2,
	      "label": "Out"
	    };
	    this.json.devices.push(d);
	    var c0 = {
	      "from": "dev" + this.id + ".in0",
	      "to": "dev" + this.s[i] + ".out0"
	    };
	    this.json.connectors.push(c0);
	    this.y2 += 50;
	    this.id += 1;
	  }
	  return this;
	}
	not(a, c) {
    var d = {
	    "type": "NOT",
	    "id": "dev" + this.id,
	    "x": 120,
	    "y": this.y1,
	    "label": "NOT"
	  };
	  this.json.devices.push(d);
	  var c0 = {
	    "from": "dev" + this.id + ".in0",
	    "to": "dev" + this.s[a] + ".out0"
	  };
	  this.s[c] = this.id;
	  this.json.connectors.push(c0);
	  this.y1 += 40;
	  this.id += 1;
	  return this;
	}
	xor(a, b, c) {
    var d = {
	    "type": "XOR",
	    "id": "dev" + this.id,
	    "x": 150,
	    "y": this.y1,
	    "label": "XOR"
	  };
	  this.json.devices.push(d);
	  var c0 = {
	    "from": "dev" + this.id + ".in0",
	    "to": "dev" + this.s[a] + ".out0"
	  };
	  var c1 = {
	    "from": "dev" + this.id + ".in1",
	    "to": "dev" + this.s[b] + ".out0"
	  }
	  this.s[c] = this.id;
	  this.json.connectors.push(c0);
	  this.json.connectors.push(c1);
	  this.y1 += 40;
	  this.id += 1;
	  return this;
	}
	nand(a, b, c) {
    var d = {
	    "type": "NAND",
	    "id": "dev" + this.id,
	    "x": 200,
	    "y": this.y1,
	    "label": "NAND"
	  };
	  this.json.devices.push(d);
	  var c0 = {
	    "from": "dev" + this.id + ".in0",
	    "to": "dev" + this.s[a] + ".out0"
	  };
	  var c1 = {
	    "from": "dev" + this.id + ".in1",
	    "to": "dev" + this.s[b] + ".out0"
	  }
	  this.s[c] = this.id;
	  this.json.connectors.push(c0);
	  this.json.connectors.push(c1);
	  this.y1 += 50;
	  this.id += 1;
	  return this;
	}
	and(a, b, c) {
    var d = {
	    "type": "AND",
	    "id": "dev" + this.id,
	    "x": 210,
	    "y": this.y1,
	    "label": "AND"
	  };
	  this.json.devices.push(d);
	  var c0 = {
	    "from": "dev" + this.id + ".in0",
	    "to": "dev" + this.s[a] + ".out0"
	  };
	  var c1 = {
	    "from": "dev" + this.id + ".in1",
	    "to": "dev" + this.s[b] + ".out0"
	  }
	  this.s[c] = this.id;
	  this.json.connectors.push(c0);
	  this.json.connectors.push(c1);
	  this.y1 += 50;
	  this.id += 1;
	  return this;
	}
	nor(a, b, c) {
    var d = {
	    "type": "NOR",
	    "id": "dev" + this.id,
	    "x": 200,
	    "y": this.y1,
	    "label": "NOR"
	  };
	  this.json.devices.push(d);
	  var c0 = {
	    "from": "dev" + this.id + ".in0",
	    "to": "dev" + this.s[a] + ".out0"
	  };
	  var c1 = {
	    "from": "dev" + this.id + ".in1",
	    "to": "dev" + this.s[b] + ".out0"
	  }
	  this.s[c] = this.id;
	  this.json.connectors.push(c0);
	  this.json.connectors.push(c1);
	  this.y1 += 50;
	  this.id += 1;
	  return this;
	}
	or(a, b, c) {
    var d = {
	    "type": "OR",
	    "id": "dev" + this.id,
	    "x": 300,
	    "y": this.y1,
	    "label": "OR"
	  };
	  this.json.devices.push(d);
	  var c0 = {
	    "from": "dev" + this.id + ".in0",
	    "to": "dev" + this.s[a] + ".out0"
	  };
	  var c1 = {
	    "from": "dev" + this.id + ".in1",
	    "to": "dev" + this.s[b] + ".out0"
	  }
	  this.s[c] = this.id;
	  this.json.connectors.push(c0);
	  this.json.connectors.push(c1);
	  this.y1 += 40;
	  this.id += 1;
	  return this;
	}
	disp(s) {
	  var koko = document.getElementById(s);
	  var str = JSON.stringify(this.json);  
    koko.value = str;
	}
}


```

# 成果物

https://embed.plnkr.co/plunk/QBs8bLvw35z9ou5s


以上。