-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScriptMod2.bas
More file actions
1798 lines (1772 loc) · 61.9 KB
/
Copy pathScriptMod2.bas
File metadata and controls
1798 lines (1772 loc) · 61.9 KB
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
Attribute VB_Name = "ScriptMod2"
Option Explicit
'Scripting Module v2.0
'by MasamuneXGP
Private Type ValueType
Real As Boolean
Link As Long
Op As Operators
UseNeg As Boolean
UseNot As Boolean
UseAlt As Boolean
End Type
Private Type TermType
V() As ValueType
End Type
Private Type BlockType
FunctionNum As Long
ArgList() As TermType
End Type
Private Type LineType
LineNum As Long
CommandNum As Long
MainBlock As Long
End Type
Private Type EventType
L() As LineType
End Type
Private Type ArgType
Name As String
Type As VbVarType
End Type
Private Type VarStruct
FuncLink As Long
Value() As Variant
LB As Long
UB As Long
End Type
Private Type FuncType
Name As String
Args() As ArgType
Type As VbVarType
Index As Long
IsVar As Boolean
Opt As Byte
End Type
Private Type CommandType
Name As String
Args() As ArgType
Index As Long
End Type
Private Type LevelType
Type As Integer
Brace As Boolean
StartNum As Long
StartLine As String
End Type
Private Type ForStruct
LNum As Long
Var As Long
LB As Variant
UB As Variant
Stp As Variant
Jump As Long
End Type
Type LocalType
Vars() As VarStruct
Funcs() As FuncType
End Type
Private Enum Operators
nbsNone
nbsExpon
nbsNegat
nbsMulti
nbsFloatDiv
nbsMod
nbsAdd
nbsConcat
nbsEqual
nbsInequal
nbsLessThan
nbsGreaterThan
nbsLessEqual
nbsGreaterEqual
nbsNot
nbsAnd
nbsOr
nbsXor
nbsEqv
End Enum
Private StackLevel As Long
Private Stack() As LocalType
Private VarNum As Long
Private FuncNum As Long
Private Values() As Variant
Private EventList() As EventType
Private EventName() As String
Private VarList() As VarStruct
Private FuncList() As EventType
Private FuncDefs() As FuncType
Private SubList() As EventType
Private SubDefs() As CommandType
Private BlockList() As BlockType
Const REALSUBSTART As Long = 6
Const FUNCLIMIT As Long = 35
Const SUBLIMIT As Long = 35
Const EVENTNUM As Long = 15
Public Sub InitScript()
'ReDim Values(0)
ReDim FuncDefs(0 To FUNCLIMIT)
ReDim FuncList(0)
ReDim SubDefs(0 To SUBLIMIT)
ReDim SubList(0)
ReDim EventList(0 To EVENTNUM * 2)
ReDim EventName(0 To EVENTNUM)
'==============COMMANDS==============
'****INTERNAL NON-SCRIPT COMMANDS****
With SubDefs(1)
'1 Set %VarIndex, %VarSubscript, !NewValue
' - Sets a variable to the assigned value. NewValue's
' type is determined by the Variable's type.
ReDim .Args(1 To 3)
.Args(1).Type = vbLong
.Args(2).Type = vbLong
End With
With SubDefs(2)
'2 GoTo %LineNum
' - Moves the execution point to the line specified by
' LineNum.
ReDim .Args(1 To 1)
.Args(1).Type = vbLong
End With
With SubDefs(3)
'3 If @Condition, %LineNum
' - If Condition is False, moves the execution point to the
' line specified by LineNum.
ReDim .Args(1 To 2)
.Args(1).Type = vbBoolean
.Args(2).Type = vbLong
End With
With SubDefs(4)
'4 For %IncVarIndex, #Start, #End, #Step, %LineNum
' - Initiates a For-Next loop. If it is already initiated,
' it increases the value of the variable of IncVarIndex by
' #Step. If the value is greater than #End, it moves the
' execution point to the line specified by LineNum.
ReDim .Args(1 To 5)
.Args(1).Type = vbLong
.Args(2).Type = vbDouble
.Args(3).Type = vbDouble
.Args(4).Type = vbDouble
.Args(5).Type = vbLong
End With
With SubDefs(5)
'5 Local %VarType, $VarName, %LBound, %UBound
' - Sets a Local Variable with type %VarType,
' name $VarName, and a subscript of %LBound
' to %UBound
ReDim .Args(1 To 4)
.Args(1).Type = vbLong
.Args(2).Type = vbString
.Args(3).Type = vbLong
.Args(4).Type = vbLong
End With
'****SCRIPTABLE COMMANDS****
With SubDefs(6)
'6 Print
' - Adds the argument to the server Text Box
.Name = "?"
ReDim .Args(1 To 1)
'.Args(1).Type = vbString
End With
Call SetFuncs
End Sub
Private Sub SetFuncs()
ReDim FuncDefs(0).Args(1 To 1) 'No Function
With FuncDefs(1)
'$CStr(!Var)
.Name = "CStr"
ReDim .Args(1 To 1)
End With
With FuncDefs(2)
'%CInt(!Var)
.Name = "CInt"
ReDim .Args(1 To 1)
End With
With FuncDefs(3)
'#CFloat(!Var)
.Name = "CFloat"
ReDim .Args(1 To 1)
End With
With FuncDefs(4)
'@CBool(!Var)
.Name = "CBool"
ReDim .Args(1 To 1)
End With
With FuncDefs(5)
'#Round(#Num, [%Places = 0])
.Name = "Round"
ReDim .Args(1 To 2)
.Args(1).Type = vbDouble
.Args(2).Type = vbLong
.Opt = 1
End With
With FuncDefs(6)
'#SysTimer
.Name = "SysTimer"
End With
With FuncDefs(7)
'%Random(%Bound1, [%Bound2 = 0])
.Name = "Random"
ReDim .Args(1 To 2)
.Args(1).Type = vbLong
.Args(2).Type = vbLong
.Opt = 1
End With
With FuncDefs(8)
'#Rnd
.Name = "Rnd"
End With
With FuncDefs(9)
'%Asc($Chr)
.Name = "Asc"
ReDim .Args(1 To 1)
.Args(1).Type = vbString
End With
With FuncDefs(10)
'$Chr(%Asc)
.Name = "Chr"
ReDim .Args(1 To 1)
.Args(1).Type = vbLong
End With
With FuncDefs(11)
'%Len($Text)
.Name = "Len"
ReDim .Args(1 To 1)
.Args(1).Type = vbString
End With
With FuncDefs(12)
'%Search($Text, $Find, [%Start = 1], [@CaseSense = False])
.Name = "Search"
ReDim .Args(1 To 4)
.Args(1).Type = vbString
.Args(2).Type = vbString
.Args(3).Type = vbLong
.Args(4).Type = vbBoolean
.Opt = 2
End With
With FuncDefs(13)
'$Replace($Text, $Find, $ReplaceWith)
.Name = "Replace"
ReDim .Args(1 To 3)
.Args(1).Type = vbString
.Args(2).Type = vbString
.Args(3).Type = vbString
End With
With FuncDefs(14)
'$Left($Text, %Number)
.Name = "Left"
ReDim .Args(1 To 2)
.Args(1).Type = vbString
.Args(2).Type = vbLong
End With
With FuncDefs(15)
'$Right($Text, %Number)
.Name = "Right"
ReDim .Args(1 To 2)
.Args(1).Type = vbString
.Args(2).Type = vbLong
End With
With FuncDefs(16)
'$Mid($Text, %Start, %Length)
.Name = "Mid"
ReDim .Args(1 To 3)
.Args(1).Type = vbString
.Args(2).Type = vbLong
.Args(3).Type = vbLong
End With
With FuncDefs(17)
'$LCase($Text)
.Name = "LCase"
ReDim .Args(1 To 1)
.Args(1).Type = vbString
End With
With FuncDefs(18)
'$UCase($Text)
.Name = "UCase"
ReDim .Args(1 To 1)
.Args(1).Type = vbString
End With
With FuncDefs(19)
'$Trim($Text)
.Name = "Trim"
ReDim .Args(1 To 1)
.Args(1).Type = vbString
End With
End Sub
Public Function CompileScript(ByVal Script As String) As String
Dim A As Long
Dim B As Long
Dim X As Long
Dim Y As Long
Dim Z As Long
Dim L As Long
Dim T As Single
Dim Temp As String
Dim Piece As String
Dim Build As String
Dim E As String
Dim iLine() As String
Dim Strings() As String
Dim Mode As Integer
Dim TempArray() As String
Dim TempEvent As EventType
Dim LNum() As Long
Dim Level() As LevelType
Dim DeleteFlag As Boolean
Dim EndFlag As Integer
T = Timer
On Error GoTo ErrorTrap
E = ""
ReDim BlockList(0)
ReDim EventList(0)
ReDim VarList(0)
ReDim Strings(0)
ReDim Level(0)
ReDim Values(0)
Erase Stack
'If (Not FuncDefs) = -1 Then
Call InitScript
'Parse into lines and remove the strings
iLine = Split(Script, vbNewLine)
For L = 0 To UBound(iLine)
iLine(L) = Trim$(iLine(L))
If InStr(1, iLine(L), vbNullChar) > 0 Then GoTo Nullchar
If InStr(1, iLine(L), Chr$(1)) > 0 Then GoTo Nullchar
RemoveStrings iLine(L), Strings, E
If iLine(L) = "EndIf" Then iLine(L) = "End If"
If E <> "" Then
ReDim LNum(L)
LNum(L) = L + 1
GoTo NotMyFault
End If
Next L
'Delete comments
Mode = 0
For L = 0 To UBound(iLine)
X = InStr(iLine(L), "'")
If X > 0 Then iLine(L) = left$(iLine(L), X - 1)
iLine(L) = Trim$(iLine(L))
Next L
'Apply linenumbers
Temp = String(4, vbNullChar)
For L = 0 To UBound(iLine)
iLine(L) = Format(L + 1, "0000000000") & vbNullChar & iLine(L)
Next L
'Compensate for linebreak character (;)
Script = Join(iLine, ";")
Erase iLine
Script = Replace(Script, "{", "{;")
Script = Replace(Script, "}", ";};")
Script = Replace(Script, "|", ";|;")
Script = ";" & Script
iLine = Split(Script, ";")
For L = 1 To UBound(iLine)
If Asc(Mid$(iLine(L), 11, 1)) = 0 Then
Temp = Mid$(iLine(L), 1, 11)
Else
iLine(L) = Temp & iLine(L)
End If
Next L
'Delete whitespace
X = 0
For L = 1 To UBound(iLine)
Do
If L + X > UBound(iLine) Then Exit For
If Len(iLine(L + X)) = 11 Then X = X + 1 Else Exit Do
Loop
If X > 0 Then iLine(L) = iLine(L + X)
Next L
ReDim Preserve iLine(UBound(iLine) - X)
'Read Linenums into memory
ReDim LNum(1 To UBound(iLine))
For L = 1 To UBound(iLine)
LNum(L) = Val(left$(ChopString(iLine(L), 11), 10))
iLine(L) = Trim$(iLine(L))
Autofix iLine(L)
Next L
'Add Variables, Functions, Commands, and Events to the existing database
For L = 1 To UBound(iLine)
'VARIABLES
If left$(iLine(L), 4) = "Dim " Then
'If Left$(iLine(L), 1) = "@" Or Left$(iLine(L), 1) = "#" Or Left$(iLine(L), 1) = "$" Or Left$(iLine(L), 1) = "%" Then
'ChopString iLine(L), 4
Call AddVariable(Right$(iLine(L), Len(iLine(L)) - 4), E)
If E <> "" Then GoTo NotMyFault
'FUNCTIONS
ElseIf left$(iLine(L), 9) = "Function " Then
Build = iLine(L)
Temp = ChopString(Build, 9)
Temp = ChopString(Build, 1)
ReDim Preserve FuncDefs(UBound(FuncDefs) + 1)
ReDim Preserve FuncList(UBound(FuncList) + 1)
Y = UBound(FuncDefs)
FuncDefs(Y).Type = Symbol2Index(Temp)
If FuncDefs(Y).Type = vbEmpty Then GoTo InvalidFuncDec
For X = L To UBound(iLine)
If iLine(X) = "End Function" Then Exit For
Next X
If X = UBound(iLine) + 1 Then GoTo MissingEndFunc
Z = X + 1
iLine(X) = iLine(X) & vbNullChar & UBound(FuncList)
X = InStr(1, Build, "(")
If X > 0 Then
If Right(Build, 1) <> ")" Then GoTo MissingPar
Temp = Mid$(Build, X + 1, Len(Build) - X - 1)
Build = left(Build, X - 1)
TempArray = Split(Temp, ",")
ReDim FuncDefs(Y).Args(UBound(TempArray) + 1)
For X = 0 To UBound(TempArray)
TempArray(X) = Trim(TempArray(X))
FuncDefs(Y).Args(X + 1).Type = Symbol2Index(ChopString(TempArray(X), 1))
If FuncDefs(Y).Args(X + 1).Type = vbEmpty Then GoTo InvalidFuncDec
If Not ValidVariable(TempArray(X)) Then GoTo InvalidVarName
FuncDefs(Y).Args(X + 1).Name = TempArray(X)
Next X
Else
Erase FuncDefs(Y).Args
End If
If Not ValidVariable(Build) Then GoTo InvalidFuncName
FuncDefs(Y).Name = Build
FuncDefs(Y).Index = UBound(FuncList)
iLine(L) = "Function " & CStr(Y)
L = Z
'SUBROUTINES
ElseIf left$(iLine(L), 8) = "Command " Then
Build = iLine(L)
Temp = ChopString(Build, 8)
ReDim Preserve SubDefs(UBound(SubDefs) + 1)
ReDim Preserve SubList(UBound(SubList) + 1)
Y = UBound(SubDefs)
For X = L To UBound(iLine)
If iLine(X) = "End Command" Then Exit For
Next X
Z = X + 1
If X = UBound(iLine) + 1 Then GoTo MissingEndSub
iLine(X) = iLine(X) & " " & vbNullChar & UBound(SubList)
X = InStr(1, Build, "(")
If X > 0 Then
If Right(Build, 1) <> ")" Then GoTo MissingPar
Temp = Mid$(Build, X + 1, Len(Build) - X - 1)
Build = left(Build, X - 1)
TempArray = Split(Temp, ",")
ReDim SubDefs(Y).Args(1 To UBound(TempArray) + 1)
For X = 0 To UBound(TempArray)
TempArray(X) = Trim(TempArray(X))
SubDefs(Y).Args(X + 1).Type = Symbol2Index(ChopString(TempArray(X), 1))
If SubDefs(Y).Args(X + 1).Type = vbEmpty Then GoTo InvalidFuncDec
If Not ValidVariable(TempArray(X)) Then GoTo InvalidVarName
SubDefs(Y).Args(X + 1).Name = TempArray(X)
Next X
Else
Erase SubDefs(Y).Args
End If
If Not ValidVariable(Build) Then GoTo InvalidFuncName
SubDefs(Y).Name = Build
SubDefs(Y).Index = UBound(SubList)
iLine(L) = "Command " & CStr(Y)
L = Z
'EVENT
ElseIf left$(iLine(L), 6) = "Event " Then
Build = iLine(L)
Temp = ChopString(Build, 6)
Temp = ChopString(Build, 1)
For X = 1 To EVENTNUM
If EventName(X) = Build Then Exit For
Next X
If X = EVENTNUM + 1 Then GoTo BadEvent
X = X * 2
If Temp = "-" Then
X = X - 1
ElseIf Temp <> "+" Then
GoTo BadEvent
End If
For Y = L To UBound(iLine)
If iLine(Y) = "End Event" Then Exit For
Next Y
If Y = UBound(iLine) + 1 Then GoTo MissingEndEvent
iLine(Y) = iLine(Y) & " " & vbNullChar & X
L = Y + 1
iLine(L) = "Event " & CStr(X)
Else
GoTo OutsideEvent
End If
Next L
VarNum = UBound(VarList)
FuncNum = UBound(FuncDefs)
'And finally, do the big parse.
Mode = 0
DeleteFlag = False
EndFlag = 0
Erase TempEvent.L
ReDim TempEvent.L(0)
For L = 1 To UBound(iLine)
If Mode = 0 Then
Select Case ChopString(iLine(L), InStr(1, iLine(L), " "))
Case "Dim ": Mode = 0
Case "Event ": Mode = 4
Case "Command ": Mode = 5
Case "Function ": Mode = 6
Case Else
GoTo OutsideEvent
End Select
If Mode > 0 Then
Erase TempEvent.L
ReDim TempEvent.L(0)
Select Case Mode
Case 4
Case 5
With SubDefs(Val(iLine(L)))
If (Not .Args) <> -1 Then
For X = 1 To UBound(.Args)
Call MakeLocal(.Args(X).Type, .Args(X).Name, 0, -1)
Next X
End If
End With
Case 6
With FuncDefs(Val(iLine(L)))
If (Not .Args) <> -1 Then
For X = 1 To UBound(.Args)
Call MakeLocal(.Args(X).Type, .Args(X).Name, 0, -1)
Next X
End If
Call MakeLocal(.Type, .Name, 0, -1)
End With
End Select
End If
ElseIf left$(iLine(L), 6) = "Local " Then
If Mode < 4 Then GoTo InvalidLocal
ChopString iLine(L), 6
ReDim Preserve TempEvent.L(UBound(TempEvent.L) + 1)
With TempEvent.L(UBound(TempEvent.L))
.LineNum = LNum(L)
Call AddVariable(iLine(L), E)
If E <> "" Then GoTo NotMyFault
.CommandNum = 5
End With
With VarList(UBound(VarList))
X = UBound(Strings) + 1
ReDim Preserve Strings(X)
Strings(X) = FuncDefs(.FuncLink).Name
Temp = CStr(FuncDefs(.FuncLink).Type)
Temp = Temp & ", [" & CStr(X) & "]"
Temp = Temp & ", " & CStr(.LB) & ", " & CStr(.UB)
TempEvent.L(UBound(TempEvent.L)).MainBlock = ParseBlock(Temp, Strings, E, 4)
If E <> "" Then GoTo NotMyFault
End With
Else
If Mode > 3 Then Mode = Mode - 3
ReDim Preserve TempEvent.L(UBound(TempEvent.L) + 1)
With TempEvent.L(UBound(TempEvent.L))
.LineNum = LNum(L)
'First, parse every set of () in the line.
Do
X = InStrRev(iLine(L), "(")
If X = 0 Then Exit Do
Y = InStr(X, iLine(L), ")")
If Y = 0 Then GoTo MissingPar
Temp = Mid(iLine(L), X + 1, Y - X - 1)
Y = ParseBlock(Temp, Strings, E)
If E <> "" Then GoTo NotMyFault
iLine(L) = left$(iLine(L), X - 1) & Replace(iLine(L), "(" & Temp & ")", ";" & CStr(Y) & ";", X, 1)
Loop
'Figure out the command
X = InStr(1, iLine(L), " ")
If X = 0 Then
Temp = iLine(L)
iLine(L) = ""
Else
Temp = Trim$(ChopString(iLine(L), X))
End If
iLine(L) = Trim$(iLine(L))
Y = InStr(1, iLine(L), ";")
For X = REALSUBSTART To UBound(SubDefs)
If SubDefs(X).Name = Temp Then Y = -1: Exit For
Next X
If Y = -1 Then
.CommandNum = X
.MainBlock = ParseBlock(iLine(L), Strings, E, UBound(SubDefs(X).Args))
If E <> "" Then GoTo NotMyFault
ElseIf left$(iLine(L), 1) = "=" Then 'Set
.CommandNum = 1
ChopString iLine(L), 1
iLine(L) = Trim$(iLine(L))
If Y > 0 Then
Build = ChopString(Temp, Y - 1)
Else
Build = Temp
Temp = ""
End If
X = GetVarIndex(Build)
If X = 0 Then GoTo UndefinedVar
If Temp = "" Then Temp = "-1"
Temp = CStr(X) & ", " & Temp & ", " & iLine(L)
If E <> "" Then GoTo NotMyFault
.MainBlock = ParseBlock(Temp, Strings, E, 3)
If E <> "" Then GoTo NotMyFault
Else
If Temp = "End" And iLine(L) <> "If" Then
Select Case left$(iLine(L), 9)
Case "Event " & vbNullChar, "Command " & vbNullChar, "Function" & vbNullChar
Temp = Temp & Trim$(ChopString(iLine(L), 8))
Case Else: GoTo SyntaxError
End Select
End If
Select Case Temp
Case "If", "While", "For", vbNullChar
X = UBound(Level) + 1
ReDim Preserve Level(X)
Level(X).StartNum = UBound(TempEvent.L)
Select Case Temp
Case "If": .CommandNum = 3: Level(X).Type = 1
Case "While": .CommandNum = 3: Level(X).Type = 2
Case "For": .CommandNum = 4: Level(X).Type = 3
Case vbNullChar: .CommandNum = 2: Level(X).Type = 4
End Select
If Right$(iLine(L), 1) = "{" Then
Level(X).Brace = True
iLine(L) = Trim$(left$(iLine(L), Len(iLine(L)) - 1))
End If
Level(X).StartLine = iLine(L)
Case "}", "End", "Next", "Wend", "Else"
Temp = Trim$(Temp & " " & iLine(L))
Select Case Temp
Case "}": A = 0
Case "End If": A = 1
Case "Else": A = 2
Case "Wend": A = 3
Case Else
If left$(Temp, 5) = "Next " Then A = 4 Else GoTo SyntaxError
End Select
Y = UBound(Level)
If Y = 0 Then
Select Case A
Case 0: E = "}:{"
Case 1: E = "End If:If"
Case 2: E = "Else:If"
Case 3: E = "Wend:While"
Case 4: E = "Next:For"
End Select
GoTo WithoutError
Else
If Level(Y).Brace Then
If Temp <> "}" Then
E = "{:}"
GoTo WithoutError
ElseIf L <> UBound(iLine) Then
If Level(Y).Type = 1 And iLine(L + 1) = "|" Then iLine(L + 1) = vbNullChar
End If
Else
Select Case Level(Y).Type
Case 1
If A <> 1 And A <> 2 Then
E = "End If or Else"
ElseIf A = 2 Then
iLine(L) = vbNullChar
End If
Case 2: If A <> 3 Then E = "Wend"
Case 3: If A <> 4 Then E = "Next"
Case 4: If A <> 1 Then E = "End If"
End Select
If E <> "" Then GoTo Expected
End If
A = UBound(TempEvent.L)
X = Level(Y).StartNum
Build = Level(Y).StartLine
Z = L
L = TempEvent.L(X).LineNum
B = 0
Select Case Level(Y).Type
Case 1, 4 'If-Else-EndIf
If Z <> UBound(iLine) Then
If Temp = "}" And iLine(Z + 1) = vbNullChar Then A = A + 1
End If
If Temp = "Else" Then A = A + 1
If Build <> "" Then Build = Build & ", "
Build = Build & CStr(A)
TempEvent.L(X).MainBlock = ParseBlock(Build, Strings, E, 2)
If E <> "" Then GoTo NotMyFault
DeleteFlag = True
If Temp = "Else" Then Z = Z - 1
Case 2 'While-Wend
.CommandNum = 2
.MainBlock = ParseBlock(CStr(Level(Y).StartNum), Strings, E)
Build = Build & ", " & CStr(A + 1)
TempEvent.L(X).MainBlock = ParseBlock(Build, Strings, E, 2)
If E <> "" Then GoTo NotMyFault
Case 3 'For-Next
.CommandNum = 2
.MainBlock = ParseBlock(CStr(Level(Y).StartNum), Strings, E)
ReDim TempArray(4)
X = InStr(1, Build, "=")
If X = 0 Then GoTo SyntaxError
Piece = Trim$(ChopString(Build, X - 1))
If Piece = "" Then GoTo SyntaxError
X = GetVarIndex(Piece)
If X = 0 Then GoTo UndefinedVar
If VarList(X).UB <> -1 Then GoTo ForNextError
TempArray(0) = CStr(X)
ChopString Build, 1
X = InStr(1, Build, "To")
If X = 0 Then GoTo SyntaxError
TempArray(1) = Trim$(ChopString(Build, X - 1))
ChopString Build, 2
X = InStr(1, Build, "Step")
If X = 0 Then
TempArray(2) = Trim$(Build)
TempArray(3) = "1"
Else
TempArray(2) = Trim$(ChopString(Build, X - 1))
ChopString Build, 4
TempArray(3) = Trim$(Build)
End If
TempArray(4) = CStr(A + 1)
For X = 0 To 4
If TempArray(X) = "" Then GoTo SyntaxError
Next X
Build = Join(TempArray, ", ")
TempEvent.L(Level(Y).StartNum).MainBlock = ParseBlock(Build, Strings, E, 5)
If E <> "" Then GoTo NotMyFault
End Select
ReDim Preserve Level(UBound(Level) - 1)
L = Z
End If
Case "EndEvent", "EndCommand", "EndFunction"
ChopString iLine(L), 1
EndFlag = Val(iLine(L))
DeleteFlag = True
Case Else
GoTo SyntaxError
End Select
End If
End With
If DeleteFlag Then
ReDim Preserve TempEvent.L(UBound(TempEvent.L) - 1)
DeleteFlag = False
End If
If EndFlag > 0 Then
If Mode > 3 Then Mode = Mode - 3
Select Case Mode
Case 1: EventList(EndFlag) = TempEvent
Case 2: SubList(EndFlag) = TempEvent
Case 3: FuncList(EndFlag) = TempEvent
End Select
Mode = 0
EndFlag = 0
ReDim Preserve VarList(VarNum) 'Destroy locals
ReDim Preserve FuncDefs(FuncNum) 'Destroy locals
End If
End If
Next L
Erase Strings
CompileScript = "Script Compiled in " & CStr(Timer - T) & " seconds."
Exit Function
'--------------------------------------'
'*********** ERROR MESSAGES ***********'
'--------------------------------------'
ErrorTrap:
CompileScript = CErr(LNum(L)) & "RTE " & CStr(Err.Number) & " - " & Err.Description
Exit Function
Resume
NotMyFault:
CompileScript = CErr(LNum(L)) & E
Exit Function
MissingQuote:
CompileScript = CErr(L) & "Missing quote"
Exit Function
Nullchar:
CompileScript = CErr(L) & "Null character detected"
Exit Function
Expected:
CompileScript = CErr(LNum(L)) & "Expected: " & E
Exit Function
ForNextError:
CompileScript = CErr(LNum(L)) & "Cannot use Arrays in For-Next loops"
Exit Function
WithoutError:
TempArray = Split(E, ":")
CompileScript = CErr(LNum(L)) & TempArray(0) & " without " & TempArray(1)
Exit Function
SyntaxError:
CompileScript = CErr(LNum(L)) & "Syntax error"
Exit Function
MissingPar:
CompileScript = CErr(LNum(L)) & "Missing parenthesis"
Exit Function
UndefinedVar:
CompileScript = CErr(LNum(L)) & "Variable undefined"
Exit Function
InvalidLocal:
CompileScript = CErr(LNum(L)) & "Local Variables must come before all other statements"
Exit Function
InvalidSubscript:
CompileScript = CErr(LNum(L)) & "Invalid variable subscript"
Exit Function
InvalidVarName:
CompileScript = CErr(LNum(L)) & "Invalid variable name"
Exit Function
InvalidSubName:
CompileScript = CErr(LNum(L)) & "Invalid command name"
Exit Function
InvalidFuncName:
CompileScript = CErr(LNum(L)) & "Invalid function name"
Exit Function
InvalidSubDec:
CompileScript = CErr(LNum(L)) & "Invalid command declaration"
Exit Function
InvalidFuncDec:
CompileScript = CErr(LNum(L)) & "Invalid function declaration"
Exit Function
MissingEndSub:
CompileScript = CErr(LNum(L)) & "Command without End Command"
Exit Function
MissingEndFunc:
CompileScript = CErr(LNum(L)) & "Function without End Function"
Exit Function
MissingEndEvent:
CompileScript = CErr(LNum(L)) & "Event without End Event"
Exit Function
OutsideEvent:
CompileScript = CErr(LNum(L)) & "Statement outside Command, Function, or Event"
Exit Function
BadEvent:
CompileScript = CErr(LNum(L)) & "Unrecognized event"
Exit Function
End Function
Private Function ParseBlock(ByVal BlockText As String, Strings() As String, ByRef ErrorBuffer As String, Optional ByVal MaxArgs = -1, Optional ByVal CheckNested As Boolean = False) As Long
Dim D As Double
Dim A As Long
Dim B As Long
Dim X As Long
Dim Y As Long
Dim Z As Long
Dim R As Long
Dim TArray() As String
Dim DataArray() As String
Dim Build As String
Dim Temp As String
If CheckNested Then
Do
X = InStrRev(BlockText, "(")
If X = 0 Then Exit Do
Y = InStr(X, BlockText, ")")
If Y = 0 Then GoTo MissingPar
Temp = Mid$(BlockText, X + 1, Y - X - 1)
Y = ParseBlock(Temp, Strings, ErrorBuffer)
If ErrorBuffer <> "" Then Exit Function
BlockText = left$(BlockText, X - 1) & Replace(BlockText, "(" & Temp & ")", ";" & CStr(Y) & ";", X, 1)
Loop
End If
R = UBound(BlockList) + 1
ReDim Preserve BlockList(R)
TArray = Split(BlockText, ",")
X = UBound(TArray)
If X + 1 > MaxArgs And MaxArgs > -1 Then GoTo TooManyArgs
If X = -1 Then
Erase BlockList(R).ArgList
Else
ReDim BlockList(R).ArgList(1 To X + 1)
For X = 1 To X + 1
Build = Trim$(TArray(X - 1))
If Build <> "" Then
Build = Replace(Build, "^", vbNullChar & "01")
Build = Replace(Build, "*", vbNullChar & "03")
Build = Replace(Build, "/", vbNullChar & "03" & Chr$(1))
Build = Replace(Build, "\", vbNullChar & "04")
Build = Replace(Build, " Mod ", vbNullChar & "05")
Build = Replace(Build, "+", vbNullChar & "06")
Build = Replace(Build, "&", vbNullChar & "07")
Build = Replace(Build, "<>", vbNullChar & "09")
Build = Replace(Build, "<=", vbNullChar & "12")
Build = Replace(Build, ">=", vbNullChar & "13")
Build = Replace(Build, "<", vbNullChar & "10")
Build = Replace(Build, ">", vbNullChar & "11")
Build = Replace(Build, "=", vbNullChar & "08")
'Build = Replace(Build, " Not ", vbNullChar & "14")
Build = Replace(Build, " And ", vbNullChar & "15")
Build = Replace(Build, " Xor ", vbNullChar & "17")
Build = Replace(Build, " Eqv ", vbNullChar & "18")
Build = Replace(Build, " Or ", vbNullChar & "16")
'The "-" character is a bit trickier, as it can be either
'a minus sign or a negation sign.
Y = 0
Do
Y = InStr(Y + 1, Build, "-")
If Y = 0 Then Exit Do
If Y > 1 Then Temp = Right$(Trim$(left$(Build, Y - 1)), 3)
If left$(Temp, 1) <> vbNullChar And Y <> 1 Then
Build = left$(Build, Y - 1) & vbNullChar & "06" & Chr$(1) & Right$(Build, Len(Build) - Y)
End If
Loop
DataArray = Split(vbNullChar & "00" & Build, vbNullChar)
'With BlockList(R).ArgList(X)
'Can't use this With here because there's
'no way around raising RTE10 with it. Ugh.
ReDim BlockList(R).ArgList(X).V(1 To UBound(DataArray))
For Y = 1 To UBound(DataArray)
Build = DataArray(Y)
BlockList(R).ArgList(X).V(Y).Op = Val(ChopString(Build, 2))
Build = Trim$(Build)
If left$(Build, 1) = Chr$(1) Then
BlockList(R).ArgList(X).V(Y).UseAlt = True
ChopString Build, 1
Build = Trim$(Build)
End If
If left$(Build, 4) = "Not " Then
BlockList(R).ArgList(X).V(Y).UseNot = True
ChopString Build, 4
Build = Trim$(Build)
End If
If left$(Build, 1) = "-" Then
BlockList(R).ArgList(X).V(Y).UseNeg = True
ChopString Build, 1
Build = Trim$(Build)
End If
If InStr(1, Build, " ") > 0 Then GoTo SyntaxError
'And now, it's time to play...
'GUESS! THAT! DATA TYPE!!
B = UBound(Values) + 1
'Could it be... a String?!
If left$(Build, 1) = "[" And Right$(Build, 1) = "]" Then
ReDim Preserve Values(B)
Values(B) = Strings(Val(Mid$(Build, 2, Len(Build) - 2)))
BlockList(R).ArgList(X).V(Y).Real = True
BlockList(R).ArgList(X).V(Y).Link = B
'Oh, I know... it's a link to another Block!
ElseIf left$(Build, 1) = ";" And Right$(Build, 1) = ";" Then
BlockList(R).ArgList(X).V(Y).Real = False
BlockList(R).ArgList(X).V(Y).Link = Val(Mid$(Build, 2, Len(Build) - 2))
'Maybe it's a Boolean!
ElseIf Build = "True" Or Build = "False" Then
ReDim Preserve Values(B)
Values(B) = CBool(Build)
BlockList(R).ArgList(X).V(Y).Real = True
BlockList(R).ArgList(X).V(Y).Link = B
'What about a Double or a Long?
ElseIf IsValidNumber(Build) Then
ReDim Preserve Values(B)
D = CDbl(Build)
If CStr(CLng(D)) = CStr(D) Then
Values(B) = CLng(Build)
Else
Values(B) = CDbl(Build)
End If
BlockList(R).ArgList(X).V(Y).Real = True
BlockList(R).ArgList(X).V(Y).Link = B
'Well the only other thing it could be is
'a Variable or a Function...
Else
BlockList(R).ArgList(X).V(Y).Real = False
If Right$(Build, 1) = ";" Then
Z = InStrRev(Build, ";", Len(Build) - 1)
Temp = ChopString(Build, Z - 1)
Z = Val(Mid$(Build, 2, Len(Build) - 2))
Else
Temp = Build
Build = ""
Z = ParseBlock("", Strings, ErrorBuffer)
End If
For A = 1 To UBound(FuncDefs)
If LCase(FuncDefs(A).Name) = LCase(Temp) Then Exit For
Next A
If A <= UBound(FuncDefs) Then
BlockList(Z).FunctionNum = A
BlockList(R).ArgList(X).V(Y).Link = Z
'Nothing?! Grr... *steals prize money anyway*
Else
GoTo Unrecognized
End If
End If
Next Y
'End With
End If
Next X