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

# 概要
高位合成言語アセンブラを作る。
論理式から、真理値表を作る。

# 投入する論理式。

```
( ( ( a & b ) | ( a & c ) ) | ( b & c ) )
( ( ( ( a & b ) & c ) | ( ( a & ( ~b ) ) & ( ~c ) ) ) | ( ( ( ( ~a ) & b ) & ( ~c ) ) | ( ( ( ~a ) & ( ~b ) ) & c ) ) )

```

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/f0eaa75a-8170-b131-f9fb-c526bfc5f420.png)

# 生成した論理アセンブラ

```
make 25
wire 3 23
out 7 24
in 0 2
and 1 2 3
and 0 2 4
and 0 1 5
or 5 4 6
or 6 3 7
not 1 8
not 0 9
and 9 8 10
and 10 2 11
not 2 12
not 0 13
and 13 1 14
and 14 12 15
or 15 11 16
not 2 17
not 1 18
and 0 18 19
and 19 17 20
and 0 1 21
and 21 2 22
or 22 20 23
or 23 16 24

```


# サンプルコード

```

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('out2').innerHTML = this.h;
	}
}

function run2() {
  var str = out.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();
}




function run() {
  var w = "";
  var in0 = 0;
  var in1 = 2;
  var ou0 = 0;
  var ou1 = 0;
  var z = 3;
  var v = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
  var str = src.value;
  var codes = str.split("\n");  
  var len = codes.length;
  var i;
  w += "in " + in0 + " " + in1 + "\n";
  for (i = 0; i < len; i++)
  {
    while (codes[i].length > 1) 
    {
      var o = codes[i].lastIndexOf("(");
      var p = codes[i].indexOf(")", o);
      var x = 0;
      var y = "";
      code = codes[i].substring(o + 2, p - 1);
      //alert(code);
      var co = code.split(" ");
      if (co.length == 1)
      {
        switch (co[0])
        {
        case "":
        break;
        case "~a":
          w += "not 0 " + z + "\n";
          z++;
        break;
        case "~b":
          w += "not 1 " + z + "\n";
          z++;
        break;
        case "~c":
          w += "not 2 " + z + "\n";
          z++;
        break;
        default:
          alert(co[0]);
        break;
        }
      }
      if (co.length == 3)
      {
        switch (co[0])
        {
        case "":
        break;
        case "a":
          x = 0;
        break;
        case "b":
          x = 1;
        break;
        case "c":
          x = 2;
        break;
        case "d":
          x = 3;
        break;
        case "e":
          x = 4;
        break;
        case "f":
          x = 5;
        break;
        case "g":
          x = 6;
        break;
        case "h":
          x = 7;
        break;
        case "i":
          x = 8;
        break;
        case "j":
          x = 9;
        break;
        case "k":
          x = 10;
        break;
        case "l":
          x = 11;
        break;
        case "m":
          x = 12;
        break;
        case "n":
          x = 13;
        break;
        case "o":
          x = 14;
        break;
        case "p":
          x = 15;
        break;
        case "q":
          x = 16;
        break;
        case "r":
          x = 17;
        break;
        case "s":
          x = 18;
        break;
        case "t":
          x = 19;
        break;
        case "u":
          x = 20;
        break;
        case "v":
          x = 21;
        break;
        case "w":
          x = 22;
        break;
        case "x":
          x = 23;
        break;
        case "y":
          x = 24;
        break;
        case "z":
          x = 25;
        break;
        default:
          alert(co[0]);
        break;
        }
        switch (co[1])
        {
        case "":
        break;
        case "&":
          y = "and ";
        break;
        case "|":
          y = "or ";
        break;
        default:
          alert(co[1]);
        break;
        }
        switch (co[2])
        {
        case "":
        break;
        case "a":
          w += y + x + " 0 " + z + "\n";
          z++;
        break;
        case "b":
          w += y + x + " 1 " + z + "\n";
          z++;
        break;
        case "c":
          w += y + x + " 2 " + z + "\n";
          z++;
        break;
        case "d":
          w += y + x + " 3 " + z + "\n";
          z++;
        break;
        case "e":
          w += y + x + " 4 " + z + "\n";
          z++;
        break;
        case "f":
          w += y + x + " 5 " + z + "\n";
          z++;
        break;
        case "g":
          w += y + x + " 6 " + z + "\n";
          z++;
        break;
        case "h":
          w += y + x + " 7 " + z + "\n";
          z++;
        break;
        case "i":
          w += y + x + " 8 " + z + "\n";
          z++;
        break;
        case "j":
          w += y + x + " 9 " + z + "\n";
          z++;
        break;
        case "k":
          w += y + x + " 10 " + z + "\n";
          z++;
        break;
        case "l":
          w += y + x + " 11 " + z + "\n";
          z++;
        break;
        case "m":
          w += y + x + " 12 " + z + "\n";
          z++;
        break;
        case "n":
          w += y + x + " 13 " + z + "\n";
          z++;
        break;
        case "o":
          w += y + x + " 14 " + z + "\n";
          z++;
        break;
        case "p":
          w += y + x + " 15 " + z + "\n";
          z++;
        break;
        case "q":
          w += y + x + " 16 " + z + "\n";
          z++;
        break;
        case "r":
          w += y + x + " 17 " + z + "\n";
          z++;
        break;
        case "s":
          w += y + x + " 18 " + z + "\n";
          z++;
        break;
        case "t":
          w += y + x + " 19 " + z + "\n";
          z++;
        break;
        case "u":
          w += y + x + " 20 " + z + "\n";
          z++;
        break;
        case "v":
          w += y + x + " 21 " + z + "\n";
          z++;
        break;
        case "w":
          w += y + x + " 22 " + z + "\n";
          z++;
        break;
        case "x":
          w += y + x + " 23 " + z + "\n";
          z++;
        break;
        case "y":
          w += y + x + " 24 " + z + "\n";
          z++;
        break;
        case "z":
          w += y + x + " 25 " + z + "\n";
          z++;
        break;
        default:
          alert(co[2]);
        break;
        }
      }
      codes[i] = codes[i].substring(0, o) + v[z - 1] + codes[i].substring(p + 1);
    }
    if (i == 0) ou0 = z - 1;
    if (i == 1) ou1 = z - 1;

  }
  out.value = "make " + z + "\n";
  out.value += "wire 3 " + (z - 2) + "\n";
  out.value += "out " + ou0 + " " + ou1 + "\n";  
  out.value += w;
}




```

# 成果物


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

以上。