Skip to content

Commit 7e44cde

Browse files
committed
work on DIF type conversion (wip)
1 parent c32602d commit 7e44cde

File tree

2 files changed

+91
-16
lines changed

2 files changed

+91
-16
lines changed

src/dif/representation.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use ordered_float::OrderedFloat;
2424
use serde::de::{MapAccess, SeqAccess, Visitor};
2525
use serde::ser::{SerializeMap, SerializeSeq};
2626
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
27+
use datex_core::types::type_container::TypeContainer;
2728

2829
#[derive(Clone, Debug, PartialEq)]
2930
pub enum DIFValueRepresentation {
@@ -176,21 +177,11 @@ impl DIFValueRepresentation {
176177
}
177178
}
178179
DIFTypeContainer::Type(dif_type) => {
179-
match &dif_type.type_definition {
180-
DIFTypeDefinition::Structural(s) => {
181-
core::todo!(
182-
"#390 Structural type conversion not supported yet"
183-
)
184-
}
185-
DIFTypeDefinition::Unit => Value {
186-
actual_type: Box::new(get_core_lib_type(
187-
CoreLibPointerId::Null,
188-
)),
189-
inner: CoreValue::Null,
190-
},
191-
_ => core::todo!(
192-
"#391 Other type definitions not supported yet"
193-
),
180+
let val = self.to_default_value(memory)?;
181+
let ty = dif_type.to_type(memory);
182+
Value {
183+
actual_type: Box::new(TypeContainer::Type(ty)),
184+
..val
194185
}
195186
}
196187
})

src/dif/type.rs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::dif::DIFConvertible;
2-
use crate::dif::representation::DIFTypeRepresentation;
2+
use crate::dif::representation::{DIFTypeRepresentation, DIFValueRepresentation};
33
use crate::references::reference::Reference;
44
use crate::references::reference::ReferenceMutability;
55
use crate::references::reference::mutability_option_as_int;
@@ -14,7 +14,13 @@ use crate::values::core_values::r#type::Type;
1414
use crate::values::pointer::PointerAddress;
1515
use core::cell::RefCell;
1616
use core::prelude::rust_2024::*;
17+
use std::hash::RandomState;
18+
use indexmap::IndexMap;
1719
use serde::{Deserialize, Serialize};
20+
use crate::libs::core::{get_core_lib_type, CoreLibPointerId};
21+
use crate::values::core_value::CoreValue;
22+
use crate::values::value::Value;
23+
use crate::values::value_container::ValueContainer;
1824

1925
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
2026
#[serde(tag = "kind", content = "def", rename_all = "lowercase")]
@@ -136,6 +142,52 @@ impl DIFTypeDefinition {
136142
},
137143
}
138144
}
145+
146+
fn to_type_definition(
147+
&self,
148+
memory: &RefCell<Memory>,
149+
) -> TypeDefinition {
150+
match self {
151+
DIFTypeDefinition::Intersection(types) => {
152+
TypeDefinition::Intersection(
153+
types
154+
.iter()
155+
.map(|t| {
156+
t.to_type_container(memory)
157+
})
158+
.collect(),
159+
)
160+
}
161+
DIFTypeDefinition::Union(types) => TypeDefinition::Union(
162+
types
163+
.iter()
164+
.map(|t| t.to_type_container(memory))
165+
.collect(),
166+
),
167+
DIFTypeDefinition::Reference(type_ref_addr) => {
168+
let type_ref = memory
169+
.borrow_mut()
170+
.get_type_reference(type_ref_addr)
171+
.expect("Reference not found in memory")
172+
.clone();
173+
TypeDefinition::Reference(type_ref)
174+
}
175+
DIFTypeDefinition::Type(dif_type) => {
176+
TypeDefinition::Type(Box::new(dif_type.to_type(memory)))
177+
},
178+
DIFTypeDefinition::Marker(ptr_address) => {
179+
TypeDefinition::Marker(ptr_address.clone())
180+
}
181+
DIFTypeDefinition::Unit => TypeDefinition::Unit,
182+
DIFTypeDefinition::Never => TypeDefinition::Never,
183+
DIFTypeDefinition::Unknown => TypeDefinition::Unknown,
184+
_ => {
185+
core::todo!(
186+
"DIFTypeDefinition::to_type_definition for this variant is not implemented yet"
187+
)
188+
}
189+
}
190+
}
139191
}
140192

141193
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -177,6 +229,18 @@ impl DIFType {
177229
),
178230
}
179231
}
232+
233+
pub(crate) fn to_type(&self, memory: &RefCell<Memory>) -> Type {
234+
Type {
235+
reference_mutability: self.mutability.clone(),
236+
type_definition: DIFTypeDefinition::to_type_definition(
237+
&self.type_definition,
238+
memory,
239+
),
240+
base_type: None,
241+
}
242+
}
243+
180244
}
181245

182246
impl From<DIFTypeRepresentation> for DIFType {
@@ -220,6 +284,26 @@ impl DIFTypeContainer {
220284
}
221285
}
222286
}
287+
288+
pub fn to_type_container(
289+
&self,
290+
memory: &RefCell<Memory>,
291+
) -> TypeContainer {
292+
match self {
293+
DIFTypeContainer::Type(dif_type) => {
294+
TypeContainer::Type(dif_type.to_type(memory))
295+
}
296+
DIFTypeContainer::Reference(addr) => {
297+
TypeContainer::TypeReference(
298+
memory
299+
.borrow_mut()
300+
.get_type_reference(addr)
301+
.expect("Reference not found in memory")
302+
.clone()
303+
)
304+
}
305+
}
306+
}
223307
}
224308

225309
#[cfg(test)]

0 commit comments

Comments
 (0)