-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASTNodes.hpp
150 lines (102 loc) · 3.24 KB
/
ASTNodes.hpp
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
#ifndef AST_Node_H
#define AST_Node_H
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include "enum.hpp"
//#include "parser.hpp"
extern FILE *yyout;
typedef struct AST_node{
int lineNo; // node in which line
// int depth; // node depth, for count white space for print
NodeType type; // node type
char* name; // node name
char* val; // node value
struct AST_node* child; // non-terminals node first child node
struct AST_node* next; // non-terminals node next brother node
} Node;
typedef Node* pNode;
/*
lineNo: 行标
NodeType: decide print logger format
*/
static inline pNode newNode(int lineNo,NodeType type,char *name,int argc,...)
{
pNode curNode=NULL;
int nameLength = strlen(name) + 1;
curNode = (pNode)malloc(sizeof(Node));
assert(curNode!=NULL);
curNode->name = (char*) malloc(sizeof(char)*nameLength);
assert(curNode->name!=NULL);
curNode->lineNo=lineNo;
curNode->type=type;
strncpy(curNode->name,name,nameLength);
va_list va_List;
va_start(va_List,argc);
pNode tempNode = va_arg(va_List, pNode);
curNode->child = tempNode;
for (int i = 1; i < argc; i++) {
tempNode->next = va_arg(va_List, pNode);
if (tempNode->next != NULL) {
tempNode = tempNode->next;
}
}
va_end(va_List);
return curNode;
}
static inline char* newString(char*str){
int len=strlen(str) + 1;
char *nstr=(char*) malloc(sizeof(char)*(len));
strncpy(nstr,str,len);
return nstr;
}
static inline pNode newTokenNode(int lineNo, NodeType type, char* tokenName,
char* tokenText) {
pNode tokenNode = (pNode)malloc(sizeof(Node));
// int nameLength = strlen(tokenName) + 1;
// int textLength = strlen(tokenText) + 1;
assert(tokenNode != NULL);
tokenNode->lineNo = lineNo;
tokenNode->type = type;
tokenNode->name = newString(tokenName);
tokenNode->val = newString(tokenText);
tokenNode->child = NULL;
tokenNode->next = NULL;
return tokenNode;
}
static inline void delNode(pNode* node) {
if (node == NULL) return;
pNode p = *node;
while (p->child != NULL) {
pNode temp = p->child;
p->child = p->child->next;
delNode(&temp);
}
free(p->name);
free(p->val);
free(p);
p = NULL;
}
static inline void printTreeInfo(pNode curNode, int height) {
if (curNode == NULL) {
return;
}
for (int i = 0; i < height; i++) {
printf(" ");
}
fprintf(yyout,"%s", curNode->name);
if (curNode->type == NOT_A_TOKEN) {
fprintf(yyout," (%d)", curNode->lineNo);
} else if (curNode->type == TOKEN_TYPE || curNode->type == TOKEN_ID ||
curNode->type == TOKEN_INT) {
fprintf(yyout,": %s", curNode->val);
} else if (curNode->type == TOKEN_FLOAT) {
fprintf(yyout,": %lf", atof(curNode->val));
}
fprintf(yyout,"\n");
printTreeInfo(curNode->child, height + 1);
printTreeInfo(curNode->next, height);
}
#endif // ASTNode