-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
108 lines (93 loc) · 2.12 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
#include<cstring>
#include<stdio.h>
#include "src/lexer.h"
#include "src/AST.h"
#include "src/gen_x86.h"
#include "src/parser.h"
//namespace Yan{
static void help()
{
std::string help="Usgae: Yan [option] file ...\n"
" -h Display help info\n"
" -S Compile only,Do not assemble and link\n"
" -o Complie and link,Generate excutable file\n";
std::cout<<help;
}
static void compile(std::vector<std::string>&files)
{
Yan::lexer pp(files.front());
std::string out_asmeble_file = "a.s";
Yan::parser pars(pp);
auto pp1= pars.parseProgram();
Yan::gen gencode(pars.getScope(),out_asmeble_file);
pp1->accept(&gencode);
// if(Yan::log_level == Yan::L_DEBUG)
// {
// std::ofstream f(files.front()+".symbol");
// pars.getScope()->dumpSymbol(f);
// f.close();
// }
// gencode.genProgram(pp1);
}
static void compile_link(std::vector<std::string>&files)
{
compile(files);
const char* cmd = "cc a.s";
if(system(cmd)!=0)
{
Yan::ExitWithError("Link error");
}
}
static void parseArgs(int argc, char* argv[])
{
if(argc<2)
{
help();
}
bool S_arg = false;
bool h_arg =false;
bool o_arg = false;
bool error = false;
std::vector<std::string> inputFiles;
for(auto i=1; i<argc;i++)
{
if(strcmp(argv[i],"-S")==0)
{
S_arg =true;
}
else if (strcmp(argv[i],"-h")==0)
{
h_arg =true;
}
else if(strcmp(argv[i],"-o")==0)
{
o_arg = true;
}
else if(argv[i][0]!='-')
{
inputFiles.emplace_back(argv[i]);
}
else
{
error =true;
}
}
if(error || h_arg || inputFiles.empty())
{
help();
return;
}
if(S_arg)
{
compile(inputFiles);
}
else if(o_arg||!inputFiles.empty())
{
compile_link(inputFiles);
}
}
int main(int argc,char*argv[])
{
parseArgs(argc,argv);
return 0;
}