aboutsummaryrefslogtreecommitdiff
path: root/include/parse.h
blob: 18207bcf08452e340f9fb5e3793adebb45502106 (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
#ifndef CERC_PARSE_H
#define CERC_PARSE_H
#include "expr.h"

struct ast_expr {
	struct disjunction_e dis;
};

struct ast_externfunc {
	const char *name;
	struct type ret;
	struct type *params;
	int paramsz, paramlen;
};

struct ast_param {
	const char *name;
	struct type type;
};

struct ast_block {
	struct ast_cmd *cmds;
	int cmdsz, cmdlen;
};

struct ast_return {
	struct ast_expr value;
};

struct ast_declaration {
	bool mut;
	const char *name;
	struct ast_expr value;
};

enum subfield {
	SUB_INDEX,
	SUB_FIELD,
};

struct loc_postfix {
	enum subfield sub;
	union {
		struct ast_expr index;
		const char *field;
	} id;
};

struct ast_location {
	const char *name;
	struct loc_postfix *subs;
	int subsz, sublen;
};

struct ast_assign {
	struct ast_location dst;
	struct ast_expr value;
};

enum statement {
	STM_RETURN,
	STM_BREAK,
	STM_CONTINUE,
	STM_DECL,
	STM_ASSIGN,
	STM_CALL,
	STM_DEFER,
};

struct ast_statement {
	enum statement action;
	union {
		struct ast_return ret;
		const char *brklabel;
		struct ast_declaration decl;
		struct ast_assign assign;
		struct call_e call;
	} stmt;
};

struct ast_elseif {
	struct ast_expr cond;
	struct ast_block blk;
};

struct ast_if {
	struct ast_expr cond;
	struct ast_block blk;
	struct ast_elseif *alts;
	struct ast_block *alt;
	int altsz, altlen;
};

struct ast_for {
	const char *label;
	struct ast_assignment *init;
	struct ast_expr cond;
	struct ast_assignment *action;
	struct ast_block blk;
};

enum control {
	CTL_IF,
	CTL_FOR,
};

struct ast_control {
	enum control kind;
	union {
		struct ast_if branch;
		struct ast_for loop;
	} ctl;
};

enum command {
	CMD_BLOCK,
	CMD_STATEMENT,
	CMD_CONTROL,
};

struct ast_cmd {
	enum command kind;
	union {
		struct ast_block blk;
		struct ast_statement stmt;
		struct ast_control ctl;
	} cmd;
};

struct ast_func {
	bool exported;
	const char *name;
	struct type ret;
	struct ast_param *params;
	int paramsz, paramlen;
	struct ast_block block;
};

enum const_global {
	CST_TYPE,
	CST_BOOL,
	CST_NUMBER,
	CST_STRING,
};

struct ast_const_global {
	const char *name;
	enum const_global kind;
	union {
		struct type type;
		bool b;
		struct number num;
		char *str;
	} value;
};

struct ast_import {
	struct path path;
	const char *name;
};

enum toplevel {
	TOP_EXTERNFUNC,
	TOP_FUNC,
	TOP_CONST,
};

struct ast_toplevel {
	enum toplevel kind;
	union {
		struct ast_externfunc *extfn;
		struct ast_func *function;
		struct ast_const_global *constant;
	} decl;
};

struct ast_unit {
	struct ast_import *imports;
	struct ast_toplevel *tops;
	int impsz, implen;
	int topsz, toplen;
};

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

#endif