Repository navigation
Expand file tree
/
Copy pathparser.h
More file actions
executable file
·222 lines (196 loc) · 3.29 KB
/
Copy pathparser.h
File metadata and controls
executable file
·222 lines (196 loc) · 3.29 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#pragma once
#include "tokenizer.h"
#include "type.h"
typedef struct _typedef_t typedef_t;
struct _typedef_t {
type_t *type;
char *name;
typedef_t *next;
};
typedef struct _global_var_t global_var_t;
struct _global_var_t {
type_t *type;
char *name;
global_var_t *next;
};
typedef struct {
token_t *cur_token;
typedef_t *typedefs;
global_var_t *globals;
} parser_ctx_t;
typedef enum {
// special
EXPR_CHAR,
EXPR_NUMBER,
EXPR_STRING,
EXPR_IDENT,
EXPR_ASSIGN,
EXPR_CALL,
EXPR_MEMBER,
// unary
EXPR_REF,
EXPR_DEREF,
EXPR_SIZEOF,
EXPR_NOT,
EXPR_NEG,
EXPR_INC_PRE,
EXPR_INC_POST,
EXPR_DEC_PRE,
EXPR_DEC_POST,
// binary
EXPR_ADD,
EXPR_SUB,
EXPR_MUL,
EXPR_DIV,
EXPR_REM,
EXPR_LT,
EXPR_LE,
EXPR_GT,
EXPR_GE,
EXPR_EQ,
EXPR_NE,
EXPR_AND,
EXPR_OR,
EXPR_XOR,
EXPR_SHL,
EXPR_SHR,
EXPR_LOGAND,
EXPR_LOGOR,
} exprtype_t;
int is_unary_expr(exprtype_t type);
int is_binary_expr(exprtype_t type);
typedef struct _argument_t argument_t;
typedef struct _expr_t expr_t;
struct _argument_t {
expr_t *value;
argument_t *next;
};
struct _expr_t {
exprtype_t type;
pos_t *pos;
union {
char char_;
int number;
char *string;
char *ident;
expr_t *unary;
struct {
expr_t *lhs;
expr_t *rhs;
} binary;
struct {
expr_t *dst;
expr_t *src;
} assign;
struct {
char *name;
argument_t *args;
} call;
struct {
expr_t *expr;
char *name;
} member;
struct {
type_t *type;
expr_t *expr;
} sizeof_;
} value;
};
typedef enum {
STMT_EXPR,
STMT_RETURN,
STMT_IF,
STMT_WHILE,
STMT_FOR,
STMT_BLOCK,
STMT_DEFINE,
STMT_BREAK,
STMT_CONTINUE,
STMT_SWITCH,
} stmttype_t;
typedef struct _stmt_list_t stmt_list_t;
typedef struct _stmt_case_t stmt_case_t;
typedef struct _stmt_t stmt_t;
struct _stmt_list_t {
stmt_t *stmt;
stmt_list_t *next;
};
struct _stmt_case_t {
expr_t *value;
stmt_list_t *body;
int label;
stmt_case_t *next;
};
struct _stmt_t {
stmttype_t type;
pos_t *pos;
union {
expr_t *expr;
expr_t *ret;
struct {
expr_t *cond;
stmt_t *then_;
stmt_t *else_;
} if_;
struct {
expr_t *cond;
stmt_t *body;
} while_;
struct {
stmt_t *init;
expr_t *cond;
expr_t *loop;
stmt_t *body;
} for_;
stmt_list_t *block;
struct {
type_t *type;
char *name;
expr_t *value;
} define;
struct {
expr_t *value;
stmt_case_t *cases;
stmt_case_t *default_case;
} switch_;
} value;
};
typedef enum {
GSTMT_FUNC,
GSTMT_FUNC_DECL,
GSTMT_STRUCT,
GSTMT_TYPEDEF,
GSTMT_UNION,
GSTMT_ENUM,
GSTMT_DEFINE,
} global_stmttype_t;
typedef struct _parameter_t parameter_t;
typedef struct _global_stmt_t global_stmt_t;
struct _parameter_t {
type_t *type;
char *name;
parameter_t *next;
};
struct _global_stmt_t {
global_stmttype_t type;
pos_t *pos;
union {
struct {
type_t *ret_type;
char *name;
parameter_t *params;
stmt_t *body;
} func;
type_t *type;
struct {
type_t *type;
char *name;
} define;
} value;
global_stmt_t *next;
};
typedef struct {
global_stmt_t *body;
global_var_t *globals;
} program_t;
program_t *parse(token_t *token);