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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
pub mod iterator;
pub mod util;

use std::{collections::HashMap, rc::Rc};

use crate::{
    context::Context,
    error::Error,
    expr::{annotate, expr_clone, format_value, Expr},
    resolver::compute_dyn_signature,
    scope::Scope,
    util::is_reserved_symbol,
};

use self::{iterator::try_iterator_from, util::eval_module};

// #Insight
// _Not_ a pure evaluator, performs side-effects.

// #Insight
// I don't like the name `interpreter`.

// #todo move excessive error-checking/linting to the resolve/typecheck pass.
// #todo encode effects in the type-system.
// #todo alternative names: Processor, Runner, Interpreter
// #todo split eval_special, eval_func -> not needed if we put everything uniformly in prelude.
// #todo Stack-trace is needed!
// #todo https://clojure.org/reference/evaluation

// #todo try to remove non-needed .into()s

// #todo give more 'general' name.
// #todo what about if a required argument is not passed to a function? currently we report undefined symbol.
fn eval_args(args: &[Expr], context: &mut Context) -> Result<Vec<Expr>, Error> {
    args.iter()
        .map(|x| eval(x, context))
        .collect::<Result<Vec<_>, _>>()
}

fn eval_quote(expr: Expr, context: &mut Context) -> Expr {
    // #insight unpack is OK, no extract needed.
    match expr.unpack() {
        Expr::List(terms) => {
            if terms.is_empty() {
                expr
            } else {
                if let Some(sym) = terms[0].unpack().as_symbol() {
                    if sym == "unquot" {
                        debug_assert!(terms.len() == 2);
                        // #todo remove the unwrap!
                        eval(&terms[1], context).unwrap()
                    } else {
                        expr
                    }
                } else {
                    expr
                }
            }
        }
        _ => expr,
    }
}

// #todo needs better conversion to Expr::Annotated

/// Evaluates via expression rewriting. The expression `expr` evaluates to
/// a fixed point. In essence this is a 'tree-walk' interpreter.
pub fn eval(expr: &Expr, context: &mut Context) -> Result<Expr, Error> {
    match expr.unpack() {
        // #todo are you sure?
        // Expr::Annotated(..) => eval(expr.unpack(), env),
        Expr::Symbol(symbol) => {
            // #todo differentiate between evaluating symbol in 'op' position.

            if is_reserved_symbol(symbol) {
                return Ok(expr.clone());
            }

            // #todo handle 'PathSymbol'

            // #todo try to populate "method"/"signature" annotations during
            // #todo this is missing now that we don't have the resolve stage.
            // #todo maybe resolve or optimize should already have placed the method in the AST?

            let value = if let Some(Expr::Symbol(method)) = expr.annotation("method") {
                // If the symbol is annotated with a `method`, it's in 'operator' position.
                // `method` is just one of the variants of a multi-method-function.
                if let Some(value) = context.scope.get(method) {
                    value
                } else {
                    // #todo ultra-hack, if the method is not found, try to lookup the function symbol, fall-through.
                    // #todo should do proper type analysis here.

                    context
                        .scope
                        .get(symbol)
                        .ok_or::<Error>(Error::undefined_function(
                            symbol,
                            method,
                            &format!("undefined function `{symbol}` with signature `{method}"),
                            expr.range(),
                        ))?
                }
            } else {
                context
                    .scope
                    .get(symbol)
                    .ok_or::<Error>(Error::undefined_symbol(
                        &symbol,
                        &format!("symbol not defined: `{symbol}`"),
                        expr.range(),
                    ))?
            };

            // #todo hm, can we somehow work with references?
            // #hint this could help: https://doc.rust-lang.org/std/rc/struct.Rc.html#method.unwrap_or_clone

            Ok(expr_clone(&value))
        }
        Expr::KeySymbol(..) => {
            // #todo handle 'PathSymbol'
            // #todo strip annotation?

            // #todo lint '::' etc.
            // #todo check that if there is a leading ':' there is only one ':', make this a lint warning!
            // #todo consider renaming KeywordSymbol to KeySymbol.

            // A `Symbol` that starts with `:` is a so-called `KeywordSymbol`. Keyword
            // symbols evaluate to themselves, and are convenient to use as Map keys,
            // named (keyed) function parameter, enum variants, etc.
            Ok(expr.clone())
        }
        // #todo if is unquotable!!
        Expr::If(predicate, true_clause, false_clause) => {
            let predicate = eval(predicate, context)?;

            let Some(predicate) = predicate.as_bool() else {
                return Err(Error::invalid_arguments(
                    "the if predicate is not a boolean value",
                    predicate.range(),
                ));
            };

            if predicate {
                eval(true_clause, context)
            } else if let Some(false_clause) = false_clause {
                eval(false_clause, context)
            } else {
                // #todo what should we return if there is no false-clause? Zero/Never?
                Ok(Expr::One.into())
            }
        }
        Expr::List(list) => {
            // #todo no need for dynamic invocable, can use (apply f ...) / (invoke f ...) instead.
            // #todo replace head/tail with first/rest

            if list.is_empty() {
                // () == One (Unit)
                // This is handled statically, in the parser, but an extra, dynamic
                // check is needed in the evaluator to handle the case where the
                // expression is constructed programmatically (e.g. self-modifying code,
                // dynamically constructed expression, homoiconicity, etc).
                return Ok(Expr::One.into());
            }

            // The unwrap here is safe.
            let head = list.first().unwrap();
            let tail = &list[1..];

            // #todo could check special forms before the eval

            // #todo this is an ULTRA-HACK! SUPER NASTY/UGLY CODE, refactor!

            // Evaluate the head, try to find dynamic signature
            let head = if let Some(name) = head.as_symbol() {
                if !is_reserved_symbol(name) {
                    // #todo super nasty hack!!!!
                    let args = eval_args(tail, context)?;

                    if let Some(value) = context.scope.get(name) {
                        if let Expr::Func(params, ..) = value.unpack() {
                            // #todo ultra-hack to kill shared ref to `env`.
                            let params = params.clone();

                            let prev_scope = context.scope.clone();
                            context.scope = Rc::new(Scope::new(prev_scope.clone()));

                            for (param, arg) in params.iter().zip(&args) {
                                let Some(param) = param.as_symbol() else {
                                    return Err(Error::invalid_arguments(
                                        "parameter is not a symbol",
                                        param.range(),
                                    ));
                                };

                                context.scope.insert(param, arg.clone());
                            }

                            let signature = compute_dyn_signature(&args, context);
                            let head = annotate(
                                head.clone(),
                                "method",
                                Expr::Symbol(format!("{name}$${signature}")),
                            );
                            let head = eval(&head, context)?;

                            context.scope = prev_scope;

                            head
                        } else if let Expr::ForeignFunc(_) = value.unpack() {
                            let signature = compute_dyn_signature(&args, &context);
                            let head = annotate(
                                head.clone(),
                                "method",
                                Expr::Symbol(format!("{name}$${signature}")),
                            );
                            let head = eval(&head, context)?;

                            head
                        } else {
                            eval(head, context)?
                        }
                    } else {
                        eval(head, context)?
                    }
                } else {
                    eval(head, context)?
                }
            } else {
                eval(head, context)?
            };

            // #todo move special forms to prelude, as Expr::Macro or Expr::Special

            match head.unpack() {
                Expr::Func(params, body, func_scope) => {
                    // Evaluate the arguments before calling the function.
                    let args = eval_args(tail, context)?;

                    // #todo ultra-hack to kill shared ref to `env`.
                    let params = params.clone();

                    // Dynamic scoping, #todo convert to lexical.

                    let prev_scope = context.scope.clone();
                    context.scope = Rc::new(Scope::new(func_scope.clone())); // #insight notice we use func_scope here!

                    for (param, arg) in params.iter().zip(args) {
                        let Some(param) = param.as_symbol() else {
                            return Err(Error::invalid_arguments(
                                "parameter is not a symbol",
                                param.range(),
                            ));
                        };

                        context.scope.insert(param, arg);
                    }

                    // #todo this code is the same as in the (do ..) block, extract.

                    // #todo do should be 'monadic', propagate Eff (effect) wrapper.
                    let mut value = Expr::One;

                    for expr in body {
                        value = eval(expr, context)?;
                    }

                    context.scope = prev_scope;

                    Ok(value)
                }
                Expr::ForeignFunc(foreign_function) => {
                    // #todo do NOT pre-evaluate args for ForeignFunc, allow to implement 'macros'.
                    // Foreign Functions do NOT change the environment, hmm...
                    // #todo use RefCell / interior mutability instead, to allow for changing the environment (with Mutation Effect)

                    // println!("--> {:?}", context.scope);

                    // Evaluate the arguments before calling the function.
                    let args = eval_args(tail, context)?;

                    let result = foreign_function(&args, context);

                    // If the error has no range, try to apply the range of the invocation.
                    if let Err(mut error) = result {
                        if let Some(note) = error.notes.first_mut() {
                            if note.range.is_none() {
                                note.range = expr.range()
                            }
                        };

                        return Err(error);
                    };

                    result
                }
                Expr::Array(arr) => {
                    // Evaluate the arguments before calling the function.
                    let args = eval_args(tail, context)?;

                    // #todo optimize this!
                    // #todo error checking, one arg, etc.
                    let index = &args[0];
                    // #todo we need UInt, USize, Nat type
                    let Some(index) = index.as_int() else {
                        return Err(Error::invalid_arguments(
                            "invalid array index, expecting Int",
                            index.range(),
                        ));
                    };
                    let index = index as usize;
                    if let Some(value) = arr.borrow().get(index) {
                        // #todo replace the clone with the custom expr::copy/ref
                        Ok(value.clone().into())
                    } else {
                        // #todo introduce Maybe { Some, None }
                        Ok(Expr::One.into())
                    }
                }
                Expr::Dict(dict) => {
                    // Evaluate the arguments before calling the function.
                    let args = eval_args(tail, context)?;

                    // #todo optimize this!
                    // #todo error checking, one arg, stringable, etc.

                    // #insight no need to unpack, format_value sees-through.
                    let key = format_value(&args[0]);
                    if let Some(value) = dict.borrow().get(&key) {
                        Ok(value.clone().into())
                    } else {
                        // #todo introduce Maybe { Some, None }
                        Ok(Expr::One.into())
                    }
                }
                // #todo add handling of 'high-level', compound expressions here.
                // #todo Expr::If
                // #todo Expr::Let
                // #todo Expr::Do
                // #todo Expr::..
                Expr::Symbol(s) => {
                    match s.as_str() {
                        // special term
                        // #todo the low-level handling of special forms should use the above high-level cases.
                        // #todo use the `optimize`/`raise` function, here to prepare high-level expression for evaluation, to avoid duplication.
                        "do" => {
                            // #todo do should be 'monadic', propagate Eff (effect) wrapper.
                            let mut value = Expr::One.into();

                            // #todo extract this.

                            let prev_scope = context.scope.clone();
                            context.scope = Rc::new(Scope::new(prev_scope.clone()));

                            for expr in tail {
                                value = eval(expr, context)?;
                            }

                            context.scope = prev_scope;

                            Ok(value)
                        }
                        "ann" => {
                            // #Insight implemented as special-form because it applies to Ann<Expr>.
                            // #todo try to implement as ForeignFn

                            if tail.len() != 1 {
                                return Err(Error::invalid_arguments(
                                    "`ann` requires one argument",
                                    expr.range(),
                                ));
                            }

                            // #todo support multiple arguments.

                            let expr = tail.first().unwrap();

                            if let Some(ann) = expr.annotations() {
                                Ok(Expr::dict(ann.clone()))
                            } else {
                                Ok(Expr::dict(HashMap::new()))
                            }
                        }
                        "eval" => {
                            let [expr] = tail else {
                                return Err(Error::invalid_arguments(
                                    "missing expression to be evaluated",
                                    expr.range(),
                                ));
                            };

                            // #todo consider naming this `form`?
                            let expr = eval(expr, context)?;

                            eval(&expr, context)
                        }
                        "quot" => {
                            // #insight not obvious how to move to static/comptime phase.
                            // #todo doesn't quote all exprs, e.g. the if expression.
                            // #todo optimize with custom exprs, e.g Expr::Quot, Expr::QuasiQuot, etc.

                            let [value] = tail else {
                                return Err(Error::invalid_arguments(
                                    "missing quote target",
                                    expr.range(),
                                ));
                            };

                            Ok(value
                                .clone()
                                .transform_mut(&mut |expr| eval_quote(expr, context)))
                        }
                        // #todo check racket.
                        // #todo implement for->list, for->map, for->fold, etc.
                        "for" => {
                            // #insight
                            // `while` is a generalization of `if`
                            // `for` is a generalization of `let`
                            // `for` is related with `do`
                            // `for` is monadic

                            // (for (x 10) (writeln x))

                            // #todo reuse code from let
                            // #todo the resolver should handle this.

                            if tail.len() < 2 {
                                // #todo add more structural checks.
                                // #todo proper error!
                                return Err(Error::invalid_arguments(
                                    "missing for arguments",
                                    expr.range(),
                                ));
                            }

                            let binding = tail.first().unwrap();
                            let body = &tail[1..];

                            // #todo should check both for list and array.
                            let Some(binding_parts) = binding.as_list() else {
                                // #todo proper error!
                                return Err(Error::invalid_arguments(
                                    "invalid for binding",
                                    binding.range(),
                                ));
                            };

                            let [var, value] = &binding_parts[..] else {
                                return Err(Error::invalid_arguments(
                                    "invalid for binding",
                                    binding.range(),
                                ));
                            };

                            let Some(var) = var.as_symbol() else {
                                // #todo proper error!
                                return Err(Error::invalid_arguments(
                                    "invalid for binding, malformed variable",
                                    var.range(),
                                ));
                            };

                            // #insight for the ListIterator
                            let value = eval(value, context)?;

                            // #todo also handle (Range start end step)
                            // #todo maybe step should be external to Range, or use SteppedRange, or (Step-By (Range T))
                            let Some(iterator) = try_iterator_from(&value) else {
                                // #todo proper error!
                                return Err(Error::invalid_arguments(
                                    "invalid for binding, the value is not iterable",
                                    value.range(),
                                ));
                            };

                            let prev_scope = context.scope.clone();
                            context.scope = Rc::new(Scope::new(prev_scope.clone()));

                            let mut iterator = iterator.borrow_mut();

                            while let Some(value) = iterator.next() {
                                context.scope.insert(var, value);
                                for expr in body {
                                    // #insight plain `for` is useful only for the side-effects.
                                    let _ = eval(expr, context)?;
                                }
                            }

                            context.scope = prev_scope;

                            Ok(Expr::One)
                        }
                        "while" => {
                            // #insight
                            // `while` is a generalization of `if`
                            // `for` is a generalization of `let`
                            // `for` is related with `do`
                            // `for` is monadic

                            // #todo
                            // try to merge `while` with `for` (maybe `for` is implemented on top of `while`?)

                            let [predicate, body] = tail else {
                                // #todo proper error!
                                return Err(Error::invalid_arguments(
                                    "missing for arguments",
                                    expr.range(),
                                ));
                            };

                            let mut value = Expr::One.into();

                            loop {
                                let predicate = eval(predicate, context)?;

                                let Some(predicate) = predicate.as_bool() else {
                                    return Err(Error::invalid_arguments(
                                        "the `while` predicate is not a boolean value",
                                        predicate.range(),
                                    ));
                                };

                                if !predicate {
                                    break;
                                }

                                value = eval(body, context)?;
                            }

                            Ok(value)
                        }
                        "if" => {
                            // #todo this is a temp hack!
                            let Some(predicate) = tail.get(0) else {
                                return Err(Error::invalid_arguments(
                                    "malformed if predicate",
                                    expr.range(),
                                ));
                            };

                            let Some(true_clause) = tail.get(1) else {
                                return Err(Error::invalid_arguments(
                                    "malformed if true clause",
                                    expr.range(),
                                ));
                            };

                            // #todo don't get false_clause if not required?
                            let false_clause = tail.get(2);

                            let predicate = eval(predicate, context)?;

                            let Some(predicate) = predicate.as_bool() else {
                                return Err(Error::invalid_arguments(
                                    "the if predicate is not a boolean value",
                                    predicate.range(),
                                ));
                            };

                            if predicate {
                                eval(true_clause, context)
                            } else if let Some(false_clause) = false_clause {
                                eval(false_clause, context)
                            } else {
                                // #todo what should we return if there is no false-clause? Zero/Never?
                                Ok(Expr::One.into())
                            }
                        }
                        // #todo is this different enough from `if`?
                        // (cond
                        //   (> i 5) (...)
                        //   (> i 15) (...)
                        //   else (...)
                        // )
                        "cond" => {
                            let mut i = 0;

                            loop {
                                if i >= tail.len() {
                                    // #todo what should we return here? probably Never/Zero?
                                    break Ok(Expr::One);
                                }

                                let Some(predicate) = tail.get(i) else {
                                    return Err(Error::invalid_arguments(
                                        "malformed cond predicate",
                                        expr.range(),
                                    ));
                                };

                                let Some(clause) = tail.get(i + 1) else {
                                    return Err(Error::invalid_arguments(
                                        "malformed cond clause",
                                        expr.range(),
                                    ));
                                };

                                // #todo `else` should not be annotated.
                                // #todo should NOT annotate symbols and keysymbols!
                                // #todo introduce a helper to check for specific symbol.

                                if let Expr::Symbol(sym) = predicate.unpack() {
                                    if sym == "else" {
                                        break eval(clause, context);
                                    }
                                }

                                let predicate = eval(predicate, context)?;

                                let Some(predicate) = predicate.as_bool() else {
                                    return Err(Error::invalid_arguments(
                                        "the if predicate is not a boolean value",
                                        predicate.range(),
                                    ));
                                };

                                if predicate {
                                    break eval(clause, context);
                                }

                                i += 2;
                            }
                        }
                        // #todo for-each or overload for?
                        "for-each" => {
                            // #todo this is a temp hack!
                            let [seq, var, body] = tail else {
                                return Err(Error::invalid_arguments(
                                    "malformed `for-each`",
                                    expr.range(),
                                ));
                            };

                            let seq = eval(seq, context)?;

                            let Some(arr) = seq.as_array() else {
                                return Err(Error::invalid_arguments(
                                    "`for-each` requires a `Seq` as the first argument",
                                    seq.range(),
                                ));
                            };

                            let Some(sym) = var.as_symbol() else {
                                return Err(Error::invalid_arguments(
                                    "`for-each` requires a symbol as the second argument",
                                    var.range(),
                                ));
                            };

                            let prev_scope = context.scope.clone();
                            context.scope = Rc::new(Scope::new(prev_scope.clone()));

                            for x in arr.iter() {
                                // #todo array should have Ann<Expr> use Ann<Expr> everywhere, avoid the clones!
                                // #todo replace the clone with custom expr::ref/copy?
                                context.scope.insert(sym, x.clone());
                                eval(body, context)?;
                            }

                            context.scope = prev_scope;

                            // #todo intentionally don't return a value, reconsider this?
                            Ok(Expr::One.into())
                        }
                        // #todo extract
                        // #todo functions implemented here have dynamic dispatch!
                        // #todo show usage in comments
                        "map" => {
                            // #todo this is a temp hack!
                            let [seq, var, body] = tail else {
                                return Err(Error::invalid_arguments(
                                    "malformed `map`",
                                    expr.range(),
                                ));
                            };

                            let seq = eval(seq, context)?;

                            let Some(arr) = seq.as_array() else {
                                return Err(Error::invalid_arguments(
                                    "`map` requires a `Seq` as the first argument",
                                    seq.range(),
                                ));
                            };

                            let Some(sym) = var.as_symbol() else {
                                return Err(Error::invalid_arguments(
                                    "`map` requires a symbol as the second argument",
                                    var.range(),
                                ));
                            };

                            let prev_scope = context.scope.clone();
                            context.scope = Rc::new(Scope::new(prev_scope.clone()));

                            let mut results: Vec<Expr> = Vec::new();

                            for x in arr.iter() {
                                // #todo array should have Ann<Expr> use Ann<Expr> everywhere, avoid the clones!
                                context.scope.insert(sym, x.clone());
                                let result = eval(body, context)?;
                                // #todo replace the clone with custom expr::ref/copy?
                                results.push(result.unpack().clone());
                            }

                            context.scope = prev_scope.clone();

                            // #todo intentionally don't return a value, reconsider this?
                            Ok(Expr::array(results).into())
                        }
                        "scope-update" => {
                            // #todo consider a function that nests a new scope.
                            // #todo maybe it should be some kind of let? e.g. (`let-all` ?? or `let*` or `let..`)
                            // #todo this is a temp hack.

                            // Updates the scope with bindings of the given dict.

                            let [dict] = tail else {
                                return Err(Error::invalid_arguments(
                                    "malformed `scope-update`",
                                    expr.range(),
                                ));
                            };

                            let dict = eval(dict, context)?;

                            let Some(dict) = dict.as_dict() else {
                                // #todo report what was found!
                                return Err(Error::invalid_arguments(
                                    "malformed `scope-update`, expects Dict argument",
                                    expr.range(),
                                ));
                            };

                            for (name, value) in dict.iter() {
                                // #todo remove clone.
                                context.scope.insert(name, expr_clone(value));
                            }

                            Ok(Expr::One)
                        }
                        "use" => {
                            // #todo extract as function
                            // #insight this code is temporarily(?) moved from `resolver` here.
                            // #todo also introduce a dynamic version of `use`.

                            // #todo temp hack!!!

                            // #todo properly handle this here, strip the use expression, remove from eval
                            // #todo move this to resolve? use should be stripped at dyn-time
                            // #todo also support path as symbol.

                            // Import a directory as a module.

                            let Some(term) = tail.get(0) else {
                                return Err(Error::invalid_arguments(
                                    "malformed use expression",
                                    expr.range(),
                                ));
                            };

                            let Some(module_path) = term.as_string() else {
                                return Err(Error::invalid_arguments(
                                    "malformed use expression",
                                    expr.range(),
                                ));
                            };

                            // #todo make sure paths are relative to the current file.
                            let result = eval_module(module_path, context);

                            if let Err(errors) = result {
                                // #todo precise formating is _required_ here!
                                // eprintln!("{}", format_errors(&errors));
                                // dbg!(errors);
                                // #todo add note with information here!
                                return Err(Error::failed_use(&module_path, errors));
                            };

                            let Ok(Expr::Module(module)) = result else {
                                // #todo could use a panic here, this should never happen.
                                return Err(Error::failed_use(&module_path, vec![]));
                            };

                            // Import public names from module scope into the current scope.

                            // #todo support (use "/path/to/module" *) or (use "/path/to/module" :embed)

                            // #todo temp, needs cleanup!
                            let bindings = module.scope.bindings.borrow().clone();
                            for (name, value) in bindings {
                                // #todo temp fix to not override the special var
                                if name.starts_with("*") {
                                    continue;
                                }

                                // #todo ONLY export public bindings

                                let name = format!("{}/{}", module.stem, name);

                                // #todo assign as top-level bindings!
                                context.scope.insert(name, value.clone());
                            }

                            // #todo what could we return here? the Expr::Module?
                            Ok(Expr::One.into())
                        }
                        "let" => {
                            // #todo this is already parsed statically by resolver, no need to duplicate the tests here?
                            // #todo also report some of these errors statically, maybe in a sema phase?
                            let mut args = tail.iter();

                            loop {
                                let Some(name) = args.next() else {
                                    break;
                                };

                                let Some(value) = args.next() else {
                                    // #todo error?
                                    break;
                                };

                                let Some(s) = name.as_symbol() else {
                                    return Err(Error::invalid_arguments(
                                        &format!("`{name}` is not a Symbol"),
                                        name.range(),
                                    ));
                                };

                                // #todo do we really want this? Maybe convert to a lint?
                                if is_reserved_symbol(s) {
                                    return Err(Error::invalid_arguments(
                                        &format!("let cannot shadow the reserved symbol `{s}`"),
                                        name.range(),
                                    ));
                                }

                                let value = eval(value, context)?;

                                // #todo notify about overrides? use `set`?
                                context.scope.insert(s, value);
                            }

                            // #todo return last value!
                            Ok(Expr::One.into())
                        }
                        "not" => {
                            // #todo make a function
                            // #todo consider binary/bitmask version.
                            // #todo consider operator `~` (_not_ `!`)

                            let [arg] = tail else {
                                return Err(Error::invalid_arguments(
                                    "`not` expects one argument",
                                    expr.range(),
                                ));
                            };

                            let value = eval(arg, context)?;

                            let Some(predicate) = value.as_bool() else {
                                return Err(Error::invalid_arguments(
                                    "`not` argument should be boolean",
                                    expr.range(),
                                ));
                            };

                            Ok(Expr::Bool(!predicate))
                        }
                        "and" => {
                            // #todo what about binary and?
                            // #todo consider operator form? `&&` or `*`
                            // #todo optimize case with 2 arguments.
                            // #insight `and` is not short-circuiting
                            // #todo make a function?
                            // #todo should these 'special forms' get added in scope/env?

                            for arg in tail {
                                let value = eval(arg, context)?;
                                let Some(predicate) = value.as_bool() else {
                                    return Err(Error::invalid_arguments(
                                        "`and` argument should be boolean",
                                        expr.range(),
                                    ));
                                };

                                if !predicate {
                                    return Ok(Expr::Bool(false));
                                }
                            }

                            Ok(Expr::Bool(true))
                        }
                        "or" => {
                            // #todo what about binary or?
                            // #todo consider operator form? `||` or `+`
                            // #insight `or` is short-circuiting so it cannot be implemented as a function

                            for arg in tail {
                                let value = eval(arg, context)?;
                                let Some(predicate) = value.as_bool() else {
                                    return Err(Error::invalid_arguments(
                                        "`or` argument should be boolean",
                                        expr.range(),
                                    ));
                                };

                                if predicate {
                                    return Ok(Expr::Bool(true));
                                }
                            }

                            Ok(Expr::Bool(false))
                        }
                        "Char" => {
                            // #todo report more than 1 arguments.

                            let Some(arg) = tail.get(0) else {
                                return Err(Error::invalid_arguments(
                                    "malformed Char constructor, missing argument",
                                    expr.range(),
                                ));
                            };

                            let Some(c) = arg.as_string() else {
                                return Err(Error::invalid_arguments(
                                    "malformed Char constructor, expected String argument",
                                    expr.range(),
                                ));
                            };

                            if c.len() != 1 {
                                // #todo better error message.
                                return Err(Error::invalid_arguments(
                                    "the Char constructor requires a single-char string",
                                    expr.range(),
                                ));
                            }

                            let c = c.chars().next().unwrap();

                            Ok(Expr::Char(c).into())
                        }
                        "List" => {
                            let args = eval_args(tail, context)?;
                            Ok(Expr::List(args).into())
                        }
                        "Func" => {
                            let Some(params) = tail.first() else {
                                // #todo seems the range is not reported correctly here!!!
                                return Err(Error::invalid_arguments(
                                    "malformed func definition, missing function parameters",
                                    expr.range(),
                                ));
                            };

                            let body = &tail[1..];

                            let Some(params) = params.as_list() else {
                                return Err(Error::invalid_arguments(
                                    "malformed func parameters definition",
                                    params.range(),
                                ));
                            };

                            // #insight captures the static (lexical scope)

                            // #todo optimize!
                            Ok(Expr::Func(
                                params.clone(),
                                body.into(),
                                context.scope.clone(),
                            ))
                        }
                        // #todo macros should be handled at a separate, comptime, macroexpand pass.
                        // #todo actually two passes, macro_def, macro_expand
                        // #todo probably macro handling should be removed from eval, there are no runtime/dynamic macro definitions!!
                        "Macro" => {
                            let Some(params) = tail.first() else {
                                // #todo seems the range is not reported correctly here!!!
                                return Err(Error::invalid_arguments(
                                    "malformed macro definition, missing function parameters",
                                    expr.range(),
                                ));
                            };

                            let body = &tail[1..];

                            let Some(params) = params.as_list() else {
                                return Err(Error::invalid_arguments(
                                    "malformed macro parameters definition",
                                    params.range(),
                                ));
                            };

                            // #todo optimize!
                            Ok(Expr::Macro(params.clone(), body.into()))
                        }
                        _ => {
                            return Err(Error::not_invocable(
                                &format!("symbol `{head}`"),
                                head.range(),
                            ));
                        }
                    }
                }
                _ => {
                    dbg!(&head);
                    return Err(Error::not_invocable(
                        &format!("expression `{head}`"),
                        head.range(),
                    ));
                }
            }
        }
        Expr::Array(items) => {
            // #insight [...] => (Array ...) => it's like a function.
            // #todo can this get pre-evaluated statically in some cases?
            let mut evaled_items = Vec::new();
            for item in items.borrow().iter() {
                evaled_items.push(eval(item, context)?);
            }
            Ok(Expr::array(evaled_items))
        }
        Expr::Dict(dict) => {
            // #insight evaluates the values.
            // #insight [...] => (Dict ...) => it's like a function.
            // #todo nasty code, improve.
            // #todo can this get pre-evaluated statically in some cases?
            let mut evaled_dict = HashMap::new();
            for (k, v) in dict.borrow().iter() {
                evaled_dict.insert(k.clone(), eval(v, context)?);
            }
            Ok(Expr::dict(evaled_dict))
        }
        _ => {
            // #todo hm, maybe need to report an error here? or even select the desired behavior? -> NO ERROR
            // #todo can we avoid the clone?
            // Unhandled expression variants evaluate to themselves.
            Ok(expr.clone())
        }
    }
}