-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
81 lines (75 loc) · 1.96 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MEMORY_SIZE 30000
char* readFile(char* filename) {
FILE* file = fopen(filename,"r");
if(file == NULL) {
return NULL;
}
fseek(file, 0, SEEK_END);
long int size = ftell(file);
rewind(file);
char* content = calloc(size + 1, 1);
fread(content,1,size,file);
return content;
}
int main(int argc, char *argv[]) {
char memory[MEMORY_SIZE] = {0};
char *ptr = memory;
if (argc != 2) {
printf ("Wrong input!");
return 0;
}
char *code = readFile(argv[1]);
for (int i = 0; code[i]; i++) {
switch (code[i]) {
case '>':
ptr++;
break;
case '<':
ptr--;
break;
case '+':
(*ptr)++;
break;
case '-':
(*ptr)--;
break;
case '.':
putchar(*ptr);
break;
case ',':
*ptr = getchar();
break;
case '[':
if (*ptr == 0) {
i++;
while (code[i] != ']' || code[i - 1] == '[') {
if (code[i] == '[') {
i++;
} else if (code[i] == ']') {
i--;
}
i++;
}
}
break;
case ']':
if (*ptr != 0) {
i--;
while (code[i] != '[' || code[i - 1] == ']') {
if (code[i] == ']') {
i--;
} else if (code[i] == '[') {
i++;
}
i--;
}
i--;
}
break;
}
}
return 0;
}