-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.h
226 lines (185 loc) · 4.66 KB
/
parser.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
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
223
224
225
226
#ifndef PARSER_H
#define PARSER_H
#include "buffer.h"
#include "utils.h"
#include "zone_alloc.h"
// clang-format off
#define TK_TOKEN_TYPES(o) \
o(TK_EEOF) \
o(TK_ERROR) \
o(TK_ID) \
o(TK_KW_WHILE) \
o(TK_KW_IF) \
o(TK_KW_ELSE) \
o(TK_KW_VOID) \
o(TK_KW_INT64) \
o(TK_KW_RETURN) \
o(TK_PLUS) \
o(TK_MINUS) \
o(TK_STRING) \
o(TK_MUL) \
o(TK_DIV) \
o(TK_NUMBER) \
o(TK_ASSIGN) \
o(TK_EQUALS) \
o(TK_GREATER) \
o(TK_LESS_THAN) \
o(TK_LESS_EQ) \
o(TK_GREATER_EQ) \
o(TK_CURLY_OPEN) \
o(TK_CURLY_CLOSE) \
o(TK_PARAN_OPEN) \
o(TK_PARAN_CLOSE) \
o(TK_SEMI_COLON)
// clang-format on
enum token_type { TK_TOKEN_TYPES(COMMA) };
struct token {
enum token_type type;
position_t pos;
range_t range;
};
// clang-format off
#define AST_NODE_TYPE(o) \
o(NUMBER, number) \
o(STRING, string) \
o(VARIABLE, variable) \
o(BINARY_EXP, binary_exp) \
o(WHILE, while) \
o(IF, if) \
o(MODULE, module) \
o(FUNCTION, function) \
o(FUNCTION_CALL, function_call) \
o(BLOCK, block) \
o(PREFIX, prefix) \
o(RETURN, return ) \
o(DECLARATION, declaration)
// clang-format on
#define AST_TYPE(prefix) struct ast_##prefix
#define AST_AS_TYPE(ptr, prefix) containerof(ptr, AST_TYPE(prefix), node)
#define COMMA2(e, p) e,
#define STR_COMMA2(e, p) #e,
enum ast_node_type { AST_NODE_TYPE(COMMA2) };
extern char *kASTNodeName[];
struct ast_node {
enum ast_node_type type;
};
struct ast_module {
struct ast_node node;
// All children must be functions.
struct ast_node **childs;
size_t childCount;
};
struct ast_function {
struct ast_node node;
range_t name;
enum token_type returnType;
size_t argumentCount;
struct ast_node **childs;
size_t childCount;
};
struct ast_function_call {
struct ast_node node;
range_t name;
struct ast_node **childs;
size_t childCount;
};
struct ast_while {
struct ast_node node;
union {
struct ast_node *childs[2];
struct {
struct ast_node *condition;
struct ast_node *block;
};
};
};
struct ast_block {
struct ast_node node;
struct ast_node **childs;
size_t childCount;
};
struct ast_if {
struct ast_node node;
union {
struct ast_node *childs[3];
struct {
struct ast_node *condition;
struct ast_node *ifBlock;
struct ast_node *elseBlock;
};
};
size_t childCount;
};
struct ast_binary_exp {
struct ast_node node;
enum token_type op;
union {
struct ast_node *childs[2];
struct {
struct ast_node *left;
struct ast_node *right;
};
};
};
struct ast_number {
struct ast_node node;
int64_t num;
};
struct ast_variable {
struct ast_node node;
range_t varName;
};
struct ast_string {
struct ast_node node;
range_t contents;
};
struct ast_prefix {
struct ast_node node;
int isPlus; // plus or minus ?
union {
struct ast_node *childs[1];
struct {
struct ast_node *child;
};
};
};
struct ast_declaration {
struct ast_node node;
enum token_type dataType;
union {
struct ast_node *childs[1];
struct ast_node *assignment;
};
};
struct ast_return {
struct ast_node node;
};
// used for things like ast_module_new
#define GEN_NEW_DECLERATION(enu, prefix) \
AST_TYPE(prefix) * ast_##prefix##_new();
AST_NODE_TYPE(GEN_NEW_DECLERATION)
#undef GEN_NEW_DECLERATION
void ast_nodeInfo(struct ast_node *node, size_t i);
void ast_dump(struct ast_node *node, size_t i);
char *ast_dumpDot(struct ast_node *node);
void token_dumpAll(char *string);
struct reader {
position_t pos;
range_t range;
};
typedef struct {
struct reader reader;
int hasPeek;
struct token peek;
char *error;
zone_allocator zone;
} parser_t;
struct ast_node *parser_parseExpression(parser_t *parser);
struct ast_node *parser_parseBlock(parser_t *parser);
struct ast_node *parser_parseFunction(parser_t *parser);
void parser_init(parser_t *parser, range_t range);
#undef COMMA2
#undef STR_COMMA2
#undef FIRST2
#undef SECOND2
#endif