-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrlc.c
193 lines (183 loc) · 4.47 KB
/
rlc.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define TAPE_SIZE 30000
int stroke_delay = 0;
int loop_depth = 0;
typedef enum {
PULL,
RECOVER,
STROKE,
BOW,
CATCH,
RELEASE,
LOOP_START,
LOOP_END,
STROKE_RATE_INC,
STROKE_RATE_DEC,
END_OF_FILE,
NEW_LINE,
ERROR
} TokenType;
typedef struct {
TokenType type;
int value;
} Token;
Token nextToken(FILE *);
void emitCode(Token);
void formatter(void);
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <input_file>", argv[0]);
return 1;
}
FILE *source = fopen(argv[1], "r");
if (source == NULL) {
printf("Error: Unable to open input file.\n");
return 1;
}
printf("#include <stdio.h>\n#include <unistd.h>\n\nint main() {\n");
printf("\tunsigned char tape[%d] = {0};\n\tunsigned char *memory = tape;\n\n", TAPE_SIZE);
Token token;
while ((token = nextToken(source)).type != END_OF_FILE) {
if (token.type == LOOP_START) {
printf("\twhile (*memory) {\n");
loop_depth++;
} else if (token.type == LOOP_END) {
printf("\t}\n");
loop_depth--;
} else {
emitCode(token);
}
}
if (loop_depth != 0) {
printf("//Error: Unmatched loop brackets.\n");
printf("\nreturn 1;\n}");
fclose(source);
}
else if (token.type == ERROR){
printf("//Error: Unrecognised command.\n");
printf("\nreturn 1;\n}");
fclose(source);
}
else{
printf("\n\treturn 0;\n}");
fclose(source);
}
return 0;
}
Token nextToken(FILE *file) {
Token token;
char ch = fgetc(file), rep;
switch (ch) {
case 'P':
token.type = PULL;
break;
case 'R':
token.type = RECOVER;
break;
case 'S':
token.type = STROKE;
break;
case 'B':
token.type = BOW;
break;
case '.':
token.type = CATCH;
break;
case ',':
token.type = RELEASE;
break;
case '[':
token.type = LOOP_START;
break;
case ']':
token.type = LOOP_END;
break;
case '^':
token.type = STROKE_RATE_INC;
break;
case 'v':
token.type = STROKE_RATE_DEC;
break;
case EOF:
token.type = END_OF_FILE;
break;
case '\n':
token.type = NEW_LINE;
break;
default:
token.type = ERROR;
}
if (token.type != (END_OF_FILE || ERROR)){
token.value = 1;
rep=fgetc(file);
if (isdigit(rep)) {
ungetc(rep, file);
fscanf(file, "%d", &token.value);
}
ungetc(rep, file);
}
return token;
}
void emitCode(Token token) {
switch (token.type) {
case PULL:
formatter();
printf("memory+=%d;\n", token.value);
break;
case RECOVER:
formatter();
printf("memory-=%d;\n", token.value);
break;
case STROKE:
formatter();
printf("*memory+=%d;\n", token.value);
formatter();
printf("sleep(%d);\n", stroke_delay);
break;
case BOW:
formatter();
printf("*memory-=%d;\n", token.value);
formatter();
printf("sleep(%d);\n", stroke_delay);
break;
case CATCH:
formatter();
printf("putchar(*memory);\n");
break;
case RELEASE:
formatter();
printf("*memory = getchar();\n");
break;
case LOOP_START:
formatter();
printf("while (*memory) {\n");
break;
case LOOP_END:
formatter();
printf("}\n");
break;
case STROKE_RATE_INC:
stroke_delay -= token.value;
if (stroke_delay < 0)
stroke_delay = 0;
break;
case STROKE_RATE_DEC:
stroke_delay += token.value;
break;
case END_OF_FILE:
formatter();
printf("// End of file\n");
break;
case NEW_LINE:
break;
default:
break;
}
}
void formatter(void){
for (int i=0; i<= loop_depth; i++)
printf("\t");
}