/
post-tur.c
285 lines (242 loc) · 6.46 KB
/
post-tur.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/***
*** Data Structures
***/
struct tape_pos {
char symbol;
struct tape_pos *left,
*right;
} *tape;
/***
*** Global Variables
***/
/* The Memory of the machine, the instruction pointer, and the symbol table */
int CodeSpace[1024][2],
label[256];
/***
*** Function Prototypes
***/
/* Utility functions */
int parse_post_turing(FILE *, FILE *, int[][2], int *);
void interpret_post_turing(int, int [][2]);
int write_to_tape(char *);
void display_tape(int);
void destroy_tape(void);
/* Language instructions */
void Right(void);
void Left(void);
void Print(char);
void Cond(char, char, int *);
/***
*** Main program
***/
int main(int argc, char *argv[]) {
FILE *infile;
char initial[128];
int currinst = 0, idx;
/*
* Program Setup
*/
tape = malloc(sizeof(struct tape_pos));
tape->right = tape->left = NULL;
tape->symbol = '0';
for(idx = 0; idx < 256; idx++) {
label[idx] = -1;
}
if(argc < 2) {
fprintf(
stderr,
"\nUsage: %s <filename>\n\tWhere <filename> a valid Post-Turing program.\n\n",
argv[0]);
exit(0);
}
infile = fopen(argv[1], "r");
if(infile == NULL) {
fprintf(stderr, "\nFile \"%s\" does not exist.\n\n", argv[1]);
exit(1);
}
/*
* Parse and run.
*/
currinst = parse_post_turing(infile, stderr, CodeSpace, label);
printf("Enter initial conditions:\n");
scanf("%127[^\n\t]", initial);
idx = write_to_tape(initial);
for( ; idx >= 0; idx--) {
Left();
}
interpret_post_turing(currinst, CodeSpace);
fclose(infile);
destroy_tape();
return 0;
}
/*
* Core runtime engine. Checks type of statement and executes
* appropriate function.
*/
void interpret_post_turing(int line, int code[1024][2]) {
int instruction;
display_tape(20);
for(instruction = 0; instruction < line; instruction++) {
switch(code[instruction][0]) {
case 0:
Right();
break;
case 1:
Left();
break;
case 2:
Print((char)code[instruction][1]);
break;
default: /* It's a label reference */
Cond((char)(code[instruction][0] - 2), code[instruction][1], &instruction);
break;
}
display_tape(20);
}
}
/*
* Straightforward parser for a very simple language.
* Only checks the first character of keywords to identify the
* statements and ignores keywords that are only for structure.
* Leaves a code identifying the statement type and parameter if any.
*/
int parse_post_turing(FILE *file, FILE *errfile, int code[1024][2], int *labels) {
char token[16];
int line = 0;
while(fscanf(file, "%15s", token) > 0) {
switch(tolower(token[0])) {
case '[': /* Label */
labels[token[1]] = line; /* Label points to current line. */
break;
case 'i': /* If */
fscanf(file, "%15s", token);
code[line][0] = token[0] + 2; /* Condition */
fscanf(file, "%15s", token); /* Goto... */
fscanf(file, "%15s", token);
code[line++][1] = token[0]; /* ...the label. */
break;
case 'l': /* Left */
code[line++][0] = 1;
break;
case 'p': /* Print */
code[line][0] = 2;
fscanf(file, "%15s", token);
code[line++][1] = token[0];
break;
case 'r': /* Right */
code[line++][0] = 0;
break;
default: /* Other */
fprintf(errfile, "Invalid program!!\n\n");
exit(2);
break;
}
}
return line;
}
/*
* write_to_tape() copies a string character-by-character onto the tape.
* Returns the number of characters copied from the string.
*/
int write_to_tape(char *string) {
int idx;
char *inputc;
for(inputc = string, idx = 0; *inputc; inputc++, idx++) {
Print(*inputc);
Right();
}
return idx;
}
/*
* display_tape() shows the local area of the tape around the head.
*/
void display_tape(int nLeft) {
int pos;
static int count = 0;
for(pos = 0; pos < nLeft; pos++) {
putchar(' ');
}
putchar('v');
putchar('\n');
for(pos = 0; pos < nLeft; pos++) {
Left();
}
for(pos = 0; pos < 79; pos++) {
putchar(tape->symbol);
Right();
}
for(pos = 0; pos <(79 - nLeft); pos++) {
Left();
}
putchar('\n');
++count;
}
/*
* Destroy() wipes the dynamically-allocated tape cells.
*/
void destroy_tape(void)
{
struct tape_pos *head = tape;
/* Move to the leftmost-allocated cell. */
while (head->left) {
head = head->left;
}
/* Head right, freeing each cell. */
while (head) {
struct tape_pos *shadow = head;
head = head->right;
free(shadow);
}
}
/***
*** Language instruction functions
***/
/*
* Right() moves the tape head to the cell to the right.
* Note that the tape is dynamically allocated as needed.
*/
void Right(void) {
if(tape->right == NULL) {
tape->right = malloc(sizeof(struct tape_pos));
tape->right->right = NULL;
tape->right->left = tape;
tape->right->symbol = '0';
}
tape = tape->right;
}
/*
* Left()--surprise!--moves the tape head to the cell to the left.
* Note that the tape is dynamically allocated as needed.
*/
void Left(void) {
if(tape->left == NULL) {
tape->left = malloc(sizeof(struct tape_pos));
tape->left->left = NULL;
tape->left->right = tape;
tape->left->symbol = '0';
}
tape = tape->left;
}
/*
* Print() writes a symbol to the current tape cell.
*/
void Print(char Symbol) {
tape->symbol = Symbol;
}
/*
* Cond() compares the current cell's contents to the symbol,
* and transfers control to the label if there's a match.
*/
void Cond(char Symbol, char newlabel, int *instruction) {
if(tape->symbol == Symbol) {
if(label[(int)newlabel] == -1) {
printf("\nProgram Terminated\n");
exit(0);
} else {
*instruction = label[(int)newlabel] - 1;
}
}
}