-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
168 lines (153 loc) · 3.52 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
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
//==============================================
/**
@date: 2012:3:27
@file: main.cpp
@author zhangshuai
*/
//==============================================
#include <stdio.h>
#include <string>
#include <assert.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <type_traits>
#include <memory>
#include "parser/Global.h"
#include "generator/Generator.h"
#include "generator/As3Generator.h"
#include "generator/CppGenerator.h"
#include "generator/CSharpGenerator.h"
#include "generator/GoGenerator.h"
#include "generator/TSGenerator.h"
#include "parser/Program.h"
#include "misc/misc.h"
void usage()
{
std::cout<< "Usage: rpc [options] file" << std::endl;
std::cout<< "Options:" << std::endl;
std::cout<< " -o dir output file directory" << std::endl;
std::cout<< " -i dir input file directory" << std::endl;
std::cout<< " -gen lang cpp as3 cs go ts Generate code" << std::endl;
std::cout<< " -json serialize json." << std::endl;
exit(0);
}
void split(std::string& s, const std::string& delim,std::vector< std::string >& ret)
{
size_t last = 0;
size_t index=s.find_first_of(delim,last);
while (index!=std::string::npos)
{
ret.push_back(s.substr(last,index-last));
last=index+1;
index=s.find_first_of(delim,last);
}
if (index-last>0)
{
ret.push_back(s.substr(last,index-last));
}
}
int main(int argc,char** argv)
{
if (argc < 2)
usage();
std::string genStr;
int i;
Program::Option option;
for (i = 1; i < argc-1; i++)
{
if (strcmp(argv[i], "-o") == 0)
{
option.outputDir_=argv[++i];
option.outputDir_+="/";
}
else if (strcmp(argv[i], "-i") == 0)
{
option.inputDir_=argv[++i];
option.inputDir_+="/";
}
else if (strcmp(argv[i], "-gen") == 0)
{
genStr=argv[++i];
}
else if (strcmp(argv[i], "-json") ==0 )
{
option.json_=true;
}
}
if (argv[i] == NULL) {
std::cerr<<"missing file name"<<std::endl;
usage();
}
if(genStr.empty())
{
std::cerr<<"missing Generate code "<<std::endl;
usage();
}
std::vector<std::string> gen;
split(genStr,";",gen);
std::string fileName=argv[i];
option.baseName_=getBaseName(fileName);
std::string f=option.inputDir_+fileName;
option.fileName_=f;
Program::inst()->option_ = option;
misc::mkdir(Program::inst()->getOutputDir().c_str());
curFileName=option.baseName_;
Program::inst()->contexts_[curFileName]=new Context(curFileName);
yyin = std::fopen(f.c_str() , "r" );
if(!yyin)
{
std::cerr<<"open fail "<< errno<<std::endl;
return 0;
}
try
{
if (yyparse() != 0)
{
std::cerr<<"parser error ."<<std::endl;
return 0;
}
}
catch (std::string x)
{
std::cerr<<x<<std::endl;
}
/**后端生成代码*/
for (auto& it:gen)
{
std::vector<std::string> gen;
split(it,":",gen);
std::string genName=gen[0];
if (gen.size()>1)
{
Program::inst()->option_.compileParam_=gen[1];
}
if (genName=="cpp")
{
std::unique_ptr<Generator> cpp(new CppGenerator(Program::inst(),"cpp"));
cpp->generateProgram();
}
else if(genName=="as3")
{
std::unique_ptr<Generator> as3(new As3Generator(Program::inst(),"as3"));
as3->generateProgram();
}
else if(genName=="cs")
{
std::unique_ptr<Generator> cs(new CSharpGenerator(Program::inst(),"cs"));
cs->generateProgram();
}
else if(genName=="go")
{
std::unique_ptr<Generator> go(new GoGenerator(Program::inst(),"go"));
go->generateProgram();
}
else if (genName == "ts")
{
std::unique_ptr<Generator> go(new TSGenerator(Program::inst(), "ts"));
go->generateProgram();
}
}
return 0;
}