vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
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
//! `Fv` Function
//!
//! Returns a Double specifying the future value of an annuity based on periodic, fixed payments and a fixed interest rate.
//!
//! # Syntax
//!
//! ```vb
//! Fv(rate, nper, pmt[, pv[, type]])
//! ```
//!
//! # Parameters
//!
//! - `rate` - Required. Double specifying interest rate per period. For example, if you get a car loan at an annual percentage rate (APR) of 10 percent and make monthly payments, the rate per period is 0.1/12, or 0.0083.
//! - `nper` - Required. Integer specifying total number of payment periods in the annuity. For example, if you make monthly payments on a four-year car loan, your loan has a total of 4 * 12 (or 48) payment periods.
//! - `pmt` - Required. Double specifying payment to be made each period. Payments usually contain principal and interest that doesn't change over the life of the annuity.
//! - `pv` - Optional. Variant specifying present value (or lump sum) of a series of future payments. For example, when you borrow money to buy a car, the loan amount is the present value to the lender of the monthly car payments you will make. If omitted, 0 is assumed.
//! - `type` - Optional. Variant specifying when payments are due. Use 0 if payments are due at the end of the payment period, or use 1 if payments are due at the beginning of the period. If omitted, 0 is assumed.
//!
//! # Return Value
//!
//! Returns a Double representing the future value of an annuity based on periodic, fixed payments and a fixed interest rate.
//!
//! # Remarks
//!
//! - An annuity is a series of fixed cash payments made over a period of time.
//! - An annuity can be a loan (such as a home mortgage) or an investment (such as a monthly savings plan).
//! - The rate and nper arguments must be calculated using payment periods expressed in the same units.
//! - For example, if rate is calculated using months, nper must also be calculated using months.
//! - For all arguments, cash paid out (such as deposits to savings) is represented by negative numbers; cash received (such as dividend checks) is represented by positive numbers.
//! - `Fv` is related to the `Pv` function. `Fv` calculates what a series of payments will be worth in the future, while `Pv` calculates what a series of future payments is worth now.
//!
//! # Typical Uses
//!
//! - Calculating savings account balance after regular deposits
//! - Determining investment portfolio value after periodic contributions
//! - Computing retirement fund balance
//! - Estimating college savings fund growth
//! - Analyzing long-term investment returns
//! - Planning for future financial goals
//!
//! # Basic Usage Examples
//!
//! ```vb
//! ' Calculate future value of monthly savings
//! Dim monthlyDeposit As Double
//! Dim annualRate As Double
//! Dim years As Integer
//! Dim futureValue As Double
//!
//! monthlyDeposit = -100  ' $100 per month (negative because it's paid out)
//! annualRate = 0.06      ' 6% annual interest
//! years = 10
//!
//! ' Calculate future value
//! futureValue = Fv(annualRate / 12, years * 12, monthlyDeposit)
//! ' Returns approximately $16,387.93
//!
//! ' Calculate future value with initial deposit
//! Dim initialDeposit As Double
//! initialDeposit = -1000  ' $1000 initial deposit
//!
//! futureValue = Fv(annualRate / 12, years * 12, monthlyDeposit, initialDeposit)
//! ' Returns approximately $18,193.97
//!
//! ' Calculate with payments at beginning of period
//! futureValue = Fv(annualRate / 12, years * 12, monthlyDeposit, initialDeposit, 1)
//! ' Returns approximately $18,284.68
//!
//! ' Calculate investment growth with no regular payments
//! Dim lumpSum As Double
//! lumpSum = -5000  ' $5000 one-time investment
//!
//! futureValue = Fv(0.08 / 12, 5 * 12, 0, lumpSum)
//! ' Returns approximately $7,449.23 (compound interest on lump sum)
//! ```
//!
//! # Common Patterns
//!
//! ## 1. Monthly Savings Calculator
//!
//! ```vb
//! Function CalculateSavings(monthlyAmount As Double, years As Integer, _
//!                           annualRate As Double) As Double
//!     Dim monthlyRate As Double
//!     Dim periods As Integer
//!     
//!     monthlyRate = annualRate / 12
//!     periods = years * 12
//!     
//!     ' Negative because it's money paid out
//!     CalculateSavings = Fv(monthlyRate, periods, -monthlyAmount)
//! End Function
//!
//! ' Usage
//! Dim savings As Double
//! savings = CalculateSavings(200, 20, 0.07)  ' $200/month, 20 years, 7%
//! MsgBox "Future value: " & FormatCurrency(savings)
//! ```
//!
//! ## 2. Retirement Planning
//!
//! ```vb
//! Function CalculateRetirementFund(monthlyContribution As Double, _
//!                                  currentAge As Integer, _
//!                                  retirementAge As Integer, _
//!                                  currentBalance As Double, _
//!                                  expectedReturn As Double) As Double
//!     Dim years As Integer
//!     Dim periods As Integer
//!     Dim monthlyRate As Double
//!     
//!     years = retirementAge - currentAge
//!     periods = years * 12
//!     monthlyRate = expectedReturn / 12
//!     
//!     CalculateRetirementFund = Fv(monthlyRate, periods, _
//!                                  -monthlyContribution, _
//!                                  -currentBalance)
//! End Function
//!
//! ' Usage
//! Dim retirementValue As Double
//! retirementValue = CalculateRetirementFund(500, 30, 65, 10000, 0.08)
//! Debug.Print "Retirement fund at 65: " & FormatCurrency(retirementValue)
//! ```
//!
//! ## 3. College Savings Plan
//!
//! ```vb
//! Function CollegeSavingsPlan(yearsUntilCollege As Integer, _
//!                             monthlyDeposit As Double, _
//!                             initialAmount As Double, _
//!                             expectedRate As Double) As Double
//!     Dim monthlyRate As Double
//!     Dim periods As Integer
//!     
//!     monthlyRate = expectedRate / 12
//!     periods = yearsUntilCollege * 12
//!     
//!     CollegeSavingsPlan = Fv(monthlyRate, periods, _
//!                             -monthlyDeposit, _
//!                             -initialAmount)
//! End Function
//!
//! ' Usage
//! Dim collegeFund As Double
//! collegeFund = CollegeSavingsPlan(18, 250, 5000, 0.06)
//! MsgBox "College fund in 18 years: " & FormatCurrency(collegeFund)
//! ```
//!
//! ## 4. Investment Comparison
//!
//! ```vb
//! Sub CompareInvestments()
//!     Dim option1 As Double
//!     Dim option2 As Double
//!     Dim years As Integer
//!     
//!     years = 10
//!     
//!     ' Option 1: $100/month at 6%
//!     option1 = Fv(0.06 / 12, years * 12, -100)
//!     
//!     ' Option 2: $50/month at 8%
//!     option2 = Fv(0.08 / 12, years * 12, -50)
//!     
//!     Debug.Print "Option 1 (6%): " & FormatCurrency(option1)
//!     Debug.Print "Option 2 (8%): " & FormatCurrency(option2)
//!     
//!     If option1 > option2 Then
//!         Debug.Print "Option 1 is better"
//!     Else
//!         Debug.Print "Option 2 is better"
//!     End If
//! End Sub
//! ```
//!
//! ## 5. Compound Interest Calculator
//!
//! ```vb
//! Function CompoundInterest(principal As Double, rate As Double, _
//!                          years As Integer, _
//!                          Optional compoundFrequency As Integer = 12) As Double
//!     Dim periods As Integer
//!     Dim periodRate As Double
//!     
//!     periods = years * compoundFrequency
//!     periodRate = rate / compoundFrequency
//!     
//!     ' No periodic payment, just compound the principal
//!     CompoundInterest = Fv(periodRate, periods, 0, -principal)
//! End Function
//!
//! ' Usage
//! Dim finalAmount As Double
//! finalAmount = CompoundInterest(10000, 0.05, 10, 12)  ' Monthly compounding
//! Debug.Print "Principal grows to: " & FormatCurrency(finalAmount)
//! ```
//!
//! ## 6. Savings Goal Calculator
//!
//! ```vb
//! Function MonthlyDepositNeeded(targetAmount As Double, _
//!                               years As Integer, _
//!                               rate As Double, _
//!                               Optional startingBalance As Double = 0) As Double
//!     ' This is the inverse - given FV, find PMT
//!     ' Using trial and error or formula
//!     Dim monthlyRate As Double
//!     Dim periods As Integer
//!     
//!     monthlyRate = rate / 12
//!     periods = years * 12
//!     
//!     ' Use Pmt function instead for accurate calculation
//!     ' This example shows how Fv relates to the goal
//!     Dim testPayment As Double
//!     testPayment = 100
//!     
//!     Do While Fv(monthlyRate, periods, -testPayment, -startingBalance) < targetAmount
//!         testPayment = testPayment + 10
//!     Loop
//!     
//!     MonthlyDepositNeeded = testPayment
//! End Function
//! ```
//!
//! ## 7. Annuity Future Value
//!
//! ```vb
//! Function AnnuityFutureValue(payment As Double, rate As Double, _
//!                             years As Integer, _
//!                             paymentTiming As Integer) As Double
//!     ' paymentTiming: 0 = end of period, 1 = beginning of period
//!     Dim periods As Integer
//!     
//!     periods = years
//!     AnnuityFutureValue = Fv(rate, periods, -payment, 0, paymentTiming)
//! End Function
//!
//! ' Usage
//! Dim fvOrdinary As Double
//! Dim fvDue As Double
//!
//! fvOrdinary = AnnuityFutureValue(1000, 0.05, 10, 0)  ' Ordinary annuity
//! fvDue = AnnuityFutureValue(1000, 0.05, 10, 1)       ' Annuity due
//!
//! Debug.Print "Ordinary annuity FV: " & FormatCurrency(fvOrdinary)
//! Debug.Print "Annuity due FV: " & FormatCurrency(fvDue)
//! ```
//!
//! ## 8. Investment Portfolio Projection
//!
//! ```vb
//! Type PortfolioProjection
//!     Years As Integer
//!     FutureValue As Double
//! End Type
//!
//! Function ProjectPortfolio(monthlyDeposit As Double, _
//!                          startingBalance As Double, _
//!                          rate As Double, _
//!                          maxYears As Integer) As PortfolioProjection()
//!     Dim projections() As PortfolioProjection
//!     Dim i As Integer
//!     Dim monthlyRate As Double
//!     
//!     ReDim projections(1 To maxYears)
//!     monthlyRate = rate / 12
//!     
//!     For i = 1 To maxYears
//!         projections(i).Years = i
//!         projections(i).FutureValue = Fv(monthlyRate, i * 12, _
//!                                         -monthlyDeposit, _
//!                                         -startingBalance)
//!     Next i
//!     
//!     ProjectPortfolio = projections
//! End Function
//! ```
//!
//! ## 9. Loan Payoff Calculator (Inverse Use)
//!
//! ```vb
//! Sub AnalyzeLoanPayoff()
//!     Dim loanAmount As Double
//!     Dim monthlyPayment As Double
//!     Dim annualRate As Double
//!     Dim years As Integer
//!     Dim remainingBalance As Double
//!     
//!     loanAmount = 200000     ' Initial loan
//!     monthlyPayment = 1200   ' Monthly payment
//!     annualRate = 0.045      ' 4.5% APR
//!     years = 5               ' After 5 years
//!     
//!     ' Future value will be negative (debt remaining)
//!     remainingBalance = -Fv(annualRate / 12, years * 12, _
//!                           monthlyPayment, -loanAmount)
//!     
//!     Debug.Print "Remaining balance after " & years & " years: " & _
//!                 FormatCurrency(remainingBalance)
//! End Sub
//! ```
//!
//! ## 10. Recurring Deposit Calculator
//!
//! ```vb
//! Sub RecurringDepositCalculator()
//!     Dim deposit As Double
//!     Dim rate As Double
//!     Dim quarters As Integer
//!     Dim maturityValue As Double
//!     
//!     deposit = 500           ' Quarterly deposit
//!     rate = 0.06 / 4        ' Quarterly rate (6% annual)
//!     quarters = 20          ' 5 years
//!     
//!     maturityValue = Fv(rate, quarters, -deposit)
//!     
//!     Debug.Print "Maturity value: " & FormatCurrency(maturityValue)
//!     Debug.Print "Total deposits: " & FormatCurrency(deposit * quarters)
//!     Debug.Print "Interest earned: " & _
//!                 FormatCurrency(maturityValue - (deposit * quarters))
//! End Sub
//! ```
//!
//! # Advanced Usage
//!
//! ## 1. Flexible Savings Calculator with UI
//!
//! ```vb
//! Sub CalculateAndDisplay()
//!     Dim monthlyDeposit As Double
//!     Dim years As Integer
//!     Dim annualRate As Double
//!     Dim initialBalance As Double
//!     Dim paymentType As Integer
//!     Dim futureValue As Double
//!     
//!     ' Get inputs from form controls
//!     monthlyDeposit = CDbl(txtMonthlyDeposit.Text)
//!     years = CInt(txtYears.Text)
//!     annualRate = CDbl(txtRate.Text) / 100
//!     initialBalance = CDbl(txtInitialBalance.Text)
//!     
//!     ' Check if payments at beginning or end
//!     paymentType = IIf(chkBeginning.Value = 1, 1, 0)
//!     
//!     ' Calculate
//!     futureValue = Fv(annualRate / 12, years * 12, _
//!                     -monthlyDeposit, _
//!                     -initialBalance, _
//!                     paymentType)
//!     
//!     ' Display result
//!     lblResult.Caption = "Future Value: " & FormatCurrency(futureValue, 2)
//!     
//!     ' Calculate total contributions
//!     Dim totalContributions As Double
//!     totalContributions = initialBalance + (monthlyDeposit * years * 12)
//!     
//!     ' Calculate interest earned
//!     Dim interestEarned As Double
//!     interestEarned = futureValue - totalContributions
//!     
//!     lblTotalDeposits.Caption = "Total Deposits: " & _
//!                                FormatCurrency(totalContributions, 2)
//!     lblInterest.Caption = "Interest Earned: " & _
//!                          FormatCurrency(interestEarned, 2)
//! End Sub
//! ```
//!
//! ## 2. Scenario Analysis
//!
//! ```vb
//! Sub AnalyzeScenarios()
//!     Dim rates() As Double
//!     Dim deposit As Double
//!     Dim years As Integer
//!     Dim i As Integer
//!     
//!     deposit = 300
//!     years = 15
//!     rates = Array(0.04, 0.06, 0.08, 0.10)  ' Different return scenarios
//!     
//!     Debug.Print "Scenario Analysis for $" & deposit & "/month over " & years & " years:"
//!     Debug.Print String(60, "=")
//!     
//!     For i = LBound(rates) To UBound(rates)
//!         Dim fv As Double
//!         fv = Fv(rates(i) / 12, years * 12, -deposit)
//!         
//!         Debug.Print "At " & FormatPercent(rates(i), 0) & ": " & _
//!                    FormatCurrency(fv, 2) & " (gain: " & _
//!                    FormatCurrency(fv - (deposit * years * 12), 2) & ")"
//!     Next i
//! End Sub
//! ```
//!
//! ## 3. Goal-Based Planning
//!
//! ```vb
//! Function YearsToReachGoal(targetAmount As Double, _
//!                           monthlyDeposit As Double, _
//!                           startingBalance As Double, _
//!                           annualRate As Double) As Double
//!     Dim years As Double
//!     Dim fv As Double
//!     Dim monthlyRate As Double
//!     
//!     monthlyRate = annualRate / 12
//!     years = 1
//!     
//!     Do While years <= 50  ' Max 50 years
//!         fv = Fv(monthlyRate, years * 12, -monthlyDeposit, -startingBalance)
//!         
//!         If fv >= targetAmount Then
//!             YearsToReachGoal = years
//!             Exit Function
//!         End If
//!         
//!         years = years + 0.25  ' Check quarterly
//!     Loop
//!     
//!     YearsToReachGoal = -1  ' Goal not reachable
//! End Function
//!
//! ' Usage
//! Dim yearsNeeded As Double
//! yearsNeeded = YearsToReachGoal(500000, 1000, 50000, 0.07)
//!
//! If yearsNeeded > 0 Then
//!     MsgBox "You will reach your goal in " & Format(yearsNeeded, "0.0") & " years"
//! Else
//!     MsgBox "Goal not reachable with current parameters"
//! End If
//! ```
//!
//! ## 4. Monte Carlo Simulation
//!
//! ```vb
//! Function SimulateFutureValue(deposit As Double, years As Integer, _
//!                              avgRate As Double, volatility As Double, _
//!                              simulations As Integer) As Variant
//!     Dim results() As Double
//!     Dim i As Integer
//!     Dim simulatedRate As Double
//!     
//!     ReDim results(1 To simulations)
//!     Randomize
//!     
//!     For i = 1 To simulations
//!         ' Simple random variation around average rate
//!         simulatedRate = avgRate + ((Rnd() - 0.5) * 2 * volatility)
//!         
//!         ' Ensure rate doesn't go negative
//!         If simulatedRate < 0 Then simulatedRate = 0
//!         
//!         results(i) = Fv(simulatedRate / 12, years * 12, -deposit)
//!     Next i
//!     
//!     SimulateFutureValue = results
//! End Function
//!
//! ' Analyze results
//! Sub AnalyzeSimulation()
//!     Dim results As Variant
//!     Dim avg As Double, minVal As Double, maxVal As Double
//!     Dim i As Integer
//!     
//!     results = SimulateFutureValue(500, 20, 0.07, 0.02, 1000)
//!     
//!     avg = 0
//!     minVal = 1E+308
//!     maxVal = -1E+308
//!     
//!     For i = 1 To UBound(results)
//!         avg = avg + results(i)
//!         If results(i) < minVal Then minVal = results(i)
//!         If results(i) > maxVal Then maxVal = results(i)
//!     Next i
//!     
//!     avg = avg / UBound(results)
//!     
//!     Debug.Print "Average FV: " & FormatCurrency(avg, 2)
//!     Debug.Print "Min FV: " & FormatCurrency(minVal, 2)
//!     Debug.Print "Max FV: " & FormatCurrency(maxVal, 2)
//! End Sub
//! ```
//!
//! ## 5. Tax-Advantaged Account Calculator
//!
//! ```vb
//! Function TaxAdvantaged401k(salary As Double, contributionPct As Double, _
//!                            employerMatch As Double, years As Integer, _
//!                            currentBalance As Double, rate As Double) As Double
//!     Dim monthlyContribution As Double
//!     Dim monthlyEmployerMatch As Double
//!     Dim totalMonthlyDeposit As Double
//!     
//!     ' Calculate monthly contributions
//!     monthlyContribution = (salary * contributionPct) / 12
//!     monthlyEmployerMatch = (salary * employerMatch) / 12
//!     totalMonthlyDeposit = monthlyContribution + monthlyEmployerMatch
//!     
//!     TaxAdvantaged401k = Fv(rate / 12, years * 12, _
//!                            -totalMonthlyDeposit, _
//!                            -currentBalance)
//! End Function
//!
//! ' Usage
//! Dim retirement401k As Double
//! retirement401k = TaxAdvantaged401k(75000, 0.06, 0.03, 30, 25000, 0.08)
//! Debug.Print "401(k) at retirement: " & FormatCurrency(retirement401k, 2)
//! ```
//!
//! ## 6. Education Savings with Increasing Contributions
//!
//! ```vb
//! Function EducationSavingsWithIncrease(initialMonthly As Double, _
//!                                       annualIncrease As Double, _
//!                                       years As Integer, _
//!                                       rate As Double) As Double
//!     Dim yearlyFV As Double
//!     Dim currentMonthly As Double
//!     Dim i As Integer
//!     
//!     yearlyFV = 0
//!     currentMonthly = initialMonthly
//!     
//!     For i = 1 To years
//!         ' Calculate FV for this year's contributions
//!         Dim yearContribution As Double
//!         yearContribution = Fv(rate / 12, (years - i + 1) * 12, _
//!                              -currentMonthly)
//!         
//!         yearlyFV = yearlyFV + yearContribution
//!         
//!         ' Increase for next year
//!         currentMonthly = currentMonthly * (1 + annualIncrease)
//!     Next i
//!     
//!     EducationSavingsWithIncrease = yearlyFV
//! End Function
//! ```
//!
//! # Error Handling
//!
//! ```vb
//! Function SafeFv(rate As Double, nper As Integer, pmt As Double, _
//!                 Optional pv As Variant, Optional pType As Variant) As Variant
//!     On Error GoTo ErrorHandler
//!     
//!     ' Validate inputs
//!     If nper <= 0 Then
//!         SafeFv = "Error: Number of periods must be positive"
//!         Exit Function
//!     End If
//!     
//!     If rate <= -1 Then
//!         SafeFv = "Error: Rate must be greater than -100%"
//!         Exit Function
//!     End If
//!     
//!     ' Set defaults
//!     If IsMissing(pv) Then pv = 0
//!     If IsMissing(pType) Then pType = 0
//!     
//!     ' Calculate
//!     SafeFv = Fv(rate, nper, pmt, pv, pType)
//!     Exit Function
//!     
//! ErrorHandler:
//!     Select Case Err.Number
//!         Case 5  ' Invalid procedure call
//!             SafeFv = "Error: Invalid arguments"
//!         Case 6  ' Overflow
//!             SafeFv = "Error: Result too large"
//!         Case 13  ' Type mismatch
//!             SafeFv = "Error: Invalid data types"
//!         Case Else
//!             SafeFv = "Error: " & Err.Description
//!     End Select
//! End Function
//! ```
//!
//! Common errors:
//! - **Error 5 (Invalid procedure call)**: Invalid argument values (e.g., negative periods).
//! - **Error 6 (Overflow)**: Result is too large to fit in a Double.
//! - **Error 13 (Type mismatch)**: Arguments are not numeric.
//!
//! # Performance Considerations
//!
//! - `Fv` is a mathematical calculation, very fast
//! - No I/O or external dependencies
//! - Safe to call repeatedly in loops for scenario analysis
//! - Consider caching results if using same parameters multiple times
//! - For large-scale simulations, consider batch calculations
//!
//! # Best Practices
//!
//! 1. **Use negative values for cash outflows** (payments, deposits)
//! 2. **Use positive values for cash inflows** (receipts, withdrawals)
//! 3. **Match time units** - if rate is monthly, nper should be in months
//! 4. **Validate inputs** - check for reasonable ranges
//! 5. **Handle edge cases** - zero rate, very long periods
//! 6. **Document assumptions** - especially for rate projections
//! 7. **Consider inflation** - future value in today's dollars may differ
//!
//! # Comparison with Other Functions
//!
//! ## `Fv` vs `Pv`
//!
//! ```vb
//! ' Fv: Future value of an investment
//! futureValue = Fv(0.06 / 12, 10 * 12, -100)  ' What will I have?
//!
//! ' Pv: Present value of an investment
//! presentValue = Pv(0.06 / 12, 10 * 12, -100)  ' What is it worth today?
//! ```
//!
//! ## `Fv` vs `NPer`
//!
//! ```vb
//! ' Fv: Calculate future value given payments
//! fv = Fv(0.05 / 12, 120, -100)
//!
//! ' NPer: Calculate periods needed to reach a goal
//! periods = NPer(0.05 / 12, -100, 0, 16000)  ' How long to reach $16,000?
//! ```
//!
//! ## Fv vs Pmt
//!
//! ```vb
//! ' Fv: Calculate future value given payment amount
//! fv = Fv(0.06 / 12, 120, -200)
//!
//! ' Pmt: Calculate payment needed to reach future value
//! payment = Pmt(0.06 / 12, 120, 0, -30000)  ' How much to save for $30,000?
//! ```
//!
//! # Limitations
//!
//! - Assumes constant interest rate (real-world rates vary)
//! - Assumes regular, fixed payments (life is rarely this predictable)
//! - Does not account for taxes, fees, or inflation
//! - Does not consider compounding frequency variations
//! - Limited to Double precision (very large values may overflow)
//! - No built-in risk or uncertainty modeling
//!
//! # Mathematical Formula
//!
//! The future value calculation uses the formula:
//!
//! ```text
//! For type = 0 (payments at end of period):
//! FV = -PV * (1 + rate)^nper - PMT * [((1 + rate)^nper - 1) / rate]
//!
//! For type = 1 (payments at beginning of period):
//! FV = -PV * (1 + rate)^nper - PMT * [((1 + rate)^nper - 1) / rate] * (1 + rate)
//!
//! Special case when rate = 0:
//! FV = -PV - PMT * nper
//! ```
//!
//! # Related Functions
//!
//! - `Pv` - Returns the present value of an annuity
//! - `Pmt` - Returns the payment for an annuity
//! - `PPmt` - Returns the principal payment for a specific period
//! - `IPmt` - Returns the interest payment for a specific period
//! - `NPer` - Returns the number of periods for an annuity
//! - `Rate` - Returns the interest rate per period
//! - `DDB` - Returns depreciation using double-declining balance
//! - `SLN` - Returns straight-line depreciation
//! - `SYD` - Returns sum-of-years' digits depreciation

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn fv_basic() {
        let source = r"result = Fv(0.06 / 12, 120, -100)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_with_pv() {
        let source = r"futureValue = Fv(rate / 12, years * 12, monthlyDeposit, initialDeposit)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_all_parameters() {
        let source =
            r"futureValue = Fv(annualRate / 12, years * 12, monthlyDeposit, initialDeposit, 1)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_no_payment() {
        let source = r"futureValue = Fv(0.08 / 12, 5 * 12, 0, lumpSum)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_in_function() {
        let source = r"Function CalculateSavings() As Double
    CalculateSavings = Fv(monthlyRate, periods, -monthlyAmount)
End Function";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_retirement() {
        let source =
            r"retirementValue = Fv(monthlyRate, periods, -monthlyContribution, -currentBalance)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_debug_print() {
        let source = r#"Debug.Print "Future value: " & Fv(0.05 / 12, 10 * 12, -200)"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_comparison() {
        let source =
            r#"If Fv(0.06 / 12, years * 12, -100) > targetAmount Then MsgBox "Goal reached""#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_assignment() {
        let source = r"Dim fv As Double
fv = Fv(rate, periods, payment)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_in_loop() {
        let source = r"For i = 1 To maxYears
    projections(i).FutureValue = Fv(monthlyRate, i * 12, -monthlyDeposit, -startingBalance)
Next i";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_negative_result() {
        let source = r"balance = Fv(rate / 12, years * 12, payment, -principal)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_formatcurrency() {
        let source = r#"lblResult.Caption = "Future Value: " & FormatCurrency(Fv(rate, periods, payment), 2)"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_array_assignment() {
        let source = r"results(i) = Fv(simulatedRate / 12, years * 12, -deposit)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_error_handling() {
        let source = r"On Error GoTo ErrorHandler
fv = Fv(rate, nper, pmt, pv, pType)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_do_while() {
        let source = r"Do While Fv(monthlyRate, periods, -testPayment, -startingBalance) < targetAmount
    testPayment = testPayment + 10
Loop";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_select_case() {
        let source = r"Select Case investment
    Case 1
        result = Fv(0.06 / 12, years * 12, -100)
End Select";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_iif() {
        let source = r"fv = IIf(useHighRate, Fv(0.08 / 12, periods, -deposit), Fv(0.05 / 12, periods, -deposit))";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_msgbox() {
        let source =
            r#"MsgBox "Your savings will grow to " & FormatCurrency(Fv(rate, periods, -deposit))"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_calculation() {
        let source = r"totalValue = Fv(rate, periods, payment) + bonus";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_type_member() {
        let source = r"investment.FutureValue = Fv(rate, periods, payment, principal)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_subtraction() {
        let source = r"interestEarned = Fv(rate, periods, payment) - totalContributions";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_division() {
        let source = r"monthlyEquivalent = Fv(rate / 12, periods * 12, payment / 12)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_with_cdbl() {
        let source = r"result = Fv(CDbl(txtRate.Text) / 100 / 12, CInt(txtYears.Text) * 12, -CDbl(txtDeposit.Text))";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_quarterly() {
        let source = r"maturityValue = Fv(rate, quarters, -deposit)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_annual() {
        let source = r"fvOrdinary = Fv(rate, periods, -payment, 0, paymentTiming)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn fv_compound_interest() {
        let source = r"finalAmount = Fv(periodRate, periods, 0, -principal)";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings
            .set_snapshot_path("../../../../../snapshots/syntax/library/functions/financial/fv");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}