-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
106 lines (71 loc) · 1.45 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include "node.h"
#include "main.h"
#include "parser.h"
#include "backend.h"
std::set<char> bf_chars;
Lexer * lexer = NULL;
Block_Node * root = NULL;
std::ifstream bf_file;
int main(int argc, char ** argv)
{
std::cout<<"Compiling "<<argv[1]<<std::endl;
{
bf_chars.insert('+');
bf_chars.insert('-');
bf_chars.insert('<');
bf_chars.insert('>');
bf_chars.insert(',');
bf_chars.insert('.');
bf_chars.insert('[');
bf_chars.insert(']');
}
std::string bf_filname(argv[1]);
std::string c_filename(argv[2]);
lexer = new First_Lexer(bf_filname);
Parser * parser = new BF_Parser;
Backend * backend = new C_Backend;
root = parser->program();
if(parser->is_error)
{
std::cout<<"Compiling failed!"<<std::endl;
}
backend->output(c_filename);
return 0;
}
First_Lexer::First_Lexer(std::string bf_filename)
{
bf_file.open(bf_filename);
current_offset = -1;
current_line = 1;
}
Token * First_Lexer::get_next_token()
{
while(!bf_file.eof())
{
current_offset++;
char current_char = bf_file.get();
if(bf_chars.count(current_char)!=0)
{
return new Token(current_char, current_line, current_offset);
}
if('\n' == current_char)
{
current_offset = -1;
current_line++;
}
}
bf_file.close();
return new Token();
}
void info(std::string info_msg)
{
std::cout<<info_msg;
}
void infon(std::string info_msg)
{
std::cout<<info_msg<<std::endl;
}