This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
151 lines (138 loc) · 4.21 KB
/
test.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
#include "frontend/tokenStream.h"
#include "frontend/lexicalAnalyzer.h"
#include "frontend/syntaxAnalyzer.h"
#include "frontend/myException.h"
#include "driver/driver.hpp"
#include "backend/include/codegen.hpp"
#include <iostream>
#include <fstream>
#include <regex>
#include <set>
using namespace std;
using namespace ast;
inline void printSpace(ostream& os, int depth){
for (int i = 0; i < depth; i++){
os << ' ';
}
}
void printType(const Type* type, ostream& os, int depth){
static set<const Type*> types;
if (types.find(type) != types.end()){
printSpace(os, depth);
os << "_type_\n";
return;
}
else{
types.emplace(type);
}
printSpace(os, depth);
if (typeid(*type) == typeid(PrimitiveType)){
os << "|Prim(" << ((PrimitiveType*)type)->name << ")\n";
}
else if (typeid(*type) == typeid(SumType)){
os << "|Sum\n";
for (unsigned i = 0; i < ((SumType*)type)->types.size(); i++){
printSpace(os, depth);
os << "(" << ((SumType*)type)->types[i].second << "\n";
printType(((SumType*)type)->types[i].first, os, depth + 1);
printSpace(os, depth);
os << ")\n";
}
}
else if (typeid(*type) == typeid(ProductType)){
os << "|Product " << ((ProductType*)type)->cons << "\n";
for (unsigned i = 0; i < ((ProductType*)type)->types.size(); i++){
printType(((ProductType*)type)->types[i], os, depth + 1);
}
}
else if (typeid(*type) == typeid(FunctionType)){
os << "|Func" << "\n";
printType(((FunctionType*)type)->left, os, depth + 1);
printType(((FunctionType*)type)->right, os, depth + 1);
}
}
void printTerm(const Term* term, ostream& os, int depth){
printSpace(os, depth);
if (typeid(*term) == typeid(Reference)){
os << "|Ref(" << ((Reference*)term)->name << ")\n";
}
else if (typeid(*term) == typeid(Abstraction)){
os << "|Abs " << ((Abstraction*)term)->arg << "\n";
printType(((Abstraction*)term)->type, os, depth + 1);
printTerm(((Abstraction*)term)->term, os, depth + 1);
}
else if (typeid(*term) == typeid(Application)){
os << "|App\n";
printTerm(((Application*)term)->func, os, depth + 1);
printTerm(((Application*)term)->arg, os, depth + 1);
}
else if (typeid(*term) == typeid(Desum)){
os << "|Desum\n";
printTerm(((Desum*)term)->sum, os, depth + 1);
for (unsigned i = 0; i < ((Desum*)term)->cases.size(); i++){
printSpace(os, depth);
os << "(" << ((Desum*)term)->cases[i].first << "\n";
printTerm(((Desum*)term)->cases[i].second, os, depth + 1);
}
}
else if (typeid(*term) == typeid(Deproduct)){
os << "|Deproduct\n";
printTerm(((Deproduct*)term)->product, os, depth + 1);
printSpace(os, depth);
os << "names:";
for (unsigned i = 0; i < ((Deproduct*)term)->names.size(); i++){
os << ((Deproduct*)term)->names[i] << ' ';
}
os << "\n";
printTerm(((Deproduct*)term)->term, os, depth + 1);
}
else if (typeid(*term) == typeid(Fixpoint)){
os << "|Fixpoint\n";
printTerm(((Fixpoint*)term)->term, os, depth + 1);
}
}
int main(){
LexicalAnalyzer lexicalAnalyzer;
//string srcfile = "it.estlc";
string srcfile = "qsort.mfc";
try{
ifstream is(srcfile);
if (!is){
throw runtime_error("Non-exist source file \"" + srcfile + "\"");
}
TokenStream tokenStream = lexicalAnalyzer.parse(is);
ofstream os("driver/qsort.tks");
for (unsigned i = 0; i < tokenStream.size(); i++){
Token* tp = &tokenStream[i];
os << "<" << TokenName[tp->type] << ">\t{" << tp->name << "}\tline " << tp->nrow << "\n";
}
os.close();
os.clear();
SyntaxAnalyzer syntaxAnalyzer(tokenStream);
os.open("driver/qsort.ast");
printTerm(syntaxAnalyzer.getProgram()->term, os, 0);
cout << "Frontend end successfully\n";
Codegen codegen;
Codegen::Term v = codegen.generate(*syntaxAnalyzer.getProgram());
cout << "Backend end successfully\n";
Driver::serialize(syntaxAnalyzer.getProgram(), &codegen.map);
cout << "Driver end successfully";
getchar();
}
catch (const lexical_error& e){
cerr << srcfile << "(" << e.line << "):lexical error: \'" << e.peek << "\' is not a valid character\n";
getchar();
return 1;
}
catch (const syntax_error& e){
cerr << srcfile << "(" << e.line << "):syntax error: " << e.what() << " with " << e.token << "\n";
getchar();
return 1;
}
catch (const exception& e){
cerr << e.what() << "\n";
getchar();
return 1;
}
return 0;
}