-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.cc
executable file
·47 lines (41 loc) · 1.28 KB
/
ast.cc
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
/* File: ast.cc
* ------------
*/
#include "ast.h"
#include "ast_type.h"
#include "ast_decl.h"
#include <string.h> // strdup
#include <stdio.h> // printf
Node::Node(yyltype loc) {
location = new yyltype(loc);
parent = NULL;
}
Node::Node() {
location = NULL;
parent = NULL;
}
/* The Print method is used to print the parse tree nodes.
* If this node has a location (most nodes do, but some do not), it
* will first print the line number to help you match the parse tree
* back to the source text. It then indents the proper number of levels
* and prints the "print name" of the node. It then will invoke the
* virtual function PrintChildren which is expected to print the
* internals of the node (itself & children) as appropriate.
*/
void Node::Print(int indentLevel, const char *label) {
const int numSpaces = 3;
printf("\n");
if (GetLocation())
printf("%*d", numSpaces, GetLocation()->first_line);
else
printf("%*s", numSpaces, "");
printf("%*s%s%s: ", indentLevel*numSpaces, "",
label? label : "", GetPrintNameForNode());
PrintChildren(indentLevel);
}
Identifier::Identifier(yyltype loc, const char *n) : Node(loc) {
name = strdup(n);
}
void Identifier::PrintChildren(int indentLevel) {
printf("%s", name);
}