stack machineを愛でる。 その5

#概要

stack machineが、好きだ。
俺仕様アセンブラの処理系書いてみた。

#サンプルコード

```

function _arrayBufferToBase64(buffer) {
  var binary = '';
  var bytes = new Uint8Array(buffer);
  var len = bytes.byteLength;
  for (var i = 0; i < len; i++)
  {
    binary += String.fromCharCode(bytes[i]);
  }
  return window.btoa(binary);
}
var swf21 = [
	0x46, 0x57, 0x53, 0x06, 0x2F, 0x02, 0x00, 0x00, 0x68, 0x00, 0x1F, 0x40, 0x00, 0x05, 0xDC,
	0x00, 0x00, 0x0C, 0x01, 0x00, 0x43, 0x02, 0xFF, 0xFF, 0xFF, 0x3F, 0x03, 0xBD, 0x01, 0x00,
	0x00, 0x88, 0xA9, 0x01, 0x03, 0x00, 0x78, 0x00, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00, 0x30, 0x31, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
	0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x00, 0x96, 0x04, 0x00, 0x08, 0x00, 0x08,
	0x01, 0x1D, 0x96, 0x04, 0x00, 0x08, 0x00, 0x08, 0x02, 0x1D, 0x00, 0x3F, 0x0C, 0x19, 0x00,
	0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x10, 0x54, 0x69, 0x6D, 0x65, 0x73, 0x20, 0x4E, 0x65,
	0x77, 0x20, 0x52, 0x6F, 0x6D, 0x61, 0x6E, 0x00, 0x00, 0x00, 0x02, 0x00, 0x7F, 0x09, 0x1F,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x6F, 0xF6, 0x1D, 0x0F, 0xFD, 0x85, 0x28, 0x00, 0x6D, 0x20,
	0x01, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x28, 0x00, 0x78, 0x00, 0x89, 0x06, 0x06, 0x01, 0x00, 0x02, 0x00, 0x12, 0x8C, 0x6E, 0x00,
	0x40, 0x00, 0x00, 0x00
];
function tob(url) {
  var bin = atob(url);
  var buffer = new Uint8Array(bin.length);
  for (var i = 0; i < bin.length; i++)
  {
    buffer[i] = bin.charCodeAt(i);
  }
  var blob = new Blob([buffer.buffer], {
    type: 'application/x-shockwave-flash'
  });
  var blob_url = window.URL.createObjectURL(blob);
  return blob_url;
}
function run() {
	var c1, 
		c2, 
		c3, 
		a, 
		i, 
		j;
	var pc = 0;
  var la = new Array(20);
	var r = new Array(400);
  var src = document.getElementById('src0').value;
	var m = src.split("\n");
	for (j = 0; j < m.length ; j++)
	{
    var cm = m[j].split("  ");
		if (cm[0] != "")
    {
      la[cm[0]] = pc;
    }
		switch (cm[1])
		{
		case "push":
			r[pc] = 0x96;
			pc++;
	  	c1 = cm[2].indexOf("\"") + 1;
			c2 = cm[2].indexOf("\"", 1) - c1;
			a = cm[2].substr(c1, c2);
			c3 = c2 + 2;
			r[pc] = c3;
			pc++;
			r[pc] = 0x00;
			pc++;
			r[pc] = 0x00;
			pc++;
			for (i = 0; i < c2; i++)
	    {
				r[pc] = a.charCodeAt(i);
				pc++;
			}
			r[pc] = 0x00;
			pc++;
		break;
		case "end":
			r[pc] = 0x00;
			pc++;
		break;
		case "+":
			r[pc] = 0x0A;
			pc++;
		break;
		case "-":
			r[pc] = 0x0B;
			pc++;
		break;
		case "*":
			r[pc] = 0x0C;
			pc++;
		break;
		case "/":
			r[pc] = 0x0D;
			pc++;
		break;
		case "=":
			r[pc] = 0x0E;
			pc++;
		break;
    case "&":
			r[pc] = 0x60;
			pc++;
		break;
		case "|":
			r[pc] = 0x61;
			pc++;
		break;
        case "^":
			r[pc] = 0x62;
			pc++;
		break;
		case "%":
			r[pc] = 0x3f;
			pc++;
		break;
    case ">":
			r[pc] = 0x0F;
			pc++;
		break;
		case "&&":
			r[pc] = 0x10;
			pc++;
		break;
		case "||":
			r[pc] = 0x11;
			pc++;
		break;
		case "!":
			r[pc] = 0x12;
			pc++;
		break;
		case "==":
			r[pc] = 0x13;
			pc++;
		break;
		case "pop":
			r[pc] = 0x17;
			pc++;
		break;
		case "get":
			r[pc] = 0x1C;
			pc++;
		break;
    case "++":
			r[pc] = 0x50;
			pc++;
		break;
		case "set":
			r[pc] = 0x1D;
			pc++;
		break;
		case "add":
			r[pc] = 0x21;
			pc++;
		break;
    case "if":
			r[pc] = 0x9D;
			pc++;
			r[pc] = 0x02;
			pc++;
			r[pc] = 0x00;
			pc++;
			if (la[cm[2]] > 1)
			{
				c3 = la[cm[2]] - pc + 254;
				r[pc] = c3;
				pc++;
				r[pc] = 0xFF;
				pc++;
			}
			else
			{
				r[pc] = cm[2];
				pc++;
				r[pc] = 0x00;
				pc++;
			}
		break;
		case "jp":
			r[pc] = 0x99;
			pc++;
			r[pc] = 0x02;
			pc++;
			r[pc] = 0x00;
			pc++;
			if (la[cm[2]] > 1)
			{
        c3 = la[cm[2]] - pc + 254;
				r[pc] = c3;
				pc++;
				r[pc] = 0xFF;
				pc++;
			}
			else
			{
				r[pc] = cm[2];
				pc++;
				r[pc] = 0x00;
				pc++;
			}
		break;
		}
	}		
  for (var l in la)
	{
    for (i = 0; i < pc; i++)
		{
			if (r[i] == l)
			{
        r[i] = la[l] - i - 2;
			}
		}
	}
	for (i = 0; i < pc; i++)
	{
	  swf21[i + 31] = r[i];
	}
  var b = _arrayBufferToBase64(swf21);
  var h = '<embed src ="';
  var l = '" type="application/x-shockwave-flash" width="300" height="300"></embed>';
  var swf = h + tob(b) + l;
  var out = document.getElementById('out');
  out.innerHTML = swf;
}




```

#成果物

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

以上。