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 d44db28..8c458da 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/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 65292a9..83cf6be 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(src, (void*)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((void*)src, dst); } /* switches endianness by reversing the digit sequence diff --git a/src/main.rs b/src/main.rs index fc14210..a31db2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,8 @@ use { laddertypes::{ dict::TypeDict, parser::ParseLadderType, - unparser::UnparseLadderType, BimapTypeDict + unparser::UnparseLadderType, BimapTypeDict, MorphismType, + Morphism }, std::sync::{Arc, RwLock} }; @@ -34,17 +35,19 @@ 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!("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 } } 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) @@ -63,8 +66,8 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_ } } -#[derive(Debug)] -struct LdmcMorphism { +#[derive(Clone, Debug)] +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, @@ -80,23 +83,118 @@ 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) { - 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"); + pub fn generate_call(&self, dict: &mut impl TypeDict, i: u64) { - let src_buf = "bufA"; - let dst_buf = "bufB"; - println!( -"{} - {} const * restrict src = {}; - {} * restrict dst = {}; - {} ( src, dst ); -{}", - '{', - src_c_type, src_buf, - dst_c_type, dst_buf, - self.symbol, - '}'); + } +} + +#[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 { + 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 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 + } } } @@ -108,7 +206,7 @@ impl 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( @@ -134,10 +232,10 @@ 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 { + LdmcPrimCMorphism { symbol, type_args, src_type, @@ -149,8 +247,23 @@ fn parser( } fn main() { - let type_dict = Arc::new(RwLock::new(BimapTypeDict::new())); + 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("") + ]); +/* + 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 @@ -159,18 +272,17 @@ 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(LdmcMorphism::Primitive(m)); } } Err(errs) => { @@ -189,4 +301,68 @@ 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) => { + 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 { + 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; + + 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; + + 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" }; + println!(r#" + printf("%s\n", {}); + + return 0; +{} + "#, out_buf, '}'); + } + None => { + eprintln!("Error: could not find morphism path"); + } + } }