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
//! # Rust Basic Macro
//!
//! `RustBasic` is a planned development that aims to make Rust easy to learn, teach, and use.

// rustbasic macro - lib.rs
// Thanks to `wusyong & Team` ( https://github.com/wusyong/smol-potat ) for the reference.
// Thanks to `Fishrock123 & Team` ( https://github.com/async-rs/async-attributes ) for the reference.
// Thanks to `all the creators` of the libraries used in this source code.
// Thanks to `all RUST Team`.

#![allow(unused_doc_comments)]
#![allow(dead_code)]

#![allow(unused_imports)]

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{quote, quote_spanned};
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::Expr;
use futures::executor::block_on;

#[proc_macro_attribute]
pub fn main(attr: TokenStream, item: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(item as syn::ItemFn);
    let opts = syn::parse_macro_input!(attr as Opts);

    let ret = &input.sig.output;
    let inputs = &input.sig.inputs;
    let name = &input.sig.ident;
    let body = &input.block;
    #[allow(unused_variables)]
    let attrs = &input.attrs;
    let vis = &input.vis;

    let crate_root = opts.crate_root;

    let threads = match opts.threads {
        Some((num, span)) => {
            let num = num.to_string();
            Some(quote_spanned!(span=> #num))
        }
        #[cfg(feature = "auto")]
        None => Some(quote! {
            #crate_root::std::string::ToString::to_string(
                &#crate_root::std::cmp::max(#crate_root::num_cpus::get(), 1)
            )
        }),
        #[cfg(not(feature = "auto"))]
        None => None,
    };

    #[allow(unused_variables)]
    let set_threads = threads.map(|threads| {
        quote! {
            #crate_root::std::env::set_var(
                "RUSTBASIC-THREADS",
                #threads,
            );
        }
    });

    let result : proc_macro2::TokenStream;

    match input.sig.asyncness {
        None | Some(_) if input.sig.ident != "main" => {    // => If it is `not async` or `not main()`
            result = quote! {
                #vis fn #name(#inputs) #ret {
                    #body
                }
            }
        }
        _ => {                                              //  => If it is `async main()`
            result = quote! {
                #vis fn main() #ret {
                    futures::executor::block_on( async { async_main().await } );
                }
                async fn async_main() #ret {
                    #body
                }
            }
        }
    }

    result.into()
}

struct Opts {
    crate_root: syn::Path,
    threads: Option<(u32, Span)>,
}

impl Parse for Opts {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let mut crate_root = None;
        let mut threads = None;

        loop {
            if input.is_empty() {
                break;
            }

            let name_value: syn::MetaNameValue = input.parse()?;
            let ident = match name_value.path.get_ident() {
                Some(ident) => ident,
                None => {
                    return Err(syn::Error::new_spanned(
                        name_value.path,
                        "Must be a single ident",
                    ))
                }
            };
            match &*ident.to_string().to_lowercase() {
                "threads" => match &name_value.lit {
                    syn::Lit::Int(expr) => {
                        if threads.is_some() {
                            return Err(syn::Error::new_spanned(
                                name_value,
                                "multiple threads argments",
                            ));
                        }

                        let num = expr.base10_parse::<std::num::NonZeroU32>()?;
                        threads = Some((num.get(), expr.span()));
                    }
                    _ => {
                        return Err(syn::Error::new_spanned(
                            name_value,
                            "threads argument must be an integer",
                        ))
                    }
                },
                "crate" => match &name_value.lit {
                    syn::Lit::Str(path) => {
                        if crate_root.is_some() {
                            return Err(syn::Error::new_spanned(
                                name_value,
                                "multiple crate arguments",
                            ));
                        }

                        crate_root = Some(path.parse()?);
                    }
                    _ => {
                        return Err(syn::Error::new_spanned(
                            name_value,
                            "crate argument must be a string",
                        ))
                    }
                },
                name => {
                    return Err(syn::Error::new_spanned(
                        name,
                        "unknown attribute {}, expected `threads` or `crate`",
                    ));
                }
            }

            input.parse::<Option<syn::Token![,]>>()?;
        }

        Ok(Self {
            crate_root: crate_root.unwrap_or_else(|| syn::parse2(quote!(::rustbasic)).unwrap()),
            threads,
        })
    }
}

async fn rustbasic_macro_addlib_temp() {
}

fn rustbasic_macro_addlib() {
    futures::executor::block_on( rustbasic_macro_addlib_temp() );
}