From 59c0ea8e574e9886f9969cee8853e75bc7d3eff7 Mon Sep 17 00:00:00 2001 From: Michael Sippel <micha@fragmental.art> Date: Mon, 3 Feb 2025 19:21:53 +0100 Subject: [PATCH 1/4] add morphisms to MorphismBase, first generation of C code for pipe-utility from ladder types --- src/main.rs | 116 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 97 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index fc14210..9d5f126 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"); + } + } } From 10dab738768fab910a1a531eeafa00aff4a6da1e Mon Sep 17 00:00:00 2001 From: Michael Sippel <micha@fragmental.art> Date: Wed, 5 Feb 2025 11:10:11 +0100 Subject: [PATCH 2/4] digit morphisms: switch char to uint8_t --- morphisms/include/morphisms/posint.h | 15 +++++++------ morphisms/src/posint.c | 32 +++++++++------------------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/morphisms/include/morphisms/posint.h b/morphisms/include/morphisms/posint.h index d44db28..3c96136 100644 --- a/morphisms/include/morphisms/posint.h +++ b/morphisms/include/morphisms/posint.h @@ -3,32 +3,33 @@ #include <morphisms/length-prefix.h> int morph_digit_as_char_to_uint8( - char const * restrict src, + uint64_t const radix, + uint8_t const * restrict src, uint8_t * restrict dst ); int morph_digit_as_char_to_uint64( - char const * restrict src, + uint64_t const radix, + uint8_t const * restrict src, uint64_t * restrict dst ); int morph_digit_as_uint8_to_char( + uint64_t const radix, uint8_t const * restrict src, - char * restrict dst + uint8_t * restrict dst ); int morph_digit_as_uint64_to_char( + uint64_t const radix, uint64_t const * restrict src, - char * restrict dst + uint8_t * restrict dst ); - int morph_posint_endianness( uint64_t const radix, - struct LengthPrefixUInt64Array const * restrict src, struct LengthPrefixUInt64Array * restrict dst ); int morph_posint_radix( uint64_t const src_radix, uint64_t const dst_radix, - struct LengthPrefixUInt64Array const * restrict src, struct LengthPrefixUInt64Array * restrict dst ); diff --git a/morphisms/src/posint.c b/morphisms/src/posint.c index 65292a9..32503c8 100644 --- a/morphisms/src/posint.c +++ b/morphisms/src/posint.c @@ -2,7 +2,8 @@ #include <morphisms/length-prefix.h> int morph_digit_as_char_to_uint8( - char const * restrict src, + uint64_t const radix, + uint8_t const * restrict src, uint8_t * restrict dst ) { if( *src >= '0' && *src <= '9' ) @@ -18,24 +19,17 @@ int morph_digit_as_char_to_uint8( } int morph_digit_as_char_to_uint64( - char const * restrict src, + uint64_t const radix, + uint8_t const * restrict src, uint64_t * restrict dst ) { - if( *src >= '0' && *src <= '9' ) - *dst = *src - '0'; - else if( *src >= 'a' && *src <= 'f') - *dst = 0xa + *src - 'a'; - else if( *src >= 'A' && *src <= 'F') - *dst = 0xa + *src - 'A'; - else - return -1; - - return 0; + return morph_digit_as_char_to_uint8(radix, src, dst); } int morph_digit_as_uint8_to_char( + uint64_t const radix, uint8_t const * restrict src, - char * restrict dst + uint8_t * restrict dst ) { if ( *src < 10 ) *dst = *src + '0'; @@ -48,17 +42,11 @@ int morph_digit_as_uint8_to_char( } int morph_digit_as_uint64_to_char( + uint64_t const radix, uint64_t const * restrict src, - char * restrict dst + uint8_t * restrict dst ) { - if ( *src < 10 ) - *dst = *src + '0'; - else if( *dst < 16 ) - *dst = *src - 0xa + 'a'; - else - return -1; - - return 0; + return morph_digit_as_uint8_to_char(radix, src, dst); } /* switches endianness by reversing the digit sequence From e07b3585f9e27822ed97dcf7acbcfe60e83244c6 Mon Sep 17 00:00:00 2001 From: Michael Sippel <micha@fragmental.art> Date: Wed, 5 Feb 2025 11:25:53 +0100 Subject: [PATCH 3/4] add C functions for map lengthPrefix --- morphisms/Makefile | 2 +- morphisms/include/morphisms/length-prefix.h | 20 ++++++++- morphisms/include/morphisms/posint.h | 8 ++-- morphisms/posint-dec-to-hex-generated.c | 7 +++- morphisms/src/length-prefix.c | 45 +++++++++++++++++++++ morphisms/src/posint.c | 12 +++--- 6 files changed, 81 insertions(+), 13 deletions(-) diff --git a/morphisms/Makefile b/morphisms/Makefile index 6526bf8..439499a 100644 --- a/morphisms/Makefile +++ b/morphisms/Makefile @@ -1,5 +1,5 @@ LIB_DIR=$(shell pwd)/lib -TARGETS=lib/libmorph_length-prefix.so lib/libmorph_posint.so test +TARGETS=lib/libmorph_length-prefix.so lib/libmorph_posint.so all: $(TARGETS) diff --git a/morphisms/include/morphisms/length-prefix.h b/morphisms/include/morphisms/length-prefix.h index 1ce7346..d3eb76f 100644 --- a/morphisms/include/morphisms/length-prefix.h +++ b/morphisms/include/morphisms/length-prefix.h @@ -23,7 +23,6 @@ void length_prefix_uint8_array_dump( struct LengthPrefixUInt8Array const * data ); - /* UInt64 */ struct LengthPrefixUInt64Array { @@ -46,6 +45,25 @@ void length_prefix_uint64_array_dump( struct LengthPrefixUInt64Array const * data ); +/* + * Map + */ +int length_prefix_array_map_64_to_64( + int (*f) ( uint64_t const * restrict, uint64_t * restrict ), + struct LengthPrefixUInt64Array const * restrict src, + struct LengthPrefixUInt64Array * restrict dst +); +int length_prefix_array_map_8_to_64( + int (*f) ( uint8_t const * restrict, uint64_t * restrict ), + struct LengthPrefixUInt8Array const * restrict src, + struct LengthPrefixUInt64Array * restrict dst +); +int length_prefix_array_map_64_to_8( + int (*f) ( uint64_t const * restrict, uint8_t * restrict ), + struct LengthPrefixUInt64Array const * restrict src, + struct LengthPrefixUInt8Array * restrict dst +); + /* * Morphisms diff --git a/morphisms/include/morphisms/posint.h b/morphisms/include/morphisms/posint.h index 3c96136..8c458da 100644 --- a/morphisms/include/morphisms/posint.h +++ b/morphisms/include/morphisms/posint.h @@ -3,22 +3,22 @@ #include <morphisms/length-prefix.h> int morph_digit_as_char_to_uint8( - uint64_t const radix, + //uint64_t const radix, uint8_t const * restrict src, uint8_t * restrict dst ); int morph_digit_as_char_to_uint64( - uint64_t const radix, + //uint64_t const radix, uint8_t const * restrict src, uint64_t * restrict dst ); int morph_digit_as_uint8_to_char( - uint64_t const radix, + //uint64_t const radix, uint8_t const * restrict src, uint8_t * restrict dst ); int morph_digit_as_uint64_to_char( - uint64_t const radix, + //uint64_t const radix, uint64_t const * restrict src, uint8_t * restrict dst ); diff --git a/morphisms/posint-dec-to-hex-generated.c b/morphisms/posint-dec-to-hex-generated.c index f1aa7cf..a942e66 100644 --- a/morphisms/posint-dec-to-hex-generated.c +++ b/morphisms/posint-dec-to-hex-generated.c @@ -22,10 +22,12 @@ int main() { { struct LengthPrefixUInt8Array * src = (void*) bufB; struct LengthPrefixUInt64Array * dst = (void*) bufA; - + length_prefix_array_map_8_to_64( morph_digit_as_char_to_uint64, src, dst ); +/* dst->len = src->len; for( uint64_t i = 0; i < src->len; ++i ) morph_digit_as_char_to_uint64( &src->items[i], &dst->items[i] ); +*/ } // morph to @@ -63,9 +65,12 @@ int main() { struct LengthPrefixUInt64Array * src = (void*) bufB; struct LengthPrefixUInt8Array * dst = (void*) bufA; + length_prefix_array_map_64_to_8( morph_digit_as_uint64_to_char, src, dst ); +/* dst->len = src->len; for( uint64_t i = 0; i < src->len; ++i ) morph_digit_as_uint64_to_char( &src->items[i], &dst->items[i] ); +*/ } // morph to diff --git a/morphisms/src/length-prefix.c b/morphisms/src/length-prefix.c index 12f6234..32029bf 100644 --- a/morphisms/src/length-prefix.c +++ b/morphisms/src/length-prefix.c @@ -28,6 +28,51 @@ int length_prefix_uint64_array_reverse( return 0; } +int length_prefix_array_map_64_to_64( + int (*f) ( uint64_t const * restrict, uint64_t * restrict ), + struct LengthPrefixUInt64Array const * restrict src, + struct LengthPrefixUInt64Array * restrict dst +) { + dst->len = src->len; + for( uint64_t i = 0; i < src->len; ++i ) { + int result = f( &src->items[i], &dst->items[i] ); + if( result ) { + return result; + } + } + return 0; +} + +int length_prefix_array_map_8_to_64( + int (*f) ( uint8_t const * restrict, uint64_t * restrict ), + struct LengthPrefixUInt8Array const * restrict src, + struct LengthPrefixUInt64Array * restrict dst +) { + dst->len = src->len; + for( uint64_t i = 0; i < src->len; ++i ) { + int result = f( &src->items[i], &dst->items[i] ); + if( result ) { + return result; + } + } + return 0; +} + +int length_prefix_array_map_64_to_8( + int (*f) ( uint64_t const * restrict, uint8_t * restrict ), + struct LengthPrefixUInt64Array const * restrict src, + struct LengthPrefixUInt8Array * restrict dst +) { + dst->len = src->len; + for( uint64_t i = 0; i < src->len; ++i ) { + int result = f( &src->items[i], &dst->items[i] ); + if( result ) { + return result; + } + } + return 0; +} + void length_prefix_uint64_array_dump( struct LengthPrefixUInt64Array const * data ) { diff --git a/morphisms/src/posint.c b/morphisms/src/posint.c index 32503c8..83cf6be 100644 --- a/morphisms/src/posint.c +++ b/morphisms/src/posint.c @@ -2,7 +2,7 @@ #include <morphisms/length-prefix.h> int morph_digit_as_char_to_uint8( - uint64_t const radix, + //uint64_t const radix, uint8_t const * restrict src, uint8_t * restrict dst ) { @@ -19,15 +19,15 @@ int morph_digit_as_char_to_uint8( } int morph_digit_as_char_to_uint64( - uint64_t const radix, + //uint64_t const radix, uint8_t const * restrict src, uint64_t * restrict dst ) { - return morph_digit_as_char_to_uint8(radix, src, dst); + return morph_digit_as_char_to_uint8(src, (void*)dst); } int morph_digit_as_uint8_to_char( - uint64_t const radix, + //uint64_t const radix, uint8_t const * restrict src, uint8_t * restrict dst ) { @@ -42,11 +42,11 @@ int morph_digit_as_uint8_to_char( } int morph_digit_as_uint64_to_char( - uint64_t const radix, + //uint64_t const radix, uint64_t const * restrict src, uint8_t * restrict dst ) { - return morph_digit_as_uint8_to_char(radix, src, dst); + return morph_digit_as_uint8_to_char((void*)src, dst); } /* switches endianness by reversing the digit sequence From 959d2f9185d5ea0ec9ddc47cbeb54ea62b88805e Mon Sep 17 00:00:00 2001 From: Michael Sippel <micha@fragmental.art> Date: Wed, 5 Feb 2025 11:26:55 +0100 Subject: [PATCH 4/4] wip generating map morphism --- src/main.rs | 182 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 140 insertions(+), 42 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9d5f126..a31db2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,8 @@ use { laddertypes::{ dict::TypeDict, parser::ParseLadderType, - unparser::UnparseLadderType, BimapTypeDict, MorphismType + unparser::UnparseLadderType, BimapTypeDict, MorphismType, + Morphism }, 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) => { 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)?; match item_c_type.as_str() { "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()) { + let _delim_val = args[1].clone(); let c_type = get_c_repr_type(dict, args[2].clone(), false)?; if skip_pointer { Some(c_type) @@ -64,7 +67,7 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_ } #[derive(Clone, Debug)] -struct LdmcMorphism { +struct LdmcPrimCMorphism { symbol: String, type_args: Vec<(String, String)>, src_type: laddertypes::TypeTerm, @@ -72,7 +75,7 @@ struct LdmcMorphism { locations: Vec<String> } -impl LdmcMorphism { +impl LdmcPrimCMorphism { pub fn expected_c_type_signature(&self, dict: &mut impl TypeDict) -> String { format!("int {} ({} const * restrict src, {} * restrict dst);", self.symbol, @@ -81,39 +84,117 @@ impl LdmcMorphism { } 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 { 1 } fn get_type(&self) -> laddertypes::MorphismType { - laddertypes::MorphismType { - src_type: self.src_type.clone().normalize(), - dst_type: self.dst_type.clone().normalize() + match self { + LdmcMorphism::Primitive(prim_morph) => + 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 > { - None + 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 { + 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( type_dict: Arc<RwLock< BimapTypeDict >> -) -> impl Parser<char, Vec<LdmcMorphism>, Error = Simple<char>> { +) -> impl Parser<char, Vec<LdmcPrimCMorphism>, Error = Simple<char>> { ident().padded() .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 dst_type = type_dict.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst type"); - LdmcMorphism { + LdmcPrimCMorphism { symbol, type_args, src_type, @@ -167,10 +248,22 @@ fn parser( fn main() { 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) { let src = std::fs::read_to_string( mb_path @@ -183,12 +276,13 @@ fn main() { 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(m); + */ + morphism_base.add_morphism(LdmcMorphism::Primitive(m)); } } Err(errs) => { @@ -208,7 +302,6 @@ 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(""), @@ -217,7 +310,6 @@ fn main() { match path { Some(path) => { - eprintln!("{:?}", path); let mut path = path.into_iter(); let mut src_type = path.next().unwrap(); @@ -236,23 +328,29 @@ int main() {} 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 m = morphism_base.find_morphism_with_subtyping(&MorphismType { - src_type, dst_type: dst_type.clone() - }); + let dst_type = m.get_type().dst_type; - if let Some((m, typ, _)) = m { - println!(r#" + println!(r#" /* morph to {} */"#, - m.dst_type.clone().param_normalize().decurry() + dst_type.clone().param_normalize().decurry() .sugar(&mut type_dict) .pretty(&mut type_dict, 1) ); - m.generate_call(&mut type_dict, i); - i += 1; - } + m.generate_call(&mut type_dict, i); + 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" };