From 03ca6e92ac39846779eab3e570d229ff609816ec Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 1 Apr 2010 20:22:15 +0000 Subject: add Halt command, cleanup --- progs/3state-bb.tm | 9 +++++++++ progs/4state-bb.tm | 12 ++++++++++++ tm.c | 49 ++++++++++++++++++++++++++++++------------------- 3 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 progs/3state-bb.tm create mode 100644 progs/4state-bb.tm diff --git a/progs/3state-bb.tm b/progs/3state-bb.tm new file mode 100644 index 0000000..a332ee7 --- /dev/null +++ b/progs/3state-bb.tm @@ -0,0 +1,9 @@ +001R1 +011H0 +0_1R1 +100R2 +111R1 +1_0R2 +201L2 +211L0 +2_1L2 diff --git a/progs/4state-bb.tm b/progs/4state-bb.tm new file mode 100644 index 0000000..8b07ad9 --- /dev/null +++ b/progs/4state-bb.tm @@ -0,0 +1,12 @@ +001R1 +011L1 +0_1R1 +101L0 +110L2 +1_1L0 +201H2 +211L3 +2_1H2 +301R3 +310R0 +3_1R3 diff --git a/tm.c b/tm.c index 569ffde..dce7ad2 100644 --- a/tm.c +++ b/tm.c @@ -62,9 +62,9 @@ typedef struct cell Cell; typedef struct table Table; typedef struct tulpe Tulpe; -char blank = ' '; -char init = 'A'; -char halt = 'H'; +char blank = '_'; +char haltst = '\0'; +char initst = '\0'; int width = 21; long counter = 0; @@ -115,7 +115,8 @@ alloctape(char *init) } while (s-- != init) movehead(t, 'L'); - } + } else + t->head->symb = blank; return t; } @@ -168,9 +169,9 @@ alloctable(char *fname) memcpy(tab->initstring, &buf[1], len); break; case '%': - init = buf[1]; + initst = buf[1]; blank = buf[2]; - halt = buf[3]; + haltst = buf[3]; break; default: if (len < 5) @@ -192,7 +193,8 @@ alloctable(char *fname) fclose(fd); tab->tulpe = t; - state = t->curst; + + state = initst ? initst : t->curst; return tab; } @@ -264,6 +266,11 @@ movehead(Tape *tape, char move) case 'N': case 'n': case ' ': + break; + case 'H': + case 'h': + dflag = 1; + break; default: break; } @@ -275,12 +282,12 @@ movehead(Tape *tape, char move) int execute(Tape *tape, Table *tab, int u) { - Tulpe *t; + Tulpe *t = tab->tulpe; if (!nflag && tab->initstring) - printtape(tape, NULL, 0); + printtape(tape, t, 0); - while (state != halt && !dflag) { + while (!dflag && state != haltst) { t = findtulpe(tab, tape->head); /* read & find */ if (!t) { putchar('\n'); @@ -306,7 +313,7 @@ execute(Tape *tape, Table *tab, int u) } if (!nflag) - printtape(tape, NULL, 1); + printtape(tape, t, 1); return 0; } @@ -316,12 +323,12 @@ printtape(Tape *t, Tulpe *tulpe, int nl) { Cell *c; - printf("%c %c %c %c %c %s", - tulpe ? tulpe->curst : ' ', - tulpe ? tulpe->ssymb : ' ', - tulpe ? tulpe->psymb : ' ', - tulpe ? tulpe->move : ' ', - tulpe ? tulpe->newst : ' ', + printf("(%c,%c) -> (%c,%c) %c %s", + tulpe->curst, + tulpe->ssymb, + tulpe->newst, + tulpe->psymb, + tulpe->move, move[movetape]); movetape = NOMOVE; putchar('\t'); @@ -344,7 +351,7 @@ main(int argc, char **argv) int c; char *columns; - while ((c = getopt(argc, argv, "d:ns")) != -1) + while ((c = getopt(argc, argv, "d:nsb:")) != -1) switch (c) { case 'd': /* execution velocity */ delay = atoi(optarg); @@ -357,6 +364,9 @@ main(int argc, char **argv) case 'n': /* be quite */ nflag = 1; break; + case 'b': + blank = *optarg; + break; default: usage(); /* NOTREACHED */ @@ -400,7 +410,8 @@ usage(void) { extern char *__progname; - (void) fprintf(stderr, "usage: %s [-d ms] [-ns] \n", __progname); + (void) fprintf(stderr, "usage: %s [-b blank] [-d ms] [-ns] \n", + __progname); exit(1); } -- cgit v1.2.3