wip generating map morphism

This commit is contained in:
Michael Sippel 2025-02-05 11:26:55 +01:00
parent e07b3585f9
commit 959d2f9185
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -6,7 +6,8 @@ use {
laddertypes::{ laddertypes::{
dict::TypeDict, dict::TypeDict,
parser::ParseLadderType, parser::ParseLadderType,
unparser::UnparseLadderType, BimapTypeDict, MorphismType unparser::UnparseLadderType, BimapTypeDict, MorphismType,
Morphism
}, },
std::sync::{Arc, RwLock} std::sync::{Arc, RwLock}
}; };
@ -34,6 +35,7 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_
laddertypes::TypeTerm::App(args) => { laddertypes::TypeTerm::App(args) => {
if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"LengthPrefix".into()).unwrap()) if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"LengthPrefix".into()).unwrap())
{ {
let _length_type = args[1].clone();
let item_c_type : String = get_c_repr_type(dict, args[2].clone(), false)?; let item_c_type : String = get_c_repr_type(dict, args[2].clone(), false)?;
match item_c_type.as_str() { match item_c_type.as_str() {
"uint8_t" => Some(format!("struct LengthPrefixUInt8Array")), "uint8_t" => Some(format!("struct LengthPrefixUInt8Array")),
@ -45,6 +47,7 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_
} }
else if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"ValueDelim".into()).unwrap()) else if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"ValueDelim".into()).unwrap())
{ {
let _delim_val = args[1].clone();
let c_type = get_c_repr_type(dict, args[2].clone(), false)?; let c_type = get_c_repr_type(dict, args[2].clone(), false)?;
if skip_pointer { if skip_pointer {
Some(c_type) Some(c_type)
@ -64,7 +67,7 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct LdmcMorphism { struct LdmcPrimCMorphism {
symbol: String, symbol: String,
type_args: Vec<(String, String)>, type_args: Vec<(String, String)>,
src_type: laddertypes::TypeTerm, src_type: laddertypes::TypeTerm,
@ -72,7 +75,7 @@ struct LdmcMorphism {
locations: Vec<String> locations: Vec<String>
} }
impl LdmcMorphism { impl LdmcPrimCMorphism {
pub fn expected_c_type_signature(&self, dict: &mut impl TypeDict) -> String { pub fn expected_c_type_signature(&self, dict: &mut impl TypeDict) -> String {
format!("int {} ({} const * restrict src, {} * restrict dst);", format!("int {} ({} const * restrict src, {} * restrict dst);",
self.symbol, self.symbol,
@ -81,39 +84,117 @@ impl LdmcMorphism {
} }
pub fn generate_call(&self, dict: &mut impl TypeDict, i: u64) { pub fn generate_call(&self, dict: &mut impl TypeDict, i: u64) {
let src_c_type = get_c_repr_type(dict, self.src_type.clone(), true).expect("cant get c-repr type for src type");
let dst_c_type = get_c_repr_type(dict, self.dst_type.clone(), true).expect("cant get c-repr type for dst type");
let src_buf = if i%2 == 0 { "bufA" } else { "bufB" };
let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" };
println!(
"{}
{} const * restrict src = (void*) {};
{} * restrict dst = (void*) {};
{} ( src, dst );
{}",
'{',
src_c_type, src_buf,
dst_c_type, dst_buf,
self.symbol,
'}');
} }
} }
impl laddertypes::Morphism for LdmcMorphism { #[derive(Clone)]
enum LdmcMorphism {
Primitive( LdmcPrimCMorphism ),
LengthPrefixMap{
length_prefix_type: laddertypes::TypeTerm,
item_morph: Box<LdmcPrimCMorphism>
},
ValueDelimMap{
delim: u64,
item_morph: Box<LdmcMorphism>
}
}
impl LdmcMorphism {
pub fn generate_call(&self, dict: &mut impl TypeDict, 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");
let dst_c_type = get_c_repr_type(dict, prim_morph.dst_type.clone(), true).expect("cant get c-repr type for dst type");
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*) {};
{} * restrict dst = (void*) {};
{} ( src, dst );
{}"#,
'{',
src_c_type, src_buf,
dst_c_type, dst_buf,
prim_morph.symbol,
'}');
}
LdmcMorphism::LengthPrefixMap { length_prefix_type, item_morph } => {
let src_c_type = get_c_repr_type(dict, item_morph.src_type.clone(), false).expect("cant get c-repr type for src type");
let dst_c_type = get_c_repr_type(dict, item_morph.dst_type.clone(), false).expect("cant get c-repr type for dst type");
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*) {};
{} * restrict dst = (void*) {};
length_prefix_array_map_8_to_64( {}, src, dst );
{}"#,
'{',
src_c_type, src_buf,
dst_c_type, dst_buf,
item_morph.symbol,
'}');
}
LdmcMorphism::ValueDelimMap { delim, item_morph } => {
}
}
}
}
impl Morphism for LdmcMorphism {
fn weight(&self) -> u64 { fn weight(&self) -> u64 {
1 1
} }
fn get_type(&self) -> laddertypes::MorphismType { fn get_type(&self) -> laddertypes::MorphismType {
laddertypes::MorphismType { match self {
src_type: self.src_type.clone().normalize(), LdmcMorphism::Primitive(prim_morph) =>
dst_type: self.dst_type.clone().normalize() 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![]);
let item_morph_type = item_morph.get_type();
laddertypes::MorphismType {
src_type: laddertypes::TypeTerm::App(vec![ value_delim_type.clone(), item_morph_type.src_type ]),
dst_type: laddertypes::TypeTerm::App(vec![ value_delim_type.clone(), item_morph_type.dst_type ]),
}
}
} }
} }
fn list_map_morphism(&self, list_typeid: laddertypes::TypeID) -> Option< Self > { fn map_morphism(&self, seq_type: laddertypes::TypeTerm) -> Option< Self > {
None 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
}
} }
} }
@ -125,7 +206,7 @@ impl laddertypes::Morphism for LdmcMorphism {
*/ */
fn parser( fn parser(
type_dict: Arc<RwLock< BimapTypeDict >> type_dict: Arc<RwLock< BimapTypeDict >>
) -> impl Parser<char, Vec<LdmcMorphism>, Error = Simple<char>> { ) -> impl Parser<char, Vec<LdmcPrimCMorphism>, Error = Simple<char>> {
ident().padded() ident().padded()
.then( .then(
@ -154,7 +235,7 @@ fn parser(
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");
let mut dst_type = type_dict.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst type"); let mut dst_type = type_dict.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst type");
LdmcMorphism { LdmcPrimCMorphism {
symbol, symbol,
type_args, type_args,
src_type, src_type,
@ -167,10 +248,22 @@ fn parser(
fn main() { fn main() {
let mut type_dict = Arc::new(RwLock::new(BimapTypeDict::new())); let mut type_dict = Arc::new(RwLock::new(BimapTypeDict::new()));
let seq_typeid = type_dict.write().unwrap().add_typename("Seq".into()); type_dict.add_varname("Delim".into());
let mut morphism_base = laddertypes::MorphismBase::<LdmcMorphism>::new(vec![
type_dict.parse("Seq ~ <ValueDelim '\\0'>").expect(""),
type_dict.parse("Seq ~ <LengthPrefix x86.UInt64>").expect("")
]);
/*
let mut t = type_dict.parse("<Seq~<ValueDelim '\0'> Char>~<Seq~<ValueDelim '\0'> Ascii~Byte>").expect("");
let mut morphism_base = laddertypes::MorphismBase::<LdmcMorphism>::new(seq_typeid); eprintln!("init :: {}\n", t.clone().sugar(&mut type_dict).pretty(&mut type_dict, 0));
//t = t.normalize();
//eprintln!("LNF :: {}\n", t.clone().sugar(&mut type_dict).pretty(&mut type_dict, 0));
t = t.param_normalize();
println!("PNF :: {}\n", t.sugar(&mut type_dict).pretty(&mut type_dict, 0));
return;
*/
for mb_path in std::env::args().skip(1) { for mb_path in std::env::args().skip(1) {
let src = std::fs::read_to_string( let src = std::fs::read_to_string(
mb_path mb_path
@ -183,12 +276,13 @@ fn main() {
let mut dict = type_dict.write().unwrap(); let mut dict = type_dict.write().unwrap();
for m in morphisms { for m in morphisms {
/*
eprintln!("{}\n {}\n---> \n {}\n", m.symbol, eprintln!("{}\n {}\n---> \n {}\n", m.symbol,
m.src_type.clone().sugar(&mut *dict).pretty(&mut *dict, 1), m.src_type.clone().sugar(&mut *dict).pretty(&mut *dict, 1),
m.dst_type.clone().sugar(&mut *dict).pretty(&mut *dict, 1), m.dst_type.clone().sugar(&mut *dict).pretty(&mut *dict, 1),
); );
*/
morphism_base.add_morphism(m); morphism_base.add_morphism(LdmcMorphism::Primitive(m));
} }
} }
Err(errs) => { Err(errs) => {
@ -208,7 +302,6 @@ fn main() {
} }
} }
let path = morphism_base.find_morphism_path(MorphismType { let path = morphism_base.find_morphism_path(MorphismType {
src_type: type_dict.parse(" ~ <PosInt 10 BigEndian> ~ <Seq~<LengthPrefix x86.UInt64> <Digit 10>~x86.UInt64>").expect(""), src_type: type_dict.parse(" ~ <PosInt 10 BigEndian> ~ <Seq~<LengthPrefix x86.UInt64> <Digit 10>~x86.UInt64>").expect(""),
dst_type: type_dict.parse(" ~ <PosInt 16 BigEndian> ~ <Seq~<LengthPrefix x86.UInt64> <Digit 16>~x86.UInt64>").expect(""), dst_type: type_dict.parse(" ~ <PosInt 16 BigEndian> ~ <Seq~<LengthPrefix x86.UInt64> <Digit 16>~x86.UInt64>").expect(""),
@ -217,7 +310,6 @@ fn main() {
match path { match path {
Some(path) => { Some(path) => {
eprintln!("{:?}", path);
let mut path = path.into_iter(); let mut path = path.into_iter();
let mut src_type = path.next().unwrap(); let mut src_type = path.next().unwrap();
@ -236,23 +328,29 @@ int main() {}
scanf("%s", bufA); scanf("%s", bufA);
"#, '{'); "#, '{');
for dst_type in path { for dst_type in path {
eprintln!("add morph from {}\nto {}
",
src_type.clone().param_normalize().sugar(&mut type_dict).pretty(&mut type_dict, 1),
dst_type.clone().param_normalize().sugar(&mut type_dict).pretty(&mut type_dict, 1),
);
let (m, typ, σ) = morphism_base.find_morphism_with_subtyping(&MorphismType {
src_type: src_type.param_normalize(),
dst_type: dst_type.clone().param_normalize()
}).expect("cant find morphism");
let m = morphism_base.find_morphism_with_subtyping(&MorphismType { let dst_type = m.get_type().dst_type;
src_type, dst_type: dst_type.clone()
});
if let Some((m, typ, _)) = m { println!(r#"
println!(r#"
/* morph to {} */"#, /* morph to {} */"#,
m.dst_type.clone().param_normalize().decurry() dst_type.clone().param_normalize().decurry()
.sugar(&mut type_dict) .sugar(&mut type_dict)
.pretty(&mut type_dict, 1) .pretty(&mut type_dict, 1)
); );
m.generate_call(&mut type_dict, i); m.generate_call(&mut type_dict, i);
i += 1; i += 1;
}
src_type = dst_type; src_type = laddertypes::TypeTerm::Ladder(vec![ typ, dst_type ]);
src_type = src_type.normalize().apply_substitution(&|k| σ.get(k).cloned()).clone();
} }
let out_buf = if i%2==0 { "bufA" } else { "bufB" }; let out_buf = if i%2==0 { "bufA" } else { "bufB" };