-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastree.cpp
183 lines (161 loc) · 4.91 KB
/
astree.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Justin Lardinois [email protected]
// astree.cpp - implementation file for astree.h
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "astree.h"
#include "stringset.h"
#include "lyutils.h"
#include "yyparse.h"
astree::astree (int symbol, int filenr, int linenr,
int offset, const char* clexinfo):
symbol (symbol), filenr (filenr), linenr (linenr),
offset (offset), lexinfo (intern_stringset (clexinfo)),
struct_name (nullptr) {
DEBUGF ('f', "astree %p->{%d:%d.%d: %s: \"%s\"}\n",
(void*) this, filenr, linenr, offset,
get_yytname (symbol), lexinfo->c_str());
}
astree* adopt1 (astree* root, astree* child) {
root->children.push_back (child);
DEBUGF ('a', "%p (%s) adopting %p (%s)\n",
root, root->lexinfo->c_str(),
child, child->lexinfo->c_str());
return root;
}
astree* adopt2 (astree* root, astree* left, astree* right) {
adopt1 (root, left);
adopt1 (root, right);
return root;
}
astree* adopt1sym (astree* root, astree* child, int symbol) {
root = adopt1 (root, child);
root->symbol = symbol;
return root;
}
astree* adoptf(astree* identdecl , astree* params , astree* block) {
astree* root;
if(block->lexinfo->c_str()[0] == ';') {
free_ast(block);
root = new astree(TOK_PROTOTYPE,identdecl->filenr,
identdecl->linenr,identdecl->offset,"");
adopt2(root,identdecl,params);
}else {
root = new astree(TOK_FUNCTION,identdecl->filenr,
identdecl->linenr,identdecl->offset,"");
adopt2(root,identdecl,params);
adopt1(root,block);
}
return root;
}
static void dump_node (FILE* outfile, astree* node) {
const char *tname = get_yytname (node->symbol);
if (strstr (tname, "TOK_") == tname) tname += 4;
fprintf(outfile,"%s \"%s\" (%zu.%zu.%zu)",tname,
node->lexinfo->c_str(),node->filenr,node->linenr,node->offset);
string attributes = stringify_attributes(node->attributes,
node->struct_name);
if (attributes != "") {
fprintf(outfile," {%zu}%s",node->blocknr,attributes.c_str());
if(node->symbol == TOK_IDENT) {
fprintf(outfile," (%zu.%zu.%zu)",node->dfilenr,node->dlinenr,
node->doffset);
}
}
}
static void dump_astree_rec (FILE* outfile, astree* root,
int depth) {
if (root == NULL) return;
const char* indent = "| ";
for(int i = 0; i < depth; i++) fprintf(outfile,indent);
dump_node (outfile, root);
fprintf (outfile, "\n");
for (size_t child = 0; child < root->children.size();
++child) {
dump_astree_rec (outfile, root->children[child],
depth + 1);
}
}
void dump_astree (FILE* outfile, astree* root) {
dump_astree_rec (outfile, root, 0);
fflush (NULL);
}
void yyprint (FILE* outfile, unsigned short toknum,
astree* yyvaluep) {
if (is_defined_token (toknum)) {
dump_node (outfile, yyvaluep);
}else {
fprintf (outfile, "%s(%d)\n",
get_yytname (toknum), toknum);
}
fflush (NULL);
}
void free_ast (astree* root) {
while (not root->children.empty()) {
astree* child = root->children.back();
root->children.pop_back();
free_ast (child);
}
DEBUGF ('f', "free [%p]-> %d:%d.%d: %s: \"%s\")\n",
root, root->filenr, root->linenr, root->offset,
get_yytname (root->symbol), root->lexinfo->c_str());
delete root;
}
void free_ast2 (astree* tree1, astree* tree2) {
free_ast (tree1);
free_ast (tree2);
}
string stringify_enum(int attr, const string* struct_name) {
switch(attr) {
case ATTR_void:
return "void";
case ATTR_bool:
return "bool";
case ATTR_char:
return "char";
case ATTR_int:
return "int";
case ATTR_null:
return "null";
case ATTR_string:
return "string";
case ATTR_struct:
if(struct_name == nullptr) return "struct NULL";
else return "struct \"" + *struct_name + "\"";
case ATTR_array:
return "array";
case ATTR_function:
return "function";
case ATTR_prototype:
return "prototype";
case ATTR_variable:
return "variable";
case ATTR_field:
return "field";
case ATTR_typeid:
return "typeid";
case ATTR_param:
return "param";
case ATTR_lval:
return "lval";
case ATTR_const:
return "const";
case ATTR_vreg:
return "vreg";
case ATTR_vaddr:
return "vaddr";
}
return ""; // shut up g++
}
string stringify_attributes(const attr_bitset& attributes,
const string* struct_name) {
string a = "";
for(int i = 0; i < ATTR_bitset_size; ++i) {
if(attributes[i]) a += " " + stringify_enum(i,struct_name);
}
return a;
}
RCSC("$Id: astree.cpp,v 1.6 2015-04-09 19:31:47-07 - - $")