-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastree.cpp
More file actions
110 lines (96 loc) · 2.47 KB
/
Copy pathastree.cpp
File metadata and controls
110 lines (96 loc) · 2.47 KB
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
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
using namespace std;
#include "astree.h"
#include "lyutils.h"
unordered_set<string> astree::lextable;
vector<string> astree::filenames { "/" };
ostream& operator<<(ostream& out, const location& loc)
{
return out << astree::filenames.at(loc.filenr) << ":" << loc.linenr
<< ":" << loc.offset;
}
astree::astree(int symbol_, const location& loc_, const char* info)
: symbol(symbol_)
, loc(loc_)
{
auto handle = lextable.emplace(info);
lexinfo = &(*handle.first);
attributes.reset();
block_number = 0;
sequence = 0;
struct_entry = nullptr;
vreg = "";
if (yydebug)
cerr << "::astree: " << *this << endl;
}
astree::~astree()
{
if (yydebug)
cerr << "::~astree: " << *this << endl;
for (astree_ptr child : children)
erase(child);
}
astree_ptr astree::adopt(
astree_ptr child1, astree_ptr child2, astree_ptr child3)
{
if (child1 != nullptr)
children.push_back(child1);
if (child2 != nullptr)
children.push_back(child2);
if (child3 != nullptr)
children.push_back(child3);
return this;
}
astree_ptr astree::adopt_as(astree_ptr child, int symbol_)
{
symbol = symbol_;
adopt(child);
return this;
}
void astree::dump_tree(int depth)
{
cerr << "::astree: " << setw(depth * 3) << "" << *this << endl;
for (astree_ptr child : children)
child->dump_tree(depth + 1);
}
void astree::print_tree(ostream& out, int depth)
{
out << "; astree: " << setw(depth * 3) << "" << *this << endl;
for (astree_ptr child : children)
child->print_tree(out, depth + 1);
}
void astree::dump_filenames()
{
for (size_t index = 0; index < filenames.size(); ++index) {
cerr << "::astree::cppfile[" << index << "]: \""
<< filenames[index] << "\"" << endl;
}
}
void astree::print_symbol_value(astree_ptr tree)
{
if (tree == nullptr)
cerr << "nullptr";
else
cerr << *tree;
}
ostream& operator<<(ostream& out, const astree& tree)
{
out << &tree << "->{" << get_parser_yytname(tree.symbol) << " \""
<< *tree.lexinfo << "\" " << tree.loc << "}";
for (astree_ptr child : tree.children)
out << " " << child;
return out;
}
astree_ptr astree::make(
int symbol, const location& loc, const char* lexinfo)
{
return new astree(symbol, loc, lexinfo);
}
void astree::erase(astree_ptr& tree)
{
delete tree;
tree = nullptr;
}