-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcsq.cpp
130 lines (123 loc) · 3.47 KB
/
mcsq.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
#include "base/str.h"
#include "base/code_analysis2.h"
#include "base/errors.h"
/*
********************For Parser ************************
*/
bool file_exists(const char *filename)
{
FILE *fp = fopen(filename, "r");
bool is_exist = false;
if (fp != NULL)
{
is_exist = true;
fclose(fp); // close the file
}
return is_exist;
}
auto import_authe(str ln){
if(file_exists(ln.Str) == 0){
csq_error::ImportError ie;
ie.filename = ln;
raise(ie);
}
}
auto __import__(str path){
// cls += name + "{\n";
import_authe(path);
str read_ = read(path,1000000);
return read_ + "\n";
}
auto replace__import__(str ln,str curr){
str path = replaceStr(ln.Str,"import ","");
path = replaceStr(path.Str,".","/");
str spacesep = replaceStr(path.Str,"/"," ");
auto splitted = split(spacesep," ");
str re;
if(path == splitted[splitted.len()-1]){
re = curr + "/";
re += path + ".csqm";
}
else{
re = path + ".csqm";
}
// printf("%s \n\n\n\n\n",re.Str);
return __import__(re);
}
auto replace_for(str ln){
array<str> words = split(ln," ");
str nln;
if(find_str(ln.Str,str("for").Str) == 1 && (find_str(ln.Str,str("for").Str)==1 || find_str(ln.Str,str("for").Str)==1)){
for(auto w : words){
if(w == "for"){
nln += "for(";
}
else if(w == "{" || w == "go"){
nln += "){";
}
else{
nln += w + " ";
}
}
}
else{
nln = ln;
}
return nln;
}
auto IR(str code){
codeAnalysis ca;
ca.raw_code = replace_tokens(semicolan_adder(split(code,"\n")));
ca.parse_down();
// str imports;
// for(auto ln : ca.imports)
// imports += replace__import__(ln) + "\n";
// str functions;
// for(auto ln : ca.functions)
// functions += ln + "\n";
// str codes;
// for(auto ln : ca.code)
// codes += ln + "\n";
// codes += "\nreturn 0;}";
return array<array<str>>{ca.imports,ca.functions,ca.code};
}
void compile_IR(str current_path, str path, str name){
str code = read(path);
write((current_path+name+".cpp").Str,code);
str command = "cd ";
command += current_path;
command += " && g++ ";
command += current_path+name+".cpp -o ";
command += name;
system(command.Str);
// system((str("rm ")+current_path+name+".cpp").Str);
}
void run(str current_dir, str name){
system((str("cd ")+current_dir+str("&& ./")+name + str(" && mv ")+name+str(" ")+name+"so").Str);
}
auto writeIR(str current_dir,str name, str compiler_path){
auto data = IR(read(current_dir + str("/") + name + ".csq"));
str total;
for(int i = 0; i < data.len(); i++){
for(auto ln : data[i]){
if(i == 0){
total += replace__import__(ln,current_dir) + "\n";
}
else{
total += ln + "\n";
}
}
}
write(current_dir + str("/") + name + ".csqm",total);
}
int main(int argc, char const *argv[]){
if(argc < 4){
printf("Enter 3 arguments: <name> <current-dir> <compiler-path>");
// abort();
}
else{
writeIR(argv[2],argv[1],argv[3]);
// compile_IR(argv[2],str(argv[2])+str("/")+str(argv[1]) + ".csqm",argv[1]);
}
// printf("%s\n", total.Str);
return 0;}