libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
ast.h
Go to the documentation of this file.
1
8#pragma once
9
10#include "def.h"
11#include "enum/make.h"
14#include "util/dynarr.h"
15#include "util/print.h"
16
17// TODO: make tuple dynarr
18// TODO: add switch statement
19
20struct ps_node;
21struct ps_expr;
22struct ps_node_block;
23struct ps_interp_str;
24struct ps_expr_arr;
25
30struct ps_name ps_dynarr(struct ps_token*);
31#define ps_name_new() ps_dynarr_new(struct ps_name)
32#define ps_name_free(arr) ps_dynarr_free(arr)
33
35
41 struct ps_token* name;
43 struct ps_type* type;
44 struct ps_expr* value;
45};
46
53 PS_FN_PUBLIC = 1 << 0,
54 PS_FN_PRIVATE = 1 << 1,
55 PS_FN_STATIC = 1 << 2,
56 PS_FN_INLINE = 1 << 3
57};
58
59// TODO: add generic stuff (this is also just more general)
60struct ps_node_fn {
61 struct ps_token* name;
63 struct ps_type_field_arr* params;
65 struct ps_node_block* body;
66};
67
72struct ps_node_fn_arr ps_dynarr(struct ps_node_fn);
73#define ps_node_fn_arr_new() ps_dynarr_new(struct ps_node_fn_arr)
74#define ps_node_fn_arr_free(arr) ps_dynarr_free(arr)
75
77 struct ps_name* name;
78};
79
82 struct ps_token* name;
83 struct ps_type_tuple* sig;
84};
85
87 struct ps_expr* expr;
88};
89
92 struct ps_node_fn_arr* methods;
93};
94
97 struct ps_node_fn_arr* methods;
98};
99
102 struct ps_expr* cond;
104 struct ps_node_block* body;
105};
106
108 struct ps_expr* cond;
109 struct ps_node_block* body;
110};
111
113 struct ps_expr* cond;
114 struct ps_node_block* body;
115 struct ps_node_block* else_body;
116};
117
119 struct ps_expr* value;
120};
121
124 struct ps_token* name;
125};
126
138#define ENUM TO_ENUM
139#include "lit_type.h"
140#undef ENUM
142
145
155 struct ps_interp_str* interp_str_val;
156 /* unit literal handled just by being a case in ps_node_lit_expr::type
157 */
159};
160
163 struct ps_expr* lhs;
164 struct ps_token* op;
165 struct ps_expr* rhs;
166};
167
170 struct ps_token* op;
171 struct ps_expr* rhs;
172};
173
176 // TODO: turn this into expr
177 // struct ps_expr* callee;
179 struct ps_expr_arr* args;
180};
181
187struct ps_expr {
189#define ENUM TO_ENUM
190#include "expr_type.h"
191#undef ENUM
193 union {
200 struct ps_type* type;
201};
202
206struct ps_node {
208#define ENUM TO_ENUM
209#include "node_type.h"
210#undef ENUM
212 union {
218 struct ps_node_block* block;
219
227};
228
232struct ps_node_arr ps_dynarr(struct ps_node*);
233#define ps_node_arr_new() ps_dynarr_new(struct ps_node_arr)
234#define ps_node_arr_free(arr) ps_dynarr_free(arr)
235
239struct ps_node_block ps_dynarr(struct ps_node*);
240#define ps_node_block_new() ps_dynarr_new(struct ps_node_block)
241#define ps_node_block_free(arr) ps_dynarr_free(arr)
242
247struct ps_interp_str ps_dynarr(struct ps_expr*);
248#define ps_interp_str_new() ps_dynarr_new(struct ps_interp_str)
249#define ps_interp_str_free(arr) ps_dynarr_free(arr)
250
251PS_PRINT_DECL(ps_interp_str);
252
257struct ps_expr_arr ps_dynarr(struct ps_expr*);
258#define ps_expr_arr_new() ps_dynarr_new(struct ps_expr_arr)
259#define ps_expr_arr_free(arr) ps_dynarr_free(arr)
260
261// nodes
262
269struct ps_node* ps_node_let_new(struct ps_token* name, bool is_mutable,
270 struct ps_type* type, struct ps_expr* value);
271
281struct ps_node* ps_node_fn_new(struct ps_token* name, struct ps_type* ret_type,
282 struct ps_type_field_arr* params, enum ps_fn_qualifiers qualifiers,
283 struct ps_node_block* body);
284
289struct ps_node* ps_node_import_new(struct ps_name* name);
290
298struct ps_node* ps_node_extern_new(struct ps_token* name,
299 struct ps_type_tuple* sig);
300
304struct ps_node* ps_node_expr_stm_new(struct ps_expr* expr);
305
309struct ps_node* ps_node_block_stm_new(struct ps_node_block* block);
310
317 struct ps_node_fn_arr* methods);
318
325 struct ps_node_fn_arr* methods);
326
334struct ps_node* ps_node_for_new(struct ps_node_let* init, struct ps_expr* cond,
335 struct ps_node_expr_stm* step, struct ps_node_block* body);
336
342struct ps_node* ps_node_while_new(struct ps_expr* cond,
343 struct ps_node_block* body);
344
349struct ps_node* ps_node_if_new(struct ps_expr* cond, struct ps_node_block* body,
350 struct ps_node_block* else_body);
351
356struct ps_node* ps_node_return_new(struct ps_expr* value);
357
376PS_IPRINT_DECL(ps_node_block);
378
379// exprs
380
384struct ps_expr* ps_expr_id_new(struct ps_token* name);
385
389struct ps_expr* ps_expr_lit_new_i64(i64 value, struct ps_token* token);
390
395struct ps_expr* ps_expr_lit_new_u64(u64 value, struct ps_token* token);
396
401struct ps_expr* ps_expr_lit_new_f64(f64 value, struct ps_token* token);
402
406struct ps_expr* ps_expr_lit_new_bool(bool value, struct ps_token* token);
407
411struct ps_expr* ps_expr_lit_new_str(STR value, struct ps_token* token);
412
416struct ps_expr* ps_expr_lit_new_interp_str(struct ps_interp_str* value);
417
421struct ps_expr* ps_expr_lit_new_unit(struct ps_token* token);
422
429struct ps_expr* ps_expr_binary_new(struct ps_expr* lhs, struct ps_token* op,
430 struct ps_expr* rhs);
431
437struct ps_expr* ps_expr_unary_new(struct ps_token* op, struct ps_expr* rhs);
438
444struct ps_expr* ps_expr_call_new(struct ps_token* callee,
445 struct ps_expr_arr* args);
446
struct ps_node * ps_node_extern_new(struct ps_token *name, struct ps_type_tuple *sig)
Definition ast.c:57
struct ps_expr * ps_expr_lit_new_f64(f64 value, struct ps_token *token)
Creates a new literal expression for a floating point number (of at most 64 bits).
Definition ast.c:361
struct ps_expr * ps_expr_unary_new(struct ps_token *op, struct ps_expr *rhs)
Creates a new unary expression.
Definition ast.c:439
struct ps_expr * ps_expr_lit_new_unit(struct ps_token *token)
Creates a new literal expression for a unit literal.
Definition ast.c:413
struct ps_expr * ps_expr_lit_new_u64(u64 value, struct ps_token *token)
Creates a new literal expression for an unsigned integer (of at most 64 bits).
Definition ast.c:335
struct ps_node * ps_node_return_new(struct ps_expr *value)
Creates a new return statement that returns the expression value.
Definition ast.c:151
struct ps_expr * ps_expr_lit_new_bool(bool value, struct ps_token *token)
Creates a new literal expression for a boolean.
Definition ast.c:374
struct ps_node * ps_node_struct_new(struct ps_type_struct *type, struct ps_node_fn_arr *methods)
Creates a new structure declaration.
Definition ast.c:88
struct ps_node * ps_node_enum_new(struct ps_type_enum *type, struct ps_node_fn_arr *methods)
Creates a new enum declaration.
Definition ast.c:100
struct ps_node * ps_node_let_new(struct ps_token *name, bool is_mutable, struct ps_type *type, struct ps_expr *value)
Creates a new let statement initializing the variable name to the expression value.
Definition ast.c:8
struct ps_node * ps_node_expr_stm_new(struct ps_expr *expr)
Creates a new expression statement for expr.
Definition ast.c:69
struct ps_expr * ps_expr_lit_new_i64(i64 value, struct ps_token *token)
Creates a new literal expression for a signed integer (of at most 64 bits).
Definition ast.c:348
struct ps_expr * ps_expr_call_new(struct ps_token *callee, struct ps_expr_arr *args)
Creates a new call expression.
Definition ast.c:451
struct ps_expr * ps_expr_lit_new_str(STR value, struct ps_token *token)
Creates a new literal expression for a string.
Definition ast.c:387
struct ps_node * ps_node_fn_new(struct ps_token *name, struct ps_type *ret_type, struct ps_type_field_arr *params, enum ps_fn_qualifiers qualifiers, struct ps_node_block *body)
Creates a new function with the given name name, parameters params, and function body body.
Definition ast.c:22
struct ps_expr * ps_expr_id_new(struct ps_token *name)
Creates a new id expression.
struct ps_node * ps_node_import_new(struct ps_name *name)
Creates a new import statement.
struct ps_node * ps_node_for_new(struct ps_node_let *init, struct ps_expr *cond, struct ps_node_expr_stm *step, struct ps_node_block *body)
Creates a new for loop.
Definition ast.c:112
struct ps_node * ps_node_block_stm_new(struct ps_node_block *block)
Creates a new block statement.
Definition ast.c:79
ps_fn_qualifiers
Function qualifier flags.
Definition ast.h:52
@ PS_FN_INLINE
Inlined everywhere.
Definition ast.h:56
@ PS_FN_PUBLIC
Visible outside the module.
Definition ast.h:53
@ PS_FN_PRIVATE
Hidden outside the module.
Definition ast.h:54
@ PS_FN_STATIC
TODO:
Definition ast.h:55
struct ps_node * ps_node_if_new(struct ps_expr *cond, struct ps_node_block *body, struct ps_node_block *else_body)
Creates a new if statement on condition cond.
Definition ast.c:138
struct ps_expr * ps_expr_binary_new(struct ps_expr *lhs, struct ps_token *op, struct ps_expr *rhs)
Creates a new binary expression.
Definition ast.c:425
struct ps_node * ps_node_while_new(struct ps_expr *cond, struct ps_node_block *body)
Creates a new while loop.
Definition ast.c:126
struct ps_expr * ps_expr_lit_new_interp_str(struct ps_interp_str *value)
Creates a new literal expression for an interpolated string.
Definition ast.c:400
Base definitions.
#define f64
Definition def.h:52
#define STR
Definition def.h:40
#define u64
Definition def.h:48
#define i64
Definition def.h:47
Defines a dynamically-allocated array template.
#define ps_dynarr(T)
Definition dynarr.h:28
Retrieval of associated data from custom enum (see enum.h).
#define PS_IPRINT_DECL(T)
Definition print.h:18
#define PS_PRINT_DECL(T)
Definition print.h:9
A binary operator.
Definition ast.h:162
struct ps_expr * rhs
Definition ast.h:165
struct ps_token * op
Definition ast.h:164
struct ps_expr * lhs
Definition ast.h:163
A function invocation.
Definition ast.h:175
struct ps_token * callee
Definition ast.h:178
struct ps_expr_arr * args
Definition ast.h:179
A variable name.
Definition ast.h:123
struct ps_token * name
Definition ast.h:124
An expression literal.
Definition ast.h:133
union ps_expr_lit::ps_expr_lit_value value
ps_expr_lit_type
Definition ast.h:137
enum ps_expr_lit::ps_expr_lit_type type
struct ps_token * token
A token associated with the expression literal.
Definition ast.h:144
An unary operator.
Definition ast.h:169
struct ps_token * op
Definition ast.h:170
struct ps_expr * rhs
Definition ast.h:171
Expression nodes are represented with tagged unions.
Definition ast.h:187
ps_expr_type
Definition ast.h:188
enum ps_expr::ps_expr_type expr_type
struct ps_expr_lit lit_expr
Definition ast.h:195
struct ps_expr_binary bin_expr
Definition ast.h:196
union ps_expr::@1 value
struct ps_expr_call call_expr
Definition ast.h:198
struct ps_type * type
Definition ast.h:200
struct ps_expr_id id_expr
Definition ast.h:194
struct ps_expr_unary un_expr
Definition ast.h:197
struct ps_type_enum * type
Definition ast.h:96
struct ps_node_fn_arr * methods
Definition ast.h:97
struct ps_expr * expr
Definition ast.h:87
A reference to an external C function.
Definition ast.h:81
struct ps_type_tuple * sig
The type signature of the function.
Definition ast.h:83
struct ps_token * name
The name of the function.
Definition ast.h:82
struct ps_type * ret_type
Definition ast.h:62
struct ps_node_block * body
Definition ast.h:65
struct ps_type_field_arr * params
Definition ast.h:63
enum ps_fn_qualifiers qualifiers
Definition ast.h:64
struct ps_token * name
Definition ast.h:61
struct ps_node_expr_stm * step
Definition ast.h:103
struct ps_node_let * init
Definition ast.h:101
struct ps_node_block * body
Definition ast.h:104
struct ps_expr * cond
Definition ast.h:102
struct ps_expr * cond
Definition ast.h:113
struct ps_node_block * else_body
Definition ast.h:115
struct ps_node_block * body
Definition ast.h:114
struct ps_name * name
Definition ast.h:77
Parses "let `name`: `type` = `value`".
Definition ast.h:40
struct ps_token * name
Definition ast.h:41
bool is_mutable
Definition ast.h:42
struct ps_type * type
Definition ast.h:43
struct ps_expr * value
Definition ast.h:44
struct ps_expr * value
Definition ast.h:119
struct ps_node_fn_arr * methods
Definition ast.h:92
struct ps_type_struct * type
Definition ast.h:91
struct ps_node_block * body
Definition ast.h:109
struct ps_expr * cond
Definition ast.h:108
AST nodes are represented with tagged unions.
Definition ast.h:206
union ps_node::@2 value
struct ps_node_while while_
Definition ast.h:223
struct ps_node_extern extern_
Definition ast.h:216
struct ps_node_if if_
Definition ast.h:224
struct ps_node_enum enum_
Definition ast.h:221
struct ps_node_block * block
Definition ast.h:218
struct ps_node_struct struct_
Definition ast.h:220
struct ps_node_expr_stm expr_stm
Definition ast.h:217
ps_node_type
Definition ast.h:207
struct ps_node_for for_
Definition ast.h:222
struct ps_node_return return_
Definition ast.h:225
struct ps_node_fn fn
Definition ast.h:214
struct ps_node_let let
Definition ast.h:213
struct ps_node_import import
Definition ast.h:215
enum ps_node::ps_node_type type
Represents a token.
Definition token.h:34
Definition type.h:66
Defines a token.
struct ps_interp_str * interp_str_val
Definition ast.h:155