川合のプログラミング言語自作のためのテキスト#0005

(10) TJ-02

void loadText(int argc, const char **argv, unsigned char *t, int siz) {

	FILE *fp;
	int i;
	if (argc < 2) {  // 引数が少ないのでエラー表示して終了.
		printf("usage>%s program-file\n", argv[0]);
		exit(1);
	}
	fp = fopen(argv[1], "rt"); // テキストモードでファイルを開く.
	if (fp == 0) {  // ファイルが開けなかった.
		printf("fopen error : %s\n", argv[1]);
		exit(1);
	}
	i = fread(t, 1, siz - 1, fp);
	fclose(fp);
	t[i] = 0; // 終端マークを書いておく.

}

int var[256]; // 変数. unsigned char txt[10000]; // ソースコード. int pc = 0; // プログラムカウンタ.

int getNumber() {

	unsigned char c = txt[pc], *p, *q;
	int i;
	if ('0' <= c && c <= '9') { // 数字
		p = &txt[pc];
		i = strtol(p, (char **) &q, 0);
		pc += q - p;
		return i;
	}
	pc++;
	return var[c]; // 1文字の変数.

}

int main(int argc, const char **argv) {

	int i, j, pc0, wpc = 0;
	loadText(argc, argv, txt, 10000);
	for (;;) {
		pc0 = pc;
		if (txt[pc] == 0)	// ファイル終端.
			exit(0);
		if (txt[pc] == '\n' || txt[pc] == ' ' || txt[pc] == '\t' || txt[pc] == ';') { // 空行など.
			pc++;
		} else if (txt[pc + 1] == '=') { // 2文字目が"=".
			i = txt[pc];	// 1文字の変数名.
			pc += 2;
			j = getNumber();
			if (txt[pc] == ';') {
				var[i] = j;
			} else if (txt[pc] == '+') {	// 加算.
				pc++;
				var[i] = j + getNumber();
			} else if (txt[pc] == '-') {	// 減算.
				pc++;
				var[i] = j - getNumber();
			} else
				goto err;
		} else if (txt[pc] == 'p' && txt[pc + 1] == 'r' && txt[pc + 5] == ' ') { // 最初の2文字しか調べてない(手抜き).
			pc += 6;
			printf("%d\n", getNumber());
		} else if (txt[pc] == 'w' && txt[pc + 1] == 'h' && txt[pc + 5] == ' ' && txt[pc + 7] == '<') { // 最初の2文字しか調べてない(手抜き).
			wpc = pc;
			i = var[txt[pc + 6]];	// 変数の値.
			pc += 8;
			j = getNumber();
			if (txt[pc] != '{')
				goto err;
			pc++;
			if (i >= j) {	// 条件不成立なので } までを読み飛ばす.
				while (txt[pc] != 0 && txt[pc] != '}')
					pc++;
				if (txt[pc] == '}')
					pc++;
			}
		} else if (txt[pc] == '}') {
			pc = wpc;
		} else if (txt[pc] == 't' && txt[pc + 1] == 'i') { // 最初の2文字しか調べてない(手抜き).
			pc += 4;
			printf("time=%.3f[sec]\n", clock() / (double) CLOCKS_PER_SEC);
		} else
			goto err;
	}

err:

	printf("syntax error : %.10s\n", &txt[pc0]);
	exit(1);

}

次回に続く

こめんと欄


コメントお名前NameLink

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS