-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
91 lines (81 loc) · 1.89 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
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <string>
#include <unistd.h>
#include <sys/wait.h>
#include "ast.h"
using namespace std;
string tempnamefor(string nm){
return nm+"._temp.cpp";
}
int main(int argc,char **argv){
ios_base::sync_with_stdio(false);
int i,j;
set<char> options;
vector<string> args;
for(i=1;i<argc;i++){
if(argv[i][0]=='-'){
for(j=1;argv[i][j];j++)options.insert(argv[i][j]);
} else {
args.emplace_back(argv[i]);
}
}
for(char it : options){
switch(it){
//case 'a':break;
default:
cerr<<"Option '"<<it<<"' not supported or unknown."<<endl;
return 1;
}
}
if(args.size()!=2){
cerr<<"Requires an input and output file."<<endl;
return 1;
}
string srcfname=args[0];
string outfname=args[1];
ifstream srcf(srcfname,ios::binary);
if(!srcf){
cerr<<"Could not open file '"<<srcfname<<"'"<<endl;
return 1;
}
string tempfname=tempnamefor(outfname);
ofstream outf(tempfname);
if(!srcf){
cerr<<"Could not open temporary file for '"<<outfname<<"'"<<endl;
return 1;
}
srcf.seekg(0,ios::end);
int len=srcf.tellg();
srcf.seekg(0);
string source;
source.resize(len);
srcf.read(&*source.begin(),len);
srcf.close();
Tokens tokens( source );
//for( auto& t : tokens ) std::cerr << t.type << ";" << t.str() << "|";
AST tree( tokens );
string trans=tree.translate();
outf.write(trans.data(),trans.size());
cerr << tempfname << endl;
outf.flush();
pid_t pid=fork();
if(pid==0){
execlp("clang++","clang++","-o",outfname.c_str(),tempfname.c_str(),"y_lib.o","-Wall","-Wextra","-pedantic","-O3","-Wno-unused-variable","-std=c++11","-lgmp","-lgmpxx",NULL);
perror("execlp");
return 1;
}
while(true){
int status;
pid_t w=waitpid(pid,&status,0);
if(w==-1){
perror("waitpid");
return 2;
}
if(WIFEXITED(status))break;
}
outf.close(); //only now release the file to any other writers
return 0;
}