swamp_script_derive/
lib.rs

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
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/script
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(SwampExport, attributes(swamp))]
pub fn derive_swamp_export(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;

    // Extract fields from struct
    let fields = match input.data {
        syn::Data::Struct(ref data) => &data.fields,
        _ => panic!("SwampExport can only be derived for structs"),
    };

    // Generate field extractions for from_swamp_value
    let from_field_extractions = fields.iter().enumerate().map(|(index, f)| {
        let field_name = &f.ident.as_ref().unwrap();
        let field_type = &f.ty;
        quote! {
            let #field_name = <#field_type>::from_swamp_value(&values[#index])?;
        }
    });

    // Collect field names and types for struct construction
    let field_names: Vec<_> = fields.iter().map(|f| f.ident.as_ref().unwrap()).collect();
    let field_types: Vec<_> = fields.iter().map(|f| &f.ty).collect();

    let expanded = quote! {
        impl SwampExport for #name {

            fn get_resolved_type(registry: &TypeRegistry) -> ResolvedType {
                let fields = vec![
                    #((stringify!(#field_names), <#field_types>::get_resolved_type(registry))),*
                ];
                registry.register_derived_struct(stringify!(#name), fields)
            }

            fn to_swamp_value(&self, registry: &TypeRegistry) -> Value {
                let mut values = Vec::new();
                #(values.push(self.#field_names.to_swamp_value(registry));)*

                let resolved_type = Self::get_resolved_type(registry);
                match &resolved_type {
                    ResolvedType::Struct(struct_type) => {
                        Value::Struct(struct_type.clone(), values, resolved_type)
                    },
                    _ => unreachable!("get_resolved_type returned non-struct type")
                }
            }

            fn from_swamp_value(value: &Value) -> Result<Self, String> {
                match value {
                    Value::Struct(struct_type_ref, values, _) => {
                        if struct_type_ref.borrow().name.text != stringify!(#name) {
                            return Err(format!(
                                "Expected {} struct, got {}",
                                stringify!(#name),
                                struct_type_ref.borrow().name.text
                            ));
                        }
                        #(#from_field_extractions)*
                        Ok(Self {
                            #(#field_names),*
                        })
                    }
                    _ => Err(format!("Expected {} struct", stringify!(#name)))
                }
            }
        }
    };

    TokenStream::from(expanded)
}

#[proc_macro_attribute]
pub fn swamp_fn(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input_fn = parse_macro_input!(item as syn::ItemFn);
    let fn_name = &input_fn.sig.ident;
    let module_name = format_ident!("swamp_{}", fn_name.to_string().to_lowercase());

    // Get the context type from the first parameter
    let context_type = match &input_fn.sig.inputs[0] {
        syn::FnArg::Typed(pat_type) => &*pat_type.ty,
        _ => panic!("First parameter must be the context type"),
    };

    // Extract the inner type from &mut MyContext
    let context_inner_type = match context_type {
        syn::Type::Reference(type_ref) => &*type_ref.elem,
        _ => panic!("Context parameter must be a mutable reference"),
    };

    // Extract return type
    let return_type = match &input_fn.sig.output {
        syn::ReturnType::Default => quote!(<()>::get_resolved_type(registry)),
        syn::ReturnType::Type(_, ty) => quote!(<#ty>::get_resolved_type(registry)),
    };

    // Skip the context parameter
    let args = input_fn
        .sig
        .inputs
        .iter()
        .skip(1)
        .map(|arg| {
            if let syn::FnArg::Typed(pat_type) = arg {
                let pat = &pat_type.pat;
                let ty = &pat_type.ty;
                (pat, ty)
            } else {
                panic!("self parameters not supported yet")
            }
        })
        .collect::<Vec<_>>();

    let arg_count = args.len();
    let arg_indices = 0..arg_count;
    let (patterns, types): (Vec<_>, Vec<_>) = args.iter().copied().unzip();

    let expanded = quote! {
        #input_fn  // Keep the original function

        mod #module_name {
            use super::*;
            use swamp_script_core::prelude::*;

            pub struct Function {
                pub name: &'static str,
                pub function_id: ExternalFunctionId,
            }

            impl Function {
                pub fn new(function_id: ExternalFunctionId) -> Self {
                    Self {
                        name: stringify!(#fn_name),
                        function_id,
                    }
                }

                pub fn handler<'a>(
                    &'a self,
                    registry: &'a TypeRegistry,
                ) -> Box<dyn FnMut(&[Value], &mut #context_inner_type) -> Result<Value, ValueError> + 'a> {
                    Box::new(move |args: &[Value], ctx: &mut #context_inner_type| {
                        if args.len() != #arg_count {
                            return Err(ValueError::WrongNumberOfArguments {
                                expected: #arg_count,
                                got: args.len(),
                            });
                        }

                        // Convert arguments
                        #(
                            let #patterns = <#types>::from_swamp_value(&args[#arg_indices])
                                .map_err(|e| ValueError::TypeError(e))?;
                        )*

                        // Call the function with context
                        let result = super::#fn_name(ctx, #(#patterns),*);

                        // Convert result back to Value
                        Ok(result.to_swamp_value(registry))
                    })
                }

                pub fn get_definition(&self, registry: &TypeRegistry) -> ResolvedExternalFunctionDefinition {
                    ResolvedExternalFunctionDefinition {
                        name: LocalIdentifier::from_str(self.name),
                        signature: ResolvedFunctionSignature {
                            parameters: vec![
                                #(ResolvedParameter {
                                    name: stringify!(#patterns).to_string(),
                                    resolved_type: <#types>::get_resolved_type(registry),
                                    ast_parameter: Parameter::default(),
                                    is_mutable: false,
                                },)*
                            ],
                            return_type: #return_type,
                        },
                        id: self.function_id,
                    }
                }
            }
        }
    };

    TokenStream::from(expanded)
}

#[proc_macro_derive(SwampExportEnum, attributes(swamp))]
pub fn derive_swamp_export_enum(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;

    let expanded = match input.data {
        syn::Data::Enum(ref data) => {
            let variant_matches = data.variants.iter().enumerate().map(|(variant_index, variant)| {
                let variant_name = &variant.ident;

                match &variant.fields {
                    syn::Fields::Unit => {
                        quote! {
                            #name::#variant_name => {
                                let variant_type = ResolvedEnumVariantType {
                                    owner: enum_type.clone(),
                                    data: ResolvedEnumVariantContainerType::Nothing,
                                    name: LocalTypeIdentifier::from_str(stringify!(#variant_name)),
                                    number: #variant_index as TypeNumber,
                                };
                                Value::EnumVariantSimple(Rc::new(variant_type))
                            }
                        }
                    }
                    syn::Fields::Named(fields) => {
                        let field_names: Vec<_> = fields.named.iter().map(|f| &f.ident).collect();
                        let field_types: Vec<_> = fields.named.iter().map(|f| &f.ty).collect();

                        let field_type_conversions = field_types.iter().map(|ty| {
                            match quote!(#ty).to_string().as_str() {
                                "f32" => quote! { registry.get_float_type() },
                                "i32" => quote! { registry.get_int_type() },
                                "bool" => quote! { registry.get_bool_type() },
                                "String" => quote! { registry.get_string_type() },
                                ty => quote! { panic!("Unsupported type: {}", #ty) },
                            }
                        });

                        let field_value_conversions = field_names.iter().zip(field_types.iter()).map(|(name, ty)| {
                            match quote!(#ty).to_string().as_str() {
                                "f32" => quote! { Value::Float(Fp::from(*#name)) },
                                "i32" => quote! { Value::Int(*#name) },
                                "bool" => quote! { Value::Bool(*#name) },
                                "String" => quote! { Value::String(#name.clone()) },
                                ty => quote! { panic!("Unsupported type: {}", #ty) },
                            }
                        });

                        quote! {
                            #name::#variant_name { #(ref #field_names),* } => {
                                let mut fields = SeqMap::new();
                                #(
                                    fields.insert(
                                        IdentifierName(stringify!(#field_names).to_string()),
                                        #field_type_conversions
                                    );
                                )*

                                let common = CommonEnumVariantType {
                                    number: #variant_index as TypeNumber,
                                    module_path: ModulePath::new(),
                                    variant_name: LocalTypeIdentifier::from_str(stringify!(#variant_name)),
                                    enum_ref: enum_type.clone(),
                                };

                                let variant_struct = Rc::new(ResolvedEnumVariantStructType {
                                    common,
                                    fields,
                                    ast_struct: AnonymousStruct::default(),
                                });

                                let values = vec![
                                    #(#field_value_conversions),*
                                ];

                                Value::EnumVariantStruct(variant_struct, values)
                            }
                        }
                    }


                    syn::Fields::Unnamed(fields) => {
                        let field_types: Vec<_> = fields.unnamed.iter().map(|f| &f.ty).collect();
                        let field_names: Vec<_> = (0..field_types.len())
                            .map(|i| format_ident!("field_{}", i))
                            .collect::<Vec<_>>();

                        let field_type_conversions = field_types.iter().map(|ty| {
                            match quote!(#ty).to_string().as_str() {
                                "f32" => quote! { registry.get_float_type() },
                                "i32" => quote! { registry.get_int_type() },
                                "bool" => quote! { registry.get_bool_type() },
                                "String" => quote! { registry.get_string_type() },
                                ty => quote! { panic!("Unsupported type: {}", #ty) },
                            }
                        });

                        let field_value_conversions = field_names.iter().zip(field_types.iter()).map(|(name, ty)| {
                            match quote!(#ty).to_string().as_str() {
                                "f32" => quote! { Value::Float(Fp::from(*#name)) },
                                "i32" => quote! { Value::Int(*#name) },
                                "bool" => quote! { Value::Bool(*#name) },
                                "String" => quote! { Value::String(#name.clone()) },
                                ty => quote! { panic!("Unsupported type: {}", #ty) },
                            }
                        });

                        quote! {
                            #name::#variant_name(#(ref #field_names),*) => {
                                let fields_in_order = vec![
                                    #(#field_type_conversions),*
                                ];

                                let common = CommonEnumVariantType {
                                    number: #variant_index as TypeNumber,
                                    module_path: ModulePath::new(),
                                    variant_name: LocalTypeIdentifier::from_str(stringify!(#variant_name)),
                                    enum_ref: enum_type.clone(),
                                };

                                let variant_tuple = Rc::new(ResolvedEnumVariantTupleType {
                                    common,
                                    fields_in_order,
                                });

                                let values = vec![
                                    #(#field_value_conversions),*
                                ];

                                Value::EnumVariantTuple(variant_tuple, values)
                            }
                        }
                    }
                }
            });

            quote! {
                impl SwampExport for #name {
                    fn get_resolved_type(registry: &TypeRegistry) -> ResolvedType {
                        let enum_type = Rc::new(ResolvedEnumType {
                            name: LocalTypeIdentifier::from_str(stringify!(#name)),
                            number: registry.allocate_type_number(),
                            module_path: ModulePath(vec![]),
                        });
                        ResolvedType::Enum(enum_type)
                    }

                    fn to_swamp_value(&self, registry: &TypeRegistry) -> Value {
                        let enum_type = match Self::get_resolved_type(registry) {
                            ResolvedType::Enum(t) => t,
                            _ => unreachable!(),
                        };

                        match self {
                            #(#variant_matches),*
                        }
                    }

                    fn from_swamp_value(value: &Value) -> Result<Self, String> {
                        match value {
                            Value::EnumVariantSimple(_) |
                            Value::EnumVariantTuple(_, _) |
                            Value::EnumVariantStruct(_, _) => {
                                todo!("Implement from_swamp_value for enums") // TODO: PBJ: Fix this when needed
                            }
                            _ => Err(format!("Expected enum variant, got {:?}", value))
                        }
                    }
                }
            }
        }
        _ => panic!("SwampExportEnum can only be derived for enums"),
    };

    TokenStream::from(expanded)
}