From 88978d9008ce29b0cd0b8148d633270ea5a1767e Mon Sep 17 00:00:00 2001
From: Michael Sippel <micha@fragmental.art>
Date: Mon, 3 Feb 2025 17:55:18 +0100
Subject: [PATCH] function to determine C-type to represent a ladder type, for
 morphism generate expected C-signatures & calls

---
 src/main.rs | 97 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 91 insertions(+), 6 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index e76a145..34ddeb8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,8 +11,60 @@ use {
     std::sync::{Arc, RwLock}
 };
 
+/* for a given ladder type `t`, get the corresponding C type
+ */
+pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_pointer: bool) -> Option<String> {
+    let lnf = t.normalize().decurry().get_lnf_vec();
+
+    match lnf.last() {
+        Some(t) => {
+            if t == &dict.parse("Byte").expect("parse")
+            || t == &dict.parse("x86.UInt8").expect("parse")
+            || t == &dict.parse("<StaticLength 8 Bit>").expect("parse")
+            {
+                Some("uint8_t".into())
+            } else if t == &dict.parse("x86.UInt16").expect("parse") {
+                Some("uint16_t".into())
+            } else if t == &dict.parse("x86.UInt32").expect("parse") {
+                Some("uint32_t".into())
+            } else if t == &dict.parse("x86.UInt64").expect("parse") {
+                Some("uint64_t".into())
+            } else {
+                match t {
+                    laddertypes::TypeTerm::App(args) => {
+                        if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"LengthPrefix".into()).unwrap())
+                        {
+                            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")),
+                                _ => None
+                            }
+                        }
+                        else if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"ValueDelim".into()).unwrap())
+                        {
+                            let c_type = get_c_repr_type(dict, args[2].clone(), false)?;
+                            if skip_pointer {
+                                Some(c_type)
+                            } else {
+                                Some(format!("{} *", c_type))
+                            }
+                        } else {
+                            None
+                        }
+                    }
+                    _ => None
+                }
+            }
+        }
+        None => None
+    }
+}
+
 #[derive(Debug)]
-struct Morphism {
+struct LdmcMorphism {
     symbol: String,
     type_args: Vec<(String, String)>,
     src_type: laddertypes::TypeTerm,
@@ -20,6 +72,34 @@ struct Morphism {
     locations: Vec<String>
 }
 
+impl LdmcMorphism {
+    pub fn expected_c_type_signature(&self, dict: &mut impl TypeDict) -> String {
+        format!("int {} ({} const * restrict src, {} * restrict dst);",
+            self.symbol,
+            get_c_repr_type(dict, self.src_type.clone(), true).expect("cant get c-repr type for src 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) {
+        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";
+        println!(
+"{}
+    {} const * restrict src = {};
+    {} * restrict dst = {};
+    {} ( src, dst );
+{}",
+    '{',
+    src_c_type, src_buf,
+    dst_c_type, dst_buf,
+    self.symbol,
+    '}');
+    }
+}
+
 /* morphism-base text format:
  *     NAME '(' [TYPE-ARG-NAME ':' KIND]  ')'
  *           SRC-TYPE
@@ -28,7 +108,7 @@ struct Morphism {
  */
 fn parser(
     type_dict: Arc<RwLock< BimapTypeDict >>
-) -> impl Parser<char, Vec<Morphism>, Error = Simple<char>> {
+) -> impl Parser<char, Vec<LdmcMorphism>, Error = Simple<char>> {
 
     ident().padded()
     .then(
@@ -57,7 +137,7 @@ fn parser(
             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");
 
-            Morphism {
+            LdmcMorphism {
                 symbol,
                 type_args,
                 src_type,
@@ -70,6 +150,7 @@ fn parser(
 
 fn main() {
     let type_dict = Arc::new(RwLock::new(BimapTypeDict::new()));
+
     let src = std::fs::read_to_string(
         std::env::args().nth(1).expect("expected file name")
     ).expect("read");
@@ -81,10 +162,14 @@ fn main() {
             let mut dict = type_dict.write().unwrap();
 
             for m in morphisms {
-                println!("{}\n    {}\n---> \n    {}\n", m.symbol,
-                    m.src_type.sugar(&mut *dict).pretty(&mut *dict, 1),
-                    m.dst_type.sugar(&mut *dict).pretty(&mut *dict, 1),
+                println!("{}\n    {}\n---> \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);
             }
         }
         Err(errs) => {