mech-core 0.3.4

The Mech language runtime.
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
use crate::*;
use super::*;
#[cfg(feature = "matrix")]
use crate::structures::Matrix;

// CompileConst Trait
// ----------------------------------------------------------------------------

pub trait CompileConst {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32>;
}

#[cfg(feature = "compiler")]
impl CompileConst for Value {

  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let reg = match self {
      #[cfg(feature = "bool")]
      Value::Bool(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "string")]
      Value::String(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "u8")]
      Value::U8(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "u16")]
      Value::U16(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "u32")]
      Value::U32(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "u64")]
      Value::U64(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "u128")]
      Value::U128(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "i8")]
      Value::I8(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "i16")]
      Value::I16(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "i32")]
      Value::I32(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "i64")]
      Value::I64(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "i128")]
      Value::I128(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "f32")]
      Value::F32(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "f64")]
      Value::F64(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "atom")]
      Value::Atom(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "index")]
      Value::Index(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "complex")]
      Value::C64(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "rational")]
      Value::R64(x) => x.borrow().compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "f64"))]
      Value::MatrixF64(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "f32"))]
      Value::MatrixF32(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "u8"))]
      Value::MatrixU8(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "u16"))]
      Value::MatrixU16(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "u32"))]
      Value::MatrixU32(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "u64"))]
      Value::MatrixU64(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "u128"))]
      Value::MatrixU128(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "i8"))]
      Value::MatrixI8(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "i16"))]
      Value::MatrixI16(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "i32"))]
      Value::MatrixI32(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "i64"))]
      Value::MatrixI64(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "i128"))]
      Value::MatrixI128(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "bool"))]
      Value::MatrixBool(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "rational"))]
      Value::MatrixR64(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "complex"))]
      Value::MatrixC64(x) => x.compile_const(ctx)?,
      #[cfg(all(feature = "matrix", feature = "string"))]
      Value::MatrixString(x) => x.compile_const(ctx)?,
      #[cfg(feature = "matrix")]
      Value::MatrixIndex(x) => x.compile_const(ctx)?,
      #[cfg(feature = "matrix")]
      Value::MatrixValue(x) => x.compile_const(ctx)?,
      #[cfg(feature = "table")]
      Value::Table(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "record")]
      Value::Record(x) => x.borrow().compile_const(ctx)?,
      #[cfg(feature = "set")]
      Value::Set(x) => x.borrow().compile_const(ctx)?,
      Value::Typed(value, kind) => match value.as_ref() {
        Value::Empty => ctx.compile_const(&[], kind.clone())?,
        _ => value.compile_const(ctx)?,
      },
      Value::EmptyKind(k) => ctx.compile_const(&[], k.clone())?,
      Value::Empty => ctx.compile_const(&[], ValueKind::Empty)?,
      x => todo!("CompileConst not implemented for {:?}", x),
    };
    Ok(reg)
  }
}

#[cfg(all(feature = "f64", feature = "compiler"))]
impl CompileConst for f64 {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_f64::<LittleEndian>(*self)?;
    ctx.compile_const(&payload, ValueKind::F64)
  }
}

#[cfg(feature = "f32")]
impl CompileConst for f32 {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_f32::<LittleEndian>(*self)?;
    ctx.compile_const(&payload, ValueKind::F32)
  }
}

#[cfg(feature = "u8")]
impl CompileConst for u8 {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_u8(*self)?;
    ctx.compile_const(&payload, ValueKind::U8)
  }
}

#[cfg(feature = "i8")]
impl CompileConst for i8 {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_i8(*self)?;
    ctx.compile_const(&payload, ValueKind::I8)
  }
}

#[cfg(feature = "compiler")]
impl CompileConst for usize {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_u64::<LittleEndian>(*self as u64)?;
    ctx.compile_const(&payload, ValueKind::Index)
  }
}

macro_rules! impl_compile_const {
  ($feature:literal, $t:tt) => {
    paste! {
      #[cfg(feature = $feature)]
      impl CompileConst for $t {
        fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
          let mut payload = Vec::<u8>::new();
          payload.[<write_ $t>]::<LittleEndian>(*self)?;
          ctx.compile_const(&payload, ValueKind::[<$t:upper>])
        }
      }
    }
  };
}

#[cfg(feature = "u16")]
impl_compile_const!("u16", u16);
#[cfg(feature = "u32")]
impl_compile_const!("u32", u32);
#[cfg(feature = "u64")]
impl_compile_const!("u64", u64);
#[cfg(feature = "u128")]
impl_compile_const!("u128", u128);
#[cfg(feature = "i16")]
impl_compile_const!("i16", i16);
#[cfg(feature = "i32")]
impl_compile_const!("i32", i32);
#[cfg(feature = "i64")]
impl_compile_const!("i64", i64);
#[cfg(feature = "i128")]
impl_compile_const!("i128", i128);

#[cfg(any(feature = "bool", feature = "variable_define"))]
impl CompileConst for bool {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_u8(if *self { 1 } else { 0 })?;
    ctx.compile_const(&payload, ValueKind::Bool)
  }
}

#[cfg(any(feature = "string", feature = "variable_define"))]
impl CompileConst for String {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_u32::<LittleEndian>(self.len() as u32)?;
    payload.extend_from_slice(self.as_bytes());
    ctx.compile_const(&payload, ValueKind::String)
  }
}

#[cfg(feature = "rational")]
impl CompileConst for R64 {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_i64::<LittleEndian>(*self.numer())?;
    payload.write_i64::<LittleEndian>(*self.denom())?;
    ctx.compile_const(&payload, ValueKind::R64)
  }
}

#[cfg(feature = "complex")]
impl CompileConst for C64 {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_f64::<LittleEndian>(self.0.re)?;
    payload.write_f64::<LittleEndian>(self.0.im)?;
    ctx.compile_const(&payload, ValueKind::C64)
  }
}

macro_rules! impl_compile_const_matrix {
  ($matrix_type:ty) => {
    impl<T> CompileConst for $matrix_type
    where
      T: ConstElem + AsValueKind,
    {
      fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
        let rows = self.nrows() as u32;
        let cols = self.ncols() as u32;
        let mut payload = Vec::<u8>::with_capacity((rows * cols) as usize * 8);

        // write header: rows, cols
        payload.write_u32::<LittleEndian>(rows)?;
        payload.write_u32::<LittleEndian>(cols)?;

        // write elements column-major
        for c in 0..cols as usize {
          for r in 0..rows as usize {
            self[(r, c)].write_le(&mut payload);
          }
        }
        let elem_vk = T::as_value_kind();
        let mat_vk = ValueKind::Matrix(Box::new(elem_vk), vec![rows as usize, cols as usize]);
        ctx.compile_const(&payload, mat_vk)
      }
    }
  };
}

#[cfg(feature = "matrix1")]
impl_compile_const_matrix!(na::Matrix1<T>);
#[cfg(feature = "matrix2")]
impl_compile_const_matrix!(na::Matrix2<T>);
#[cfg(feature = "matrix3")]
impl_compile_const_matrix!(na::Matrix3<T>);
#[cfg(feature = "matrix4")]
impl_compile_const_matrix!(na::Matrix4<T>);
#[cfg(feature = "matrix2x3")]
impl_compile_const_matrix!(na::Matrix2x3<T>);
#[cfg(feature = "matrix3x2")]
impl_compile_const_matrix!(na::Matrix3x2<T>);
#[cfg(feature = "row_vector2")]
impl_compile_const_matrix!(na::RowVector2<T>);
#[cfg(feature = "row_vector3")]
impl_compile_const_matrix!(na::RowVector3<T>);
#[cfg(feature = "row_vector4")]
impl_compile_const_matrix!(na::RowVector4<T>);
#[cfg(feature = "vector2")]
impl_compile_const_matrix!(na::Vector2<T>);
#[cfg(feature = "vector3")]
impl_compile_const_matrix!(na::Vector3<T>);
#[cfg(feature = "vector4")]
impl_compile_const_matrix!(na::Vector4<T>);
#[cfg(feature = "matrixd")]
impl_compile_const_matrix!(na::DMatrix<T>);
#[cfg(feature = "vectord")]
impl_compile_const_matrix!(na::DVector<T>);
#[cfg(feature = "row_vectord")]
impl_compile_const_matrix!(na::RowDVector<T>);

#[cfg(feature = "matrix")]
impl<T> CompileConst for Matrix<T> 
where
  T: CompileConst + ConstElem + AsValueKind
{
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    match self {
      #[cfg(feature = "matrixd")]
      Matrix::DMatrix(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "vectord")]
      Matrix::DVector(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "row_vectord")]
      Matrix::RowDVector(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "matrix1")]
      Matrix::Matrix1(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "matrix2")]
      Matrix::Matrix2(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "matrix3")]
      Matrix::Matrix3(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "matrix4")]
      Matrix::Matrix4(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "matrix2x3")]
      Matrix::Matrix2x3(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "matrix3x2")]
      Matrix::Matrix3x2(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "row_vector2")]
      Matrix::RowVector2(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "row_vector3")]
      Matrix::RowVector3(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "row_vector4")]
      Matrix::RowVector4(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "vector2")]
      Matrix::Vector2(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "vector3")]
      Matrix::Vector3(mat) => mat.borrow().compile_const(ctx),
      #[cfg(feature = "vector4")]
      Matrix::Vector4(mat) => mat.borrow().compile_const(ctx),
    }
  }
}

#[cfg(feature = "matrixd")]
impl<T> CompileConst for Ref<DMatrix<T>> 
where
  T: CompileConst + ConstElem + AsValueKind
{
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    self.borrow().compile_const(ctx)
  }
}

#[cfg(feature = "vectord")]
impl<T> CompileConst for Ref<DVector<T>> 
where
  T: CompileConst + ConstElem + AsValueKind
{
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    self.borrow().compile_const(ctx)
  }
}

#[cfg(feature = "row_vectord")]
impl<T> CompileConst for Ref<RowDVector<T>> 
where
  T: CompileConst + ConstElem + AsValueKind
{
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    self.borrow().compile_const(ctx)
  }
}

#[cfg(feature = "record")]
impl CompileConst for MechRecord {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();

    // write the number of columns
    payload.write_u32::<LittleEndian>(self.cols as u32)?;

    // write each column: (name hash, value kind, data)
    for (col_id, value) in self.data.iter() {
      // column name hash
      payload.write_u64::<LittleEndian>(*col_id)?;
      // value kind
      let value_kind = value.kind();
      value_kind.write_le(&mut payload);
      // value data
      value.write_le(&mut payload);
    }

    // Write the field name strings into the payload
    for (_col_id, col_name) in self.field_names.iter() {
      col_name.write_le(&mut payload);
    }
    ctx.compile_const(&payload, self.kind())
  }
}

#[cfg(feature = "enum")]
impl CompileConst for MechEnum {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_u64::<LittleEndian>(self.id)?;
    payload.write_u32::<LittleEndian>(self.variants.len() as u32)?;
    for (variant_id, variant_value) in self.variants.iter() {
      payload.write_u64::<LittleEndian>(*variant_id)?;
      match variant_value {
        Some(v) => {
          // has value
          payload.write_u8(1)?;
          // value kind
          let value_kind = v.kind();
          value_kind.write_le(&mut payload);
          // value data
          v.write_le(&mut payload);
        },
        None => {
          // has no value
          payload.write_u8(0)?;
        }
      }
    }
    ctx.compile_const(&payload, ValueKind::Enum(self.id, self.name()))
  }
}

#[cfg(feature = "atom")]
impl CompileConst for MechAtom {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    payload.write_u64::<LittleEndian>(self.id())?;
    self.name().write_le(&mut payload);
    ctx.compile_const(&payload, ValueKind::Atom(self.id(), self.name().clone()))
  }
}

#[cfg(feature = "set")]
impl CompileConst for MechSet {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    self.kind.write_le(&mut payload);
    payload.write_u32::<LittleEndian>(self.num_elements as u32)?;
    for element in &self.set {
      element.write_le(&mut payload);
    }
    ctx.compile_const(&payload, self.kind())
  }
}

#[cfg(feature = "tuple")]
impl CompileConst for MechTuple {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    self.value_kind().write_le(&mut payload);
    payload.write_u32::<LittleEndian>(self.elements.len() as u32)?;
    for elem in &self.elements {
      elem.write_le(&mut payload);
    }
    ctx.compile_const(&payload, self.value_kind())
  }
}

#[cfg(feature = "map")]
impl CompileConst for MechMap {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    self.key_kind.write_le(&mut payload);
    self.value_kind.write_le(&mut payload);
    payload.write_u32::<LittleEndian>(self.map.len() as u32)?;
    for (key, value) in &self.map {
      key.write_le(&mut payload);
      value.write_le(&mut payload);
    }
    let map_vk = ValueKind::Map(Box::new(self.key_kind.clone()), Box::new(self.value_kind.clone()));
    ctx.compile_const(&payload, map_vk)
  }
}

// ConstElem Trait
// ----------------------------------------------------------------------------

pub trait ConstElem {
  fn write_le(&self, out: &mut Vec<u8>);
  fn from_le(bytes: &[u8]) -> Self;
  fn value_kind(&self) -> ValueKind;
  fn align() -> u8 { 1 }
}

macro_rules! impl_const_elem {
  ($feature:literal, $t:ty, $align:expr) => {
    paste!{
      #[cfg(feature = $feature)]
      impl ConstElem for $t {
        fn write_le(&self, out: &mut Vec<u8>) {
          out.[<write_ $t>]::<LittleEndian>(*self).expect(concat!("write ", stringify!($t)));
        }
        fn from_le(bytes: &[u8]) -> Self {
          let mut rdr = std::io::Cursor::new(bytes);
          rdr.[<read_ $t>]::<LittleEndian>().expect(concat!("read ", stringify!($t)))
        }
        fn value_kind(&self) -> ValueKind { ValueKind::[<$t:upper>] }
        fn align() -> u8 { $align }
      }
    }
  };
}

#[cfg(feature = "u16")]
impl_const_elem!("u16", u16, 2);
#[cfg(feature = "u32")]
impl_const_elem!("u32", u32, 4);
#[cfg(feature = "u64")]
impl_const_elem!("u64", u64, 8);
#[cfg(feature = "u128")]
impl_const_elem!("u128", u128, 16);
#[cfg(feature = "i16")]
impl_const_elem!("i16", i16, 2);
#[cfg(feature = "i32")]
impl_const_elem!("i32", i32, 4);
#[cfg(feature = "i64")]
impl_const_elem!("i64", i64, 8);
#[cfg(feature = "i128")]
impl_const_elem!("i128", i128, 16);
#[cfg(feature = "f32")]
impl_const_elem!("f32", f32, 4);
#[cfg(feature = "f64")]
impl_const_elem!("f64", f64, 8);

#[cfg(feature = "u8")]
impl ConstElem for u8 {
  fn write_le(&self, out: &mut Vec<u8>) {
    out.write_u8(*self).expect("write u8");
  }
  fn from_le(bytes: &[u8]) -> Self {
    bytes[0]
  }
  fn value_kind(&self) -> ValueKind { ValueKind::U8 }
  fn align() -> u8 { 1 }
} 

#[cfg(feature = "i8")]
impl ConstElem for i8 {
  fn write_le(&self, out: &mut Vec<u8>) {
    out.write_i8(*self).expect("write i8");
  }
  fn from_le(bytes: &[u8]) -> Self {
    bytes[0] as i8
  }
  fn value_kind(&self) -> ValueKind { ValueKind::I8 }
  fn align() -> u8 { 1 }
}

#[cfg(feature = "rational")]
impl ConstElem for R64 {
  fn write_le(&self, out: &mut Vec<u8>) {
    out.write_i64::<LittleEndian>(*self.numer()).expect("write rational numer");
    out.write_i64::<LittleEndian>(*self.denom()).expect("write rational denom");
  }
  fn from_le(bytes: &[u8]) -> Self {
    let numer = match bytes[0..8].try_into() {
      Ok(arr) => i64::from_le_bytes(arr),
      Err(_) => panic!("Failed to read numerator from bytes"),
    };
    let denom = match bytes[8..16].try_into() {
      Ok(arr) => i64::from_le_bytes(arr),
      Err(_) => panic!("Failed to read denominator from bytes"),
    };
    if denom == 0 {
      panic!("Denominator cannot be zero");
    }
    R64::new(numer, denom)
  }
  fn value_kind(&self) -> ValueKind { ValueKind::R64 }
  fn align() -> u8 { 16 }
}

#[cfg(feature = "complex")]
impl ConstElem for C64 {
  fn write_le(&self, out: &mut Vec<u8>) {
    out.write_f64::<LittleEndian>(self.0.re).expect("write complex real");
    out.write_f64::<LittleEndian>(self.0.im).expect("write complex imag");
  }
  fn from_le(bytes: &[u8]) -> Self {
    let real = match bytes[0..8].try_into() {
      Ok(arr) => f64::from_le_bytes(arr),
      Err(_) => panic!("Failed to read real part from bytes"),
    };
    let imag = match bytes[8..16].try_into() {
      Ok(arr) => f64::from_le_bytes(arr),
      Err(_) => panic!("Failed to read imaginary part from bytes"),
    };
    C64::new(real, imag)
  }
  fn value_kind(&self) -> ValueKind { ValueKind::C64 }
  fn align() -> u8 { 16 }
}

#[cfg(feature = "string")]
impl ConstElem for String {
  fn write_le(&self, out: &mut Vec<u8>) {
    use byteorder::{LittleEndian, WriteBytesExt};
    out.write_u32::<LittleEndian>(self.len() as u32).expect("write string length");
    out.extend_from_slice(self.as_bytes());
  }
  fn from_le(bytes: &[u8]) -> Self {
    use byteorder::{LittleEndian, ReadBytesExt};
    use std::io::Cursor;
    let mut cursor = Cursor::new(bytes);
    // read length safely
    let len = match cursor.read_u32::<LittleEndian>() {
      Ok(n) => n as usize,
      Err(_) => panic!("Failed to read string length from bytes"),
    };
    let start = cursor.position() as usize;
    let end = start + len;
    if end > bytes.len() {
      panic!(
        "String::from_le: declared length {} exceeds available bytes ({})",
        len, bytes.len()
      );
    }
    let str_bytes = &bytes[start..end];
    match std::str::from_utf8(str_bytes) {
      Ok(s) => s.to_string(),
      Err(_) => panic!("Failed to convert bytes to UTF-8 string"),
    }
  }
  fn value_kind(&self) -> ValueKind { ValueKind::String }
  fn align() -> u8 { 1 }
}

#[cfg(feature = "bool")]
impl ConstElem for bool {
  fn write_le(&self, out: &mut Vec<u8>) {
    out.write_u8(if *self { 1 } else { 0 }).expect("write bool");
  }
  fn from_le(bytes: &[u8]) -> Self {
    bytes[0] != 0
  }
  fn value_kind(&self) -> ValueKind { ValueKind::Bool }
  fn align() -> u8 { 1 }
}

impl ConstElem for usize {
  fn write_le(&self, out: &mut Vec<u8>) {
    out.write_u64::<LittleEndian>(*self as u64).expect("write usize");
  }
  fn from_le(bytes: &[u8]) -> Self {
    let val = match bytes[0..8].try_into() {
      Ok(arr) => u64::from_le_bytes(arr),
      Err(_) => panic!("Failed to read usize from bytes"),
    };
    val as usize
  }
  fn value_kind(&self) -> ValueKind { ValueKind::Index }
  fn align() -> u8 { 8 }
}

macro_rules! impl_const_elem_matrix {
  ($matrix_type:ty) => {
    impl<T> ConstElem for $matrix_type
    where
      T: ConstElem + std::fmt::Debug + std::clone::Clone + PartialEq + 'static,
    {
      fn write_le(&self, out: &mut Vec<u8>) {
        out.write_u32::<LittleEndian>(self.nrows() as u32).unwrap();
        out.write_u32::<LittleEndian>(self.ncols() as u32).unwrap();
        for c in 0..self.ncols() {
          for r in 0..self.nrows() {
            self[(r, c)].write_le(out);
          }
        }
      }
      fn from_le(bytes: &[u8]) -> Self {
        let mut cursor = Cursor::new(bytes);
        let rows = cursor.read_u32::<LittleEndian>().unwrap() as usize;
        let cols = cursor.read_u32::<LittleEndian>().unwrap() as usize;
        let mut elements: Vec<T> = Vec::with_capacity(rows * cols);

        // Read in column-major order
        for _c in 0..cols {
          for _r in 0..rows {
            let elem = T::from_le(&bytes[cursor.position() as usize..]);
            let mut buf = Vec::new();
            elem.write_le(&mut buf);
            cursor.set_position(cursor.position() + buf.len() as u64);
            elements.push(elem);
          }
        }
        // Now construct the fixed-size matrix
        // All nalgebra fixed-size matrices implement `from_row_slice`
        <$matrix_type>::from_row_slice(&elements)
      }
      fn value_kind(&self) -> ValueKind { self.value_kind() }
      fn align() -> u8 { 8 }
    }
  };
}

#[cfg(feature = "matrixd")]
impl<T> ConstElem for DMatrix<T>
where
  T: ConstElem + std::fmt::Debug + std::clone::Clone + PartialEq + 'static,
{
  fn write_le(&self, out: &mut Vec<u8>) {
    out.write_u32::<LittleEndian>(self.nrows() as u32).unwrap();
    out.write_u32::<LittleEndian>(self.ncols() as u32).unwrap();
    for c in 0..self.ncols() {
      for r in 0..self.nrows() {
        self[(r, c)].write_le(out);
      }
    }
  }
  fn from_le(bytes: &[u8]) -> Self {
    let mut cursor = Cursor::new(bytes);
    let rows = cursor.read_u32::<LittleEndian>().unwrap() as usize;
    let cols = cursor.read_u32::<LittleEndian>().unwrap() as usize;
    let mut elements = Vec::with_capacity(rows * cols);
    // Read in column-major order
    for _c in 0..cols {
      for _r in 0..rows {
        let elem = T::from_le(&bytes[cursor.position() as usize..]);
        let mut buf = Vec::new();
        elem.write_le(&mut buf);
        cursor.set_position(cursor.position() + buf.len() as u64);
        elements.push(elem);
      }
    }
    DMatrix::from_vec(rows, cols, elements)
  }
  fn value_kind(&self) -> ValueKind { self.value_kind() }
  fn align() -> u8 { 8 }
}

#[cfg(feature = "vectord")]
impl<T> ConstElem for DVector<T>
where
  T: ConstElem + std::fmt::Debug + std::clone::Clone + PartialEq + 'static,
{
  fn write_le(&self, out: &mut Vec<u8>) {
    out.write_u32::<LittleEndian>(self.nrows() as u32).unwrap();
    out.write_u32::<LittleEndian>(self.ncols() as u32).unwrap();
    for c in 0..self.ncols() {
      for r in 0..self.nrows() {
        self[(r, c)].write_le(out);
      }
    }
  }
  fn from_le(bytes: &[u8]) -> Self {
    let mut cursor = Cursor::new(bytes);
    let rows = cursor.read_u32::<LittleEndian>().unwrap() as usize;
    let cols = cursor.read_u32::<LittleEndian>().unwrap() as usize;
    let mut elements = Vec::with_capacity(rows * cols);
    // Read in column-major order
    for _c in 0..cols {
      for _r in 0..rows {
        let elem = T::from_le(&bytes[cursor.position() as usize..]);
        let mut buf = Vec::new();
        elem.write_le(&mut buf);
        cursor.set_position(cursor.position() + buf.len() as u64);
        elements.push(elem);
      }
    }
    DVector::from_vec(elements)
  }
  fn value_kind(&self) -> ValueKind { self.value_kind() }
  fn align() -> u8 { 8 }
}

#[cfg(feature = "row_vectord")]
impl<T> ConstElem for RowDVector<T>
where
  T: ConstElem + std::fmt::Debug + std::clone::Clone + PartialEq + 'static,
{
  fn write_le(&self, out: &mut Vec<u8>) {
    out.write_u32::<LittleEndian>(self.nrows() as u32).unwrap();
    out.write_u32::<LittleEndian>(self.ncols() as u32).unwrap();
    for c in 0..self.ncols() {
      for r in 0..self.nrows() {
        self[(r, c)].write_le(out);
      }
    }
  }
  fn from_le(bytes: &[u8]) -> Self {
    let mut cursor = Cursor::new(bytes);
    let rows = cursor.read_u32::<LittleEndian>().unwrap() as usize;
    let cols = cursor.read_u32::<LittleEndian>().unwrap() as usize;
    let mut elements = Vec::with_capacity(rows * cols);
    // Read in column-major order
    for _c in 0..cols {
      for _r in 0..rows {
        let elem = T::from_le(&bytes[cursor.position() as usize..]);
        let mut buf = Vec::new();
        elem.write_le(&mut buf);
        cursor.set_position(cursor.position() + buf.len() as u64);
        elements.push(elem);
      }
    }
    RowDVector::from_vec(elements)
  }
  fn value_kind(&self) -> ValueKind { self.value_kind() }
  fn align() -> u8 { 8 }
}

#[cfg(feature = "matrix1")]
impl_const_elem_matrix!(Matrix1<T>);
#[cfg(feature = "matrix2")]
impl_const_elem_matrix!(Matrix2<T>);
#[cfg(feature = "matrix3")]
impl_const_elem_matrix!(Matrix3<T>);
#[cfg(feature = "matrix4")]
impl_const_elem_matrix!(Matrix4<T>);
#[cfg(feature = "matrix2x3")]
impl_const_elem_matrix!(Matrix2x3<T>);
#[cfg(feature = "matrix3x2")]
impl_const_elem_matrix!(Matrix3x2<T>);
#[cfg(feature = "row_vector2")]
impl_const_elem_matrix!(RowVector2<T>);
#[cfg(feature = "row_vector3")]
impl_const_elem_matrix!(RowVector3<T>);
#[cfg(feature = "row_vector4")]
impl_const_elem_matrix!(RowVector4<T>);
#[cfg(feature = "vector2")]
impl_const_elem_matrix!(Vector2<T>);
#[cfg(feature = "vector3")]
impl_const_elem_matrix!(Vector3<T>);
#[cfg(feature = "vector4")]
impl_const_elem_matrix!(Vector4<T>);

#[cfg(feature = "matrix")]
impl<T> ConstElem for Matrix<T> 
where
  T: ConstElem + std::fmt::Debug + std::clone::Clone + PartialEq + 'static,
{
  fn write_le(&self, out: &mut Vec<u8>) {
    match self {
      #[cfg(feature = "matrixd")]
      Matrix::DMatrix(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "vectord")]
      Matrix::DVector(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "row_vectord")]
      Matrix::RowDVector(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "matrix1")]
      Matrix::Matrix1(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "matrix2")]
      Matrix::Matrix2(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "matrix3")]
      Matrix::Matrix3(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "matrix4")]
      Matrix::Matrix4(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "matrix2x3")]
      Matrix::Matrix2x3(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "matrix3x2")]
      Matrix::Matrix3x2(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "row_vector2")]
      Matrix::RowVector2(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "row_vector3")]
      Matrix::RowVector3(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "row_vector4")]
      Matrix::RowVector4(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "vector2")]
      Matrix::Vector2(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "vector3")]
      Matrix::Vector3(mat) => mat.borrow().write_le(out),
      #[cfg(feature = "vector4")]
      Matrix::Vector4(mat) => mat.borrow().write_le(out),
    }
  }
  fn from_le(bytes: &[u8]) -> Self {
    let mut cursor = Cursor::new(bytes);
    let rows = cursor.read_u32::<LittleEndian>().unwrap() as usize;
    let cols = cursor.read_u32::<LittleEndian>().unwrap() as usize;
    let mut elements = Vec::with_capacity(rows * cols);
    // Read in column-major order
    for _c in 0..cols {
      for _r in 0..rows {
        let elem = T::from_le(&bytes[cursor.position() as usize..]);
        let mut buf = Vec::new();
        elem.write_le(&mut buf);
        cursor.set_position(cursor.position() + buf.len() as u64);
        elements.push(elem);
      }
    }
    if rows == 0 || cols == 0 {
      panic!("Cannot create Matrix with zero rows or columns");
    } else if cols == 1 {
      match rows {
        #[cfg(feature = "matrix1")]
        1 => Matrix::Matrix1(Ref::new(Matrix1::from_vec(elements))),
        #[cfg(all(feature = "matrixd", not(feature = "matrix1")))]
        1 => Matrix::DMatrix(Ref::new(DMatrix::from_vec(1,1, elements))),
        #[cfg(feature = "vector2")]
        2 => Matrix::Vector2(Ref::new(Vector2::from_vec(elements))),
        #[cfg(feature = "vector3")]
        3 => Matrix::Vector3(Ref::new(Vector3::from_vec(elements))),
        #[cfg(feature = "vector4")]
        4 => Matrix::Vector4(Ref::new(Vector4::from_vec(elements))),
        #[cfg(feature = "vectord")]
        _ => Matrix::DVector(Ref::new(DVector::from_vec(elements))),
        _ => panic!("No suitable Matrix variant for dimensions {}x{}", rows, cols),
      }
    } else if rows == 1 {
      match cols {
        #[cfg(feature = "row_vector2")]
        2 => Matrix::RowVector2(Ref::new(RowVector2::from_vec(elements))),
        #[cfg(feature = "row_vector3")]
        3 => Matrix::RowVector3(Ref::new(RowVector3::from_vec(elements))),
        #[cfg(feature = "row_vector4")]
        4 => Matrix::RowVector4(Ref::new(RowVector4::from_vec(elements))),
        #[cfg(feature = "row_vectord")]
        _ => Matrix::RowDVector(Ref::new(RowDVector::from_vec(elements))),
        _ => panic!("No suitable Matrix variant for dimensions {}x{}", rows, cols),
      }
    } else {
      match (rows, cols) {
        #[cfg(feature = "matrix1")]
        (1, 1) => Matrix::Matrix1(Ref::new(Matrix1::from_row_slice(&elements))),
        #[cfg(feature = "matrix2")]
        (2, 2) => Matrix::Matrix2(Ref::new(Matrix2::from_row_slice(&elements))),
        #[cfg(feature = "matrix3")]
        (3, 3) => Matrix::Matrix3(Ref::new(Matrix3::from_row_slice(&elements))),
        #[cfg(feature = "matrix4")]
        (4, 4) => Matrix::Matrix4(Ref::new(Matrix4::from_row_slice(&elements))),
        #[cfg(feature = "matrix2x3")]
        (2, 3) => Matrix::Matrix2x3(Ref::new(Matrix2x3::from_row_slice(&elements))),
        #[cfg(feature = "matrix3x2")]
        (3, 2) => Matrix::Matrix3x2(Ref::new(Matrix3x2::from_row_slice(&elements))),
        #[cfg(feature = "matrixd")]
        _ => Matrix::DMatrix(Ref::new(DMatrix::from_vec(rows, cols, elements))),
        _ => panic!("No suitable Matrix variant for dimensions {}x{}", rows, cols),
      }
    }
  }
  fn value_kind(&self) -> ValueKind { self.value_kind() }
  fn align() -> u8 { T::align() }
}


impl ConstElem for Value {
  fn write_le(&self, out: &mut Vec<u8>) {
    // Write the kind tag first
    self.kind().write_le(out);

    // Then write the payload
    match self {
      Value::Empty | Value::EmptyKind(_) => { 
        // no payload for Empty 
      },
      Value::Typed(value, _) => {
        if !matches!(value.as_ref(), Value::Empty) {
          unimplemented!("write_le for non-empty typed value is not implemented");
        }
      }
      #[cfg(feature = "bool")]
      Value::Bool(x) => x.borrow().write_le(out),
      #[cfg(feature = "string")]
      Value::String(x) => x.borrow().write_le(out),
      #[cfg(feature = "u8")]
      Value::U8(x) => x.borrow().write_le(out),
      #[cfg(feature = "u16")]
      Value::U16(x) => x.borrow().write_le(out),
      #[cfg(feature = "u32")]
      Value::U32(x) => x.borrow().write_le(out),
      #[cfg(feature = "u64")]
      Value::U64(x) => x.borrow().write_le(out),
      #[cfg(feature = "u128")]
      Value::U128(x) => x.borrow().write_le(out),
      #[cfg(feature = "i8")]
      Value::I8(x) => x.borrow().write_le(out),
      #[cfg(feature = "i16")]
      Value::I16(x) => x.borrow().write_le(out),
      #[cfg(feature = "i32")]
      Value::I32(x) => x.borrow().write_le(out),
      #[cfg(feature = "i64")]
      Value::I64(x) => x.borrow().write_le(out),
      #[cfg(feature = "i128")]
      Value::I128(x) => x.borrow().write_le(out),
      #[cfg(feature = "f32")]
      Value::F32(x) => x.borrow().write_le(out),
      #[cfg(feature = "f64")]
      Value::F64(x) => x.borrow().write_le(out),
      #[cfg(feature = "rational")]
      Value::R64(x) => x.borrow().write_le(out),
      #[cfg(feature = "complex")]
      Value::C64(x) => x.borrow().write_le(out),
      #[cfg(feature = "set")]
      Value::Set(x) => x.borrow().write_le(out),
      _ => unimplemented!("write_le not implemented for this Value variant"),
    }
  }
  fn from_le(bytes: &[u8]) -> Self {
    let mut cursor = std::io::Cursor::new(bytes);

    // 1. read ValueKind
    let kind = ValueKind::from_le(cursor.get_ref());

    // 2. determine the offset of the payload (length of encoded kind)
    let mut kind_buf = Vec::new();
    kind.write_le(&mut kind_buf);
    let payload = &bytes[kind_buf.len()..];

    // 3. dispatch based on ValueKind
    match kind {
      ValueKind::Empty => Value::Empty,
      ValueKind::Option(inner) => Value::EmptyKind(ValueKind::Option(inner)),
      #[cfg(feature = "bool")]
      ValueKind::Bool => Value::Bool(Ref::new(<bool as ConstElem>::from_le(payload))),
      #[cfg(feature = "string")]
      ValueKind::String => Value::String(Ref::new(<String as ConstElem>::from_le(payload))),
      #[cfg(feature = "u8")]
      ValueKind::U8 => Value::U8(Ref::new(<u8 as ConstElem>::from_le(payload))),
      #[cfg(feature = "u16")]
      ValueKind::U16 => Value::U16(Ref::new(<u16 as ConstElem>::from_le(payload))),
      #[cfg(feature = "u32")]
      ValueKind::U32 => Value::U32(Ref::new(<u32 as ConstElem>::from_le(payload))),
      #[cfg(feature = "u64")]
      ValueKind::U64 => Value::U64(Ref::new(<u64 as ConstElem>::from_le(payload))),
      #[cfg(feature = "u128")]
      ValueKind::U128 => Value::U128(Ref::new(<u128 as ConstElem>::from_le(payload))),
      #[cfg(feature = "i8")]
      ValueKind::I8 => Value::I8(Ref::new(<i8 as ConstElem>::from_le(payload))),
      #[cfg(feature = "i16")]
      ValueKind::I16 => Value::I16(Ref::new(<i16 as ConstElem>::from_le(payload))),
      #[cfg(feature = "i32")]
      ValueKind::I32 => Value::I32(Ref::new(<i32 as ConstElem>::from_le(payload))),
      #[cfg(feature = "i64")]
      ValueKind::I64 => Value::I64(Ref::new(<i64 as ConstElem>::from_le(payload))),
      #[cfg(feature = "i128")]
      ValueKind::I128 => Value::I128(Ref::new(<i128 as ConstElem>::from_le(payload))),
      #[cfg(feature = "f32")]
      ValueKind::F32 => Value::F32(Ref::new(<f32 as ConstElem>::from_le(payload))),
      #[cfg(feature = "f64")]
      ValueKind::F64 => Value::F64(Ref::new(<f64 as ConstElem>::from_le(payload))),
      #[cfg(feature = "rational")]
      ValueKind::R64 => Value::R64(Ref::new(<R64 as ConstElem>::from_le(payload))),
      #[cfg(feature = "complex")]
      ValueKind::C64 => Value::C64(Ref::new(<C64 as ConstElem>::from_le(payload))),
      x => unimplemented!("from_le not implemented for this ValueKind variant: {:?}", x),
    }
  }
  fn value_kind(&self) -> ValueKind {
    self.value_kind()
  }
  fn align() -> u8 {
    1
  }
}

impl ConstElem for ValueKind {
  fn write_le(&self, out: &mut Vec<u8>) {
    match self {
      ValueKind::U8 => out.write_u8(1).expect("write value kind"),
      ValueKind::U16 => out.write_u8(2).expect("write value kind"),
      ValueKind::U32 => out.write_u8(3).expect("write value kind"),
      ValueKind::U64 => out.write_u8(4).expect("write value kind"),
      ValueKind::U128 => out.write_u8(5).expect("write value kind"),
      ValueKind::I8 => out.write_u8(6).expect("write value kind"),
      ValueKind::I16 => out.write_u8(7).expect("write value kind"),
      ValueKind::I32 => out.write_u8(8).expect("write value kind"),
      ValueKind::I64 => out.write_u8(9).expect("write value kind"),
      ValueKind::I128 => out.write_u8(10).expect("write value kind"),
      ValueKind::F32 => out.write_u8(11).expect("write value kind"),
      ValueKind::F64 => out.write_u8(12).expect("write value kind"),
      ValueKind::C64 => out.write_u8(13).expect("write value kind"),
      ValueKind::R64 => out.write_u8(14).expect("write value kind"),
      ValueKind::String => out.write_u8(15).expect("write value kind"),
      ValueKind::Bool => out.write_u8(16).expect("write value kind"),
      ValueKind::Id => out.write_u8(17).expect("write value kind"),
      ValueKind::Index => out.write_u8(18).expect("write value kind"),
      ValueKind::Empty => out.write_u8(19).expect("write value kind"),
      ValueKind::Any => out.write_u8(20).expect("write value kind"),
      ValueKind::Matrix(elem_vk, dims) => {
        out.write_u8(21).expect("write value kind");
        elem_vk.write_le(out);
        out.write_u32::<LittleEndian>(dims.len() as u32).expect("write matrix dims length");
        for d in dims.iter() {
          out.write_u32::<LittleEndian>(*d as u32).expect("write matrix dim");
        }
      },
      ValueKind::Enum(id, name) => {
        out.write_u8(22).expect("write value kind");
        out.write_u64::<LittleEndian>(*id).expect("write enum id");
        name.write_le(out);
      },
      #[cfg(feature = "record")]
      ValueKind::Record(fields) => {
        out.write_u8(23).expect("write value kind");
        out.write_u32::<LittleEndian>(fields.len() as u32).expect("write record fields length");
        for (name, vk) in fields.iter() {
          name.write_le(out);
          vk.write_le(out);
        }
      },
      ValueKind::Map(key_vk, val_vk) => {
        out.write_u8(24).expect("write value kind");
        key_vk.write_le(out);
        val_vk.write_le(out);
      },
      ValueKind::Atom(id, name) => {
        out.write_u8(25).expect("write value kind");
        out.write_u64::<LittleEndian>(*id).expect("write atom id");
        name.write_le(out);
      },
      #[cfg(feature = "table")]
      ValueKind::Table(fields, row_count) => {
        out.write_u8(26).expect("write value kind");
        out.write_u32::<LittleEndian>(fields.len() as u32).expect("write table fields length");
        for (name, vk) in fields.iter() {
          name.write_le(out);
          vk.write_le(out);
        }
        out.write_u32::<LittleEndian>(*row_count as u32).expect("write table row count");
      },
      ValueKind::Tuple(vks) => {
        out.write_u8(27).expect("write value kind");
        out.write_u32::<LittleEndian>(vks.len() as u32).expect("write tuple length");
        for vk in vks.iter() {
          vk.write_le(out);
        }
      },
      ValueKind::Reference(vk) => {
        out.write_u8(28).expect("write value kind");
        vk.write_le(out);
      },
      ValueKind::Set(vk, opt_size) => {
        out.write_u8(29).expect("write value kind");
        vk.write_le(out);
        match opt_size {
          Some(sz) => {
            out.write_u8(1).expect("write set size flag");
            out.write_u32::<LittleEndian>(*sz as u32).expect("write set size");
          },
          None => {
            out.write_u8(0).expect("write set size flag");
          }
        }
      },
      ValueKind::Option(vk) => {
        out.write_u8(30).expect("write value kind");
        vk.write_le(out);
      },
      _ => unimplemented!("write_le not implemented for this ValueKind variant"),
    }
  }
  fn from_le(bytes: &[u8]) -> Self {
    let mut cursor = Cursor::new(bytes);
    let tag = cursor.read_u8().expect("read value kind tag");

    match tag {
      0 => ValueKind::Empty,
      1 => ValueKind::U8,
      2 => ValueKind::U16,
      3 => ValueKind::U32,
      4 => ValueKind::U64,
      5 => ValueKind::U128,
      6 => ValueKind::I8,
      7 => ValueKind::I16,
      8 => ValueKind::I32,
      9 => ValueKind::I64,
      10 => ValueKind::I128,
      11 => ValueKind::F32,
      12 => ValueKind::F64,
      13 => ValueKind::C64,
      14 => ValueKind::R64,
      15 => ValueKind::String,
      16 => ValueKind::Bool,
      17 => ValueKind::Id,
      18 => ValueKind::Index,
      19 => ValueKind::Empty,
      20 => ValueKind::Any,
      #[cfg(feature = "matrix")]
      21 => {
        let elem_vk = ValueKind::from_le(&bytes[cursor.position() as usize..]);
        cursor.set_position(cursor.position() + 1); // advance past elem_vk tag
        let dim_count = cursor.read_u32::<LittleEndian>().expect("read matrix dim count") as usize;
        let mut dims = Vec::with_capacity(dim_count);
        for _ in 0..dim_count {
            dims.push(cursor.read_u32::<LittleEndian>().expect("read matrix dim") as usize);
        }
        ValueKind::Matrix(Box::new(elem_vk), dims)
      }
      #[cfg(feature = "enum")]
      22 => {
        let id = cursor.read_u64::<LittleEndian>().expect("read enum id");
        let name = String::from_le(&bytes[cursor.position() as usize..]);
        ValueKind::Enum(id, name)
      }
      #[cfg(feature = "table")]
      26 => {
        let field_count = cursor.read_u32::<LittleEndian>().expect("read table fields length") as usize;
        let mut fields = Vec::with_capacity(field_count);
        for _ in 0..field_count {
          let name = String::from_le(&bytes[cursor.position() as usize..]);
          let mut buf = Vec::new();
          name.write_le(&mut buf);
          cursor.set_position(cursor.position() + buf.len() as u64);
          let vk = ValueKind::from_le(&bytes[cursor.position() as usize..]);
          let mut buf = Vec::new();
          vk.write_le(&mut buf);
          cursor.set_position(cursor.position() + buf.len() as u64);
          fields.push((name, vk));
        }
        let row_count = cursor.read_u32::<LittleEndian>().expect("read table row count") as usize;
        ValueKind::Table(fields, row_count)
      }
      #[cfg(feature = "set")]
      29 => {
        let elem_vk = ValueKind::from_le(&bytes[cursor.position() as usize..]);
        cursor.set_position(cursor.position() + 1);
        let size_flag = cursor.read_u8().expect("read set size flag");
        let opt_size = if size_flag != 0 {
            Some(cursor.read_u32::<LittleEndian>().expect("read set size") as usize)
        } else {
            None
        };
        ValueKind::Set(Box::new(elem_vk), opt_size)
      }
      x => unimplemented!("from_le not implemented for this ValueKind variant: {:?}", x),
    }
  }
  fn value_kind(&self) -> ValueKind { self.clone() }
  fn align() -> u8 { 1 }
}

// helper to read a length-prefixed string from cursor
fn read_string_from_cursor(cursor: &mut std::io::Cursor<&[u8]>) -> Vec<u8> {
  let len = cursor.read_u32::<LittleEndian>().expect("read string len") as usize;
  let mut buf = vec![0u8; len];
  cursor.read_exact(&mut buf).expect("read string bytes");
  buf
}

#[cfg(feature = "enum")]
impl ConstElem for MechEnum {
  fn write_le(&self, out: &mut Vec<u8>) {
    // write the enum id
    out.write_u64::<LittleEndian>(self.id).expect("write enum id");

    // write the number of variants
    out.write_u32::<LittleEndian>(self.variants.len() as u32).expect("write enum variants length");

    // write each variant: (variant id, has value, value data)
    for (variant_id, variant_value) in self.variants.iter() {
      // variant id
      out.write_u64::<LittleEndian>(*variant_id).expect("write enum variant id");
      match variant_value {
        Some(v) => {
          // has value
          out.write_u8(1).expect("write enum variant has value");
          // value kind
          let value_kind = v.kind();
          value_kind.write_le(out);
          // value data
          v.write_le(out);
        },
        None => {
          // has no value
          out.write_u8(0).expect("write enum variant has no value");
        }
      }
    }
  }
  fn from_le(_bytes: &[u8]) -> Self {
    unimplemented!("from_le not implemented for MechEnum")
  }
  fn value_kind(&self) -> ValueKind { ValueKind::Enum(0,"".to_string()) } // id 0 as placeholder
  fn align() -> u8 { 8 }
}

#[cfg(feature = "table")]
impl ConstElem for MechTable {
  fn write_le(&self, out: &mut Vec<u8>) {
    // Write kind
    self.value_kind().write_le(out);
    // Write number of rows and columns
    out.write_u32::<LittleEndian>(self.rows as u32).expect("write table rows");
    out.write_u32::<LittleEndian>(self.cols as u32).expect("write table cols");
    // Write each column: (id, kind, data, name)
    for (col_id, (vk, col_data)) in &self.data {
      // Column id
      out.write_u64::<LittleEndian>(*col_id).expect("write column id");
      // Value kind
      vk.write_le(out);
      // Column data matrix
      col_data.write_le(out);
      // Column name
      if let Some(name) = self.col_names.get(col_id) {
        name.write_le(out);
      } else {
        String::from("").write_le(out);
      }
    }
  }
  fn from_le(data: &[u8]) -> Self {
    use indexmap::IndexMap;
    let mut cursor = Cursor::new(data);
    // Kind
    let kind = ValueKind::from_le(cursor.get_ref());
    let mut buf = Vec::new();
    kind.write_le(&mut buf);
    cursor.set_position(buf.len() as u64);

    // Read row and column counts
    let rows = cursor.read_u32::<LittleEndian>().expect("read rows") as usize;
    let cols = cursor.read_u32::<LittleEndian>().expect("read cols") as usize;

    let mut data_map: IndexMap<u64, (ValueKind, Matrix<Value>)> = IndexMap::new();
    let mut col_names: HashMap<u64, String> = HashMap::new();

    // Decode each column
    for _ in 0..cols {
      let col_id = cursor.read_u64::<LittleEndian>().expect("read column id");

      // read value kind
      let kind = ValueKind::from_le(&data[cursor.position() as usize..]);
      let mut tmp = Vec::new();
      kind.write_le(&mut tmp);
      cursor.set_position(cursor.position() + tmp.len() as u64);

      // read matrix
      let matrix = Matrix::<Value>::from_le(&data[cursor.position() as usize..]);
      let mut tmp = Vec::new();
      matrix.write_le(&mut tmp);
      cursor.set_position(cursor.position() + tmp.len() as u64);

      // read column name
      let name = String::from_le(&data[cursor.position() as usize..]);
      let mut tmp = Vec::new();
      name.write_le(&mut tmp);
      cursor.set_position(cursor.position() + tmp.len() as u64);

      data_map.insert(col_id, (kind, matrix));
      col_names.insert(col_id, name);
    }

    MechTable { rows, cols, data: data_map, col_names }
  }
  fn value_kind(&self) -> ValueKind { self.kind() }
  fn align() -> u8 { 8 }
}

#[cfg(feature = "table")]
impl CompileConst for MechTable {
  fn compile_const(&self, ctx: &mut CompileCtx) -> MResult<u32> {
    let mut payload = Vec::<u8>::new();
    self.value_kind().write_le(&mut payload);
    payload.write_u32::<LittleEndian>(self.rows as u32)?;
    payload.write_u32::<LittleEndian>(self.cols as u32)?;
    for (col_id, (vk, col_data)) in &self.data {
      payload.write_u64::<LittleEndian>(*col_id)?;
      vk.write_le(&mut payload);
      col_data.write_le(&mut payload);

      if let Some(name) = self.col_names.get(col_id) {
        name.write_le(&mut payload);
      } else {
        String::from("").write_le(&mut payload);
      }
    }
    ctx.compile_const(&payload, self.value_kind())
  }
}

#[cfg(feature = "set")]
impl ConstElem for MechSet {
  fn write_le(&self, out: &mut Vec<u8>) {
    // write kind
    self.kind.write_le(out);
    // write element count
    out.write_u32::<LittleEndian>(self.num_elements as u32)
      .expect("write set element count");
    // write each element
    for value in &self.set {
      value.write_le(out);
    }
  }
  fn from_le(data: &[u8]) -> Self {
    use indexmap::IndexSet;
    let mut cursor = Cursor::new(data);
    // 1) read kind from current position
    let start = cursor.position() as usize;
    let kind = ValueKind::from_le(&data[start..]);
    // compute how many bytes the kind encoding consumes (so we can advance)
    let mut kind_buf = Vec::new();
    kind.write_le(&mut kind_buf);
    cursor.set_position(start as u64 + kind_buf.len() as u64);
    // 2) element count (little endian)
    let num_elements = cursor
      .read_u32::<LittleEndian>()
      .expect("read set element count") as usize;
    // 3) read each Value (advance cursor using each value's encoded length)
    let mut set = IndexSet::with_capacity(num_elements);
    for _ in 0..num_elements {
      let pos = cursor.position() as usize;
      let value = Value::from_le(&data[pos..]);
      // measure its encoded length by re-serializing
      let mut tmp = Vec::new();
      value.write_le(&mut tmp);
      cursor.set_position(pos as u64 + tmp.len() as u64);
      set.insert(value);
    }
    Self { kind, num_elements, set }
  }
  fn value_kind(&self) -> ValueKind { self.kind.clone() }
  fn align() -> u8 { 8 }
}

#[cfg(feature = "tuple")]
impl ConstElem for MechTuple {
  fn write_le(&self, out: &mut Vec<u8>) {
    self.value_kind().write_le(out);
    out.write_u32::<LittleEndian>(self.elements.len() as u32)
      .expect("write tuple element count");
    for elem in &self.elements {
      elem.write_le(out);
    }
  }
  fn from_le(data: &[u8]) -> Self {
    let mut cursor = Cursor::new(data);
    // 1) Read ValueKind (tuple kind)
    let start = cursor.position() as usize;
    let kind = ValueKind::from_le(&data[start..]);
    // Determine how many bytes were used for kind
    let mut kind_buf = Vec::new();
    kind.write_le(&mut kind_buf);
    cursor.set_position(start as u64 + kind_buf.len() as u64);
    // 2) Read element count
    let num_elements = cursor
      .read_u32::<LittleEndian>()
      .expect("read tuple element count") as usize;
    // 3) Read each element
    let mut elements: Vec<Box<Value>> = Vec::with_capacity(num_elements);
    for _ in 0..num_elements {
      let pos = cursor.position() as usize;
      let value = Value::from_le(&data[pos..]);
      // Measure how many bytes were consumed by this value
      let mut tmp = Vec::new();
      value.write_le(&mut tmp);
      cursor.set_position(pos as u64 + tmp.len() as u64);
      elements.push(Box::new(value));
    }
    Self { elements }
  }
  fn value_kind(&self) -> ValueKind {
    ValueKind::Tuple(
      self.elements
        .iter()
        .map(|v| v.value_kind())
        .collect::<Vec<_>>()
    )
  }  
  fn align() -> u8 { 8 }
}