libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
type.h
Go to the documentation of this file.
1// Copyright (C) 2023 Ethan Uppal. All rights reserved.
2
3#pragma once
4
5#include "def.h"
6#include "enum/make.h" // TO_ENUM
7#include "frontend/lexer/token.h" // struct ps_token
8#include "util/print.h" // PS_PRINT_DECL, PS_PRINT_IMPL
9#include "util/dynarr.h"
10
11struct ps_type;
12struct ps_type_tuple;
13
17 struct ps_type* type;
18};
19
23 struct ps_type_tuple* args;
24};
25
29struct ps_type_field_arr ps_dynarr(struct ps_type_field);
30#define ps_type_field_arr_new() ps_dynarr_new(struct ps_type_field_arr)
31#define ps_type_field_arr_free(arr) ps_dynarr_free(arr)
32
36struct ps_type_variant_arr ps_dynarr(struct ps_type_variant);
37#define ps_type_variant_arr_new() ps_dynarr_new(struct ps_type_variant_arr)
38#define ps_type_variant_arr_free(arr) ps_dynarr_free(arr)
39
44struct ps_type_tuple ps_dynarr(struct ps_type*);
45#define ps_type_tuple_new() ps_dynarr_new(struct ps_type_tuple)
46#define ps_type_tuple_free(arr) ps_dynarr_free(arr)
47
49 struct ps_token* name;
50 struct ps_type_field_arr* fields;
51};
52
54 struct ps_token* name;
56 struct ps_type_variant_arr* variants;
57};
58
61#define ENUM TO_ENUM
62#include "type_type.h"
63#undef ENUM
64};
65
66struct ps_type {
68 union {
69 struct ps_type_tuple* tuple;
72 // TODO: trait type
75};
76
79
85struct ps_type* ps_type_prim_new(enum ps_type_type prim_type);
86
92struct ps_type* ps_type_struct_new(struct ps_token* name);
93
99void ps_type_struct_add(struct ps_type* type, struct ps_type_field field);
100
106struct ps_type* ps_type_enum_new(struct ps_token* name);
107
113void ps_type_enum_add(struct ps_type* type, struct ps_type_variant variant);
114
120struct ps_type* ps_type_unresolved_new(struct ps_token* name);
121
122// factory function for identifier-based types
129struct ps_type* ps_type_from_name(struct ps_token* name);
130
131// factory function for tuples
135struct ps_type* ps_type_from_tuple(struct ps_type_tuple* tuple);
136
138PS_PRINT_DECL(ps_type_tuple);
143PS_PRINT_DECL(ps_type_field_arr);
Base definitions.
#define STR
Definition def.h:40
#define usize
Definition def.h:50
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
Represents a token.
Definition token.h:34
enum ps_token_type type
Definition token.h:36
struct ps_type_variant_arr * variants
Definition type.h:56
usize length
Definition type.h:55
struct ps_token * name
Definition type.h:54
A pair (string, type).
Definition type.h:15
STR name
Definition type.h:16
struct ps_type * type
Definition type.h:17
struct ps_type_field_arr * fields
Definition type.h:50
struct ps_token * name
Definition type.h:49
A pair (string, types).
Definition type.h:21
struct ps_type_tuple * args
Definition type.h:23
Definition type.h:66
struct ps_type_enum enum_
Definition type.h:71
struct ps_type_struct struct_
Definition type.h:70
struct ps_type_tuple * tuple
Definition type.h:69
union ps_type::@0 value
struct ps_token * unresolved
Definition type.h:73
enum ps_type_type type
Definition type.h:67
Defines a token.
struct ps_type * ps_type_struct_new(struct ps_token *name)
Constructs a new empty struct type with the name name.
Definition type.c:37
struct ps_type * ps_type_from_name(struct ps_token *name)
Returns the type associated with the given name (e.g.
Definition type.c:76
struct ps_type * ps_type_enum_new(struct ps_token *name)
Constructs a new empty enum type with the name name.
Definition type.c:52
bool ps_type_is_prim(enum ps_type_type type)
Returns whether type represents a primitive type.
Definition type.c:19
struct ps_type * ps_type_from_tuple(struct ps_type_tuple *tuple)
Constructs and returns a type for the given tuple type tuple.
Definition type.c:86
ps_type_type
The type of a type :)
Definition type.h:60
void ps_type_struct_add(struct ps_type *type, struct ps_type_field field)
Adds the given field field to the struct type type.
Definition type.c:47
struct ps_type * ps_type_prim_new(enum ps_type_type prim_type)
Constructs a new primitive type specified by prim_type.
Definition type.c:23
struct ps_type * ps_type_unresolved_new(struct ps_token *name)
Constructs a new unresolved type corresponding to the given name.
Definition type.c:67
void ps_type_enum_add(struct ps_type *type, struct ps_type_variant variant)
Adds the given variant variant to the struct type type.
Definition type.c:62