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

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

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

# 真理値表を書く。

sympyで論理式を得たので、インタープリターのソースを書く

```

                9
      7                  8
  3      4           5      6
  2    1   0       2   1    0
(~c & (b & a)) | ((c & b) & ~a)

```

# インタープリター

```
make 10
in 0 2
wire 3 8
out 9 9
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

```

# 実行結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/6b2199ac-edc0-2f71-0f18-fd08c13d3800.png)

# サンプルコード

```

var src = document.getElementById("src");
var out = document.getElementById("out");
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('out').innerHTML = this.h;
	}
}

function run() {
  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":
    break;
    case "wire":
    break;
    case "make":
      var a = parseInt(code[1]);
      c0 = new C(a);
    break;
    case "in":
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      c0.init(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.table();
}




```

# 成果物

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

以上。