-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqt2sexp.cpp
140 lines (108 loc) · 2.45 KB
/
qt2sexp.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
#include <iostream>
#include <fstream>
#include <string>
#include "ast.h"
#include "control.h"
#include "parser.h"
#include "codemodel.h"
#include "binder.h"
using namespace std;
/* Output format:
*
* namespace: (<name> (namespace*) (class*))
* class: (<name> (class*) (function*))
* function: (<return-type> <name> arg*)
* arg: (<type> <name>)
*/
void doType(TypeInfo type)
{
cout << type.toString().toStdString();
}
void doArgument(ArgumentModelItem &item)
{
string name = item->name().toStdString();
if(name == "") name = "<unknown>";
cout << "(";
doType(item->type());
cout << " " << name << ")";
}
void doFunction(FunctionModelItem &item)
{
cout << "(";
doType(item->type());
cout << " " << item->name().toStdString();
QListIterator<ArgumentModelItem> ai(item->arguments());
while(ai.hasNext()) {
cout << " ";
ArgumentModelItem item = ai.next();
doArgument(item);
}
cout << ")";
}
void doClass(ClassModelItem &item)
{
cout << "(" << item->name().toStdString() << " (";
QListIterator<ClassModelItem> ci(item->classes());
while(ci.hasNext()) {
ClassModelItem item = ci.next();
doClass(item);
}
cout << ") (";
QListIterator<FunctionModelItem> fi(item->functions());
while(fi.hasNext()) {
FunctionModelItem item = fi.next();
doFunction(item);
}
cout << ")";
}
void doNamespace(NamespaceModelItem &item)
{
cout << "(" << item->name().toStdString() << " (";
QListIterator<NamespaceModelItem> ni(item->namespaces());
while(ni.hasNext()) {
NamespaceModelItem item = ni.next();
doNamespace(item);
}
cout << ") (";
QListIterator<ClassModelItem> ci(item->classes());
while(ci.hasNext()) {
ClassModelItem item = ci.next();
doClass(item);
}
cout << "))";
}
void doFile(FileModelItem &item)
{
cout << "(top-level " << "(";
QListIterator<NamespaceModelItem> ni(item->namespaces());
while(ni.hasNext()) {
NamespaceModelItem item = ni.next();
doNamespace(item);
}
cout << ") (";
QListIterator<ClassModelItem> ci(item->classes());
while(ci.hasNext()) {
ClassModelItem item = ci.next();
doClass(item);
}
cout << "))" << endl;
}
int main(int argc, char *argv[])
{
ifstream f;
string s, l;
f.open(argv[1]);
while(!f.eof()) {
getline(f, l);
s += l + "\n";
}
Control control;
Parser p(&control);
pool poo;
TranslationUnitAST *ast = p.parse(s.c_str(), s.length(), &poo);
CodeModel model;
Binder binder(&model, p.location());
FileModelItem dom = binder.run(ast);
doFile(dom);
return 0;
}