summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2010-04-01 20:22:15 +0000
committerDimitri Sokolyuk <demon@dim13.org>2010-04-01 20:22:15 +0000
commit03ca6e92ac39846779eab3e570d229ff609816ec (patch)
tree2a9d1392edad4ae97238bb7c98d97e73194d133a
parenteec064b72b1c1d9f6f997041a47d3664599bf43a (diff)
add Halt command, cleanup
-rw-r--r--progs/3state-bb.tm9
-rw-r--r--progs/4state-bb.tm12
-rw-r--r--tm.c49
3 files changed, 51 insertions, 19 deletions
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] <prog>\n", __progname);
+ (void) fprintf(stderr, "usage: %s [-b blank] [-d ms] [-ns] <prog>\n",
+ __progname);
exit(1);
}