TL-2に少し命令を追加して、while命令を使えるようにしようと思います。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef unsigned char *String; // こう書くと String は unsigned char * の代用になる.
void loadText(int argc, const char **argv, String t, int siz) → TL-1と同じなので省略
int isNumber(unsigned char c) → TL-2と同じなので省略
int isAlphabet(unsigned char c) → TL-2と同じなので省略
int isSymbol(unsigned char c) → TL-2と同じなので省略
int isSymbol1(unsigned char c) → TL-2と同じなので省略
int lexer(String s, String b, String t[]) → TL-2と同じなので省略
int var[256]; // 変数.
String varName[256]; // 変数名.
unsigned char txt[10000]; // ソースコード.
int pc = 0; // プログラムカウンタ.
int varNumber(String t) → TL-2と同じなので省略
int getNumber(String t) → TL-2と同じなので省略
int main(int argc, const char **argv)
{
int i, pc0, wpc;
unsigned char buf[10000];
String t[1000]; // トークン.
loadText(argc, argv, txt, 10000);
i = lexer(txt, buf, t);
t[i + 0] = t[i + 1] = t[i + 2] = t[i + 3] = ""; // エラー表示用のために末尾にいくつか長さ0の文字列を登録しておく.
for (;;) {
pc0 = pc;
if (strcmp(t[pc], "") == 0) // ファイル終端.
exit(0);
if (isAlphabet(t[pc][0]) != 0 && strcmp(t[pc + 1], "=") == 0) { → TL-2と同じなので省略
} else if (strcmp(t[pc], "while") == 0 && strcmp(t[pc + 1], "(") == 0 && strcmp(t[pc + 3], "<") == 0 && strcmp(t[pc + 5], ")") == 0 && strcmp(t[pc + 6], "{") == 0) {
wpc = pc;
if (getNumber(t[pc + 2]) < getNumber(t[pc + 4])) {
pc += 7;
} else { // 条件不成立なので } の次まで読み飛ばす.
while (strcmp(t[pc], "}") != 0 && strcmp(t[pc], "") != 0)
pc++;
if (strcmp(t[pc], "}") == 0)
pc++;
}
continue;
} else if (strcmp(t[pc], "}") == 0) {
pc = wpc;
continue;
} else if (strcmp(t[pc], "time") == 0 && strcmp(t[pc + 1], ";") == 0) { // time.
printf("time=%.3f[sec]\n", clock() / (double) CLOCKS_PER_SEC);
} else if (strcmp(t[pc], "print") == 0 && strcmp(t[pc + 2], ";") == 0) { // print.
printf("%d\n", getNumber(t[pc + 1]));
} else
goto err;
while (strcmp(t[pc], ";") != 0)
pc++;
pc++;
}
err:
printf("syntax error : %s %s %s %s\n", t[pc0], t[pc0 + 1], t[pc0 + 2], t[pc0 + 3]);
exit(1);
}