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

extern crate darling;
extern crate proc_macro;
use darling::FromMeta;
use self::proc_macro::TokenStream;

use quote::{format_ident, quote};
// use syn::parse::{Parse, ParseStream, Result};
// use syn::{parse_macro_input, DeriveInput, Expr, ExprArray};
 use syn::{FnArg, ItemFn, Pat};
// use syn::{Lit, Meta, MetaNameValue};
use syn::{AttributeArgs};


#[derive(Debug, FromMeta)]
struct MacroArgs {
    #[darling(default)]
    undo: bool,
}

#[derive(Debug, FromMeta)]
struct ReactionMacroArgs {
    #[darling(default)]
    always_run : bool,
}

#[proc_macro_attribute]
pub fn atom(args: TokenStream, input: TokenStream) -> TokenStream {
    let attr_args = syn::parse_macro_input!(args as AttributeArgs);

    let args = match MacroArgs::from_list(&attr_args){
        Ok(v) => v,
        Err(e) => panic!("{}",e),
    };

    let input_fn: ItemFn = syn::parse_macro_input!(input);

    let input_fn_string = input_fn.sig.ident.to_string();
    let atom_ident_string = input_fn_string.as_str();

    let atom_ident = format_ident!("{}", atom_ident_string);

    let the_type = match input_fn.sig.output {
        syn::ReturnType::Default => panic!("Your atom MUST return a non-Unit value"),
        syn::ReturnType::Type(_, the_type) => the_type.clone(),
    };
    let body = input_fn.block.clone();


    


    let inputs_iter = &mut input_fn.sig.inputs.iter();
    let  mut inputs_iter_3 = inputs_iter.clone();

    let  inputs_iter_2 = inputs_iter.clone();
    
    
    let mut arg_quote = quote!();
    if let Some(first_arg) = inputs_iter_3.next(){
        arg_quote  = quote!(#first_arg,);
        for input in inputs_iter_3 {
            arg_quote = quote!(#arg_quote, #input,);
        }
    }   
    
    let mut template_quote = quote!();
    let mut use_args_quote = quote!();

    let mut first = true;
    for input in inputs_iter_2 {
        let arg_name_ident = format_ident!("{}",get_arg_name(input));
        
        
        if first {
        template_quote = quote!(#arg_name_ident,);
        use_args_quote = quote!(#arg_name_ident,);
        } else {
            first = false;
            template_quote = quote!(#template_quote,#arg_name_ident,);
            use_args_quote = quote!(#use_args_quote, #arg_name_ident);
        }
    }

    let hash_quote = quote!( (CallSite::here(), #template_quote) );
    

    let (atom_fn_ident,marker) = if args.undo {
        (format_ident!("atom_with_undo"), format_ident!("AllowUndo"))
    } else {
        (format_ident!("atom"), format_ident!("NoUndo"))
    };


    let update_with_undo= if args.undo {
       quote!( set_inert_atom_state_with_id(UndoVec::<#the_type>(vec![value]), &__id); )
    } else {
       quote!()
    };

    let set_inert_with_undo= if args.undo {
        quote!( set_inert_atom_state_with_id::<#the_type>(value.clone(),&__id ); )
     } else {
        quote!(set_inert_atom_state_with_id::<#the_type>(value,&__id );)
     };


    
    quote!(

        pub fn #atom_ident(#arg_quote) -> ReactiveStateAccess<#the_type,#marker,IsAnAtomState>{
                let __id  = return_key_for_type_and_insert_if_required(#hash_quote);
                let func = move || {
                    topo::call(||{
                        illicit::Env::hide::<topo::Point>();
                        topo::call(||{

                            let context = ReactiveContext::new(__id );
                            illicit::child_env!( std::cell::RefCell<ReactiveContext> => std::cell::RefCell::new(context) ).enter(|| {

                                let value = {#body};
                                #set_inert_with_undo
                                #update_with_undo
                            })


                        })
                    })
                };

                #atom_fn_ident::<#the_type,_,#marker,IsAnAtomState>(__id ,func)
            
        } 

        // fn #atom_default_ident<F:FnOnce() -> #the_type>( #arg_quote default : F ) -> ReactiveStateAccess<#the_type,#marker,IsAnAtomState>{
        //     let atom_ident = format!(#atom_atom , module_path!(), #template_quote);

        //     #atom_fn_ident::<#the_type,_,#marker,IsAnAtomState>(&atom_ident,default)
        // } 

        // fn #reset_atom_ident(#arg_quote){
        //     illicit::Env::hide::<topo::Point>();
        //     topo::call(||{
        //         #atom_ident(#use_args_quote).update(|v| {
        //             *v = {#body}   
        //             }
        //         );
        //     })
        //  } 
  
        
    ).into()

}


fn get_arg_name(fnarg : &FnArg) -> String {
    match fnarg {
            FnArg::Receiver(_) => panic!("cannot be a method with self receiver"),
            FnArg::Typed(t) => {
                match &*t.pat {
                    Pat::Ident(syn::PatIdent { ident, .. }) => ident.to_string(), //syn::parse_quote!(&#ident),
                    _ => unimplemented!("Cannot get arg name"),
                }
            }
    }
}


#[proc_macro_attribute]
pub fn reaction(args: TokenStream, input: TokenStream) -> TokenStream {
    let attr_args = syn::parse_macro_input!(args as AttributeArgs);

    let args = match ReactionMacroArgs::from_list(&attr_args){
        Ok(v) => v,
        Err(e) => panic!("{}",e),
    };


    let input_fn: ItemFn = syn::parse_macro_input!(input);
    
    let input_fn_string = input_fn.sig.ident.to_string();
    let atom_ident_string = input_fn_string.as_str();

    let atom_ident = format_ident!("{}", atom_ident_string);

    let the_type = match input_fn.sig.output {
        syn::ReturnType::Default => panic!("Your atom MUST return a non-Unit value"),
        syn::ReturnType::Type(_, the_type) => the_type.clone(),
    };
    let body = input_fn.block.clone();

    let inputs_iter = &mut input_fn.sig.inputs.iter();
    let  mut inputs_iter_3 = inputs_iter.clone();

    let  inputs_iter_2 = inputs_iter.clone();
    
    
    let mut arg_quote = quote!();
    if let Some(first_arg) = inputs_iter_3.next(){
        arg_quote  = quote!(#first_arg);
        for input in inputs_iter_3 {
            arg_quote = quote!(#arg_quote, #input);
        }
    }   
    
    let mut template_quote = quote!();

    let mut first = true;
    for input in inputs_iter_2 {
        let arg_name_ident = format_ident!("{}",get_arg_name(input));
        if first {
        template_quote = quote!(#arg_name_ident,);
        } else {
            first = false;
            template_quote = quote!(#template_quote,#arg_name_ident,);
        }
    }
    let hash_quote = quote!( (CallSite::here(), #template_quote) );

    let always_run_quote =  if args.always_run { quote!(_context.always_run = true;) } else {quote!()};

    let quote = quote!(

        pub fn #atom_ident<'_a>(#arg_quote) -> ReactiveStateAccess<#the_type,NoUndo,IsAReactionState>{
          
                let __id = return_key_for_type_and_insert_if_required(#hash_quote);
            
                if !reactive_state_exists_for_id::<#the_type>(&__id ){
                                    
                    let func = move || {
                        topo::call(||{
                        illicit::Env::hide::<topo::Point>();
                        topo::call(||{


                        let mut _context = ReactiveContext::new(__id );
                        
                        #always_run_quote

                        illicit::child_env!( std::cell::RefCell<ReactiveContext> => std::cell::RefCell::new(_context) ).enter(|| {
                            // let mut existing_state = remove_reactive_state_with_id::<#the_type>(&atom_ident2.clone());
                            let value = {#body};
                            set_inert_atom_state_with_id::<#the_type>(value,&__id );
                            // we need to remove dependencies that do nto exist anymore
                            unlink_dead_links(&__id );
                        })
                    })
                })


                    };

                    reaction::<#the_type,NoUndo,IsAReactionState,_>(__id ,func)
                } else {
                    ReactiveStateAccess::<#the_type,NoUndo,IsAReactionState>::new(__id )                 
                }
            
        }
        
    );

    quote.into()
}