more work on code generation

This commit is contained in:
Michael Sippel 2025-02-14 14:07:18 +01:00
parent c270efbb87
commit afa326ed23
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -4,10 +4,7 @@ use {
prelude::*, text::*
},
laddertypes::{
dict::TypeDict,
parser::ParseLadderType,
unparser::UnparseLadderType, BimapTypeDict, MorphismType,
Morphism
dict::TypeDict, parser::ParseLadderType, subtype_unify, unparser::UnparseLadderType, BimapTypeDict, Morphism, MorphismType
},
std::sync::{Arc, RwLock}
};
@ -82,10 +79,6 @@ impl LdmcPrimCMorphism {
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"))
}
pub fn generate_call(&self, dict: &mut impl TypeDict, i: u64) {
}
}
#[derive(Clone)]
@ -97,7 +90,7 @@ enum LdmcMorphism {
},
ValueDelimMap{
delim: u64,
item_morph: Box<LdmcMorphism>
item_morph: Box<LdmcPrimCMorphism>
}
}
@ -123,6 +116,40 @@ impl LdmcMorphism {
'}');
}
LdmcMorphism::LengthPrefixMap { length_prefix_type, item_morph } => {
let src_c_type = get_c_repr_type(dict, self.get_type().src_type, true).expect("cant get c-repr type for src type");
let dst_c_type = get_c_repr_type(dict, self.get_type().dst_type, true).expect("cant get c-repr type for dst type");
let map_fn = match (src_c_type.as_str(), dst_c_type.as_str()) {
("struct LengthPrefixUInt64Array", "struct LengthPrefixUInt64Array") => {
"length_prefix_array_map_64_to_64"
},
("struct LengthPrefixUInt8Array", "struct LengthPrefixUInt64Array") => {
"length_prefix_array_map_8_to_64"
},
("struct LengthPrefixUInt64Array", "struct LengthPrefixUInt8Array") => {
"length_prefix_array_map_64_to_8"
},
_ => {
"{{ ERROR: no map function implemented }}"
}
};
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,
map_fn,
item_morph.symbol,
'}');
}
LdmcMorphism::ValueDelimMap { delim, 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");
@ -132,7 +159,7 @@ impl LdmcMorphism {
{}
{} const * restrict src = (void*) {};
{} * restrict dst = (void*) {};
length_prefix_array_map_8_to_64( {}, src, dst );
value_delim_array_map_8_to_64( {}, src, dst );
{}"#,
'{',
src_c_type, src_buf,
@ -140,9 +167,6 @@ impl LdmcMorphism {
item_morph.symbol,
'}');
}
LdmcMorphism::ValueDelimMap { delim, item_morph } => {
}
}
}
}
@ -167,20 +191,21 @@ impl Morphism for LdmcMorphism {
},
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 ]),
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() ]),
}
}
}
}.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 {
/*
if seq_type == self.length_prefix_type {
*/
Some(LdmcMorphism::LengthPrefixMap{
length_prefix_type: seq_type,
item_morph,
@ -248,40 +273,22 @@ fn parser(
fn main() {
let mut type_dict = Arc::new(RwLock::new(BimapTypeDict::new()));
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("")
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("");
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) {
let src = std::fs::read_to_string(
mb_path
).expect("read");
let mut args = std::env::args().skip(1);
let src_type_arg = args.next().expect("src type expected");
let dst_type_arg = args.next().expect("dst type expected");
for mb_path in args {
let src = std::fs::read_to_string(mb_path).expect("read");
let result = parser(type_dict.clone()).parse(src.clone());
match result {
Ok(morphisms) => {
eprintln!("parse ok.");
let mut dict = type_dict.write().unwrap();
for m in morphisms {
/*
eprintln!("{}\n {}\n---> \n {}\n", m.symbol,
m.src_type.clone().sugar(&mut *dict).pretty(&mut *dict, 1),
m.dst_type.clone().sugar(&mut *dict).pretty(&mut *dict, 1),
);
*/
morphism_base.add_morphism(LdmcMorphism::Primitive(m));
}
}
@ -302,55 +309,48 @@ fn main() {
}
}
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(""),
dst_type: type_dict.parse(" ~ <PosInt 16 BigEndian> ~ <Seq~<LengthPrefix x86.UInt64> <Digit 16>~x86.UInt64>").expect(""),
});
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()
);
match path {
Some(path) => {
let mut path = path.into_iter();
let mut src_type = path.next().unwrap();
let mut i = 0;
println!(r#"
#include <stdio.h>
#include <stdint.h>
#include <morphisms/length-prefix.h>
#include <morphisms/posint.h>
int main() {}
int main() {{
uint8_t bufA[1024];
uint8_t bufB[1024];
scanf("%s", bufA);
"#, '{');
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 dst_type = m.get_type().dst_type;
"#);
for morph_inst in path {
println!(r#"
/* morph to {} */"#,
dst_type.clone().param_normalize().decurry()
.sugar(&mut type_dict)
.pretty(&mut type_dict, 1)
);
m.generate_call(&mut type_dict, i);
i += 1;
/* morph to {}
src_type = laddertypes::TypeTerm::Ladder(vec![ typ, dst_type ]);
src_type = src_type.normalize().apply_substitution(&|k| σ.get(k).cloned()).clone();
...with
morph {}
---> {},
subst σ = {{ {:?} }},
halo Ψ = {}
*/"#,
type_dict.unparse(&morph_inst.get_type().dst_type.param_normalize().decurry()),
type_dict.unparse(&morph_inst.m.get_type().src_type),
type_dict.unparse(&morph_inst.m.get_type().dst_type),
morph_inst.σ,
type_dict.unparse(&morph_inst.halo),
);
morph_inst.m.generate_call(&mut type_dict, i);
i += 1;
}
let out_buf = if i%2==0 { "bufA" } else { "bufB" };
@ -358,8 +358,8 @@ int main() {}
printf("%s\n", {});
return 0;
{}
"#, out_buf, '}');
}}
"#, out_buf);
}
None => {
eprintln!("Error: could not find morphism path");