libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
Functions | Variables
type.c File Reference
#include <string.h>
#include "type.h"
#include "util/abort.h"
#include "util/arena.h"

Go to the source code of this file.

Functions

bool ps_type_is_prim (enum ps_type_type type)
 Returns whether type represents a primitive type.
 
struct ps_typeps_type_prim_new (enum ps_type_type prim_type)
 Constructs a new primitive type specified by prim_type.
 
struct ps_typeps_type_struct_new (struct ps_token *name)
 Constructs a new empty struct type with the name name.
 
void ps_type_struct_add (struct ps_type *type, struct ps_type_field field)
 Adds the given field field to the struct type type.
 
struct ps_typeps_type_enum_new (struct ps_token *name)
 Constructs a new empty enum type with the name name.
 
void ps_type_enum_add (struct ps_type *type, struct ps_type_variant variant)
 Adds the given variant variant to the struct type type.
 
struct ps_typeps_type_unresolved_new (struct ps_token *name)
 Constructs a new unresolved type corresponding to the given name.
 
struct ps_typeps_type_from_name (struct ps_token *name)
 Returns the type associated with the given name (e.g.
 
struct ps_typeps_type_from_tuple (struct ps_type_tuple *tuple)
 Constructs and returns a type for the given tuple type tuple.
 
 PS_PRINT_IMPL (ps_type, { switch(self->type) { case PS_TYPE_U8:{ fputs(_STRING(PS_PRIM_TYPE_U8), stdout);break;} case PS_TYPE_I8:{ fputs(_STRING(PS_PRIM_TYPE_I8), stdout);break;} case PS_TYPE_U16:{ fputs(_STRING(PS_PRIM_TYPE_U16), stdout);break;} case PS_TYPE_I16:{ fputs(_STRING(PS_PRIM_TYPE_I16), stdout);break;} case PS_TYPE_U32:{ fputs(_STRING(PS_PRIM_TYPE_U32), stdout);break;} case PS_TYPE_I32:{ fputs(_STRING(PS_PRIM_TYPE_I32), stdout);break;} case PS_TYPE_U64:{ fputs(_STRING(PS_PRIM_TYPE_U64), stdout);break;} case PS_TYPE_I64:{ fputs(_STRING(PS_PRIM_TYPE_I64), stdout);break;} case PS_TYPE_F32:{ fputs(_STRING(PS_PRIM_TYPE_F32), stdout);break;} case PS_TYPE_F64:{ fputs(_STRING(PS_PRIM_TYPE_F64), stdout);break;} case PS_TYPE_B:{ fputs(_STRING(PS_PRIM_TYPE_B), stdout);break;} case PS_TYPE_TUPLE:{ ps_type_tuple_print(self->value.tuple, 0);break;} case PS_TYPE_STRUCT:{ ps_type_struct_print(&self->value.struct_, 0, 0);break;} case PS_TYPE_ENUM:{ ps_type_enum_print(&self->value.enum_, 0, 0);break;} case PS_TYPE_UNRESOLVED:{ printf("%s", self->value.unresolved->start);break;} default:{ ps_abort("unknown type encountered");break;} } })
 
 PS_PRINT_IMPL (ps_type_variant, { printf("%s", self->name);ps_type_tuple_print(self->args, 0);})
 
 PS_IPRINT_IMPL (ps_type_enum, { ps_print_indent();printf("enum %.*s {\n",(int) self->name->length, self->name->start);for(usize i=0;i< self->variants->length;i++) { ps_type_variant_print(& $arr(self->variants)[i], 0);printf("\n");} ps_print_indent();printf("}");})
 

Variables

STR prim_type_names []
 For each type PS_TYPE_U8 to PS_TYPE_B, this array indexes by prim_type_names[PS_TYPE_U8] == _STRING(PS_PRIM_TYPE_U8).
 

Function Documentation

◆ PS_IPRINT_IMPL()

PS_IPRINT_IMPL ( ps_type_enum  ,
{ ps_print_indent();printf("enum %.*s {\n",(int) self->name->length, self->name->start);for(usize i=0;i< self->variants->length;i++) { ps_type_variant_print(&$arr(self->variants)[i], 0);printf("\n");} ps_print_indent();printf("}");}   
)

Definition at line 191 of file type.c.

◆ PS_PRINT_IMPL() [1/2]

PS_PRINT_IMPL ( ps_type  ,
{ switch(self->type) { case PS_TYPE_U8:{ fputs(_STRING(PS_PRIM_TYPE_U8), stdout);break;} case PS_TYPE_I8:{ fputs(_STRING(PS_PRIM_TYPE_I8), stdout);break;} case PS_TYPE_U16:{ fputs(_STRING(PS_PRIM_TYPE_U16), stdout);break;} case PS_TYPE_I16:{ fputs(_STRING(PS_PRIM_TYPE_I16), stdout);break;} case PS_TYPE_U32:{ fputs(_STRING(PS_PRIM_TYPE_U32), stdout);break;} case PS_TYPE_I32:{ fputs(_STRING(PS_PRIM_TYPE_I32), stdout);break;} case PS_TYPE_U64:{ fputs(_STRING(PS_PRIM_TYPE_U64), stdout);break;} case PS_TYPE_I64:{ fputs(_STRING(PS_PRIM_TYPE_I64), stdout);break;} case PS_TYPE_F32:{ fputs(_STRING(PS_PRIM_TYPE_F32), stdout);break;} case PS_TYPE_F64:{ fputs(_STRING(PS_PRIM_TYPE_F64), stdout);break;} case PS_TYPE_B:{ fputs(_STRING(PS_PRIM_TYPE_B), stdout);break;} case PS_TYPE_TUPLE:{ ps_type_tuple_print(self->value.tuple, 0);break;} case PS_TYPE_STRUCT:{ ps_type_struct_print(&self->value.struct_, 0, 0);break;} case PS_TYPE_ENUM:{ ps_type_enum_print(&self->value.enum_, 0, 0);break;} case PS_TYPE_UNRESOLVED:{ printf("%s", self->value.unresolved->start);break;} default:{ ps_abort("unknown type encountered");break;} } }   
)

Definition at line 95 of file type.c.

◆ PS_PRINT_IMPL() [2/2]

PS_PRINT_IMPL ( ps_type_variant  ,
{ printf("%s", self->name);ps_type_tuple_print(self->args, 0);}   
)

Definition at line 175 of file type.c.

◆ ps_type_enum_add()

void ps_type_enum_add ( struct ps_type type,
struct ps_type_variant  variant 
)

Adds the given variant variant to the struct type type.

Precondition
type represents a enum type.

Definition at line 62 of file type.c.

◆ ps_type_enum_new()

struct ps_type * ps_type_enum_new ( struct ps_token name)

Constructs a new empty enum type with the name name.

TODO: add trait implementations?

Definition at line 52 of file type.c.

◆ ps_type_from_name()

struct ps_type * ps_type_from_name ( struct ps_token name)

Returns the type associated with the given name (e.g.

a primitive type, a struct type).

Precondition
name is a valid pulsar identifier.

Definition at line 76 of file type.c.

◆ ps_type_from_tuple()

struct ps_type * ps_type_from_tuple ( struct ps_type_tuple *  tuple)

Constructs and returns a type for the given tuple type tuple.

Definition at line 86 of file type.c.

◆ ps_type_is_prim()

bool ps_type_is_prim ( enum ps_type_type  type)

Returns whether type represents a primitive type.

Definition at line 19 of file type.c.

◆ ps_type_prim_new()

struct ps_type * ps_type_prim_new ( enum ps_type_type  prim_type)

Constructs a new primitive type specified by prim_type.

Precondition
prim_type corresponds to a primitive type.

Definition at line 23 of file type.c.

◆ ps_type_struct_add()

void ps_type_struct_add ( struct ps_type type,
struct ps_type_field  field 
)

Adds the given field field to the struct type type.

Precondition
type represents a struct type.

Definition at line 47 of file type.c.

◆ ps_type_struct_new()

struct ps_type * ps_type_struct_new ( struct ps_token name)

Constructs a new empty struct type with the name name.

TODO: add trait implementations

Definition at line 37 of file type.c.

◆ ps_type_unresolved_new()

struct ps_type * ps_type_unresolved_new ( struct ps_token name)

Constructs a new unresolved type corresponding to the given name.

These types are resolved in later passes.

Definition at line 67 of file type.c.

Variable Documentation

◆ prim_type_names

STR prim_type_names[]
Initial value:
#define _STRING(x)
Definition def.h:74
#define PS_PRIM_TYPE_F32
Definition type_prim.h:11
#define PS_PRIM_TYPE_I16
Definition type_prim.h:6
#define PS_PRIM_TYPE_B
Definition type_prim.h:13
#define PS_PRIM_TYPE_F64
Definition type_prim.h:12
#define PS_PRIM_TYPE_I64
Definition type_prim.h:10
#define PS_PRIM_TYPE_U16
Definition type_prim.h:5
#define PS_PRIM_TYPE_U64
Definition type_prim.h:9
#define PS_PRIM_TYPE_I32
Definition type_prim.h:8
#define PS_PRIM_TYPE_U32
Definition type_prim.h:7
#define PS_PRIM_TYPE_I8
Definition type_prim.h:4
#define PS_PRIM_TYPE_U8
Definition type_prim.h:3

For each type PS_TYPE_U8 to PS_TYPE_B, this array indexes by prim_type_names[PS_TYPE_U8] == _STRING(PS_PRIM_TYPE_U8).

Definition at line 12 of file type.c.