1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use quote::format_ident;

use crate::{
    savvy_fn::{SavvyFnReturnType, UserDefinedStructReturnType},
    ParsedResult, SavvyFn, SavvyFnType, SavvyImpl,
};

fn get_r_doc_comment(docs: &[String]) -> String {
    docs.iter()
        .map(|doc| format!("#'{doc}"))
        .collect::<Vec<String>>()
        .join("\n")
}

impl SavvyFn {
    pub fn fn_name_r(&self) -> syn::Ident {
        match self.get_self_ty_ident() {
            Some(ty) => {
                // Special convention. If the method name is "new", use type
                // itself.
                if self.fn_name.to_string().as_str() == "new" {
                    ty
                } else {
                    format_ident!("{}_{}", ty, self.fn_name)
                }
            }
            None => self.fn_name.clone(),
        }
    }

    fn get_r_args(&self) -> Vec<String> {
        let mut out: Vec<_> = self.args.iter().map(|arg| arg.pat_string()).collect();

        // if it's a method, add `self__` arg
        match &self.fn_type {
            SavvyFnType::Method(_) | SavvyFnType::ConsumingMethod(_) => {
                out.insert(0, "self".to_string())
            }
            _ => {}
        }

        out
    }

    fn to_r_function(&self) -> String {
        let fn_name = self.fn_name_r();
        let fn_name_c = self.fn_name_outer();

        let doc_comments = get_r_doc_comment(self.docs.as_slice());

        let args = self
            .get_c_args()
            .iter()
            .map(|(pat, _)| pat.clone())
            .collect::<Vec<String>>();

        let mut args_call = args.clone();
        args_call.insert(0, format!("{fn_name_c}__impl"));

        let args = args.join(", ");
        let args_call = args_call.join(", ");

        let mut body_lines = self.get_extractions();

        // If the result is NULL, wrap it with invisible
        let fn_call = match &self.return_type {
            SavvyFnReturnType::Unit(_) => {
                format!("  invisible(.Call({args_call}))")
            }
            SavvyFnReturnType::Sexp(_) => format!("  .Call({args_call})"),
            SavvyFnReturnType::UserDefinedStruct(UserDefinedStructReturnType {
                ty_str, ..
            }) => {
                format!("  .savvy_wrap_{ty_str}(.Call({args_call}))")
            }
        };
        body_lines.push(fn_call);

        let body = body_lines.join("\n");

        format!(
            "{doc_comments}
{fn_name} <- function({args}) {{
{body}
}}
"
        )
    }

    fn get_extractions(&self) -> Vec<String> {
        self.args
            .iter()
            .flat_map(|arg| {
                if arg.is_user_defined_type() {
                    let r_var = arg.pat_string();
                    let r_class = arg.ty_string();
                    Some(format!(
                        r#"  {r_var} <- .savvy_extract_ptr({r_var}, "{r_class}")"#,
                    ))
                } else {
                    None
                }
            })
            .collect::<Vec<String>>()
    }
}

impl SavvyImpl {
    fn generate_r_impl_for_impl(&self) -> String {
        let mut associated_fns: Vec<&SavvyFn> = Vec::new();
        let mut method_fns: Vec<&SavvyFn> = Vec::new();
        let class_r = self.ty.clone();

        for savvy_fn in &self.fns {
            match savvy_fn.fn_type {
                SavvyFnType::AssociatedFunction(_) => associated_fns.push(savvy_fn),
                SavvyFnType::Method(_) | SavvyFnType::ConsumingMethod(_) => {
                    method_fns.push(savvy_fn)
                }
                SavvyFnType::BareFunction => panic!("Something is wrong"),
            }
        }

        let closures = method_fns
            .iter()
            .map(|x| {
                let fn_name = x.fn_name_r();
                let fn_name_c = x.fn_name_outer();

                let mut args = x.get_r_args();
                // Remove self from arguments for R
                let args_r = args
                    .clone()
                    .into_iter()
                    .filter(|e| *e != "self")
                    .collect::<Vec<String>>()
                    .join(", ");

                args.insert(0, format!("{fn_name_c}__impl"));
                let args_call = args.join(", ");

                let mut body_lines = x.get_extractions();

                let fn_call = match &x.return_type {
                    SavvyFnReturnType::Sexp(_) => format!(".Call({args_call})"),
                    // If the result is NULL, wrap it with invisible
                    SavvyFnReturnType::Unit(_) => format!("invisible(.Call({args_call}))"),
                    // If the result is an external pointer, wrap it with the
                    // corresponding wraping function
                    SavvyFnReturnType::UserDefinedStruct(UserDefinedStructReturnType {
                        ty_str,
                        ..
                    }) => {
                        format!("  .savvy_wrap_{ty_str}(.Call({args_call}))")
                    }
                };
                body_lines.push(fn_call);

                let body = body_lines.join("\n");

                format!(
                    "{fn_name} <- function(self) {{
  function({args_r}) {{
  {body}
  }}
}}
"
                )
            })
            .collect::<Vec<String>>()
            .join("\n");

        let methods = method_fns
            .iter()
            .map(|o| format!("  e${} <- {}(ptr)", o.fn_name, o.fn_name_r()))
            .collect::<Vec<String>>()
            .join("\n");

        let wrap_fn_name = format!(".savvy_wrap_{}", class_r);

        let wrap_fn = format!(
            r#"{wrap_fn_name} <- function(ptr) {{
  e <- new.env(parent = emptyenv())
  e$.ptr <- ptr
{methods}

  class(e) <- "{class_r}"
  e
}}
"#
        );

        let associated_fns = associated_fns
            .iter()
            .map(|x| {
                let fn_name = x.fn_name.clone();
                let fn_name_c = x.fn_name_outer();

                let mut args = x.get_r_args();

                let args_r = args.join(", ");

                args.insert(0, format!("{fn_name_c}__impl"));
                let args_call = args.join(", ");

                let mut body_lines = x.get_extractions();

                let fn_call = match &x.return_type {
                    SavvyFnReturnType::Sexp(_) => format!(".Call({args_call})"),
                    // If the result is NULL, wrap it with invisible
                    SavvyFnReturnType::Unit(_) => format!("invisible(.Call({args_call}))"),
                    // If the result is an external pointer, wrap it with the
                    // corresponding wraping function
                    SavvyFnReturnType::UserDefinedStruct(UserDefinedStructReturnType {
                        ty_str,
                        ..
                    }) => {
                        format!("  .savvy_wrap_{ty_str}(.Call({args_call}))")
                    }
                };

                body_lines.push(fn_call);

                let body = body_lines.join("\n");

                format!(
                    r#"{class_r}${fn_name} <- function({args_r}) {{
{body}
}}
"#
                )
            })
            .collect::<Vec<String>>()
            .join("\n");

        let doc_comments = get_r_doc_comment(self.docs.as_slice());

        format!(
            "{doc_comments}
{class_r} <- new.env(parent = emptyenv())
{associated_fns}

{wrap_fn}

{closures}
"
        )
    }
}

pub fn generate_r_impl_file(parsed_results: &[ParsedResult], pkg_name: &str) -> String {
    let r_fns = parsed_results
        .iter()
        .flat_map(|x| &x.bare_fns)
        .map(|x| x.to_r_function())
        .collect::<Vec<String>>()
        .join("\n");

    let r_impls = parsed_results
        .iter()
        .flat_map(|x| &x.impls)
        .map(|x| x.generate_r_impl_for_impl())
        .collect::<Vec<String>>()
        .join("\n");

    format!(
        r#"#' @useDynLib {pkg_name}, .registration = TRUE
#' @keywords internal
NULL

# Check class and extract the external pointer embedded in the environment
.savvy_extract_ptr <- function(e, class) {{
  if(inherits(e, class)) {{
    e$.ptr
  }} else {{
    msg <- paste0("Expected ", class, ", got ", class(e)[1])
    stop(msg, call. = FALSE)
  }}
}}

{r_fns}
{r_impls}
"#
    )
}