diff --git a/Cargo.toml b/Cargo.toml index bf945fc..8eace12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2024" chumsky = "0.9.0" ariadne = "0.2" laddertypes = { path = "../lib-laddertypes", features = ["pretty"] } +tiny-ansi = { version = "0.1.0" } diff --git a/src/main.rs b/src/main.rs index 60ea003..e1262e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,8 @@ use { laddertypes::{ 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"); 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()); match result { Ok(morphisms) => { - eprintln!("parse ok."); + eprintln!("[{}] parse ok.", mb_path.bright_yellow()); for m in morphisms { 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 { - src_type: type_dict.parse( src_type_arg.as_str() ).expect(""), - dst_type: type_dict.parse( dst_type_arg.as_str() ).expect(""), + src_type: src_type.clone(), + dst_type: dst_type.clone(), }); match path { @@ -414,7 +418,10 @@ fn main() { /* todo: collect include files from morphism base */ println!(r#" #include <stdio.h> +#include <unistd.h> +#include <string.h> #include <stdint.h> +#include <stdbool.h> #include <array/length-prefix.h> "#); @@ -453,9 +460,18 @@ int main() {{ uint8_t bufA[1024]; uint8_t bufB[1024]; - scanf("%s", bufA); - "#); - for morph_inst in path { + char in_str[] = "read :: {} \n"; + char out_str[]= "write:: {} \n"; + 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#" /* morph to {} @@ -476,15 +492,44 @@ int main() {{ } 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; }} - "#, out_buf); + "#); + + eprintln!("Success: generated C code"); } None => { eprintln!("Error: could not find morphism path"); + std::process::exit(-1); } } }