-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrainFuck.cpp
More file actions
122 lines (107 loc) · 3.2 KB
/
BrainFuck.cpp
File metadata and controls
122 lines (107 loc) · 3.2 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
110
111
112
113
114
115
116
117
118
119
120
121
122
// BrainFuck.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#define PRG1\
"++++++++++ initializes cell zero to 10\
[\
>+++++++>++++++++++>+++>+<<<<-\
] this loop sets the next four cells to 70/100/30/10 \
>++. print 'H'\
>+. print 'e'\
+++++++. 'l'\
. 'l'\
+++. 'o'\
>++. space\
<<+++++++++++++++. 'W'\
>. 'o'\
+++. 'r'\
------. 'l'\
--------. 'd'\
>+. '!'\
>. newline"
#define ADD ",>++++++[<-------->-],[<+>-]<."
#define MUL ",>,>++++++++[<------<------>>-]<<[>[>+>+<<-]>>[<<+>>-]<<<-]>>>++++++[<++++++++>-],<.>.";
#define FIZZBUZZ\
">++++++++++[<++++++++++>-]>>>+++>+++++>+<<<<<<\
[\
>>>+\
>- [<<+>>-]<<<+>[<[-]>>>+<<-]<[>>>+++>>[-]<<<<<- +++++++[>++++++++++<-]>.<+++++++[>+++++<-]>.<++++[>++++<-]>+..[-]<]>>\
>>- [<<<+>>>-]<<<<+>[<[-]>>>>+<<<-]<[>>>>+++++>[-]<<<<<- +++++++++++[>++++++<-]>.<+++++++[>+++++++<-]>++.+++++..[-]<]>>\
>>>\
[-\
<<<[<+>>>>+<<<-]<[>+<-]>>>>\
[\
>++++++++++<\
[>-[>+>+<<-]>[<+>-] +>[<[-]>-]< [>>+<<<++++++++++>-]<<-]\
>---------- >>>[<<<<+>>>>-] <<<<\
>>>>>+> >>[-] <[>+<-] <[>+<-] <<<<< [>>>>>+<<<<<+] <\
]\
>>>>>\
[ <++++++[>>++++++++<<-]>> . [-] >[<+>-] >[<+>-] <<<-]\
<<<<<\
]+\
<<<<<\
+++++++++++++.---.[-]\
<-]"\
char cells[100] = {0};
void interpret(static char* program)
{
int cnt; // cnt is a counter that is going to be used
// only when parsing 0-loops
int stack[100] = {0}; // create a stack, 100 levels deep - modeled
// using a simple array - and initialized to 0
int sp = 0; // sp is going to be used as a 'stack pointer'
char* ip = program; // ip is going to be used as instruction pointer
char* cell = cells; // cell is the pointer to the 'current' memory cell
// and as such, it is initialized to the first
// memory cell
while(*ip) // as long as ip point to 'valid code' keep going
{
if(*ip == ',')
*cell = getch();
else if(*ip == '.')
putch(*cell);
else if(*ip == '>')
cell++;
else if(*ip == '<')
cell--;
else if(*ip == '+')
*cell = *cell + 1;
else if(*ip == '-')
*cell = *cell - 1;
else if(*ip == '[')
{
if(stack[sp] != ip - program)
stack[++sp] = ip - program;
*ip++;
if(*cell != 0)
continue;
else
{
cnt = 1;
while((cnt > 0) || *ip != ']')
{
*ip++;
if(*ip == '[')
cnt++;
else if(*ip == ']')
cnt--;
}
sp--;
}
}else if(*ip == ']')
{
ip = program + stack[sp];
continue;
}
*ip++;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char *prg = FIZZBUZZ;
interpret(prg);
return 0;
}