Cthulhu  0.2.10
Cthulhu compiler collection
ast.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #pragma once
4 
5 #include "core/where.h"
6 
7 #include "cthulhu/tree/ops.h"
8 
9 #include <gmp.h>
10 
11 #include <stdbool.h>
12 
13 typedef struct obr_t obr_t;
14 typedef struct scan_t scan_t;
15 typedef struct node_t node_t;
16 typedef struct vector_t vector_t;
17 
21 
22 typedef enum obr_visibility_t
23 {
24 #define OBR_VIS(ID, STR, SYMBOL) ID,
25 #include "oberon/oberon.inc"
27 
28 typedef struct obr_symbol_t
29 {
32 
34  char *name;
35 } obr_symbol_t;
36 
40 
41 typedef enum obr_kind_t
42 {
43  /* types */
50 
51  /* exprs */
54 
58 
65 
66  /* stmts */
75 
76  /* decls */
81 
82  /* other */
86 
87  /* modules */
91 
92 typedef struct obr_t
93 {
95  const node_t *node;
96 
97  union {
98  struct {
99  union {
100  /* eObrExprField */
101  char *field;
102 
103  /* eObrExprUnary */
105 
106  /* eObrExprCast */
108 
109  /* eObrExprCall */
110  const vector_t *args;
111  };
112 
113  /* eObrStmtReturn */
115  };
116 
117  /* eObrStmtWhile */
118  struct {
121  };
122 
123  /* eObrStmtLoop */
125 
126  /* eObrStmtRepeat */
127  struct {
130  };
131 
132  /* eObrExprBinary|eObrExprCompare|eObrExprIn|eObrExprIs */
133  struct {
134  union {
137  };
138 
141  };
142 
143  /* eObrStmtAssign */
144  struct {
147  };
148 
149  /* eObrStmtBlock */
151 
152  /* eObrStmtBranch */
153  struct {
157  };
158 
159  /* eObrTypePointer */
161 
162  /* eObrTypeArray */
163  struct {
164  const vector_t *sizes;
166  };
167 
168  /* eObrTypeRecord */
170 
171  /* eObrExprDigit */
172  mpz_t digit;
173 
174  /* eObrExprName */
175  char *object;
176 
177  /* eObrExprString */
178  struct {
179  char *text;
180  size_t length;
181  };
182 
183  struct {
184  char *name;
186 
187  union {
188  /* eObrModule */
189  struct {
193  };
194 
195  /* eObrDeclVar|eObrDeclType|eObrReceiver|eObrParam */
196  struct {
197  bool mut;
199  };
200 
201  /* eObrDeclConst */
203 
204  /* eObrDeclProcedure|eObrTypeSignature */
205  struct {
207  const vector_t *params;
209 
212  };
213 
214  /* eObrImport|eObrTypeQual */
215  char *symbol;
216  };
217  };
218  };
219 } obr_t;
220 
221 /* modules */
222 
223 obr_t *obr_module(scan_t *scan, where_t where, char *name, char *end, const vector_t *imports,
224  vector_t *decls, vector_t *init);
225 obr_t *obr_import(scan_t *scan, where_t where, char *name, char *symbol);
226 
227 /* decls */
228 
229 obr_t *obr_decl_type(scan_t *scan, where_t where, obr_symbol_t *symbol, obr_t *type);
230 obr_t *obr_decl_var(obr_symbol_t *symbol, obr_t *type);
231 obr_t *obr_decl_const(scan_t *scan, where_t where, obr_symbol_t *symbol, obr_t *value);
232 
233 obr_t *obr_decl_procedure(scan_t *scan, where_t where, obr_symbol_t *symbol, obr_t *receiver,
234  const vector_t *params, obr_t *result, vector_t *locals, vector_t *body,
235  char *end);
236 
237 /* exprs */
238 
239 obr_t *obr_expr_name(scan_t *scan, where_t where, char *name);
240 obr_t *obr_expr_field(scan_t *scan, where_t where, obr_t *expr, char *field);
241 
242 obr_t *obr_expr_call(scan_t *scan, where_t where, obr_t *expr, const vector_t *args);
243 
244 obr_t *obr_expr_cast(scan_t *scan, where_t where, obr_t *expr, obr_t *cast);
245 obr_t *obr_expr_is(scan_t *scan, where_t where, obr_t *lhs, obr_t *rhs);
246 obr_t *obr_expr_in(scan_t *scan, where_t where, obr_t *lhs, obr_t *rhs);
247 
248 obr_t *obr_expr_compare(scan_t *scan, where_t where, compare_t op, obr_t *lhs, obr_t *rhs);
249 obr_t *obr_expr_binary(scan_t *scan, where_t where, binary_t op, obr_t *lhs, obr_t *rhs);
250 obr_t *obr_expr_unary(scan_t *scan, where_t where, unary_t op, obr_t *expr);
251 
252 obr_t *obr_expr_digit(scan_t *scan, where_t where, const mpz_t digit);
253 obr_t *obr_expr_string(scan_t *scan, where_t where, char *text, size_t length);
254 
255 /* stmts */
256 
257 obr_t *obr_stmt_return(scan_t *scan, where_t where, obr_t *expr);
258 obr_t *obr_stmt_while(scan_t *scan, where_t where, obr_t *cond, vector_t *then);
259 obr_t *obr_stmt_loop(scan_t *scan, where_t where, vector_t *loop);
260 obr_t *obr_stmt_repeat(scan_t *scan, where_t where, vector_t *repeat, obr_t *until);
261 obr_t *obr_stmt_assign(scan_t *scan, where_t where, obr_t *dst, obr_t *src);
262 obr_t *obr_stmt_block(scan_t *scan, where_t where, vector_t *stmts);
263 
264 obr_t *obr_stmt_branch(scan_t *scan, where_t where, obr_t *cond, vector_t *then, obr_t *other);
265 
266 obr_t *obr_stmt_break(scan_t *scan, where_t where);
267 
268 /* types */
269 
270 obr_t *obr_type_name(scan_t *scan, where_t where, char *symbol);
271 obr_t *obr_type_qual(scan_t *scan, where_t where, char *name, char *symbol);
272 obr_t *obr_type_pointer(scan_t *scan, where_t where, obr_t *type);
273 obr_t *obr_type_array(scan_t *scan, where_t where, const vector_t *sizes, obr_t *type);
274 obr_t *obr_type_record(scan_t *scan, where_t where, vector_t *fields);
275 
276 /* extras */
277 
278 obr_t *obr_field(obr_symbol_t *symbol, obr_t *type);
279 obr_t *obr_param(obr_symbol_t *symbol, obr_t *type, bool mut);
280 obr_t *obr_receiver(scan_t *scan, where_t where, bool mut, char *name, char *type);
281 
282 /* partial symbols */
283 
284 obr_symbol_t *obr_symbol(scan_t *scan, where_t where, char *name, obr_visibility_t visibility);
285 
286 vector_t *obr_expand_vars(scan_t *scan, vector_t *symbols, obr_t *type);
287 vector_t *obr_expand_fields(scan_t *scan, vector_t *symbols, obr_t *type);
288 vector_t *obr_expand_params(scan_t *scan, vector_t *symbols, obr_t *type, bool mut);
binary_t
all binary operators
Definition: ops.h:32
unary_t
all unary operators
Definition: ops.h:48
compare_t
all comparison operators
Definition: ops.h:40
obr_t * obr_param(obr_symbol_t *symbol, obr_t *type, bool mut)
Definition: ast.c:334
obr_t * obr_expr_digit(scan_t *scan, where_t where, const mpz_t digit)
Definition: ast.c:210
obr_t * obr_import(scan_t *scan, where_t where, char *name, char *symbol)
Definition: ast.c:85
obr_t * obr_expr_cast(scan_t *scan, where_t where, obr_t *expr, obr_t *cast)
Definition: ast.c:152
obr_t * obr_type_name(scan_t *scan, where_t where, char *symbol)
Definition: ast.c:288
obr_t * obr_expr_name(scan_t *scan, where_t where, char *name)
Definition: ast.c:137
obr_symbol_t * obr_symbol(scan_t *scan, where_t where, char *name, obr_visibility_t visibility)
Definition: ast.c:352
obr_t * obr_stmt_block(scan_t *scan, where_t where, vector_t *stmts)
Definition: ast.c:270
obr_t * obr_decl_type(scan_t *scan, where_t where, obr_symbol_t *symbol, obr_t *type)
Definition: ast.c:94
obr_t * obr_expr_string(scan_t *scan, where_t where, char *text, size_t length)
Definition: ast.c:217
vector_t * obr_expand_fields(scan_t *scan, vector_t *symbols, obr_t *type)
Definition: ast.c:384
obr_t * obr_decl_const(scan_t *scan, where_t where, obr_symbol_t *symbol, obr_t *value)
Definition: ast.c:108
obr_t * obr_type_qual(scan_t *scan, where_t where, char *name, char *symbol)
Definition: ast.c:295
obr_t * obr_field(obr_symbol_t *symbol, obr_t *type)
Definition: ast.c:327
obr_t * obr_stmt_loop(scan_t *scan, where_t where, vector_t *loop)
Definition: ast.c:242
obr_t * obr_stmt_return(scan_t *scan, where_t where, obr_t *expr)
Definition: ast.c:227
obr_t * obr_type_record(scan_t *scan, where_t where, vector_t *fields)
Definition: ast.c:318
obr_t * obr_stmt_repeat(scan_t *scan, where_t where, vector_t *repeat, obr_t *until)
Definition: ast.c:254
obr_t * obr_stmt_assign(scan_t *scan, where_t where, obr_t *dst, obr_t *src)
Definition: ast.c:262
obr_visibility_t
Definition: ast.h:23
obr_t * obr_expr_call(scan_t *scan, where_t where, obr_t *expr, const vector_t *args)
Definition: ast.c:160
obr_t * obr_expr_is(scan_t *scan, where_t where, obr_t *lhs, obr_t *rhs)
Definition: ast.c:168
obr_t * obr_type_array(scan_t *scan, where_t where, const vector_t *sizes, obr_t *type)
Definition: ast.c:310
obr_t * obr_expr_compare(scan_t *scan, where_t where, compare_t op, obr_t *lhs, obr_t *rhs)
Definition: ast.c:184
obr_t * obr_expr_unary(scan_t *scan, where_t where, unary_t op, obr_t *expr)
Definition: ast.c:202
obr_t * obr_expr_in(scan_t *scan, where_t where, obr_t *lhs, obr_t *rhs)
Definition: ast.c:176
obr_t * obr_expr_field(scan_t *scan, where_t where, obr_t *expr, char *field)
Definition: ast.c:144
vector_t * obr_expand_vars(scan_t *scan, vector_t *symbols, obr_t *type)
Definition: ast.c:379
obr_t * obr_decl_procedure(scan_t *scan, where_t where, obr_symbol_t *symbol, obr_t *receiver, const vector_t *params, obr_t *result, vector_t *locals, vector_t *body, char *end)
Definition: ast.c:115
obr_t * obr_stmt_while(scan_t *scan, where_t where, obr_t *cond, vector_t *then)
Definition: ast.c:234
obr_t * obr_decl_var(obr_symbol_t *symbol, obr_t *type)
Definition: ast.c:101
obr_t * obr_expr_binary(scan_t *scan, where_t where, binary_t op, obr_t *lhs, obr_t *rhs)
Definition: ast.c:193
obr_kind_t
Definition: ast.h:42
@ eObrExprCall
Definition: ast.h:57
@ eObrStmtLoop
Definition: ast.h:69
@ eObrExprIn
Definition: ast.h:63
@ eObrExprDigit
Definition: ast.h:52
@ eObrTypeQual
Definition: ast.h:45
@ eObrTypeArray
Definition: ast.h:47
@ eObrStmtBlock
Definition: ast.h:72
@ eObrExprCast
Definition: ast.h:64
@ eObrExprBinary
Definition: ast.h:60
@ eObrExprCompare
Definition: ast.h:61
@ eObrExprField
Definition: ast.h:56
@ eObrExprName
Definition: ast.h:55
@ eObrTypeRecord
Definition: ast.h:48
@ eObrDeclVar
Definition: ast.h:77
@ eObrExprUnary
Definition: ast.h:59
@ eObrTypePointer
Definition: ast.h:46
@ eObrReceiver
Definition: ast.h:85
@ eObrImport
Definition: ast.h:89
@ eObrDeclConst
Definition: ast.h:78
@ eObrStmtWhile
Definition: ast.h:68
@ eObrDeclProcedure
Definition: ast.h:80
@ eObrTypeSignature
Definition: ast.h:49
@ eObrDeclType
Definition: ast.h:79
@ eObrStmtBranch
Definition: ast.h:73
@ eObrExprString
Definition: ast.h:53
@ eObrExprIs
Definition: ast.h:62
@ eObrStmtBreak
Definition: ast.h:74
@ eObrModule
Definition: ast.h:88
@ eObrParam
Definition: ast.h:84
@ eObrStmtAssign
Definition: ast.h:71
@ eObrStmtRepeat
Definition: ast.h:70
@ eObrStmtReturn
Definition: ast.h:67
@ eObrField
Definition: ast.h:83
@ eObrTypeName
Definition: ast.h:44
obr_t * obr_stmt_break(scan_t *scan, where_t where)
Definition: ast.c:249
obr_t * obr_type_pointer(scan_t *scan, where_t where, obr_t *type)
Definition: ast.c:303
vector_t * obr_expand_params(scan_t *scan, vector_t *symbols, obr_t *type, bool mut)
Definition: ast.c:389
obr_t * obr_stmt_branch(scan_t *scan, where_t where, obr_t *cond, vector_t *then, obr_t *other)
Definition: ast.c:277
obr_t * obr_receiver(scan_t *scan, where_t where, bool mut, char *name, char *type)
Definition: ast.c:342
obr_t * obr_module(scan_t *scan, where_t where, char *name, char *end, const vector_t *imports, vector_t *decls, vector_t *init)
Definition: ast.c:72
a position in a source file
Definition: node.h:23
where_t where
Definition: ast.h:31
obr_visibility_t visibility
Definition: ast.h:33
scan_t * scan
Definition: ast.h:30
char * name
Definition: ast.h:34
Definition: ast.h:93
obr_t * array_element
Definition: ast.h:165
vector_t * then
Definition: ast.h:120
obr_kind_t kind
Definition: ast.h:94
obr_t * dst
Definition: ast.h:145
obr_t * result
Definition: ast.h:208
obr_t * until
Definition: ast.h:129
unary_t unary
Definition: ast.h:104
obr_t * lhs
Definition: ast.h:139
const vector_t * args
Definition: ast.h:110
obr_t * cond
Definition: ast.h:119
char * name
Definition: ast.h:184
vector_t * decls
Definition: ast.h:191
const node_t * node
Definition: ast.h:95
bool mut
Definition: ast.h:197
const vector_t * params
Definition: ast.h:207
obr_t * type
Definition: ast.h:198
vector_t * body
Definition: ast.h:211
char * text
Definition: ast.h:179
mpz_t digit
Definition: ast.h:172
vector_t * init
Definition: ast.h:192
obr_t * branch
Definition: ast.h:154
vector_t * branch_body
Definition: ast.h:155
binary_t binary
Definition: ast.h:136
obr_t * expr
Definition: ast.h:114
vector_t * fields
Definition: ast.h:169
obr_t * src
Definition: ast.h:146
vector_t * repeat
Definition: ast.h:128
char * object
Definition: ast.h:175
char * symbol
Definition: ast.h:215
vector_t * stmts
Definition: ast.h:150
size_t length
Definition: ast.h:180
vector_t * locals
Definition: ast.h:210
obr_t * cast
Definition: ast.h:107
const vector_t * sizes
Definition: ast.h:164
compare_t compare
Definition: ast.h:135
vector_t * loop
Definition: ast.h:124
obr_t * value
Definition: ast.h:202
obr_t * branch_else
Definition: ast.h:156
obr_t * receiver
Definition: ast.h:206
char * field
Definition: ast.h:101
const vector_t * imports
Definition: ast.h:190
obr_visibility_t visibility
Definition: ast.h:185
obr_t * pointer
Definition: ast.h:160
obr_t * rhs
Definition: ast.h:140
a source file scanner
Definition: scan.h:24
a generic vector of pointers
Definition: vector.c:16
a location inside a scanner locations are inclusive and 0-based
Definition: where.h:23