-
Notifications
You must be signed in to change notification settings - Fork 3
/
bf.monga
154 lines (137 loc) · 3.02 KB
/
bf.monga
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
/* BF INTERPRETER IN MONGA
Copyright 25/10/16
Modified 17/12/16
Lucas Menezes */
int sleep(int t);
int exit(int number);
char getchar();
void putchar(int c);
char[] readFile(char[] filename);
char[] newCharArrayZeroed(int n) {
int i;
char[] array;
array = new char[n];
i=0;
while(i<n) {
array[i] = 0;
i=i+1;
}
return array;
}
int[] newIntArrayZeroed(int n) {
int i;
int[] array;
array = new int[n];
i=0;
while(i<n) {
array[i] = 0;
i=i+1;
}
return array;
}
void error(char[] errorString, int signal) {
@ errorString ; @"\n";
exit(signal);
}
void execute(char[] program)
{
/*does array contains its size? */
/* expects it to finish with null? */
char[] memory;
int prgIdx;
int memIdx;
int[] loopStack;
int stackIdx;
int internalLoopCount;
memory = newCharArrayZeroed(30000);
prgIdx = 0;
memIdx = 0;
loopStack = newIntArrayZeroed(500);
stackIdx = 0;
while(program[prgIdx]) /*assume it finishes in null like a C String*/
{
/*@program[prgIdx];*/
if(program[prgIdx] == '>') {
memIdx = memIdx + 1;
if(memIdx >= 30000)
error("Access Violation, no cell 30000",-1);
}
else if(program[prgIdx] == '<') {
memIdx = memIdx - 1;
if(memIdx < 0)
error("Access Violation, no cell zero",-1);
}
else if(program[prgIdx] == '+') {
memory[memIdx] = (memory[memIdx] as int) + 1;
}
else if(program[prgIdx] == '-') {
memory[memIdx] = (memory[memIdx] as int) - 1;
}
else if(program[prgIdx] == ',') {
memory[memIdx] = getchar();
}
else if(program[prgIdx] == '.') {
@ memory[memIdx];
}
else if(program[prgIdx] == '[') {
/*@"Will begin loop? ";
@memory[memIdx] as int; @"\n";*/
if(memory[memIdx]){
/*@"Entered Loop\n";*/
stackIdx = stackIdx + 1;
loopStack[stackIdx] = prgIdx;
/*@stackIdx; @"\n";*/
}
else {
/*jump to next ]*/
/*@"Won't enter loop\n";*/
prgIdx = prgIdx + 1;
internalLoopCount = 0;
while(!(program[prgIdx] == ']' &&
internalLoopCount == 0))
/*@"Inside While: ";
@"memIdx "; @memIdx;
@"\n"; */
/* while not match stop requirements*/
{
if(program[prgIdx] == '[')
{
internalLoopCount = internalLoopCount + 1;
}
else if(program[prgIdx] == ']')
{
internalLoopCount = internalLoopCount - 1;
}
prgIdx = prgIdx + 1;
}
}
}
else if(program[prgIdx] == ']') {
/*@"Will exit loop? ";
@memory[memIdx] as int;
@"\n";*/
if(memory[memIdx]) {
/*@"continue looping\n";*/
prgIdx = loopStack[stackIdx];
}
else {
/*@"exit loop\n";*/
stackIdx = stackIdx - 1;
/*@stackIdx; @"\n";*/
}
} else {
}
prgIdx = prgIdx + 1;
/*@"prgIdx "; @prgIdx; @"\n";*/
}
}
void main (int argc, char[][] argv) {
char[] programStr;
if(argc == 2) {
programStr = readFile(argv[1]);
}
else {
programStr = ">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]<.#>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]>++++++++[<++++>-]<+.[-]++++++++++.";
}
execute(programStr);
}