11 node->
type = PS_NODE_LET;
24 struct ps_node_block* body) {
26 node->
type = PS_NODE_FN;
39 for (
usize i = 0; i < self->length; i++) {
43 ps_token_print(&self->contents[i], 0);
49 node->
type = PS_NODE_IMPORT;
58 struct ps_type_tuple* sig) {
60 node->
type = PS_NODE_EXTERN;
71 node->
type = PS_NODE_EXPR_STM;
81 node->
type = PS_NODE_BLOCK;
89 struct ps_node_fn_arr* methods) {
91 node->
type = PS_NODE_STRUCT;
101 struct ps_node_fn_arr* methods) {
103 node->
type = PS_NODE_ENUM;
115 node->
type = PS_NODE_FOR;
127 struct ps_node_block* body) {
129 node->
type = PS_NODE_WHILE;
139 struct ps_node_block* else_body) {
141 node->
type = PS_NODE_IF;
153 node->
type = PS_NODE_RETURN;
163 printf(
"let %s%s", self->is_mutable ?
"mut " :
"", self->name->start);
166 ps_type_print(self->type, 0);
169 ps_expr_print(self->value, 0);
186 printf(
"fn %s", self->name->start);
187 ps_type_field_arr_print(self->params, 0);
188 if (self->ret_type) {
190 ps_type_print(self->ret_type, 0);
193 ps_node_block_print(self->body, 0, indent);
199 for (
usize i = 0; i < self->name->length; i++) {
201 printf(
"%s", $arr(self->name)[i]->start);
207 printf(
"extern %s", self->name->start);
208 ps_type_tuple_print(self->sig, 0);
213 ps_expr_print(self->expr, 0);
218 for (
size_t i = 0; i < self->length; i++) {
250 ps_expr_print(self->cond, 0);
252 ps_node_block_print(self->body, 0, indent);
258 ps_expr_print(self->cond, 0);
260 ps_node_block_print(self->body, 0, indent);
261 if (self->else_body) {
263 ps_node_block_print(self->else_body, 0, indent);
272 ps_expr_print(self->value, 0);
277 switch (self->type) {
279 ps_node_let_print(&self->value.let, 0, indent);
282 ps_node_fn_print(&self->value.fn, 0, indent);
285 ps_node_import_print(&self->value.import, 0, indent);
288 ps_node_extern_print(&self->value.extern_, 0, indent);
290 case PS_NODE_EXPR_STM:
291 ps_node_expr_stm_print(&self->value.expr_stm, 0, indent);
295 ps_node_block_print(self->value.block, 0, indent);
298 ps_node_struct_print(&self->value.struct_, 0, indent);
301 ps_node_enum_print(&self->value.enum_, 0, indent);
304 ps_node_for_print(&self->value.for_, 0, indent);
307 ps_node_while_print(&self->value.while_, 0, indent);
310 ps_node_if_print(&self->value.if_, 0, indent);
313 ps_node_return_print(&self->value.return_, 0, indent);
320 ps_abort(
"you're missing a case here");
341 lit->
type = PS_EXPR_LIT_U64;
354 lit->
type = PS_EXPR_LIT_I64;
367 lit->
type = PS_EXPR_LIT_F64;
380 lit->
type = PS_EXPR_LIT_BOOL;
393 lit->
type = PS_EXPR_LIT_STR;
406 lit->
type = PS_EXPR_LIT_INTERP_STR;
419 lit->
type = PS_EXPR_LIT_UNIT;
452 struct ps_expr_arr* args) {
466 for (
usize i = 0; i < self->length; i++) {
467 struct ps_expr* part = self->contents[i];
473 ps_expr_print(part,
')');
480 switch (self->type) {
481 case PS_EXPR_LIT_U64:
482 printf(
"%llu", self->value.uint_val);
484 case PS_EXPR_LIT_I64:
485 printf(
"%lld", self->value.int_val);
487 case PS_EXPR_LIT_F64:
488 printf(
"%f", self->value.float_val);
490 case PS_EXPR_LIT_BOOL:
491 printf(
"%s", self->value.bool_val ?
"true" :
"false");
493 case PS_EXPR_LIT_STR:
494 printf(
"\"%s\"", self->value.str_val);
496 case PS_EXPR_LIT_INTERP_STR:
497 ps_interp_str_print(self->value.interp_str_val, 0);
499 case PS_EXPR_LIT_UNIT:
509 ps_expr_print(self->lhs, 0);
510 printf(
" %s ", self->op->start);
511 ps_expr_print(self->rhs, 0);
516 printf(
"%s ", self->op->start);
517 ps_expr_print(self->rhs, 0);
521 printf(
"%s(", self->callee->start);
522 for (
usize i = 0; i < self->args->length; i++) {
526 ps_expr_print(self->args->contents[i], 0);
532 switch (self->expr_type) {
533 case PS_EXPR_ID_EXPR:
534 ps_expr_id_print(&self->value.id_expr, 0);
536 case PS_EXPR_LIT_EXPR:
537 ps_expr_lit_print(&self->value.lit_expr, 0);
539 case PS_EXPR_BIN_EXPR:
540 ps_expr_binary_print(&self->value.bin_expr, 0);
542 case PS_EXPR_UN_EXPR:
543 ps_expr_unary_print(&self->value.un_expr, 0);
545 case PS_EXPR_CALL_EXPR:
546 ps_expr_call_print(&self->value.call_expr, 0);
#define ps_abort(msg)
Aborts the program with the given message.
#define PS_NO_IMPL()
Aborts the program due to a function taking a path not yet implemented.
Defines an arena allocator for the compiler.
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).
struct ps_expr * ps_expr_unary_new(struct ps_token *op, struct ps_expr *rhs)
Creates a new unary expression.
struct ps_expr * ps_expr_lit_new_unit(struct ps_token *token)
Creates a new literal expression for a unit literal.
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).
struct ps_node * ps_node_return_new(struct ps_expr *value)
Creates a new return statement that returns the expression value.
struct ps_expr * ps_expr_lit_new_bool(bool value, struct ps_token *token)
Creates a new literal expression for a boolean.
struct ps_node * ps_node_struct_new(struct ps_type_struct *type, struct ps_node_fn_arr *methods)
Creates a new structure declaration.
struct ps_node * ps_node_extern_new(struct ps_token *name, struct ps_type_tuple *sig)
struct ps_node * ps_node_enum_new(struct ps_type_enum *type, struct ps_node_fn_arr *methods)
Creates a new enum declaration.
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.
struct ps_node * ps_node_expr_stm_new(struct ps_expr *expr)
Creates a new expression statement for expr.
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).
struct ps_expr * ps_expr_call_new(struct ps_token *callee, struct ps_expr_arr *args)
Creates a new call expression.
struct ps_expr * ps_expr_lit_new_str(STR value, struct ps_token *token)
Creates a new literal expression for a string.
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.
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.
struct ps_node * ps_node_block_stm_new(struct ps_node_block *block)
Creates a new block statement.
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.
struct ps_expr * ps_expr_binary_new(struct ps_expr *lhs, struct ps_token *op, struct ps_expr *rhs)
Creates a new binary expression.
struct ps_node * ps_node_while_new(struct ps_expr *cond, struct ps_node_block *body)
Creates a new while loop.
struct ps_expr * ps_expr_lit_new_interp_str(struct ps_interp_str *value)
Creates a new literal expression for an interpolated string.
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.
ps_fn_qualifiers
Function qualifier flags.
@ PS_FN_INLINE
Inlined everywhere.
@ PS_FN_PUBLIC
Visible outside the module.
@ PS_FN_PRIVATE
Hidden outside the module.
#define PS_IPRINT_IMPL(T,...)
#define ps_print_indent()
#define PS_PRINT_IMPL(T,...)
struct ps_expr_arr * args
union ps_expr_lit::ps_expr_lit_value value
enum ps_expr_lit::ps_expr_lit_type type
struct ps_token * token
A token associated with the expression literal.
Expression nodes are represented with tagged unions.
enum ps_expr::ps_expr_type expr_type
struct ps_expr_lit lit_expr
struct ps_expr_binary bin_expr
struct ps_expr_call call_expr
struct ps_expr_id id_expr
struct ps_expr_unary un_expr
struct ps_type_enum * type
struct ps_node_fn_arr * methods
A reference to an external C function.
struct ps_type_tuple * sig
The type signature of the function.
struct ps_token * name
The name of the function.
struct ps_type * ret_type
struct ps_node_block * body
struct ps_type_field_arr * params
enum ps_fn_qualifiers qualifiers
struct ps_node_expr_stm * step
struct ps_node_let * init
struct ps_node_block * body
struct ps_node_block * else_body
struct ps_node_block * body
Parses "let `name`: `type` = `value`".
struct ps_node_fn_arr * methods
struct ps_type_struct * type
struct ps_node_block * body
AST nodes are represented with tagged unions.
struct ps_node_while while_
struct ps_node_extern extern_
struct ps_node_enum enum_
struct ps_node_block * block
struct ps_node_struct struct_
struct ps_node_expr_stm expr_stm
struct ps_node_return return_
enum ps_node::ps_node_type type
struct ps_interp_str * interp_str_val