インタープリタを作る その4

#概要

インタープリタを作ってみた。
俺言語、インタープリタを作って見た。

#出来ること。

fizzbuzz,zundoko,動く。


```
10 a=0
20 a=a+1
30 #=a>100*255
40 b=a%15
50 #=b=0*120
60 b=a%5
70 #=b=0*140
80 b=a%3
90 #=b=0*160
100 ?=a
110 #=20
120 ?="fizzbuzz "
130 #=20
140 ?="buzz "
150 #=20
160 ?="fizz "
170 #=20
#=1
```

#文法

行番号、スペース、ステートメントの順
代入文しか無い。
行番号は、１から２５５
メモリは５１２バイト
ラインエディタあり。
30で、行３０が消える
\#=0でソース表示
\#=1で実行
行番号なしで、ダイレクト実行
?="ok"で、OK表示
\#=120で、１２０行へジャンプ
\#=a<10*120で、if a<10 goto 120
a=2+3*5で、(2+3)*5で２５が変数aへ代入。
演算子は、+,-,/,%の4個
変数は、a,b,cの三個

#サンプルコード


```
var koko = document.getElementById('koko');
var MEMSIZE = 512;
var ram = Array(MEMSIZE);
var Pcc = 257;
var Rmd = 258;
var Bnd = 259;
var Lbf = 300;
var Obj = 0;
var ptr = Lbf;
var cur;
var c;
var adr;
var adr2;
var val;
var val2;
var i;

function READB(adr) {
	var v;
	v = ram[adr];
	return v;
}
function WRITEB(adr, data) {
	ram[adr] = data;
}
function READW(adr) {
	var v;
	v = (ram[adr]).charCodeAt(0);
	return v;
}
function WRITEW(adr, data) {
	ram[adr] = String.fromCharCode(data % 256);
}
function putchr(c) {
	var out = document.getElementById('out');
	out.value += c;
}
function putstr(str) {
	var i;
	for (i = 0; i < str.length; i++)
	{
		putchr(str[i]);
	}
}
function crlf() {
	putchr('\r');
}
function putl(pt, d) {
	while (READB(pt) != d)
	{
		putchr(READB(pt++));
	}
	return pt;
}
function putlp(d) {
	while (READB(ptr) != d)
	{
		putchr(READB(ptr++));
	}
}
function term() {
	var cc;
	cc = READB(ptr++);
	var val2 = val;
	factr();
	if (cc == '*')
	{
		val = val2 * val;
	}
	else if (cc == '+')
	{
		val = val2 + val;
	}
	else if (cc == '-')
	{
		val = val2 - val;
	}
	else if (cc == '/')
	{
		WRITEW(Rmd, val2 % val);
		val = Math.floor(val2 / val);
	}
	else if (cc == '=')
	{
		if (val2 == val) val = 1;
		else val = 0;
	}
	else if (cc == '^')
	{
		val = val2 ^ val;
	}
	else if (cc == '&')
	{
		val = val2 & val;
	}
	else if (cc == '|')
	{
		val = val2 | val;
	}
	else if (cc == '>')
	{
		if (val2 >= val) val = 1;
		else val = 0;
	}
	else
	{
		if (val2 < val) val = 1;
		else val = 0;
	}
	return;
}
function factr() {
	var cc;
	if (READB(ptr) == '\0')
	{
		val = 0;
		return;
	}
	if (getnm2()) return;
	cc = READB(ptr++);
 	if (cc == '?')
	{
	}
	else if (cc == '$')
	{
		val = Math.floor(Math.random() * 10);
	}
	else if (cc == ':')
	{
		val = '\t';
	}
	else if (cc == ';')
	{
		val = "\n";
	}
	else if (cc == '(')
	{
	}
	else
	{
		ptr--;
		getvr2();
		val = READW(adr2);
		ptr++;
	}
	return;
}
function expr() {
	var cc;
	factr();
	while ((cc = READB(ptr)) != '\0' && cc != ')')
	{
		term();
	}
	ptr++;
	return;
}
function getvr2() {
	var cc = READB(ptr);
	if (cc == 'a')	adr2 = 260;
	else if (cc == '#')	adr2 = Pcc;
	else if (cc == 'b')	adr2 = 261;
	else if (cc == 'c')	adr2 = 262;
	else if (cc == '%')	adr2 = Rmd;
	else adr2 = 260;
}
function getvr() {
	c = READB(ptr);
	if (c == 'a')	adr = 260;
	else if (c == '#') adr = Pcc;
	else if (c == 'b') adr = 261;
	else if (c == 'c') adr = 262;
	else adr = 260;
}
function ordr() {
	getvr();
 	ptr++;
	ptr++;
	if (READB(ptr) == '"')
	{
		ptr++;
		putlp('"');
	}
	else
	{
		expr();
		if (c == '$')
		{
		}
		else if (c == '?')
		{
			putstr((val).toString());
		}
		else
		{
	   	WRITEW(adr, val);
		}
	}
	return;
}
function nxtln(pt) {
	for (pt += 1; READB(pt++) != '\0';);
	return pt;
}
function fndln() {
	for (cur = Obj; cur != READW(Bnd); cur = nxtln(cur))
	{
		if (READW(cur) == READW(Pcc)) return true;
	}
	return false;
}
function nxline() {
	for (cur = Obj; cur != READW(Bnd); cur = nxtln(cur))
	{
		if (READW(cur) > READW(Pcc)) return true;
	}
	return false;
}
function num() {
	if (48 <= (READB(ptr)).charCodeAt(0) && (READB(ptr)).charCodeAt(0) <= 57) return true;
	else return false;
}
function getnm2() {
	var ch;
	if (!num()) return false;
	var n = 0;
	do
	{
		n *= 10;
		ch = READB(ptr++);
		n += ch.charCodeAt(0) - 48;
	} while (num());
	val = n;
	return true;
}
function run() {
	var s = "";
	var ima = 0;
	ptr = Lbf;
	if (!getnm2())
	{
		ptr = Lbf;
		WRITEW(Pcc, 0);
		for ( ; ; )
		{
			ima = READW(Pcc);
			ordr();
			if (READW(Pcc) == 0 && ima == 0)
			{
				break;
			}
			else
			{
				if (READW(Pcc) == 0) WRITEW(Pcc, ima);
				if (READW(Pcc) == 1) ima = 1;
				if (READW(Pcc) == ima)
				{
					if (nxline())
					{
						WRITEW(Pcc, READW(cur));
						ptr = cur + 2;
					}
					else
					{
						break;
					}
				}
				else
				{
					if (fndln())
					{
						WRITEW(Pcc, READW(cur));
						ptr = cur + 2;
					}
					else
					{
						break;
					}
				}
			}
		}
	}
	else
	{
 		if (val == 0)
		{
			var pt;
			for (pt = Obj; pt < READW(Bnd); pt++)
			{
				putstr((READW(pt)).toString());
				pt += 1;
				pt = putl(pt, '\0');
				crlf();
			}
 		}
		else
		{
			var src,
				dst,
				m,
				h;
			var f = 0;
			WRITEW(Pcc, val);
 			if (fndln())
			{
		 		src = nxtln(cur);
				for (dst = cur; src != READW(Bnd); dst++, src++)
				{
					WRITEB(dst, READB(src));
				}
				WRITEW(Bnd, dst);
			}
			if (nxline())
			{
				f = 1;
			}
			src = READW(Bnd);
			for (m = 2, h = ptr; READB(h) != '\0'; m++)
			{
				h++;
			}
			if (m > 2)
			{
				WRITEW(Bnd, src + m);
				if (f == 1)
				{
					cur--;
					for (dst = READW(Bnd); src != cur; dst--, src--)
					{
						WRITEB(dst, READB(src));
					}
					src++;
				}
				WRITEW(src, val);
				src += 1;
				for (i = 1; i < m; i++)
				{
					WRITEB(src++, READB(ptr++));
				}
			}
		}
	}
	ptr = Lbf;
	putstr("\n>");
}
WRITEW(Bnd, Obj);
function fire() {
	var i;
	var str = koko.value + "\n";
	for (i = 0; i < str.length; i++)
	{
		var ch = str[i];
		putchr(ch);
		if (ch == '\n')
		{
			WRITEB(ptr++, '\0');
			run();
		}
		else
		{
			WRITEB(ptr++, ch);
		}
	}
	koko.value = "";
}




```

#成果物

https://embed.plnkr.co/plunk/9kOQm3dZQVz2Q5vu

以上。