-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplys.aup
950 lines (839 loc) · 38.3 KB
/
plys.aup
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
#NoTrayIcon
#include <ProcessConstants.au3>
#include <TrayConstants.au3>
#include <WinAPIHObj.au3>
#include <WinAPIProc.au3>
#include <WinAPISys.au3>
Opt("MustDeclareVars", 1)
; NOTE: rel_* - relative to InputFileDir, relrel_* - relative to rel_path
const Version = "0.5.0"
const PlysPathRegEx = PrepareForRegEx _
(PathPart(@AutoItExe, "drive", "dir") . "Plys\plys.aup.au3")
const TrayIconPathRegEx = _
PrepareForRegEx(PathPart(@AutoItExe, "drive", "dir") . "Plys\tray.ico")
enum IncludeDep = 2, MirIncludeDep = 1.5, ImportDep = 1
enum LambdaFunctionsKey, NoDollarPrefixKey, ConstByDefaultKey, _
CloseBlockByIndentKey, ImportKeywordKey, SynonymsKey
; Translation code settings
const MainTokenDelim = Chr(0x0C) ; form feed
const MiscTokenDelim = Chr(0x0B) ; vertical tab
const LineNumCommentPrefix = @tab . @tab . "; #"
const LambdaNameLen = 36
const TabInSpaces = " "
const PublicSuffixRegEx = "\*"
const NewSuffixLen = 2
const NameDelimRegEx = ":"
; Translation timing settings
const FlashStopTrayIconDelay = 300 ; ms
const ErrorWinWaitDelay = 500 ; ms
const ProcessExistsChance = 100 ; hand-picked value
const CatchErrorWinChance = 15 ; hand-picked value
; Default Plys-file settings
const LambdaFunctionsDefault = True
const NoDollarPrefixDefault = True
const ConstByDefaultDefault = True ; dim instead of local/global, func args
const CloseBlockByIndentDefault = True
const ImportKeywordDefault = True
const SynonymsDefault = True
; Default translator settings
const StdioExchangeDefault = True
const RunModeDefault = True
const ErrorStdOutDefault = False
const RapidModeDefault = False
; Translator options
dim StdioExchange
dim RunMode
dim ErrorStdOut
dim RapidMode
dim InputFilePath
dim InputFileDir
dim InputFileNameExt
; Table of dependencies
; Array size: DepTable[n+1][n+1]
; Array structure:
; DepTable[0][0] = n number of files
; DepTable[1..n][0] relative path to file “n”
; DepTable[0][1..n] names array of file “n”
; DepTable[i][j] = include file “i” includes file “j”
dim DepTable[0][0]
; Table of visibilities: which module sees names from which module
; Only for replacement of not own names
; Based on DepTable
dim ThruTable
dim PlysOpts[0] ; Plys-file settings for each file
dim PathsToDeleteOnExit[0]
if @ScriptName == "plys.aup" then Main()
func Main*()
AdlibRegister(FuncName(ShowTrayIcon), FlashStopTrayIconDelay)
const scriptArgs = CmdLineArgs()
const rel_path = PathPart(InputFilePath, "name", "ext")
redim DepTable[1][1]
DepTable[0][0] = 0
redim PlysOpts[1]
PlysOpts[0] = 0
GetDeps(rel_path)
if not RunMode then Echo("[Dependencies are get]" . @)
ThruTable = DepTable
MakeThruTable() ;~if ImportKeyword then MakeThruTable()
ProcessFiles()
redim DepTable[0][0]
redim PlysOpts[0]
ThruTable = 0 ;~if ImportKeyword then ThruTable = 0
AdlibUnRegister(FuncName(ShowTrayIcon))
if not RunMode then return
const error = RunScript(scriptArgs)
if not error and not RapidMode then
for path in PathsToDeleteOnExit
FileDelete(path)
func CmdLineArgs()
; @CmdLine = [0:n, 1:path, 2:args] or [0:n, 1:/ErrorStdOut, 2:path, 3:args]
const pathIndex = _ParseCmdLine()
if pathIndex = 0 then
InputFilePath = OpenDialog("Run Script:", @MyDocumentsDir, _
"Plys script files (*.aup)|All files (*.*)")
; TODO: AutoIt script files (*.au3;*.a3x)
if @error then exit
else
InputFilePath = @CmdLine[pathIndex]
InputFileDir = PathPart(InputFilePath, "drive", "dir")
InputFileNameExt = PathPart(InputFilePath, "name", "ext")
const argc = @CmdLine[0] - pathIndex
if argc = 0 then return ""
dim argv[argc]
for i = pathIndex + 1 to @CmdLine[0]
argv[i - 2] = '"' . @CmdLine[i] . '"'
return _ArrayToString(argv, " ")
func ShowTrayIcon()
Opt("TrayIconHide", 0)
TraySetIcon("stop")
TraySetState(Tray_IconState_Flash)
AdlibUnRegister(FuncName(ShowTrayIcon))
func _ParseCmdLine()
StdioExchange = StdioExchangeDefault
RunMode = RunModeDefault
ErrorStdOut = ErrorStdOutDefault
RapidMode = RapidModeDefault
if @CmdLine[0] = 0 then return 0
for i = 1 to @CmdLine[0]
if Left(@CmdLine[i], 1) <> "/" then exitloop
switch @CmdLine[i]
case "/Translate"
RunMode = False
case "/NoStdio"
StdioExchange = False
case "/ErrorStdOut"
ErrorStdOut = True
case "/Rapid"
RapidMode = True
return i
func GetDeps(rel_path)
; fill DepTable with dependencies recursively
const depTypes = [IncludeDep, ImportDep]
dim relrel_deps, rel_dep, dep
dim relrel_includes, relrel_imports
; Attention! _GetDepsFirstStageData increments the DepTable[0][0]
_GetDepsFirstStageData(rel_path, relrel_includes, relrel_imports)
const rel_dirSlash = PathPart(rel_path, "dir") ; current_file\directory\
const module = DepTable[0][0]
for depType in depTypes
relrel_deps = (depType=IncludeDep)? relrel_includes : relrel_imports
if not IsArray(relrel_deps) then continueloop
for relrel_dep in relrel_deps
rel_dep = rel_dirSlash . relrel_dep
dep = Search(DepTable, rel_dep, 1, default, _
default, default, default, 0) ; from 1 element in col 0
if dep = -1 then ; new path
GetDeps(rel_dep)
dep = Search(DepTable, rel_dep, 1, default, _
default, default, default, 0) ; from 1st elem in col 0
DepTable[module][dep] = depType
;··············································································
func _GetDepsFirstStageData _
(rel_path, dim byref relrel_includes, dim byref relrel_imports)
; Append and init new module DepTable cells: path, names, Plys-options.
; Return arrays with include and import paths.
;··············································································
dim mainText = Read(InputFileDir . "\" . rel_path)
const miscText = MiscTokenDelim _
. _ArrayToString(_SeparateMain(mainText), MiscTokenDelim)
const module = DepTable[0][0] + 1
redim PlysOpts[module + 1]
PlysOpts[0] = module
dim opts[SynonymsKey + 1]
opts[LambdaFunctionsKey] = LambdaFunctionsDefault _
and not ReFind(miscText, MiscTokenDelim . "\s*\#plys\s+nolambda")
opts[NoDollarPrefixKey] = NoDollarPrefixDefault _
and not ReFind(miscText, MiscTokenDelim . "\s*\#plys\s+dollarprefix")
opts[ConstByDefaultKey] = ConstByDefaultDefault _
and not ReFind(miscText, MiscTokenDelim . "\s*\#plys\s+noconst")
opts[CloseBlockByIndentKey] = CloseBlockByIndentDefault _
and not ReFind(miscText, MiscTokenDelim . "\s*\#plys\s+noindent")
opts[ImportKeywordKey] = ImportKeywordDefault _
and not ReFind(miscText, MiscTokenDelim . "\s*\#plys\s+noimport")
opts[SynonymsKey] = SynonymsDefault _
and not ReFind(miscText, MiscTokenDelim . "\s*\#plys\s+nosynonyms")
PlysOpts[module] = opts
redim DepTable[module + 1][module + 1]
DepTable[0][0] = module
DepTable[module][0] = rel_path
DepTable[0][module] = _FindNames(mainText, opts[ConstByDefaultKey])
relrel_includes = ReFind(miscText, _
MiscTokenDelim . '\s*\#include\s+"(.+?)"', _
@ReArrayGlobal)
if opts[ImportKeywordKey] and _IsPlysFile(rel_path) then
relrel_imports = ReFind(miscText, _
MiscTokenDelim . '\s*\#import\s+"(.+?)"', _
@ReArrayGlobal)
func _FindNames(byref text, constByDefault)
dim names = ReFind(text, _
"(?im)^\s*func\s+([A-Za-z_]\w*" . PublicSuffixRegEx . "?)", _
@ReArrayGlobal)
if not IsArray(names) then
dim names[0]
dim varsDeclars
if constByDefault then
varsDeclars = ReFind(text, _
"(?im)^(?:\s*global\s+)?(?:dim|const|enum)\s+(.*)", _
@ReArrayGlobal)
else
varsDeclars = ReFind(text, "(?im)^\s*global\s+(.*)", @ReArrayGlobal)
if IsArray(varsDeclars) then
; FIXME: "const $bar = foo(1, 2)" → ["$bar = foo(1", " 2)"]
dim newNames
for varsDeclar in varsDeclars
for varDeclar in Split(varsDeclar, ",", @NoCount)
;~newNames = ReFind _
;~ (varDeclar, "(\$\w*)", @ReArrayGlobal)
;~if IsArray(newNames) then Add(names, newNames[0])
newNames = ReFind(varDeclar, _
"([\$A-Za-z_]\w*" . PublicSuffixRegEx . "?)", _
@ReArrayGlobal)
if IsArray(newNames) then
Add(names, ReReplace(newNames[0], "^\w", "\$$0", 1))
return Unique(names, 0, 0, 0, ArrayUnique_NoCount)
;··············································································
func MakeThruTable()
;··············································································
; capture dependencies throught
dim changed
do
changed = False
for row = 1 to ThruTable[0][0]
for col = 1 to ThruTable[0][0]
switch ThruTable[row][col]
case IncludeDep ; ———————————————
; “mirror” rule
if ThruTable[col][row] <= ImportDep then
ThruTable[col][row] = MirIncludeDep
changed = True
; “chain” rule for column
for y = 1 to ThruTable[0][0]
if y <> col _
and ThruTable[y][row] > ThruTable[y][col] _
then
ThruTable[y][col] = ThruTable[y][row]
changed = True
case ImportDep ; ————————————————
; “chain” rule for column
for y = 1 to ThruTable[0][0]
if y <> col _
and ThruTable[y][row] > ImportDep _
and ThruTable[y][col] = "" _
then
ThruTable[y][col] = ImportDep
changed = True
endswitch
next col
next row
until not changed
;··············································································
func ProcessFiles()
;··············································································
dim sourcePath, targetPath, isPlysFile
dim mainText, miscTokens ; storages for processing file data
dim opts
for module = 1 to DepTable[0][0]
sourcePath = InputFileDir . "\" . DepTable[module][0]
if not FileExists(sourcePath) then continueloop
targetPath = PathPart(sourcePath, "drive", "ext") . ".au3"
if RapidMode then
if _OldSource(sourcePath, targetPath) then continueloop
elseif RunMode then
Add(PathsToDeleteOnExit, targetPath)
isPlysFile = _IsPlysFile(sourcePath)
mainText = Read(sourcePath)
_EnumerateLines(mainText, isPlysFile)
opts = PlysOpts[module]
if module = 1 then _AppendHeader(mainText, opts[SynonymsKey])
miscTokens = _SeparateMain(mainText)
if isPlysFile then
if opts[CloseBlockByIndentKey] then _CloseBlocks(mainText)
if opts[LambdaFunctionsKey] then
Add(DepTable[0][module], _ProcessLambdas(mainText, miscTokens))
if opts[NoDollarPrefixKey] then
_AddVarPrefixes(mainText, DepTable[0][module])
_RemoveAsterisks(mainText, opts[ConstByDefaultKey])
if opts[ConstByDefaultKey] then _ToggleDimConst(mainText)
if opts[ImportKeywordKey] then
_ProcessImports(miscTokens, module, isPlysFile)
if DepTable[0][0] > 1 then _ReplaceOwnNames(mainText, module)
_ReplaceNotOwnNames(mainText, module)
_SubstituteMacros(mainText)
if isPlysFile and opts[SynonymsKey] then
_RenameSynonyms(mainText)
_CombineTokens(mainText, miscTokens)
FileDelete(targetPath)
Write(targetPath, mainText)
if RunMode then
SetAttrib(targetPath, "+H")
else
Echo('[Writed] "' . _ANSI(targetPath) . '"' . @)
next module
func _OldSource(sourcePath, targetPath)
return FileExists(targetPath) _
and GetTime(sourcePath, 0, 1) < GetTime(targetPath, 0, 1)
func _EnumerateLines(dim byref text, isPlysFile)
text = Split(text, @, @EntireSplit + @NoCount)
dim misc
for i = 0 to UBound(text) - 1
if ReFind(text[i], "^\s*$") then continueloop
if RunMode then
text[i] .= LineNumCommentPrefix . (i + 1)
if isPlysFile then
misc = _SeparateMain(text[i])
text[i] = ReReplace(text[i], "(?i)@ScriptLineNumber\b", i + 1)
_CombineTokens(text[i], misc)
text = _ArrayToString(text, @)
func _AppendHeader(dim byref text, synonyms)
dim header = ""
if synonyms then
header .= "#include <StringConstants.au3>" . @
header .= "#include <Array.au3>" . @
;~header .= "#include <Function.au3>" . @
if RunMode then
header .= 'TraySetToolTip("AutoIt Plys - " . @ScriptName)' . @
header .= 'TraySetIcon("' . TrayIconPathRegEx . '")' . @
text = ReReplace(text, "(?im)^(?!\#|$)", header, 1)
func _SeparateMain(dim byref txt)
const miscPattern = "(?m)" _
. "^\s*\#cs(?s:.*?)\#ce.*" _
. "|^\s*\#comments-start(?s:.*?)\#comments-end.*" _
. "|^\s*\#.*" _
. '|"(?:[^"]|"")*"' _
. "|'(?:[^']|'')*'" _
. "|\s_\h*(?:;.*)?\R" _ ; \R needs for continuation in func
. "|;.*" _
. "|\{|}"
dim miscTokens = ReFind(txt, miscPattern, @ReArrayGlobal)
if @error then
dim miscTokens[0]
txt = ReReplace(txt, miscPattern, MainTokenDelim)
return miscTokens
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
func _CloseBlocks(dim byref text)
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
text = ReReplace(text, "(?m:^|\G)\t", TabInSpaces)
text = Split(text . @ . "end.", @, @EntireSplit + @NoCount)
dim i = -1
dim comments = False
dim indentSize, prevIndentSize = 0
dim statement, sttmnt
dim closer, indent
dim stack[0]
while i + 1 <= UBound(text) - 1
i += 1
; skip comments and directives (and empty lines)
if ReFind(text[i], "^\s*($|;)") then continueloop
if comments then
if ReFind(text[i], "(?i)^\s*(\#ce|\#comments-end)") then
comments = False
continueloop
if ReFind(text[i], "(?i)^\s*(\#cs|\#comments-start)") then
comments = True
continueloop
;~ if ReFind(text[i], "^\s*\#") then continueloop
indentSize = Len(ReReplace(text[i], "^( *).*", "$1"))
statement = ReReplace(text[i], "^ *(\w*).*", "$1")
; with “then” expression
if ReFind(text[i], "^ *(else)?if\b.*?\bthen\b.*?\S") then
statement = ""
if indentSize < prevIndentSize then
;~ func __CloseOpenedBlocks _
;~ (statement, indentSize, prevIndentSize, stack)
sttmnt = statement
for is = indentSize to prevIndentSize - 1
if is > UBound(stack) - 1 then exitloop
switch stack[is]
case "if"
if ReFind(sttmnt, "(?i)^else(if)?$") then
sttmnt = ""
continueloop
closer = "endif"
case "elseif"
if ReFind(sttmnt, "(?i)^else(if)?$") then continueloop
closer = "endif"
case "else"
closer = "endif"
case "select", "switch", "with", "func"
closer = "end" . stack[is]
case "for"
closer = "next"
case "while"
closer = "wend"
case else
continueloop
indent = ""
for k = 1 to is
indent .= " "
if ReFind(text[i], "^" . indent . closer . "\b") then
; TODO: should be replaced even if closeBlock is disabled
text[i] = ReReplace(text[i], closer, closer . ";", 1)
else
text[i] = indent . closer . @ . text[i]
next is
redim stack[indentSize]
endif
switch statement
case "if", "elseif", _
"else", "select", "switch", "for", "while", "with", "func"
redim stack[indentSize + 1]
stack[indentSize] = statement
prevIndentSize = indentSize
wend
text = _ArrayToString(text, @)
text = ReReplace(text, "\Rend\.\s*(;.*)?$", "")
func _ProcessLambdas(dim byref mainText, dim byref miscTokens)
dim names[UBound(miscTokens)]
dim name, n = 0, lambdaLines = ""
dim mainDeleteRanges = "", miscDeleteRanges = ""
mainText = Split(mainText, MainTokenDelim, @NoCount)
; example: a{b""c}d
; i:a b j:c d – extr i+1..j:bc, del i+1..j+1:bcd, res a
; i:{ "" j:} – extr i+1..j-1:"", del i..j:{""}, res empty
for i = 0 to UBound(miscTokens) - 1
if miscTokens[i] <> "{" then continueloop
for j = i + 1 to UBound(miscTokens) - 1
if miscTokens[j] <> "}" then continueloop
name = __AppendNewName(names, n)
mainDeleteRanges .= (i + 1) . "-" . (j + 1) . ";"
lambdaLines .= __MakeOrdinaryFunc(Extract(mainText, i + 1, j), _
name, __GetLineNum(miscTokens, j))
mainText[i] .= name . mainText[j + 1]
miscDeleteRanges .= i . "-" . j . ";"
Concat(miscTokens, Extract(miscTokens, i + 1, j - 1))
i = j + 1
exitloop
mainDeleteRanges = ReReplace(mainDeleteRanges, ";$", "")
miscDeleteRanges = ReReplace(miscDeleteRanges, ";$", "")
_ArrayDelete(mainText, mainDeleteRanges)
_ArrayDelete(miscTokens, miscDeleteRanges)
mainText = _ArrayToString(mainText, MainTokenDelim) . lambdaLines
redim names[n]
return names
func __AppendNewName(dim byref names, dim byref n)
dim name
do
name = ""
for j = 1 to LambdaNameLen
name .= Chr(Random(Asc("a"), Asc("z"), 1))
until Search(names, name) = -1
names[n] = name
n += 1
return name
func __GetLineNum(byref miscTokens, startIndex)
dim lineNum
for k = startIndex to UBound(miscTokens) - 1
lineNum = ReFind(miscTokens[k], "^; \#(\d+)$", @ReArray)
if IsArray(lineNum) then
return LineNumCommentPrefix . lineNum[0]
return ""
func __MakeOrdinaryFunc(byref array, name, lineNum)
const text = _ArrayToString(array, MainTokenDelim)
const args = ReReplace(text, ":?[^:]*$", "")
const expr = ReFind(text, "[^:]*$", @ReArray)[0]
return @ . @ _
. "func " . name . "(" . args . ")" . lineNum . @ _
. TabInSpaces . "return " . expr . lineNum . @ _
. "endfunc" . @
func _AddVarPrefixes(dim byref text, dim names)
if IsArray(names) then
names = _ArrayToString(names)
names = ReReplace(names, "\|\$\w+\*?", "") ; funcs only
names = ReReplace(names, "\$\w+\*?\|", "") ; funcs only
if names <> "" then names = "|" . names
else
names = ""
; FIXME: call function, stored in variable: $foo($bar, $buz)
text = ReReplace(text, "(?i)" _
. "(?(?=" _ ; if look ahead one of this
. "\b(_|and|byref|case|const|continuecase|continueloop|default|" _
. "dim|do|else|elseif|endfunc|endif|endselect|endswitch|endwith|" _
. "enum|exit|exitloop|false|for|func|global|if|in|local|next|" _
. "not|null|or|redim|return|select|static|step|switch|then|to|" _
. "true|until|volatile|wend|while|with" . names . ")\b" _
. ")" _ ; then replace
. " " _
. "|" _ ; else it’s variable: replace
. "(?<![\w@$])" _ ; ← previous isn’t [\w@$]
. "[A-Za-z_]\w*" _ ; ↓ next isn’t call, scope (or continuation)
. "(?!\w*(\s*\(|:[A-Za-z_]))" _ ;|\s*_\W))" _
. ")", _
"\$$0") ; by this
func _RemoveAsterisks(dim byref text, constByDefault)
text = ReReplace(text, "(?im)^(\s*func\s+)([A-Za-z_]\w*)" _
. PublicSuffixRegEx, "$1$2")
const varsDeclars = ReFind(text, "(?im)^" _
. (constByDefault? _
"(?:\s*global\s+)?(?:dim|const|enum)" _
: "\s*global") _
. "\s+(.*)", @ReArrayGlobal)
if not IsArray(varsDeclars) then return
; FIXME: "const $bar = foo(1, 2)" → ["$bar = foo(1", " 2)"]
dim newVarsDeclar, publicNames
for varsDeclar in varsDeclars
newVarsDeclar = varsDeclar
for varDeclar in Split(varsDeclar, ",", @NoCount)
publicNames = ReFind(varDeclar, _
"(\$\w+" . PublicSuffixRegEx . ")", _
@ReArrayGlobal)
if not IsArray(publicNames) then continueloop
newVarsDeclar = ReReplace(newVarsDeclar, _
"(\" . TrimRight(publicNames[0], 1) . ")" _
. PublicSuffixRegEx, "\1", 1)
if newVarsDeclar <> varsDeclar then
text = Replace(text, varsDeclar, newVarsDeclar)
func _ToggleDimConst(dim byref text)
text = ReReplace(text, _
"(?im)^(const|enum|static)\s+\$", "global $0")
text = ReReplace(text, _
"(?im)^(\s+)((?:const|enum|static)\s+\$)", "$1local $2")
text = ReReplace(text, _
"(?im)^dim(\s+\$)", "global$1")
text = ReReplace(text, _
"(?im)^(\s+)dim(\s+\$)", "$1local$2")
dim funcDeclars = ReFind(text, "(?im)^\s*func\s+.*$", @ReArrayGlobal)
if IsArray(funcDeclars) then
dim newFuncDeclar, args, pos
for funcDeclar in funcDeclars
newFuncDeclar = funcDeclar
args = ReReplace _
(funcDeclar, "(?i)\s*func\s+.*?\(\s*(.*)\s*\).*", "$1")
if args = "" then continueloop
args = Split(args, ",", @NoCount)
for arg in args ; insert “const”
if not ReFind(arg, "\b(const|dim)\b") then
ReFind(arg, "[a-zA-Z_\$]", @ReArray)
pos = @extended - 2
newFuncDeclar = Replace(newFuncDeclar, arg, _
Left(arg, pos) . "const " . Mid(arg, pos + 1))
newFuncDeclar = ReReplace _
(newFuncDeclar, "\bdim\s+", "") ; delete “dim”
text = Replace(text, funcDeclar, newFuncDeclar)
func _ProcessImports(dim byref miscTokens, module, isPlysFile)
miscTokens = _ArrayToString(miscTokens, MiscTokenDelim)
if isPlysFile then __PreventReimporting(miscTokens, module)
; replace “#include/#import "path\name.ext"”
; with “#include "path\%target_prefix%name.ext.au3"”
miscTokens = ReReplace(miscTokens, _
"((?:^|" . MiscTokenDelim . ')\s*)\#(?:include' _
. (isPlysFile? "|import" : "") _
. ')(\s+)"(.*?)"', _
'$1#include$2"$3.au3"')
miscTokens = Split(miscTokens, MiscTokenDelim, @NoCount)
func __PreventReimporting(dim byref text, module)
; comment #import if module including or importing alredy been
const rel_dirSlash = PathPart(DepTable[module][0], "dir")
dim relrel_dep, foundInPrev
for dep = 1 to DepTable[0][0]
if DepTable[module][dep] <> ImportDep then continueloop
relrel_dep = DepTable[dep][0]
if rel_dirSlash <> "" then
relrel_dep = Replace(relrel_dep, rel_dirSlash, "")
relrel_dep = PrepareForRegEx(relrel_dep)
text = ReReplace(text, _
"(" . MiscTokenDelim . "\s*)" _
. '(\#(?:include|import)\s+[<"]' _
. relrel_dep . '[>"])', _
"$1; PLYS $2")
foundInPrev = False
for prev = 1 to module - 1
if DepTable[prev][dep] then
foundInPrev = True
exitloop
if not foundInPrev then ; uncomment first
text = ReReplace(text, _
"(" . MiscTokenDelim . "\s*)" _
. '; PLYS (\#(?:include|import)\s+[<"]' _
. relrel_dep . '[>"])', _
"$1$2", 1)
func _ReplaceOwnNames(dim byref text, module)
const names = DepTable[0][module]
if not IsArray(names) then return
; purified filename
dim prefix = PathPart(DepTable[module][0], "dir", "name")
prefix = ReReplace(prefix, "\W", "_")
;~const suffix = GetTime(DepTable[module][0], 0, 1)
; TODO: put names into pattern (name|name|…)
; and replace it in one fell swoop
for name in names
if Left(name, 1) = "$" then ; is variable
text = ReReplace(text, _
"(?im)([^\w""'" . NameDelimRegEx . "]|^)\$(" _
. TrimLeft(name, 1) . ")([^\w""'])", _
"$1\$_" . prefix . "__$2$3") ;~__" . suffix . "$3")
else ; is function
prefix = ReReplace(prefix, "^[0-9]", "_$1")
text = ReReplace(text, _
"(?im)([^\w""'\$" . NameDelimRegEx . "]|^)(" . name _
. ")([^\w""'])", _
"${1}_" . prefix . "__$2$3") ;~__" . suffix . "$3")
;··············································································
func _ReplaceNotOwnNames(dim byref text, module)
; replace names from ThruTable[0][dep] in module file
;··············································································
const depTypes = [ImportDep, IncludeDep, MirIncludeDep]
dim names
dim filename, prefix, varPrefix, isPublic
for depType in depTypes
for dep = 1 to DepTable[0][0]
if ThruTable[module][dep] <> depType then continueloop
names = DepTable[0][dep]
if not IsArray(names) then continueloop
; purified filename
filename = PathPart(DepTable[dep][0], "name")
filename = ReReplace(filename, "\W", "_")
prefix = PathPart(DepTable[dep][0], "dir")
prefix = ReReplace(prefix, "\W", "_")
prefix .= filename
;~suffix = GetTime(DepTable[dep][0], 0, 1)
for name in names
if Left(name, 1) = "$" then ; is variable
name = TrimLeft(name, 1)
varPrefix = "\$"
else
prefix = ReReplace(prefix, "^[0-9]", "_$1")
varPrefix = ""
if ReFind(name, PublicSuffixRegEx . "$") then
name = TrimRight(name, 1)
isPublic = True
else
isPublic = False
if ThruTable[module][dep] <> ImportDep then
text = ReReplace(text, _
"(?im)([^\w""']|^)" . varPrefix . name _
. "([^\w""'])", _
"${1}" . varPrefix . "_" . prefix _
. "__" . name . "$2") ;~. "__" . suffix . "$2")
elseif isPublic then ; ImportDep and isPublic
text = ReReplace(text, _
"(?im)([^\w""']|^)" . filename _
. NameDelimRegEx . varPrefix . name _
. "([^\w""'])", "${1}" . varPrefix . "_" . prefix _
. "__" . name . "$2") ;~ . "__" . suffix . "$2")
func _SubstituteMacros(dim byref text)
text = ReReplace(text, "(?i)@PlysPath\b", '"' . PlysPathRegEx . '"')
text = ReReplace(text, "(?i)@PlysVersion\b", '"' . Version . '"')
text = ReReplace(text, "(?i)@ScriptFullPath\b", _
'"' . PrepareForRegEx(InputFilePath) . '"')
text = ReReplace(text, "(?i)@ScriptName\b", _
'"' . PrepareForRegEx(InputFileNameExt) . '"')
if RunMode then
text = ReReplace(text, "(?i)\b(TraySetToolTip\()\)", _
'$1"AutoIt Plys - " . @ScriptName)')
text = ReReplace(text, "(?i)\b(TraySetIcon\()\)", _
'$1"' . TrayIconPathRegEx . '")')
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
func _RenameSynonyms(dim byref text)
; TODO: remove Get prefixes, move patterns to INI
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
text = ReReplace(text, "(?i)\bEcho\b", "ConsoleWrite")
; DllStruct ————————————————————————
text = ReReplace(text, "(?i)\bStruct\b", "DllStructCreate")
text = ReReplace(text, "(?i)\bStructGet\b", "DllStructGetData")
text = ReReplace(text, "(?i)\bStructGetSize\b", "DllStructGetSize")
text = ReReplace(text, "(?i)\bStructGetPtr\b", "DllStructGetPtr")
text = ReReplace(text, "(?i)\bStructSet\b", "DllStructSetData")
text = ReReplace(text, "(?i)\bIsStruct\b", "IsDllStruct")
; File —————————————————————————————
text = ReReplace(text, "(?i)\b(" _
. "ChangeDir|Copy|CreateShortcut|Flush|GetAttrib" _
. "|GetEncoding|GetLongName|GetShortcut|GetShortName|GetSize" _
. "|GetTime|GetVersion|Open|OpenDialog|Read|ReadLine" _
. "|ReadToArray|Recycle|RecycleEmpty|SaveDialog|SelectFolder" _
. "|SetAttrib|SetEnd|SetPos|SetTime|Write|WriteLine" _
. ")\b", "File$1")
text = ReReplace(text, "(?i)\bCreateLink\b", "FileCreateNTFSLink")
text = ReReplace(text, "(?i)\bFirstFile\b", "FileFindFirstFile")
text = ReReplace(text, "(?i)\bNextFile\b", "FileFindNextFile")
; **Close|**Delete|**Exists|**GetPos|Install|**Move
; String ———————————————————————————
text = ReReplace(text, "(?i)\b(" _
. "AddCR|Format|InStr|IsAlNum|IsAlpha|IsASCII|IsDigit" _
. "|IsLower|IsSpace|IsUpper|IsXDigit|Left|Len|Lower|Mid" _
. "|Replace|Right|Split|StripCR|StripWS|TrimLeft|TrimRight" _
. "|Upper" _
. ")\b", "String$1")
text = ReReplace(text, "(?i)\bReFind\b", "StringRegExp")
text = ReReplace(text, "(?i)\bReReplace\b", "StringRegExpReplace")
; Compare|FromASCIIArray|*IsFloat|*IsInt|**Reverse|ToASCIIArray
; Win ——————————————————————————————
text = ReReplace(text, "(?i)\b(" _
. "Activate|Active|Flash|GetCaretPos|GetClassList" _
. "|GetClientSize|GetProcess|GetTitle|Kill|List" _
. "|MenuSelectItem|MinimizeAll|MinimizeAllUndo|SetOnTop" _
. "|SetTitle|SetTrans|Wait|WaitActive|WaitClose" _
. "|WaitNotActive" _
. ")\b", "Win$1")
; **Close|**Exists|*GetHandle|**GetPos|*GetState|*GetText|**Move
; |*SetState
; Macros ———————————————————————————
text = ReReplace(text, "(?i)(@)(" _
. "NoCaseSense|CaseSense|NoCaseSenseBasic|StripLeading" _
. "|StripTrailing|StripSpaces|StripAll|ChrSplit|EntireSplit" _
. "|NoCount|EndNotStart|UTF16|UCS2" _
. ")\b", "$Str_$2")
text = ReReplace(text, "(?i)(@Re)(" _
. "Array|ArrayFull|ArrayGlobal|ArrayGlobalFull" _
. ")\b", "$Str_RegExp$2Match")
text = ReReplace(text, "(?i)@ReMatch\b", "$STR_REGEXPMATCH")
text = ReReplace(text, "(?i)@ActiveWin\b", 'WinGetHandle("[ACTIVE]")')
text = ReReplace(text, "(?i)@CmdLine\b", "$CmdLine")
text = ReReplace(text, "@(\W)", "@CRLF$1")
; _Array ———————————————————————————
text = ReReplace(text, "(?i)\b(" _
. "Add|ColDelete|ColInsert|Combinations|Display|Extract" _
. "|FindAll|Insert|Max|MaxIndex|Min|MinIndex|Permute|Pop" _
. "|Push|Search|Shuffle|Sort|Swap|ToClip|Transpose|Trim" _
. "|Unique" _
. ")\b", "_Array$1")
; TODO: ToHistogram → Histogram
text = ReReplace(text, "(?i)\bToHistogram\b", "_Array1DToHistogram")
;~ text = ReReplace(text, "(?i)\b(Filter|Map|Reduce)\b", "_Array$1")
text = ReReplace(text, "(?i)\bBinSearch\b", "_ArrayBinarySearch")
text = ReReplace(text, "(?i)\bConcat\b", "_ArrayConcatenate")
; **Delete|**Reverse|*ToString
; ——————————————————————————————————
text = ReReplace(text, "\.(\W)", "&$1")
text = ReReplace(text, "\.=", "&=")
func _CombineTokens(dim byref mainText, byref miscTokens)
mainText = Split(mainText, MainTokenDelim, @NoCount)
dim text = ""
for i = 0 to UBound(mainText) - 2
text .= mainText[i] . miscTokens[i]
mainText = text . mainText[i]
func RunScript(scriptArgs)
; NOTE: the Plys will not exit until the running process terminates.
; Although the Plys will be executed to the end.
; To exit the Plys, will need to close the error message window.
const pid = Run('"' . @AutoItExe _
. '" "' . InputFilePath . '.au3" ' . scriptArgs, "", default, _
StdioExchange? StdIn_Child + StdErr_Child + StdOut_Child : 0)
if not StdioExchange then return 0
Opt("TrayIconHide", 1)
dim errorWin = _ProcessStdio(pid)
StdioClose(pid)
if not errorWin then return False
dim timer
while True
_ProcessError(errorWin)
timer = TimerInit()
while True
errorWin = _CatchErrorWin(pid)
if errorWin then exitloop
if TimerDiff(timer) > ErrorWinWaitDelay then return True
func _ProcessStdio(pid)
dim n = 1
dim lastLoop = False
dim errorWin
do
if Mod(n, ProcessExistsChance) = 0 then
if not ProcessExists(pid) then ; ~20% CPU
lastLoop = True
elseif Mod(n, CatchErrorWinChance) = 0 then
errorWin = _CatchErrorWin(pid) ; ~10% CPU
if errorWin then
lastLoop = True
StdinWrite(pid, ConsoleRead())
ConsoleWriteError(StderrRead(pid))
ConsoleWrite(StdoutRead(pid))
GUIGetMsg() ; prevents hogging of the CPU
n = Mod(n + 1, 1000)
until lastLoop
return errorWin
func _CatchErrorWin(pid)
;~const errorWin = WinGetHandle("[REGEXPTITLE:^AutoIt Error$]")
;~if @error then return 0
;~if GetProcess(errorWin) = pid then return errorWin
; ↑ does not affect performance
const errorWins = List("[REGEXPTITLE:^AutoIt Error$]")
for i = 1 to errorWins[0][0]
if GetProcess(errorWins[i][1]) = pid then
return errorWins[i][1]
return 0
func _ProcessError(errorWin)
dim errorData = _
ControlGetText(errorWin, "", "[CLASSNN:Static2]")
errorData = ReFind(errorData, '^Line (\d+) ' _ ; 0: line number
. '\(File "(.*?)\.au3"\):\R\R' _ ; 1: source filename
. '(.+\R)' _ ; 2: statemXnt
. '(.*?)' _ ; 3: statem
. '(\^ ERROR\R)?' _ ; 4: ^ ERROR
. '\RError: (.+)', _ ; 5: Message
@ReArray)
dim filename = errorData[1]
const au3LineNum = errorData[0]
const line = ReadLine(filename . ".au3", au3LineNum)
dim lineNum = _
ReFind(line, ".*?" . LineNumCommentPrefix . "(\d+)$", @ReArray)
dim message = errorData[5]
const code = errorData[2] . errorData[3] . errorData[4]
; TODO: translate code to Plys
if ErrorStdOut then
; close error window
ControlClick(errorWin, "", "[CLASSNN:Button1]")
if IsArray(lineNum) then
lineNum = lineNum[0]
else
filename .= ".au3"
lineNum = au3LineNum
message .= " ==> Plys Translator."
errorData = '"' . filename . '" (' . lineNum . ') : ==> ' . message _
. ':' . @ . code
ConsoleWriteError(_ANSI(errorData))
WaitClose(errorWin)
elseif IsArray(lineNum) then
SetTitle(errorWin, "", "Plys Error")
errorData = _
'Line ' . lineNum[0] . ' (File "' . filename . '"):' . @ . @ _
. code . @ _
. 'Error: ' . message
ControlSetText(errorWin, "", "[CLASSNN:Static2]", errorData)
WaitClose(errorWin)
func PrepareForRegEx(str)
return ReReplace(str, "[\\\.\^\$\[\(\{\+\#]", "\\$0")
func PathPart(path, dim start="path", dim finish="")
; Select subpath from start to finish part
; if start="path" then returns original path
; if finish="" then returns start only
const components = ["path", "drive", "dir", "name", "ext"]
start = Search(components, start)
if @error then start = 0
finish = Search(components, finish)
if @error then finish = start
; from _PathSplit (File.au3)
dim split_ = ReFind(path, _
"^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?" _
. "((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", @ReArray)
if @error then ; This error should never happen.
redim split_[5]
split_[0] = path
split_[2] = ReReplace(split_[2], "\h*[\/\\]+\h*", _
(Left(split_[2], 1) == "/")? "\/" : "\\")
dim result = ""
for i = start to finish
result .= split_[i]
return result
func _IsPlysFile(path)
return PathPart(path, "ext") = ".aup"
func _ANSI(str)
return BinaryToString(StringToBinary(str, 4), 1)