-
Notifications
You must be signed in to change notification settings - Fork 41
/
ast.c
92 lines (85 loc) · 1.63 KB
/
ast.c
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
#include "ngs.h"
#include "ast.h"
#include "obj.h"
char *NGS_AST_NODE_TYPES_NAMES[] = {
NULL,
"assignment",
"identifier",
"int",
"real",
"expressions",
"for",
"call",
"empty",
"array",
"func",
"params",
"param",
"str_comps",
"str_comp_imm",
"str_comp_expansion",
"str_comp_splat_expansion",
"null",
"true",
"false",
"defined",
"if",
"assign_default",
"while",
"args",
"arg",
"local",
"upvar",
"global",
"hash",
"index",
"attr",
"ns",
"return",
"and",
"or",
"tand",
"tor",
"arr_splat",
"hash_splat",
"guard",
"try_catch",
"throw",
"commands_pipeline",
"commands_pipe",
"command",
"break",
"continue",
"redir",
"switch",
"super",
"regexp",
"table",
};
#define AST_NODE_INFO_STR_LEN (128)
void print_ast(ast_node *node, int level) {
ast_node *child;
char *name = "(n/a)";
char info[AST_NODE_INFO_STR_LEN] = "(n/a)";
switch(node->type) {
case CALL_NODE:
case INT_NODE:
snprintf(info, AST_NODE_INFO_STR_LEN-1, "%ld", node->number);
break;
case REAL_NODE:
snprintf(info, AST_NODE_INFO_STR_LEN-1, NGS_REAL_FMT, (*(NGS_REAL *)node->data));
break;
}
if(node->name) {
name = node->name;
}
printf("[DEBUG] %*s+ AST node at %p, type %s, name %s, info %s, data %s, source_location %d:%d-%d:%d%s\n", level*2, "", \
node, NGS_AST_NODE_TYPES_NAMES[node->type], name, info, \
node->data ? "exists" : "does not exist", \
node->location.first_line, node->location.first_column, \
node->location.last_line, node->location.last_column, \
node->location.is_generated ? " (generated)" : "");
for(child = node->first_child; child; child=child->next_sibling) {
print_ast(child, level+1);
}
}