-
Notifications
You must be signed in to change notification settings - Fork 6
/
ast.h
197 lines (177 loc) · 3.47 KB
/
ast.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef AST_H
#define AST_H
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
#else
#ifndef stricmp
#define stricmp strcasecmp
#endif
#ifndef sprintf_s
#define sprintf_s snprintf
#endif
#ifndef Sleep
#define Sleep sleep
#endif
#endif
#define EXPR_LENGTH 256
typedef unsigned int u32, uint;
typedef unsigned short u16;
typedef unsigned char byte;
extern char *type_name[1024];
enum TYPE_ID
{
EXPR_INTNUM = 512,
EXPR_APPROXNUM,
EXPR_STRING,
EXPR_DATETIME,
EXPR_NAME,
EXPR_TABLE_COLUMN,
EXPR_ADD,
EXPR_SUB,
EXPR_MUL,
EXPR_DIV,
EXPR_MOD,
EXPR_AND,
EXPR_OR,
EXPR_XOR,
EXPR_EQ,
EXPR_NE,
EXPR_LT,
EXPR_GT,
EXPR_LE,
EXPR_GE,
EXPR_NEG,
EXPR_NOT,
EXPR_IN_VAL_LIST,
EXPR_NOT_IN_VAL_LIST,
EXPR_LIKE,
EXPR_NOT_LIKE,
EXPR_SELECT,
EXPR_IN_SELECT,
EXPR_NOT_IN_SELECT,
EXPR_VAL_LIST,
EXPR_FUNC,
EXPR_AGR_FUNC_BEGIN,
EXPR_COUNT_ALL,
EXPR_COUNT,
EXPR_SUM_ALL,
EXPR_SUM,
EXPR_AGR_FUNC_END,
EXPR_CASE,
EXPR_CASE_ELSE,
EXPR_CASE_EXPR,
EXPR_CASE_EXPR_ELSE,
EXPR_CASE_NODE,
TABLE_DEFAULT,
TABLE_SUBQUERY,
ORDERBY,
GROUPBY,
SELECT_ALL,
SELECT_STMT,
DELETE_STMT,
INSERT_STMT,
UPDATE_STMT,
EXPR_LAZY,
EXPR_NULL,
EXPR_ERROR
};
typedef struct ExprNode
{
u16 type;
union {
int intval;
float floatval;
char *strval;
struct SelectNode *select;
struct ExprNode *case_head;
};
char *table, *alias;
u16 op;
byte sc;
char text[EXPR_LENGTH];
struct ExprNode *l, *r;
struct ExprNode *next;
} ExprNode;
typedef struct ValueListNode
{
struct ExprNode *head;
struct ValueListNode *next;
} ValueListNode;
typedef struct TableNode
{
u16 type;
union {
char *table;
struct SelectNode *select; //子查询
};
char *alias;
struct TableNode *next;
} TableNode;
typedef struct SetNode
{
char *column;
u16 op;
struct ExprNode *expr;
struct SetNode *next;
} SetNode;
typedef struct ColumnNode
{
char *column;
struct ColumnNode *next;
} ColumnNode;
typedef struct LimitNode
{
u32 start, count;
} LimitNode;
typedef struct SelectNode
{
struct ExprNode *column_head;
struct TableNode *table_head;
struct ExprNode *where;
struct ExprNode *group;
struct ExprNode *order;
struct LimitNode *limit;
void *recs;
} SelectNode;
typedef struct DeleteNode
{
//简易版
char *table;
struct ExprNode *where;
} DeleteNode;
typedef struct InsertNode
{
struct ColumnNode *column_head;
char *table;
struct ValueListNode *value_list_head;
} InsertNode;
typedef struct UpdateNode
{
char *table;
SetNode *set_head;
struct ExprNode *where;
} UpdateNode;
typedef struct SqlAst
{
u16 type;
union {
SelectNode *select;
DeleteNode *delete;
InsertNode *insert;
UpdateNode *update;
};
} SqlAst;
extern SqlAst *ast_root;
extern void yyerror(char *s, ...);
void emit(char *s, ...);
extern SqlAst *parse_sql(char *sql);
extern char *my_strdup(const char *s);
void repeat(char c, int cnt);
void print_ast(SqlAst *node, int d);
void print_select(SelectNode *node, int d);
void print_delete(DeleteNode *node, int d);
void print_insert(InsertNode *node, int d);
void print_update(UpdateNode *node, int d);
void print_column(ExprNode *node, int d);
void print_val_list(ExprNode *node, int d);
void print_expr(ExprNode *node, int d);
#endif