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
use crate::collections::HashMap;
use crate::{IrError, IrErrorKind, Spanned};
use runestick::{Bytes, ConstValue, Shared, TypeInfo};
use std::convert::TryFrom;
#[derive(Debug, Clone)]
pub enum IrValue {
Unit,
Byte(u8),
Char(char),
Bool(bool),
Integer(num::BigInt),
Float(f64),
String(Shared<String>),
Option(Shared<Option<IrValue>>),
Bytes(Shared<Vec<u8>>),
Vec(Shared<Vec<IrValue>>),
Tuple(Shared<Box<[IrValue]>>),
Object(Shared<HashMap<String, IrValue>>),
}
impl IrValue {
pub fn from_const(value: ConstValue) -> Self {
match value {
ConstValue::Unit => Self::Unit,
ConstValue::Byte(b) => Self::Byte(b),
ConstValue::Char(c) => Self::Char(c),
ConstValue::Bool(b) => Self::Bool(b),
ConstValue::Integer(n) => Self::Integer(n.into()),
ConstValue::Float(n) => Self::Float(n),
ConstValue::String(s) => Self::String(Shared::new(s)),
ConstValue::StaticString(s) => Self::String(Shared::new((**s).to_owned())),
ConstValue::Bytes(b) => Self::Bytes(Shared::new(b.into_vec())),
ConstValue::Option(option) => Self::Option(Shared::new(match option {
Some(some) => Some(Self::from_const(*some)),
None => None,
})),
ConstValue::Vec(vec) => {
let mut ir_vec = Vec::with_capacity(vec.len());
for value in vec {
ir_vec.push(Self::from_const(value));
}
Self::Vec(Shared::new(ir_vec))
}
ConstValue::Tuple(tuple) => {
let mut ir_tuple = Vec::with_capacity(tuple.len());
for value in Vec::from(tuple) {
ir_tuple.push(Self::from_const(value));
}
Self::Tuple(Shared::new(ir_tuple.into_boxed_slice()))
}
ConstValue::Object(object) => {
let mut ir_object = HashMap::with_capacity(object.len());
for (key, value) in object {
ir_object.insert(key, Self::from_const(value));
}
Self::Object(Shared::new(ir_object))
}
}
}
pub fn into_const<S>(self, spanned: S) -> Result<ConstValue, IrError>
where
S: Copy + Spanned,
{
use num::ToPrimitive as _;
Ok(match self {
IrValue::Unit => ConstValue::Unit,
IrValue::Byte(b) => ConstValue::Byte(b),
IrValue::Char(c) => ConstValue::Char(c),
IrValue::Bool(b) => ConstValue::Bool(b),
IrValue::Integer(n) => {
let n = match n.clone().to_i64() {
Some(n) => n,
None => {
return Err(IrError::new(spanned, IrErrorKind::NotInteger { value: n }))
}
};
ConstValue::Integer(n)
}
IrValue::Float(f) => ConstValue::Float(f),
IrValue::String(s) => {
let s = s.take().map_err(IrError::access(spanned))?;
ConstValue::String(s)
}
IrValue::Bytes(b) => {
let b = b.take().map_err(IrError::access(spanned))?;
ConstValue::Bytes(Bytes::from(b))
}
Self::Option(option) => {
ConstValue::Option(match option.take().map_err(IrError::access(spanned))? {
Some(value) => Some(Box::new(value.into_const(spanned)?)),
None => None,
})
}
IrValue::Vec(vec) => {
let vec = vec.take().map_err(IrError::access(spanned))?;
let mut const_vec = Vec::with_capacity(vec.len());
for value in vec {
const_vec.push(value.into_const(spanned)?);
}
ConstValue::Vec(const_vec)
}
IrValue::Tuple(tuple) => {
let tuple = tuple.take().map_err(IrError::access(spanned))?;
let mut const_tuple = Vec::with_capacity(tuple.len());
for value in Vec::from(tuple) {
const_tuple.push(value.into_const(spanned)?);
}
ConstValue::Tuple(const_tuple.into_boxed_slice())
}
IrValue::Object(object) => {
let object = object.take().map_err(IrError::access(spanned))?;
let mut const_object = HashMap::with_capacity(object.len());
for (key, value) in object {
const_object.insert(key, value.into_const(spanned)?);
}
ConstValue::Object(const_object)
}
})
}
pub fn into_bool(self) -> Result<bool, Self> {
match self {
Self::Bool(value) => Ok(value),
value => Err(value),
}
}
pub fn into_integer<T>(self) -> Option<T>
where
T: TryFrom<num::BigInt>,
{
match self {
Self::Integer(n) => T::try_from(n).ok(),
_ => None,
}
}
pub fn type_info(&self) -> TypeInfo {
match self {
Self::Unit => TypeInfo::StaticType(runestick::UNIT_TYPE),
Self::Byte(..) => TypeInfo::StaticType(runestick::BYTE_TYPE),
Self::Char(..) => TypeInfo::StaticType(runestick::CHAR_TYPE),
Self::Bool(..) => TypeInfo::StaticType(runestick::BOOL_TYPE),
Self::String(..) => TypeInfo::StaticType(runestick::STRING_TYPE),
Self::Bytes(..) => TypeInfo::StaticType(runestick::BYTES_TYPE),
Self::Integer(..) => TypeInfo::StaticType(runestick::INTEGER_TYPE),
Self::Float(..) => TypeInfo::StaticType(runestick::FLOAT_TYPE),
Self::Option(..) => TypeInfo::StaticType(runestick::OPTION_TYPE),
Self::Vec(..) => TypeInfo::StaticType(runestick::VEC_TYPE),
Self::Tuple(..) => TypeInfo::StaticType(runestick::TUPLE_TYPE),
Self::Object(..) => TypeInfo::StaticType(runestick::OBJECT_TYPE),
}
}
}