-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
112 lines (95 loc) · 3.08 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
107
108
109
110
111
112
#include <unordered_map>
#include <iostream>
#include <fstream>
#include "FileUtils.h"
#include "StringUtils.h"
#include "Symbol.h"
using namespace std;
void compile(const list<string>& tokens, ofstream& out);
void compile(const list<string>& tokens, ofstream&& out);
int main() {
string source = file_uitls::read("main.cmm");
list<string> tokens;
try {
tokens = string_utils::tokenize(source, regex(R"([\w]+|\->|[(){}])"));
} catch(regex_error& e)
{
cout << "Regex error code " << e.code() << ": "
<< e.what() << endl;
}
string_utils::print_tokens(tokens);
compile(tokens, ofstream("out/main.c"));
return 0;
}
void compile(const list<string>& tokens, ofstream& out)
{
compile(tokens, move(out));
}
void compile(const list<string>& tokens, ofstream&& out)
{
unordered_map<string, shared_ptr<Symbol>> symbolTable;
for (auto tok = tokens.begin(); tok != tokens.end();)
{
if (*tok == "def")
{
string fname = *(++tok);
if (*(++tok) != "(")
{
// compile error
cout << "expected parameter list following function name \
in definition of '"
<< fname << "'" << endl;
}
else
{
// TEMPORARY
if (*(++tok) != ")")
{
// compile error
cout << "missing closing ')' in function definition of '"
<< fname << "'" << endl;
}
else
{
if (*(++tok) != "->")
{
// compile error
cout << "expected '->' following '"
<< fname << "'" << endl;
}
else
{
string rtype = *(++tok);
symbolTable.insert(make_pair(fname, make_shared<Function>(Function(fname, rtype))));
if (*(++tok) != "{")
{
// compile error
cout << "expected '{' following return type in function definition"
<< endl;
} else
{
// TEMPORARY
if (*(++tok) != "}")
{
cout << "something's wrong..."
<< endl;
}
else
{
// everything's good
++tok; // should == tokens.end()
}
}
}
}
}
}
}
Function main = (Function&)*symbolTable["main"];
out << endl
<< main.getReturnType()
<< " "
<< main.getName()
<< "() {\n\n}" << endl;
out.close();
}