improve output

This commit is contained in:
Michael Sippel 2025-02-20 05:35:40 +01:00
parent 44ff30e599
commit 186c12fc5d
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 57 additions and 11 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

@ -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
}; };
/* /*
@ -375,11 +376,11 @@ 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(morphisms) => {
eprintln!("parse ok."); eprintln!("[{}] parse ok.", mb_path.bright_yellow());
for m in morphisms { for m in morphisms {
morphism_base.add_morphism(LdmcMorphism::Primitive(m)); morphism_base.add_morphism(LdmcMorphism::Primitive(m));
} }
@ -402,9 +403,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 {
@ -414,7 +418,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 <stdbool.h>
#include <array/length-prefix.h> #include <array/length-prefix.h>
"#); "#);
@ -453,9 +460,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 {}
@ -476,15 +492,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);
} }
} }
} }