libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
Functions
ast.c File Reference
#include "ast.h"
#include <stdio.h>
#include "util/arena.h"

Go to the source code of this file.

Functions

struct ps_nodeps_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_nodeps_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.
 
 PS_PRINT_IMPL (ps_name, { for(usize i=0;i< self->length;i++) { if(i > 0) { printf("::");} ps_token_print(&self->contents[i], 0);} })
 
struct ps_nodeps_node_extern_new (struct ps_token *name, struct ps_type_tuple *sig)
 
struct ps_nodeps_node_expr_stm_new (struct ps_expr *expr)
 Creates a new expression statement for expr.
 
struct ps_nodeps_node_block_stm_new (struct ps_node_block *block)
 Creates a new block statement.
 
struct ps_nodeps_node_struct_new (struct ps_type_struct *type, struct ps_node_fn_arr *methods)
 Creates a new structure declaration.
 
struct ps_nodeps_node_enum_new (struct ps_type_enum *type, struct ps_node_fn_arr *methods)
 Creates a new enum declaration.
 
struct ps_nodeps_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_nodeps_node_while_new (struct ps_expr *cond, struct ps_node_block *body)
 Creates a new while loop.
 
struct ps_nodeps_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_nodeps_node_return_new (struct ps_expr *value)
 Creates a new return statement that returns the expression value.
 
 PS_IPRINT_IMPL (ps_node_let, { ps_print_indent();printf("let %s%s", self->is_mutable ? "mut " :"", self->name->start);if(self->type) { printf(": ");ps_type_print(self->type, 0);} printf(" = ");ps_expr_print(self->value, 0);})
 
 PS_IPRINT_IMPL (ps_node_import, { ps_print_indent();printf("import ");for(usize i=0;i< self->name->length;i++) { if(i) printf("::");printf("%s", $arr(self->name)[i]->start);} })
 
 PS_IPRINT_IMPL (ps_node_expr_stm, { ps_print_indent();ps_expr_print(self->expr, 0);})
 
 PS_IPRINT_IMPL (ps_node_struct, { ps_print_indent();ps_type_struct_print(self->type, 0, ps_next_indent());})
 
 PS_IPRINT_IMPL (ps_node_for, { ps_print_indent();})
 
 PS_IPRINT_IMPL (ps_node_if, { ps_print_indent();printf("if ");ps_expr_print(self->cond, 0);printf(" ");ps_node_block_print(self->body, 0, indent);if(self->else_body) { printf(" else ");ps_node_block_print(self->else_body, 0, indent);} })
 
 PS_IPRINT_IMPL (ps_node, { switch(self->type) { case PS_NODE_LET:ps_node_let_print(&self->value.let, 0, indent);break;case PS_NODE_FN:ps_node_fn_print(&self->value.fn, 0, indent);break;case PS_NODE_IMPORT:ps_node_import_print(&self->value.import, 0, indent);break;case PS_NODE_EXTERN:ps_node_extern_print(&self->value.extern_, 0, indent);break;case PS_NODE_EXPR_STM:ps_node_expr_stm_print(&self->value.expr_stm, 0, indent);break;case PS_NODE_BLOCK:ps_print_indent();ps_node_block_print(self->value.block, 0, indent);break;case PS_NODE_STRUCT:ps_node_struct_print(&self->value.struct_, 0, indent);break;case PS_NODE_ENUM:ps_node_enum_print(&self->value.enum_, 0, indent);break;case PS_NODE_FOR:ps_node_for_print(&self->value.for_, 0, indent);break;case PS_NODE_WHILE:ps_node_while_print(&self->value.while_, 0, indent);break;case PS_NODE_IF:ps_node_if_print(&self->value.if_, 0, indent);break;case PS_NODE_RETURN:ps_node_return_print(&self->value.return_, 0, indent);break;case PS_NODE_TRAIT:PS_NO_IMPL();case PS_NODE_EXPR:ps_abort("idk what this is");default:ps_abort("you're missing a case here");} })
 
struct ps_exprps_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_exprps_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_exprps_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_exprps_expr_lit_new_bool (bool value, struct ps_token *token)
 Creates a new literal expression for a boolean.
 
struct ps_exprps_expr_lit_new_str (STR value, struct ps_token *token)
 Creates a new literal expression for a string.
 
struct ps_exprps_expr_lit_new_interp_str (struct ps_interp_str *value)
 Creates a new literal expression for an interpolated string.
 
struct ps_exprps_expr_lit_new_unit (struct ps_token *token)
 Creates a new literal expression for a unit literal.
 
struct ps_exprps_expr_binary_new (struct ps_expr *lhs, struct ps_token *op, struct ps_expr *rhs)
 Creates a new binary expression.
 
struct ps_exprps_expr_unary_new (struct ps_token *op, struct ps_expr *rhs)
 Creates a new unary expression.
 
struct ps_exprps_expr_call_new (struct ps_token *callee, struct ps_expr_arr *args)
 Creates a new call expression.
 
 PS_PRINT_IMPL (ps_interp_str, { putchar('"'); for (usize i = 0; i < self->length; i++) { struct ps_expr* part = self->contents[i]; if (part->expr_type == PS_EXPR_LIT_EXPR && part->value.lit_expr.type == PS_EXPR_LIT_STR) { printf("%s", part->value.lit_expr.value.str_val); } else { printf("\\‍("); ps_expr_print(part, ')'); } } putchar('"');})
 
 PS_PRINT_IMPL (ps_expr_binary, { putchar('(');ps_expr_print(self->lhs, 0);printf(" %s ", self->op->start);ps_expr_print(self->rhs, 0);putchar(')');})
 
 PS_PRINT_IMPL (ps_expr_call, { printf("%s(", self->callee->start);for(usize i=0;i< self->args->length;i++) { if(i > 0) { printf(", ");} ps_expr_print(self->args->contents[i], 0);} putchar(')');})
 

Function Documentation

◆ ps_expr_binary_new()

struct ps_expr * ps_expr_binary_new ( struct ps_expr lhs,
struct ps_token op,
struct ps_expr rhs 
)

Creates a new binary expression.

Parameters
lhsThe left hand side of the expression.
opThe operator of the expression.
rhsThe right hand side of the expression.

Definition at line 425 of file ast.c.

◆ ps_expr_call_new()

struct ps_expr * ps_expr_call_new ( struct ps_token callee,
struct ps_expr_arr *  args 
)

Creates a new call expression.

Parameters
calleeThe callee of the expression.
argsThe arguments of the expression.

Definition at line 451 of file ast.c.

◆ ps_expr_lit_new_bool()

struct ps_expr * ps_expr_lit_new_bool ( bool  value,
struct ps_token token 
)

Creates a new literal expression for a boolean.

Definition at line 374 of file ast.c.

◆ ps_expr_lit_new_f64()

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 at line 361 of file ast.c.

◆ ps_expr_lit_new_i64()

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 at line 348 of file ast.c.

◆ ps_expr_lit_new_interp_str()

struct ps_expr * ps_expr_lit_new_interp_str ( struct ps_interp_str *  value)

Creates a new literal expression for an interpolated string.

Definition at line 400 of file ast.c.

◆ ps_expr_lit_new_str()

struct ps_expr * ps_expr_lit_new_str ( STR  value,
struct ps_token token 
)

Creates a new literal expression for a string.

Definition at line 387 of file ast.c.

◆ ps_expr_lit_new_u64()

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 at line 335 of file ast.c.

◆ ps_expr_lit_new_unit()

struct ps_expr * ps_expr_lit_new_unit ( struct ps_token token)

Creates a new literal expression for a unit literal.

Definition at line 413 of file ast.c.

◆ ps_expr_unary_new()

struct ps_expr * ps_expr_unary_new ( struct ps_token op,
struct ps_expr rhs 
)

Creates a new unary expression.

Parameters
opThe operator of the expression.
rhsThe right hand side of the expression.

Definition at line 439 of file ast.c.

◆ PS_IPRINT_IMPL() [1/7]

PS_IPRINT_IMPL ( ps_node  ,
{ switch(self->type) { case PS_NODE_LET:ps_node_let_print(&self->value.let, 0, indent);break;case PS_NODE_FN:ps_node_fn_print(&self->value.fn, 0, indent);break;case PS_NODE_IMPORT:ps_node_import_print(&self->value.import, 0, indent);break;case PS_NODE_EXTERN:ps_node_extern_print(&self->value.extern_, 0, indent);break;case PS_NODE_EXPR_STM:ps_node_expr_stm_print(&self->value.expr_stm, 0, indent);break;case PS_NODE_BLOCK:ps_print_indent();ps_node_block_print(self->value.block, 0, indent);break;case PS_NODE_STRUCT:ps_node_struct_print(&self->value.struct_, 0, indent);break;case PS_NODE_ENUM:ps_node_enum_print(&self->value.enum_, 0, indent);break;case PS_NODE_FOR:ps_node_for_print(&self->value.for_, 0, indent);break;case PS_NODE_WHILE:ps_node_while_print(&self->value.while_, 0, indent);break;case PS_NODE_IF:ps_node_if_print(&self->value.if_, 0, indent);break;case PS_NODE_RETURN:ps_node_return_print(&self->value.return_, 0, indent);break;case PS_NODE_TRAIT:PS_NO_IMPL();case PS_NODE_EXPR:ps_abort("idk what this is");default:ps_abort("you're missing a case here");} }   
)

Definition at line 276 of file ast.c.

◆ PS_IPRINT_IMPL() [2/7]

PS_IPRINT_IMPL ( ps_node_expr_stm  ,
{ ps_print_indent();ps_expr_print(self->expr, 0);}   
)

Definition at line 211 of file ast.c.

◆ PS_IPRINT_IMPL() [3/7]

PS_IPRINT_IMPL ( ps_node_for  ,
{ ps_print_indent();}   
)

Definition at line 235 of file ast.c.

◆ PS_IPRINT_IMPL() [4/7]

PS_IPRINT_IMPL ( ps_node_if  ,
{ ps_print_indent();printf("if ");ps_expr_print(self->cond, 0);printf(" ");ps_node_block_print(self->body, 0, indent);if(self->else_body) { printf(" else ");ps_node_block_print(self->else_body, 0, indent);} }   
)

Definition at line 255 of file ast.c.

◆ PS_IPRINT_IMPL() [5/7]

PS_IPRINT_IMPL ( ps_node_import  ,
{ ps_print_indent();printf("import ");for(usize i=0;i< self->name->length;i++) { if(i) printf("::");printf("%s", $arr(self->name)[i]->start);} }   
)

Definition at line 196 of file ast.c.

◆ PS_IPRINT_IMPL() [6/7]

PS_IPRINT_IMPL ( ps_node_let  ,
{ ps_print_indent();printf("let %s%s", self->is_mutable ? "mut " :"", self->name->start);if(self->type) { printf(": ");ps_type_print(self->type, 0);} printf(" = ");ps_expr_print(self->value, 0);}   
)

Definition at line 161 of file ast.c.

◆ PS_IPRINT_IMPL() [7/7]

PS_IPRINT_IMPL ( ps_node_struct  ,
{ ps_print_indent();ps_type_struct_print(self->type, 0, ps_next_indent());}   
)

Definition at line 225 of file ast.c.

◆ ps_node_block_stm_new()

struct ps_node * ps_node_block_stm_new ( struct ps_node_block *  block)

Creates a new block statement.

Definition at line 79 of file ast.c.

◆ ps_node_enum_new()

struct ps_node * ps_node_enum_new ( struct ps_type_enum type,
struct ps_node_fn_arr *  methods 
)

Creates a new enum declaration.

Parameters
typeThe type blueprint of the enum.
methodsThe methods of the enum.

Definition at line 100 of file ast.c.

◆ ps_node_expr_stm_new()

struct ps_node * ps_node_expr_stm_new ( struct ps_expr expr)

Creates a new expression statement for expr.

Definition at line 69 of file ast.c.

◆ ps_node_extern_new()

struct ps_node * ps_node_extern_new ( struct ps_token name,
struct ps_type_tuple *  sig 
)

Definition at line 57 of file ast.c.

◆ ps_node_fn_new()

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.

Parameters
ret_typeThe return type of the function, or NULL if there is none.
qualifiersFunction qualifiers such as static.
See also
ps_fn_qualifiers

Definition at line 22 of file ast.c.

◆ ps_node_for_new()

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.

Parameters
initThe initialization statement of the loop.
condThe condition of the loop.
stepThe step statement of the loop.
bodyThe body of the loop.

Definition at line 112 of file ast.c.

◆ ps_node_if_new()

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.

Executes body when the condition is true, else_body otherwise.

Definition at line 138 of file ast.c.

◆ ps_node_let_new()

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.

Parameters
is_mutableWhether the variable is mutable.
typeThe type of the variable.

Definition at line 8 of file ast.c.

◆ ps_node_return_new()

struct ps_node * ps_node_return_new ( struct ps_expr value)

Creates a new return statement that returns the expression value.

Pass NULL if the statement does not return anything.

Definition at line 151 of file ast.c.

◆ ps_node_struct_new()

struct ps_node * ps_node_struct_new ( struct ps_type_struct type,
struct ps_node_fn_arr *  methods 
)

Creates a new structure declaration.

Parameters
typeThe type blueprint of the structure.
methodsThe methods of the structure.

Definition at line 88 of file ast.c.

◆ ps_node_while_new()

struct ps_node * ps_node_while_new ( struct ps_expr cond,
struct ps_node_block *  body 
)

Creates a new while loop.

Parameters
condThe condition of the loop.
bodyThe body of the loop.

Definition at line 126 of file ast.c.

◆ PS_PRINT_IMPL() [1/4]

PS_PRINT_IMPL ( ps_expr_binary  ,
{ putchar('(');ps_expr_print(self->lhs, 0);printf(" %s ", self->op->start);ps_expr_print(self->rhs, 0);putchar(')');}   
)

Definition at line 507 of file ast.c.

◆ PS_PRINT_IMPL() [2/4]

PS_PRINT_IMPL ( ps_expr_call  ,
{ printf("%s(", self->callee->start);for(usize i=0;i< self->args->length;i++) { if(i > 0) { printf(", ");} ps_expr_print(self->args->contents[i], 0);} putchar(')');}   
)

Definition at line 520 of file ast.c.

◆ PS_PRINT_IMPL() [3/4]

PS_PRINT_IMPL ( ps_interp_str  ,
{ putchar('"'); for (usize i = 0; i < self->length; i++) { struct ps_expr* part = self->contents[i]; if (part->expr_type == PS_EXPR_LIT_EXPR && part->value.lit_expr.type == PS_EXPR_LIT_STR) { printf("%s", part->value.lit_expr.value.str_val); } else { printf("\\‍("); ps_expr_print(part, ')'); } } putchar('"');}   
)

Definition at line 464 of file ast.c.

◆ PS_PRINT_IMPL() [4/4]

PS_PRINT_IMPL ( ps_name  ,
{ for(usize i=0;i< self->length;i++) { if(i > 0) { printf("::");} ps_token_print(&self->contents[i], 0);} }   
)

Definition at line 38 of file ast.c.