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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*!
# Procedural macro's crate for **rust-spice**

See:

+ [**rust-spice**][rust-spice link]
+ [proc macro doc][proc-macro link]

[rust-spice link]: https://docs.rs/rust-spice
[proc-macro link]: https://doc.rust-lang.org/reference/procedural-macros.html
*/

extern crate proc_macro;
#[macro_use]
extern crate quote;
extern crate syn;

use proc_macro::TokenStream;
use proc_macro2::{Ident, Span, TokenStream as TS2};
use quote::ToTokens;
use std::{boxed::Box, str::FromStr};
use syn::{
    parse_macro_input, parse_quote,
    punctuated::Punctuated,
    token::{Colon, Eq, Let, Semi},
    Expr, FnArg, GenericArgument, ItemFn, Lit, Local, Pat, PatIdent, PatType, Path, PathArguments,
    ReturnType, Signature, Token, Type, TypeArray, TypePath,
};

/**
Get the [`String`] representation of a [`Pat::Ident`].
*/
macro_rules! tts {
    ($e: expr) => {
        $e.to_token_stream().to_string()
    };
}

/**
Build a [`Pat::Macro`] from its name as [`String`] and the tokens inside as a [`String`], just like:

```.ignore
path!(tokens);
```
*/
fn pat_macro<S>(path: S, tokens: S) -> Pat
where
    S: Into<String>,
{
    syn::parse_str(&format!("{}!({})", path.into(), tokens.into())).unwrap()
}

/**
Build a [`Pat::Ident`] from [`String`].
*/
fn pat_ident<S>(ident: S) -> Pat
where
    S: Into<String>,
{
    syn::parse_str(&ident.into()).unwrap()
}

/**
Build a [`Pat::Verbatim`] from [`String`].
*/
fn new_pat<S>(expr: S) -> Pat
where
    S: Into<String>,
{
    Pat::Verbatim(TS2::from_str(&expr.into()).unwrap())
}

/**
Build a [`Pat::Type`] from [`String`].
*/
fn new_pat_type<S>(pat: S, ty: S) -> PatType
where
    S: Into<String>,
{
    PatType {
        attrs: vec![],
        pat: Box::new(new_pat(pat)),
        colon_token: Colon::default(),
        ty: Box::new(new_type(ty)),
    }
}

/**
Build an [`Expr] from a [`String`].
*/
#[allow(unused)]
fn expr_ident<S>(ident: S) -> Expr
where
    S: Into<String>,
{
    syn::parse_str(&ident.into()).unwrap()
}

/**
Build an [`Expr::Verbatim`] from [`String`].
*/
#[allow(dead_code)]
fn new_expr<S>(expr: S) -> Expr
where
    S: Into<String>,
{
    Expr::Verbatim(TS2::from_str(&expr.into()).unwrap())
}

/**
Build a [`Type`] from [`String`].
*/
fn new_type<S>(s: S) -> Type
where
    S: Into<String>,
{
    Type::Verbatim(TS2::from_str(&s.into()).unwrap())
}

/**
Build a [`Local`] to declare a variable from its name and mutability as [`String`] and an
optional initial value as [`String`].
*/
fn declare<S>(ident: S, init: Option<S>) -> Local
where
    S: Into<String>,
{
    Local {
        attrs: vec![],
        let_token: Let::default(),
        pat: new_pat(ident),
        init: init.map(|i| (Eq::default(), Box::new(new_expr(i)))),
        semi_token: Semi::default(),
    }
}

/**
Get the size of a [`TypeArray`].
*/
fn array_get_size(arr: &TypeArray) -> usize {
    if let Expr::Lit(el) = &arr.len {
        if let Lit::Int(li) = &el.lit {
            li.base10_parse::<usize>().unwrap()
        } else {
            unreachable!("array size must be syn::LitInt")
        }
    } else {
        unreachable!("array size must be syn::ExprLit")
    }
}

/**
Get the [`String`] representation of the last [`PathSegment`] of a [`TypePath`].
*/
fn path_get_last_s_ident(path: &TypePath) -> (String, PathArguments) {
    let seg = path.path.segments.last().unwrap();
    (seg.ident.to_string(), seg.arguments.clone())
}

#[allow(unused)]
fn semi(b: bool) -> TS2 {
    if b {
        Semi::default().to_token_stream()
    } else {
        TS2::new()
    }
}

/**
Get list of [`GenericArgument`]s in [`PathArguments`].
*/
#[allow(unused)]
fn generic_arguments(pargs: &PathArguments) -> Vec<GenericArgument> {
    if let PathArguments::AngleBracketed(abga) = &pargs {
        abga.args.iter().cloned().collect::<Vec<_>>()
    } else {
        unreachable!("ParthArguments is not an AngleBracketed.")
    }
}

/**
I write Rust idiomatic interface for CSPICE.
*/
#[proc_macro]
pub fn cspice_proc(input: TokenStream) -> TokenStream {
    let f = parse_macro_input!(input as ItemFn);

    let attrs = f.attrs;
    let vis = f.vis;
    let sig = f.sig;
    let _block = f.block;

    let fname = sig.ident.clone();
    let generics = sig.generics;

    let return_output = attrs.iter().any(|attr| tts!(attr.path) == "return_output");

    let semi_call = semi(!return_output);

    let cspice_func = Ident::new(&format!("{}_c", fname), Span::call_site());

    // Update wrapper input.
    let inputs = sig
        .inputs
        .iter()
        .map(|arg| {
            FnArg::Typed(match arg {
                FnArg::Typed(pt) => {
                    let pat = *pt.clone().pat;
                    let ty = *pt.clone().ty;

                    match ty.clone() {
                        Type::Path(tp) => match path_get_last_s_ident(&tp).0.as_str() {
                            "DLADSC" => new_pat_type(format!("mut {}", tts!(pat)), tts!(ty)),
                            _ => pt.clone(),
                        },
                        Type::Array(_) => new_pat_type(format!("mut {}", tts!(pat)), tts!(ty)),
                        _ => pt.clone(),
                    }
                }
                FnArg::Receiver(_) => panic!("->5 bis"),
            })
        })
        .collect::<Punctuated<_, Token![,]>>();

    // Build CSPICE inputs from function inputs and reference to function outputs.
    let mut cspice_inputs = Punctuated::<Pat, Token![,]>::new();
    // Function inpus into CSPICE inputs.
    cspice_inputs.extend(sig.inputs.iter().map(|arg| -> Pat {
        match arg {
            FnArg::Typed(pt) => {
                let pat = *pt.clone().pat;
                let ty = *pt.clone().ty;

                let ident = tts!(&pat);

                match ty {
                    Type::Path(tp) => match path_get_last_s_ident(&tp).0.as_str() {
                        "String" => pat_macro("crate::cstr", &ident),
                        "f64" | "i32" => new_pat(ident),
                        "usize" => new_pat(format!("{} as i32", ident)),
                        "DLADSC" => new_pat(format!("&mut {}", ident)),
                        _ => panic!("->1"),
                    },
                    Type::Reference(tr) => match *tr.elem {
                        Type::Path(tp) => match path_get_last_s_ident(&tp).0.as_str() {
                            "str" => pat_macro("crate::cstr", &format!("{}.to_string()", ident)),
                            _ => panic!("->2"),
                        },
                        Type::Slice(_) => new_pat(format!("{}.as_mut_ptr()", ident)),
                        _ => panic!("->3"),
                    },
                    Type::Array(_) => new_pat(format!("{}.as_mut_ptr()", ident)),
                    _ => panic!("->4"),
                }
            }
            FnArg::Receiver(_) => panic!("->5"),
        }
    }));

    // Needed allocations declarations for the function ouputs that will be converted to pointers for CSPICE function.
    let mut vars_out_decl = Vec::<Local>::new();
    let mut vars_out = Vec::<Pat>::new();
    let mut out_is_bool = false;
    // Get function ouputs
    let output = match sig.output {
        ReturnType::Type(_, ty) => {
            // Reference to function ouputs into CSPICE inputs.
            if !return_output {
                match *ty.clone() {
                    Type::Tuple(tt) => tt.elems.iter().for_each(|e| match e {
                        Type::Path(tp) => {
                            let tpp = path_get_last_s_ident(tp);
                            match tpp.0.as_str() {
                                "f64" => {
                                    let ident = format!("varout_{}", vars_out_decl.len());
                                    vars_out_decl.push(declare(
                                        format!("mut {}", ident),
                                        Some("0.0f64".to_string()),
                                    ));
                                    cspice_inputs.push(pat_ident(format!("&mut {}", ident)));
                                    vars_out.push(pat_ident(ident));
                                }
                                "i32" => {
                                    let ident = format!("varout_{}", vars_out_decl.len());
                                    vars_out_decl.push(declare(
                                        format!("mut {}", ident),
                                        Some("0i32".to_string()),
                                    ));
                                    cspice_inputs.push(pat_ident(format!("&mut {}", ident)));
                                    vars_out.push(pat_ident(ident));
                                }
                                "String" => {
                                    let ident = format!("varout_{}", vars_out_decl.len());
                                    vars_out_decl.push(declare(
                                        &ident,
                                        Some(&"crate::mallocstr!(crate::MAX_LEN_OUT)".to_string()),
                                    ));
                                    cspice_inputs.push(pat_ident(ident.clone()));
                                    vars_out.push(new_pat(format!("crate::fcstr!({})", ident)));
                                }
                                "bool" => {
                                    let ident = format!("varout_{}", vars_out_decl.len());
                                    vars_out_decl.push(declare(
                                        format!("mut {}", ident),
                                        Some("0i32".to_string()),
                                    ));
                                    cspice_inputs.push(pat_ident(format!("&mut {}", ident)));
                                    vars_out.push(new_pat(format!("{} != 0", ident)));
                                }
                                "DLADSC" => {
                                    let ident = format!("varout_{}", vars_out_decl.len());
                                    vars_out_decl.push(declare(
                                        format!("mut {}", ident),
                                        Some("std::mem::MaybeUninit::uninit()".to_string()),
                                    ));
                                    cspice_inputs.push(new_pat(format!("{}.as_mut_ptr()", ident)));
                                    vars_out.push(new_pat(format!("{}.assume_init()", ident)));
                                }
                                _ => panic!("->6: {}", tpp.0.as_str()),
                            }
                        }
                        Type::Array(ta) => {
                            let ident = format!("varout_{}", vars_out_decl.len());
                            let pat_ident_fc = new_pat(format!("{}.as_mut_ptr()", ident));
                            let size = array_get_size(ta);
                            let init = format!("{:?}", vec![0.0f64; size]);
                            vars_out_decl.push(declare(format!("mut {}", ident), Some(init)));
                            cspice_inputs.push(pat_ident_fc);
                            vars_out.push(pat_ident(ident));
                        }
                        _ => panic!("->7"),
                    }),
                    Type::Path(tp) => {
                        let a = path_get_last_s_ident(&tp);
                        let b = a.0.as_str();
                        // println!("{}: {}", fname, b);
                        match b {
                            "f64" => {
                                let ident = format!("varout_{}", vars_out_decl.len());
                                vars_out_decl.push(declare(
                                    format!("mut {}", ident),
                                    Some("0.0f64".to_string()),
                                ));
                                cspice_inputs.push(pat_ident(format!("&mut {}", ident)));
                                vars_out.push(pat_ident(ident));
                            }
                            "i32" => {
                                let ident = format!("varout_{}", vars_out_decl.len());
                                vars_out_decl.push(declare(
                                    format!("mut {}", ident),
                                    Some("0i32".to_string()),
                                ));
                                cspice_inputs.push(pat_ident(format!("&mut {}", ident)));
                                vars_out.push(pat_ident(ident));
                            }
                            "String" => {
                                let ident = format!("varout_{}", vars_out_decl.len());
                                vars_out_decl.push(declare(
                                    &ident,
                                    Some(&"mallocstr!(crate::MAX_LEN_OUT)".to_string()),
                                ));
                                cspice_inputs.push(pat_ident(ident.clone()));
                                vars_out.push(new_pat(format!("crate::fcstr!({})", ident)));
                            }
                            "bool" => {
                                let ident = format!("varout_{}", vars_out_decl.len());
                                vars_out_decl.push(declare(
                                    format!("mut {}", ident),
                                    Some("0i32".to_string()),
                                ));
                                cspice_inputs.push(pat_ident(format!("&mut {}", ident)));
                                vars_out.push(new_pat(format!("{} != 0", ident)));
                            }
                            "DSKDSC" => {
                                let ident = format!("varout_{}", vars_out_decl.len());
                                vars_out_decl.push(declare(
                                    format!("mut {}", ident),
                                    Some("std::mem::MaybeUninit::uninit()".to_string()),
                                ));
                                cspice_inputs.push(new_pat(format!("{}.as_mut_ptr()", ident)));
                                vars_out.push(new_pat(format!("{}.assume_init()", ident)));
                            }
                            "Cell" => {
                                let ident = format!("varout_{}", vars_out_decl.len());
                                vars_out_decl.push(declare(
                                    format!("mut {}", ident),
                                    Some("Cell::new_int()".to_string()),
                                ));
                                cspice_inputs.push(new_pat(format!("&mut {}.0", ident)));
                                vars_out.push(new_pat(ident));
                            }
                            _ => panic!("->8"),
                        }
                    }
                    Type::Array(ta) => match *ta.clone().elem {
                        Type::Path(tp) => match path_get_last_s_ident(&tp).0.as_str() {
                            "f64" => {
                                let ident = format!("varout_{}", vars_out_decl.len());
                                let pat_ident_fc = new_pat(format!("{}.as_mut_ptr()", ident));
                                let size = array_get_size(&ta);
                                let init = format!("{:?}", vec![0.0f64; size]);
                                vars_out_decl.push(declare(format!("mut {}", ident), Some(init)));
                                cspice_inputs.push(pat_ident_fc);
                                vars_out.push(pat_ident(ident));
                            }
                            _ => panic!("->12"),
                        },
                        Type::Array(ta_2) => match *ta_2.clone().elem {
                            Type::Path(tp) => match path_get_last_s_ident(&tp).0.as_str() {
                                "f64" => {
                                    let ident = format!("varout_{}", vars_out_decl.len());
                                    let pat_ident_fc = new_pat(format!("{}.as_mut_ptr()", ident));
                                    let size = array_get_size(&ta);
                                    let size_2 = array_get_size(&ta_2);
                                    let init = format!("{:?}", vec![vec![0.0f64; size_2]; size]);
                                    vars_out_decl
                                        .push(declare(format!("mut {}", ident), Some(init)));
                                    cspice_inputs.push(pat_ident_fc);
                                    vars_out.push(pat_ident(ident));
                                }
                                _ => panic!("->13"),
                            },

                            _ => panic!("->11"),
                        },
                        _ => panic!("->10"),
                    },
                    _ => panic!("->9"),
                }
            } else {
                let ty_token = ty.to_token_stream().to_string();
                let ty_str = ty_token.as_str();
                if ty_str == "bool" {
                    out_is_bool = true;
                }
            }
            *ty
        }
        ReturnType::Default => parse_quote! {()},
    };

    let function_output = match return_output {
        true => match out_is_bool {
            true => new_pat("!= 0".to_string()).to_token_stream(),
            false => TS2::new(),
        },
        false => match vars_out.is_empty() {
            true => quote! {},
            false => quote! { ( #(#vars_out),* ) },
        },
    };

    let tokens = quote! {
        #(#attrs)*
        #vis fn #fname#generics(#inputs) -> #output {
            #(#vars_out_decl)*
            #[allow(unused_unsafe)]
            unsafe {
                crate::c::#cspice_func(#cspice_inputs)#semi_call
                #function_output
            }
        }
    };
    if [].contains(&fname.to_string().as_str()) {
        println!("{}", tokens);
    }
    tokens.into()
}

#[proc_macro_attribute]
pub fn return_output(_attr: TokenStream, item: TokenStream) -> TokenStream {
    item
}

#[proc_macro_attribute]
pub fn impl_for(struct_path: TokenStream, function: TokenStream) -> TokenStream {
    let function = parse_macro_input!(function as ItemFn);

    let Signature {
        ident: fname,
        generics,
        inputs,
        output,
        ..
    } = function.sig.clone();

    let attrs = function.attrs.clone();

    let new_fname = Ident::new(&fname.to_string(), Span::call_site());

    // Retreive argument identifiers without types, mutability etc.
    let arg_idents = inputs
        .iter()
        .map(|i| match i {
            FnArg::Typed(PatType { pat, .. }) => match &**pat {
                Pat::Ident(PatIdent { ident, .. }) => ident.clone(),
                _ => panic!("Only bare identifiers are allowed as parameter patterns"),
            },
            FnArg::Receiver(_) => panic!("Expected typed arg, found receiver"),
        })
        .collect::<Punctuated<Ident, Token![,]>>();

    let struct_path = syn::parse::<Path>(struct_path).expect("Invalid struct path");

    let impl_block = quote! {
        impl #struct_path {
            #(#attrs)*
            pub fn #new_fname#generics(&self, #inputs)#output {
                #fname(#arg_idents)
            }
        }
    };

    let mut out = function.to_token_stream();
    out.extend(impl_block.to_token_stream());
    out.into()
}