-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.c
52 lines (47 loc) · 1.3 KB
/
interpreter.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
/* A simple brainfuck interpreter */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned char tape[30000] = {0};
unsigned char* ptr = tape;
void bf_to_c(char* input) {
char current;
size_t loop;
for(size_t i = 0; input[i]!=0 ; i++)
{
current = input[i];
switch(current) {
case '>': ++ptr;
break;
case '<': --ptr;
break;
case '+': ++*ptr;
break;
case '-': --*ptr;
break;
case ',': *ptr = getchar();
break;
case '.': putchar(*ptr);
break;
case '[': continue;
case ']': if(*ptr) {
loop = 1;
while(loop > 0) {
current = input[--i];
if(current == '[') {
loop--;
}
if(current == ']') {
loop++;
}
}
}
break;
}
}
}
int main()
{
bf_to_c(">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++."); //Prints hello world
return 0;
}