-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.cpp
204 lines (160 loc) · 5.04 KB
/
engine.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
/* BSD 3-Clause License
*
* Copyright (c) 2016, Arty McLabin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include"engine.h"
string engine::custom_cpp_params = "";
vector<CompilationObject*> engine::cobjects;
vector<ChronicsFile*> engine::dot_chr_files;
void engine::abort(string message = "compiler aborted.", int error_code = -1)
{
cout << message << "\t error code: " << error_code << endl;
exit(error_code);
}
int engine::tryCompile(CompilationObject* co)
{
int error_code;
if((error_code=privates::generateCpp(co)))
return error_code;
if((error_code=privates::compileCpp(co)))
return error_code;
return 0;
}
int engine::privates::generateCpp(CompilationObject* co)
{
ensureDir(co->path_root+BUILD_CPP_DIR);
ofstream cpp(co->path_to_cpp);
if(!cpp){
cerr << "couldn't create \"" << co->path_to_cpp << "\"" << endl;
return -1;
}
VERBOSE(cout << "created \"" << co->path_to_cpp << "\"" << endl;)
ifstream src(co->path_to_rune);
cpp <<
"#include<iostream>" << endl <<
"int main(){" << endl;
Compiler compiler(src,cpp);
for(ChronicsFile* chr : dot_chr_files){
compiler.loadChronic(chr->stream, chr->path_to_chr);
}
compiler.translate(); //actual Runetala->C++ processing
cpp <<
"}" << endl;
VERBOSE(cout << "done generating \"" << co->path_to_cpp << "\"" << endl;)
return 0;
}
int engine::privates::compileCpp(CompilationObject *co)
{
VERBOSE(cout << "starting compilation of \"" << co->name << "\"" << endl;)
string system_string = (string)CPP_COMPILER_SETTING +" -o " +co->path_root +co->name +EXECUTABLE_EXTENSION;
system_string += " "+custom_cpp_params+" "+co->path_to_cpp;
int error_code = system(system_string.c_str());
if(error_code==0){
VERBOSE(cout << "done compilation of \"" << co->name << "\"" << endl;)
}
return error_code;
}
void engine::askToRun(string path_to_exe)
{
cout << "would you like to launch it? (y/n)" << endl;
while(char option = getChar())
{
switch(option)
{
#ifdef ON_WINDOWS
case 'y':system(withBackSlashes(path_to_exe).c_str());
#elif defined ON_UNIX
case 'y':system(((string)"./"+path_to_exe).c_str());
#endif
case 'n':goto out_of_while;
}
}out_of_while:
return;
}
void engine::ensureDir(string dir)
{
#ifdef ON_WINDOWS
replace(dir.begin(),dir.end(),'/','\\');
system(((string)"mkdir "+dir).c_str());
#elif defined ON_UNIX
mkdir(dir.c_str());
#endif
}
string engine::withFrontSlashes(string path)
{
#ifdef ON_UNIX
return path; //unix don't have backslashes anyway
#elif defined ON_WINDOWS
replace(path.begin(),path.end(),'\\','/');
return path;
#endif
}
string engine::withoutPath(string filename)
{
return (filename.substr(filename.find_last_of("/")+1));
}
string engine::pathOnly(string filename)
{
return (filename.substr(0,filename.find_last_of("/")+1));
}
string engine::withBackSlashes(string path)
{
replace(path.begin(),path.end(),'/','\\');
return path;
}
char engine::getChar()
{
#ifdef ON_WINDOWS
return getch();
#elif defined ON_UNIX
return getchar();
#endif
}
void engine::parseArgs(vector<string> &args)
{
for(string arg : args){
//+ if(arg is compiler flag){} else:
if(CompilationObject::isDotRuneFile(arg)){
try{
cobjects.push_back(new CompilationObject(withFrontSlashes(arg)));
}catch(CompilationObject*){ // constructor failure
exit(-1);
}
}else if(ChronicsFile::isDotChrFile(arg)){
try{
dot_chr_files.push_back(new ChronicsFile(withFrontSlashes(arg)));
}catch(ChronicsFile*){ // constructor failure
exit(-1);
}
}else{
abort("unexpected command line argument: \""+arg+"\"",-1);
}
}
}