add morphisms to MorphismBase, first generation of C code for pipe-utility from ladder types

This commit is contained in:
Michael Sippel 2025-02-03 19:21:53 +01:00
parent 1068b8ed45
commit 59c0ea8e57
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -6,7 +6,7 @@ use {
laddertypes::{
dict::TypeDict,
parser::ParseLadderType,
unparser::UnparseLadderType, BimapTypeDict
unparser::UnparseLadderType, BimapTypeDict, MorphismType
},
std::sync::{Arc, RwLock}
};
@ -36,10 +36,10 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_
{
let item_c_type : String = get_c_repr_type(dict, args[2].clone(), false)?;
match item_c_type.as_str() {
"uint8_t" => Some(format!("LengthPrefixUInt8Array")),
"uint16_t" => Some(format!("LengthPrefixUInt16Array")),
"uint32_t" => Some(format!("LengthPrefixUInt32Array")),
"uint64_t" => Some(format!("LengthPrefixUInt64Array")),
"uint8_t" => Some(format!("struct LengthPrefixUInt8Array")),
"uint16_t" => Some(format!("struct LengthPrefixUInt16Array")),
"uint32_t" => Some(format!("struct LengthPrefixUInt32Array")),
"uint64_t" => Some(format!("struct LengthPrefixUInt64Array")),
_ => None
}
}
@ -63,7 +63,7 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_
}
}
#[derive(Debug)]
#[derive(Clone, Debug)]
struct LdmcMorphism {
symbol: String,
type_args: Vec<(String, String)>,
@ -80,16 +80,16 @@ impl LdmcMorphism {
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) {
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 = "bufA";
let dst_buf = "bufB";
let src_buf = if i%2 == 0 { "bufA" } else { "bufB" };
let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" };
println!(
"{}
{} const * restrict src = {};
{} * restrict dst = {};
{} const * restrict src = (void*) {};
{} * restrict dst = (void*) {};
{} ( src, dst );
{}",
'{',
@ -100,6 +100,23 @@ impl LdmcMorphism {
}
}
impl laddertypes::Morphism for LdmcMorphism {
fn weight(&self) -> u64 {
1
}
fn get_type(&self) -> laddertypes::MorphismType {
laddertypes::MorphismType {
src_type: self.src_type.clone().normalize(),
dst_type: self.dst_type.clone().normalize()
}
}
fn list_map_morphism(&self, list_typeid: laddertypes::TypeID) -> Option< Self > {
None
}
}
/* morphism-base text format:
* NAME '(' [TYPE-ARG-NAME ':' KIND] ')'
* SRC-TYPE
@ -134,8 +151,8 @@ fn parser(
type_dict.add_varname(var.clone());
}
let src_type = type_dict.parse(&src_type.iter().collect::<String>()).expect("couldnt parse src type");
let dst_type = type_dict.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst 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");
LdmcMorphism {
symbol,
@ -149,7 +166,10 @@ fn parser(
}
fn main() {
let 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());
let mut morphism_base = laddertypes::MorphismBase::<LdmcMorphism>::new(seq_typeid);
for mb_path in std::env::args().skip(1) {
let src = std::fs::read_to_string(
@ -159,18 +179,16 @@ fn main() {
let result = parser(type_dict.clone()).parse(src.clone());
match result {
Ok(morphisms) => {
println!("parse ok.");
eprintln!("parse ok.");
let mut dict = type_dict.write().unwrap();
for m in morphisms {
println!("{}\n {}\n---> \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.dst_type.clone().sugar(&mut *dict).pretty(&mut *dict, 1),
m.expected_c_type_signature(&mut *dict),
);
m.generate_call(&mut *dict);
morphism_base.add_morphism(m);
}
}
Err(errs) => {
@ -189,4 +207,64 @@ 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(""),
});
match path {
Some(path) => {
eprintln!("{:?}", 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() {}
uint8_t bufA[1024];
uint8_t bufB[1024];
scanf("%s", bufA);
"#, '{');
for dst_type in path {
let m = morphism_base.find_morphism_with_subtyping(&MorphismType {
src_type, dst_type: dst_type.clone()
});
if let Some((m, typ, _)) = m {
println!(r#"
/* morph to {} */"#,
m.dst_type.clone().param_normalize().decurry()
.sugar(&mut type_dict)
.pretty(&mut type_dict, 1)
);
m.generate_call(&mut type_dict, i);
i += 1;
}
src_type = dst_type;
}
let out_buf = if i%2==0 { "bufA" } else { "bufB" };
println!(r#"
printf("%s\n", {});
return 0;
{}
"#, out_buf, '}');
}
None => {
eprintln!("Error: could not find morphism path");
}
}
}