-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfcompiler.h
194 lines (144 loc) · 5.19 KB
/
bfcompiler.h
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef BFCOMPILER_H
#define BFCOMPILER_H
#include <iostream>
#include <string>
#include <stack>
// Constants:
const int DEFAULT_TAPE_SIZE = 4000;
/**
BrainFuck Compiler.
Gets a string of tokens and a tape size.
Returns a string of assembly output of compiled code.
Example:
BFCompiler compiler ("+++[>++<-].,.", 4000);
compiled = compiler.compile();
*/
class BFCompiler {
private:
// Members:
// Used to count ammount of conditions in the assembly code.
// Initialized to 0.
int cond_id;
// Used to count ammount of loops in the assembly code.
// Initialized to 0.
int loop_id;
// Defined Tape Size for the program.
int tape_size;
// Stack for following the scope of loops in the BrainFuck Code.
std::stack<int> loop_stack;
// BrainFuck tokens string to compile.
std::string tokens;
// Private Methods:
/**
Formats the assembly template with the compiled code,
and the tape size.
@param content: The compiled code to put in the template.
@return: Full templated assembly code.
*/
std::string format_template(std::string content);
/**
Gets a BrainFuck token and creates an assembly comment.
@param token: BrainFuck token.
@return: Assembly comment with the BrainFuck token.
*/
std::string token_doc(std::string token);
/**
Creates the assembly code equivelent to '+' in BrainFuck,
which is adding 1 to the char to whom the pointer is pointing.
Adds at the top of the string a doc with the token.
@return: Assembly code for this function.
*/
std::string add();
/**
Creates the assembly code equivelent to '-' in BrainFuck,
which is subtracting 1 from the char to whom the pointer is pointing.
Adds at the top of the string a doc with the token.
@return: Assembly code for this function.
*/
std::string sub();
/**
Creates the assembly code equivelent to '>' in BrainFuck,
which is adding 1 to the pointer and incrementing it's
position on the tape.
Adds at the top of the string a doc with the token.
@return: Assembly code for this function.
*/
std::string increment();
/**
Creates the assembly code equivelent to '<' in BrainFuck,
which is subtracting 1 from the pointer and decrementing it's
position on the tape.
Adds at the top of the string a doc with the token.
@return: Assembly code for this function.
*/
std::string decrement();
/**
Creates the assembly code equivelent to '.' in BrainFuck,
which is to put the char to whom the pointer is pointing
to the stdout.
Adds at the top of the string a doc with the token.
@return: Assembly code for this function.
*/
std::string putch();
/**
Creates the assembly code equivelent to ',' in BrainFuck,
which is getting a single char from the stdin and putting it
in the position to whom the pointer is pointing.
Adds at the top of the string a doc with the token.
The 'cond_id' member is incremented once, and the formatted into
the assembly code.
@return: Assembly code for this function.
*/
std::string getch();
/**
Creates the assembly code equivelent to '[' in BrainFuck,
which is to start a loop.
Adds at the top of the string a doc with the token.
The 'loop_id' member is incremented and pushed into the loop_stack,
Then it is ormatted into the assembly code.
@return: Assembly code for this function.
*/
std::string start_loop();
/**
Creates the assembly code equivelent to ']' in BrainFuck,
which is to end a loop.
Adds at the top of the string a doc with the token.
The top of the loop_stack is formatted into the assembly code,
and the popped.
@return: Assembly code for this function.
*/
std::string end_loop();
/**
Loops over the 'tokens' member's chars. For each token,
runs the appropriate function and concatinates the output to
the compiled string.
Adds at the top of the string a doc with the token.
@return: A string of assembly code equivalent to the BrainFuck code.
*/
std::string compile_tokens();
public:
// Public Methods:
/**
Constructor:
Initializes with given tokens member.
Initializes cond_id to 0, loop_id to 0 and
default tape_size which is DEFAULT_TAPE_SIZE.
@param tokens: BrainFuck tokens string to compile.
*/
BFCompiler(std::string tokens);
/**
Constructor:
Initializes with given tokens and tape_size members.
Initializes cond_id to 0 and loop_id to 0.
@param tokens: BrainFuck tokens string to compile.
@param tape_size: Tape Size for the BrainFuck program.
*/
BFCompiler(std::string tokens, int tape_size);
/**
Calls compile_tokens() and formats the output into the template
by calling format_template().
@return: Fully compiled and templated assembly code.
*/
std::string compile();
};
#endif