aboutsummaryrefslogtreecommitdiff
path: root/include/parse.h
blob: 68cf6c064298e7ab21cf0b0e9dc9a9a9f14431b7 (plain) (blame)
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
#ifndef CERC_PARSE_H
#define CERC_PARSE_H
#include "expr.h"

struct ast_local {
	struct location loc;

	const char *name;
	struct type type;
	struct expr *init;
};

struct ast_assign {
	struct location loc;

	struct expr lhs;
	struct expr rhs;
};

struct ast_elsebody {
	struct ast_stmt *stmts;
	int stmtsz, stmtlen;
};

struct ast_else {
	struct location loc;

	int cond;
	union {
		struct ast_if *conditional;
		struct ast_elsebody body;
	} action;
};

struct ast_if {
	struct location loc;

	struct expr cond;
	struct ast_stmt *stmts;
	int stmtsz, stmtlen;
	struct ast_else *alt;
};

struct ast_for {
	struct location loc;

	struct ast_stmt *init;
	struct expr cond;
	struct ast_stmt *step;
	struct ast_stmt *stmts;
	int stmtsz, stmtlen;
};

struct ast_return {
	struct location loc;

	struct expr *value;
};

enum stmt {
	S_LOCALVAR,
	S_ASSIGN,
	S_ADDASSIGN,
	S_SUBASSIGN,
	S_MULASSIGN,
	S_DIVASSIGN,
	S_REMASSIGN,
	S_BANDASSIGN,
	S_BORASSIGN,
	S_BXORASSIGN,
	S_INCR,
	S_DECR,
	S_IF,
	S_FOR,
	S_RETURN,
	S_EXPR,
};

struct ast_stmt {
	struct location loc;

	enum stmt kind;
	union {
		struct ast_local localvar;
		struct ast_assign assign;
		struct ast_if conditional;
		struct ast_for loop;
		struct ast_return ret;
		struct expr e;
	} stmt;
};

struct ast_include {
	struct location loc;

	const char *path;
};

struct ast_global {
	struct location loc;

	const char *name;
	int ispub;
	int isextern;
	struct type type;
};

struct ast_const {
	struct location loc;

	const char *name;
	int ispub;
	struct expr value;
};

struct ast_enumvar {
	struct location loc;

	const char *name;
	struct expr *override;
};

struct ast_enum {
	struct location loc;

	struct expr init;
	int ispub;
	struct ast_enumvar *variants;
	int variantsz, variantlen;
};

struct ast_field {
	struct location loc;

	const char *name;
	struct type type;
};

struct ast_struct {
	struct location loc;

	const char *name;
	int align;
	int ispub;
	struct ast_field *fields;
	int fieldsz, fieldlen;
};

struct ast_union {
	struct location loc;

	const char *name;
	int ispub;
	struct type *types;
	int typesz, typelen;
};

struct ast_func {
	struct location loc;

	int ispub;
	int isextern;
	const char *name;
	struct ast_field *params;
	int paramsz, paramlen;
	struct type *ret;
	struct ast_stmt *stmts;
	int stmtsz, stmtlen;
};

enum toplevel {
	TOP_INCLUDE,
	TOP_GLOBAL,
	TOP_CONST,
	TOP_ENUM,
	TOP_STRUCT,
	TOP_UNION,
	TOP_FUNC,
};

struct ast_toplevel {
	struct location loc;

	enum toplevel kind;
	union {
		struct ast_include include;
		struct ast_global globalvar;
		struct ast_const constant;
		struct ast_enum enumconst;
		struct ast_struct structdecl;
		struct ast_union uniondecl;
		struct ast_func function;
	} tl;
};

struct ast_unit {
	struct ast_toplevel *tops;
	int topsz, toplen;
};

void parse(struct lexer *lexer, struct ast_unit *ast);

#endif