generate instanitated symbol name
This commit is contained in:
parent
afa326ed23
commit
3f4a99ad79
1 changed files with 50 additions and 15 deletions
65
src/main.rs
65
src/main.rs
|
@ -6,9 +6,18 @@ use {
|
||||||
laddertypes::{
|
laddertypes::{
|
||||||
dict::TypeDict, parser::ParseLadderType, subtype_unify, unparser::UnparseLadderType, BimapTypeDict, Morphism, MorphismType
|
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
|
/* 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> {
|
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)]
|
#[derive(Clone, Debug)]
|
||||||
struct LdmcPrimCMorphism {
|
struct LdmcPrimCMorphism {
|
||||||
symbol: String,
|
symbol: String,
|
||||||
type_args: Vec<(String, String)>,
|
type_args: Vec<(laddertypes::TypeID, String)>,
|
||||||
src_type: laddertypes::TypeTerm,
|
src_type: laddertypes::TypeTerm,
|
||||||
dst_type: laddertypes::TypeTerm,
|
dst_type: laddertypes::TypeTerm,
|
||||||
locations: Vec<String>
|
locations: Vec<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LdmcPrimCMorphism {
|
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);",
|
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.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"))
|
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 {
|
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 {
|
match self {
|
||||||
LdmcMorphism::Primitive(prim_morph) => {
|
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");
|
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 src_buf = if i%2 == 0 { "bufA" } else { "bufB" };
|
||||||
let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" };
|
let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" };
|
||||||
|
|
||||||
println!(r#"
|
println!(r#"
|
||||||
{}
|
{}
|
||||||
{} const * restrict src = (void*) {};
|
{} const * restrict src = (void*) {};
|
||||||
|
@ -112,7 +144,7 @@ impl LdmcMorphism {
|
||||||
'{',
|
'{',
|
||||||
src_c_type, src_buf,
|
src_c_type, src_buf,
|
||||||
dst_c_type, dst_buf,
|
dst_c_type, dst_buf,
|
||||||
prim_morph.symbol,
|
prim_morph.instantiated_symbol_name(dict, σ),
|
||||||
'}');
|
'}');
|
||||||
}
|
}
|
||||||
LdmcMorphism::LengthPrefixMap { length_prefix_type, item_morph } => {
|
LdmcMorphism::LengthPrefixMap { length_prefix_type, item_morph } => {
|
||||||
|
@ -146,7 +178,7 @@ impl LdmcMorphism {
|
||||||
src_c_type, src_buf,
|
src_c_type, src_buf,
|
||||||
dst_c_type, dst_buf,
|
dst_c_type, dst_buf,
|
||||||
map_fn,
|
map_fn,
|
||||||
item_morph.symbol,
|
item_morph.instantiated_symbol_name(dict, σ),
|
||||||
'}');
|
'}');
|
||||||
}
|
}
|
||||||
LdmcMorphism::ValueDelimMap { delim, item_morph } => {
|
LdmcMorphism::ValueDelimMap { delim, item_morph } => {
|
||||||
|
@ -164,7 +196,7 @@ impl LdmcMorphism {
|
||||||
'{',
|
'{',
|
||||||
src_c_type, src_buf,
|
src_c_type, src_buf,
|
||||||
dst_c_type, dst_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)| {
|
move |(((symbol, type_args), ((src_type, _), (dst_type, _))), locations)| {
|
||||||
let mut type_dict = type_dict.write().unwrap();
|
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();
|
let type_args : Vec<_> = type_args.into_iter().map(|(v,k)| (v,k.into_iter().collect())).collect();
|
||||||
for (var, kind) in type_args.iter() {
|
let mut ty_args = Vec::new();
|
||||||
type_dict.add_varname(var.clone());
|
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");
|
let mut src_type = type_dict.parse(&src_type.iter().collect::<String>()).expect("couldnt parse src type");
|
||||||
|
@ -262,7 +297,7 @@ fn parser(
|
||||||
|
|
||||||
LdmcPrimCMorphism {
|
LdmcPrimCMorphism {
|
||||||
symbol,
|
symbol,
|
||||||
type_args,
|
type_args: ty_args,
|
||||||
src_type,
|
src_type,
|
||||||
dst_type,
|
dst_type,
|
||||||
locations: locations.into_iter().map(|l| l.into_iter().collect()).collect()
|
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 {
|
let path = morphism_base.find_morphism_path(MorphismType {
|
||||||
src_type: type_dict.parse( src_type_arg.as_str() ).expect(""),
|
src_type: type_dict.parse( src_type_arg.as_str() ).expect(""),
|
||||||
dst_type: type_dict.parse( dst_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 {
|
match path {
|
||||||
|
@ -340,7 +375,7 @@ int main() {{
|
||||||
...with
|
...with
|
||||||
morph {}
|
morph {}
|
||||||
---> {},
|
---> {},
|
||||||
subst σ = {{ {:?} }},
|
subst σ = {:?},
|
||||||
halo Ψ = {}
|
halo Ψ = {}
|
||||||
*/"#,
|
*/"#,
|
||||||
type_dict.unparse(&morph_inst.get_type().dst_type.param_normalize().decurry()),
|
type_dict.unparse(&morph_inst.get_type().dst_type.param_normalize().decurry()),
|
||||||
|
@ -349,7 +384,7 @@ int main() {{
|
||||||
morph_inst.σ,
|
morph_inst.σ,
|
||||||
type_dict.unparse(&morph_inst.halo),
|
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;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue