diff --git a/src/main.rs b/src/main.rs index 34eedba..6abf52e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,9 +6,18 @@ use { laddertypes::{ dict::TypeDict, parser::ParseLadderType, subtype_unify, unparser::UnparseLadderType, BimapTypeDict, Morphism, MorphismType }, - std::sync::{Arc, RwLock} + std::{any::Any, sync::{Arc, RwLock}} }; +/* +*/ +pub fn get_c_repr_kind(kind: String) -> Option<String> { + match kind.as_str() { + "ℤ" => Some("uint64_t".into()), + _ => None + } +} + /* for a given ladder type `t`, get the corresponding C type */ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_pointer: bool) -> Option<String> { @@ -66,16 +75,38 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_ #[derive(Clone, Debug)] struct LdmcPrimCMorphism { symbol: String, - type_args: Vec<(String, String)>, + type_args: Vec<(laddertypes::TypeID, String)>, src_type: laddertypes::TypeTerm, dst_type: laddertypes::TypeTerm, locations: Vec<String> } impl LdmcPrimCMorphism { - pub fn expected_c_type_signature(&self, dict: &mut impl TypeDict) -> String { + pub fn instantiated_symbol_name(&self, dict: &mut impl TypeDict, + σ: &std::collections::HashMap<laddertypes::TypeID, laddertypes::TypeTerm>) -> String { + let mut s = self.symbol.clone(); + for (k_id,v) in self.type_args.iter() { + if let Some(val_trm) = σ.get(k_id) { + if let laddertypes::TypeID::Var(var_id) = k_id { + let name = dict.get_varname(*var_id).unwrap(); + let val_str = dict.unparse(val_trm); + s.push_str(&format!("_{}_{}", name, val_str)); + } + } else { + if let laddertypes::TypeID::Var(var_id) = k_id { + let k = dict.get_varname(*var_id).unwrap(); + s.push_str(&format!("_{:?}_MISSING", k)); + eprintln!("INCOMPLETE MORPHISM INSTANTIATION, missing {} ({})", k, var_id); + } + } + } + s + } + + pub fn expected_c_type_signature(&self, dict: &mut impl TypeDict, + σ: &std::collections::HashMap<laddertypes::TypeID, laddertypes::TypeTerm>) -> String { format!("int {} ({} const * restrict src, {} * restrict dst);", - self.symbol, + self.instantiated_symbol_name(dict, σ), get_c_repr_type(dict, self.src_type.clone(), true).expect("cant get c-repr type for src type"), get_c_repr_type(dict, self.dst_type.clone(), true).expect("cant get c-repr type for dst type")) } @@ -95,7 +126,7 @@ enum LdmcMorphism { } impl LdmcMorphism { - pub fn generate_call(&self, dict: &mut impl TypeDict, i: u64) { + pub fn generate_call(&self, dict: &mut impl TypeDict, σ: &std::collections::HashMap<laddertypes::TypeID, laddertypes::TypeTerm>, i: u64) { match self { LdmcMorphism::Primitive(prim_morph) => { let src_c_type = get_c_repr_type(dict, prim_morph.src_type.clone(), true).expect("cant get c-repr type for src type"); @@ -103,6 +134,7 @@ impl LdmcMorphism { let src_buf = if i%2 == 0 { "bufA" } else { "bufB" }; let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" }; + println!(r#" {} {} const * restrict src = (void*) {}; @@ -112,7 +144,7 @@ impl LdmcMorphism { '{', src_c_type, src_buf, dst_c_type, dst_buf, - prim_morph.symbol, + prim_morph.instantiated_symbol_name(dict, σ), '}'); } LdmcMorphism::LengthPrefixMap { length_prefix_type, item_morph } => { @@ -146,7 +178,7 @@ impl LdmcMorphism { src_c_type, src_buf, dst_c_type, dst_buf, map_fn, - item_morph.symbol, + item_morph.instantiated_symbol_name(dict, σ), '}'); } LdmcMorphism::ValueDelimMap { delim, item_morph } => { @@ -164,7 +196,7 @@ impl LdmcMorphism { '{', src_c_type, src_buf, dst_c_type, dst_buf, - item_morph.symbol, + item_morph.instantiated_symbol_name(dict, σ), '}'); } } @@ -253,8 +285,11 @@ fn parser( move |(((symbol, type_args), ((src_type, _), (dst_type, _))), locations)| { let mut type_dict = type_dict.write().unwrap(); let type_args : Vec<_> = type_args.into_iter().map(|(v,k)| (v,k.into_iter().collect())).collect(); - for (var, kind) in type_args.iter() { - type_dict.add_varname(var.clone()); + let mut ty_args = Vec::new(); + for (var, kind) in type_args.into_iter() { + let var_id = type_dict.add_varname(var.clone()); + eprintln!("parser: add varname {} -> {:?}", var, type_dict.get_typeid(&var)); + ty_args.push((var_id, kind)); } let mut src_type = type_dict.parse(&src_type.iter().collect::<String>()).expect("couldnt parse src type"); @@ -262,7 +297,7 @@ fn parser( LdmcPrimCMorphism { symbol, - type_args, + type_args: ty_args, src_type, dst_type, locations: locations.into_iter().map(|l| l.into_iter().collect()).collect() @@ -314,8 +349,8 @@ fn main() { let path = morphism_base.find_morphism_path(MorphismType { src_type: type_dict.parse( src_type_arg.as_str() ).expect(""), dst_type: type_dict.parse( dst_type_arg.as_str() ).expect(""), - }, - &mut *type_dict.write().unwrap() + }//, + // &mut *type_dict.write().unwrap() ); match path { @@ -340,7 +375,7 @@ int main() {{ ...with morph {} ---> {}, - subst σ = {{ {:?} }}, + subst σ = {:?}, halo Ψ = {} */"#, type_dict.unparse(&morph_inst.get_type().dst_type.param_normalize().decurry()), @@ -349,7 +384,7 @@ int main() {{ morph_inst.σ, type_dict.unparse(&morph_inst.halo), ); - morph_inst.m.generate_call(&mut type_dict, i); + morph_inst.m.generate_call(&mut type_dict, &morph_inst.σ, i); i += 1; }