This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
main.cpp
256 lines (211 loc) · 6.01 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "../h/msclStringFuncs.h"
#include "../h/PineconeProgram.h"
#include "../h/ErrorHandler.h"
#include <iostream>
using std::cout;
using std::endl;
using std::vector;
using std::string;
vector <string> cmdLineArgs;
struct Flags
{
string myPath; // path to the pinecone exeutable that is now running
vector<string> inFiles; // all the input files
bool debug=false; // if to show debugging info
bool help=false; // if to show help message
bool version=false; // if to show version message
bool runInterpreted=true; // if to run the program in the interpreter
string cppOutFile=""; // output file for transpiled C++ code, empty if flag not set
string binOutFile=""; // binary executable output file, empty if flag not set
bool runCompiled=false; // if to run the program compiled
bool flagError=false; // if to quit immediately, this is set if there is an unrecognised flag
};
Flags getFlags(int argc, char ** argv);
int main(int argc, char ** argv)
{
Flags flags=getFlags(argc, argv);
if (flags.flagError)
{
cout << "try 'pinecone -h' for help" << endl;
return 0;
}
if (flags.help)
{
cout << "Pinecone v" << VERSION_X << "." << VERSION_Y << "." << VERSION_Z << endl;
cout << "usage: pinecone [options] [source file] [options]" << endl;
cout << "options: " << endl;
cout << "-v, -version display the version of Pinecone" << endl;
cout << "-d, -debug display debugging info before running the program" << endl;
cout << "-r, -run run the program with the interpreter" << endl;
cout << " active by default if no transpiling commands are present" << endl;
cout << " currently, anything after -r is ignored" << endl;
cout << "-cpp [file] transpile to C++ and save the output in the given file" << endl;
cout << "-bin [file] transpile, compile with GCC and save the binary" << endl;
cout << "-e, -execute transpile, compile and execute the binary" << endl;
cout << " any combination of -cpp, -bin and -e can be used" << endl;
cout << " like -r, anything after -e is ignored" << endl;
cout << "-h, -help display this help and quit" << endl;
cout << endl;
cout << endl;
return 0;
}
if (flags.version)
{
cout << "Pinecone version " << VERSION_X << "." << VERSION_Y << "." << VERSION_Z << endl;
return 0;
}
PineconeProgram program;
if (flags.inFiles.empty())
{
cout << "no source file specified" << endl;
cout << "try 'pinecone -h' for help" << endl;
return 0;
}
else if (flags.inFiles.size()>1)
{
cout << "multiple source files specified, Pinecone does not currently support this" << endl;
cout << "try 'pinecone -h' for help" << endl;
return 0;
}
program.resolveProgram(flags.inFiles[0], flags.debug);
if (flags.runInterpreted)
{
if (error.getIfErrorLogged())
{
if (flags.debug)
cout << endl << ">>>>>> execution aborted due to previous error <<<<<<" << endl;
else
cout << "program not executed due to errors" << endl;
}
else
{
if (flags.debug)
cout << endl << "running program..." << endl << endl;
program.execute();
}
}
if (!flags.cppOutFile.empty() || !flags.binOutFile.empty() || flags.runCompiled)
{
string cppCode=program.getCpp();
if (error.getIfErrorLogged())
{
if (flags.debug)
cout << endl << ">>>>>> transpiling failed <<<<<<" << endl;
else
cout << "transpiling failed" << endl;
}
else
{
string cppFileName=flags.cppOutFile;
if (cppFileName.empty())
cppFileName="tmp_pn_transpiled.cpp";
if (flags.debug)
cout << endl << putStringInBox(cppCode, "C++ code", true, false, -1) << endl;
writeFile(cppFileName, cppCode, flags.debug);
if (!flags.binOutFile.empty() || flags.runCompiled)
{
string binFileName=flags.binOutFile;
if (binFileName.empty())
binFileName="tmp_pn_compiled";
string cmd;
cmd="g++ -std=c++11 '"+cppFileName+"' -o '"+binFileName+"'";
if (flags.debug)
cout << "running '"+cmd+"'" << endl;
runCmd(cmd, true);
if (flags.runCompiled)
{
if (flags.debug)
cout << endl;
cmd = "./"+binFileName + " --running-from-pinecone " + str::join(cmdLineArgs, " ", false);
if (flags.debug)
cout << "running '"+cmd+"'" << endl << endl;
runCmd(cmd, true);
}
if (flags.binOutFile.empty())
remove(binFileName.c_str());
}
if (flags.cppOutFile.empty())
remove(cppFileName.c_str());
}
}
if (flags.debug)
cout << endl << "all done" << endl;
return 0;
}
Flags getFlags(int argc, char ** argv)
{
bool after = false;
Flags flags;
for (int i=1; i<argc; i++)
{
string arg(argv[i]);
if (!after)
{
if (arg.size()>1 && arg[0]=='-')
{
string flag=arg.substr(1, string::npos);
if (flag=="d" || flag=="debug")
{
flags.debug=true;
}
else if (flag=="v" || flag=="version")
{
flags.version=true;
}
else if (flag=="h" || flag=="help")
{
flags.help=true;
}
else if (flag=="r" || flag=="run")
{
flags.runCompiled=false;
flags.runInterpreted=true;
after = true;
}
else if (flag=="cpp")
{
if (i+1>=argc)
{
cout << "output file must follow '-cpp' flag";
flags.flagError=true;
}
flags.runInterpreted=false;
flags.cppOutFile=string(argv[i+1]);
i++;
}
else if (flag=="bin")
{
if (i+1>=argc)
{
cout << "output file must follow '-bin' flag";
flags.flagError=true;
}
flags.runInterpreted=false;
flags.binOutFile=string(argv[i+1]);
i++;
}
else if (flag=="e" || flag=="execute")
{
flags.runCompiled=true;
flags.runInterpreted=false;
after = true;
}
else
{
cout << "unknown flag '"+flag+"'" << endl;
flags.flagError=true;
}
}
else
{
flags.inFiles.push_back(arg);
cmdLineArgs.push_back(arg);
}
}
else
{
cmdLineArgs.push_back(arg);
}
}
return flags;
}