-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.c
176 lines (162 loc) · 4.19 KB
/
brainfuck.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
#include <stdio.h>
#include <stdlib.h>
#define MAX_MEM (2048 * 1024)
#define MIN_MEM 32
#define DEFAULT_MEM 2048
void display(char* array, int size, char* current);
void showProgram(FILE* program);
char* getProgram(FILE* program, char* allowedChars, int* length);
int contains(char check, char* allowed);
typedef struct Node {
struct Node* next;
int value;
} Node;
void push(Node** head, int data){
Node* new = malloc(sizeof(Node));
new->value = data;
new->next = *head;
*head = new;
}
int pop(Node** head){
if (*head == NULL)
return -1;
Node* trash = *head;
int res = trash->value;
*head = (*head)->next;
free(trash);
return res;
}
int main(int argc, char** argv){
if (!(argc == 2 || argc == 3)){
printf("\nCan't do that! Specify the program file name as follows -\n\tbrainfuck <program-to-execute> [mem_size]\nPlease try again!\n");
return 0;
}
char* allowedChars = "+-[]<>,.\0";
FILE* program = fopen(argv[1], "r");
int mem = DEFAULT_MEM;
if (argc == 3){
mem = atoi(argv[2]);
if (mem > MAX_MEM || mem < MIN_MEM){
mem = DEFAULT_MEM;
}
}
if (!program){
printf("\nUnable to open the given code!\n");
return 0;
}
char* ptr = calloc(mem, sizeof(char));
char* base = ptr;
int size, count;
char* code = getProgram(program, allowedChars, &size);
char read;
Node* stack;
int pos = 0;
#ifdef DEBUG
printf("\nSize : %d, Code: %s", size, code);
#endif
printf("\n");
for (pos = 0; pos < size; pos++){
read = code[pos];
if (read == '\n') continue;
#ifdef DEBUG
printf("inst : %c | pos : %d | ", read, pos);
display(base, 10, ptr);
#endif
switch (read){
case '+':
*ptr += 1;
break;
case '-':
*ptr -= 1;
break;
case '<':
ptr--;
break;
case '>':
ptr++;
break;
case '.':
#if defined(DEBUG) || defined(NUMBER)
printf("%d\n", *ptr);
#else
putc(*ptr, stdout);
#endif
break;
case ',':
*ptr = getchar();
break;
case '[':
if (*ptr == 0){
count = 1;
while (count != 0){
pos++;
if (code[pos] == '[')
count++;
else if (code[pos] == ']')
count--;
}
}
else {
push(&stack, pos);
}
break;
case ']':
if (*ptr != 0){
pos = pop(&stack);
pos--;
}
else {
pop(&stack);
}
break;
}
}
free(base);
printf("\n");
return 0;
}
char* getProgram(FILE* program, char* allowedChars, int* length){
int size = 0;
char read;
while ((read = fgetc(program)) != EOF){
if (contains(read, allowedChars)){
size++;
}
}
if (size == 0)
return NULL;
*length = size;
char* res = calloc(size, sizeof(char));
int i = 0;
fseek(program, 0, SEEK_SET);
while ((read = fgetc(program)) != EOF){
if (contains(read, allowedChars)){
res[i] = read;
i++;
}
}
return res;
}
int contains(char check, char* allowed){
while (*allowed != '\0'){
if (*allowed == check)
return 1;
allowed++;
}
return 0;
}
void showProgram(FILE* program){
char str[100];
fscanf(program, "%s", str);
printf("%s\n", str);
}
void display(char* array, int size, char* current){
printf("{");
for(int i = 0; i < size - 1; i++){
if (&array[i] == current)
printf("|%d|, ", array[i]);
else
printf("%d, ", array[i]);
}
printf("%d}\n", array[size - 1]);
}