cscの作法 その100

# 概要
cscの作法、調べてみた。
dll作ってみた。

# コンパイル手順

```
>csc -target:library gengo.cs

```

# サンプルコード

```
using System;
using System.Windows.Forms;
using System.Drawing;

namespace Ore
{
public class Gengo {
	private byte[] ram = new byte[256];
	private byte Rmd = 233;
	private byte Bnd = 234;
	private byte Pcc = 235;
	private byte Lbf = 237;
	private byte Ptr;
	private byte Cur;
	private byte c;
	private byte adr;
	private byte adr2;
	private byte val;
	public string res = "";
	public Gengo() {
	}
	private byte READB(byte adr) {
		byte v = ram[adr];
		return v;
	}
	private void WRITEB(byte adr, byte data) {
		ram[adr] = data;
	}
	private void putchar(byte b) {
		char c = Convert.ToChar(b);
		res += c;
	}
	private void put(byte b) {
		res += b + " ";
	}
	private void crlf() {
		putchar(Convert.ToByte('\r'));
		putchar(Convert.ToByte('\n'));
	}
	private byte putl(byte pt, byte d) {
		while (READB(pt) != d)
		{
			putchar(READB(pt++));
		}
		return pt;
	}
	private void putlp(byte d) {
		while (READB(Ptr) != d)
		{
			putchar(READB(Ptr++));
		}
	}
	private void term() {
		byte cc;
		cc = READB(Ptr++);
		byte val2 = val;
		factr();
		if (cc == Convert.ToByte('*'))
		{
			val = Convert.ToByte(val2 * val);
		}
		else if (cc == Convert.ToByte('+'))
		{
			val = Convert.ToByte(val2 + val);
		}
		else if (cc == Convert.ToByte('-'))
		{
			val = Convert.ToByte(val2 - val);
		}
		else if (cc == Convert.ToByte('/'))
		{
			WRITEB(Rmd, Convert.ToByte(val2 % val));
			val = Convert.ToByte(val2 / val);
		}
		else if (cc == Convert.ToByte('='))
		{
			if (val2 == val)
				val = 1;
			else
				val = 0;
		}
		else if (cc == Convert.ToByte('^'))
		{
			val = Convert.ToByte(val2 ^ val);
		}
		else if (cc == Convert.ToByte('&'))
		{
			val = Convert.ToByte(val2 & val);
		}
		else if (cc == Convert.ToByte('|'))
		{
			val = Convert.ToByte(val2 | val);
		}
		else if (cc == Convert.ToByte('>'))
		{
			if (val2 >= val)
				val = 1;
			else
				val = 0;
		}
		else
		{
			if (val2 < val)
				val = 1;
			else
				val = 0;
		}
	}
	private void factr() {
		byte cc;
		if (READB(Ptr) == Convert.ToByte('\0'))
		{
			val = 0;
			return;
		}
		if (getnm2()) return;
		cc = READB(Ptr++);
		if (cc == Convert.ToByte('$'))
		{
			Random r = new Random();
			val = Convert.ToByte(r.Next(10));
		}
		else if (cc == Convert.ToByte(':'))
		{
			val = Convert.ToByte('\t');
		}
		else if (cc == Convert.ToByte(';'))
		{
			val = Convert.ToByte('\n');
		}
		else
		{
			Ptr--;
			getvr2();
			val = READB(adr2);
			Ptr++;
		}
	}
	private void expr() {
		byte cc;
		factr();
		while ((cc = READB(Ptr)) != Convert.ToByte('\0'))
		{
			term();
		}
		Ptr++;
	}
	private void getvr2() {
		byte cc = READB(Ptr);
		if (cc == Convert.ToByte('a'))
			adr2 = 230;
		else if (cc == Convert.ToByte('#'))
			adr2 = Pcc;
		else if (cc == Convert.ToByte('b'))
			adr2 = 231;
		else if (cc == Convert.ToByte('c'))
			adr2 = 232;
		else if (cc == Convert.ToByte('%'))
			adr2 = Rmd;
		else
			adr2 = 230;
	}
	private void getvr() {
		c = READB(Ptr);
		if (c == Convert.ToByte('a'))
			adr = 230;
		else if (c == Convert.ToByte('#'))
			adr = Pcc;
		else if (c == Convert.ToByte('b'))
			adr = 231;
		else if (c == Convert.ToByte('c'))
			adr = 232;
		else
			adr = 230;
	}
	private void ordr() {
		getvr();
		Ptr++;
		Ptr++;
		if (READB(Ptr) == Convert.ToByte('"'))
		{
			Ptr++;
			putlp(Convert.ToByte('"'));
		}
		else
		{
			expr();
			if (c == Convert.ToByte('$'))
			{
			}
			else if (c == Convert.ToByte('?'))
			{
				put(val);
			}
			else
			{
				WRITEB(adr, val);
			}
		}
	}
	private byte nxtln(byte pt) {
		for (pt += 1; READB(pt++) != Convert.ToByte('\0'););
		return pt;
	}
	private bool fndln() {
		for (Cur = 0; Cur != READB(Bnd); Cur = nxtln(Cur))
		{
			if (READB(Cur) == READB(Pcc))
				return true;
		}
		return false;
	}
	private bool nxline() {
		for (Cur = 0; Cur != READB(Bnd); Cur = nxtln(Cur))
		{
			if (READB(Cur) > READB(Pcc))
				return true;
		}
		return false;
	}
	private bool num() {
		if (48 <= (READB(Ptr)) && (READB(Ptr)) <= 57)
			return true;
		else
			return false;
	}
	private bool getnm2() {
		byte ch;
		if (!num())
			return false;
		byte n = 0;
		do
		{
			n *= 10;
			ch = READB(Ptr++);
			n += Convert.ToByte(ch - 48);
		} while (num());
		val = n;
		return true;
	}
	public string run(string str) {
		WRITEB(Bnd, 0);
		byte i;
		byte j;
		string[] lines = str.Split(new string[] {
			"\r\n"
		}, StringSplitOptions.None);
		for (i = 0; i < lines.Length; i++)
		{
			if (lines[i].Length < 1)
				break;
			Ptr = Lbf;
			for (j = 0; j < lines[i].Length; j++)
			{
				WRITEB(Ptr++, Convert.ToByte(lines[i][j]));
			}
			WRITEB(Ptr++, Convert.ToByte('\0'));
			byte ima = 0;
			Ptr = Lbf;
			if (!getnm2())
			{
				Ptr = Lbf;
				WRITEB(Pcc, 0);
				for ( ; ; )
				{
					ima = READB(Pcc);
					ordr();
					if (READB(Pcc) == 0 && ima == 0)
					{
						break;
					}
					else
					{
						if (READB(Pcc) == 0)
							WRITEB(Pcc, ima);
						if (READB(Pcc) == 1)
							ima = 1;
						if (READB(Pcc) == ima)
						{
							if (nxline())
							{
								WRITEB(Pcc, READB(Cur));
								Ptr = Convert.ToByte(Cur + 2);
							}
							else
							{
								break;
							}
						}
						else
						{
							if (fndln())
							{
								WRITEB(Pcc, READB(Cur));
								Ptr = Convert.ToByte(Cur + 2);
							}
							else
							{
								break;
							}
						}
					}
				}
			}
			else
			{
				if (val == 0)
				{
					byte pt;
					for (pt = 0; pt < READB(Bnd); pt++)
					{
						put(READB(pt));
						pt += 1;
						pt = putl(pt, Convert.ToByte('\0'));
						crlf();
					}
				}
				else
				{
					byte src,
						dst,
						m,
						h;
					byte f = 0;
					WRITEB(Pcc, val);
					if (fndln())
					{
						src = nxtln(Cur);
						for (dst = Cur; src != READB(Bnd); dst++, src++)
						{
							WRITEB(dst, READB(src));
						}
						WRITEB(Bnd, dst);
					}
					if (nxline())
					{
						f = 1;
					}
					src = READB(Bnd);
					for (m = 2, h = Ptr; READB(h) != Convert.ToByte('\0'); m++)
					{
						h++;
					}
					if (m > 2)
					{
						WRITEB(Bnd, Convert.ToByte(src + m));
						if (f == 1)
						{
							Cur--;
							for (dst = READB(Bnd); src != Cur; dst--, src--)
							{
								WRITEB(dst, READB(src));
							}
							src++;
						}
						WRITEB(src, val);
						src += 1;
						byte k;
						for (k = 1; k < m; k++)
						{
							WRITEB(src++, READB(Ptr++));
						}
					}
				}
			}
		}
		return res;
	}
}
}





```

以上。