-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccc.cpp
158 lines (145 loc) · 3.58 KB
/
ccc.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
#include "Scanner.hpp"
#include "Parser.hpp"
#include "CodeGen.hpp"
#include <stdlib.h>
#include <stdio.h>
static string Set_cap(int pos){
string str;
for(int i = 0; i < pos; i++)
str += ' ';
str += '^';
return str;
}
void mes_error_input(){
printf("%s\n", "ccc:\tfatal error: no input files");
}
void mes_option(char* op){
printf("%s%s'\n", "ccc: error: unrecognize command line option '", op);
}
void mes_help(){
printf("%s\n", "C Compiler\nUsage: ccc [options] file ...\n\
Options:\n\
\t-h\tDisplay this information\n\
\t-l\tScan only\n\
\t-s\tParse syntax tree\n\
\t-S\tCompile only; do not assemble or link\n\
\t-c\tCompile, assemble and link\n");
}
void mes_error_lex(Error& e, char* filename){
int last = e.Lex.getText().size();
cout << filename << ":" << e.Lex.getX() << ":" << e.Lex.getY() + last << ' ' + e.str << (e.Lex.getText()).substr(last - e.end, e.end) << endl
<< '\t' + e.Lex.getText() << endl
<< '\t' + Set_cap(last - e.end) << endl;
}
void mes_error_nosuchfile(Error& e, char* filename){
cout << "ccc: error: " << filename << ": No such file in directory\n"
<<"ccc: fatal error: no input files\ncompilation terminated." << endl;
}
void mes_error_synt(SyntaxError &e, char* filename, Scanner :: Lexem Lex){
auto tok = Lex;
cout << filename << ":" << tok.getX() << ":" << tok.getY() + tok.getText().size() << ' ';
e.a?(cout << e.str + " ‘" + tok.getText() + "’"):(cout << e.str);
cout << endl;
}
void scan(char* filename){
try{
Scanner Sc(filename);
try{
Sc.next();
while(Sc.get() != Eof){
Sc.get().print();
Sc.next();
}
}
catch(Error& e){
mes_error_lex(e, filename);
}
}
catch(Error& e){
mes_error_nosuchfile(e, filename);
}
}
void parse(char* filename){
try{
Scanner Sc(filename);
try{
Sc.next();
}
catch(Error& e){
mes_error_lex(e, filename);
}
if(Sc.get() != Eof){
try{
Parser Pa(&Sc);
Pa.print();
}
catch(SyntaxError& e){
mes_error_synt(e, filename, Sc.get());
}
}
}
catch(Error& e){
mes_error_nosuchfile(e, filename);
}
}
void genAssemble(char* filename, char* outfile, bool optimization){
try{
Scanner Sc(filename);
try{
Sc.next();
}
catch(Error& e){
mes_error_lex(e, filename);
}
if(Sc.get() != Eof){
try{
Parser Pa(&Sc);
CodeGen CG(outfile);
Pa.generate(CG);
if(optimization) CG.optimize();
CG.print();
}
catch(SyntaxError& e){
mes_error_synt(e, filename, Sc.get());
}
}
}
catch(Error& e){
mes_error_nosuchfile(e, filename);
}
}
int main(int argc, char *argv[]){
switch(argc){
case 1:
mes_help();
break;
case 2:
if(strcmp(argv[1], "-h") == 0) mes_help();
else mes_error_input();
break;
case 3:
if(strcmp(argv[1], "-l") != 0 && strcmp(argv[1], "-s") != 0 && strcmp(argv[1], "-h") != 0) mes_option(argv[1]);
else if(strcmp(argv[1], "-h") == 0) mes_help();
else if(strcmp(argv[1], "-l") == 0) scan(argv[2]);
else if(strcmp(argv[1], "-s") == 0) parse(argv[2]);
break;
case 5:
if(strcmp(argv[1], "-l") != 0 && strcmp(argv[1], "-s") != 0 && strcmp(argv[1], "-S") != 0 ) mes_option(argv[1]);
else if(strcmp(argv[3], "-out") != 0) mes_option(argv[3]);
else if(strcmp(argv[3], "-out") == 0){
if(strcmp(argv[1], "-S") == 0) genAssemble(argv[2], argv[4], false);
else if(strcmp(argv[1], "-S") == 0) genAssemble(argv[2], argv[4], true);
else{
freopen(argv[4], "w", stdout);
if(strcmp(argv[1], "-l") == 0) scan(argv[2]);
else if(strcmp(argv[1], "-s") == 0) parse(argv[2]);
fclose(stdout);
}
}
break;
case 6:
genAssemble(argv[3], argv[5], true);
break;
}
return 0;
}