Skip to main content

typr_core/utils/
builder.rs

1#![allow(
2    dead_code,
3    unused_variables,
4    unused_imports,
5    unreachable_code,
6    unused_assignments
7)]
8
9use crate::components::error_message::help_data::HelpData;
10use crate::components::error_message::type_error::TypeError;
11use crate::components::language::operators::Op;
12use crate::components::language::var::Var;
13use crate::components::language::Lang;
14use crate::components::r#type::argument_type::ArgumentType;
15use crate::components::r#type::tchar::Tchar;
16use crate::components::r#type::tint::Tint;
17use crate::components::r#type::type_operator::TypeOperator;
18use crate::components::r#type::vector_type::VecType;
19use crate::components::r#type::Type;
20use crate::TypRError;
21use std::collections::HashSet;
22
23pub fn generic_type() -> Type {
24    Type::Generic("T".to_string(), HelpData::default())
25}
26
27pub fn self_generic_type() -> Type {
28    Type::Generic("Self".to_string(), HelpData::default())
29}
30
31pub fn empty_type() -> Type {
32    Type::Empty(HelpData::default())
33}
34
35pub fn empty_lang() -> Lang {
36    Lang::Empty(HelpData::default())
37}
38
39pub fn any_type() -> Type {
40    Type::Any(HelpData::default())
41}
42
43pub fn integer_type(i: i32) -> Type {
44    Type::Integer(Tint::Val(i), HelpData::default())
45}
46
47pub fn integer_type_default() -> Type {
48    Type::Integer(Tint::Unknown, HelpData::default())
49}
50
51pub fn character_type(s: &str) -> Type {
52    Type::Char(Tchar::Val(s.to_string()), HelpData::default())
53}
54
55pub fn character_type_default() -> Type {
56    Type::Char(Tchar::Unknown, HelpData::default())
57}
58
59pub fn number_type() -> Type {
60    Type::Number(
61        crate::components::r#type::tnumber::Tnum::Unknown,
62        HelpData::default(),
63    )
64}
65
66pub fn boolean_type() -> Type {
67    Type::Boolean(
68        crate::components::r#type::tbool::Tbool::Unknown,
69        HelpData::default(),
70    )
71}
72
73pub fn record_type(params: &[(String, Type)]) -> Type {
74    let args = params
75        .iter()
76        .map(|param| ArgumentType::from(param.to_owned()))
77        .collect::<HashSet<_>>();
78    Type::Record(args, HelpData::default())
79}
80
81pub fn params_type() -> Type {
82    Type::Params(vec![], HelpData::default())
83}
84
85pub fn generic_function(s: &str) -> Lang {
86    let body = format!("{} <- function(x, ...) {{ UseMethod('{}') }}", s, s);
87    Lang::GenFunc {
88        name: body,
89        help_data: HelpData::default(),
90    }
91}
92
93pub fn tuple_type(types: &[Type]) -> Type {
94    Type::Tuple(types.to_vec(), HelpData::default())
95}
96
97pub fn array_type(i: Type, t: Type) -> Type {
98    Type::Vec(VecType::S3, Box::new(i), Box::new(t), HelpData::default())
99}
100
101pub fn array_type2(i: i32, t: Type) -> Type {
102    let i2 = integer_type(i);
103    Type::Vec(VecType::S3, Box::new(i2), Box::new(t), HelpData::default())
104}
105
106pub fn dataframe_type(i: Type, columns: &[(String, Type)]) -> Type {
107    let fields = columns
108        .iter()
109        .map(|param| ArgumentType::from(param.to_owned()))
110        .collect::<HashSet<_>>();
111    Type::Vec(
112        VecType::DataFrame,
113        Box::new(i),
114        Box::new(Type::Record(fields, HelpData::default())),
115        HelpData::default(),
116    )
117}
118
119pub fn opaque_type(name: &str) -> Type {
120    Type::Opaque(name.to_string(), HelpData::default())
121}
122
123pub fn function_type(args: &[Type], return_type: Type) -> Type {
124    let arg_types: Vec<ArgumentType> = args
125        .iter()
126        .enumerate()
127        .map(|(i, typ)| {
128            let arg_name = crate::components::r#type::generate_arg(i);
129            ArgumentType::new(&arg_name, typ)
130        })
131        .collect();
132    Type::Function(arg_types, Box::new(return_type), HelpData::default())
133}
134
135pub fn interface_type(signatures: &[(&str, Type)]) -> Type {
136    let args = signatures
137        .iter()
138        .cloned()
139        .map(|(name, typ)| ArgumentType::from((name, typ)))
140        .collect::<HashSet<_>>();
141    Type::Interface(args, HelpData::default())
142}
143
144pub fn interface_type2(signatures: &[(String, Type)]) -> Type {
145    let args = signatures
146        .iter()
147        .cloned()
148        .map(|(name, typ)| ArgumentType::from((name, typ)))
149        .collect::<HashSet<_>>();
150    Type::Interface(args, HelpData::default())
151}
152
153pub fn intersection_type(types: &[Type]) -> Type {
154    types
155        .iter()
156        .cloned()
157        .reduce(|acc, t| {
158            Type::Operator(
159                TypeOperator::Intersection,
160                Box::new(acc),
161                Box::new(t),
162                HelpData::default(),
163            )
164        })
165        .unwrap_or(Type::Empty(HelpData::default()))
166}
167
168pub fn union_type(types: &[Type]) -> Type {
169    types
170        .iter()
171        .cloned()
172        .reduce(|acc, t| {
173            Type::Operator(
174                TypeOperator::Union,
175                Box::new(acc),
176                Box::new(t),
177                HelpData::default(),
178            )
179        })
180        .unwrap_or(Type::Empty(HelpData::default()))
181}
182
183pub fn unknown_function_type() -> Type {
184    Type::UnknownFunction(HelpData::default())
185}
186
187pub fn operation(operator: Op, left: Lang, right: Lang) -> Lang {
188    Lang::Operator {
189        operator,
190        rhs: Box::new(left),
191        lhs: Box::new(right),
192        help_data: HelpData::default(),
193    }
194}
195
196pub fn let_var(name: &str, typ: Type) -> (Var, Type) {
197    (Var::from(name).set_type(typ.clone()), typ)
198}
199
200pub fn null_type() -> Type {
201    Type::Null(HelpData::default())
202}
203
204pub fn na_type() -> Type {
205    Type::NA(HelpData::default())
206}
207
208pub fn null_lang() -> Lang {
209    Lang::Null(HelpData::default())
210}
211
212pub fn unmatching_return_type(typ1: &Type, typ2: &Type) -> TypRError {
213    TypRError::Type(TypeError::UnmatchingReturnType(typ1.clone(), typ2.clone()))
214}