-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.hpp
88 lines (64 loc) · 2.09 KB
/
compiler.hpp
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
#pragma once
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <iterator>
namespace BF {
/* ------------------------------ */
/* DEFINITION */
/* ------------------------------ */
class Compiler {
public:
Compiler(size_t mem_size=30000);
bool compile(const std::string& input, std::ostream& out=std::cout);
private:
size_t mem_size_;
std::map<char, std::string> ops_;
};
static void init_BF_compile_map(std::map<char, std::string>& ops);
/* ------------------------------ */
/* IMPLEMENTATION */
/* ------------------------------ */
Compiler::Compiler(size_t mem_size) : mem_size_(mem_size) {
init_BF_compile_map(ops_);
}
bool Compiler::compile(const std::string& input, std::ostream& out) {
out << "#include <stdio.h>\n";
out << "int main() { ";
out << "char array[" << mem_size_ << "] = {0}; ";
out << "char *ptr=array; ";
/* interpret string */
for (auto it = input.begin(); it != input.end(); it++) {
auto& op_sym = *it;
/* handle EOF and \n */
if ((*it) == EOF || (*it) == '\n' || (*it) == '\r' || (*it) == '\t' || (*it) == ' ') {
continue;
}
auto search = ops_.find(op_sym);
if (search == ops_.end()) {
/* bad input */
std::cerr << "Invalid symbol: " << op_sym << "\n";
continue;
}
out << search->second;
}
out << " return 0; }\n";
return true;
}
void init_BF_compile_map(std::map<char, std::string>& ops) {
/* clear to begin with */
ops.clear();
/* fill the ops map here */
ops.insert( std::make_pair('>', "++ptr; ") );
ops.insert( std::make_pair('<', "--ptr; ") );
ops.insert( std::make_pair('+', "++*ptr; ") );
ops.insert( std::make_pair('-', "--*ptr; ") );
ops.insert( std::make_pair('.', "putchar(*ptr); ") );
ops.insert( std::make_pair(',', "*ptr=getchar(); ") );
ops.insert( std::make_pair('[', "while(*ptr){ ") );
ops.insert( std::make_pair(']', "} ") );
}
} // namespace BF