somni-expr 0.3.2

An expression evaluation library
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
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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
//! # Somni expression evaluation Library
//!
//! This crate implements the expression evaluation subset of the Somnni language and VM. The crate
//! can be used by itself, to evaluate simple expressions or even to run complete Somni programs, although
//! slower than the Somni VM would.
//!
//! ## Overview
//!
//! Expressions are a subset of the Somni language:
//!
//! The expression language includes:
//!
//! - Literals: integers, floats, booleans, strings.
//! - Variables
//! - A basic set of operators
//! - Function calls
//!
//! The expression language does not include:
//!
//! - Declaring new variables. You can assign to existing variables.
//! - Control flow (if, loops, etc.)
//! - Complex data structures (arrays, objects, etc.)
//! - Defining functions and variables (these are provided by the context)
//!
//! ## Operators
//!
//! The following binary operators are supported, in order of precedence:
//!
//! - `=`: assign a value to an existing variable
//! - `||`: logical OR, short-circuiting
//! - `&&`: logical AND, short-circuiting
//! - `<`, `<=`, `>`, `>=`, `==`, `!=`: comparison operators
//! - `|`: bitwise OR
//! - `^`: bitwise XOR
//! - `&`: bitwise AND
//! - `<<`, `>>`: bitwise shift
//! - `+`, `-`: addition and subtraction
//! - `*`, `/`: multiplication and division
//!
//! Unary operators include:
//! - `&`: taking the address of a variable
//! - `*`: dereferencing an address to a variable
//! - `!`: logical NOT
//! - `-`: negation
//!
//! For the full specification of the grammar, see the [`parser`] module's documentation.
//!
//! ## Numeric types
//!
//! The Somni language supports three numeric types:
//!
//! - Integers
//! - Signed integers
//! - Floats
//!
//! By default, the library uses the [`DefaultTypeSet`], which uses `u64`, `i64`, and `f64` for
//! these types. You can use other type sets like [`TypeSet32`] or [`TypeSet128`] to use
//! 32-bit or 128-bit integers and floats. You need to specify the type set when creating
//! the context.
//!
//! Numeric integer literals can be either signed or unsigned integers. Their type is inferred from the usage.
//!
//! ## Usage
//!
//! To evaluate an expression, you need to create a [`Context`] first. You can assign
//! variables and define functions in this context, and then you can use this context
//! to evaluate expressions.
//!
//! ```rust
//! use somni_expr::Context;
//!
//! let mut context = Context::new();
//!
//! // Define a variable
//! context.add_variable::<u64>("x", 42);
//! context.add_function("add_one", |x: u64| { x + 1 });
//! context.add_function("floor", |x: f64| { x.floor() as u64 });
//!
//! // Evaluate an expression - we expect it to evaluate
//! // to a number, which is u64 in the default type set.
//! let result = context.evaluate::<u64>("add_one(x + floor(1.2))");
//!
//! assert_eq!(result, Ok(44));
//! ```
//!
//! The context may also include a complete Somni program. The program may use the entirety
//! of the Somni language, not just the expression language.
//!
//! ```rust
//! use somni_expr::Context;
//!
//! let mut context = Context::parse("fn double(x: int) -> int { return x * 2; }").unwrap();
//!
//! // Evaluate an expression by calling the function defined by the program:
//! let result = context.evaluate::<u64>("double(4)");
//!
//! assert_eq!(result, Ok(8));
//! ```
#![warn(missing_docs)]

macro_rules! for_each {
    // Any parenthesized set of choices, allows multiple matchers in the pattern
    ($(($pattern:tt) in [$( ($($choice:tt)*) ),*] => $code:tt;)*) => {
        $(
            macro_rules! inner { $pattern => $code; }

            $(
                inner!( $($choice)* );
            )*
        )*
    };
    // Single type, single matcher
    ($($pattern:tt in [$($choice:ty),*] => $code:tt;)*) => {
        $(
            macro_rules! inner { $pattern => $code; }

            $(
                inner!($choice);
            )*
        )*
    };
}

pub mod error;
pub mod function;
pub mod iter;
pub mod value;
mod visitor;

pub use function::{DynFunction, FunctionCallError};
pub use iter::{SomniIterator, WithIterator};
pub use value::{Place, Reference, SomniStruct, TypedValue};
pub use visitor::ExpressionVisitor;

/// Re-exported for use by the [`somni_struct!`] macro. Not a stable API.
#[doc(hidden)]
pub use indexmap;

/// Constructs a [`SomniStruct`] value from Rust.
///
/// Field values are converted eagerly through the given type context (`&mut T`,
/// obtainable via [`Context::type_context`]), so string fields are interned and
/// nested structs may be built with nested invocations.
///
/// ```
/// use somni_expr::{somni_struct, Context, ExprContext, TypedValue};
///
/// let mut ctx = Context::new();
/// let tc = ctx.type_context();
/// let point = somni_struct!(tc, Point { x: 1u64, y: 2u64 });
/// assert_eq!(point.name(), "Point");
/// assert_eq!(point.fields()["x"], TypedValue::Int(1));
/// ```
#[macro_export]
macro_rules! somni_struct {
    ($ctx:ident, $name:ident { $($field:ident : $value:expr),* $(,)? }) => {{
        let $ctx: &mut _ = $ctx;
        let mut fields = $crate::indexmap::IndexMap::new();
        $(
            let value = $crate::value::LoadStore::store(&$value, $ctx);
            fields.insert(::std::boxed::Box::<str>::from(stringify!($field)), value);
        )*
        $crate::SomniStruct::new(
            ::std::boxed::Box::<str>::from(stringify!($name)),
            fields,
        )
    }};
}

use std::{
    cell::RefCell,
    collections::HashMap,
    fmt::{Debug, Display},
    rc::Rc,
};

use somni_parser::{
    Location,
    ast::{self, Expression, Function, Item, Program},
    parser::{self, TypeSet as ParserTypeSet, parse},
};

use crate::{
    error::MarkInSource,
    function::ExprFn,
    value::{LoadOwned, LoadStore, ValueType},
};

pub use somni_parser::parser::{DefaultTypeSet, TypeSet32, TypeSet128};

/// Defines the backing types for Somni types.
///
/// The [`LoadStore`] and [`LoadOwned`] traits can be used to convert between Rust and Somni types.
pub trait TypeSet: Sized + Default + Debug + 'static {
    /// The typeset that will be used to parse source code.
    type Parser: ParserTypeSet<Integer = Self::Integer, Float = Self::Float>;

    /// The type of unsigned integers in this type set.
    type Integer: Copy + ValueType<NegateOutput: LoadStore<Self>> + LoadStore<Self>;

    /// The type of signed integers in this type set.
    type SignedInteger: Copy + ValueType<NegateOutput: LoadStore<Self>> + LoadStore<Self>;

    /// The type of floating point numbers in this type set.
    type Float: Copy + ValueType<NegateOutput: LoadStore<Self>> + LoadStore<Self>;

    /// The type of a string in this type set.
    type String: ValueType<NegateOutput: LoadStore<Self>> + LoadStore<Self>;

    /// The type of an iterator value in this type set.
    ///
    /// This is the payload carried directly by [`TypedValue::Iter`]. Type sets that
    /// do not support iteration use the uninhabited [`NoIterator`], making it
    /// impossible to construct an iterator value.
    type Iterator: Clone + PartialEq + Debug;

    /// Converts an unsigned integer into a signed integer.
    fn to_signed(v: Self::Integer) -> Result<Self::SignedInteger, OperatorError>;

    /// Converts an unsigned integer into a Rust usize.
    fn to_usize(v: Self::Integer) -> Result<usize, OperatorError>;

    /// Converts the given Rust usize to an integer.
    fn int_from_usize(v: usize) -> Self::Integer;

    /// Loads a string.
    fn load_string<'s>(&'s self, str: &'s Self::String) -> &'s str;

    /// Stores a string.
    fn store_string(&mut self, str: &str) -> Self::String;

    /// Returns whether the given iterator can yield another value.
    fn iter_has_next(&self, iter: &Self::Iterator) -> bool;

    /// Advances the given iterator, returning its next value, or `None` if the
    /// iterator is exhausted.
    fn iter_next(&self, iter: &Self::Iterator) -> Option<TypedValue<Self>>;
}

/// The iterator type used by type sets that do not support iteration.
///
/// This type is uninhabited, so such type sets can never construct a
/// [`TypedValue::Iter`] value.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum NoIterator {}

for_each! {
    (($name:ident, $signed:ty)) in [(DefaultTypeSet, i64), (TypeSet32, i32), (TypeSet128, i128)] => {
        impl TypeSet for $name {
            type Parser = Self;

            type Integer = <Self::Parser as ParserTypeSet>::Integer;
            type SignedInteger = $signed;
            type Float = <Self::Parser as ParserTypeSet>::Float;
            type String = Box<str>;
            type Iterator = NoIterator;

            fn to_signed(v: Self::Integer) -> Result<Self::SignedInteger, OperatorError> {
                <$signed>::try_from(v).map_err(|_| OperatorError::RuntimeError)
            }

            fn to_usize(v: Self::Integer) -> Result<usize, OperatorError> {
                usize::try_from(v).map_err(|_| OperatorError::RuntimeError)
            }

            fn int_from_usize(v: usize) -> Self::Integer {
                Self::Integer::try_from(v).unwrap()
            }

            fn load_string<'s>(&'s self, str: &'s Self::String) -> &'s str {
                str
            }

            fn store_string(&mut self, str: &str) -> Self::String {
                str.to_string().into_boxed_str()
            }

            fn iter_has_next(&self, iter: &Self::Iterator) -> bool {
                match *iter {}
            }

            fn iter_next(&self, iter: &Self::Iterator) -> Option<TypedValue<Self>> {
                match *iter {}
            }
        }
    };
}

/// Represents an error that can occur during operator evaluation.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum OperatorError {
    /// A type error occurred.
    TypeError,
    /// A runtime error occurred.
    RuntimeError,
}

impl Display for OperatorError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let message = match self {
            OperatorError::TypeError => "Type error",
            OperatorError::RuntimeError => "Runtime error",
        };

        f.write_str(message)
    }
}

macro_rules! dispatch_binary {
    ($method:ident) => {
        pub(crate) fn $method(ctx: &mut T, lhs: Self, rhs: Self) -> Result<Self, OperatorError> {
            let result = match (lhs, rhs) {
                (Self::Bool(value), Self::Bool(other)) => {
                    ValueType::$method(value, other)?.store(ctx)
                }
                (Self::Int(value), Self::Int(other)) => {
                    ValueType::$method(value, other)?.store(ctx)
                }
                (Self::SignedInt(value), Self::SignedInt(other)) => {
                    ValueType::$method(value, other)?.store(ctx)
                }
                (Self::MaybeSignedInt(value), Self::MaybeSignedInt(other)) => {
                    match ValueType::$method(value, other)?.store(ctx) {
                        Self::Int(v) => Self::MaybeSignedInt(v),
                        other => other,
                    }
                }
                (Self::Float(value), Self::Float(other)) => {
                    ValueType::$method(value, other)?.store(ctx)
                }
                (Self::String(value), Self::String(other)) => {
                    ValueType::$method(value, other)?.store(ctx)
                }
                (Self::Int(value), Self::MaybeSignedInt(other)) => {
                    ValueType::$method(value, other)?.store(ctx)
                }
                (Self::MaybeSignedInt(value), Self::Int(other)) => {
                    ValueType::$method(value, other)?.store(ctx)
                }
                (Self::SignedInt(value), Self::MaybeSignedInt(other)) => {
                    ValueType::$method(value, T::to_signed(other)?)?.store(ctx)
                }
                (Self::MaybeSignedInt(value), Self::SignedInt(other)) => {
                    ValueType::$method(T::to_signed(value)?, other)?.store(ctx)
                }
                _ => return Err(OperatorError::TypeError),
            };

            Ok(result)
        }
    };
}

macro_rules! dispatch_unary {
    ($method:ident) => {
        pub(crate) fn $method(ctx: &mut T, operand: Self) -> Result<Self, OperatorError> {
            match operand {
                Self::Bool(value) => Ok(ValueType::$method(value)?.store(ctx)),
                Self::Int(value) | Self::MaybeSignedInt(value) => {
                    Ok(ValueType::$method(value)?.store(ctx))
                }
                Self::SignedInt(value) => Ok(ValueType::$method(value)?.store(ctx)),
                Self::Float(value) => Ok(ValueType::$method(value)?.store(ctx)),
                Self::String(value) => Ok(ValueType::$method(value)?.store(ctx)),
                _ => return Err(OperatorError::TypeError),
            }
        }
    };
}

impl<T> TypedValue<T>
where
    T: TypeSet,
{
    dispatch_binary!(equals);
    dispatch_binary!(less_than);
    dispatch_binary!(less_than_or_equal);
    dispatch_binary!(not_equals);
    dispatch_binary!(bitwise_or);
    dispatch_binary!(bitwise_xor);
    dispatch_binary!(bitwise_and);
    dispatch_binary!(shift_left);
    dispatch_binary!(shift_right);
    dispatch_binary!(add);
    dispatch_binary!(subtract);
    dispatch_binary!(multiply);
    dispatch_binary!(divide);
    dispatch_binary!(modulo);
    dispatch_unary!(not);
    dispatch_unary!(negate);
}

/// An expression context that provides the necessary environment for evaluating expressions.
pub trait ExprContext<T = DefaultTypeSet>
where
    T: TypeSet,
{
    /// Returns a reference to the `TypeSet`.
    fn type_context(&mut self) -> &mut T;

    /// Attempts to load a variable from the context.
    fn try_load_variable(&mut self, variable: &str) -> Option<TypedValue<T>>;

    /// Declares a variable in the context.
    fn declare(&mut self, variable: &str, value: TypedValue<T>);

    /// Assigns a new value to a variable in the context.
    fn assign_variable(&mut self, variable: &str, value: &TypedValue<T>) -> Result<(), Box<str>>;

    /// Returns the [`Place`] naming a variable (the root of a reference).
    fn place_of_variable(&mut self, variable: &str) -> Result<Place, Box<str>>;

    /// Loads (a clone of) the value at the given place, descending its field path.
    fn load_place(&mut self, place: &Place) -> Result<TypedValue<T>, Box<str>>;

    /// Stores a value into the given place, descending its field path.
    fn store_place(&mut self, place: &Place, value: &TypedValue<T>) -> Result<(), Box<str>>;

    /// Returns the `(field name, field type name)` pairs of a struct definition,
    /// in declaration order, or `None` if no such struct is defined.
    ///
    /// Used to validate and coerce struct literals and to type-check struct-typed
    /// annotations.
    fn struct_fields(&self, struct_name: &str) -> Option<Vec<(Box<str>, Box<str>)>>;

    /// Opens a new scope in the current stack frame.
    fn open_scope(&mut self);

    /// Closes the last scope in the current stack frame.
    fn close_scope(&mut self);

    /// Calls a function in the context.
    fn call_function(
        &mut self,
        function_name: &str,
        args: &[TypedValue<T>],
    ) -> Result<TypedValue<T>, FunctionCallError>;
}

/// An error that occurs during evaluation of an expression.
#[derive(Clone, Debug, PartialEq)]
pub struct EvalError {
    /// The error message.
    pub message: Box<str>,
    /// The location in the source code where the error occurred.
    pub location: Location,
}

impl Display for EvalError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Evaluation error: {}", self.message)
    }
}

/// An error that occurs during evaluation.
///
/// Printing this error will show the error message and the location in the source code.
///
/// ```rust
/// use somni_expr::{Context, TypeSet32};
/// let mut ctx = Context::<TypeSet32>::new_with_types();
///
/// let error = ctx.evaluate::<u32>("true + 1").unwrap_err();
///
/// println!("{error:?}");
///
/// // Output:
/// //
/// // Evaluation error
/// // ---> at line 1 column 1
/// //   |
/// // 1 | true + 1
/// //   | ^^^^^^^^ Failed to evaluate expression: Type error
/// ```
#[derive(Clone, PartialEq)]
pub struct ExpressionError<'s> {
    error: EvalError,
    source: &'s str,
}

impl ExpressionError<'_> {
    /// Returns the inner [`EvalError`].
    pub fn into_inner(self) -> EvalError {
        self.error
    }
}

impl Debug for ExpressionError<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let marked = MarkInSource(
            self.source,
            self.error.location,
            "Evaluation error",
            &self.error.message,
        );
        marked.fmt(f)
    }
}

/// A type in the Somni language.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Type {
    /// Represents no value, used for e.g. functions that do not return a value.
    Void,
    /// Represents integer that may be signed or unsigned.
    MaybeSignedInt,
    /// Represents an unsigned integer.
    Int,
    /// Represents a signed integer.
    SignedInt,
    /// Represents a floating point number.
    Float,
    /// Represents a boolean value.
    Bool,
    /// Represents a string value.
    String,
    /// Represents an iterator handle. The element type is not part of the type;
    /// it is checked at runtime when a value is produced.
    Iter,
    /// Represents a struct value. The struct's identity (name and fields) is not
    /// part of the type; it is carried by the value and checked at runtime.
    Struct,
    /// Represents a reference. The pointee *kind* is static; a struct pointee's
    /// identity is checked at runtime.
    Ref(RefPointee),
}

/// The kind of value a reference points to.
///
/// References are single-level (there is no `&&T`), so a pointee is always a
/// non-reference type. A `Struct` pointee carries no identity — which struct it
/// is gets checked at runtime.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum RefPointee {
    /// A reference to nothing (uncommon, but representable).
    Void,
    /// A reference to an unsigned integer.
    Int,
    /// A reference to a signed integer.
    SignedInt,
    /// A reference to a float.
    Float,
    /// A reference to a boolean.
    Bool,
    /// A reference to a string.
    String,
    /// A reference to an iterator handle.
    Iter,
    /// A reference to a struct (identity checked at runtime).
    Struct,
}

impl RefPointee {
    /// Derives the pointee kind for a reference to a value of the given type.
    ///
    /// Returns `None` for `Type::Ref` (references to references are unsupported).
    /// `MaybeSignedInt` is treated as `Int`.
    pub fn from_type(ty: Type) -> Option<Self> {
        Some(match ty {
            Type::Void => RefPointee::Void,
            Type::Int | Type::MaybeSignedInt => RefPointee::Int,
            Type::SignedInt => RefPointee::SignedInt,
            Type::Float => RefPointee::Float,
            Type::Bool => RefPointee::Bool,
            Type::String => RefPointee::String,
            Type::Iter => RefPointee::Iter,
            Type::Struct => RefPointee::Struct,
            Type::Ref(_) => return None,
        })
    }
}

impl Display for RefPointee {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RefPointee::Void => write!(f, "void"),
            RefPointee::Int => write!(f, "int"),
            RefPointee::SignedInt => write!(f, "signed"),
            RefPointee::Float => write!(f, "float"),
            RefPointee::Bool => write!(f, "bool"),
            RefPointee::String => write!(f, "string"),
            RefPointee::Iter => write!(f, "iter"),
            RefPointee::Struct => write!(f, "struct"),
        }
    }
}

impl Type {
    fn from_name(source: &str) -> Result<Self, Box<str>> {
        match source {
            "int" => Ok(Type::Int),
            "signed" => Ok(Type::SignedInt),
            "float" => Ok(Type::Float),
            "bool" => Ok(Type::Bool),
            "string" => Ok(Type::String),
            "iter" => Ok(Type::Iter),
            other => Err(format!("Unknown type `{other}`").into_boxed_str()),
        }
    }
}

impl Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Type::Void => write!(f, "void"),
            Type::MaybeSignedInt => write!(f, "{{int/signed}}"),
            Type::Int => write!(f, "int"),
            Type::SignedInt => write!(f, "signed"),
            Type::Bool => write!(f, "bool"),
            Type::String => write!(f, "string"),
            Type::Float => write!(f, "float"),
            Type::Iter => write!(f, "iter"),
            Type::Struct => write!(f, "struct"),
            Type::Ref(pointee) => write!(f, "&{pointee}"),
        }
    }
}

/// State of an unevaluated global.
enum InitializerState {
    /// Untouched. Contains the item index of the global
    Unevaluated(usize),
    /// The global is being evaluated. This state is used to detect cycles.
    Evaluating,
}

struct StackFrame<T: TypeSet> {
    start_addr: usize,
    variables: Vec<TypedValue<T>>,
    scopes: Vec<Scope>,
}

struct Scope {
    variable_start: usize,
    variables: HashMap<String, usize>,
}

impl<T: TypeSet> StackFrame<T> {
    fn new() -> StackFrame<T> {
        StackFrame {
            start_addr: 0,
            variables: vec![],
            scopes: vec![Scope {
                variable_start: 0,
                variables: HashMap::new(),
            }],
        }
    }

    fn next_address(&self) -> usize {
        self.start_addr + self.variables.len()
    }

    fn reset(&mut self, start_addr: usize) {
        self.start_addr = start_addr;
        self.variables.clear();
        self.scopes.truncate(1);
        self.scopes[0].variable_start = 0;
        self.scopes[0].variables.clear();
    }

    fn declare(&mut self, variable: &str, value: TypedValue<T>) -> usize {
        let index = self.variables.len();
        self.variables.push(value);
        self.scopes
            .last_mut()
            .unwrap()
            .variables
            .insert(variable.to_string(), index);
        index + self.start_addr
    }

    fn lookup_index(&self, name: &str) -> Option<usize> {
        for scope in self.scopes.iter().rev() {
            if let Some(idx) = scope.variables.get(name) {
                return Some(*idx);
            }
        }
        None
    }

    fn store(&mut self, variable: &str, value: &TypedValue<T>) -> bool {
        if let Some(idx) = self.lookup_index(variable) {
            self.variables.get_mut(idx).unwrap().clone_from(value);
            true
        } else {
            false
        }
    }

    fn lookup_by_address(&mut self, address: usize) -> Result<&mut TypedValue<T>, Box<str>> {
        self.variables
            .get_mut(address - self.start_addr)
            .ok_or_else(|| format!("Invalid address {address}").into_boxed_str())
    }

    fn lookup_by_name<'s>(&'s mut self, variable: &str) -> Option<(usize, &'s mut TypedValue<T>)> {
        let index = self.lookup_index(variable)?;
        let address = index + self.start_addr;

        Some((address, self.variables.get_mut(index).unwrap()))
    }

    fn open_scope(&mut self) {
        self.scopes.push(Scope {
            variable_start: self.variables.len(),
            variables: HashMap::new(),
        });
    }

    fn close_scope(&mut self) {
        let scope = self.scopes.pop().unwrap();
        self.variables.truncate(scope.variable_start);
    }
}

struct ProgramData<'ctx, T: TypeSet> {
    source: &'ctx str,
    program: Program<T::Parser>,
    program_functions: HashMap<&'ctx str, usize>,
    // Struct definitions, indexed by name (item index into `program.items`).
    program_structs: HashMap<&'ctx str, usize>,
    // User-registered functions
    functions: RefCell<HashMap<&'ctx str, ExprFn<'ctx, T>>>,
}

impl<'ctx> Default for Context<'ctx, DefaultTypeSet> {
    fn default() -> Self {
        Self::new()
    }
}

/// The expression context, which holds variables, functions, and other state needed for evaluation.
pub struct Context<'ctx, T = DefaultTypeSet>
where
    T: TypeSet,
{
    program: Rc<ProgramData<'ctx, T>>,
    // Program state
    // ----
    /// Variable stack. Element 0 is the global scope.
    stack: Vec<StackFrame<T>>,
    /// Call frames retained for reuse after functions return.
    frame_pool: Vec<StackFrame<T>>,
    // unevaluated globals
    initializers: HashMap<&'ctx str, InitializerState>,
    type_context: T,
}

impl<'ctx> Context<'ctx, DefaultTypeSet> {
    /// Creates a new context with [default types][DefaultTypeSet].
    pub fn new() -> Self {
        Self::new_with_types()
    }

    /// Loads the given program into a new context with [default types][DefaultTypeSet].
    pub fn parse(source: &'ctx str) -> Result<Self, ExpressionError<'ctx>> {
        Self::parse_with_types(source)
    }
}

const GLOBAL_VARIABLE: usize = usize::MAX - usize::MAX / 2;

impl<'ctx, T> Context<'ctx, T>
where
    T: TypeSet,
{
    /// Creates a new context. The type set must be specified when using this function.
    ///
    /// ```rust
    /// use somni_expr::{Context, TypeSet32};
    /// let mut ctx = Context::<TypeSet32>::new_with_types();
    /// ```
    pub fn new_with_types() -> Self {
        Self::new_from_program("", Program { items: vec![] })
    }

    /// Parses the given program into a new context. The type set must be specified when using this function.
    ///
    /// ```rust
    /// use somni_expr::{Context, TypeSet32};
    /// let mut ctx = Context::<TypeSet32>::parse_with_types("// program source comes here").unwrap();
    /// ```
    pub fn parse_with_types(source: &'ctx str) -> Result<Self, ExpressionError<'ctx>> {
        let program = parse::<T::Parser>(source).map_err(|e| ExpressionError {
            error: EvalError {
                message: format!("Failed to parse program: {e}").into_boxed_str(),
                location: e.location,
            },
            source,
        })?;

        Ok(Self::new_from_program(source, program))
    }

    /// Loads the given program into a new context.
    pub fn new_from_program(source: &'ctx str, program: Program<T::Parser>) -> Self {
        let mut program_functions = HashMap::new();
        let mut program_structs = HashMap::new();
        let mut initializers = HashMap::new();
        // Extract data for O(1) function/initializer/struct lookup
        for (idx, item) in program.items.iter().enumerate() {
            match item {
                ast::Item::Function(function) => {
                    program_functions.insert(function.name.source(source), idx);
                }
                ast::Item::GlobalVariable(global_variable) => {
                    initializers.insert(
                        global_variable.identifier.source(source),
                        InitializerState::Unevaluated(idx),
                    );
                }
                ast::Item::Struct(struct_def) => {
                    program_structs.insert(struct_def.name.source(source), idx);
                }
                ast::Item::ExternFunction(_) => {}
            }
        }
        Self {
            program: Rc::new(ProgramData {
                source,
                program,
                program_functions,
                program_structs,
                functions: RefCell::new(HashMap::new()),
            }),
            stack: vec![StackFrame::new()],
            frame_pool: Vec::new(),
            type_context: T::default(),
            initializers,
        }
    }

    fn evaluate_any_function_impl(
        &mut self,
        function_name: &Function<T::Parser>,
        args: &[TypedValue<T>],
    ) -> Result<TypedValue<T>, EvalError> {
        let source = self.program.clone().source;

        let start_addr = self
            .stack
            .last()
            .expect("The global scope must always be present")
            .next_address();
        let mut stack_frame = self.frame_pool.pop().unwrap_or_else(StackFrame::new);
        stack_frame.reset(start_addr);
        self.stack.push(stack_frame);

        let mut visitor = ExpressionVisitor::<Self, T> {
            context: self,
            source,
            _marker: std::marker::PhantomData,
        };

        let result = visitor.visit_function(function_name, args);

        let stack_frame = self.stack.pop().unwrap();
        self.frame_pool.push(stack_frame);

        result
    }

    /// Parses and evaluates an expression and returns the result as a specific value type.
    ///
    /// This function will attempt to convert the result of the expression to the specified type `V`.
    /// If the conversion fails, it will return an `ExpressionError`.
    ///
    /// ```rust
    /// use somni_expr::{Context, TypedValue};
    ///
    /// let mut context = Context::new();
    ///
    /// assert_eq!(context.evaluate::<u64>("1 + 2"), Ok(3));
    /// assert_eq!(context.evaluate::<TypedValue>("1 + 2"), Ok(TypedValue::Int(3)));
    /// ```
    pub fn evaluate<'s, V>(&'s mut self, source: &'s str) -> Result<V::Output, ExpressionError<'s>>
    where
        V: LoadOwned<T>,
    {
        let expression =
            parser::parse_expression::<T::Parser>(source).map_err(|e| ExpressionError {
                error: EvalError {
                    message: format!("Parser error: {e}").into_boxed_str(),
                    location: e.location,
                },
                source,
            })?;

        self.evaluate_parsed::<V>(source, &expression)
    }

    /// Evaluates a pre-parsed expression and returns the result as a specific value type.
    ///
    /// This function will attempt to convert the result of the expression to the specified type `V`.
    /// If the conversion fails, it will return an `ExpressionError`.
    ///
    /// ```rust
    /// use somni_expr::{Context, TypedValue};
    ///
    /// let mut context = Context::new();
    ///
    /// let source = "1 + 2";
    /// let expr = somni_parser::parser::parse_expression(source).unwrap();
    ///
    /// assert_eq!(context.evaluate_parsed::<u64>(source, &expr), Ok(3));
    /// assert_eq!(context.evaluate_parsed::<TypedValue>(source, &expr), Ok(TypedValue::Int(3)));
    /// ```
    pub fn evaluate_parsed<'s, V>(
        &'s mut self,
        source: &'s str,
        expression: &Expression<T::Parser>,
    ) -> Result<V::Output, ExpressionError<'s>>
    where
        V: LoadOwned<T>,
    {
        self.evaluate_impl::<V>(source, expression)
            .map_err(|error| ExpressionError { error, source })
    }

    fn evaluate_impl<V>(
        &mut self,
        source: &str,
        expression: &Expression<T::Parser>,
    ) -> Result<V::Output, EvalError>
    where
        V: LoadOwned<T>,
    {
        let mut visitor = ExpressionVisitor::<Self, T> {
            context: self,
            source,
            _marker: std::marker::PhantomData,
        };
        let result = visitor.visit_expression(expression)?;
        let result_ty = result.type_of();
        V::load_owned(self.type_context(), &result).ok_or_else(|| EvalError {
            message: format!(
                "Expression evaluates to {result_ty}, which cannot be converted to {}",
                std::any::type_name::<V>()
            )
            .into_boxed_str(),
            location: expression.location(),
        })
    }

    /// Defines a new variable in the context.
    ///
    /// The variable can be any type from the current [`TypeSet`], even [`TypedValue`].
    ///
    /// The variable will act as a global variable in the context of the program. Its
    /// value can be changed by expressions.
    ///
    /// ```rust
    /// use somni_expr::{Context, TypedValue};
    ///
    /// let mut context = Context::new();
    ///
    /// // Variable does not exist, it can't be assigned:
    /// assert!(context.evaluate::<()>("counter = 0").is_err());
    ///
    /// context.add_variable::<u64>("counter", 0);
    ///
    /// // Variable exists now, so we can use it:
    /// assert_eq!(context.evaluate::<()>("counter = counter + 1"), Ok(()));
    /// assert_eq!(context.evaluate::<u64>("counter"), Ok(1));
    /// ```
    pub fn add_variable<V>(&mut self, name: &'ctx str, value: V)
    where
        V: LoadStore<T>,
    {
        let stored = value.store(self.type_context());
        self.stack[0].declare(name, stored);
    }

    /// Adds a new function to the context.
    ///
    /// ```rust
    /// use somni_expr::{Context, TypedValue};
    ///
    /// let mut context = Context::new();
    ///
    /// context.add_function("plus_one", |x: u64| x + 1);
    ///
    /// assert_eq!(context.evaluate::<u64>("plus_one(2)"), Ok(3));
    /// ```
    pub fn add_function<F, A>(&mut self, name: &'ctx str, func: F)
    where
        F: DynFunction<A, T> + 'ctx,
    {
        self.program
            .functions
            .borrow_mut()
            .insert(name, ExprFn::new(func));
    }

    fn lookup(&mut self, variable: &str) -> Option<(usize, TypedValue<T>)> {
        if self.stack.len() > 1 {
            let frame = self.stack.last_mut().unwrap();
            if let Some((index, var)) = frame.lookup_by_name(variable) {
                // Already evaluated / user provided
                return Some((index, var.clone()));
            }
        }

        {
            let global_frame = &mut self.stack[0];
            if let Some((index, var)) = global_frame.lookup_by_name(variable) {
                // Already evaluated / user provided
                return Some((index | GLOBAL_VARIABLE, var.clone()));
            }
        }

        // Mark as "initializing" to detect potential cycles
        let state = self.initializers.get_mut(variable)?;
        let InitializerState::Unevaluated(idx) =
            std::mem::replace(state, InitializerState::Evaluating)
        else {
            return None;
        };

        // Get a reference to the initializer
        let program = self.program.clone();
        let Some(Item::GlobalVariable(global)) = program.program.items.get(idx) else {
            return None;
        };

        let value = self
            .evaluate_parsed::<TypedValue<T>>(self.program.source, &global.initializer)
            .ok()?;

        let global_frame = &mut self.stack[0];
        let index = global_frame.declare(variable, value.clone());

        Some((index | GLOBAL_VARIABLE, value))
    }

    /// Resolves a raw root address to the variable slot it names.
    fn lookup_address_raw(&mut self, address: usize) -> Result<&mut TypedValue<T>, Box<str>> {
        if address & GLOBAL_VARIABLE != 0 {
            return self.stack[0].lookup_by_address(address & !GLOBAL_VARIABLE);
        }

        for frame in self.stack.iter_mut().rev() {
            if frame.start_addr <= address {
                return frame.lookup_by_address(address);
            }
        }

        Err(format!("Not a valid memory address: {address}").into_boxed_str())
    }

    /// Resolves a place to the mutable slot it names, descending its field path.
    fn resolve_place_mut(&mut self, place: &Place) -> Result<&mut TypedValue<T>, Box<str>> {
        let mut current = self.lookup_address_raw(place.root)?;
        for field in place.path.iter() {
            let TypedValue::Struct(structure) = current else {
                return Err(
                    format!("Cannot access field `{field}` of a non-struct value").into_boxed_str(),
                );
            };
            let struct_name = structure.name().to_string();
            current = structure.fields_mut().get_mut(&**field).ok_or_else(|| {
                format!("Struct `{struct_name}` has no field `{field}`").into_boxed_str()
            })?;
        }
        Ok(current)
    }
}

impl<T> ExprContext<T> for Context<'_, T>
where
    T: TypeSet,
{
    fn type_context(&mut self) -> &mut T {
        &mut self.type_context
    }

    // TODO: return Result
    fn try_load_variable(&mut self, variable: &str) -> Option<TypedValue<T>> {
        self.lookup(variable).map(|(_idx, var)| var)
    }

    fn place_of_variable(&mut self, variable: &str) -> Result<Place, Box<str>> {
        let root = self
            .lookup(variable)
            .map(|(address, _var)| address)
            .ok_or_else(|| format!("Variable not found: {variable}").into_boxed_str())?;
        Ok(Place {
            root,
            path: Box::new([]),
        })
    }

    fn load_place(&mut self, place: &Place) -> Result<TypedValue<T>, Box<str>> {
        self.resolve_place_mut(place).map(|v| v.clone())
    }

    fn store_place(&mut self, place: &Place, value: &TypedValue<T>) -> Result<(), Box<str>> {
        let slot = self.resolve_place_mut(place)?;
        slot.clone_from(value);
        Ok(())
    }

    fn struct_fields(&self, struct_name: &str) -> Option<Vec<(Box<str>, Box<str>)>> {
        let idx = *self.program.program_structs.get(struct_name)?;
        let Some(Item::Struct(struct_def)) = self.program.program.items.get(idx) else {
            return None;
        };
        let source = self.program.source;
        Some(
            struct_def
                .fields
                .iter()
                .map(|field| {
                    (
                        Box::from(field.name.source(source)),
                        Box::from(field.field_type.type_name.source(source)),
                    )
                })
                .collect(),
        )
    }

    /// Declares a variable in the context.
    fn declare(&mut self, variable: &str, value: TypedValue<T>) {
        self.stack.last_mut().unwrap().declare(variable, value);
    }

    /// Assigns a new value to a variable in the context.
    fn assign_variable(&mut self, variable: &str, value: &TypedValue<T>) -> Result<(), Box<str>> {
        if self.stack.last_mut().unwrap().store(variable, value) {
            return Ok(());
        }
        if self.stack[0].store(variable, value) {
            return Ok(());
        }

        Err(format!("Variable not found: {variable}").into_boxed_str())
    }

    fn call_function(
        &mut self,
        function_name: &str,
        args: &[TypedValue<T>],
    ) -> Result<TypedValue<T>, FunctionCallError> {
        let program = self.program.clone();
        let Some(fn_item) = self.program.program_functions.get(function_name) else {
            // Call out to a Rust function
            return match program.functions.borrow().get(function_name) {
                Some(func) => func.call(self.type_context(), args),
                None => Err(FunctionCallError::FunctionNotFound),
            };
        };

        // Call a Somni function
        let Some(ast::Item::Function(function)) = program.program.items.get(*fn_item) else {
            return Err(FunctionCallError::FunctionNotFound);
        };
        self.evaluate_any_function_impl(function, args)
            .map_err(|err| {
                FunctionCallError::Other(
                    format!(
                        "{:?}",
                        ExpressionError {
                            source: self.program.source,
                            error: err,
                        }
                    )
                    .into_boxed_str(),
                )
            })
    }

    /// Opens a new scope in the current stack frame.
    fn open_scope(&mut self) {
        // TODO: error handling
        self.stack.last_mut().unwrap().open_scope();
    }

    /// Closes the last scope in the current stack frame.
    fn close_scope(&mut self) {
        // TODO: error handling
        self.stack.last_mut().unwrap().close_scope();
    }
}

#[macro_export]
#[doc(hidden)]
macro_rules! for_all_tuples {
    ($pat:tt => $code:tt;) => {
        macro_rules! inner { $pat => $code; }

        inner!();
        inner!(V1);
        inner!(V1, V2);
        inner!(V1, V2, V3);
        inner!(V1, V2, V3, V4);
        inner!(V1, V2, V3, V4, V5);
        inner!(V1, V2, V3, V4, V5, V6);
        inner!(V1, V2, V3, V4, V5, V6, V7);
        inner!(V1, V2, V3, V4, V5, V6, V7, V8);
        inner!(V1, V2, V3, V4, V5, V6, V7, V8, V9);
        inner!(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10);
    };
}

#[cfg(test)]
mod test {
    use std::path::Path;

    use super::*;

    fn strip_ansi(s: impl AsRef<str>) -> String {
        use ansi_parser::AnsiParser;
        fn text_block(output: ansi_parser::Output<'_>) -> Option<&str> {
            match output {
                ansi_parser::Output::TextBlock(text) => Some(text),
                _ => None,
            }
        }

        s.as_ref()
            .ansi_parse()
            .filter_map(text_block)
            .collect::<String>()
    }

    #[test]
    fn test_evaluating_exprs() {
        let mut ctx = Context::new();

        ctx.add_variable::<i64>("signed", 30);
        ctx.add_variable::<u64>("value", 30);
        ctx.add_function("func", |v: u64| 2 * v);
        ctx.add_function("func2", |v1: u64, v2: u64| v1 + v2);
        ctx.add_function("five", || "five");
        ctx.add_function("is_five", |num: &str| num == "five");
        ctx.add_function("concatenate", |a: &str, b: &str| format!("{a}{b}"));

        assert_eq!(ctx.evaluate::<bool>("value / 5 == 6"), Ok(true));
        assert_eq!(ctx.evaluate::<bool>("five() == \"five\""), Ok(true));
        assert_eq!(
            ctx.evaluate::<bool>("is_five(five()) != is_five(\"six\")"),
            Ok(true)
        );
        assert_eq!(ctx.evaluate::<u64>("func(20) / 5"), Ok(8));
        assert_eq!(
            ctx.evaluate::<TypedValue>("func(20) / 5"),
            Ok(TypedValue::Int(8))
        );
        assert_eq!(ctx.evaluate::<u64>("func2(20, 20) / 5"), Ok(8));
        assert_eq!(ctx.evaluate::<bool>("true & false"), Ok(false));
        assert_eq!(ctx.evaluate::<bool>("!true"), Ok(false));
        assert_eq!(ctx.evaluate::<bool>("false | false"), Ok(false));
        assert_eq!(ctx.evaluate::<bool>("true ^ true"), Ok(false));
        assert_eq!(ctx.evaluate::<u64>("!0x1111"), Ok(0xFFFF_FFFF_FFFF_EEEE));
        assert_eq!(
            ctx.evaluate::<String>("concatenate(five(), \"six\")"),
            Ok(String::from("fivesix"))
        );
        assert_eq!(ctx.evaluate::<bool>("signed * 2 == 60"), Ok(true));
        assert_eq!(ctx.evaluate::<i64>("*&signed"), Ok(30));
    }

    #[test]
    fn test_context_is_mutable() {
        let mut ctx = Context::new();

        ctx.add_variable::<u64>("value", 30);

        ctx.evaluate::<()>("value = 5").unwrap();
        assert_eq!(ctx.evaluate::<bool>("value == 5"), Ok(true));
    }

    #[test]
    fn closing_scope_releases_its_variables() {
        let mut frame = StackFrame::<DefaultTypeSet>::new();
        frame.declare("outer", TypedValue::Int(1));
        frame.open_scope();
        frame.declare("inner", TypedValue::Int(2));

        frame.close_scope();

        assert_eq!(frame.variables.len(), 1);
        assert_eq!(frame.lookup_index("outer"), Some(0));
        assert_eq!(frame.lookup_index("inner"), None);
    }

    #[test]
    fn test_evaluating_exprs_with_u32() {
        let mut ctx = Context::<TypeSet32>::new_with_types();

        ctx.add_variable::<u32>("value", 30);
        ctx.add_function("func", |v: u32| 2 * v);
        ctx.add_function("func2", |v1: u32, v2: u32| v1 + v2);

        assert_eq!(ctx.evaluate::<bool>("value / 5 == 6"), Ok(true));
        assert_eq!(ctx.evaluate::<u32>("func(20) / 5"), Ok(8));
        assert_eq!(ctx.evaluate::<u32>("func2(20, 20) / 5"), Ok(8));
    }

    #[test]
    fn test_evaluating_exprs_with_u128() {
        let mut ctx = Context::<TypeSet128>::new_with_types();

        ctx.add_variable::<u128>("value", 30);
        ctx.add_function("func", |v: u128| 2 * v);
        ctx.add_function("func2", |v1: u128, v2: u128| v1 + v2);

        assert_eq!(ctx.evaluate::<bool>("value / 5 == 6"), Ok(true));
        assert_eq!(ctx.evaluate::<u128>("func(20) / 5"), Ok(8));
        assert_eq!(ctx.evaluate::<u128>("func2(20, 20) / 5"), Ok(8));
    }

    #[test]
    fn test_evaluate_function() {
        let mut ctx =
            Context::parse("fn multiply_with_global(a: int) -> int { return a * global; }")
                .unwrap();

        ctx.add_variable::<u64>("global", 3);

        assert_eq!(
            ctx.evaluate::<bool>("multiply_with_global(2) == 6"),
            Ok(true)
        );
        assert!(
            ctx.evaluate::<bool>("multiply_with_global(\"2\") == 6")
                .is_err()
        );
    }

    #[test]
    fn run_eval_tests() {
        fn filter(path: &Path) -> bool {
            let Ok(env) = std::env::var("TEST_FILTER") else {
                // No filter set, walk folders and somni source files.
                return path.is_dir() || path.extension().map_or(false, |ext| ext == "sm");
            };

            Path::new(&env) == path
        }

        fn walk(dir: &Path, on_file: &impl Fn(&Path)) {
            for entry in std::fs::read_dir(dir)
                .unwrap_or_else(|_| panic!("Folder not found: {}", dir.display()))
                .flatten()
            {
                let path = entry.path();

                if !filter(&path) {
                    continue;
                }

                if path.is_file() {
                    on_file(&path);
                } else {
                    walk(&path, on_file);
                }
            }
        }

        fn run_eval_test(path: &Path) {
            type Types = WithIterator<DefaultTypeSet>;

            fn parse(source: &str) -> Context<'_, Types> {
                let mut context = Context::<Types>::parse_with_types(source).unwrap();

                context.add_function("add_from_rust", |a: u64, b: u64| -> i64 { (a + b) as i64 });
                context.add_function("assert", |a: bool| a); // No-op to test calling Rust functions from expressions
                context.add_function("reverse", |s: &str| s.chars().rev().collect::<String>());
                context.add_function("range", |a: u64, b: u64| {
                    SomniIterator::new((a..b).map(TypedValue::<DefaultTypeSet>::Int))
                });

                context
            }

            let test_name = path.file_stem().unwrap();
            let parent = path.parent().unwrap().canonicalize().unwrap();
            let vm_error = parent.join(test_name).join("stderr");
            let expr_error = parent.join(test_name).join("stderr_expr");
            let source = std::fs::read_to_string(path).unwrap();

            let expressions = source
                .lines()
                .filter_map(|line| line.trim().strip_prefix("//@"))
                .collect::<Vec<_>>();

            let mut context = parse(&source);
            let fail_expected = std::fs::exists(&expr_error).unwrap_or(false)
                || std::fs::exists(&vm_error).unwrap_or(false);

            let blessed = std::env::var("BLESS").as_deref() == Ok("1");

            for expression in &expressions {
                let expression = if let Some(e) = expression.strip_prefix('+') {
                    // `//@+` preserves VM state (like changes to globals)
                    e.trim()
                } else {
                    // `//@` resets VM state (like changes to globals)
                    context = parse(&source);
                    expression
                };
                println!("Running `{expression}`");
                match context.evaluate::<TypedValue<Types>>(expression) {
                    Ok(_) if fail_expected => {
                        panic!(
                            "Expected {} to fail evaluating, but it succeeded",
                            path.display()
                        )
                    }
                    Ok(value) => assert_eq!(
                        value,
                        TypedValue::Bool(true),
                        "{}: Expression `{expression}` evaluated to {value:?}",
                        path.display()
                    ),
                    Err(e) if fail_expected => {
                        let error = strip_ansi(format!("{e:?}"));
                        if blessed {
                            std::fs::write(&expr_error, error).unwrap();
                        } else {
                            let expected_error = std::fs::read_to_string(&expr_error).unwrap();
                            pretty_assertions::assert_eq!(strip_ansi(expected_error), error);
                        }
                    }
                    Err(e) => panic!("{}: {e:?}", path.display()),
                };
            }
        }

        walk("../tests/eval".as_ref(), &|path| {
            run_eval_test(path);
        });
    }

    #[test]
    fn test_struct_literals_and_field_access() {
        let program = r#"
struct Point { x: int, y: int }

fn make() -> Point {
    return Point { x: 3, y: 4 };
}

fn sum_sq() -> int {
    var p = make();
    return p.x * p.x + p.y * p.y;
}
"#;
        let mut ctx = Context::parse(program).unwrap();
        assert_eq!(ctx.evaluate::<bool>("sum_sq() == 25"), Ok(true));
    }

    #[test]
    fn test_struct_field_write() {
        let program = r#"
struct Point { x: int, y: int }

fn moved() -> int {
    var p = Point { x: 1, y: 2 };
    p.x = 10;
    p.y = p.y + 5;
    return p.x + p.y;
}
"#;
        let mut ctx = Context::parse(program).unwrap();
        assert_eq!(ctx.evaluate::<bool>("moved() == 17"), Ok(true));
    }

    #[test]
    fn test_nested_struct() {
        let program = r#"
struct Point { x: int, y: int }
struct Line { start: Point, end: Point }

fn build() -> int {
    var l = Line { start: Point { x: 1, y: 2 }, end: Point { x: 3, y: 4 } };
    l.end.x = 30;
    return l.start.x + l.end.x;
}
"#;
        let mut ctx = Context::parse(program).unwrap();
        assert_eq!(ctx.evaluate::<bool>("build() == 31"), Ok(true));
    }

    #[test]
    fn test_struct_pass_by_reference_and_autoderef() {
        let program = r#"
struct Point { x: int, y: int }

fn scale(p: &Point, factor: int) {
    p.x = p.x * factor;
    p.y = p.y * factor;
}

fn run() -> int {
    var p = Point { x: 2, y: 3 };
    scale(&p, 4);
    return p.x + p.y;
}
"#;
        let mut ctx = Context::parse(program).unwrap();
        assert_eq!(ctx.evaluate::<bool>("run() == 20"), Ok(true));
    }

    #[test]
    fn test_reference_to_field() {
        let program = r#"
struct Point { x: int, y: int }

fn double(v: &int) {
    *v = *v * 2;
}

fn run() -> int {
    var p = Point { x: 5, y: 6 };
    double(&p.x);
    return p.x;
}
"#;
        let mut ctx = Context::parse(program).unwrap();
        assert_eq!(ctx.evaluate::<bool>("run() == 10"), Ok(true));
    }

    #[test]
    fn test_struct_equality() {
        let program = r#"
struct Point { x: int, y: int }

fn a() -> Point { return Point { x: 1, y: 2 }; }
fn b() -> Point { return Point { x: 1, y: 2 }; }
fn c() -> Point { return Point { x: 1, y: 9 }; }
"#;
        let mut ctx = Context::parse(program).unwrap();
        assert_eq!(ctx.evaluate::<bool>("a() == b()"), Ok(true));
        assert_eq!(ctx.evaluate::<bool>("a() != c()"), Ok(true));
        assert_eq!(ctx.evaluate::<bool>("a() == c()"), Ok(false));
    }

    #[test]
    fn test_struct_boundary_and_macro() {
        let program = r#"
struct Point { x: int, y: int }
"#;
        let mut ctx = Context::parse(program).unwrap();
        ctx.add_function("origin_distance_sq", |p: SomniStruct| -> i64 {
            let TypedValue::Int(x) = p.fields()["x"] else {
                panic!("x not an int")
            };
            let TypedValue::Int(y) = p.fields()["y"] else {
                panic!("y not an int")
            };
            (x * x + y * y) as i64
        });

        // Build a struct from Rust and hand it to the program.
        let tc = ctx.type_context();
        let point = somni_struct!(tc, Point { x: 3u64, y: 4u64 });
        assert_eq!(point.name(), "Point");

        assert_eq!(
            ctx.evaluate::<bool>("origin_distance_sq(Point { x: 3, y: 4 }) == 25"),
            Ok(true)
        );
    }

    #[test]
    fn test_unknown_struct_is_error() {
        let mut ctx = Context::new();
        assert!(ctx.evaluate::<TypedValue>("Nope { x: 1 }").is_err());
    }

    #[test]
    fn test_eval_error() {
        let mut ctx = Context::new();

        ctx.add_function("func", |v1: u64, v2: u64| v1 + v2);

        let err = ctx
            .evaluate::<u64>("func(20, true)")
            .expect_err("Expected expression to return an error");

        pretty_assertions::assert_eq!(
            strip_ansi(format!("\n{err:?}")),
            r#"
Evaluation error
 ---> at line 1 column 10
  |
1 | func(20, true)
  |          ^^^^ func expects argument 1 to be u64, got bool"#,
        );
    }
}