-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
399 lines (345 loc) · 10.9 KB
/
main.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <stdbool.h>
#include "brainfuck.h"
#define COMMAND_LINE_STR_SIZE (20)
#define FILE_NAME_SIZE (20)
static bf_env *global_env;
static struct option long_options[] = {
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{"eval", required_argument, 0, 'e'},
{"print", no_argument, 0, 'p'},
{"step", no_argument, 0, 's'},
{"information", no_argument, 0, 'i'},
{"noninit", no_argument, 0, 'n'},
{0, 0, 0, 0}
};
char *program_name;
bool print_code = false;
bool initEnv = true; // 每次执行命令行参数指定的文件时都重置读取头位置
void printUsage(void);
void printVersion(void);
void printMenu(void);
int stepModeFunc(bf_env *env);
int runFile(int argc, char **argv, bf_env *env);
int runCommandLine_(bf_env *env);
int runCommandLine(bf_env *env);
void free_env_at_exit(void);
int clInformation(int ch, bf_env *env);
int main(int argc, char **argv){
int option_index = 0;
int status;
program_name = *argv;
global_env = bf_setEnv();
atexit(free_env_at_exit);
bf_setEnvStepFunc(global_env, stepModeFunc);
while (1) {
option_index = 0;
int c = getopt_long (argc, argv, "vhsie:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
break;
case 'h':
printUsage();
goto end;
case 'v':
printVersion();
goto end;
case 'e': {
bf_code code;
code = bf_parserBrainFuck_Str((char *) optarg); // 不会回到头部重新执行
if (print_code) {
printf("code: ");
bf_printBrainFuck(code);
printf("\n");
}
bf_runBrainFuck(code, global_env);
bf_printError("eval error", global_env);
bf_freeBrainFuck(code);
printf("\n");
break;
}
case 's':
bf_setEnvMode(global_env, step, 1);
break;
case 'i':
bf_setEnvMode(global_env, information, 1);
break;
case 'p':
print_code = true;
break;
case 'n':
initEnv = false;
break;
default:
case '?':
printUsage();
return 1;
}
}
status = runFile(argc, argv, global_env);
if (status != 0)
return status;
status = runCommandLine(global_env);
if (status == 1) {
printf("stdin error\n");
return status;
}
printf("BrainFuckPro: bye~\n");
return 0;
end: // 从-v和-h中退出
if (argc > 2) {
printf("Too many argument\n");
return 1;
}
return 0;
}
void printUsage(void) {
printf("Usage: %s[options] file..\n", program_name);
printf("Options: \n");
printf(" -e --eval\t\t\tRun code in a string\n");
printf(" -v --version\t\t\tShow version\n");
printf(" -h --help\t\t\tShow help\n");
printf(" -s --step\t\t\tRun with step mode\n");
printf(" -i --information\t\tRun with information mode\n");
printf(" -n --noninit\t\t\tDon't init env after run file\n\n");
printf(" -p --print\t\t\tPrint code after parser\n\n");
printf("CommandLine Options: \n");
printf(" v show version\n");
printf(" h show help\n");
printf(" q quit\n");
printf(" m show CommandLine Menu\n");
printf("CommandLine Menu: \n");
printMenu();
printf("Note: CommandLine does not support entering multiple lines of code\n");
printf("\n");
printf("Step Options: \n");
printf(" n continue\n");
printf(" m show step menu\n");
printf(" j (Only debug) Jump to the next debug point\n");
printf(" g (Only debug) Ignore all debug point (just in this code)\n");
printf("Step Menu: \n");
printMenu();
printf("\n");
printf(" For more information, please see: \n");
printf(" github.com/SuperH-0630/BrainFuckPro\n");
}
void printVersion(void) {
printf("%s", bf_getVersionInfo());
}
void printMenu(void) {
printf("+---+----------------------------+\n");
printf("+ v + show version +\n");
printf("+ h + show help +\n");
printf("+ m + show menu +\n");
printf("+ d + print code after parser +\n");
printf("+ w + print env information +\n");
printf("+ p + print paper tape +\n");
printf("+ t + print paper tape little +\n");
printf("+ r + print read head +\n");
printf("+ s + run in step model +\n");
printf("+ i + run with information +\n");
printf("+ n + set new env +\n");
printf("+ f + run file +\n");
printf("+ c + clear screen(May not work) +\n");
printf("+ q + quit +\n");
printf("+ e + exit the menu +\n");
printf("+---+----------------------------+\n");
}
int runFile(int argc, char **argv, bf_env *env) {
while (optind < argc) {
FILE *file = fopen(argv[optind], "r");
if (file == NULL) {
perror("read file error");
return 1;
}
bf_code code;
if (initEnv)
bf_initEnv(env); // 每次执行文件都回到头部重新执行
code = bf_parserBrainFuck_File(file);
if (print_code) {
printf("code: ");
bf_printBrainFuck(code);
printf("\n");
}
bf_runBrainFuck(code, env);
bf_printError("run error", env);
bf_freeBrainFuck(code);
fclose(file);
printf("\n");
optind++;
}
return 0;
}
int stepModeFunc(bf_env *env) {
return clInformation('m', env);
}
int clInformation(int ch, bf_env *env) {
int return_ = 0;
switch (ch) {
case 'v':
printVersion();
break;
case 'h':
printUsage();
break;
case 'm': {
printMenu();
printf("Enter the operation:");
int del_ch;
while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
continue;
ch = getc(stdin);
return_ = clInformation(ch, env);
goto NOT_CLEAR; // 不用清除stdin
}
case 'q': // 退出菜单
return_ = 1;
break;
case 'd':
print_code = !print_code;
if (print_code)
printf("print code mode on\n");
else
printf("print code mode off\n");
break;
case 'w':
bf_printEnvWithMode(env);
break;
case 'p':
bf_printPaperTape(env);
printf("\n");
break;
case 't':
bf_printPaperTapeNear(env);
printf("\n");
break;
case 'r':
bf_printHead(env);
printf("\n");
break;
case 's':
if (bf_setEnvMode(env, step, -1))
printf("step mode on\n");
else
printf("step mode off\n");
break;
case 'i':
if (bf_setEnvMode(env, information, -1))
printf("information mode on\n");
else
printf("information mode off\n");
break;
case 'n':
bf_resetEnv(global_env);
break;
case 'f': {
int size = FILE_NAME_SIZE;
char *str = calloc(size + 1, sizeof(char ));
int del_ch;
while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
continue;
printf("Enter file address:");
fgets(str, FILE_NAME_SIZE + 1, stdin);
while (!strchr(str, '\n') && !feof(stdin) && !ferror(stdin)) {
char *new = calloc(size + FILE_NAME_SIZE + 1, sizeof(char ));
strcpy(new, str);
fgets(new + size, FILE_NAME_SIZE + 1, stdin);
free(str);
str = new;
size += FILE_NAME_SIZE;
}
*strchr(str, '\n') = '\0'; // 去掉回车
FILE *file = fopen(str, "r");
if (file == NULL) {
printf("file = '%s'\n", str);
perror("read file error222");
return -1;
}
bf_code code;
code = bf_parserBrainFuck_File(file);
if (print_code) {
printf("code: ");
bf_printBrainFuck(code);
printf("\n");
}
return_ = bf_runBrainFuck(code, env);
bf_printError("run error", env);
bf_freeBrainFuck(code);
fclose(file);
free(str);
printf("\n");
goto NOT_CLEAR;
}
case 'c':
system("clear"); // 清空
break;
case 'e': // 退出菜单
break;
default:
printf("Unsupported menu option\n");
break;
}
int del_ch;
while ((del_ch = getc(stdin) != '\n') && del_ch != EOF)
continue;
NOT_CLEAR:
return return_;
}
int runCommandLine(bf_env *env) {
int ch;
printf("BrainFuck %s (" __DATE__ ", " __TIME__ ")\n", bf_getVersion());
printf("Welcome to ues BrainFuck CommandLine (Type 'q' to quit)\n");
printf("Type v(version), h(help) and m(menu) for more information\n");
for (unsigned count = 0; true;count++) {
if (feof(stdin) || ferror(stdin))
return 1;
printf("[%d] >", count);
ch = getc(stdin);
if (ch == 'q')
return 0;
else if (ch == 'v' || ch == 'h' || ch == 'm') {
if(clInformation(ch, env) == 1)
return 0; // quit
continue;
} else
ungetc(ch, stdin);
if (runCommandLine_(env) == -2)
return 0; // 退出
}
}
int runCommandLine_(bf_env *env) {
int size = COMMAND_LINE_STR_SIZE;
int status;
char *str = calloc(size + 1, sizeof(char ));
fgets(str, COMMAND_LINE_STR_SIZE + 1, stdin);
while (!strchr(str, '\n') && !feof(stdin) && !ferror(stdin)) {
char *new = calloc(size + COMMAND_LINE_STR_SIZE + 1, sizeof(char ));
strcpy(new, str);
fgets(new + size, COMMAND_LINE_STR_SIZE + 1, stdin);
free(str);
str = new;
size += COMMAND_LINE_STR_SIZE;
}
bf_code code;
code = bf_parserBrainFuck_Str(str);
if (print_code) {
printf("code: ");
bf_printBrainFuck(code);
printf("\n");
}
status = bf_runBrainFuck(code, env);
bf_printError("command line error", env);
bf_freeBrainFuck(code);
free(str);
printf("\n");
return status;
}
void free_env_at_exit(void) {
bf_freeEnv(global_env);
}