ad-astra-export 1.0.0

Embeddable scripting language platform Ad Astra. Macro Crate.
Documentation
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
////////////////////////////////////////////////////////////////////////////////
// This file is part of "Ad Astra", an embeddable scripting programming       //
// language platform.                                                         //
//                                                                            //
// This work is proprietary software with source-available code.              //
//                                                                            //
// To copy, use, distribute, or contribute to this work, you must agree to    //
// the terms of the General License Agreement:                                //
//                                                                            //
// https://github.com/Eliah-Lakhin/ad-astra/blob/master/EULA.md               //
//                                                                            //
// The agreement grants a Basic Commercial License, allowing you to use       //
// this work in non-commercial and limited commercial products with a total   //
// gross revenue cap. To remove this commercial limit for one of your         //
// products, you must acquire a Full Commercial License.                      //
//                                                                            //
// If you contribute to the source code, documentation, or related materials, //
// you must grant me an exclusive license to these contributions.             //
// Contributions are governed by the "Contributions" section of the General   //
// License Agreement.                                                         //
//                                                                            //
// Copying the work in parts is strictly forbidden, except as permitted       //
// under the General License Agreement.                                       //
//                                                                            //
// If you do not or cannot agree to the terms of this Agreement,              //
// do not use this work.                                                      //
//                                                                            //
// This work is provided "as is", without any warranties, express or implied, //
// except where such disclaimers are legally invalid.                         //
//                                                                            //
// Copyright (c) 2024 Ilya Lakhin (Илья Александрович Лахин).                 //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

use proc_macro2::{Span, TokenStream};
use quote::{quote_spanned, ToTokens};
use syn::Type;

use crate::utils::Facade;

pub trait DefaultScriptOperator {
    fn new_stream(span: Span, lhs: &Type, rhs: Option<&Type>) -> TokenStream;
}

pub struct ScriptAssign<'a> {
    pub span: Span,
    pub ty: &'a Type,
}

impl<'a> ToTokens for ScriptAssign<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();
        let intrinsics = span.face_intrinsics();

        let ty = self.ty;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptAssign for #ty {
                type RHS = #ty;

                fn script_assign(
                    _origin: #core::runtime::Origin,
                    lhs: #core::runtime::Arg,
                    rhs: #core::runtime::Arg,
                ) -> #core::runtime::RuntimeResult<()> {
                    #intrinsics::canonicals::script_assign::<#ty>(lhs, rhs)
                }
            }
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptConcat<'a> {
    pub span: Span,
    pub ty: &'a Type,
}

impl<'a> ToTokens for ScriptConcat<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();
        let intrinsics = span.face_intrinsics();

        let ty = self.ty;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptConcat for #ty {
                type Result = #ty;

                fn script_concat(
                    origin: #core::runtime::Origin,
                    items: &mut [#core::runtime::Arg],
                ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                    #intrinsics::canonicals::script_concat::<#ty>(origin, items)
                }
            }
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptClone<'a> {
    pub span: Span,
    pub lhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptClone<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, _rhs: Option<&Type>) -> TokenStream {
        ScriptClone { span, lhs }.to_token_stream()
    }
}

impl<'a> ToTokens for ScriptClone<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();

        let lhs = self.lhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptClone for #lhs {}
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptDebug<'a> {
    pub span: Span,
    pub lhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptDebug<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, _rhs: Option<&Type>) -> TokenStream {
        ScriptDebug { span, lhs }.to_token_stream()
    }
}

impl<'a> ToTokens for ScriptDebug<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();

        let lhs = self.lhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptDebug for #lhs {}
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptDisplay<'a> {
    pub span: Span,
    pub lhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptDisplay<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, _rhs: Option<&Type>) -> TokenStream {
        ScriptDisplay { span, lhs }.to_token_stream()
    }
}

impl<'a> ToTokens for ScriptDisplay<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();

        let lhs = self.lhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptDisplay for #lhs {}
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptPartialEq<'a> {
    pub span: Span,
    pub lhs: &'a Type,
    pub rhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptPartialEq<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, rhs: Option<&Type>) -> TokenStream {
        ScriptPartialEq {
            span,
            lhs,
            rhs: rhs.unwrap_or(lhs),
        }
        .to_token_stream()
    }
}

impl<'a> ToTokens for ScriptPartialEq<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();
        let partial_eq = span.face_partial_eq();

        let lhs = self.lhs;
        let rhs = self.rhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptPartialEq for #lhs {
                type RHS = #rhs;

                fn script_eq(
                    _origin: #core::runtime::Origin,
                    mut lhs: #core::runtime::Arg,
                    mut rhs: #core::runtime::Arg,
                ) -> #core::runtime::RuntimeResult<bool> {
                    let lhs = #core::runtime::Cell::borrow_ref::<#lhs>(
                        &mut lhs.data,
                        lhs.origin,
                    )?;

                    let rhs = #core::runtime::Cell::borrow_ref::<#rhs>(
                        &mut rhs.data,
                        rhs.origin,
                    )?;

                    #core::runtime::RuntimeResult::<bool>::Ok(
                        <#lhs as #partial_eq::<#rhs>>::eq(lhs, rhs),
                    )
                }
            }
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptDefault<'a> {
    pub span: Span,
    pub lhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptDefault<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, _rhs: Option<&Type>) -> TokenStream {
        ScriptDefault { span, lhs }.to_token_stream()
    }
}

impl<'a> ToTokens for ScriptDefault<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();
        let default = span.face_default();

        let lhs = self.lhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptDefault for #lhs {
                fn script_default(
                    origin: #core::runtime::Origin,
                ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                    #core::runtime::Cell::give(
                        origin,
                        <#lhs as #default>::default(),
                    )
                }
            }
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptPartialOrd<'a> {
    pub span: Span,
    pub lhs: &'a Type,
    pub rhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptPartialOrd<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, rhs: Option<&Type>) -> TokenStream {
        ScriptPartialOrd {
            span,
            lhs,
            rhs: rhs.unwrap_or(lhs),
        }
        .to_token_stream()
    }
}

impl<'a> ToTokens for ScriptPartialOrd<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();
        let option = span.face_option();
        let ordering = span.face_ordering();
        let partial_ord = span.face_partial_ord();

        let lhs = self.lhs;
        let rhs = self.rhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptPartialOrd for #lhs {
                type RHS = #rhs;

                fn script_partial_cmp(
                    _origin: #core::runtime::Origin,
                    mut lhs: #core::runtime::Arg,
                    mut rhs: #core::runtime::Arg,
                ) -> #core::runtime::RuntimeResult<#option<#ordering>> {
                    let lhs = #core::runtime::Cell::borrow_ref::<#lhs>(
                        &mut lhs.data,
                        lhs.origin,
                    )?;

                    let rhs = #core::runtime::Cell::borrow_ref::<#rhs>(
                        &mut rhs.data,
                        rhs.origin,
                    )?;

                    #core::runtime::RuntimeResult::<#option<#ordering>>::Ok(
                        <#lhs as #partial_ord::<#rhs>>::partial_cmp(lhs, rhs),
                    )
                }
            }
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptOrd<'a> {
    pub span: Span,
    pub lhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptOrd<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, _rhs: Option<&Type>) -> TokenStream {
        ScriptOrd { span, lhs }.to_token_stream()
    }
}

impl<'a> ToTokens for ScriptOrd<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();
        let ordering = span.face_ordering();
        let ord = span.face_ord();

        let lhs = self.lhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptOrd for #lhs {
                fn script_cmp(
                    _origin: #core::runtime::Origin,
                    mut lhs: #core::runtime::Arg,
                    mut rhs: #core::runtime::Arg,
                ) -> #core::runtime::RuntimeResult<#ordering> {
                    let lhs = #core::runtime::Cell::borrow_ref::<#lhs>(
                        &mut lhs.data,
                        lhs.origin,
                    )?;

                    let rhs = #core::runtime::Cell::borrow_ref::<#lhs>(
                        &mut rhs.data,
                        rhs.origin,
                    )?;

                    #core::runtime::RuntimeResult::<#ordering>::Ok(
                        <#lhs as #ord>::cmp(lhs, rhs),
                    )
                }
            }
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptHash<'a> {
    pub span: Span,
    pub lhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptHash<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, _rhs: Option<&Type>) -> TokenStream {
        ScriptHash { span, lhs }.to_token_stream()
    }
}

impl<'a> ToTokens for ScriptHash<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();

        let lhs = self.lhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptHash for #lhs {}
        )
        .to_tokens(tokens)
    }
}

macro_rules! binary {
    (
        impl $script_operator:ident($script_function:ident)
        for $facade:ident($facade_function:ident)
    ) => {
        pub struct $script_operator<'a> {
            pub span: Span,
            pub lhs: &'a Type,
            pub rhs: &'a Type,
        }

        impl<'a> DefaultScriptOperator for $script_operator<'a> {
            #[inline(always)]
            fn new_stream(span: Span, lhs: &Type, rhs: Option<&Type>) -> TokenStream {
                $script_operator {
                    span,
                    lhs,
                    rhs: rhs.unwrap_or(lhs),
                }
                .to_token_stream()
            }
        }

        impl<'a> ToTokens for $script_operator<'a> {
            fn to_tokens(&self, tokens: &mut TokenStream) {
                let span = self.span;

                let core = span.face_core();
                let $facade = span.$facade();

                let lhs = self.lhs;
                let rhs = self.rhs;

                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::$script_operator for #lhs {
                        type RHS = #rhs;
                        type Result = <#lhs as #$facade::<#rhs>>::Output;

                        fn $script_function(
                            origin: #core::runtime::Origin,
                            lhs: #core::runtime::Arg,
                            rhs: #core::runtime::Arg,
                        ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                            let lhs = #core::runtime::Cell::take::<#lhs>(
                                lhs.data,
                                lhs.origin,
                            )?;

                            let rhs = #core::runtime::Cell::take::<#rhs>(
                                rhs.data,
                                rhs.origin,
                            )?;

                            let result = <#lhs as #$facade::<#rhs>>::$facade_function(
                                lhs,
                                rhs,
                            );

                            #core::runtime::Cell::give(origin, result)
                        }
                    }
                )
                .to_tokens(tokens)
            }
        }
    };
}

macro_rules! binary_assign {
    (
        impl $script_operator:ident($script_function:ident)
        for $facade:ident($facade_function:ident)
    ) => {
        pub struct $script_operator<'a> {
            pub span: Span,
            pub lhs: &'a Type,
            pub rhs: &'a Type,
        }

        impl<'a> DefaultScriptOperator for $script_operator<'a> {
            #[inline(always)]
            fn new_stream(span: Span, lhs: &Type, rhs: Option<&Type>) -> TokenStream {
                $script_operator {
                    span,
                    lhs,
                    rhs: rhs.unwrap_or(lhs),
                }
                .to_token_stream()
            }
        }

        impl<'a> ToTokens for $script_operator<'a> {
            fn to_tokens(&self, tokens: &mut TokenStream) {
                let span = self.span;

                let core = span.face_core();
                let $facade = span.$facade();

                let lhs = self.lhs;
                let rhs = self.rhs;

                quote_spanned!(span=>
                    #[allow(non_local_definitions)]
                    impl #core::runtime::ops::$script_operator for #lhs {
                        type RHS = #rhs;

                        fn $script_function(
                            _origin: #core::runtime::Origin,
                            mut lhs: #core::runtime::Cell,
                            rhs: #core::runtime::Arg,
                        ) -> #core::runtime::RuntimeResult<()> {
                            let rhs = #core::runtime::Cell::take::<#rhs>(
                                rhs.data,
                                rhs.origin,
                            )?;

                            let lhs = #core::runtime::Cell::borrow_mut::<#lhs>(
                                &mut lhs.data,
                                lhs.origin,
                            )?;

                            <#lhs as #$facade::<#rhs>>::$facade_function(
                                lhs,
                                rhs,
                            );

                            #core::RuntimeResult::<()>::Ok(())
                        }
                    }
                )
                .to_tokens(tokens)
            }
        }
    };
}

binary!(impl ScriptAdd(script_add) for face_add(add));

binary_assign!(impl ScriptAddAssign(script_add_assign) for face_add_assign(add_assign));

binary!(impl ScriptSub(script_sub) for face_sub(sub));

binary_assign!(impl ScriptSubAssign(script_sub_assign) for face_sub_assign(sub_assign));

binary!(impl ScriptMul(script_mul) for face_mul(mul));

binary_assign!(impl ScriptMulAssign(script_mul_assign) for face_mul_assign(mul_assign));

binary!(impl ScriptDiv(script_div) for face_div(div));

binary_assign!(impl ScriptDivAssign(script_div_assign) for face_div_assign(div_assign));

pub struct ScriptNot<'a> {
    pub span: Span,
    pub lhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptNot<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, _rhs: Option<&Type>) -> TokenStream {
        ScriptNot { span, lhs }.to_token_stream()
    }
}

impl<'a> ToTokens for ScriptNot<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();
        let not = span.face_not();

        let lhs = self.lhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptNot for #lhs {
                type Result = <#lhs as #not>::Output;

                fn script_not(
                    origin: #core::runtime::Origin,
                    lhs: #core::runtime::Arg,
                ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                    let lhs = #core::runtime::Cell::take::<#lhs>(
                        lhs.data,
                        lhs.origin,
                    )?;

                    #core::runtime::Cell::give(origin, <#lhs as #not>::not(lhs))
                }
            }
        )
        .to_tokens(tokens)
    }
}

pub struct ScriptNeg<'a> {
    pub span: Span,
    pub lhs: &'a Type,
}

impl<'a> DefaultScriptOperator for ScriptNeg<'a> {
    #[inline(always)]
    fn new_stream(span: Span, lhs: &Type, _rhs: Option<&Type>) -> TokenStream {
        ScriptNeg { span, lhs }.to_token_stream()
    }
}

impl<'a> ToTokens for ScriptNeg<'a> {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let span = self.span;

        let core = span.face_core();
        let neg = span.face_neg();

        let lhs = self.lhs;

        quote_spanned!(span=>
            #[allow(non_local_definitions)]
            impl #core::runtime::ops::ScriptNeg for #lhs {
                type Result = <#lhs as #neg>::Output;

                fn script_neg(
                    origin: #core::runtime::Origin,
                    lhs: #core::runtime::Arg,
                ) -> #core::runtime::RuntimeResult<#core::runtime::Cell> {
                    let lhs = #core::runtime::Cell::take::<#lhs>(
                        lhs.data,
                        lhs.origin,
                    )?;

                    #core::runtime::Cell::give(origin, <#lhs as #neg>::neg(lhs))
                }
            }
        )
        .to_tokens(tokens)
    }
}

binary!(impl ScriptBitAnd(script_bit_and) for face_bit_and(bitand));

binary_assign!(impl ScriptBitAndAssign(script_bit_and_assign) for face_bit_and_assign(bitand_assign));

binary!(impl ScriptBitOr(script_bit_or) for face_bit_or(bitor));

binary_assign!(impl ScriptBitOrAssign(script_bit_or_assign) for face_bit_or_assign(bitor_assign));

binary!(impl ScriptBitXor(script_bit_xor) for face_bit_xor(bitxor));

binary_assign!(impl ScriptBitXorAssign(script_bit_xor_assign) for face_bit_xor_assign(bitxor_assign));

binary!(impl ScriptShl(script_shl) for face_shl(shl));

binary_assign!(impl ScriptShlAssign(script_shl_assign) for face_shl_assign(shl_assign));

binary!(impl ScriptShr(script_shr) for face_shr(shr));

binary_assign!(impl ScriptShrAssign(script_shr_assign) for face_shr_assign(shr_assign));

binary!(impl ScriptRem(script_rem) for face_rem(rem));

binary_assign!(impl ScriptRemAssign(script_rem_assign) for face_rem_assign(rem_assign));