1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::error::*;
use crate::prelude::*;
use std::collections::HashMap;
use std::any::TypeId;
use std::char;
use std::u32;
use crate::builtins;
use num::bigint::ToBigInt;
use num::BigInt;
use crate::func_object;
func_object!(String_, (1..=1), _c, args -> {
Ok(StringObject::new(args[0].to_string()?))
});
func_object!(Bool, (1..=1), _c, args -> {
Ok(BoolObject::new(args[0].truthy()))
});
pub fn to_int(val: ObjectRef) -> RuntimeResult<BigInt> {
let val_any = val.as_any();
match val_any.type_id() {
a if a == TypeId::of::<ObjectCell<IntObject>>() => Ok(val_any
.downcast_ref::<ObjectCell<IntObject>>()
.unwrap()
.try_borrow()?
.val
.clone()),
a if a == TypeId::of::<ObjectCell<FloatObject>>() => Ok((val_any
.downcast_ref::<ObjectCell<FloatObject>>()
.unwrap()
.try_borrow()?
.val as i64)
.to_bigint()
.unwrap()),
a if a == TypeId::of::<ObjectCell<StringObject>>() => {
let as_str = val_any
.downcast_ref::<ObjectCell<StringObject>>()
.unwrap()
.try_borrow()?;
let as_str = &as_str.val;
Ok(as_str.parse::<BigInt>().map_err(|e| {
RuntimeError::type_error(format!(
"Error converting string to int: {}",
e.to_string()
))
})?)
}
a if a == TypeId::of::<ObjectCell<CharObject>>() => Ok((val_any
.downcast_ref::<ObjectCell<CharObject>>()
.unwrap()
.try_borrow()?
.val as u32)
.to_bigint()
.unwrap()),
_ => Err(RuntimeError::type_error(format!(
"Unable to convert from {} to int",
val.technetium_type_name()
))),
}
}
func_object!(Int, (1..=1), _c, args -> {
Ok(IntObject::new_big(to_int(ObjectRef::clone(&args[0]))?))
});
pub fn to_float(val: ObjectRef) -> RuntimeResult<f64> {
let val_any = val.as_any();
match val_any.type_id() {
a if a == TypeId::of::<ObjectCell<FloatObject>>() => Ok(val_any
.downcast_ref::<ObjectCell<FloatObject>>()
.unwrap()
.try_borrow()?
.val),
a if a == TypeId::of::<ObjectCell<IntObject>>() => Ok(val_any
.downcast_ref::<ObjectCell<IntObject>>()
.unwrap()
.try_borrow()?
.to_i64()? as f64),
a if a == TypeId::of::<ObjectCell<StringObject>>() => {
let as_str = val_any
.downcast_ref::<ObjectCell<StringObject>>()
.unwrap()
.try_borrow()?;
let as_str = &as_str.val;
Ok(as_str.parse::<f64>().map_err(|e| {
RuntimeError::type_error(format!(
"Error converting string to int: {}",
e.to_string()
))
})?)
}
_ => Err(RuntimeError::type_error(format!(
"Unable to convert from {} to int",
val.technetium_type_name()
))),
}
}
func_object!(Float, (1..=1), _c, args -> {
Ok(FloatObject::new(to_float(ObjectRef::clone(&args[0]))?))
});
pub fn to_char(val: ObjectRef) -> RuntimeResult<char> {
let val_any = val.as_any();
match val_any.type_id() {
a if a == TypeId::of::<ObjectCell<IntObject>>() => {
let as_int = val_any
.downcast_ref::<ObjectCell<IntObject>>()
.unwrap()
.try_borrow()?
.to_i64()?;
if as_int < 0 || as_int > u32::MAX as i64 {
Err(RuntimeError::type_error(
"Value out of range to be converted to character",
))
} else {
let as_char = char::from_u32(as_int as u32);
if let Some(c) = as_char {
Ok(c)
} else {
Err(RuntimeError::type_error(format!(
"Integer does not map to character: {}",
as_int
)))
}
}
}
a if a == TypeId::of::<ObjectCell<StringObject>>() => {
let as_str = val_any
.downcast_ref::<ObjectCell<StringObject>>()
.unwrap()
.try_borrow()?;
let as_str = &as_str.val;
if as_str.len() != 1 {
Err(RuntimeError::type_error(format!(
"Unable to convert string of length {} to character",
as_str.len()
)))
} else {
Ok(as_str.chars().next().unwrap())
}
}
_ => Err(RuntimeError::type_error(format!(
"Unable to convert from {} to int",
val.technetium_type_name()
))),
}
}
func_object!(Char, (1..=1), _c, args -> {
Ok(CharObject::new(to_char(ObjectRef::clone(&args[0]))?))
});
func_object!(List_, (0..=1), context, args -> {
let mut res = vec![];
if args.len() == 0 {
return Ok(ObjectRef::new(List { contents: res }));
}
let iter = args[0].make_iter(context)?;
while let Some(val) = iter.take_iter(context)? {
res.push(ObjectRef::clone(&val));
}
Ok(ObjectRef::new(List { contents: res }))
});
func_object!(Set_, (0..=1), context, args -> {
let mut res = HashSet::new();
if args.len() == 0 {
return Ok(ObjectRef::new(Set { contents: res }));
}
let iter = args[0].make_iter(context)?;
while let Some(val) = iter.take_iter(context)? {
let hashable = val.hashable().ok_or_else(|| RuntimeError::type_error(format!("Type {} used in set is not hashable", val.technetium_type_name())))?;
res.insert(hashable);
}
Ok(ObjectRef::new(Set { contents: res }))
});
func_object!(Dict_, (0..=1), context, args -> {
let mut res = HashMap::<HashableObjectRef, ObjectRef>::new();
if args.len() == 0 {
return Ok(ObjectRef::new(Dictionary { contents: res }));
}
downcast!((_as_dict: Dictionary = args[0]) -> {
return Ok(args[0].clone());
} else { });
let iter = args[0].make_iter(context)?;
while let Some(val) = iter.take_iter(context)? {
let key = builtins::index_get(val.clone(), IntObject::new(0))?.hashable().ok_or_else(|| RuntimeError::type_error(format!("Type {} used in set is not hashable", val.technetium_type_name())))?;
let val = builtins::index_get(val.clone(), IntObject::new(1))?;
key.lock_immutable();
res.insert(key, val);
}
Ok(ObjectRef::new(Dictionary { contents: res }))
});