This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
95 lines (83 loc) · 3.25 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
#include <iostream>
#include <string>
#include <vector>
#include <bamboo/module/Module.h>
#include <bamboo/module/Class.h>
#include <bamboo/module/Method.h>
#include <bamboo/dcfile/parse.h>
using namespace bamboo;
using namespace std;
vector<string> astron_keywords {"clsend", "ownsend", "clrecv", "ownrecv",
"airecv", "broadcast", "ram", "required", "db"};
/* Helper function for printing complex types */
void print_type(Type *typ, int indent) {
string tab(2 * indent, ' ');
if(typ->subtype() == kTypeMethod) {
Method *method = typ->as_method();
for(unsigned int i = 0; i < method->num_parameters(); ++i) {
Parameter *param = method->get_parameter(i);
cout << tab;
if(!param->name().empty()) { cout << param->name() << " : "; }
cout << param->type()->to_string() << '\n';
print_type(param->type(), indent + 1);
}
} else if(typ->subtype() == kTypeStruct) {
Struct *cls = typ->as_struct();
for(unsigned int i = 0; i < cls->num_fields(); ++i) {
Field *field = cls->get_field(i);
cout << tab;
if(!field->name().empty()) { cout << field->name() << " : "; }
cout << field->type()->to_string() << '\n';
print_type(field->type(), indent + 1);
}
}
}
int main(void) {
Module *mod = new Module();
for(string keyword : astron_keywords) {
mod->add_keyword(keyword);
}
parse_dcfile(mod, "simple_example.dc");
cout << '\n'; // Newline after prompt
/* Inspect python imports */
cout << "Imports\n";
for(unsigned int i = 0; i < mod->num_imports(); ++i) {
Import *pyimport = mod->get_import(i);
cout << " " << pyimport->module << '\n';
for(string symbol: pyimport->symbols) {
// Figure out the names of used classes
string base = symbol.substr(0, symbol.find('/'));
string remaining = symbol.substr(symbol.find('/'));
vector<string> suffixes;
while(remaining.length() > 0) {
suffixes.push_back(remaining.substr(0, remaining.find('/')));
if(remaining.find('/') == string::npos) { break; }
remaining = remaining.substr(remaining.find('/') + 1);
}
for(string suffix: suffixes) {
cout << " " << base << suffix << '\n';
}
}
}
cout << '\n'; // Newline after imports
/* Inspect module keywords */
cout << "Keywords\n";
for(unsigned int i = 0; i < mod->num_keywords(); ++i) {
cout << " " << mod->get_keyword(i) << '\n';
}
cout << '\n'; // Newline after keywords
/* Inspect module classes */
cout << "Classes\n";
for(unsigned int cls_num = 0; cls_num < mod->num_classes(); ++cls_num) {
Class *cls = mod->get_class(cls_num);
cout << " " << cls->name() << '\n';
for(unsigned int field_num = 0; field_num < cls->num_fields(); ++field_num) {
Field *field = cls->get_field(field_num);
cout << " " << field->name() << " : " << field->type()->to_string() << '\n';
print_type(field->type(), 3);
}
}
cout << '\n'; // Newline before prompt
delete mod;
return 0;
}