libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
Data Structures | Macros | Enumerations | Functions
ast.h File Reference

Abstract syntax tree. More...

#include "def.h"
#include "enum/make.h"
#include "frontend/analyzer/type.h"
#include "frontend/lexer/token.h"
#include "util/dynarr.h"
#include "util/print.h"
#include "lit_type.h"
#include "expr_type.h"
#include "node_type.h"

Go to the source code of this file.

Data Structures

struct  ps_node_let
 Parses "let `name`: `type` = `value`". More...
 
struct  ps_node_fn
 
struct  ps_node_import
 
struct  ps_node_extern
 A reference to an external C function. More...
 
struct  ps_node_expr_stm
 
struct  ps_node_struct
 
struct  ps_node_enum
 
struct  ps_node_for
 
struct  ps_node_while
 
struct  ps_node_if
 
struct  ps_node_return
 
struct  ps_expr_id
 A variable name. More...
 
struct  ps_expr_lit
 An expression literal. More...
 
union  ps_expr_lit::ps_expr_lit_value
 
struct  ps_expr_binary
 A binary operator. More...
 
struct  ps_expr_unary
 An unary operator. More...
 
struct  ps_expr_call
 A function invocation. More...
 
struct  ps_expr
 Expression nodes are represented with tagged unions. More...
 
struct  ps_node
 AST nodes are represented with tagged unions. More...
 

Macros

#define ps_name_new()   ps_dynarr_new(struct ps_name)
 
#define ps_name_free(arr)   ps_dynarr_free(arr)
 
#define ps_node_fn_arr_new()   ps_dynarr_new(struct ps_node_fn_arr)
 
#define ps_node_fn_arr_free(arr)   ps_dynarr_free(arr)
 
#define ENUM   TO_ENUM
 
#define ENUM   TO_ENUM
 
#define ENUM   TO_ENUM
 
#define ps_node_arr_new()   ps_dynarr_new(struct ps_node_arr)
 
#define ps_node_arr_free(arr)   ps_dynarr_free(arr)
 
#define ps_node_block_new()   ps_dynarr_new(struct ps_node_block)
 
#define ps_node_block_free(arr)   ps_dynarr_free(arr)
 
#define ps_interp_str_new()   ps_dynarr_new(struct ps_interp_str)
 
#define ps_interp_str_free(arr)   ps_dynarr_free(arr)
 
#define ps_expr_arr_new()   ps_dynarr_new(struct ps_expr_arr)
 
#define ps_expr_arr_free(arr)   ps_dynarr_free(arr)
 

Enumerations

enum  ps_fn_qualifiers { PS_FN_PUBLIC = 1 << 0 , PS_FN_PRIVATE = 1 << 1 , PS_FN_STATIC = 1 << 2 , PS_FN_INLINE = 1 << 3 }
 Function qualifier flags. More...
 

Functions

struct ps_name ps_dynarr (struct ps_token *)
 A name is a list of identifiers.
 
 PS_PRINT_DECL (ps_name)
 
struct ps_node_fn_arr ps_dynarr (struct ps_node_fn)
 Represents a list of functions.
 
struct ps_node_arr ps_dynarr (struct ps_node *)
 Represents an array of nodes.
 
struct ps_interp_str ps_dynarr (struct ps_expr *)
 Represents a list of expressions concatenated as a string literal.
 
 PS_PRINT_DECL (ps_interp_str)
 
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.
 
struct ps_nodeps_node_import_new (struct ps_name *name)
 Creates a new import statement.
 
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_DECL (ps_node_let)
 
 PS_IPRINT_DECL (ps_node_fn)
 
 PS_IPRINT_DECL (ps_node_import)
 
 PS_IPRINT_DECL (ps_node_extern)
 
 PS_IPRINT_DECL (ps_node_expr_stm)
 
 PS_IPRINT_DECL (ps_node_struct)
 
 PS_IPRINT_DECL (ps_node_enum)
 
 PS_IPRINT_DECL (ps_node_for)
 
 PS_IPRINT_DECL (ps_node_while)
 
 PS_IPRINT_DECL (ps_node_if)
 
 PS_IPRINT_DECL (ps_node_return)
 
 PS_IPRINT_DECL (ps_node_block)
 Prints the given node block to standard out.
 
 PS_IPRINT_DECL (ps_node)
 
struct ps_exprps_expr_id_new (struct ps_token *name)
 Creates a new id expression.
 
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_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_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_DECL (ps_expr_id)
 
 PS_PRINT_DECL (ps_expr_lit)
 
 PS_PRINT_DECL (ps_expr_binary)
 
 PS_PRINT_DECL (ps_expr_unary)
 
 PS_PRINT_DECL (ps_expr_call)
 
 PS_PRINT_DECL (ps_expr)
 

Detailed Description

Abstract syntax tree.

Author
Ethan Uppal

Definition in file ast.h.

Macro Definition Documentation

◆ ENUM [1/3]

#define ENUM   TO_ENUM

Definition at line 138 of file ast.h.

◆ ENUM [2/3]

#define ENUM   TO_ENUM

Definition at line 138 of file ast.h.

◆ ENUM [3/3]

#define ENUM   TO_ENUM

Definition at line 138 of file ast.h.

◆ ps_expr_arr_free

#define ps_expr_arr_free (   arr)    ps_dynarr_free(arr)

Definition at line 259 of file ast.h.

◆ ps_expr_arr_new

#define ps_expr_arr_new ( )    ps_dynarr_new(struct ps_expr_arr)

Definition at line 258 of file ast.h.

◆ ps_interp_str_free

#define ps_interp_str_free (   arr)    ps_dynarr_free(arr)

Definition at line 249 of file ast.h.

◆ ps_interp_str_new

#define ps_interp_str_new ( )    ps_dynarr_new(struct ps_interp_str)

Definition at line 248 of file ast.h.

◆ ps_name_free

#define ps_name_free (   arr)    ps_dynarr_free(arr)

Definition at line 32 of file ast.h.

◆ ps_name_new

#define ps_name_new ( )    ps_dynarr_new(struct ps_name)

Definition at line 31 of file ast.h.

◆ ps_node_arr_free

#define ps_node_arr_free (   arr)    ps_dynarr_free(arr)

Definition at line 234 of file ast.h.

◆ ps_node_arr_new

#define ps_node_arr_new ( )    ps_dynarr_new(struct ps_node_arr)

Definition at line 233 of file ast.h.

◆ ps_node_block_free

#define ps_node_block_free (   arr)    ps_dynarr_free(arr)

Definition at line 241 of file ast.h.

◆ ps_node_block_new

#define ps_node_block_new ( )    ps_dynarr_new(struct ps_node_block)

Definition at line 240 of file ast.h.

◆ ps_node_fn_arr_free

#define ps_node_fn_arr_free (   arr)    ps_dynarr_free(arr)

Definition at line 74 of file ast.h.

◆ ps_node_fn_arr_new

#define ps_node_fn_arr_new ( )    ps_dynarr_new(struct ps_node_fn_arr)

Definition at line 73 of file ast.h.

Enumeration Type Documentation

◆ ps_fn_qualifiers

Function qualifier flags.

Enumerator
PS_FN_PUBLIC 

Visible outside the module.

PS_FN_PRIVATE 

Hidden outside the module.

PS_FN_STATIC 

TODO:

PS_FN_INLINE 

Inlined everywhere.

Definition at line 52 of file ast.h.

Function Documentation

◆ ps_dynarr() [1/4]

struct ps_expr_arr ps_dynarr ( struct ps_expr )

Represents a list of expressions concatenated as a string literal.

Represents an array of expressions.

See also
struct ps_expr

◆ ps_dynarr() [2/4]

struct ps_node_block ps_dynarr ( struct ps_node )

Represents an array of nodes.

Represents a block of nodes.

◆ ps_dynarr() [3/4]

struct ps_node_fn_arr ps_dynarr ( struct ps_node_fn  )

Represents a list of functions.

See also
struct ps_node_fn

◆ ps_dynarr() [4/4]

struct ps_name ps_dynarr ( struct ps_token )

A name is a list of identifiers.

See also
struct ps_token

◆ 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_id_new()

struct ps_expr * ps_expr_id_new ( struct ps_token name)

Creates a new id expression.

◆ 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_DECL() [1/13]

PS_IPRINT_DECL ( ps_node  )

◆ PS_IPRINT_DECL() [2/13]

PS_IPRINT_DECL ( ps_node_block  )

Prints the given node block to standard out.

Note
This function does not print an indent at the start. You must use ps_print_indent() and such before calling it if you wish the first line to be properly indented.

◆ PS_IPRINT_DECL() [3/13]

PS_IPRINT_DECL ( ps_node_enum  )

◆ PS_IPRINT_DECL() [4/13]

PS_IPRINT_DECL ( ps_node_expr_stm  )

◆ PS_IPRINT_DECL() [5/13]

PS_IPRINT_DECL ( ps_node_extern  )

◆ PS_IPRINT_DECL() [6/13]

PS_IPRINT_DECL ( ps_node_fn  )

◆ PS_IPRINT_DECL() [7/13]

PS_IPRINT_DECL ( ps_node_for  )

◆ PS_IPRINT_DECL() [8/13]

PS_IPRINT_DECL ( ps_node_if  )

◆ PS_IPRINT_DECL() [9/13]

PS_IPRINT_DECL ( ps_node_import  )

◆ PS_IPRINT_DECL() [10/13]

PS_IPRINT_DECL ( ps_node_let  )

◆ PS_IPRINT_DECL() [11/13]

PS_IPRINT_DECL ( ps_node_return  )

◆ PS_IPRINT_DECL() [12/13]

PS_IPRINT_DECL ( ps_node_struct  )

◆ PS_IPRINT_DECL() [13/13]

PS_IPRINT_DECL ( ps_node_while  )

◆ 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_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_import_new()

struct ps_node * ps_node_import_new ( struct ps_name *  name)

Creates a new import statement.

Parameters
nameThe name of the module to import.

◆ 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_DECL() [1/8]

PS_PRINT_DECL ( ps_expr  )

◆ PS_PRINT_DECL() [2/8]

PS_PRINT_DECL ( ps_expr_binary  )

◆ PS_PRINT_DECL() [3/8]

PS_PRINT_DECL ( ps_expr_call  )

◆ PS_PRINT_DECL() [4/8]

PS_PRINT_DECL ( ps_expr_id  )

◆ PS_PRINT_DECL() [5/8]

PS_PRINT_DECL ( ps_expr_lit  )

◆ PS_PRINT_DECL() [6/8]

PS_PRINT_DECL ( ps_expr_unary  )

◆ PS_PRINT_DECL() [7/8]

PS_PRINT_DECL ( ps_interp_str  )

◆ PS_PRINT_DECL() [8/8]

PS_PRINT_DECL ( ps_name  )