-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
109 lines (100 loc) · 2.47 KB
/
main.c
File metadata and controls
109 lines (100 loc) · 2.47 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define memLength 30000
int lengthOfString(char* n) {
int counter = 0;
while(n[counter] != 0 && n[counter] != -1) {
++counter;
}
return counter;
}
void arrcpy(int* arr1, int* arr2, int len) {
for(int i = 0; i < len - 1; ++i) {
arr1[i] = arr2[i];
}
}
void evaluate(char* code, int len) {
int b = 0;
int i = -1;
int memory[memLength] = {0};
int memPointer = 0;
int startsLength = 0;
int* starts = malloc(startsLength * sizeof(int));
while(i < len) {
++i;
if(code[i] == '+') {
++memory[memPointer];
} else if (code[i] == '-') {
--memory[memPointer];
} else if (code[i] == '>') {
if(memPointer < memLength) {
++memPointer;
} else {
printf("Error: Index %i out of range\n", memPointer);
exit(-1);
}
} else if (code[i] == '<') {
if(memPointer > 0) {
--memPointer;
} else {
printf("Error: Index %i out of range\n", memPointer);
exit(-1);
}
} else if (code[i] == '[') {
if(memory[memPointer] != 0) {
int* temp = starts;
++startsLength;
starts = malloc(startsLength * sizeof(int));
arrcpy(starts, temp, startsLength);
starts[startsLength - 1] = i;
} else {
b = 0;
++i;
while(!(code[i] == ']' && b == 0)) {
if(code[i] == ']') {
--b;
} else if (code[i] == '[') {
++b;
}
++i;
}
}
} else if (code[i] == ']') {
if(memory[memPointer] != 0) {
i = starts[startsLength - 1];
} else {
int* temp = starts;
--startsLength;
starts = malloc(startsLength * sizeof(int));
arrcpy(starts, temp, startsLength + 1);
}
} else if (code[i] == '.') {
printf("%c", memory[memPointer]);
} else if (code[i] == ',') {
system ("/bin/stty raw");
memory[memPointer] = fgetc(stdin);
system ("/bin/stty cooked");
} else {
// Do nothing
}
}
}
int main(int argc, char* argv[]) {
FILE* code = fopen(argv[1], "r");
char current = 'a';
int length = 0;
char* str = malloc(length * sizeof(char*));
while(current != -1) {
++length;
char* temp = str;
str = malloc(length * sizeof(char*));
strcpy(str, temp);
current = fgetc(code);
str[length - 1] = current;
}
fclose(code);
int codeLength = lengthOfString(str);
evaluate(str, codeLength);
return 0;
}