wlambda 0.5.0

WLambda is an embeddable scripting language for Rust
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
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
// Copyright (c) 2020 Weird Constructor <weirdconstructor@gmail.com>
// This is a part of WLambda. See README.md and COPYING for details.

/*!

This module provides the core data structures used by the parser,
compiler and evaluator of WLambda.

*/

use std::rc::Rc;
use std::rc::Weak;
use std::cell::RefCell;
use std::fmt;
use std::fmt::{Display, Debug, Formatter};

use crate::str_int::*;
use crate::compiler::{GlobalEnv, GlobalEnvRef};
use crate::nvec::NVec;
use crate::ops::Prog;

use fnv::FnvHashMap;

#[derive(Debug, Clone, PartialEq)]
pub struct FileRef {
    s: Rc<String>,
}

impl Display for FileRef {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "{}", *self.s)
    }
}

impl FileRef {
    pub fn new(s: &str) -> Self {
        FileRef { s: Rc::new(String::from(s)) }
    }
    pub fn s(&self) -> &str { &(*self.s) }
}

/// Structure for holding information about origin
/// of an AST node.
#[derive(Clone, PartialEq)]
pub struct SynPos {
    pub syn:        Syntax,
    pub line:       u32,
    pub col:        u16,
    pub file:       FileRef,
    pub name:       Option<std::rc::Rc<String>>,
}

impl SynPos {
    pub fn empty() -> Self {
        Self {
            syn:     Syntax::Block,
            line:    0,
            col:     0,
            file:    FileRef::new(""),
            name:    None,
        }
    }

    pub fn s_short(&self) -> String {
        format!("[{},{}({:?})]", self.line, self.col, self.syn)
    }
}

impl Display for SynPos {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        if self.line > 0 {
            if self.name.is_some() && !self.name.as_ref().unwrap().is_empty() {
                write!(f, "[{},{}:{}({:?})@{}]",
                       self.line, self.col, self.file.s(), self.syn,
                       self.name.as_ref().unwrap())
            } else {
                write!(f, "[{},{}:{}({:?})]",
                       self.line, self.col, self.file.s(), self.syn)
            }
        } else {
            write!(f, "")
        }
    }
}

impl Debug for SynPos {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct CompileError {
    pub pos:    SynPos,
    pub msg:    String,
}

impl Display for CompileError {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(
            f,
            "[{},{}:{}] Compilation Error: {}",
            self.pos.line, self.pos.col, self.pos.file, self.msg)
    }
}


/// Encodes the different types of AST nodes.
#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub enum Syntax {
    Var,
    Key,
    SetKey,
    GetKey,
    GetKey2,
    GetKey3,
    GetSym,
    GetSym2,
    GetSym3,
    GetIdx,
    GetIdx2,
    GetIdx3,
    BinOpAdd,
    BinOpSub,
    BinOpMul,
    BinOpDiv,
    BinOpMod,
    BinOpLe,
    BinOpLt,
    BinOpGe,
    BinOpGt,
    BinOpEq,
    Str,
    Lst,
    IVec,
    FVec,
    Opt,
    Iter,
    Map,
    Expr,
    Func,
    Block,
    Err,
    Call,
    Apply,
    And,
    Or,
    Assign,
    Def,
    Ref,
    WRef,
    Deref,
    CaptureRef,
    AssignRef,
    DefGlobRef,
    DefConst,
    SelfObj,
    SelfData,
    Import,
    Export,
    DumpStack,
    DumpVM,
    MapSplice,
    VecSplice,
    Accum,
    GlobVar,
    Selector,
    Pattern,
}

#[derive(Clone)]
pub struct Stdio {
    pub write: Rc<RefCell<dyn std::io::Write>>,
    pub read:  Rc<RefCell<dyn std::io::BufRead>>,
}

impl Stdio {
    pub fn new_rust_std() -> Self {
        Self {
            write: Rc::new(RefCell::new(std::io::stdout())),
            read:  Rc::new(RefCell::new(std::io::BufReader::new(std::io::stdin()))),
        }
    }

    pub fn new_from_mem(input: Rc<RefCell<std::io::Cursor<Vec<u8>>>>,
                        output: Rc<RefCell<Vec<u8>>>)
        -> Self
    {
        Self {
            write: output,
            read: input,
        }
    }
}

impl std::fmt::Debug for Stdio {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "wlambda::vval::Stdio")
    }
}

/// The maximum stack size.
///
/// Currently hardcoded, but later the API user will be able to specify it.
const STACK_SIZE : usize = 10240;

#[derive(Default, Debug, Clone, Copy)]
pub struct LoopInfo {
    pub pc:       usize,
    pub uw_depth: usize,
    pub sp:       usize,
    pub break_pc: usize,
}

impl LoopInfo {
    #[inline]
    pub fn new() -> Self {
        Self { pc: 0, uw_depth: 0, sp: 0, break_pc: 0 }
    }
}

/// Describes an action that needs to be done when returning from a function
/// or somehow jumps unpredictably around the VM prog.
#[derive(Debug, Clone)]
pub enum UnwindAction {
    Null,
    RestoreAccum(VVal, VVal),
    RestoreSP(usize),
    ClearLocals(usize, usize),
    RestoreSelf(VVal),
    RestoreLoopInfo(LoopInfo),
    RestoreIter(Option<Rc<RefCell<VValIter>>>),
    FunctionCall(usize, usize, usize),
}

/// The runtime environment of the evaluator.
#[derive(Debug)]
pub struct Env {
    /// The argument stack, limited to `STACK_SIZE`.
    pub args: std::vec::Vec<VVal>,
    /// A stack of the currently called functions.
    ///
    /// Used for accessing the up values, backtrace
    /// and other details about the function.
    pub call_stack: std::vec::Vec<Rc<VValFun>>,
    /// A stack that holds cleanup routines that need to be handled:
    pub unwind_stack: std::vec::Vec<UnwindAction>,
    /// A stack pointer for unwind_stack,
    pub unwind_sp: usize,
    /// Holds the object of the currently called method:
    pub current_self: VVal,
    /// The basepointer to reference arguments and
    /// local variables.
    ///
    /// - `bp + n (n >= 0)` references a local variable
    /// - `bp - n (n > 0)` references an argument
    pub bp:   usize,
    /// The current stack pointer.
    pub sp:   usize,
    /// The argument count to the current call.
    pub argc: usize,
    /// A user defined variable that holds user context information.
    /// See also the [with_user_do](struct.Env.html#method.with_user_do) function.
    pub user: Rc<RefCell<dyn std::any::Any>>,
    /// The exported names of this module.
    pub exports: FnvHashMap<Symbol, VVal>,
    /// This is the standard output used for any functions in
    /// WLambda that print something. Such as `std:displayln`
    /// or `std:writeln`.
    pub stdio: Stdio,
    /// Current accumulator value:
    pub accum_val: VVal,
    /// Current accumulator function:
    pub accum_fun: VVal,
    /// A pointer to the global environment, holding stuff like
    /// the module loader and thread creator.
    pub global: GlobalEnvRef,
    /// A counter that counts the nesting depth in vm() calls
    pub vm_nest: usize,
    /// Holds information to process 'next' and 'break' for loop
    /// constructs:
    pub loop_info: LoopInfo,
    /// Holds the current iterator for the 'iter' construct.
    pub iter: Option<Rc<RefCell<VValIter>>>,
}

//impl Default for Env {
//    fn default() -> Self { Self::new() }
//}
//
impl Env {
    pub fn new(global: GlobalEnvRef) -> Env {
        let mut e = Env {
            args:               Vec::with_capacity(STACK_SIZE),
            current_self:       VVal::None,
            bp:                 0,
            sp:                 0,
            argc:               0,
            user:               Rc::new(RefCell::new(VVal::vec())),
            exports:            FnvHashMap::with_capacity_and_hasher(5, Default::default()),
            stdio:              Stdio::new_rust_std(),
            accum_fun:          VVal::None,
            accum_val:          VVal::None,
            call_stack:         vec![],
            unwind_stack:       vec![],
            unwind_sp:          0,
            loop_info:          LoopInfo::new(),
            iter:               None,
            vm_nest:            0,
            global
        };
        e.args.resize(STACK_SIZE, VVal::None);
        e.unwind_stack.resize(STACK_SIZE, UnwindAction::Null);
        e
    }

    pub fn new_with_user(global: GlobalEnvRef, user: Rc<RefCell<dyn std::any::Any>>) -> Env {
        let mut e = Env {
            args:               Vec::with_capacity(STACK_SIZE),
            current_self:       VVal::None,
            bp:                 0,
            sp:                 0,
            argc:               0,
            exports:            FnvHashMap::with_capacity_and_hasher(2, Default::default()),
            stdio:              Stdio::new_rust_std(),
            accum_fun:          VVal::None,
            accum_val:          VVal::None,
            call_stack:         vec![],
            unwind_stack:       std::vec::Vec::with_capacity(1000),
            unwind_sp:          0,
            loop_info:          LoopInfo::new(),
            iter:               None,
            vm_nest:            0,
            user,
            global,
        };
        e.args.resize(STACK_SIZE, VVal::None);
        e.unwind_stack.resize(STACK_SIZE, UnwindAction::Null);
        e
    }

    /// Sets a custom stdio procedure. This can be used to redirect the
    /// standard input/output operations of WLambda to other sinks and sources.
    /// For instance writing and reading from a Buffer when using WASM
    /// or some other embedded application:
    ///
    /// ```
    /// use wlambda::*;
    /// use std::rc::Rc;
    /// use std::cell::RefCell;
    ///
    /// let new_output : Rc<RefCell<Vec<u8>>> =
    ///     Rc::new(RefCell::new(vec![]));
    /// let new_input =
    ///     Rc::new(RefCell::new(std::io::Cursor::new("abc\ndef\n1 2 3 4\n"
    ///                          .to_string().as_bytes().to_vec())));
    ///
    /// let memory_stdio =
    ///     vval::Stdio::new_from_mem(new_input, new_output.clone());
    ///
    /// let mut ctx = EvalContext::new_default();
    /// ctx.local.borrow_mut().set_stdio(memory_stdio);
    ///
    /// ctx.eval("std:displayln :TEST 123").unwrap();
    ///
    /// let output = String::from_utf8(new_output.borrow().clone()).unwrap();
    /// assert_eq!(output, "TEST 123\n");
    ///
    /// let out_lines =
    ///     ctx.eval("!l = $[]; std:io:lines \\std:push l _; l").unwrap().s();
    /// assert_eq!(out_lines, "$[\"abc\\n\",\"def\\n\",\"1 2 3 4\\n\"]");
    /// ```
    pub fn set_stdio(&mut self, stdio: Stdio) {
        self.stdio = stdio;
    }

    /// Returns the passed in user context value.
    pub fn get_user(&self) -> Rc<RefCell<dyn std::any::Any>> {
        self.user.clone()
    }

    /// Easier access to the user data field in Env
    ///
    /// In the following example the user supplies a registry
    /// vector for storing VVals. A callback is stored, which is
    /// then later executed.
    ///
    /// ```
    /// use wlambda::{VVal, EvalContext, GlobalEnv};
    /// use wlambda::vval::Env;
    /// use std::rc::Rc;
    /// use std::cell::RefCell;
    ///
    /// let global = GlobalEnv::new_default();
    ///
    /// global.borrow_mut().add_func("reg", |env: &mut Env, _argc: usize| {
    ///     let fun = env.arg(0);
    ///     env.with_user_do(|v: &mut Vec<VVal>| v.push(fun.clone()));
    ///     Ok(VVal::None)
    /// }, Some(1), Some(1));
    ///
    /// let reg : Rc<RefCell<Vec<VVal>>> =
    ///     Rc::new(RefCell::new(Vec::new()));
    ///
    /// let mut ctx = EvalContext::new_with_user(global, reg.clone());
    /// ctx.eval("reg { _ + 10 }").unwrap();
    ///
    /// let n = reg.borrow_mut()[0].clone();
    /// let ret = ctx.call(&n, &vec![VVal::Int(11)]).unwrap();
    /// assert_eq!(ret.i(), 21);
    /// ```
    #[allow(dead_code)]
    pub fn with_user_do<T: 'static, F, X>(&mut self, f: F) -> X
        where F: Fn(&mut T) -> X {
        let mut any = self.user.borrow_mut();
        let ref_reg = any.downcast_mut::<T>().unwrap();
        f(ref_reg)
    }

    pub fn export_name(&mut self, name: &str, value: &VVal) {
        self.exports.insert(s2sym(name), value.clone());
    }

    #[inline]
    pub fn set_bp(&mut self, env_size: usize) -> usize {
        let new_bp = self.sp;
        self.sp += env_size;
        std::mem::replace(&mut self.bp, new_bp)
    }

    #[inline]
    pub fn reset_bp(&mut self, env_size: usize, oldbp: usize) {
        for i in self.bp..self.sp {
            self.args[i] = VVal::None;
        }
//        self.sp -= env_size;
        self.popn(env_size);
        self.bp = oldbp;
    }

    pub fn self_object(&self) -> VVal {
        self.current_self.clone()
    }

    #[inline]
    pub fn with_object<T>(&mut self, object: VVal, f: T) -> Result<VVal, StackAction>
        where T: Fn(&mut Env) -> Result<VVal, StackAction> {
        let old_self = std::mem::replace(&mut self.current_self, object);
        let ret = f(self);
        std::mem::replace(&mut self.current_self, old_self);
        ret
    }

    #[inline]
    pub fn with_local_call_info<T>(&mut self, argc: usize, f: T) -> Result<VVal, StackAction>
        where T: Fn(&mut Env) -> Result<VVal, StackAction> {
        let local_size = 0;
        let old_argc = std::mem::replace(&mut self.argc, argc);
        let old_bp   = self.set_bp(local_size);

        let ret = f(self);

        self.reset_bp(local_size, old_bp);
        self.argc = old_argc;

        ret
    }

//    #[inline]
//    pub fn with_fun_info<T>(&mut self, fu: Rc<VValFun>, argc: usize, f: T) -> Result<VVal, StackAction>
//        where T: Fn(&mut Env) -> Result<VVal, StackAction> {
//        self.push_fun_call(fu);
////        let local_size = fu.local_size;
////        let old_argc = std::mem::replace(&mut self.argc, argc);
//////        let old_fun  = std::mem::replace(&mut self.fun, fu.clone());
////        self.call_stack.push(fu);
////        let old_bp   = self.set_bp(local_size);
////        //d// println!("OLD FUN: {:?}", old_fun.upvalues);
//
//        let ret = f(self);
//
//        self.unwind_one();
////        self.reset_bp(local_size, old_bp);
//////        self.fun  = old_fun;
////        self.call_stack.pop();
////        self.argc = old_argc;
//
//        ret
//    }
//
    #[inline]
    pub fn with_restore_sp<T>(&mut self, f: T) -> Result<VVal, StackAction>
        where T: Fn(&mut Env) -> Result<VVal, StackAction> {

        let old_sp = self.sp;
        let ret = f(self);
        self.popn(self.sp - old_sp);
        ret
    }

    #[inline]
    pub fn push_sp(&mut self, n: usize) {
        self.sp += n;
        //d// println!("PUSH_SP {} => {}", n, self.sp);
    }

    #[inline]
    pub fn push(&mut self, v: VVal) -> usize {
        self.args[self.sp] = v;
        self.sp += 1;
        self.sp - 1
    }

    #[inline]
    pub fn stk(&self, offs: usize) -> &VVal {
        &self.args[self.sp - offs] // implicit self.sp - 1! We must not call with 0!
    }

    #[inline]
    pub fn stk_i(&self, offs: usize) -> i64 {
        if let VVal::Int(i) = &self.args[(self.sp - 1) +  offs] {
            *i
        } else {
            0
        }
    }

    #[inline]
    pub fn inc_local(&mut self, idx: usize, inc: i16) -> i64 {
        if let VVal::Int(i) = &mut self.args[self.bp + idx] {
            if inc > 0 { *i += 1; }
            else { *i -= 1; }

            *i
        } else {
            0
        }
    }

    #[inline]
    pub fn pop(&mut self) -> VVal {
        if self.sp < 1 {
            panic!(format!("Stack pointer underflow {} {}", self.sp, 1));
        }
        self.sp -= 1;
        std::mem::replace(&mut self.args[self.sp], VVal::None)
    }

    #[inline]
    pub fn null_locals(&mut self, from: usize, to: usize) {
        for i in from..to {
            self.args[self.bp + i] = VVal::None;
        }
    }

    #[inline]
    pub fn popn(&mut self, n: usize) {
        if self.sp < n {
            panic!(format!("Stack pointer underflow {} {}", self.sp, n));
        }
        if n > 0 {
            //d// println!("SP={}, N={}", self.sp, n);
            for i in (self.sp - n)..self.sp {
                //d// println!("POP[{}] {} [of {}]", i, self.args[i].s(), n);
                self.args[i] = VVal::None;
            }
        }
        self.sp -= n;
        //d// println!("POPN {} => {}", n, self.sp);
    }

    /// Prints a dump of the stack state.
    pub fn dump_stack(&self) {
        //d// println!("* SP={}, BP={}", self.sp, self.bp);
        for (i, v) in self.args.iter().enumerate() {
            let mut mark = String::from("");
            if i == self.bp { mark = format!("{} BP", mark); }
            if i == self.sp { mark = format!("{} SP", mark); }
            if !mark.is_empty() { mark = format!("{} ->", mark); }

            println!("    {:9} [{:3}] = {}", mark, i, v.s());
            if i >= (1 + self.sp) { break; }
        }
        if !self.call_stack.is_empty() {
            for (i, u) in self.call_stack.last().unwrap().upvalues.iter().enumerate() {
                println!("  UP[{:3}] = {}", i, u.s());
            }
        }
    }

    #[inline]
    pub fn argv(&self) -> VVal {
        VVal::vec_from(&self.args[(self.bp - self.argc)..self.bp])
    }

    #[inline]
    pub fn argv_ref(&self) -> &[VVal] {
        &self.args[(self.bp - self.argc)..self.bp]
    }

    #[inline]
    pub fn get_up_raw(&mut self, i: usize) -> VVal {
        //d// println!("GET UP {}: {:?}", i, self.fun.upvalues);
        self.call_stack.last().unwrap().upvalues[i].clone()
    }

    #[inline]
    pub fn get_up_captured_ref(&self, i: usize) -> VVal {
        self.call_stack.last().unwrap().upvalues[i].to_ref()
    }

    #[inline]
    pub fn get_up(&self, i: usize) -> VVal {
        self.call_stack.last().unwrap().upvalues[i].deref()
//        match self.fun.upvalues[i].deref() {
//            VVal::WWRef(l) => { 
//                match l.upgrade() {
//                    Some(v) => v.borrow().clone(),
//                    None => VVal::None,
//                }
//            },
//            v => v,
//        }
    }

    #[inline]
    pub fn arg_ref(&self, i: usize) -> Option<&VVal> {
        if i >= self.argc { return None; }
        Some(&self.args[(self.bp - self.argc) + i])
    }

    #[inline]
    pub fn arg_err_internal(&self, i: usize) -> Option<VVal> {
        let v = &self.args[(self.bp - self.argc) + i];
        match v {
            VVal::Err(_) => Some(v.clone()),
            _            => None,
        }
    }

    #[inline]
    pub fn arg(&self, i: usize) -> VVal {
        //d// println!("GET ARGC [{}] = {}", i, self.argc);
        if i >= self.argc { return VVal::None; }
        let v = &self.args[(self.bp - self.argc) + i];
        //d// println!("GET ARG [{}/{}] = {}", i, self.sp - (i + 1), v.s());
        v.clone()
    }

    pub fn get_local_up_promotion(&mut self, i: usize) -> VVal {
        let idx = self.bp + i;
        match &self.args[idx] {
            VVal::CRef(r) => VVal::WWRef(Rc::downgrade(&r)),
            VVal::Ref(r)  => VVal::Ref(r.clone()),
            VVal::WWRef(r) => VVal::WWRef(r.clone()),
            v => {
                let new_v = v.to_weakened_upvalue_ref();
                self.args[idx] = new_v.clone();
                new_v.downgrade()
            }
        }
    }

    pub fn get_local_captured_ref(&self, i: usize) -> VVal {
        let idx = self.bp + i;
        self.args[idx].to_ref()
    }

    #[inline]
    pub fn reg(&self, i: i32) -> VVal {
        if i >= 0 {
            self.get_local(i as usize)
        } else {
            self.stk((-i) as usize).clone()
        }
    }

    #[inline]
    pub fn get_local(&self, i: usize) -> VVal {
        match &self.args[self.bp + i] {
            VVal::CRef(r)  => r.borrow().clone(),
            v              => v.clone(),
        }
    }

    pub fn assign_ref_up(&mut self, i: usize, value: VVal) {
        let fun = self.call_stack.last().unwrap().clone();
        let upv = &fun.upvalues[i];

        match upv {
            VVal::Ref(r)     => { r.replace(value); }
            VVal::CRef(r)    => { r.replace(value); }
            VVal::WWRef(l)   => {
                if let Some(r) = l.upgrade() {
                    r.replace(value);
                }
            },
            _ => (),
        }
    }

    pub fn assign_ref_local(&mut self, i: usize, value: VVal) {
        let idx = self.bp + i;
        match &mut self.args[idx] {
            VVal::Ref(r)     => { r.replace(value); }
            VVal::CRef(r)    => { r.replace(value); }
            VVal::WWRef(l)   => {
                if let Some(r) = l.upgrade() {
                    r.replace(value);
                }
            },
            v => { *v = value },
        }
    }

    pub fn set_up(&mut self, index: usize, value: VVal) {
        let fun = self.call_stack.last().unwrap().clone();
        let upv = &fun.upvalues[index];

        match upv {
            VVal::Ref(r)   => { r.replace(value); }
            VVal::CRef(r)  => { r.replace(value); }
            VVal::WWRef(r) => {
                if let Some(r) = Weak::upgrade(r) {
                    r.replace(value);
                }
            },
            _ => {}
        }
    }

    #[inline]
    pub fn set_consume(&mut self, i: usize, value: VVal) {
        match &mut self.args[self.bp + i] {
            VVal::CRef(r)  => { r.replace(value); }
            v              => { *v = value }
        }
    }

    #[inline]
    pub fn unwind_depth(&self) -> usize {
        self.unwind_sp
    }

    #[inline]
    pub fn push_unwind(&mut self, uwa: UnwindAction) {
        self.unwind_stack[self.unwind_sp] = uwa;
        self.unwind_sp += 1;
    }

    #[inline]
    pub fn unwind_to_depth(&mut self, depth: usize) {
        while self.unwind_sp > depth {
            self.unwind_one();
        }
    }

    #[inline]
    pub fn push_fun_call(&mut self, fu: Rc<VValFun>, argc: usize) {
        let local_size = fu.local_size;
        let old_bp = self.set_bp(local_size);
        let uwa =
            UnwindAction::FunctionCall(
                std::mem::replace(&mut self.argc, argc),
                old_bp,
                local_size);
        self.push_unwind(uwa);
        self.call_stack.push(fu);
    }

    #[inline]
    pub fn push_unwind_sp(&mut self) {
        self.push_unwind(UnwindAction::RestoreSP(self.sp));
    }

    #[inline]
    pub fn push_clear_locals(&mut self, from: usize, to: usize) {
        self.push_unwind(UnwindAction::ClearLocals(from, to));
    }

    #[inline]
    pub fn push_unwind_self(&mut self, new_self: VVal) {
        let uwa =
            UnwindAction::RestoreSelf(
                std::mem::replace(&mut self.current_self, new_self));
        self.push_unwind(uwa);
    }

    #[inline]
    pub fn cleanup_loop(&mut self) {
        while self.sp > self.loop_info.sp {
            self.pop();
        }
        self.unwind_to_depth(self.loop_info.uw_depth);
    }

    #[inline]
    pub fn push_loop_info(&mut self, current_pc: usize, break_pc: usize, uw_depth_offs: usize) {
        let uw_depth = self.unwind_depth() + 1 + uw_depth_offs;
        let uwa =
            UnwindAction::RestoreLoopInfo(
                std::mem::replace(&mut self.loop_info, LoopInfo {
                    pc:       current_pc,
                    sp:       self.sp,
                    uw_depth,
                    break_pc,
                }));
        self.push_unwind(uwa);
    }

    #[inline]
    pub fn push_iter(&mut self, iter: Rc<RefCell<VValIter>>) {
        let uwa =
            UnwindAction::RestoreIter(
                std::mem::replace(
                    &mut self.iter, Some(iter)));
        self.push_unwind(uwa);
    }

    pub fn dump_unwind_stack(&self) -> String {
        let mut s = String::new();
        for i in 0..self.unwind_sp {
            let add =
                match &self.unwind_stack[self.unwind_sp - (i + 1)] {
                    UnwindAction::RestoreSP(sp) =>
                        format!("rsp({})", *sp),
                    UnwindAction::ClearLocals(from, to) =>
                        format!("cl({},{})", *from, *to),
                    UnwindAction::RestoreLoopInfo(li) =>
                        format!("loinf(uws:{},sp:{})", li.uw_depth, li.sp),
                    UnwindAction::RestoreAccum(_fun, _val) =>
                        ("raccm".to_string()),
                    UnwindAction::RestoreSelf(_slf) =>
                        ("rslf".to_string()),
                    UnwindAction::RestoreIter(_i) =>
                        ("ritr".to_string()),
                    UnwindAction::FunctionCall(argc, old_bp, local_size) =>
                        format!("fcal({},{},{})", argc, old_bp, local_size),
                    UnwindAction::Null =>
                        ("nul".to_string()),
                };

            if !s.is_empty() {
                s += ";";
            }
            s += &add[..];
        }
        s
    }

    #[inline]
    pub fn unwind(&mut self, ua: UnwindAction) {
        match ua {
            UnwindAction::RestoreSP(sp) => {
                while self.sp > sp {
                    self.pop();
                }
            },
            UnwindAction::ClearLocals(from, to) => {
                self.null_locals(from, to);
            },
            UnwindAction::RestoreLoopInfo(li) => {
                std::mem::replace(&mut self.loop_info, li);
            },
            UnwindAction::RestoreAccum(fun, val) => {
                std::mem::replace(&mut self.accum_fun, fun);
                std::mem::replace(&mut self.accum_val, val);
            },
            UnwindAction::RestoreSelf(slf) => {
                std::mem::replace(&mut self.current_self, slf);
            },
            UnwindAction::RestoreIter(i) => {
                std::mem::replace(&mut self.iter, i);
            },
            UnwindAction::FunctionCall(argc, old_bp, local_size) => {
                self.reset_bp(local_size, old_bp);
                self.call_stack.pop();
                self.argc = argc;
            },
            UnwindAction::Null => (),
        }
    }

    #[inline]
    pub fn unwind_one(&mut self) {
        self.unwind_sp -= 1;
        let ua =
            std::mem::replace(
                &mut self.unwind_stack[self.unwind_sp],
                UnwindAction::Null);
        self.unwind(ua);
    }

    pub fn setup_accumulator(&mut self, v: VVal) {
        let f =
            match v {
                VVal::Map(_) => {
                    VValFun::new_fun(
                        move |env: &mut Env, _argc: usize| {
                            let k = env.arg(0);
                            let v = env.arg(1);
                            env.accum_val.set_key(&k, v.clone())?;
                            Ok(v)
                        }, Some(2), Some(2), false)
                },
                _ => {
                    VValFun::new_fun(
                        move |env: &mut Env, _argc: usize| {
                            let v = env.arg(0);
                            env.accum_val.accum(&v);
                            Ok(v)
                        }, Some(1), Some(1), false)
                }
            };

        let uwa =
            UnwindAction::RestoreAccum(
                std::mem::replace(&mut self.accum_fun, f),
                std::mem::replace(&mut self.accum_val, v));
        self.push_unwind(uwa);
    }

    pub fn with_accum<T>(&mut self, v: VVal, acfun: T) -> Result<VVal, StackAction>
        where T: Fn(&mut Env) -> Result<VVal, StackAction>
    {
        self.setup_accumulator(v);

        let ret = acfun(self);

        let val = self.accum_val.clone();
        self.unwind_one();

        ret?;
        Ok(val)
    }

    pub fn get_accum_value(&self) -> VVal {
        self.accum_val.clone()
    }

    pub fn get_accum_function(&self) -> VVal {
        self.accum_fun.clone()
    }

    /// Creates a new error VVal with the given string as error message.
    /// Takes care to annotate the error value with the current function
    /// information.
    ///
    ///```
    /// use wlambda::compiler::EvalContext;
    /// use wlambda::vval::{VVal, VValFun, Env};
    ///
    /// let mut ctx = EvalContext::new_default();
    ///
    /// ctx.set_global_var("xyz",
    ///     &VValFun::new_fun(
    ///         move |env: &mut Env, argc: usize| {
    ///             let ok = false;
    ///             if !ok {
    ///                 Ok(env.new_err(
    ///                     format!("Something was not ok!")))
    ///             } else {
    ///                 Ok(VVal::Bol(true))
    ///             }
    ///         }, None, None, false));
    ///```
    pub fn new_err(&self, s: String) -> VVal {
        for i in self.call_stack.iter().rev() {
            if i.syn_pos.is_some() {
                return
                    VVal::err(
                        VVal::new_str_mv(s),
                        i.syn_pos.clone().unwrap());
            }
        };

        VVal::err(
            VVal::new_str_mv(s),
            self.call_stack.last().unwrap().syn_pos.clone().or_else(
                || Some(SynPos { syn: Syntax::Block, line: 0,
                                 col: 0, file: FileRef::new("?"),
                                 name: None })).unwrap())
    }
}

/// Encodes all kinds of jumps up the call stack, like `break` and `next` in Loops.
///
/// As WLambda is not using a VM, it uses return values of the
/// closure call tree to handle jumping up the stack.
#[derive(Clone)]
pub enum StackAction {
    Panic(Box<(VVal, Vec<Option<SynPos>>)>),
    Return(Box<(VVal, VVal)>),
    Break(Box<VVal>),
    Next,
}

impl Display for StackAction {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        match self {
            StackAction::Panic(panic) => {
                let stk : Vec<String> =
                    panic.1.iter().map(|s|
                        if let Some(p) = s { format!("{}", p) }
                        else { String::from("[?]") })
                    .collect();
                write!(f, "{} SA::Panic({})", stk.join("=>"), panic.0.s())
            },
            StackAction::Return(ret) => write!(f, "SA::Return(lbl={},{})", ret.0.s(), ret.1.s()),
            StackAction::Break(v) => write!(f, "SA::Break({})", v.s()),
            StackAction::Next     => write!(f, "SA::Next"),
        }
    }
}

impl Debug for StackAction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl StackAction {
    pub fn panic_borrow(v: &VVal) -> Self {
        Self::panic_msg(format!("Can't mutate borrowed value: {}", v.s()))
    }

    pub fn panic_msg(err: String) -> Self {
        let v = Vec::new();
        StackAction::Panic(Box::new((VVal::new_str_mv(err), v)))
    }
    pub fn panic_str(err: String, sp: Option<SynPos>) -> Self {
        let mut v = Vec::new();
        v.push(sp);
        StackAction::Panic(Box::new((VVal::new_str_mv(err), v)))
    }
    pub fn panic(err: VVal, sp: Option<SynPos>) -> Self {
        let mut v = Vec::new();
        v.push(sp);
        StackAction::Panic(Box::new((err, v)))
    }
    pub fn wrap_panic(self, sp: Option<SynPos>) -> Self {
        match self {
            StackAction::Panic(mut panic) => {
                panic.as_mut().1.push(sp);
                StackAction::Panic(panic)
            },
            _ => self,
        }
    }
}

impl From<VVal> for StackAction {
    fn from(v: VVal) -> StackAction {
        StackAction::panic(v, None)
    }
}

/// Position of a variable represented in the `CompileEnv`.
#[derive(Debug, Clone)]
pub enum VarPos {
    /// No position of the variable. Mostly placeholder value for non existing variable.
    NoPos,
    /// Variable is stored in upvalue at the specified position.
    UpValue(usize),
    /// Variable is stored in local variables on the stack at the specified position.
    Local(usize),
    /// Variable is stored in the global variables with the given value.
    Global(VVal),
    /// A constant value, major difference to Global is, that it is not a reference
    /// and a slight bit faster.
    Const(VVal),
}

pub type EvalNode = Box<dyn Fn(&mut Env) -> Result<VVal,StackAction>>;
pub type ClosNodeRef = Rc<RefCell<dyn Fn(&mut Env, usize) -> Result<VVal,StackAction>>>;

#[derive(Clone)]
pub enum FunType {
    ClosureNode(ClosNodeRef),
    VMProg(Rc<Prog>),
}

#[derive(Clone)]
/// This structure is the runtime representation of a WLambda function value.
pub struct VValFun {
    /// The closure or vm program that runs the function.
    pub fun:        FunType,
    /// The positions of the upvalues that are being captured by this function.
    pub upvalue_pos: Rc<std::vec::Vec<VarPos>>,
    /// Contains any caught upvalues.
    pub upvalues:   std::vec::Vec<VVal>,
    /// The number of local variables defined in this functions.
    ///
    /// This value is used to reserve stack space for storing them.
    pub local_size: usize,
    /// Min number of arguments this functions requires.
    pub min_args:   Option<usize>,
    /// Max number of arguments this functions requires.
    pub max_args:   Option<usize>,
    /// If true, then this function accepts error values without panic.
    /// Functions by default don't accept errors as argument. It needs to be
    /// explicitly enabled.
    pub err_arg_ok: bool,
    /// The location of the definition of this function.
    pub syn_pos:    Option<SynPos>,
    /// The return label of the function:
    pub label:      VVal,
}

impl VValFun {
    /// Creates a new VVal containing the given closure with the given minimum
    /// and maximum parameters (see also [`add_func` of GlobalEnv](compiler/struct.GlobalEnv.html#method.add_func)).
    ///
    /// The `err_arg_ok` parameter specifies whether the function accepts
    /// error values as arguments. If it doesn't, the program will panic
    /// once an error value is encountered. This makes programs more maintainable.
    ///
    /// This is usually useful if you want to add functions to the [EvalContext](compiler/struct.EvalContext.html).
    /// at runtime.
    ///
    ///```rust
    /// use wlambda::compiler::EvalContext;
    /// use wlambda::vval::{VVal, VValFun, Env};
    ///
    /// let mut ctx = wlambda::compiler::EvalContext::new_empty_global_env();
    ///
    /// ctx.set_global_var("xyz",
    ///     &VValFun::new_fun(
    ///         move |env: &mut Env, argc: usize| {
    ///             Ok(VVal::new_str("xyz"))
    ///         }, None, None, false));
    ///
    /// assert_eq!(ctx.eval("xyz[]").unwrap().s_raw(), "xyz")
    ///```
    pub fn new_fun<T>(fun: T, min_args: Option<usize>, max_args: Option<usize>, err_arg_ok: bool) -> VVal
        where T: 'static + Fn(&mut Env, usize) -> Result<VVal, StackAction> {

        VValFun::new_val(Rc::new(RefCell::new(fun)), Vec::new(), 0, min_args, max_args, err_arg_ok, None, Rc::new(vec![]))
    }

    pub fn new_fun_with_pos<T>(fun: T, min_args: Option<usize>, max_args: Option<usize>, err_arg_ok: bool, spos: SynPos) -> VVal
        where T: 'static + Fn(&mut Env, usize) -> Result<VVal, StackAction> {

        VValFun::new_val(Rc::new(RefCell::new(fun)), Vec::new(), 0, min_args, max_args, err_arg_ok, Some(spos), Rc::new(vec![]))
    }

    /// Internal utility function. Use at your own risk, API might change.
    #[allow(clippy::too_many_arguments)]
    pub fn new_val(fun: ClosNodeRef, upvalues: std::vec::Vec<VVal>,
                   env_size: usize, min_args: Option<usize>,
                   max_args: Option<usize>, err_arg_ok: bool, syn_pos: Option<SynPos>,
                   upvalue_pos: Rc<std::vec::Vec<VarPos>>) -> VVal {
        VVal::Fun(Rc::new(VValFun {
            upvalue_pos,
            upvalues,
            fun: FunType::ClosureNode(fun),
            local_size: env_size,
            min_args,
            max_args,
            err_arg_ok,
            syn_pos,
            label: VVal::None,
        }))
    }

    /// Internal utility function. Use at your own risk, API might change.
    #[allow(clippy::too_many_arguments)]
    pub fn new_prog(prog: Rc<Prog>, upvalues: std::vec::Vec<VVal>,
                   env_size: usize, min_args: Option<usize>,
                   max_args: Option<usize>, err_arg_ok: bool, syn_pos: Option<SynPos>,
                   upvalue_pos: Rc<std::vec::Vec<VarPos>>,
                   label: VVal) -> VVal {
        VVal::Fun(Rc::new(VValFun {
            upvalue_pos,
            upvalues,
            fun: FunType::VMProg(prog),
            local_size: env_size,
            min_args,
            max_args,
            err_arg_ok,
            syn_pos,
            label
        }))
    }

    /// Returns a dummy function that does nothing.
    pub fn new_dummy() -> Rc<VValFun> {
        Rc::new(VValFun {
            fun:         FunType::ClosureNode(Rc::new(RefCell::new(|_: &mut Env, _a: usize| { Ok(VVal::None) }))),
            upvalue_pos: Rc::new(vec![]),
            upvalues:    Vec::new(),
            local_size:  0,
            min_args:    None,
            max_args:    None,
            err_arg_ok:  false,
            syn_pos:     None,
            label:       VVal::None,
        })
    }

    /// Dumps captured up values of this function. Useful only if you want to
    /// debug functions/closures creates by WLambda code.
    pub fn dump_upvals(&self) -> VVal {
        let v = VVal::vec();
        for uv in self.upvalues.iter() {
            v.push(uv.clone());
        }
        v
    }
}

/// You can implement your own VVal data type and provide it
/// via global functions:
///
///```
/// use std::rc::Rc;
/// use std::cell::RefCell;
/// use wlambda::vval::Env;
/// use wlambda::{VVal, GlobalEnv, StackAction};
///
/// #[derive(Clone, Debug)]
/// struct MyType {
///     x: Rc<RefCell<(i64, i64)>>,
/// }
///
/// impl wlambda::vval::VValUserData for MyType {
///     fn s(&self) -> String { format!("$<MyType({:?})>", self.x.borrow()) }
///     fn i(&self) -> i64    { self.x.borrow_mut().1 }
///     fn get_key(&self, key: &str) -> Option<VVal> {
///         Some(VVal::new_str(key))
///     }
///     fn call_method(&self, key: &str, env: &mut Env) -> Result<VVal, StackAction> {
///         let args = env.argv_ref();
///         match key {
///             "test" => Ok(VVal::Int(42)),
///             _ => Ok(VVal::err_msg(&format!("Unknown method called: {}", key))),
///         }
///     }
///     fn call(&self, env: &mut Env) -> Result<VVal, StackAction> {
///         let args = env.argv_ref();
///         if args.len() < 0 {
///             return Err(StackAction::panic_msg(
///                 format!("{} called with too few arguments", self.s())));
///         }
///         Ok(args[0].clone())
///     }
///     fn as_any(&mut self) -> &mut dyn std::any::Any { self }
///     fn clone_ud(&self) -> Box<dyn wlambda::vval::VValUserData> {
///         Box::new(self.clone())
///     }
/// }
///
/// let global_env = GlobalEnv::new_default();
/// global_env.borrow_mut().add_func(
///     "new_mytype",
///     |_env: &mut Env, _argc: usize| {
///         Ok(VVal::Usr(Box::new(MyType { x: Rc::new(RefCell::new((13, 42))) })))
///     }, Some(0), Some(0));
///
/// global_env.borrow_mut().add_func(
///     "modify_mytype",
///     |env: &mut Env, _argc: usize| {
///         Ok(if let VVal::Usr(mut u) = env.arg(0) {
///             if let Some(ud) = u.as_any().downcast_mut::<MyType>() {
///                 ud.x.borrow_mut().0 += 1;
///                 ud.x.borrow_mut().1 *= 2;
///                 VVal::Int(ud.x.borrow().0 + ud.x.borrow().1)
///             } else {
///                 VVal::None
///             }
///         } else { VVal::None })
///     }, Some(1), Some(1));
///
/// let mut ctx = wlambda::compiler::EvalContext::new(global_env);
///
/// let r = &mut ctx.eval(r#"
///     !x = new_mytype[];
///     !i = modify_mytype x;
///     $[i, x]
/// "#).unwrap();
///
/// assert_eq!(
///     r.s(), "$[98,$<MyType((14, 84))>]", "Userdata implementation works");
///```

/// Sometimes, if your UserData is a Rc<RefCell<...>>, it can pay off defining
/// some wrapper and From/Into traits for easier handling:
///
/// Here an example from a game I worked on:
///```
/// use wlambda::vval::{VVal, StackAction, VValUserData};
/// use std::rc::Rc;
/// use std::cell::RefCell;
///
/// #[derive(Clone)]
/// struct Ship { }
///
/// #[derive(Clone)]
/// struct ShipWlWrapper(Rc<RefCell<Ship>>);
///
/// impl From<Rc<RefCell<Ship>>> for ShipWlWrapper {
///     fn from(r: Rc<RefCell<Ship>>) -> ShipWlWrapper {
///         ShipWlWrapper(r)
///     }
/// }
///
/// impl Into<VVal> for ShipWlWrapper {
///     fn into(self) -> VVal { VVal::Usr(Box::new(self)) }
/// }
///
/// impl VValUserData for ShipWlWrapper {
///     // ...
///     fn as_any(&mut self) -> &mut dyn std::any::Any { self }
///     fn clone_ud(&self) -> Box<dyn wlambda::vval::VValUserData> {
///         Box::new(self.clone())
///     }
/// }
///```

#[allow(clippy::borrowed_box)]
pub trait VValUserData {
    /// This method should return a human readable syntax representation
    /// of your VValUserData.
    fn s(&self)     -> String { format!("$<userdata:{:p}>", self) }
    /// If your data has a plain string representation,
    /// you can return the string directly from here.
    fn s_raw(&self) -> String { self.s() }
    /// Returns the i64 representation of your data.
    fn i(&self)     -> i64    { -1 }
    /// Returns the f64 representation of your data.
    fn f(&self)     -> f64    { self.i() as f64 }
    /// Returns the boolean representation of your data. Can for instance
    /// be used to check if your data is _valid_ or something.
    fn b(&self)     -> bool   { true }
    /// Allows you to specify how two instances of your data
    /// should be compared for equivalentness.
    fn eqv(&self, _other: &Box<dyn VValUserData>) -> bool { false }
    /// Should clone your user data instance. Whether you are doing
    /// a deep clone or a shallow clone or something else is up to you.
    fn clone_ud(&self) -> Box<dyn VValUserData>;
    /// Makes your user data act like a map. This can be useful
    /// for implementing your own registries or data structures.
    /// Implement this method for setting a key to a value.
    fn set_key(&self, _key: &VVal, _val: VVal) -> Result<(), StackAction> { Ok(()) }
    /// This method is called when the user wants to remove a key.
    /// Typically called when `std:delete` from the prelude is called.
    fn delete_key(&self, _key: &VVal) -> Result<VVal, StackAction> { Ok(VVal::None) }
    /// This method returns some value that your user data
    /// associates with the given key.
    fn get_key(&self, _key: &str) -> Option<VVal> { None }
    /// This method is called, when the user data object is used in a method call directly.
    /// Use this to implement convenient APIs for the user of the user data object.
    /// To quickly get the arguments you may use `env.argv_ref()`.
    fn call_method(&self, _key: &str, _env: &mut Env) -> Result<VVal, StackAction> { Ok(VVal::None) }
    /// This method is called when the user data is called.
    /// To quickly get the arguments you may use `env.argv_ref()`.
    fn call(&self, _env: &mut Env) -> Result<VVal, StackAction> { Ok(VVal::None) }
    /// This should be implemented simply by returning
    /// a mutable reference to the concrete type self.
    /// It allows you to access your data structure from inside
    /// a function yourself.
    ///
    /// This is a good default implementation for your struct/type:
    ///
    ///```rust,no_run,compile_fail
    /// fn as_any(&mut self) -> &mut dyn std::any::Any { self }
    ///```
    fn as_any(&mut self) -> &mut dyn std::any::Any;
}

impl std::fmt::Debug for dyn VValUserData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.s())
    }
}

impl std::clone::Clone for Box<dyn VValUserData> {
    fn clone(&self) -> Self {
        (**self).clone_ud()
    }
}

/// Handles calling of destructor functions.
#[derive(Debug, Clone)]
pub struct DropVVal {
    pub v:      VVal,
    pub fun:    VVal,
}

impl Drop for DropVVal {
    #[allow(unused_must_use)]
    fn drop(&mut self) {
        let global = GlobalEnv::new_default();
        let mut e = Env::new(global);
        e.push(self.v.clone());
        if let Err(e) = self.fun.call_internal(&mut e, 1) {
            eprintln!("Error in drop function: {}", e);
        }
    }
}

/// VVal aka. VariantValue is a data structure to represent
/// all kinds of WLambda data structures.
///
/// It's used for the AST, for internal data and for runtime data structures.
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum VVal {
    /// The none value, the default value of all non initialized data.
    None,
    /// The err value is a special sentinel value for representing any kind of
    /// application error condition. It's created using the special $e <expr> or $error <expr>
    /// syntax.
    Err(Rc<RefCell<(VVal, SynPos)>>),
    /// Representation of a boolean value.
    Bol(bool),
    /// Representation of an interned string aka symbol or key.
    Sym(Symbol),
    /// Representation of a unicode/text string.
    Str(Rc<String>),
    /// Representation of a byte buffer.
    Byt(Rc<Vec<u8>>),
    /// Integer value
    Int(i64),
    /// Float value
    Flt(f64),
    /// A syntax node in the AST, records the position too.
    Syn(SynPos),
    /// A pair
    Pair(Rc<(VVal, VVal)>),
    /// An optional value. While VVal::None basically has the same meaning
    /// as "no value", an optional value provides functions a way to say
    /// that they don't even return none value but nothing. Yes, it sounds
    /// weird, and it is weird. But in case of iterator functions that
    /// iterate over an array you can find out whether the iterator is at
    /// the end or will provide more values.
    Opt(Option<Rc<VVal>>),
    /// A special internal iterator type for built in VVal data structures.
    Iter(Rc<RefCell<VValIter>>),
    /// A list (or vector) of VVals.
    Lst(Rc<RefCell<std::vec::Vec<VVal>>>),
    /// A mapping of strings to VVals.
    Map(Rc<RefCell<FnvHashMap<Symbol, VVal>>>),
    /// A function, see also [VValFun](struct.VValFun.html)
    Fun(Rc<VValFun>),
    /// A guarded VVal, that executes a given function when it is
    /// no longer referenced.
    DropFun(Rc<DropVVal>),
    /// A (strong) reference to a VVal.
    Ref(Rc<RefCell<VVal>>),
    /// A numerical (mathematical) vector storing integers. See NVec for more information.
    FVec(NVec<f64>),
    /// A numerical (mathematical) vector storing floats. See NVec for more information.
    IVec(NVec<i64>),
    /// A (still strong) reference to a VVal, which becomes a weak reference if
    /// captured by a closure.
    CRef(Rc<RefCell<VVal>>),
    /// A (weak) reference to a VVal. Might turn VVal::None anytime.
    WWRef(Weak<RefCell<VVal>>),
    /// A vval that can box some user data which can later be accessed
    /// from inside user supplied Rust functions via std::any::Any.
    Usr(Box<dyn VValUserData>),
}
impl PartialEq for VVal {
    fn eq(&self, rhs: &Self) -> bool {
        self.eqv(rhs)
    }
}

impl std::fmt::Debug for VValFun {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "&VValFun")
    }
}

pub fn format_vval_str(s: &str, narrow_ascii: bool) -> String {
    let mut v : Vec<String> = s.chars().map(|c| {
        match c {
            '\\' => { String::from("\\\\") },
            '\n' => { String::from("\\n")  },
            '\t' => { String::from("\\t")  },
            '\r' => { String::from("\\r")  },
            '\0' => { String::from("\\0")  },
            '\'' => { String::from("\\'")  },
            '\"' => { String::from("\\\"") },
            _ if narrow_ascii
                 && c.is_ascii()
                 && (c.is_ascii_alphanumeric()
                     || c.is_ascii_graphic()
                     || c.is_ascii_punctuation()
                     || c == ' ') => { format!("{}", c) },
            _ if narrow_ascii => { format!("\\x{:02X}", c as u32) },
            _ if !narrow_ascii && c.is_ascii_control() => { format!("\\x{:02x}", c as u32) },
            _ if !narrow_ascii && c.is_control() => { c.escape_unicode().to_string() },
            _ => { format!("{}", c) }

        }
    }).collect();
    v.insert(0, String::from("\""));
    v.push(String::from("\""));
    v.concat()
}

pub fn format_vval_byt(v: &[u8]) -> String {
    let mut s = String::from("");
    for b in v.iter() {
        s.push(*b as char);
    }
    format_vval_str(&s, true)
}

struct CycleCheck {
    refs: FnvHashMap<i64, i64>,
    backref_counter: i64,
}

impl CycleCheck {
    fn new() -> Self {
        CycleCheck {
            refs: FnvHashMap::with_capacity_and_hasher(2, Default::default()),
            backref_counter: 1,
        }
    }

    fn touch_walk(&mut self, v: &VVal) {
        if self.touch(v).is_some() { return; }

        match v {
            VVal::Err(e) => self.touch_walk(&(*e).borrow().0),
            VVal::Pair(b) => {
                self.touch_walk(&b.0);
                self.touch_walk(&b.1);
            },
            VVal::Opt(b) => {
                if let Some(x) = b {
                    self.touch_walk(x);
                }
            },
            VVal::Lst(l) => {
                for v in l.borrow().iter() { self.touch_walk(v); }
            },
            VVal::Map(l) => {
                for (_k, v) in l.borrow().iter() { self.touch_walk(&v); }
            },
            VVal::DropFun(f) => { self.touch_walk(&f.v); },
            VVal::Fun(f) => {
                for v in f.upvalues.iter() {
                    self.touch_walk(v);
                }
            },
            VVal::Ref(l) => { self.touch_walk(&(*l).borrow()); },
            VVal::CRef(l) => { self.touch_walk(&(*l).borrow()); },
            VVal::WWRef(l) => {
                if let Some(v) = l.upgrade() {
                    self.touch_walk(&(*v).borrow());
                }
            },
              VVal::Str(_)
            | VVal::Byt(_)
            | VVal::None
            | VVal::Bol(_)
            | VVal::Sym(_)
            | VVal::Syn(_)
            | VVal::Iter(_)
            | VVal::FVec(_)
            | VVal::IVec(_)
            | VVal::Int(_)
            | VVal::Flt(_)
            | VVal::Usr(_) => {},
        }
    }

    fn touch(&mut self, v: &VVal) -> Option<i64> {
        let id = v.ref_id()?;

        if let Some(backref) = self.refs.get(&id) {
            if *backref == 0 {
                let bkr_count = self.backref_counter;
                self.backref_counter += 1;
                self.refs.insert(id, bkr_count);
                Some(bkr_count)
            } else {
                Some(*backref)
            }
        } else {
            self.refs.insert(id, 0);
            None
        }
    }

    fn backref(&mut self, v: &VVal) -> Option<(bool, String)> {
        // Do not generate back refs for symbols. they are interned
        // anyways!
        if let VVal::Sym(_) = v { return None; }

        let id = v.ref_id()?;

        if let Some(backref) = self.refs.get(&id) {
            match *backref {
                br if br > 0 => {
                    self.refs.insert(id, -br);
                    Some((true, format!("$<{}=>", br)))
                },
                br if br < 0 =>
                    Some((false, format!("$<{}>", -br))),
                _ => None,
            }
        } else {
            None
        }
    }
}

pub type VValIter =
    std::iter::FromFn<Box<dyn FnMut() -> Option<(VVal, Option<VVal>)>>>;

macro_rules! swizzle_char2value {
    ($c: expr, $i: expr, $x: ident, $y: ident, $z: ident, $w: ident) => (
        match $c.chars().nth($i).unwrap_or(' ') {
            'r' => $x,
            'g' => $y,
            'b' => $z,
            'a' => $w,
            'h' => $x,
            's' => $y,
            'v' => $z,
            '0' => $x,
            '1' => $y,
            '2' => $z,
            '3' => $w,
            'x' => $x,
            'y' => $y,
            'z' => $z,
            'w' => $w,
            _ => return VVal::None,
        }
    )
}

#[allow(clippy::many_single_char_names)]
fn swizzle_i(s: &str, x: i64, y: i64, z: i64, w: i64) -> VVal {
    match s.len() {
        2 =>
            VVal::IVec(NVec::Vec2(
                swizzle_char2value!(s, 0, x, y, z, w),
                swizzle_char2value!(s, 1, x, y, z, w))),
        3 =>
            VVal::IVec(NVec::Vec3(
                swizzle_char2value!(s, 0, x, y, z, w),
                swizzle_char2value!(s, 1, x, y, z, w),
                swizzle_char2value!(s, 2, x, y, z, w))),
        4 =>
            VVal::IVec(NVec::Vec4(
                swizzle_char2value!(s, 0, x, y, z, w),
                swizzle_char2value!(s, 1, x, y, z, w),
                swizzle_char2value!(s, 2, x, y, z, w),
                swizzle_char2value!(s, 3, x, y, z, w))),
        _ => VVal::None,
    }
}

#[allow(clippy::many_single_char_names)]
fn swizzle_f(s: &str, x: f64, y: f64, z: f64, w: f64) -> VVal {
    match s.len() {
        2 =>
            VVal::FVec(NVec::Vec2(
                swizzle_char2value!(s, 0, x, y, z, w),
                swizzle_char2value!(s, 1, x, y, z, w))),
        3 =>
            VVal::FVec(NVec::Vec3(
                swizzle_char2value!(s, 0, x, y, z, w),
                swizzle_char2value!(s, 1, x, y, z, w),
                swizzle_char2value!(s, 2, x, y, z, w))),
        4 =>
            VVal::FVec(NVec::Vec4(
                swizzle_char2value!(s, 0, x, y, z, w),
                swizzle_char2value!(s, 1, x, y, z, w),
                swizzle_char2value!(s, 2, x, y, z, w),
                swizzle_char2value!(s, 3, x, y, z, w))),
        _ => VVal::None,
    }
}

macro_rules! iter_next {
    ($i: expr) => {
        if let Some((v, k)) = $i.next() {
            if let Some(k) = k {
                VVal::opt(VVal::pair(v, k))
            } else {
                VVal::opt(v)
            }
        } else {
            VVal::opt_none()
        }
    }
}

macro_rules! iter_next_value {
    ($i: expr, $v: ident, $conv: block, $def: expr) => {
        if let Some(($v, _)) = $i.next() { $conv } else { $def }
    }
}


macro_rules! pair_key_to_iter {
    ($p: ident) => {
        if let VVal::Iter(ai) = &$p.0 {
            let ai = ai.clone();

            if let VVal::Iter(bi) = &$p.1 {
                let bi = bi.clone();

                std::iter::from_fn(Box::new(move || {
                    let a = ai.borrow_mut().next();
                    if let Some((a, ak)) = a {
                        let a =
                            if let Some(ak) = ak {
                                VVal::pair(a, ak)
                            } else {
                                a
                            };

                        let b = bi.borrow_mut().next();
                        if let Some((b, bk)) = b {
                            let b =
                                if let Some(bk) = bk {
                                    VVal::pair(b, bk)
                                } else {
                                    b
                                };

                            Some((a, Some(b)))
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                }))
            } else {
                let mut bi = $p.1.iter();
                std::iter::from_fn(Box::new(move || {
                    let a = ai.borrow_mut().next();
                    if let Some((a, ak)) = a {
                        let a =
                            if let Some(ak) = ak {
                                VVal::pair(a, ak)
                            } else {
                                a
                            };

                        if let Some((b, bk)) = bi.next() {
                            let b =
                                if let Some(bk) = bk {
                                    VVal::pair(b, bk)
                                } else {
                                    b
                                };

                            Some((VVal::pair(a, b), None))
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                }))
            }
        } else {
            $p.0.with_s_ref(|key: &str| -> VValIter {
                let l =
                    match &key[..] {
                        "keys"      => $p.1.keys(),
                        "values"    => $p.1.values(),
                        "enumerate" => $p.1.enumerate(),
                        _ => {
                            return std::iter::from_fn(
                                Box::new(move || { None }))
                        }
                    };
                if let VVal::Lst(l) = l {
                    let l = l.clone();
                    let mut idx = 0;
                    std::iter::from_fn(Box::new(move || {
                        if idx >= l.borrow().len() {
                            return None;
                        }
                        let r = Some((l.borrow()[idx].clone(), None));
                        idx += 1;
                        r
                    }))
                } else {
                    std::iter::from_fn(Box::new(move || { None }))
                }
            })
        }
    }
}

fn range_extract(from: i64, cnt: i64, val: &VVal) -> VVal {
    match val {
        VVal::Byt(s) => {
            VVal::new_byt(
                s.iter()
                 .skip(from as usize)
                 .take(cnt as usize).copied().collect())
        },
        VVal::Str(s) => {
            VVal::new_str_mv(
                s.chars()
                 .skip(from as usize)
                 .take(cnt as usize).collect())
        },
        _ => VVal::None,
    }
}


fn pair_extract(a: &VVal, b: &VVal, val: &VVal) -> VVal {
    match val {
        VVal::Int(i) =>
            if i % 2 == 0 { a.clone() }
            else          { b.clone() },
        VVal::Byt(s) => {
            match (a, b) {
                (VVal::Int(from), VVal::Int(cnt)) => {
                    range_extract(*from, *cnt, val)
                },
                (VVal::Byt(splitstr), VVal::Int(max)) => {
                    let out      = VVal::vec();
                    let splitstr = &splitstr.as_ref()[..];
                    let len      = s.len();
                    let ss_len   = splitstr.len();
                    let inp      = &s.as_ref()[..];

                    if ss_len > len {
                        out.push(VVal::new_byt(inp[..].to_vec()));
                    } else {
                        let mut i            = 0;
                        let mut last_split_i = 0;

                        while i < len {
                            if i + ss_len > len { break; }
                            if inp[i..(i + ss_len)] == *splitstr {
                                out.push(
                                    VVal::new_byt(
                                        inp[last_split_i..i]
                                        .to_vec()));

                                i += ss_len;
                                last_split_i = i;

                                if *max > 0
                                   && (out.len() + 1) >= *max as usize
                                {
                                    out.push(
                                        VVal::new_byt(
                                            inp[last_split_i..len]
                                            .to_vec()));
                                    i += inp[last_split_i..len].len();
                                    last_split_i = i + 1; // total end!
                                    break;
                                }
                            } else {
                                i += 1;
                            }
                        }

                        match last_split_i {
                            ls if ls < i  => {
                                out.push(
                                    VVal::new_byt(
                                        inp[last_split_i..len]
                                        .to_vec()));
                            },
                            ls if ls == i => {
                                out.push(VVal::new_byt(vec![]));
                            },
                            _ => (),
                        }
                    }

                    out
                },
                (VVal::Byt(needle), VVal::Byt(replace)) => {
                    let inp        = s.as_ref();
                    let needle     = needle.as_ref();
                    let needle_len = needle.len();

                    if needle_len > inp.len() {
                        VVal::Byt(s.clone())
                    } else {
                        let mut out : Vec<u8>
                            = Vec::with_capacity(inp.len());

                        let mut i = 0;
                        while i < inp.len() {
                            if    i <= (inp.len() - needle_len)
                               && inp[i..(i + needle_len)] == needle[0..needle_len]
                            {
                                out.extend_from_slice(&replace.as_ref()[..]);
                                i += needle_len;
                            } else {
                                out.push(inp[i]);
                                i += 1;
                            }
                        }

                        VVal::new_byt(out)
                    }
                },
                _ => VVal::None
            }
        },
        VVal::Str(s) => {
            match (a, b) {
                (VVal::Int(from), VVal::Int(cnt)) => {
                    range_extract(*from, *cnt, val)
                },
                (VVal::Str(splitstr), VVal::Int(max)) => {
                    let l = VVal::vec();
                    if *max > 0 {
                        for part in s.as_ref().splitn(*max as usize, splitstr.as_ref()) {
                            l.push(VVal::new_str(part));
                        }
                    } else {
                        for part in s.as_ref().split(splitstr.as_ref()) {
                            l.push(VVal::new_str(part));
                        }
                    }
                    l
                },
                (VVal::Str(needle), VVal::Str(replace)) => {
                    VVal::new_str_mv(
                        s.as_ref()
                         .replace(
                            needle.as_ref(), replace.as_ref()))
                },
                _ => VVal::None
            }
        },
        _ => VVal::None
    }
}

fn vval_rc_ptr_eq(v: &VVal, l: &Rc<RefCell<VVal>>) -> bool {
    match v {
        VVal::Ref(r2) => Rc::ptr_eq(l, r2),
        VVal::CRef(r2) => Rc::ptr_eq(l, r2),
        VVal::WWRef(r2) =>
            match r2.upgrade() {
                Some(v2) => Rc::ptr_eq(l, &v2),
                None => false,
            },
        _ => false,
    }
}

#[allow(dead_code)]
impl VVal {
    #[inline]
    pub fn new_str(s: &str) -> VVal {
        VVal::Str(Rc::new(String::from(s)))
    }

    #[inline]
    pub fn new_str_mv(s: String) -> VVal {
        VVal::Str(Rc::new(s))
    }

    #[inline]
    pub fn new_sym(s: &str) -> VVal {
        VVal::Sym(s2sym(s))
    }

    #[inline]
    pub fn new_sym_mv(s: String) -> VVal {
        VVal::Sym(new_sym_mv(s))
    }

    #[inline]
    pub fn new_byt(v: Vec<u8>) -> VVal {
        VVal::Byt(Rc::new(v))
    }

    #[inline]
    pub fn err(v: VVal, pos: SynPos) -> VVal {
        VVal::Err(Rc::new(RefCell::new((v, pos))))
    }

    pub fn err_msg(s: &str) -> VVal {
        VVal::Err(Rc::new(RefCell::new(
            (VVal::new_str(s),
             SynPos { syn: Syntax::Block, line: 0,
                      col: 0, file: FileRef::new("?"), name: None }))))
    }

    #[inline]
    pub fn vec() -> VVal {
        VVal::Lst(Rc::new(RefCell::new(Vec::new())))
    }

    #[inline]
    pub fn vec1(a: VVal) -> VVal {
        let v = Self::vec();
        v.push(a);
        v
    }

    #[inline]
    pub fn vec2(a: VVal, b: VVal) -> VVal {
        let v = Self::vec();
        v.push(a);
        v.push(b);
        v
    }

    #[inline]
    pub fn vec3(a: VVal, b: VVal, c: VVal) -> VVal {
        let v = Self::vec();
        v.push(a);
        v.push(b);
        v.push(c);
        v
    }

    #[inline]
    pub fn opt(v: VVal) -> VVal {
        VVal::Opt(Some(Rc::new(v)))
    }

    #[inline]
    pub fn opt_none() -> VVal { VVal::Opt(None) }

    #[inline]
    pub fn pair(a: VVal, b: VVal) -> VVal {
        VVal::Pair(Rc::new((a, b)))
    }

    pub fn to_vec(&self) -> Vec<VVal> {
        if let VVal::Lst(l) = self {
            let r : Vec<VVal> = l.borrow_mut().iter().cloned().collect();
            r
        } else {
            vec![self.clone()]
        }
    }

    pub fn vec_from(vl: &[VVal]) -> VVal {
        let mut v = Vec::new();
        v.extend_from_slice(vl);
        VVal::Lst(Rc::new(RefCell::new(v)))
    }

    pub fn vec_mv(v: Vec<VVal>) -> VVal {
        VVal::Lst(Rc::new(RefCell::new(v)))
    }

    pub fn call(&self, env: &mut Env, args: &[VVal]) -> Result<VVal, StackAction> {
        let argc = args.len();
        for v in args.iter() {
            env.push(v.clone());
        }
        let ret = self.call_internal(env, argc);
        env.popn(argc);
        ret
    }

    pub fn compare_str(&self, b: &VVal) -> std::cmp::Ordering {
        self.with_s_ref(|a: &str| b.with_s_ref(|b: &str| a.cmp(b)))
    }

    pub fn shallow_clone(&self) -> VVal {
        match self {
            VVal::Lst(l) => {
                let out = VVal::vec();
                for v in l.borrow().iter() { out.push(v.clone()); }
                out
            },
            VVal::Map(m) => {
                let out = VVal::map();
                for (k, v) in m.borrow_mut().iter() {
                    if out.set_key_sym(k.clone(), v.clone()).is_err() {
                        continue;
                    }
                }
                out
            },
            VVal::Str(s) => {
                VVal::new_str_mv(s.as_ref().clone())
            },
            v => v.with_deref(
                |v| v.shallow_clone(),
                |v| if let Some(v) = v { v.clone() }
                    else { VVal::None }),
        }
    }

    pub fn compare_num(&self, b: &VVal) -> std::cmp::Ordering {
        if self.is_float() {
            self.f().partial_cmp(&b.f())
                .unwrap_or(std::cmp::Ordering::Greater)
        } else {
            self.i().cmp(&b.i())
        }
    }

    pub fn sort<F>(&self, compare: F)
        where F: FnMut(&VVal, &VVal) -> std::cmp::Ordering
    {
        if let VVal::Lst(v) = self {
            v.borrow_mut().sort_by(compare);
        }
    }

    pub fn fisher_yates_shuffle<I>(&mut self, mut rand: I)
        where I: FnMut() -> i64
    {
        if let VVal::Lst(list) = self {
            let len = list.borrow().len();
            if len <= 1 { return; }

            for k in 1..len {
                let i = len - k;
                let j = rand().abs() as usize % (i + 1);
                list.borrow_mut().swap(j, i);
            }
        }
    }

    #[allow(clippy::while_let_on_iterator)]
    pub fn keys(&self) -> VVal {
        match self {
            VVal::Map(m) => {
                let out = VVal::vec();
                for (k, _) in m.borrow_mut().iter() {
                    out.push(VVal::new_str_mv(k.to_string()));
                }
                out
            },
            VVal::Iter(i) => {
                let out = VVal::vec();
                let mut i = i.borrow_mut();
                while let Some((_, k)) = i.next() {
                    if let Some(k) = k {
                        out.push(k);
                    }
                }
                out
            },
            VVal::Lst(l) => {
                let out = VVal::vec();
                for (i, _) in l.borrow_mut().iter().enumerate() {
                    out.push(VVal::Int(i as i64));
                }
                out
            },
            v => v.with_deref(
                |v| v.keys(),
                |_| VVal::None),
        }
    }

    #[allow(clippy::while_let_on_iterator)]
    pub fn values(&self) -> VVal {
        match self {
            VVal::Map(m) => {
                let out = VVal::vec();
                for (_, v) in m.borrow_mut().iter() {
                    out.push(v.clone());
                }
                out
            },
            VVal::Lst(l) => {
                let out = VVal::vec();
                for v in l.borrow_mut().iter() {
                    out.push(v.clone());
                }
                out
            },
            VVal::Iter(i) => {
                let out = VVal::vec();
                let mut i = i.borrow_mut();
                while let Some((v, _)) = i.next() {
                    out.push(v);
                }
                out
            },
            v => v.with_deref(
                |v| v.values(),
                |_| VVal::None),
        }
    }

    #[allow(clippy::while_let_on_iterator)]
    pub fn enumerate(&self) -> VVal {
        match self {
            VVal::Map(m) => {
                let out = VVal::vec();
                for (i, _) in m.borrow_mut().iter().enumerate() {
                    out.push(VVal::Int(i as i64));
                }
                out
            },
            VVal::Lst(l) => {
                let out = VVal::vec();
                for (i, _) in l.borrow_mut().iter().enumerate() {
                    out.push(VVal::Int(i as i64));
                }
                out
            },
            VVal::Iter(i) => {
                let out = VVal::vec();
                let mut i = i.borrow_mut();
                let mut c = 0;
                while let Some(_) = i.next() {
                    out.push(VVal::Int(c));
                    c += 1;
                }
                out
            },
            v => v.with_deref(
                |v| v.enumerate(),
                |_| VVal::None),
        }
    }

    /// Returns true if this vval is containing (multiple) other VVals.
    /// Usually true for: Maps, Vectors, Pairs and Opt.
    ///
    /// Helpful if you want to iterate through a data structure by recursively
    /// iterating over a vval. Without getting iterations over individual
    /// string characters.
    pub fn iter_over_vvals(&self) -> bool {
        self.with_deref(
            |v| {
                match v {
                    VVal::Lst(_) => true,
                    VVal::Map(_) => true,
                    VVal::Opt(_) => true,
                    _            => false,
                }
            },
            |v| {
                match v {
                    Some(VVal::Lst(_)) => true,
                    Some(VVal::Map(_)) => true,
                    Some(VVal::Opt(_)) => true,
                    _                  => false,
                }
            })
    }

    /// This function returns you an iterator over the VVal.
    /// It will iterate over data such as VVal::Str, VVal::Sym, VVal::Lst,
    /// VVal::Map and VVal::Byt.
    ///
    /// This functionality provides the `for` keyword/function in WLambda.
    ///
    /// ```
    /// use wlambda::*;
    ///
    /// let some_vec = VVal::vec();
    /// some_vec.push(VVal::Int(10));
    /// some_vec.push(VVal::Int(22));
    /// some_vec.push(VVal::Int(36));
    ///
    /// let mut sum = 0;
    /// for (v, _) in some_vec.iter() {
    ///     sum += v.i();
    /// }
    ///
    /// assert_eq!(sum, 68);
    /// ```
    pub fn iter(&self) -> VValIter {
        match self {
            VVal::Lst(l) => {
                let l = l.clone();
                let mut idx = 0;
                std::iter::from_fn(Box::new(move || {
                    if idx >= l.borrow().len() { return None; }
                    let r = Some((l.borrow()[idx].clone(), None));
                    idx += 1;
                    r
                }))
            },
            VVal::Map(m) => {
                let m = m.clone();
                let mut idx = 0;
                std::iter::from_fn(Box::new(move || {
                    let r = match m.borrow().iter().nth(idx) {
                        Some((k, v)) => {
                            Some((v.clone(), Some(VVal::new_str(k.as_ref()))))
                        },
                        None => None,
                    };
                    idx += 1;
                    r
                }))
            },
            VVal::Byt(b) => {
                let b = b.clone();
                let mut idx = 0;
                std::iter::from_fn(Box::new(move || {
                    if idx >= b.len() { return None; }
                    let r = Some((VVal::new_byt(vec![b[idx]]), None));
                    idx += 1;
                    r
                }))
            },
            VVal::Sym(s) => {
                let s = s.clone();
                let mut idx = 0;
                std::iter::from_fn(Box::new(move || {
                    let r = match s.chars().nth(idx) {
                        Some(chr) => Some((VVal::new_str_mv(chr.to_string()), None)),
                        None      => None,
                    };
                    idx += 1;
                    r
                }))
            },
            VVal::Str(s) => {
                let s = s.clone();
                let mut idx = 0;
                std::iter::from_fn(Box::new(move || {
                    match s[idx..].chars().next() {
                        Some(chr) => {
                            idx += chr.len_utf8();
                            Some((VVal::new_str_mv(chr.to_string()), None))
                        },
                        None      => None,
                    }
                }))
            },
            VVal::None => {
                std::iter::from_fn(Box::new(move || { None }))
            },
            VVal::Pair(p) => {
                pair_key_to_iter!(p)
            },
            VVal::IVec(NVec::Vec2(a, b)) => {
                let mut i = *a;
                let b = *b;
                std::iter::from_fn(Box::new(move || {
                    if i >= b { return None; }
                    let ret = Some((VVal::Int(i), None));
                    i += 1;
                    ret
                }))
            },
            VVal::IVec(NVec::Vec3(a, b, skip)) => {
                let mut i = *a;
                let b = *b;
                let skip = *skip;
                std::iter::from_fn(Box::new(move || {
                    if i >= b { return None; }
                    let ret = Some((VVal::Int(i), None));
                    i += skip;
                    ret
                }))
            },
            VVal::FVec(NVec::Vec2(a, b)) => {
                let mut i = *a;
                let b = *b;
                std::iter::from_fn(Box::new(move || {
                    if i >= b { return None; }
                    let r = Some((VVal::Flt(i), None));
                    i += 1.0;
                    r
                }))
            },
            VVal::FVec(NVec::Vec3(a, b, s)) => {
                let mut i = *a;
                let b = *b;
                let s = *s;
                std::iter::from_fn(Box::new(move || {
                    if i >= b { return None; }
                    let ret = Some((VVal::Flt(i), None));
                    i += s;
                    ret
                }))
            },
            VVal::Opt(Some(v)) => {
                let x = v.as_ref().clone();
                let mut used = false;
                std::iter::from_fn(Box::new(move || {
                    if used { return None; }
                    used = true;
                    Some((x.clone(), None))
                }))
            },
            VVal::Opt(None) => {
                std::iter::from_fn(Box::new(move || { None }))
            },
            _ => self.with_deref(
                |v| { v.iter() },
                |v| if let Some(v) = v {
                    let x = v.clone();
                    let mut used = false;
                    std::iter::from_fn(Box::new(move || {
                        if used { return None; }
                        used = true;
                        Some((x.clone(), None))
                    }))
                } else {
                    std::iter::from_fn(Box::new(move || { None }))
                })
        }
    }

    /// This method will disable all arity checks of the function in the VVal.
    /// Does nothing if the VVal is not a function.
    ///
    /// It is used for disabling checks of drop functions, as they are
    /// evaluated in their own environment and there is no proper way to report
    /// errors upwards.
    pub fn disable_function_arity(&self) -> VVal {
        if let VVal::Fun(fu) = self {
            let mut new_fu = fu.as_ref().clone();
            new_fu.min_args = None;
            new_fu.max_args = None;
            VVal::Fun(Rc::new(new_fu))
        } else {
            self.clone()
        }
    }

    /// This function is an internal function that clones a function reference
    /// and assigns new upvalues to it for making a new closure instance.
    pub fn clone_and_rebind_upvalues<T>(&self, f: T) -> VVal
        where T: FnOnce(&std::vec::Vec<VarPos>, &mut std::vec::Vec<VVal>) -> ()
    {
        if let VVal::Fun(fu) = self {
            let mut new_fu = fu.as_ref().clone();
            f(&new_fu.upvalue_pos, &mut new_fu.upvalues);
            VVal::Fun(Rc::new(new_fu))
        } else {
            panic!("clone_and_rebind_upvalues does not work on non functions!");
        }
    }

    pub fn call_no_args(&self, env: &mut Env) -> Result<VVal, StackAction> {
        self.call_internal(env, 0)
    }

    pub fn call_internal(&self, env: &mut Env, argc: usize) -> Result<VVal, StackAction> {
        match self {
            VVal::None => {
                Err(StackAction::panic_msg("Calling $none is invalid".to_string()))
            },
            VVal::Fun(fu) => {
                if let Some(i) = fu.min_args {
                    if argc < i {
                        return Err(StackAction::panic_str(
                            format!(
                                "function expects at least {} arguments, got {}",
                                i, argc),
                            fu.syn_pos.clone()));
                    }
                }

                if let Some(i) = fu.max_args {
                    if argc > i {
                        return Err(StackAction::panic_str(
                            format!(
                                "function expects at most {} arguments, got {}",
                                i, argc),
                            fu.syn_pos.clone()));
                    }
                }

                env.push_fun_call(fu.clone(), argc);
                if !(*fu).err_arg_ok {
                    for i in 0..argc {
                        if let Some(VVal::Err(ev)) = env.arg_err_internal(i) {
                            return
                                Err(StackAction::panic_str(
                                    format!("Error value in parameter list: {}",
                                            ev.borrow().0.s()),
                                    Some(ev.borrow().1.clone())));
                        }
                    }
                }

                let ret =
                    match &(*fu).fun {
                        FunType::ClosureNode(cn) => (cn.borrow())(env, argc),
                        FunType::VMProg(prog)    => {
                            match crate::vm::vm(&*prog, env) {
                                Ok(v) => Ok(v),
                                Err(StackAction::Return(ret)) => {
                                    if ret.0.eqv(&fu.label) {
                                        Ok(ret.1)
                                    } else {
                                        Err(StackAction::Return(ret))
                                    }
                                },
                                Err(e) => {
                                    Err(e.wrap_panic(fu.syn_pos.clone()))
                                },
                            }
                        },
                    };
                env.unwind_one();
                ret
            },
            VVal::Bol(b) => {
                env.with_local_call_info(argc, |e: &mut Env| {
                    let first = e.arg(0);
                    match first {
                        // Calling a boolean on a list converts the boolean into an integer
                        // and fetches the value at that index.
                        VVal::Lst(_) => Ok(first.at(*b as usize).unwrap_or(VVal::None)),
                        // Calling a boolean on a pair converts the boolean into an integer
                        // and fetches the value at that index.
                        VVal::Pair(_) => Ok(first.at(*b as usize).unwrap_or(VVal::None)),
                        // Calling a boolean with anything else becomes an implicit `if`,
                        // calling the first parameter if the boolean is true
                        // and the second if the boolean is false.
                        _ => {
                            let idx = if *b { 0 } else { 1 };
                            if argc > 0 {
                                let v = e.arg(idx);
                                if !v.is_none() {
                                    v.call_internal(e, 0)
                                } else {
                                    Ok(VVal::None)
                                }
                            } else { Ok(self.clone()) }
                        }
                    }
                })
            },
            VVal::Err(e) => {
                Err(StackAction::panic_msg(
                    format!("Called an error value: {}", e.borrow().0.s())))
            },
            VVal::Sym(sym) => {
                env.with_local_call_info(argc, |e: &mut Env| {
                    if argc > 0 {
                        let v = e.arg(0);
                        Ok(v.get_key(&*sym).unwrap_or(VVal::None))
                    } else { Ok(self.clone()) }
                })
            },
            VVal::Map(m) => {
                env.with_local_call_info(argc, |e: &mut Env| {
                    let f = e.arg(0);

                    let mut ret = VVal::None;
                    for (k, v) in m.borrow().iter() {
                        e.push(v.clone());
                        e.push(VVal::new_str(k));
                        let el = f.call_internal(e, 2);
                        e.popn(2);

                        match el {
                            Ok(v)                      => { ret = v; },
                            Err(StackAction::Break(v)) => { ret = *v; break; },
                            Err(StackAction::Next)     => { },
                            Err(e)                     => { return Err(e); },
                        }
                    }
                    Ok(ret)
                })
            },
            VVal::Lst(l) => {
                env.with_local_call_info(argc, |e: &mut Env| {
                    // calling a list with any other value is an implicit map, meaning that for
                    // each value in the list the argument is called and the respective element
                    // in the map is passed in as the parameter.
                    let f = e.arg(0);

                    let mut ret = VVal::None;
                    for i in l.borrow().iter() {
                        e.push(i.clone());
                        let el = f.call_internal(e, 1);
                        e.popn(1);

                        match el {
                            Ok(v)                      => { ret = v; },
                            Err(StackAction::Break(v)) => { ret = *v; break; },
                            Err(StackAction::Next)     => { },
                            Err(e)                     => { return Err(e); },
                        }
                    }
                    Ok(ret)
                })
            },
            VVal::Byt(vval_bytes) => {
                env.with_local_call_info(argc, |e: &mut Env| {
                    if argc > 0 {
                        let first_arg = e.arg(0);
                        match first_arg {
                            VVal::Byt(b2) => {
                                if argc > 1 {
                                    let mut accum = vval_bytes.as_ref().clone();
                                    accum.extend_from_slice(b2.as_ref());
                                    for i in 2..argc {
                                        match e.arg(i) {
                                            VVal::Byt(b3) =>
                                                accum.extend_from_slice(
                                                    b3.as_ref()),
                                            _ =>
                                                accum.extend_from_slice(
                                                    &e.arg(i).as_bytes()),
                                        }
                                    }
                                    Ok(VVal::new_byt(accum))
                                } else {
                                    let mut accum = vval_bytes.as_ref().clone();
                                    accum.extend_from_slice(b2.as_ref());
                                    Ok(VVal::new_byt(accum))
                                }
                            },
                            VVal::Int(arg_int) => {
                                if argc > 1 {
                                    let from = arg_int as usize;
                                    let cnt  = e.arg(1).i() as usize;
                                    let r : Vec<u8> =
                                        vval_bytes.iter()
                                            .skip(from).take(cnt).copied()
                                            .collect();
                                    Ok(VVal::new_byt(r))
                                } else if arg_int as usize >= vval_bytes.len() {
                                    Ok(VVal::None)
                                } else {
                                    Ok(VVal::new_byt(vec![vval_bytes[arg_int as usize]]))
                                }
                            },
                            VVal::Fun(_) => {
                                let mut ret = VVal::None;
                                for c in vval_bytes.iter() {
                                    e.push(VVal::new_byt(vec![*c]));
                                    let el = first_arg.call_internal(e, 1);
                                    e.popn(1);
                                    match el {
                                        Ok(v)                      => { ret = v; },
                                        Err(StackAction::Break(v)) => { ret = *v; break; },
                                        Err(StackAction::Next)     => { },
                                        Err(e)                     => { return Err(e); },
                                    }
                                }
                                Ok(ret)
                            },
                            VVal::Lst(_) => {
                                let from =
                                    first_arg.at(0).unwrap_or(VVal::Int(0))
                                             .i() as usize;
                                let cnt  =
                                    first_arg.at(1).unwrap_or_else(
                                        || VVal::Int((vval_bytes.len() - from) as i64))
                                    .i() as usize;
                                let r : Vec<u8> =
                                    vval_bytes.iter()
                                        .skip(from).take(cnt).copied().collect();
                                Ok(VVal::new_byt(r))
                            },
                            VVal::Pair(p) => Ok(pair_extract(&p.0, &p.1, self)),
                            VVal::IVec(iv) => Ok(range_extract(iv.x_raw(), iv.y_raw(), self)),
                            VVal::Map(_) => Ok(self.with_s_ref(|key: &str| first_arg.get_key(key).unwrap_or(VVal::None))),
                            _ => Ok(VVal::None)
                        }
                    } else { Ok(self.clone()) }
                })
            },
            VVal::Str(vval_str) => {
                env.with_local_call_info(argc, |e: &mut Env| {
                    if argc > 0 {
                        let first_arg = e.arg(0);
                        match first_arg {
                            VVal::Int(arg_int) => {
                                if argc > 1 {
                                    let from = arg_int as usize;
                                    let cnt  = e.arg(1).i() as usize;
                                    Ok(VVal::new_str_mv(
                                        vval_str
                                            .chars().skip(from).take(cnt)
                                            .collect()))
                                } else {
                                    let r = vval_str.chars().nth(arg_int as usize);
                                    match r {
                                        None    => Ok(VVal::new_str("")),
                                        Some(c) => {
                                            let mut b = [0; 4];
                                            Ok(VVal::new_str(c.encode_utf8(&mut b)))
                                        },
                                    }
                                }
                            },
                            VVal::Fun(_) => {
                                let mut ret = VVal::None;
                                for c in vval_str.chars() {
                                    e.push(VVal::new_str_mv(c.to_string()));
                                    let el = first_arg.call_internal(e, 1);
                                    e.popn(1);
                                    match el {
                                        Ok(v)                      => { ret = v; },
                                        Err(StackAction::Break(v)) => { ret = *v; break; },
                                        Err(StackAction::Next)     => { },
                                        Err(e)                     => { return Err(e); },
                                    }
                                }
                                Ok(ret)
                            },
                            VVal::Lst(_) => {
                                let from = first_arg.at(0).unwrap_or(VVal::Int(0)).i() as usize;
                                let cnt  = first_arg.at(1).unwrap_or_else(|| VVal::Int((vval_str.len() - from) as i64)).i() as usize;
                                let r : String = vval_str.chars().skip(from).take(cnt).collect();
                                Ok(VVal::new_str_mv(r))
                            },
                            VVal::Str(s2) => {
                                if argc > 1 {
                                    let mut accum = vval_str.as_ref().clone() + s2.as_ref();
                                    for i in 1..argc {
                                        e.arg_ref(i).unwrap().with_s_ref(|s: &str| accum += s);
                                    }
                                    Ok(VVal::new_str_mv(accum))
                                } else {
                                    Ok(VVal::new_str_mv(vval_str.as_ref().clone() + s2.as_ref()))
                                }
                            },
                            VVal::Map(_) => Ok(
                                first_arg
                                    .get_key(vval_str.as_ref())
                                    .unwrap_or(VVal::None)),
                            VVal::Pair(p) => Ok(pair_extract(&p.0, &p.1, self)),
                            VVal::IVec(iv) => Ok(range_extract(iv.x_raw(), iv.y_raw(), self)),
                            _ => Ok(VVal::None)
                        }
                    } else { Ok(self.clone()) }
                })
            },
            VVal::Int(i) => {
                env.with_local_call_info(argc, |e: &mut Env| {
                    let v = e.arg(0);
                    if argc > 0 { Ok(v.at(*i as usize).unwrap_or(VVal::None)) }
                    else { Ok(self.clone()) }
                })
            },
            VVal::Pair(p) => {
                env.with_local_call_info(argc, |e: &mut Env| {
                    if argc != 1 { return Ok(VVal::None) }
                    Ok(pair_extract(&p.0, &p.1, e.arg_ref(0).unwrap()))
                })
            },
            VVal::IVec(iv) => {
                env.with_local_call_info(argc, |e: &mut Env| {
                    if argc != 1 { return Ok(VVal::None) }
                    Ok(range_extract(iv.x_raw(), iv.y_raw(), e.arg_ref(0).unwrap()))
                })
            },
            VVal::Iter(i) => {
                if argc == 1 {
                    env.with_local_call_info(argc, |e: &mut Env| {
                        let f = e.arg(0);

                        let mut ret = VVal::None;
                        #[allow(clippy::while_let_loop)]
                        loop {
                            let v =
                                if let Some((v, k)) = i.borrow_mut().next() {
                                    if let Some(k) = k {
                                        VVal::pair(v, k)
                                    } else {
                                        v
                                    }
                                } else {
                                    break;
                                };

                            e.push(v);
                            let el = f.call_internal(e, 1);
                            e.popn(1);

                            match el {
                                Ok(v)                      => { ret = v; },
                                Err(StackAction::Break(v)) => { ret = *v; break; },
                                Err(StackAction::Next)     => { },
                                Err(e)                     => { return Err(e); },
                            }
                        }
                        Ok(ret)
                    })
                } else {
                    Ok(iter_next!(i.borrow_mut()))
                }
            },
            VVal::Opt(v) => {
                if argc == 0 {
                    if let Some(v) = v {
                        Ok(v.as_ref().clone())
                    } else {
                        Ok(VVal::None)
                    }
                } else {
                    let v =
                        if let Some(v) = v { v.as_ref().clone() }
                        else               { VVal::None };

                    v.call_internal(env, argc)
                }
            },
            VVal::Usr(ud) => {
                env.with_local_call_info(argc, |e: &mut Env| ud.call(e))
            },
            VVal::DropFun(v) => v.v.call_internal(env, argc),
            VVal::Ref(v)     => v.borrow().call_internal(env, argc),
            VVal::CRef(v)    => v.borrow().call_internal(env, argc),
            VVal::WWRef(v)   =>
                if let Some(r) = v.upgrade() {
                    r.borrow().call_internal(env, argc)
                } else { Ok(VVal::None) },
            _ => { Ok(self.clone()) },
        }
    }

    pub fn to_ref(&self) -> VVal {
        match self {
            VVal::CRef(r)    => VVal::Ref(r.clone()),
            VVal::Ref(r)     => VVal::Ref(r.clone()),
            VVal::WWRef(v)   =>
                if let Some(r) = v.upgrade() {
                    VVal::Ref(r)
                } else {
                    VVal::Ref(Rc::new(RefCell::new(VVal::None)))
                },
            _ => VVal::Ref(Rc::new(RefCell::new(self.clone()))),
        }
    }

    pub fn to_weakened_upvalue_ref(&self) -> VVal {
        VVal::CRef(Rc::new(RefCell::new(self.clone())))
    }

    pub fn set_ref(&self, v: VVal) -> VVal {
        match self {
            VVal::Ref(r)     => r.replace(v),
            VVal::CRef(r)    => r.replace(v),
            VVal::WWRef(l)   => {
                if let Some(r) = l.upgrade() {
                    r.replace(v)
                } else {
                    VVal::None
                }
            },
            _ => VVal::None
        }
    }

    pub fn upgrade(self) -> VVal {
        match self {
            VVal::CRef(f) => VVal::Ref(f),
            VVal::WWRef(f) => {
                if let Some(r) = f.upgrade() {
                    VVal::Ref(r)
                } else {
                    VVal::None
                }
            },
            _ => self,
        }
    }

    pub fn downgrade(self) -> VVal {
        match self {
            VVal::Ref(f)  => VVal::WWRef(Rc::downgrade(&f)),
            VVal::CRef(f) => VVal::WWRef(Rc::downgrade(&f)),
            _ => self,
        }
    }

    pub fn map() -> VVal {
        VVal::Map(Rc::new(RefCell::new(FnvHashMap::with_capacity_and_hasher(2, Default::default()))))
    }

    pub fn map1(k: &str, v: VVal) -> VVal {
        let m = VVal::map();
        m.set_key_str(k, v).expect("single use");
        m
    }

    pub fn map2(k: &str, v: VVal, k2: &str, v2: VVal) -> VVal {
        let m = VVal::map();
        m.set_key_str(k, v).expect("single use");
        m.set_key_str(k2, v2).expect("single use");
        m
    }

    pub fn map3(k: &str, v: VVal, k2: &str, v2: VVal, k3: &str, v3: VVal) -> VVal {
        let m = VVal::map();
        m.set_key_str(k, v).expect("single use");
        m.set_key_str(k2, v2).expect("single use");
        m.set_key_str(k3, v3).expect("single use");
        m
    }

    pub fn sym(s: &str) -> VVal {
        VVal::Sym(s2sym(s))
    }

    pub fn to_sym(&self) -> Symbol {
        if let VVal::Sym(s) = self {
            s.clone()
        } else {
            self.with_s_ref(|s: &str| s2sym(s))
        }
    }

    #[allow(clippy::cast_ptr_alignment)]
    pub fn ref_id(&self) -> Option<i64> {
        Some(match self {
            VVal::Err(r)     => { &*r.borrow() as *const (VVal, SynPos) as i64 },
            VVal::Str(s)     => { &*s.as_ref() as *const String as i64 },
            VVal::Byt(s)     => { &*s.as_ref() as *const Vec<u8> as i64 },
            VVal::Sym(s)     => { s.ref_id() },
            VVal::Lst(v)     => { &*v.borrow() as *const Vec<VVal> as i64 },
            VVal::Map(v)     => { &*v.borrow() as *const FnvHashMap<Symbol, VVal> as i64 },
            VVal::Iter(v)    => { &*v.borrow() as *const VValIter as i64 },
            VVal::Opt(p)     => { if let Some(p) = p { &*p.as_ref() as *const VVal as i64 } else { 0 } },
            VVal::Fun(f)     => { &**f as *const VValFun as i64 },
            VVal::DropFun(f) => { &**f as *const DropVVal as i64 },
            VVal::Pair(v)    => { &**v as *const (VVal, VVal) as i64 },
            VVal::Ref(v)     => { &*v.borrow() as *const VVal as i64 },
            VVal::CRef(v)    => { &*v.borrow() as *const VVal as i64 },
            VVal::Usr(b)     => { &**b as *const dyn VValUserData as *const usize as i64 },
            VVal::WWRef(r)   => {
                if let Some(l) = r.upgrade() {
                    &*l.borrow() as *const VVal as i64
                } else {
                    return None;
                }
            },
            _ => return None,
        })
    }

    pub fn eqv(&self, v: &VVal) -> bool {
        match self {
            VVal::None    => { if let VVal::None = v { true } else { false } },
            VVal::Bol(ia) => { if let VVal::Bol(ib) = v { ia == ib } else { false } },
            VVal::Int(ia) => { if let VVal::Int(ib) = v { ia == ib } else { false } },
            VVal::Flt(ia) => { if let VVal::Flt(ib) = v { (ia - ib).abs() < std::f64::EPSILON } else { false } },
            VVal::Sym(s)  => { if let VVal::Sym(ib) = v { s == ib } else { false } },
            VVal::Syn(s)  => { if let VVal::Syn(ib) = v { *s == *ib } else { false } },
            VVal::Str(s)  => { if let VVal::Str(ib) = v { *s == *ib } else { false } },
            VVal::Byt(s)  => { if let VVal::Byt(s2) = v { s[..] == s2[..] } else { false } },
            VVal::Pair(b)  => {
                if let VVal::Pair(b2) = v { b.0.eqv(&b2.0) && b.1.eqv(&b2.1) }
                else { false }
            },
            VVal::Opt(a)  => {
                if let VVal::Opt(b) = v {
                    if let Some(a) = a {
                        if let Some(b) = b {
                            a.eqv(b)
                        } else {
                            false
                        }
                    } else {
                        b.is_none()
                    }
                } else {
                    false
                }
            },
            VVal::Lst(l)  => {
                if let VVal::Lst(l2) = v { Rc::ptr_eq(l, l2) } else { false }
            },
            VVal::Map(l)  => {
                if let VVal::Map(l2) = v { Rc::ptr_eq(l, l2) } else { false }
            },
            VVal::Fun(l)  => {
                if let VVal::Fun(l2) = v { Rc::ptr_eq(l, l2) } else { false }
            },
            VVal::IVec(iv) => match v { VVal::IVec(v) => *v == *iv, _ => false },
            VVal::FVec(fv) => match v { VVal::FVec(v) => *v == *fv, _ => false },
            VVal::DropFun(l)  => {
                if let VVal::DropFun(l2) = v { Rc::ptr_eq(l, l2) } else { false }
            },
            VVal::Err(l)  => {
                if let VVal::Err(l2) = v { Rc::ptr_eq(l, l2) } else { false }
            },
            VVal::Iter(i) => {
                if let VVal::Iter(i2) = v {
                    Rc::ptr_eq(i, i2)
                } else {
                    false
                }
            },
            VVal::Ref(l)  => vval_rc_ptr_eq(v, l),
            VVal::CRef(l) => vval_rc_ptr_eq(v, l),
            VVal::WWRef(lw) => {
                if let Some(l) = lw.upgrade() {
                    vval_rc_ptr_eq(v, &l)
                } else {
                    false
                }
            },
            VVal::Usr(u)  => {
                if let VVal::Usr(u2) = v {
                    u.eqv(&u2)
                } else {
                    false
                }
            }
        }
    }

    fn dump_vec_as_str(v: &Rc<RefCell<std::vec::Vec<VVal>>>, c: &mut CycleCheck) -> String {
        let mut out : Vec<String> = Vec::new();
        let mut first = true;
        out.push(String::from("$["));
        for s in v.borrow().iter().map(|v| v.s_cy(c)) {
            if !first { out.push(String::from(",")); }
            else { first = false; }
            out.push(s);
        }
        out.push(String::from("]"));
        out.concat()
    }

    fn dump_sym(s: &str) -> String {
        if s.chars().all(|c|
            c.is_alphanumeric()
            || c == '_' || c == '-' || c == '+'
            || c == '&' || c == '@')
        {
            format!(":{}", s)
        } else {
            format!(":\"{}\"", s)
        }
    }

    fn dump_map_as_str(m: &Rc<RefCell<FnvHashMap<Symbol,VVal>>>, c: &mut CycleCheck) -> String {
        let mut out : Vec<String> = Vec::new();
        let mut first = true;
        out.push(String::from("${"));
        let hm = m.borrow();

        let mut keys : Vec<&Symbol> = hm.keys().collect();
        keys.sort();
        for k in keys {
            let v = hm.get(k).unwrap();
            if !first { out.push(String::from(",")); }
            else { first = false; }
            if k.as_ref().chars().any(char::is_whitespace) {
                out.push(format!("\"{}\"", k.as_ref()));
            } else {
                out.push(k.as_ref().to_string());
            }
            out.push(String::from("="));
            out.push(v.s_cy(c));
        }
        out.push(String::from("}"));
        out.concat()
    }

    pub fn map_ok_skip<T, R>(&self, mut op: T, skip: usize) -> Vec<R>
        where T: FnMut(&VVal) -> R {

        let mut res : Vec<R> = Vec::new();
        if let VVal::Lst(b) = &self {
            for i in b.borrow().iter().skip(skip) {
                res.push(op(i));
            }
        }
        res
    }

    pub fn map_skip<R, E, T>(&self, mut op: T, skip: usize) -> Result<Vec<R>, E>
        where T: FnMut(&VVal) -> Result<R, E> {

        let mut res : Vec<R> = Vec::new();
        if let VVal::Lst(b) = &self {
            for i in b.borrow().iter().skip(skip) {
                res.push(op(i)?);
            }
        }
        Ok(res)
    }

    pub fn unshift(&self, val: VVal) -> &VVal {
        if let VVal::Lst(b) = &self {
            b.borrow_mut().insert(0, val);
            self
        } else {
            self.with_deref(move |v| { v.unshift(val); }, |_| ());
            self
        }
    }

    pub fn insert_at(&self, index: usize, val: VVal) {
        if let VVal::Lst(b) = &self {
            b.borrow_mut().insert(index, val);
        } else {
            self.with_deref(|v| v.insert_at(index, val), |_| ())
        }
    }

    pub fn set(&self, index: usize, val: VVal) {
        if let VVal::Lst(b) = &self {
            if index >= b.borrow().len() {
                b.borrow_mut().resize(index + 1, VVal::None);
            }
            b.borrow_mut()[index] = val;
        }
    }

    pub fn set_at(&self, index: usize, val: VVal) {
        if let VVal::Lst(b) = &self {
            b.borrow_mut()[index] = val;
        }
    }

    pub fn at(&self, index: usize) -> Option<VVal> {
        match self {
            VVal::Byt(vval_bytes) => {
                if index as usize >= vval_bytes.len() {
                    None
                } else {
                    Some(VVal::new_byt(vec![vval_bytes[index as usize]]))
                }
            },
            VVal::Str(vval_str) => {
                let opt_char = vval_str.chars().nth(index as usize);
                match opt_char {
                    None    => None,
                    Some(char) => {
                        let mut buf = [0; 4];
                        Some(VVal::new_str(char.encode_utf8(&mut buf)))
                    },
                }
            },
            VVal::Pair(b) => {
                Some(if index % 2 == 0 { b.0.clone() } else { b.1.clone() })
            },
            VVal::Iter(i) => {
                let mut i = i.borrow_mut();
                for _ in 0..index { i.next(); }
                iter_next_value!(i, v, { Some(v) }, None)
            },
            VVal::Opt(ref b) => {
                if let Some(b) = b {
                    b.as_ref().at(index)
                } else {
                    None
                }
            },
            VVal::IVec(b) => {
                Some(match index {
                    0 => b.x(),
                    1 => b.y(),
                    2 => b.z().unwrap_or(VVal::None),
                    3 => b.w().unwrap_or(VVal::None),
                    _ => VVal::None
                })
            },
            VVal::FVec(b) => {
                Some(match index {
                    0 => b.x(),
                    1 => b.y(),
                    2 => b.z().unwrap_or(VVal::None),
                    3 => b.w().unwrap_or(VVal::None),
                    _ => VVal::None
                })
            },
            VVal::Lst(b) => {
                if b.borrow().len() > index {
                    Some(b.borrow()[index].clone())
                } else {
                    None
                }
            },
            v => v.with_deref(
                |v| v.at(index),
                |v| if let Some(v) = v { v.get_key(&format!("{}", index)) }
                    else { None }),
        }
    }

    pub fn proto_data(&self) -> VVal {
        match self {
            VVal::Map(m) => m.borrow().get(&s2sym("_data")).cloned().unwrap_or(VVal::None),
            VVal::Lst(l) => {
                if l.borrow().len() > 1 {
                    l.borrow()[1].clone()
                } else {
                    VVal::None
                }
            },
            v => v.with_deref(
                |v| v.proto_data(),
                |_| VVal::None),
        }
    }

    pub fn proto_lookup(&self, key: &str) -> Option<VVal> {
        match self {
            VVal::Map(m) => {
                if let Some(func) = m.borrow().get(&s2sym(key)) {
                    Some(func.clone())
                } else if let Some(proto) = m.borrow().get(&s2sym("_proto")) {
                    proto.proto_lookup(key)
                } else {
                    None
                }
            },
            VVal::Lst(l) => {
                let l = l.borrow();
                if l.is_empty() {
                    None
                } else {
                    l[0].proto_lookup(key)
                }
            },
            v => v.with_deref(|v| v.proto_lookup(key), |_| None),
        }
    }

    pub fn get_key_sym(&self, key: &Symbol) -> Option<VVal> {
        match self {
            VVal::Map(m) => m.borrow().get(key).cloned(),
            _            => self.get_key(key.as_ref()),
        }
    }

    pub fn get_key(&self, key: &str) -> Option<VVal> {
        match self {
            VVal::Map(m) => m.borrow().get(&s2sym(key)).cloned(),
            VVal::IVec(b) => {
                Some(match key {
                    "0" | "first"  | "x" | "r" | "h" => b.x(),
                    "1" | "second" | "y" | "g" | "s" => b.y(),
                    "2" | "third"  | "z" | "b" | "v" => b.z().unwrap_or(VVal::None),
                    "3" | "fourth" | "w" | "a" => b.w().unwrap_or(VVal::None),
                    _ =>
                        swizzle_i(
                            key,
                            b.x_raw(),
                            b.y_raw(),
                            b.z_raw().unwrap_or(0),
                            b.w_raw().unwrap_or(0)),
                })
            },
            VVal::FVec(b) => {
                Some(match key {
                    "0" | "first"  | "x" | "r" | "h" => b.x(),
                    "1" | "second" | "y" | "g" | "s" => b.y(),
                    "2" | "third"  | "z" | "b" | "v" => b.z().unwrap_or(VVal::None),
                    "3" | "fourth" | "w" | "a" => b.w().unwrap_or(VVal::None),
                    _ =>
                        swizzle_f(
                            key,
                            b.x_raw(),
                            b.y_raw(),
                            b.z_raw().unwrap_or(0.0),
                            b.w_raw().unwrap_or(0.0)),
                })
            },
            VVal::Pair(_) => {
                self.at(match key {
                    "0" | "value" | "v" | "car" | "head" | "first"  => 0,
                    "1" | "key"   | "k" | "cdr" | "tail" | "second" => 1,
                    _ => {
                        if !key.chars().all(|c| c.is_digit(10)) { return None; }
                        usize::from_str_radix(key, 10).unwrap_or(0)
                    }
                })
            },
            VVal::Iter(i) => {
                if !key.chars().all(|c| c.is_digit(10)) { return None; }

                let index = usize::from_str_radix(key, 10).unwrap_or(0);
                let mut i = i.borrow_mut();
                for _ in 0..index { i.next(); }
                iter_next_value!(i, v, { Some(v) }, None)
            },
            VVal::Lst(l) => {
                if !key.chars().all(|c| c.is_digit(10)) { return None; }

                let idx = usize::from_str_radix(key, 10).unwrap_or(0);
                if idx < l.borrow().len() {
                    Some(l.borrow()[idx].clone())
                } else {
                    Some(VVal::None)
                }
            },
            VVal::Usr(u) => u.get_key(key),
            v => v.with_deref(|v| v.get_key(key), |_| None),
        }
    }

    pub fn set_map_key_fun<T>(&self, key: &str, fun: T, min_args: Option<usize>, max_args: Option<usize>, err_arg_ok: bool)
        where T: 'static + Fn(&mut Env, usize) -> Result<VVal, StackAction> {
        self.set_key_sym(
                s2sym(key),
                VValFun::new_fun(fun, min_args, max_args, err_arg_ok))
            .expect("Map not borrowed when using set_map_key_fun");
    }

    pub fn deref(&self) -> VVal {
        self.with_deref(
            |v| v.clone(),
            |v| v.map_or(VVal::None, |v| v.clone()))
    }

    #[inline]
    pub fn with_deref<O, D, R>(&self, op: O, default: D) -> R
        where O: FnOnce(&VVal) -> R, D: FnOnce(Option<&VVal>) -> R
    {
        match self {
            VVal::DropFun(r)    => op(&r.v),
            VVal::Opt(Some(v))  => op(v.as_ref()),
            VVal::Opt(None)     => op(&VVal::None),
            VVal::Ref(l)        => op(&(*l).borrow()),
            VVal::CRef(l)       => op(&(*l).borrow()),
            VVal::WWRef(l)      => {
                match l.upgrade() {
                    Some(v) => op(&v.borrow()),
                    None    => default(None),
                }
            },
            _ => default(Some(self)),
        }
    }

    pub fn list_operation<O, R>(&self, mut op: O) -> Result<R, StackAction>
        where O: FnMut(&mut std::cell::RefMut<std::vec::Vec<VVal>>) -> R
    {
        match self {
            VVal::Lst(l) => {
                match l.try_borrow_mut() {
                    Ok(mut v) => { Ok(op(&mut v)) },
                    Err(_) => Err(StackAction::panic_borrow(self)),
                }
            },
            v => v.with_deref(|v| {
                v.list_operation(op)
            }, |v| Err(StackAction::panic_msg(format!(
                    "Can't do list operation with non list value: {}",
                    v.map_or("".to_string(), |v| v.s())))))
        }
    }

    pub fn delete_key(&self, key: &VVal) -> Result<VVal, StackAction> {
        match self {
            VVal::Map(m) => {
                let ks = key.to_sym();
                match m.try_borrow_mut() {
                    Ok(mut r)  => Ok(r.remove(&ks).unwrap_or_else(|| VVal::None)),
                    Err(_)     => Err(StackAction::panic_borrow(self)),
                }
            },
            VVal::Lst(l) => {
                let idx = key.i() as usize;
                match l.try_borrow_mut() {
                    Ok(mut v) => {
                        if idx < v.len() {
                            Ok(v.remove(idx))
                        } else {
                            Ok(VVal::None)
                        }
                    },
                    Err(_) => Err(StackAction::panic_borrow(self)),
                }
            },
            VVal::Usr(u) => u.delete_key(key),
            v => v.with_deref(
                |v| v.delete_key(key),
                |_| Ok(VVal::None)),
        }
    }

    pub fn set_key_str(&self, key: &str, val: VVal) -> Result<(), StackAction> {
        self.set_key_sym(s2sym(key), val)
    }

    pub fn set_key_sym(&self, key: Symbol, val: VVal) -> Result<(), StackAction> {
        match self {
            VVal::Map(m) => {
                match m.try_borrow_mut() {
                    Ok(mut r)  => { r.insert(key, val); Ok(()) },
                    Err(_)     => Err(StackAction::panic_borrow(self)),
                }
            },
            _ => self.set_key(&VVal::Sym(key), val),
        }
    }



    pub fn set_key(&self, key: &VVal, val: VVal) -> Result<(), StackAction> {
        match self {
            VVal::Map(m) => {
                let ks = key.to_sym();
                match m.try_borrow_mut() {
                    Ok(mut r)  => { r.insert(ks, val); Ok(()) },
                    Err(_)     => Err(StackAction::panic_borrow(self)),
                }
            },
            VVal::Lst(l) => {
                let idx = key.i() as usize;
                match l.try_borrow_mut() {
                    Ok(mut v) => {
                        if v.len() <= idx {
                            v.resize(idx + 1, VVal::None);
                        }
                        v[idx] = val;
                        Ok(())
                    },
                    Err(_) => Err(StackAction::panic_borrow(self)),
                }
            },
            VVal::Usr(u) => u.set_key(key, val),
            v => v.with_deref(
                |v| v.set_key(key, val),
                |_| Ok(())),
        }
    }

    pub fn pop(&self) -> VVal {
        if let VVal::Lst(b) = &self {
            b.borrow_mut().pop().unwrap_or(VVal::None)
        } else {
            self.with_deref(|v| v.pop(), |_| VVal::None)
        }
    }

    pub fn accum(&mut self, val: &VVal) {
        match self {
            VVal::Byt(ref mut b) => {
                match val {
                    VVal::Int(i) => { Rc::make_mut(b).push(*i as u8); },
                    VVal::Flt(f) => { Rc::make_mut(b).push(*f as u8); },
                    VVal::Str(s) => { Rc::make_mut(b).extend_from_slice(s.as_bytes()); },
                    VVal::Sym(s) => { Rc::make_mut(b).extend_from_slice(s.as_bytes()); },
                    VVal::Byt(s) => { Rc::make_mut(b).extend_from_slice(s.as_ref()); },
                    VVal::Bol(o) => { Rc::make_mut(b).push(*o as u8); },
                    _ => {
                        val.with_s_ref(|s: &str|
                            Rc::make_mut(b)
                                .extend_from_slice(
                                    s.as_bytes()));
                    }
                }
            },
            VVal::Str(ref mut a) => {
                match val {
                    VVal::Str(s) => { Rc::make_mut(a).push_str(s.as_ref()); },
                    VVal::Sym(s) => { Rc::make_mut(a).push_str(&*s); },
                    VVal::Byt(s) => {
                        for b in s.as_ref().iter() {
                            let b = *b as char;
                            Rc::make_mut(a).push(b);
                        }
                    },
                    _ => {
                        val.with_s_ref(|s: &str|
                            Rc::make_mut(a).push_str(s));
                    }
                }
            },
            VVal::Int(i) => { *i += val.i(); },
            VVal::Flt(f) => { *f += val.f(); },
            VVal::Lst(v) => { v.borrow_mut().push(val.clone()); },
            _ => (),
        }
    }

    pub fn push(&self, val: VVal) -> &VVal {
        //d// println!("FN PUSH {} v {}", self.s(), val.s());
        if let VVal::Lst(b) = &self {
            //d// println!("FN ! PUSH {} v {}", self.s(), val.s());
            b.borrow_mut().push(val);
        } else {
            self.with_deref(|v| { v.push(val); }, |_| ())
        }
        self
    }

    pub fn is_empty(&self) -> bool { self.len() == 0 }

    pub fn len(&self) -> usize {
        match self {
            VVal::Lst(l) => l.borrow().len(),
            VVal::Map(l) => l.borrow().len(),
            VVal::Byt(l) => l.len(),
            VVal::Str(l) => l.len(),
            VVal::Sym(l) => l.len(),
            v => v.with_deref(|v| v.len(), |_| 0),
        }
    }

    pub fn s_len(&self) -> usize {
        match self {
            VVal::Str(s)  => s.chars().count(),
            VVal::Sym(s)  => s.chars().count(),
            VVal::Usr(s)  => s.s_raw().chars().count(),
            VVal::Byt(b)  => b.len(),
            VVal::None    => 0,
            _             => self.s().chars().count(),
        }
    }

    /// Returns the string data or converts the value to a string for displaying.
    /// The conversion format is not for reading the value in again via
    /// the WLambda parser, it's for accessing the data as pure as possible.
    ///
    /// Use this if you need the raw unescaped contents of VVal::Str, VVal::Sym,
    /// VVal::Byt and other VVals.
    ///
    /// As this is used usually for generating output a VVal::None is
    /// turned into an empty string
    ///
    /// **There is also `with_s_ref()` which allows you to work with a
    /// reference to the string, in case you don't need to own the copy!**
    ///
    /// ```
    /// use wlambda::VVal;
    ///
    /// assert_eq!(VVal::None.s_raw(), "");
    /// assert_eq!(VVal::new_str("Test").s_raw(), "Test");
    /// ```
    pub fn s_raw(&self) -> String {
        match self {
            VVal::Str(s)  => s.as_ref().clone(),
            VVal::Sym(s)  => String::from(s.as_ref()),
            VVal::Usr(s)  => s.s_raw(),
            VVal::Byt(s)  => s.iter().map(|b| *b as char).collect(),
            VVal::None    => String::from(""),
            v => v.with_deref(|v| {
                if v.is_none() { "".to_string() }
                else { v.s_raw() }
            }, |v| {
                v.map_or_else(
                    || "".to_string(),
                    |v| if v.is_none() { "".to_string() }
                        else { v.s() })
            }),
        }
    }

    /// Like s_raw() but returns a reference to the string.
    ///
    /// Use this if you need the raw unescaped contents of VVal::Str, VVal::Sym,
    /// VVal::Byt and other VVals.
    ///
    /// As this is used usually for generating output a VVal::None is
    /// turned into an empty string
    ///
    /// ```
    /// use wlambda::VVal;
    ///
    /// VVal::None.with_s_ref(
    ///     |s: &str| assert_eq!(s, ""));
    ///
    /// VVal::new_str("Test").with_s_ref(
    ///     |s: &str| assert_eq!(s, "Test"));
    /// ```
    #[inline]
    pub fn with_s_ref<T, R>(&self, f: T) -> R
        where T: FnOnce(&str) -> R
    {
        match self {
            VVal::Str(s)  => f(s.as_ref()),
            VVal::Sym(s)  => f(&*s),
            VVal::Usr(s)  => f(&s.s_raw()),
            VVal::Byt(_)  => f(&self.s_raw()),
            VVal::None    => f(""),
            _             => f(&self.s_raw()),
        }
    }

    pub fn is_float(&self) -> bool {
        match self { VVal::Flt(_) => true, _ => false }
    }

    pub fn is_int(&self) -> bool {
        match self { VVal::Int(_) => true, _ => false }
    }

    pub fn is_sym(&self) -> bool {
        match self { VVal::Sym(_) => true, _ => false }
    }

    pub fn is_syn(&self) -> bool {
        match self { VVal::Syn(_) => true, _ => false }
    }

    pub fn get_syn_pos(&self) -> SynPos {
        if let VVal::Syn(s) = self {
            s.clone()
        } else {
            SynPos {
                syn: Syntax::Block,
                line: 0, col: 0,
                file: FileRef::new("?"),
                name: None,
            }
        }
    }

    pub fn set_syn(&mut self, syn: Syntax) {
        if let VVal::Syn(s) = self {
            s.syn = syn;
        }
    }

    pub fn set_syn_at(&mut self, idx: usize, syn: Syntax) {
        let mut v = self.v_(idx);
        v.set_syn(syn);
        self.set(idx, v);
    }

    pub fn get_syn(&self) -> Syntax {
        if let VVal::Syn(s) = self {
            s.syn.clone()
        } else {
            Syntax::Block
        }
    }

    pub fn compile_err(&self, msg: String) -> CompileError {
        CompileError {
            msg,
            pos: self.at(0).unwrap_or(VVal::None).get_syn_pos(),
        }
    }

    pub fn to_compile_err(&self, msg: String) -> Result<EvalNode, CompileError> {
        Err(self.compile_err(msg))
    }

    pub fn is_pair(&self) -> bool {
        match self { VVal::Pair(_) => true, _ => false }
    }

    pub fn is_iter(&self) -> bool {
        if let VVal::Iter(_) = self { true } else { false }
    }

    pub fn is_optional(&self) -> bool {
        if let VVal::Opt(_) = self { true } else { false }
    }

    pub fn is_ref(&self) -> bool {
        match self { VVal::Ref(_) => true, VVal::CRef(_) => true, VVal::WWRef(_) => true, _ => false }
    }

    pub fn is_wref(&self) -> bool {
        match self { VVal::WWRef(_) => true, _ => false }
    }

    pub fn is_bool(&self) -> bool {
        match self { VVal::Bol(_) => true, _ => false }
    }

    pub fn is_bytes(&self) -> bool {
        match self { VVal::Byt(_) => true, _ => false }
    }

    pub fn is_str(&self) -> bool {
        match self { VVal::Str(_) => true, _ => false }
    }

    pub fn is_fun(&self) -> bool {
        match self { VVal::Fun(_) => true, _ => false }
    }

    pub fn is_vec(&self) -> bool {
        match self { VVal::Lst(_) => true, _ => false }
    }

    pub fn is_nvec(&self) -> bool {
        match self { VVal::FVec(_) => true, VVal::IVec(_) => true, _ => false }
    }

    pub fn is_ivec(&self) -> bool {
        match self { VVal::IVec(_) => true, _ => false }
    }

    pub fn is_fvec(&self) -> bool {
        match self { VVal::FVec(_) => true, _ => false }
    }

    pub fn nvec_len(&self) -> usize {
        match self {
            VVal::IVec(nv) => nv.dims().len(),
            VVal::FVec(nv) => nv.dims().len(),
            _ => 0,
        }
    }

    pub fn is_map(&self) -> bool {
        match self { VVal::Map(_) => true, _ => false }
    }

    pub fn is_some(&self) -> bool {
        match self {
            VVal::None      => false,
            VVal::Opt(None) => false,
            _ => true
        }
    }

    pub fn is_none(&self) -> bool {
        match self {
            VVal::None      => true,
            VVal::Opt(None) => true,
            _ => false,
        }
    }

    pub fn is_err(&self) -> bool {
        match self { VVal::Err(_) => true, _ => false }
    }

    pub fn type_name(&self) -> &str {
        match self {
            VVal::Str(_)     => "string",
            VVal::Byt(_)     => "bytes",
            VVal::None       => "none",
            VVal::Err(_)     => "error",
            VVal::Bol(_)     => "bool",
            VVal::Sym(_)     => "symbol",
            VVal::Syn(_)     => "syntax",
            VVal::Int(_)     => "integer",
            VVal::Flt(_)     => "float",
            VVal::Pair(_)    => "pair",
            VVal::Iter(_)    => "iter",
            VVal::Opt(_)     => "optional",
            VVal::Lst(_)     => "vector",
            VVal::Map(_)     => "map",
            VVal::Usr(_)     => "userdata",
            VVal::Fun(_)     => "function",
            VVal::IVec(_)    => "integer_vector",
            VVal::FVec(_)    => "float_vector",
            VVal::DropFun(_) => "drop_function",
            VVal::Ref(_)     => "strong",
            VVal::CRef(_)    => "weakable",
            VVal::WWRef(_)   => "weak",
        }
    }

    /// Quick access method for retrieving the VVal at index `idx`.
    /// Returns VVal::None if the VVal is not a VVal::Lst or no such index exists.
    /// See also the shorthands `v_i`, `v_f`, `v_s` and `v_s_raw`.
    ///
    ///```
    /// use wlambda::VVal;
    /// let v = VVal::vec();
    /// v.push(VVal::Int(10));
    /// v.push(VVal::Int(11));
    ///
    /// assert_eq!(v.v_(1).i(), 11);
    /// assert_eq!(v.v_i(1),    11);
    ///```
    pub fn v_(&self, idx: usize) -> VVal { self.at(idx).unwrap_or(VVal::None) }

    /// Quick access method for retrieving the VVal at key `idx`.
    /// Returns VVal::None if the VVal is not a VVal::Map or no such index exists.
    /// See also the shorthands `v_ik`, `v_fk`, `v_sk` and `v_s_rawk`.
    ///
    ///```
    /// use wlambda::VVal;
    /// let v = VVal::map();
    /// v.set_key_str("aaa", VVal::Int(12));
    /// v.set_key_str("abc", VVal::Int(13));
    /// v.set_key_str("zyy", VVal::Int(14));
    ///
    /// assert_eq!(v.v_k("abc").i(), 13);
    /// assert_eq!(v.v_ik("aaa"),    12);
    /// assert_eq!(v.v_ik("zyy"),    14);
    ///```
    pub fn v_k(&self, key: &str) -> VVal { self.get_key(key).unwrap_or(VVal::None) }
    /// Quick access of an integer at the given `idx`.
    /// See also `v_`.
    ///
    ///```
    /// let v = wlambda::VVal::vec();
    /// v.push(wlambda::VVal::Int(11));
    /// assert_eq!(v.v_i(0),    11);
    ///```
    pub fn v_i(&self, idx: usize)     -> i64 { self.v_(idx).i() }
    /// Quick access of the integer at the given `key`.
    /// See also `v_k`.
    ///
    ///```
    /// let v = wlambda::VVal::map();
    /// v.set_key_str("aaa", wlambda::VVal::new_str("10"));
    /// assert_eq!(v.v_ik("aaa"), 10);
    ///```
    pub fn v_ik(&self, key: &str)     -> i64 { self.v_k(key).i() }
    /// Quick access of a raw string at the given `idx`.
    /// See also `v_`.
    ///
    ///```
    /// let v = wlambda::VVal::vec();
    /// v.push(wlambda::VVal::Int(12));
    /// assert_eq!(v.v_s_raw(0), "12");
    ///```
    pub fn v_s_raw(&self, idx: usize) -> String { self.v_(idx).s_raw() }
    /// Quick access of a raw string as reference at the given `idx`.
    /// See also `v_`.
    ///
    ///```
    /// let v = wlambda::VVal::vec();
    /// v.push(wlambda::VVal::new_str("12"));
    /// assert_eq!(v.v_with_s_ref(0, |s: &str| s.chars().nth(0).unwrap()), '1');
    ///```
    pub fn v_with_s_ref<T, R>(&self, idx: usize, f: T) -> R
        where T: FnOnce(&str) -> R
    {
        self.v_(idx).with_s_ref(f)
    }
    /// Quick access of the string at the given `key`.
    /// See also `v_k`.
    ///
    ///```
    /// let v = wlambda::VVal::map();
    /// v.set_key_str("aaa", wlambda::VVal::new_str("XYX"));
    /// assert_eq!(v.v_s_rawk("aaa"), "XYX");
    ///```
    pub fn v_s_rawk(&self, key: &str) -> String { self.v_k(key).s_raw() }
    /// Quick access of the string as reference at the given `key`.
    /// See also `v_k`.
    ///
    ///```
    /// let v = wlambda::VVal::map();
    /// v.set_key_str("aaa", wlambda::VVal::new_str("XYX"));
    /// assert!(v.v_with_s_refk("aaa", |s: &str| s == "XYX"));
    ///```
    pub fn v_with_s_refk<T, R>(&self, key: &str, f: T) -> R
        where T: FnOnce(&str) -> R
    {
        self.v_k(key).with_s_ref(f)
    }
    /// Quick access of the string representation at the given `idx`.
    /// See also `v_`.
    ///
    ///```
    /// let v = wlambda::VVal::vec();
    /// v.push(wlambda::VVal::Int(13));
    /// assert_eq!(v.v_s(0), "13");
    ///```
    pub fn v_s(&self, idx: usize)     -> String { self.v_(idx).s() }
    /// Quick access of the string represenation at the given `key`.
    /// See also `v_k`.
    ///
    ///```
    /// let v = wlambda::VVal::map();
    /// v.set_key_str("aaa", wlambda::VVal::Flt(12.2));
    /// assert_eq!(v.v_sk("aaa"), "12.2");
    ///```
    pub fn v_sk(&self, key: &str)     -> String { self.v_k(key).s() }
    /// Quick access of the float at the given `idx`.
    /// See also `v_`.
    ///
    ///```
    /// let v = wlambda::VVal::vec();
    /// v.push(wlambda::VVal::Flt(13.2));
    /// assert_eq!(v.v_f(0), 13.2);
    ///```
    pub fn v_f(&self, idx: usize)     -> f64 { self.v_(idx).f() }
    /// Quick access of the float at the given `key`.
    /// See also `v_k`.
    ///
    ///```
    /// let v = wlambda::VVal::map();
    /// v.set_key_str("aaa", wlambda::VVal::Flt(12.2));
    /// assert_eq!(v.v_fk("aaa"), 12.2);
    ///```
    pub fn v_fk(&self, key: &str)     -> f64 { self.v_k(key).f() }

    pub fn for_each<T>(&self, mut op: T)
        where T: FnMut(&VVal) -> () {
        if let VVal::Lst(b) = &self {
            for i in b.borrow().iter() { op(i); }
        }
    }

    pub fn for_eachk<T>(&self, mut op: T)
        where T: FnMut(&str, &VVal) -> () {
        if let VVal::Map(b) = &self {
            for (k, v) in b.borrow().iter() { op(&k, v); }
        }
    }

    #[allow(clippy::cast_lossless)]
    pub fn f(&self) -> f64 {
        match self {
            VVal::Str(s)     => (*s).parse::<f64>().unwrap_or(0.0),
            VVal::Sym(s)     => (*s).parse::<f64>().unwrap_or(0.0),
            VVal::Byt(s)     => if s.len() > 0 { s[0] as f64 } else { 0.0 },
            VVal::None       => 0.0,
            VVal::Err(_)     => 0.0,
            VVal::Bol(b)     => if *b { 1.0 } else { 0.0 },
            VVal::Syn(s)     => (s.syn.clone() as i64) as f64,
            VVal::Int(i)     => *i as f64,
            VVal::Flt(f)     => *f,
            VVal::Pair(b)    => b.0.f(),
            VVal::Lst(l)     => l.borrow().len() as f64,
            VVal::Map(l)     => l.borrow().len() as f64,
            VVal::Usr(u)     => u.f(),
            VVal::Fun(_)     => 1.0,
            VVal::IVec(iv)   => iv.x_raw() as f64,
            VVal::FVec(fv)   => fv.x_raw(),
            VVal::Iter(i)    => iter_next_value!(i.borrow_mut(), v, { v.f() }, 0.0),
            v => v.with_deref(|v| v.f(), |_| 0.0),
        }
    }

    #[allow(clippy::cast_lossless)]
    pub fn i(&self) -> i64 {
        match self {
            VVal::Str(s)     => (*s).parse::<i64>().unwrap_or(0),
            VVal::Sym(s)     => (*s).parse::<i64>().unwrap_or(0),
            VVal::Byt(s)     => if s.len() > 0 { s[0] as i64 } else { 0 as i64 },
            VVal::None       => 0,
            VVal::Err(_)     => 0,
            VVal::Bol(b)     => if *b { 1 } else { 0 },
            VVal::Syn(s)     => s.syn.clone() as i64,
            VVal::Int(i)     => *i,
            VVal::Flt(f)     => (*f as i64),
            VVal::Pair(b)    => b.0.i(),
            VVal::Lst(l)     => l.borrow().len() as i64,
            VVal::Map(l)     => l.borrow().len() as i64,
            VVal::Usr(u)     => u.i(),
            VVal::Fun(_)     => 1,
            VVal::IVec(iv)   => iv.x_raw(),
            VVal::FVec(fv)   => fv.x_raw() as i64,
            VVal::Iter(i)    => iter_next_value!(i.borrow_mut(), v, { v.i() }, 0),
            v => v.with_deref(|v| v.i(), |_| 0),
        }
    }

    #[allow(clippy::cast_lossless)]
    pub fn b(&self) -> bool {
        match self {
            VVal::Str(s)       => (*s).parse::<i64>().unwrap_or(0) != 0,
            VVal::Sym(s)       => (*s).parse::<i64>().unwrap_or(0) != 0,
            VVal::Byt(s)       => (if s.len() > 0 { s[0] as i64 } else { 0 as i64 }) != 0,
            VVal::None         => false,
            VVal::Err(_)       => false,
            VVal::Bol(b)       => *b,
            VVal::Syn(s)       => (s.syn.clone() as i64) != 0,
            VVal::Pair(b)      => b.0.b() || b.1.b(),
            VVal::Int(i)       => (*i) != 0,
            VVal::Flt(f)       => (*f as i64) != 0,
            VVal::Lst(l)       => (l.borrow().len() as i64) != 0,
            VVal::Map(l)       => (l.borrow().len() as i64) != 0,
            VVal::Usr(u)       => u.b(),
            VVal::Fun(_)       => true,
            VVal::Opt(None)    => false,
            VVal::Opt(Some(_)) => true,
            VVal::IVec(iv)     => iv.x().b(),
            VVal::FVec(fv)     => fv.x().b(),
            VVal::Iter(i)      => iter_next_value!(i.borrow_mut(), v, { v.b() }, false),
            v                  => v.with_deref(|v| v.b(), |_| false),
        }
    }

    pub fn nvec<N: crate::nvec::NVecNum>(&self) -> NVec<N> {
        use NVec::*;
        match self {
            VVal::IVec(i) => N::from_ivec(*i),
            VVal::FVec(f) => N::from_fvec(*f),
            VVal::Map(map)  => {
                let m = map.borrow();
                let o = N::zero().into_vval();
                NVec::from_vval_tpl(
                    (m.get(&s2sym("x")).unwrap_or(&o),
                     m.get(&s2sym("y")).unwrap_or(&o),
                     m.get(&s2sym("z")),
                     m.get(&s2sym("w")))
                ).unwrap_or_else(|| {
                    // The only way from_vval_tpl can fail is if the fourth
                    // parameter is Some(_) but the third is None.
                    // That means that the following will always succeed
                    // (even if the above did not):
                    NVec::from_vval_tpl(
                        (m.get(&s2sym("x")).unwrap_or(&o),
                         m.get(&s2sym("y")).unwrap_or(&o),
                         Some(&o),
                         m.get(&s2sym("w")))
                    ).unwrap()
                })
            },
            VVal::Lst(lst) => {
                let list = lst.borrow();
                let mut lst = list.iter();
                let zero = N::zero().into_vval();
                let (x, y, z, w) = (lst.next(), lst.next(), lst.next(), lst.next());
                // The only way from_vval_tpl can fail is if the fourth
                // parameter is Some(_) but the third is None.
                // That means that the following will always succeed,
                // because lists can't have holes.
                NVec::from_vval_tpl(
                    (x.unwrap_or(&zero), y.unwrap_or(&zero), z, w))
                .unwrap()
            },
            VVal::Pair(p) => {
                NVec::from_vval_tpl((p.0.clone(), p.1.clone(), None, None)).unwrap()
            },
            VVal::Iter(i) => {
                iter_next_value!(
                    i.borrow_mut(), v, { v.nvec::<N>() },
                    NVec::from_vval_tpl(
                        (VVal::Int(0), VVal::Int(0), None, None)).unwrap())
            },
            _ => Vec2(N::from_vval(self), N::zero()),
        }
    }

    fn s_cy(&self, c: &mut CycleCheck) -> String {
        let br = if let Some((do_continue, backref_str)) = c.backref(self) {
            if !do_continue { return backref_str; }
            backref_str
        } else {
            String::from("")
        };
        let s = match self {
            VVal::Str(_)     => self.with_s_ref(|s| format_vval_str(s, false)),
            VVal::Sym(s)     => VVal::dump_sym(&*s),
            VVal::Byt(s)     => format!("$b{}", format_vval_byt(s.as_ref())),
            VVal::None       => "$n".to_string(),
            VVal::Err(e)     => format!("$e{} {}", (*e).borrow().1, (*e).borrow().0.s_cy(c)),
            VVal::Bol(b)     => if *b { "$true".to_string() } else { "$false".to_string() },
            VVal::Syn(s)     => format!("&{:?}", s.syn),
            VVal::Int(i)     => i.to_string(),
            VVal::Flt(f)     => f.to_string(),
            VVal::Pair(b)    => format!("$p({},{})", b.0.s_cy(c), b.1.s_cy(c)),
            VVal::Iter(_)    => "$iter(&)".to_string(),
            VVal::Opt(b)     =>
                if let Some(b) = b { format!("$o({})", b.s_cy(c)) }
                else { "$o()".to_string() },
            VVal::Lst(l)     => VVal::dump_vec_as_str(l, c),
            VVal::Map(l)     => VVal::dump_map_as_str(l, c), // VVal::dump_map_as_str(l),
            VVal::Usr(u)     => u.s(),
            VVal::Fun(f)     => {
                let min = if f.min_args.is_none() { "any".to_string() }
                          else { format!("{}", f.min_args.unwrap()) };
                let max = if f.max_args.is_none() { "any".to_string() }
                          else { format!("{}", f.max_args.unwrap()) };
                let upvalues : String =
                    f.upvalues
                     .iter()
                     .map(|v| v.s_cy(c))
                     .collect::<Vec<String>>()
                     .join(",");
                if let Some(ref sp) = f.syn_pos {
                    format!("&F{{@{},amin={},amax={},locals={},upvalues=$[{}]}}",
                            sp, min, max, f.local_size, upvalues)
                } else {
                    format!("&F{{@[0,0:?()],amin={},amax={},locals={},upvalues=$[{}]}}",
                            min, max, f.local_size, upvalues)
                }
            },
            VVal::DropFun(f) => format!("std:to_drop[{}]", f.v.s_cy(c)),
            VVal::Ref(l)     => format!("$&&{}", (*l).borrow().s_cy(c)),
            VVal::CRef(l)    => format!("$&{}", (*l).borrow().s_cy(c)),
            VVal::FVec(nvec) => nvec.s(),
            VVal::IVec(nvec) => nvec.s(),
            VVal::WWRef(l)   => {
                match l.upgrade() {
                    Some(v) => format!("$(&){}", v.borrow().s_cy(c)),
                    None => "$n".to_string(),
                }
            },
        };
        format!("{}{}", br, s)
    }

    pub fn as_bytes(&self) -> std::vec::Vec<u8> {
        match self {
            VVal::Byt(b) => b.as_ref().clone(),
            v => v.with_deref(
                |v| v.as_bytes(),
                |v| v.map_or_else(
                    || vec![],
                    |v| v.with_s_ref(|s: &str| s.as_bytes().to_vec()))),
        }
    }

    pub fn to_duration(&self) -> Result<std::time::Duration, VVal> {
        match self {
            VVal::Int(i) => Ok(std::time::Duration::from_millis(*i as u64)),
            VVal::Pair(b) => {
                let a = &b.0;
                let b = &b.1;

                a.with_s_ref(|astr|
                    match astr {
                        "s"  => Ok(std::time::Duration::from_secs(b.i() as u64)),
                        "ms" => Ok(std::time::Duration::from_millis(b.i() as u64)),
                        "us" => Ok(std::time::Duration::from_micros(b.i() as u64)),
                        "ns" => Ok(std::time::Duration::from_nanos(b.i() as u64)),
                        _    => Err(VVal::err_msg(&format!("Bad duration: {}", self.s()))),
                    })
            },
            _ => Err(VVal::err_msg(&format!("Bad duration: {}", self.s()))),
        }
    }

    /// Returns a string representation of the VVal data structure.
    /// It handles cyclic data structures fine.
    /// The purpose is to return an unambigous represenation of the data
    /// structure. That means strings are quoted and VVal::None becomes `$n`
    /// for instance.
    ///
    /// If you need strings in pure form, use the `s_raw()` method.
    ///
    /// ```
    /// use wlambda::VVal;
    ///
    /// let v = VVal::None;
    /// assert_eq!(v.s(), "$n");
    ///
    /// assert_eq!(VVal::new_str("Foo").s(), "\"Foo\"");
    /// ```
    pub fn s(&self) -> String {
        let mut cc = CycleCheck::new();
        cc.touch_walk(self);
        self.s_cy(&mut cc)
    }

    /// Serializes the VVal (non cyclic) structure to a msgpack byte vector.
    #[cfg(feature="rmp-serde")]
    pub fn to_msgpack(&self) -> Result<Vec<u8>, String> {
        match rmp_serde::to_vec(self) {
            Ok(s) => Ok(s),
            Err(e) => Err(format!("to_msgpack failed: {}", e))
        }
    }

    /// Creates a VVal structure from a msgpack byte vector.
    #[cfg(feature="rmp-serde")]
    pub fn from_msgpack(s: &[u8]) -> Result<VVal, String> {
        match rmp_serde::from_read_ref(&s) {
            Ok(v) => Ok(v),
            Err(e) => Err(format!("from_msgpack failed: {}", e)),
        }
    }

    /// Serializes the VVal (non cyclic) structure to a JSON string.
    #[cfg(feature="serde_json")]
    pub fn to_json(&self, not_pretty: bool) -> Result<String, String> {
        if not_pretty {
            match serde_json::to_string(self) {
                Ok(s) => Ok(s),
                Err(e) => Err(format!("to_json failed: {}", e))
            }
        } else {
            match serde_json::to_string_pretty(self) {
                Ok(s) => Ok(s),
                Err(e) => Err(format!("to_json failed: {}", e))
            }
        }
    }

    /// Creates a VVal structure from a JSON string.
    #[cfg(feature="serde_json")]
    pub fn from_json(s: &str) -> Result<VVal, String> {
        match serde_json::from_str(&s) {
            Ok(v) => Ok(v),
            Err(e) => Err(format!("from_json failed: {}", e)),
        }
    }
}

#[cfg(feature="serde")]
impl serde::ser::Serialize for VVal {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: serde::ser::Serializer {
        use serde::ser::{SerializeSeq, SerializeMap};

        match self {
            VVal::Str(_)     => self.with_s_ref(|s: &str| serializer.serialize_str(s)),
            VVal::Sym(_)     => self.with_s_ref(|s: &str| serializer.serialize_str(s)),
            VVal::Byt(b)     => serializer.serialize_bytes(&b[..]),
            VVal::None       => serializer.serialize_none(),
            VVal::Iter(_)    => serializer.serialize_none(),
            VVal::Err(_)     => serializer.serialize_str(&self.s()),
            VVal::Bol(b)     => serializer.serialize_bool(*b),
            VVal::Syn(_)     => serializer.serialize_str(&self.s()),
            VVal::Int(i)     => serializer.serialize_i64(*i),
            VVal::Flt(f)     => serializer.serialize_f64(*f),
            VVal::Pair(b)    => {
                let mut seq = serializer.serialize_seq(Some(2))?;
                seq.serialize_element(&b.0)?;
                seq.serialize_element(&b.1)?;
                seq.end()
            },
            VVal::Opt(b)    => {
                if let Some(b) = b {
                    let mut seq = serializer.serialize_seq(Some(1))?;
                    seq.serialize_element(b.as_ref())?;
                    seq.end()
                } else {
                    let seq = serializer.serialize_seq(Some(0))?;
                    seq.end()
                }
            },
            VVal::Lst(l)     => {
                let mut seq = serializer.serialize_seq(Some(l.borrow().len()))?;
                for v in l.borrow().iter() {
                    seq.serialize_element(v)?;
                }
                seq.end()
            },
            VVal::Map(l) => {
                let hm = l.borrow();

                let mut map = serializer.serialize_map(Some(l.borrow().len()))?;
                for (k, v) in hm.iter() {
                    map.serialize_entry(k.as_ref(), v)?;
                }
                map.end()
            },
            VVal::Usr(_)     => serializer.serialize_str(&self.s()),
            VVal::Fun(_)     => serializer.serialize_str(&self.s()),
            VVal::FVec(fv)   => fv.serialize(serializer),
            VVal::IVec(iv)   => iv.serialize(serializer),
            VVal::DropFun(_) => serializer.serialize_str(&self.s()),
            VVal::Ref(_)     => self.deref().serialize(serializer),
            VVal::CRef(_)    => self.deref().serialize(serializer),
            VVal::WWRef(_)   => self.deref().serialize(serializer),
        }
    }
}

#[cfg(feature="serde")]
struct VValVisitor;

#[cfg(feature="serde")]
impl<'de> serde::de::Visitor<'de> for VValVisitor {
    type Value = VVal;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a VVal")
    }

    fn visit_i128<E>(self, value: i128) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(value as i64)) }
    fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(value)) }
    fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(i64::from(value))) }
    fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(i64::from(value))) }
    fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(i64::from(value))) }
    fn visit_u128<E>(self, value: u128) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(value as i64)) }
    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(value as i64)) }
    fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(i64::from(value))) }
    fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(i64::from(value))) }
    fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Int(i64::from(value))) }

    fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Flt(value)) }
    fn visit_f32<E>(self, value: f32) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Flt(f64::from(value))) }

    fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::Bol(value)) }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::new_str(value)) }

    fn visit_bytes<E>(self, value: &[u8]) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::new_byt(value.to_vec())) }

    fn visit_none<E>(self) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::None) }
    fn visit_unit<E>(self) -> Result<Self::Value, E>
        where E: serde::de::Error { Ok(VVal::None) }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
        where A: serde::de::SeqAccess<'de> {

        let v = VVal::vec();

        while let Some(ve) = seq.next_element()? {
            v.push(ve);
        }

        Ok(v)
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
        where A: serde::de::MapAccess<'de> {

        let v = VVal::map();

        while let Some((ke, ve)) = map.next_entry()? {
            let k : VVal = ke;
            v.set_key(&k, ve)
             .expect("Deserialized map not used more than once");
        }

        Ok(v)
    }
}

#[cfg(feature="serde")]
impl<'de> serde::de::Deserialize<'de> for VVal {
    fn deserialize<D>(deserializer: D) -> Result<VVal, D::Error>
        where D: serde::de::Deserializer<'de>,
    {
        deserializer.deserialize_any(VValVisitor)
    }
}