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
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
//! # Sin Function
//!
//! Returns a Double specifying the sine of an angle.
//!
//! ## Syntax
//!
//! ```vb
//! Sin(number)
//! ```
//!
//! ## Parameters
//!
//! - `number` - Required. Double or any valid numeric expression that expresses an angle in radians.
//!
//! ## Return Value
//!
//! Returns a Double value representing the sine of the angle:
//! - Range: -1 to 1 (inclusive)
//! - Sin(0) = 0
//! - Sin(π/2) ≈ 1
//! - Sin(π) ≈ 0
//! - Sin(3π/2) ≈ -1
//!
//! ## Remarks
//!
//! The Sin function takes an angle in radians and returns the ratio of two sides of a right triangle. The ratio is the length of the side opposite the angle divided by the length of the hypotenuse.
//!
//! Key characteristics:
//! - Input is in radians, not degrees
//! - To convert degrees to radians: radians = degrees × (π / 180)
//! - To convert radians to degrees: degrees = radians × (180 / π)
//! - π (Pi) ≈ 3.14159265358979
//! - Use `Atn(1) * 4` to calculate π in VB6
//! - Periodic function: Sin(x) = Sin(x + 2π)
//! - Returns values between -1 and 1
//!
//! The sine function is one of the fundamental trigonometric functions:
//! - Sin(x): Sine (this function)
//! - Cos(x): Cosine (use Cos function)
//! - Tan(x): Tangent (use Tan function or Sin(x)/Cos(x))
//!
//! Common angles and their sines:
//! - Sin(0°) = Sin(0 rad) = 0
//! - Sin(30°) = Sin(π/6 rad) = 0.5
//! - Sin(45°) = Sin(π/4 rad) ≈ 0.707
//! - Sin(60°) = Sin(π/3 rad) ≈ 0.866
//! - Sin(90°) = Sin(π/2 rad) = 1
//! - Sin(180°) = Sin(π rad) = 0
//! - Sin(270°) = Sin(3π/2 rad) = -1
//! - Sin(360°) = Sin(2π rad) = 0
//!
//! ## Typical Uses
//!
//! 1. **Wave Generation**: Create sine waves for animations or signals
//! 2. **Circular Motion**: Calculate vertical position in circular paths
//! 3. **Oscillations**: Model periodic oscillating systems
//! 4. **Physics Simulations**: Projectile motion, pendulum swing
//! 5. **Graphics**: Rotation, transformation, curve drawing
//! 6. **Audio Processing**: Sine wave tone generation
//! 7. **Engineering Calculations**: Structural analysis, AC circuits
//! 8. **Game Development**: Movement patterns, trajectories
//!
//! ## Basic Examples
//!
//! ```vb
//! ' Example 1: Calculate sine of 45 degrees
//! Const PI As Double = 3.14159265358979
//! Dim angle45 As Double
//! Dim sineValue As Double
//!
//! angle45 = 45 * (PI / 180)  ' Convert to radians
//! sineValue = Sin(angle45)   ' Returns ≈ 0.707
//! ```
//!
//! ```vb
//! ' Example 2: Calculate sine of π/2 (90 degrees)
//! Const PI As Double = 3.14159265358979
//! Dim result As Double
//! result = Sin(PI / 2)  ' Returns 1
//! ```
//!
//! ```vb
//! ' Example 3: Use with Atn to calculate π
//! Dim pi As Double
//! Dim sineValue As Double
//! pi = Atn(1) * 4
//! sineValue = Sin(pi)  ' Returns ≈ 0 (very small number)
//! ```
//!
//! ```vb
//! ' Example 4: Sine wave generation
//! Dim i As Integer
//! Dim y As Double
//! For i = 0 To 360
//!     y = Sin(i * (Atn(1) * 4) / 180)
//!     Debug.Print i & " degrees: " & y
//! Next i
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: `DegreesToRadians`
//! Convert degrees to radians for Sin function
//! ```vb
//! Function DegreesToRadians(degrees As Double) As Double
//!     Const PI As Double = 3.14159265358979
//!     DegreesToRadians = degrees * (PI / 180)
//! End Function
//!
//! ' Usage:
//! result = Sin(DegreesToRadians(45))
//! ```
//!
//! ### Pattern 2: `SinDegrees`
//! Sine function that accepts degrees
//! ```vb
//! Function SinDegrees(degrees As Double) As Double
//!     Const PI As Double = 3.14159265358979
//!     SinDegrees = Sin(degrees * (PI / 180))
//! End Function
//! ```
//!
//! ### Pattern 3: `GenerateSineWave`
//! Generate array of sine wave values
//! ```vb
//! Function GenerateSineWave(samples As Integer, amplitude As Double, _
//!                           frequency As Double) As Double()
//!     Dim result() As Double
//!     Dim i As Integer
//!     Dim angle As Double
//!     Const PI As Double = 3.14159265358979
//!     
//!     ReDim result(0 To samples - 1)
//!     
//!     For i = 0 To samples - 1
//!         angle = (i / samples) * 2 * PI * frequency
//!         result(i) = amplitude * Sin(angle)
//!     Next i
//!     
//!     GenerateSineWave = result
//! End Function
//! ```
//!
//! ### Pattern 4: `CircularMotionY`
//! Calculate vertical position in circular motion
//! ```vb
//! Function CircularMotionY(centerY As Double, radius As Double, _
//!                          angle As Double) As Double
//!     ' angle in radians
//!     CircularMotionY = centerY + radius * Sin(angle)
//! End Function
//! ```
//!
//! ### Pattern 5: `OscillatingValue`
//! Create oscillating value over time
//! ```vb
//! Function OscillatingValue(time As Double, amplitude As Double, _
//!                           frequency As Double, Optional phase As Double = 0) As Double
//!     Const PI As Double = 3.14159265358979
//!     OscillatingValue = amplitude * Sin(2 * PI * frequency * time + phase)
//! End Function
//! ```
//!
//! ### Pattern 6: `SineInterpolation`
//! Smooth interpolation using sine
//! ```vb
//! Function SineInterpolation(startValue As Double, endValue As Double, _
//!                            t As Double) As Double
//!     ' t ranges from 0 to 1
//!     Dim factor As Double
//!     Const PI As Double = 3.14159265358979
//!     
//!     factor = (1 - Cos(t * PI)) / 2
//!     SineInterpolation = startValue + (endValue - startValue) * factor
//! End Function
//! ```
//!
//! ### Pattern 7: `AngleFromSine`
//! Get angle from sine value (inverse sine approximation)
//! ```vb
//! Function ArcSineApprox(sineValue As Double) As Double
//!     ' For small angles, asin(x) ≈ x
//!     ' For better accuracy, use iterative methods or Atn
//!     ' Using Atn for proper arcsin:
//!     If Abs(sineValue) >= 1 Then
//!         ArcSineApprox = Sgn(sineValue) * Atn(1) * 2
//!     Else
//!         ArcSineApprox = Atn(sineValue / Sqr(1 - sineValue * sineValue))
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 8: `SineWaveAnalysis`
//! Analyze sine wave properties
//! ```vb
//! Sub AnalyzeSineWave(amplitude As Double, frequency As Double, _
//!                     ByRef maxVal As Double, ByRef minVal As Double, _
//!                     ByRef period As Double)
//!     Const PI As Double = 3.14159265358979
//!     
//!     maxVal = amplitude
//!     minVal = -amplitude
//!     period = 1 / frequency  ' In seconds or time units
//! End Sub
//! ```
//!
//! ### Pattern 9: `ProjectileMotionY`
//! Calculate vertical position in projectile motion
//! ```vb
//! Function ProjectileY(initialY As Double, velocity As Double, _
//!                      angle As Double, time As Double, gravity As Double) As Double
//!     ' angle in radians
//!     Dim verticalVelocity As Double
//!     
//!     verticalVelocity = velocity * Sin(angle)
//!     ProjectileY = initialY + verticalVelocity * time - 0.5 * gravity * time * time
//! End Function
//! ```
//!
//! ### Pattern 10: `HarmonicMotion`
//! Simple harmonic motion displacement
//! ```vb
//! Function HarmonicDisplacement(amplitude As Double, angularFrequency As Double, _
//!                               time As Double, Optional phase As Double = 0) As Double
//!     HarmonicDisplacement = amplitude * Sin(angularFrequency * time + phase)
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: `WaveformGenerator` Class
//! Generate various waveforms using sine function
//! ```vb
//! ' Class: WaveformGenerator
//! Private Const PI As Double = 3.14159265358979
//! Private m_sampleRate As Long
//! Private m_duration As Double
//!
//! Public Sub Initialize(sampleRate As Long, duration As Double)
//!     m_sampleRate = sampleRate
//!     m_duration = duration
//! End Sub
//!
//! Public Function GenerateSineWave(frequency As Double, amplitude As Double) As Double()
//!     Dim samples As Long
//!     Dim result() As Double
//!     Dim i As Long
//!     Dim t As Double
//!     
//!     samples = CLng(m_sampleRate * m_duration)
//!     ReDim result(0 To samples - 1)
//!     
//!     For i = 0 To samples - 1
//!         t = i / m_sampleRate
//!         result(i) = amplitude * Sin(2 * PI * frequency * t)
//!     Next i
//!     
//!     GenerateSineWave = result
//! End Function
//!
//! Public Function GenerateAMWave(carrier As Double, modulator As Double, _
//!                                amplitude As Double, modDepth As Double) As Double()
//!     ' Amplitude Modulation
//!     Dim samples As Long
//!     Dim result() As Double
//!     Dim i As Long
//!     Dim t As Double
//!     Dim envelope As Double
//!     
//!     samples = CLng(m_sampleRate * m_duration)
//!     ReDim result(0 To samples - 1)
//!     
//!     For i = 0 To samples - 1
//!         t = i / m_sampleRate
//!         envelope = 1 + modDepth * Sin(2 * PI * modulator * t)
//!         result(i) = amplitude * envelope * Sin(2 * PI * carrier * t)
//!     Next i
//!     
//!     GenerateAMWave = result
//! End Function
//!
//! Public Function GenerateFMWave(carrier As Double, modulator As Double, _
//!                                amplitude As Double, modIndex As Double) As Double()
//!     ' Frequency Modulation
//!     Dim samples As Long
//!     Dim result() As Double
//!     Dim i As Long
//!     Dim t As Double
//!     Dim phase As Double
//!     
//!     samples = CLng(m_sampleRate * m_duration)
//!     ReDim result(0 To samples - 1)
//!     
//!     For i = 0 To samples - 1
//!         t = i / m_sampleRate
//!         phase = 2 * PI * carrier * t + modIndex * Sin(2 * PI * modulator * t)
//!         result(i) = amplitude * Sin(phase)
//!     Next i
//!     
//!     GenerateFMWave = result
//! End Function
//!
//! Public Function GenerateHarmonics(fundamental As Double, harmonics As Integer, _
//!                                   amplitude As Double) As Double()
//!     ' Generate complex tone with harmonics
//!     Dim samples As Long
//!     Dim result() As Double
//!     Dim i As Long
//!     Dim h As Integer
//!     Dim t As Double
//!     Dim value As Double
//!     
//!     samples = CLng(m_sampleRate * m_duration)
//!     ReDim result(0 To samples - 1)
//!     
//!     For i = 0 To samples - 1
//!         t = i / m_sampleRate
//!         value = 0
//!         
//!         For h = 1 To harmonics
//!             value = value + (amplitude / h) * Sin(2 * PI * fundamental * h * t)
//!         Next h
//!         
//!         result(i) = value
//!     Next i
//!     
//!     GenerateHarmonics = result
//! End Function
//! ```
//!
//! ### Example 2: `CircularMotion` Module
//! Calculate circular and elliptical motion using trigonometry
//! ```vb
//! ' Module: CircularMotion
//! Private Const PI As Double = 3.14159265358979
//!
//! Public Sub GetCircularPosition(centerX As Double, centerY As Double, _
//!                                radius As Double, angle As Double, _
//!                                ByRef x As Double, ByRef y As Double)
//!     ' angle in radians
//!     x = centerX + radius * Cos(angle)
//!     y = centerY + radius * Sin(angle)
//! End Sub
//!
//! Public Sub GetEllipticalPosition(centerX As Double, centerY As Double, _
//!                                  radiusX As Double, radiusY As Double, _
//!                                  angle As Double, ByRef x As Double, ByRef y As Double)
//!     ' angle in radians
//!     x = centerX + radiusX * Cos(angle)
//!     y = centerY + radiusY * Sin(angle)
//! End Sub
//!
//! Public Function CalculateAngularVelocity(rpm As Double) As Double
//!     ' Convert revolutions per minute to radians per second
//!     CalculateAngularVelocity = (rpm / 60) * 2 * PI
//! End Function
//!
//! Public Sub AnimateCircularMotion(centerX As Double, centerY As Double, _
//!                                  radius As Double, angularVelocity As Double, _
//!                                  time As Double, ByRef x As Double, ByRef y As Double)
//!     Dim angle As Double
//!     angle = angularVelocity * time
//!     
//!     x = centerX + radius * Cos(angle)
//!     y = centerY + radius * Sin(angle)
//! End Sub
//!
//! Public Function CalculateTangentialVelocity(radius As Double, _
//!                                             angularVelocity As Double) As Double
//!     ' v = r * ω
//!     CalculateTangentialVelocity = radius * angularVelocity
//! End Function
//!
//! Public Sub GetVelocityComponents(speed As Double, angle As Double, _
//!                                  ByRef vx As Double, ByRef vy As Double)
//!     ' angle in radians from horizontal
//!     vx = speed * Cos(angle)
//!     vy = speed * Sin(angle)
//! End Sub
//! ```
//!
//! ### Example 3: `PhysicsSimulator` Class
//! Simulate physics using trigonometric functions
//! ```vb
//! ' Class: PhysicsSimulator
//! Private Const PI As Double = 3.14159265358979
//! Private Const GRAVITY As Double = 9.81  ' m/s²
//!
//! Public Function CalculateRange(velocity As Double, angle As Double) As Double
//!     ' Projectile range formula: R = v² * sin(2θ) / g
//!     ' angle in radians
//!     CalculateRange = (velocity * velocity * Sin(2 * angle)) / GRAVITY
//! End Function
//!
//! Public Function CalculateMaxHeight(velocity As Double, angle As Double) As Double
//!     ' Max height: H = (v * sin(θ))² / (2g)
//!     Dim verticalVelocity As Double
//!     verticalVelocity = velocity * Sin(angle)
//!     CalculateMaxHeight = (verticalVelocity * verticalVelocity) / (2 * GRAVITY)
//! End Function
//!
//! Public Function CalculateTimeOfFlight(velocity As Double, angle As Double) As Double
//!     ' Time of flight: T = 2 * v * sin(θ) / g
//!     CalculateTimeOfFlight = (2 * velocity * Sin(angle)) / GRAVITY
//! End Function
//!
//! Public Sub GetProjectilePosition(velocity As Double, angle As Double, _
//!                                  time As Double, ByRef x As Double, ByRef y As Double)
//!     ' angle in radians
//!     Dim vx As Double, vy As Double
//!     
//!     vx = velocity * Cos(angle)
//!     vy = velocity * Sin(angle)
//!     
//!     x = vx * time
//!     y = vy * time - 0.5 * GRAVITY * time * time
//! End Sub
//!
//! Public Function CalculatePendulumDisplacement(length As Double, angle0 As Double, _
//!                                               time As Double) As Double
//!     ' Small angle approximation
//!     ' angle(t) = angle0 * cos(ωt) where ω = sqrt(g/L)
//!     Dim omega As Double
//!     omega = Sqr(GRAVITY / length)
//!     
//!     ' For small angles, displacement ≈ L * θ
//!     CalculatePendulumDisplacement = length * angle0 * Cos(omega * time)
//! End Function
//!
//! Public Function CalculateInclinedPlaneForce(mass As Double, angle As Double) As Double
//!     ' Force down incline: F = m * g * sin(θ)
//!     ' angle in radians
//!     CalculateInclinedPlaneForce = mass * GRAVITY * Sin(angle)
//! End Function
//! ```
//!
//! ### Example 4: `GraphicsHelper` Module
//! Graphics and animation helpers using trigonometry
//! ```vb
//! ' Module: GraphicsHelper
//! Private Const PI As Double = 3.14159265358979
//!
//! Public Function RotatePointX(x As Double, y As Double, angle As Double, _
//!                              centerX As Double, centerY As Double) As Double
//!     ' Rotate point around center, return new X
//!     ' angle in radians
//!     Dim dx As Double, dy As Double
//!     
//!     dx = x - centerX
//!     dy = y - centerY
//!     
//!     RotatePointX = centerX + dx * Cos(angle) - dy * Sin(angle)
//! End Function
//!
//! Public Function RotatePointY(x As Double, y As Double, angle As Double, _
//!                              centerX As Double, centerY As Double) As Double
//!     ' Rotate point around center, return new Y
//!     ' angle in radians
//!     Dim dx As Double, dy As Double
//!     
//!     dx = x - centerX
//!     dy = y - centerY
//!     
//!     RotatePointY = centerY + dx * Sin(angle) + dy * Cos(angle)
//! End Function
//!
//! Public Function CreatePulseEffect(time As Double, frequency As Double) As Double
//!     ' Create pulsing effect (0 to 1)
//!     CreatePulseEffect = (Sin(2 * PI * frequency * time) + 1) / 2
//! End Function
//!
//! Public Function CreateFadeInOut(time As Double, duration As Double) As Double
//!     ' Smooth fade in and out using sine
//!     Dim t As Double
//!     t = (time / duration) * PI
//!     CreateFadeInOut = Sin(t)
//! End Function
//!
//! Public Function EaseInOutSine(t As Double) As Double
//!     ' Easing function using sine (t from 0 to 1)
//!     EaseInOutSine = -(Cos(PI * t) - 1) / 2
//! End Function
//!
//! Public Sub DrawSineWave(picBox As Object, amplitude As Double, _
//!                         frequency As Double, Optional phase As Double = 0)
//!     Dim x As Integer
//!     Dim y As Double
//!     Dim prevX As Integer, prevY As Integer
//!     Dim width As Integer
//!     
//!     width = picBox.ScaleWidth
//!     
//!     For x = 0 To width
//!         y = amplitude * Sin(2 * PI * frequency * (x / width) + phase)
//!         y = picBox.ScaleHeight / 2 - y  ' Flip Y axis
//!         
//!         If x > 0 Then
//!             picBox.Line (prevX, prevY)-(x, y)
//!         End If
//!         
//!         prevX = x
//!         prevY = y
//!     Next x
//! End Sub
//! ```
//!
//! ## Error Handling
//!
//! The Sin function can generate the following errors:
//!
//! - **Error 13** (Type mismatch): Argument cannot be interpreted as numeric
//! - **Error 5** (Invalid procedure call): In rare cases with invalid input
//!
//! Error handling example:
//! ```vb
//! On Error Resume Next
//! result = Sin(angle)
//! If Err.Number <> 0 Then
//!     MsgBox "Error calculating sine: " & Err.Description
//! End If
//! ```
//!
//! ## Performance Considerations
//!
//! - Sin is a relatively fast mathematical function
//! - Uses hardware FPU for calculation when available
//! - For repeated calculations with same angles, consider caching results
//! - Lookup tables can be faster for real-time applications with limited angle sets
//! - Modern CPUs execute Sin very quickly (microseconds)
//!
//! ## Best Practices
//!
//! 1. **Use Radians**: Remember Sin takes radians, not degrees
//! 2. **Convert Carefully**: Use consistent conversion factor for degrees↔radians
//! 3. **Cache Pi**: Define PI as a constant rather than calculating repeatedly
//! 4. **Range Awareness**: Sin always returns -1 to 1
//! 5. **Precision**: Be aware of floating-point precision limits
//! 6. **Angle Normalization**: For large angles, consider normalizing to 0-2π
//! 7. **Avoid Division**: Use multiplication by inverse when possible
//! 8. **Test Edge Cases**: Test with 0, π/2, π, 3π/2, 2π
//! 9. **Document Units**: Always document whether angles are in degrees or radians
//! 10. **Combine Functions**: Use with Cos, Tan for complete trigonometric operations
//!
//! ## Comparison with Related Functions
//!
//! | Function | Input (radians) | Output Range | Description |
//! |----------|-----------------|--------------|-------------|
//! | Sin | angle | -1 to 1 | Sine of angle |
//! | Cos | angle | -1 to 1 | Cosine of angle |
//! | Tan | angle | -∞ to +∞ | Tangent of angle |
//! | Atn | ratio | -π/2 to π/2 | Arctangent (inverse tangent) |
//! | Sqr | number ≥ 0 | ≥ 0 | Square root |
//!
//! ## Platform Considerations
//!
//! - Available in VB6, VBA (all versions)
//! - Uses system math library
//! - Precision depends on Double data type (IEEE 754)
//! - Results consistent across Windows versions
//! - Very small return values near multiples of π due to floating-point precision
//!
//! ## Limitations
//!
//! - Input must be in radians (no built-in degree support)
//! - Floating-point precision limits (≈15-17 decimal digits)
//! - Sin(π) returns very small number, not exactly 0
//! - Large angle values may accumulate rounding errors
//! - No complex number support
//! - No automatic angle normalization
//!
//! ## Related Functions
//!
//! - `Cos`: Returns the cosine of an angle in radians
//! - `Tan`: Returns the tangent of an angle in radians
//! - `Atn`: Returns the arctangent of a number in radians
//! - `Sqr`: Returns the square root (used in inverse sine calculations)
//! - `Abs`: Returns absolute value (useful for angle normalization)
//!
#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn sin_basic() {
        let source = r"
Sub Test()
    Dim result As Double
    result = Sin(1.5708)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_with_pi() {
        let source = r"
Sub Test()
    Dim pi As Double
    Dim sineValue As Double
    pi = Atn(1) * 4
    sineValue = Sin(pi / 2)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_if_statement() {
        let source = r#"
Sub Test()
    If Sin(angle) > 0.5 Then
        MsgBox "Greater than 0.5"
    End If
End Sub
"#;
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_function_return() {
        let source = r"
Function CalculateSine(angle As Double) As Double
    CalculateSine = Sin(angle)
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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_variable_assignment() {
        let source = r"
Sub Test()
    Dim y As Double
    y = Sin(x)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_msgbox() {
        let source = r#"
Sub Test()
    MsgBox "Sine value: " & Sin(angle)
End Sub
"#;
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_debug_print() {
        let source = r"
Sub Test()
    Debug.Print Sin(1.0472)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_select_case() {
        let source = r#"
Sub Test()
    Select Case Sin(angle)
        Case Is > 0
            MsgBox "Positive"
        Case Is < 0
            MsgBox "Negative"
        Case Else
            MsgBox "Zero"
    End Select
End Sub
"#;
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_class_usage() {
        let source = r"
Class TrigCalculator
    Public Function GetSine(angle As Double) As Double
        GetSine = Sin(angle)
    End Function
End Class
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_with_statement() {
        let source = r"
Sub Test()
    With Calculator
        Dim s As Double
        s = Sin(angle)
    End With
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_elseif() {
        let source = r#"
Sub Test()
    If Sin(a) > 0.9 Then
        MsgBox "High"
    ElseIf Sin(a) > 0.5 Then
        MsgBox "Medium"
    Else
        MsgBox "Low"
    End If
End Sub
"#;
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_for_loop() {
        let source = r"
Sub Test()
    Dim i As Integer
    For i = 0 To 360
        Debug.Print Sin(i)
    Next i
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_do_while() {
        let source = r"
Sub Test()
    Do While Sin(angle) < 1
        angle = angle + 0.1
    Loop
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_do_until() {
        let source = r"
Sub Test()
    Do Until Sin(x) > threshold
        x = x + step
    Loop
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_while_wend() {
        let source = r"
Sub Test()
    While Abs(Sin(angle)) > 0.01
        angle = angle - 0.1
    Wend
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_parentheses() {
        let source = r"
Sub Test()
    Dim value As Double
    value = (Sin(a) + Sin(b)) / 2
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_iif() {
        let source = r#"
Sub Test()
    Dim msg As String
    msg = IIf(Sin(angle) > 0, "Positive", "Non-positive")
End Sub
"#;
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_array_assignment() {
        let source = r"
Sub Test()
    Dim waveform(100) As Double
    waveform(0) = Sin(angle)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_property_assignment() {
        let source = r"
Class Point
    Public Y As Double
End Class

Sub Test()
    Dim pt As New Point
    pt.Y = Sin(angle)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_function_argument() {
        let source = r"
Sub ProcessValue(v As Double)
End Sub

Sub Test()
    ProcessValue Sin(angle)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_concatenation() {
        let source = r#"
Sub Test()
    Dim output As String
    output = "Sine: " & Sin(angle)
End Sub
"#;
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_comparison() {
        let source = r"
Sub Test()
    Dim isPositive As Boolean
    isPositive = (Sin(angle) > 0)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_arithmetic() {
        let source = r"
Sub Test()
    Dim amplitude As Double
    Dim wave As Double
    wave = amplitude * Sin(angle)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_degrees_conversion() {
        let source = r"
Sub Test()
    Const PI As Double = 3.14159265358979
    Dim degrees As Double
    Dim radians As Double
    Dim result As Double
    degrees = 45
    radians = degrees * (PI / 180)
    result = Sin(radians)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_error_handling() {
        let source = r#"
Sub Test()
    On Error Resume Next
    Dim s As Double
    s = Sin(inputValue)
    If Err.Number <> 0 Then
        MsgBox "Error"
    End If
End Sub
"#;
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_on_error_goto() {
        let source = r#"
Sub Test()
    On Error GoTo ErrorHandler
    Dim sineVal As Double
    sineVal = Sin(angle)
    Exit Sub
ErrorHandler:
    MsgBox "Error calculating sine"
End Sub
"#;
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn sin_circular_motion() {
        let source = r"
Sub Test()
    Dim y As Double
    Dim radius As Double
    y = centerY + radius * Sin(angle)
End Sub
";
        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/math/sin");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}