177 lines
6.4 KiB
Rust
177 lines
6.4 KiB
Rust
|
||
use laddertypes::{morphism::Morphism, morphism_base::MorphismBase, morphism_path::ShortestPathProblem, morphism_sugared::{SugaredMorphism, SugaredMorphismType}, SugaredStructMember};
|
||
use crate::{c_gen_types::encode_type_to_symbol, struct_layout::StructLayout};
|
||
|
||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||
pub struct LdmcPrimMorph {
|
||
pub symbol: String,
|
||
pub type_args: Vec<(laddertypes::TypeID, String)>,
|
||
pub ty: SugaredMorphismType,
|
||
pub c_source: String
|
||
}
|
||
|
||
impl LdmcPrimMorph {
|
||
/*
|
||
pub fn into_prim_c_morphism(self, dict: &mut impl laddertypes::TypeDict, symbol: String) -> LdmcPrimCMorphism {
|
||
|
||
let src_c_type = ;
|
||
let dst_c_type = ;
|
||
|
||
let c_source =
|
||
format!("
|
||
int {} ( {} const * restrict src, {} * restrict dst ) {{
|
||
{}
|
||
return 0;
|
||
}}
|
||
",
|
||
symbol, src_c_type, dst_c_type,
|
||
self.generate_call(dict, &std::collections::HashMap::new(), 0)
|
||
);
|
||
|
||
LdmcPrimCMorphism {
|
||
symbol,
|
||
type_args: Vec::new(),
|
||
src_type,
|
||
dst_type,
|
||
c_source
|
||
}
|
||
}
|
||
*/
|
||
/*
|
||
|
||
pub fn new_struct_map(
|
||
dict: &mut impl laddertypes::TypeDict,
|
||
morph_base: &MorphismBase<LdmcPrimMorph>,
|
||
src: laddertypes::TypeTerm,
|
||
dst: laddertypes::TypeTerm,
|
||
) -> Self {
|
||
let src_struct = StructLayout::parse(dict, &src).expect("cant parse src struct layout");
|
||
let dst_struct = StructLayout::parse(dict, &dst).expect("cant parse dst struct layout");
|
||
|
||
let mut member_morphisms = Vec::new();
|
||
|
||
for SugaredStructMember{ symbol: field_name, ty: dst_type } in dst_struct.members.iter() {
|
||
let mut src_type = None;
|
||
for SugaredStructMember{ symbol: src_name, ty: st } in src_struct.members.iter() {
|
||
if src_name == field_name {
|
||
src_type = Some(st.clone());
|
||
}
|
||
}
|
||
let src_type = src_type.expect(&format!("cant find member {} in src struct", field_name));
|
||
|
||
if src_type == *dst_type {
|
||
member_morphisms.push(
|
||
StructFieldMorphism {
|
||
field_name: field_name.to_string(),
|
||
item_morph: None,
|
||
src_offset: src_struct.get_offset( field_name ),
|
||
dst_offset: dst_struct.get_offset( field_name ),
|
||
}
|
||
);
|
||
} else {
|
||
match ShortestPathProblem::new(
|
||
&morph_base,
|
||
laddertypes::MorphismType {
|
||
src_type: src_type.clone().desugar(dict),
|
||
dst_type: dst_type.clone().desugar(dict)
|
||
}
|
||
).solve()
|
||
{
|
||
Some(path) => {
|
||
|
||
let src_type = src_type.clone().desugar(dict);
|
||
let dst_type = dst_type.clone().desugar(dict);
|
||
|
||
let morph_symb = format!("morph_{}_to_{}",
|
||
encode_type_to_symbol(dict, &src_type),
|
||
encode_type_to_symbol(dict, &dst_type)
|
||
);
|
||
member_morphisms.push(
|
||
StructFieldMorphism {
|
||
field_name: field_name.to_string(),
|
||
item_morph: None,//Some(Box::new(item_morph.m.into_prim_c_morphism(dict, morph_symb, &item_morph.σ))),
|
||
src_offset: src_struct.get_offset( field_name ),
|
||
dst_offset: dst_struct.get_offset( field_name ),
|
||
}
|
||
);
|
||
},
|
||
|
||
None => {
|
||
eprintln!("cant map field {}", field_name);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
LdmcPrimMorph::StructMap {
|
||
src_struct, dst_struct,
|
||
member_morphisms
|
||
}
|
||
}
|
||
*/
|
||
}
|
||
|
||
impl SugaredMorphism for LdmcPrimMorph {
|
||
fn get_type(&self) -> SugaredMorphismType {
|
||
self.ty.clone()
|
||
}
|
||
}
|
||
/*
|
||
impl Morphism for LdmcMorphism {
|
||
fn weight(&self) -> u64 {
|
||
1
|
||
}
|
||
|
||
fn get_type(&self) -> laddertypes::MorphismType {
|
||
match self {
|
||
LdmcMorphism::Primitive(prim_morph) =>
|
||
laddertypes::MorphismType {
|
||
src_type: prim_morph.src_type.clone().normalize(),
|
||
dst_type: prim_morph.dst_type.clone().normalize()
|
||
},
|
||
LdmcMorphism::LengthPrefixMap{ length_prefix_type, item_morph } => {
|
||
laddertypes::MorphismType {
|
||
src_type: laddertypes::TypeTerm::App(vec![ length_prefix_type.clone(), item_morph.src_type.clone() ]),
|
||
dst_type: laddertypes::TypeTerm::App(vec![ length_prefix_type.clone(), item_morph.dst_type.clone() ]),
|
||
}
|
||
},
|
||
LdmcMorphism::ValueDelimMap{ delim, item_morph } => {
|
||
let value_delim_type = laddertypes::TypeTerm::App(vec![]);
|
||
laddertypes::MorphismType {
|
||
src_type: laddertypes::TypeTerm::App(vec![ value_delim_type.clone(), item_morph.src_type.clone() ]),
|
||
dst_type: laddertypes::TypeTerm::App(vec![ value_delim_type.clone(), item_morph.dst_type.clone() ]),
|
||
}
|
||
},
|
||
LdmcMorphism::StructMap { src_struct, dst_struct, member_morphisms } => {
|
||
laddertypes::MorphismType {
|
||
src_type: src_struct.get_type(),
|
||
dst_type: dst_struct.get_type()
|
||
}
|
||
}
|
||
}.normalize()
|
||
}
|
||
|
||
fn map_morphism(&self, seq_type: laddertypes::TypeTerm) -> Option< Self > {
|
||
match self {
|
||
LdmcMorphism::Primitive(prim) => {
|
||
let item_morph = Box::new(prim.clone());
|
||
/*
|
||
if seq_type == self.length_prefix_type {
|
||
*/
|
||
Some(LdmcMorphism::LengthPrefixMap{
|
||
length_prefix_type: seq_type,
|
||
item_morph,
|
||
})
|
||
/*
|
||
} else if seq_type == self.value_delim_type {
|
||
Some(LdmcMorphism::ValueDelimMap { delim, item_morph })
|
||
} else {
|
||
None
|
||
}
|
||
*/
|
||
}
|
||
_ => None
|
||
}
|
||
}
|
||
}
|
||
*/
|