Compare commits

...

10 commits

8 changed files with 235 additions and 61 deletions

View file

@ -7,3 +7,4 @@ edition = "2024"
chumsky = "0.9.0" chumsky = "0.9.0"
ariadne = "0.2" ariadne = "0.2"
laddertypes = { path = "../lib-laddertypes", features = ["pretty"] } laddertypes = { path = "../lib-laddertypes", features = ["pretty"] }
tiny-ansi = { version = "0.1.0" }

View file

@ -1,6 +0,0 @@
*.so
posint-dec-to-hex-generated-gcc
posint-dec-to-hex-generated-clang
posint-dec-to-hex-optimal-gcc
posint-dec-to-hex-optimal-clang

View file

@ -1,3 +1,6 @@
```
```
morph_digit_as_char_to_uint8 (Radix:) morph_digit_as_char_to_uint8 (Radix:)
<Digit Radix> ~ Char ~ Ascii ~ x86.UInt8 <Digit Radix> ~ Char ~ Ascii ~ x86.UInt8
--> <Digit Radix> ~ x86.UInt8 --> <Digit Radix> ~ x86.UInt8

View file

@ -63,3 +63,8 @@ int length_prefix_array_map_64_to_8(
struct LengthPrefixUInt64Array const * restrict src, struct LengthPrefixUInt64Array const * restrict src,
struct LengthPrefixUInt8Array * restrict dst struct LengthPrefixUInt8Array * restrict dst
); );
int length_prefix_array_map_8_to_8(
int (*f) ( uint8_t const * restrict, uint8_t * restrict ),
struct LengthPrefixUInt8Array const * restrict src,
struct LengthPrefixUInt8Array * restrict dst
);

View file

@ -1,24 +1,63 @@
```
#include <array/length-prefix.h>
```
morph_array_as_valterm_to_lenpfx (Terminator:x86.UInt8) morph_array_as_valterm_to_lenpfx (Terminator:x86.UInt8)
<Seq~<ValueTerminated Terminator> x86.UInt8> <Seq~<ValueTerminated Terminator> x86.UInt8>
--> <Seq~<LengthPrefix x86.UInt64> x86.UInt8> --> <Seq~<LengthPrefix x86.UInt64> x86.UInt8>
``` ```
length_prefix_uint8_array_clear(dst); length_prefix_uint8_array_clear(dst);
while( *src != Terminator ) { while( *src != Terminator )
length_prefix_uint8_array_push(dst, *src); length_prefix_uint8_array_push(dst, *src++);
src++;
}
return 0; return 0;
``` ```
morph_array_as_lenpfx_to_valterm (Terminator:x86.UInt8) morph_array_as_lenpfx_to_valterm (Terminator:x86.UInt8)
<Seq~<LengthPrefix x86.UInt64> x86.UInt8> <Seq~<LengthPrefix x86.UInt64> x86.UInt8>
--> <Seq~<ValueTerminated Terminator> x86.UInt8> --> <Seq~<ValueTerminated Terminator> x86.UInt8>
``` ```
for( uint64_t i = 0; i < src->len; ++i ) { for( uint64_t i = 0; i < src->len; ++i )
*dst ++ = src->items[i]; *dst++ = src->items[i];
}
*dst = Terminator; *dst = Terminator;
return 0; return 0;
``` ```
morph_array_as_lenpfx_to_continuation_bit_8 ()
<Seq~<LengthPrefix x86.UInt64> x86.UInt8>
--> <Seq~MsbCont x86.UInt8>
```
for( uint64_t i = 0; i < src->len; ++i ) {
if( src->items[i] & (1<<7) ) {
fprintf(stderr, "error: value to high for MsbContinuation\n");
return -1;
}
dst[i] = src->items[i];
if( i+1 < src->len )
dst[i] |= (1<<7);
}
return 0;
```
morph_array_as_lenpfx_to_continuation_bit_16 ()
<Seq~<LengthPrefix x86.UInt64> x86.UInt16>
--> <Seq~MsbCont x86.UInt16>
```
for( uint64_t i = 0; i < src->len; ++i ) {
if( src->items[i] & (1<<15) ) {
fprintf(stderr, "error: value to high for MsbContinuation\n");
return -1;
}
dst[i] = src->items[i];
if( i+1 < src->len )
dst[i] |= (1<<15);
}
return 0;
```

View file

@ -1,3 +1,30 @@
```
#include <array/length-prefix.h>
```
morph_nat_as_u64_to_pos ()
~ x86.UInt64
-->
~ <PosInt 0 LittleEndian>
~ <Seq~<LengthPrefix x86.UInt64> <Digit 0>~x86.UInt64>
```
dst->len = 1;
dst->items[0] = *src;
return 0;
```
morph_nat_as_u64_to_pos ()
~ <PosInt 0 LittleEndian>
~ <Seq~<LengthPrefix x86.UInt64> <Digit 0>~x86.UInt64>
-->
~ x86.UInt64
```
*dst = src->items[0];
return 0;
```
morph_posint_radix_le (SrcRadix:, DstRadix:) morph_posint_radix_le (SrcRadix:, DstRadix:)
~ <PosInt SrcRadix LittleEndian> ~ <PosInt SrcRadix LittleEndian>
@ -15,9 +42,13 @@ morph_posint_radix_le (SrcRadix:, DstRadix:)
length_prefix_uint64_array_clear( dst ); length_prefix_uint64_array_clear( dst );
while( value > 0 ) { if( DstRadix == 0 ) {
length_prefix_uint64_array_push( dst, value % DstRadix ); length_prefix_uint64_array_push( dst, value );
value /= DstRadix; } else if( DstRadix > 0 ) {
while( value > 0 ) {
length_prefix_uint64_array_push( dst, value % DstRadix );
value /= DstRadix;
}
} }
return 0; return 0;
@ -38,17 +69,22 @@ morph_posint_radix_be (SrcRadix:, DstRadix:)
value += src->items[i]; value += src->items[i];
} }
uint64_t v = value; if( DstRadix == 0 ) {
dst->len = 0; dst->len = 1;
while( v ) { dst->items[0] = value;
dst->len++; } else {
v /= DstRadix; uint64_t v = value;
} dst->len = 0;
while( v ) {
dst->len++;
v /= DstRadix;
}
uint64_t i = dst->len; uint64_t i = dst->len;
while( value > 0 ) { while( value > 0 ) {
dst->items[--i] = ( dst, value % DstRadix ); dst->items[--i] = value % DstRadix;
value /= DstRadix; value /= DstRadix;
}
} }
return 0; return 0;

View file

@ -28,6 +28,21 @@ int length_prefix_uint64_array_reverse(
return 0; return 0;
} }
int length_prefix_array_map_8_to_8(
int (*f) ( uint8_t const * restrict, uint8_t * restrict ),
struct LengthPrefixUInt8Array 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;
}
int length_prefix_array_map_64_to_64( int length_prefix_array_map_64_to_64(
int (*f) ( uint64_t const * restrict, uint64_t * restrict ), int (*f) ( uint64_t const * restrict, uint64_t * restrict ),
struct LengthPrefixUInt64Array const * restrict src, struct LengthPrefixUInt64Array const * restrict src,

View file

@ -6,7 +6,8 @@ use {
laddertypes::{ laddertypes::{
dict::TypeDict, parser::ParseLadderType, subtype_unify, unparser::UnparseLadderType, BimapTypeDict, Morphism, MorphismType dict::TypeDict, parser::ParseLadderType, subtype_unify, unparser::UnparseLadderType, BimapTypeDict, Morphism, MorphismType
}, },
std::{any::Any, sync::{Arc, RwLock}} std::{any::Any, sync::{Arc, RwLock}},
tiny_ansi::TinyAnsi
}; };
/* /*
@ -60,10 +61,21 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_
} else { } else {
Some(format!("{} *", c_type)) Some(format!("{} *", c_type))
} }
} else { }
else if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"MsbCont".into()).unwrap())
{
let c_type = get_c_repr_type(dict, args[1].clone(), false)?;
if skip_pointer {
Some(c_type)
} else {
Some(format!("{} *", c_type))
}
}
else {
None None
} }
} }
_ => None _ => None
} }
} }
@ -84,13 +96,31 @@ struct LdmcPrimCMorphism {
fn encode_type_to_symbol(dict: &mut impl TypeDict, t: &laddertypes::TypeTerm)-> String { fn encode_type_to_symbol(dict: &mut impl TypeDict, t: &laddertypes::TypeTerm)-> String {
match t { match t {
laddertypes::TypeTerm::Char(c) => { laddertypes::TypeTerm::Char(c) => {
match *c { match c {
'\0' => { "NULL".into() }, '.' => format!("_dot_"),
'\t' => { "TAB".into() }, _ =>
c => { format!("{}", c) } format!("{}", (*c as u64))
} }
}, },
t => dict.unparse(t) laddertypes::TypeTerm::Num(n) => {
format!("{}", n)
}
laddertypes::TypeTerm::TypeID(ty_id) => {
if let Some(name) = dict.get_typename(ty_id) {
format!("{}", name.replace(".", "_dot_"))
} else {
format!("")
}
}
laddertypes::TypeTerm::Ladder(rs) |
laddertypes::TypeTerm::App(rs) => {
let mut s = String::new();
for r in rs {
s.push('_');
s.push_str(&encode_type_to_symbol(dict, r));
}
s
}
} }
} }
@ -174,8 +204,8 @@ impl LdmcMorphism {
pub fn generate_call(&self, dict: &mut impl TypeDict, σ: &std::collections::HashMap<laddertypes::TypeID, laddertypes::TypeTerm>, i: u64) { pub fn generate_call(&self, dict: &mut impl TypeDict, σ: &std::collections::HashMap<laddertypes::TypeID, laddertypes::TypeTerm>, i: u64) {
match self { match self {
LdmcMorphism::Primitive(prim_morph) => { 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 src_c_type = get_c_repr_type(dict, prim_morph.src_type.clone().apply_substitution(&|k| σ.get(k).cloned()).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 dst_c_type = get_c_repr_type(dict, prim_morph.dst_type.clone().apply_substitution(&|k| σ.get(k).cloned()).clone(), true).expect("cant get c-repr type for dst type");
let src_buf = if i%2 == 0 { "bufA" } else { "bufB" }; let src_buf = if i%2 == 0 { "bufA" } else { "bufB" };
let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" }; let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" };
@ -193,19 +223,14 @@ impl LdmcMorphism {
'}'); '}');
} }
LdmcMorphism::LengthPrefixMap { length_prefix_type, item_morph } => { 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 src_c_type = get_c_repr_type(dict, self.get_type().src_type.clone().apply_substitution(&|k| σ.get(k).cloned()).clone(), 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 dst_c_type = get_c_repr_type(dict, self.get_type().dst_type.clone().apply_substitution(&|k| σ.get(k).cloned()).clone(), true).expect("cant get c-repr type for dst type");
let map_fn = match (src_c_type.as_str(), dst_c_type.as_str()) { let map_fn = match (src_c_type.as_str(), dst_c_type.as_str()) {
("struct LengthPrefixUInt64Array", "struct LengthPrefixUInt64Array") => { ("struct LengthPrefixUInt64Array", "struct LengthPrefixUInt64Array") => "length_prefix_array_map_64_to_64",
"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",
("struct LengthPrefixUInt8Array", "struct LengthPrefixUInt64Array") => { ("struct LengthPrefixUInt8Array", "struct LengthPrefixUInt8Array") => "length_prefix_array_map_8_to_8",
"length_prefix_array_map_8_to_64"
},
("struct LengthPrefixUInt64Array", "struct LengthPrefixUInt8Array") => {
"length_prefix_array_map_64_to_8"
},
_ => { _ => {
"{{ ERROR: no map function implemented }}" "{{ ERROR: no map function implemented }}"
} }
@ -310,10 +335,19 @@ impl Morphism for LdmcMorphism {
*/ */
fn parser( fn parser(
type_dict: Arc<RwLock< BimapTypeDict >> type_dict: Arc<RwLock< BimapTypeDict >>
) -> impl Parser<char, Vec<LdmcPrimCMorphism>, Error = Simple<char>> { ) -> impl Parser<char, (String, Vec<LdmcPrimCMorphism>), Error = Simple<char>> {
just("```")
.then(take_until(just("```")))
.map(
move |(a, (b, c))| {
b.iter().collect()
}
)
// morph name // morph name
ident().padded() .then(
ident().padded()
// type args // type args
.then( .then(
@ -358,12 +392,15 @@ fn parser(
} }
}) })
.separated_by(text::newline()) .separated_by(text::newline())
)
} }
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 mut morphism_base = laddertypes::MorphismBase::<LdmcMorphism>::new(vec![ let mut morphism_base = laddertypes::MorphismBase::<LdmcMorphism>::new(vec![
type_dict.parse("Seq~<ValueTerminated '\\0'>").expect(""), //type_dict.parse("Seq~MsbCont").expect(""),
//type_dict.parse("Seq~<ValueTerminated '\\0'>").expect(""),
type_dict.parse("Seq~<LengthPrefix x86.UInt64>").expect("") type_dict.parse("Seq~<LengthPrefix x86.UInt64>").expect("")
]); ]);
@ -372,11 +409,12 @@ fn main() {
let dst_type_arg = args.next().expect("dst type expected"); let dst_type_arg = args.next().expect("dst type expected");
for mb_path in args { for mb_path in args {
let src = std::fs::read_to_string(mb_path).expect("read"); let src = std::fs::read_to_string(mb_path.clone()).expect("read");
let result = parser(type_dict.clone()).parse(src.clone()); let result = parser(type_dict.clone()).parse(src.clone());
match result { match result {
Ok(morphisms) => { Ok((includes, morphisms)) => {
eprintln!("parse ok."); eprintln!("[{}] parse ok.", mb_path.bright_yellow());
println!("{}", includes);
for m in morphisms { for m in morphisms {
morphism_base.add_morphism(LdmcMorphism::Primitive(m)); morphism_base.add_morphism(LdmcMorphism::Primitive(m));
} }
@ -399,9 +437,12 @@ fn main() {
} }
let src_type = type_dict.parse( src_type_arg.as_str() ).expect("");
let dst_type = type_dict.parse( dst_type_arg.as_str() ).expect("");
let path = morphism_base.find_morphism_path(MorphismType { let path = morphism_base.find_morphism_path(MorphismType {
src_type: type_dict.parse( src_type_arg.as_str() ).expect(""), src_type: src_type.clone(),
dst_type: type_dict.parse( dst_type_arg.as_str() ).expect(""), dst_type: dst_type.clone(),
}); });
match path { match path {
@ -411,8 +452,10 @@ fn main() {
/* todo: collect include files from morphism base */ /* todo: collect include files from morphism base */
println!(r#" println!(r#"
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h> #include <stdint.h>
#include <array/length-prefix.h> #include <stdbool.h>
"#); "#);
let mut existing_instantiations = Vec::new(); let mut existing_instantiations = Vec::new();
@ -450,9 +493,18 @@ int main() {{
uint8_t bufA[1024]; uint8_t bufA[1024];
uint8_t bufB[1024]; uint8_t bufB[1024];
scanf("%s", bufA); char in_str[] = "read :: {} \n";
"#); char out_str[]= "write:: {} \n";
for morph_inst in path { write(2, in_str, strlen(in_str));
write(2, out_str, strlen(out_str));
int l = read(0, bufA, 1024);
fprintf(stderr, "read %d bytes\n", l);
"#,
type_dict.unparse(&src_type).replace("\\", "\\\\"),
type_dict.unparse(&dst_type).replace("\\", "\\\\") );
for morph_inst in path.iter() {
println!(r#" println!(r#"
/* morph to {} /* morph to {}
@ -473,15 +525,44 @@ int main() {{
} }
let out_buf = if i%2==0 { "bufA" } else { "bufB" }; let out_buf = if i%2==0 { "bufA" } else { "bufB" };
println!(r#"
printf("%s\n", {});
let is_string = false;
if let Ok((halo, σ)) = laddertypes::subtype_unify(
&dst_type,
&type_dict.parse("<Seq~<ValueTerminated 0> x86.UInt8>").unwrap()
) {
println!(r#"
printf("%s\n", {});"#, out_buf);
} else if let Ok((halo, σ)) = laddertypes::subtype_unify(
&dst_type,
&type_dict.parse("<Seq~<LengthPrefix x86.UInt64> x86.UInt8>").unwrap()
) {
println!(r#"
/* write output
*/
{{
struct LengthPrefixUInt8Array * buf = (void*){};
write(1, {}, sizeof(uint64_t) + buf->len);
}}"#, out_buf, out_buf);
} else {
println!(r#"
write(1, {}, {});"#,
out_buf,
1024
);
}
println!(r#"
return 0; return 0;
}} }}
"#, out_buf); "#);
eprintln!("Success: generated C code");
} }
None => { None => {
eprintln!("Error: could not find morphism path"); eprintln!("Error: could not find morphism path");
std::process::exit(-1);
} }
} }
} }