AHK v1 to AHK v2 converter

Discuss the future of the AutoHotkey language
User avatar
jeeswg
Posts: 4971
Joined: 19 Dec 2016, 01:58
Location: UK

AHK v1 to AHK v2 converter

06 Sep 2017, 20:11

See here for first release.
AHK v1 to AHK v2 converter (initial work) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=36754&p=201908#p201908

==================================================

This will become a script that converts AHK v1.1 code to AHK v2 code.

For the time being, it will be a script that converts AHK v1.1 code to AHK v1.1 code that is more forwards compatible, i.e. more AHK v2-ready.

I will eventually make it able to convert from AHK v1.1 to AHK v2, however I need AHK v2 to be more finalised before it's worth finishing/publishing stage 2.

Note: Currently this handles:
- var = string -> var := "string"
- var = num -> var := num
It can also identify where continuation sections begin/end.
I will add in further 'var = ' to 'var := ' support, and also various conversions such as StringReplace to StrReplace.

I would suggest that you compare any before/after code with a utility such as WinMerge to confirm that any changes were the right ones, and I would recommend backing up your original code.

I may also add in some code, which can be turned on/off to:
- confirm each line matches certain standards (including: ensure commands have/don't have commas between the command name and the first parameter)
- convert some parameters to force expression mode, e.g. 'Cmd, %var%' to 'Cmd, % var'
- clean up NumPut e.g. ensure 4 parameters
- clean up DllCall lines (this could be quite challenging)
- indent code (I finally got some working code for this yesterday, that correctly converted an entire script)

Some things may be trickier, and so I may provide no or limited support for them, e.g. converting StringSplit to StrSplit, or handling 'return' lines with multiple parameters, or handling the removal of SetFormat. [I will return with a list of the more difficult conversions that would need to be done manually.]

I may also provide support to convert code so that it can use my 'AHK v2 functions for AHK v1' library.

I may also provide support for converting existing functions to variant functions, e.g. SubStr lines to 'JEE_SubStr' lines, providing an AHK v2-style 'SubStr' in AHK v1. Perhaps I will call that particular function one of: 'AHKv2_SubStr' / 'Ahk2_SubStr' / 'SubStr2' / 'JEE_SubStr2', I'm not sure, although I tend to prefer that any nonstandard function have a prefix of some sort to avoid confusion with standard functions.

However, some of these things may present difficulties, so there may be limited modes that convert some matching lines (safer conversions), but that instead notify you of potential other matching lines (riskier conversions) (perhaps by appending comments to those lines).

Btw I've already done all the serious work for this, it's more getting it ready for public viewing that is the issue. Also, of course I need AHK v2 to be more finalised, I've already had to do a lot of 'unconverting' and major rewrites to conversion scripts as the AHK v2 specs have changed, so I've decided to put AHK v1 -> AHK v2 conversion on hold, and focus on AHK v1 command style to AHK v1 expression style, and related script checking and script tidying.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by jeeswg on 21 Feb 2018, 18:49, edited 4 times in total.
Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: AHK v1 to AHK v2 converter (initial work)

07 Sep 2017, 02:07

Thanks for your effors jeeswg, I will test this. :thumbup:
@ guest3456, hello :wave: , is your converter up to date, I remember it was quite good, but there has been many changes since that thread was active.

Cheers.
guest3456
Posts: 2396
Joined: 09 Oct 2013, 10:31

Re: AHK v1 to AHK v2 converter (initial work)

07 Sep 2017, 08:45

Helgef wrote:@ guest3456, hello :wave: , is your converter up to date, I remember it was quite good, but there has been many changes since that thread was active.


not up to date unfortunately, but the framework should be solid to build upon and update, pull requests are welcome :)

User avatar
derz00
Posts: 495
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: AHK v1 to AHK v2 converter (initial work)

25 Sep 2017, 13:36

jeeswg wrote:

Code: [Select all] [Download] GeSHi © Codebox Plus


;JEE_WinMergeCompareStrings("BEFORE`r`n" vTextOrig, "AFTER`r`n" vOutput)



Mind sharing this function?

I ran the script on one of my scripts and it changed nothing. Any idea why? What exactly is it supposed to change?

guest3456 has GUI set up nice already. And his script saves the new code as A_ScriptName "_v2new.ahk"
try it and see
...
User avatar
jeeswg
Posts: 4971
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK v1 to AHK v2 converter (initial work)

25 Sep 2017, 15:06

The script is currently very limited, see the first post, it just handles some 'var = value' lines. Hence 'initial work' in the thread title.

If rapid progress is made with the development of AHK v2, this will become a priority, otherwise, I will post some updates at some point.

Recently I've been working on some scripts that tidy instances of DllCall, and that identify where functions and their parameters start and end. I've also been investigating continuation sections as part of an automated indentation function. Plus I wrote the basis of a WinMerge alternative.

While I've been having a lot of success with parsing, I've recently discovered the problem of handling all notation to do with objects.

indent code (automated code indentation) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=37270

It's quite easy to parse instances of 'one line one function' / 'one line one command', and these are the bulk of conversion, I'll share some code fairly soon, but functions within functions, back-to-back commands, and multi-line functions/commands, that may include indentation, comments and the use of continuation sections are harder.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4971
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK v1 to AHK v2 converter (initial work)

21 Feb 2018, 18:37

- I have a first release if anybody wants to play around with it.
- The script is written for AHK v1, a main script, and a library that comes with it. It also needs the 2 libraries from here, the main functions library and expansion pack:
commands as functions (AHK v2 functions for AHK v1) - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=29689&p=197355#p197355
[EDIT:] Changed the hotkeys from ^q and ^w to ^q (the same as before) and ^#w.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Helgef
Posts: 3163
Joined: 17 Jul 2016, 01:02
Contact:

Re: AHK v1 to AHK v2 converter

25 Feb 2018, 14:15

problem, cmd,% expr doesn't work, but cmd, % expr does, and also cmd % expr. Also cmd,expr doesn't convert, but cmd expr and cmd, expr does. Note the difference in spaces.

Anyways, initial testing is positive. Very well done, and thanks for sharing :clap:. I look forward to the continuation.

Cheers :salute:.
User avatar
jeeswg
Posts: 4971
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK v1 to AHK v2 converter

26 Feb 2018, 05:41

- Hello Helgef, thanks very much for testing and for the great feedback. I was hoping for some feedback just to know that someone had got it to work.

INSTALLATION
- Essentially it involves 4 scripts. I will put it up on my website as a zip at some point, for easier installation.

USAGE
- I mainly intended the script to convert the selected text. (Default hotkey: Ctrl+Q.)
- The script can also input/output files.
- There is a secondary hotkey that simply identifies lines that aren't converted. (Default hotkey: Ctrl+Win+W.)
- I had two ideas in mind re. checking conversion, (a) use a tool like WinMerge to check each before/after line individually, (b) maintain a log of unique converted lines, so you don't have to review the same conversion multiple times, and so that if at some future point, a conversion was discovered to be faulty, it will be possible to retrospectively identify and correct those lines. E.g.

Code: [Select all] [Download] GeSHi © Codebox Plus

[1]WinGet, hWnd, ID, A
[2]hWnd := WinGetID("A")
You could run an updated converter on each '[1]' line to see if you get the same '[2]' line.

BACKGROUND
- I had heard that some major work had been done re. conversion. However, I had done a lot of work on conversion previously, so I felt that I had a good grasp of most of the issues, such that, in the long run, it would be easier to write my own script, than it would be to modify someone else's.
- Writing the script yourself may sound harder, but the advantage is that you do not have to wait for someone else.
- In theory, other scripts could have been useful to learn from, but the only thing I wanted information on was how to handle continuation sections.
- I wasn't sure how important adding special handling for 'can be an expression' parameters would be, it turns out that it's very beneficial, by supporting them, it means you have to do virtually no retrospective parameter corrections. It does involve having massive lists of parameter types, and it's quite difficult to get a perfect list of which parameters in a command are/aren't 'can be an expression'. Generally speaking for 'can be an expression' parameters, no conversion takes place.

RESTRUCTURING
- Having now converted all of my main scripts and libraries, my feeling was 'I'm never going to look at it again', although a few days later, I've already forgotten all of the hard work that went into it, and I'm thinking of one or two basic but major simplifications for the script.
- I think I might separate the bulk of the script into a 'convert this line' function. That would make it easier to use the code in other scripts.
- Having a separate function would also make it easier to convert something like this:

Code: [Select all] [Download] GeSHi © Codebox Plus

;before
IfEqual, var, value, Cmd, arg1, arg2
;after
if (var = "value")
Cmd(arg1, arg2)
Because that effectively involves two separate line conversions.
- I may also move the code for handling the Control/Drive/Process/Win commands into the same location as the handling for all other commands that have special conversion requirements, it should make the script a lot easier to follow/maintain.
- Also, I've been thinking about some basic first steps re. conversion in reverse i.e. AHK v2 -> AHK v1.
- Now that I've converted all of my scripts to AHK v2, it's a problem to share any of my functions in AHK v1 form on the forum.
- I feel better now after having talked a lot about conversion in the 'conversion logic' thread, having some actual code to show.

TO IMPROVE
- Handle 'Cmd,Arg'. I made some efforts to handle whitespace in a variety of 'if' statements shortly before submitting the script, but I will explore further handling for whitespace in commands.
- I could add handling for 'var ='/'var :=' -> 'var := ""'. I didn't, perhaps because there was a risk of incorrectly converting lines that marked the start of continuation sections.
- There may be some issues with Loop at present, this is because I had 3 modes in mind:

Code: [Select all] [Download] GeSHi © Codebox Plus

Loop Parse, vText, `n, `r ;AHK v1 minimal force expression
Loop Parse, vText, % "`n", % "`r" ;AHK v1 everything force expression (almost like AHK v2)
Loop Parse, vText, "`n", "`r" ;AHK v2
Also, my script is optimised for handling function-style parameters, I haven't done too much work on command-style parameters yet, although it would be a key feature of AHK v2 -> AHK v1 conversion.
- Any good code to identify a valid hotkey label would be great if someone can help me with it.
- I recently found out that the SoundSet 'NewSetting' parameter is not a standard 'can be an expression' parameter, I guess that it must have special handling for '+10' etc. Any minor deviations from standard parameter types like this, are something I really need to know about.
- Do post any errors in the script to this thread.

REINVENTING THE WHEEL
- It's funny potentially reinventing everything. I would have used ideas from elsewhere if I'd known them, instead I ended up inventing things as and when I needed them.
- One key function was 'ObjPopBlank', to remove any empty keys from the end of an array.
- Another was 'CvtJoin' to stitch parameters together.

SUBSTR IS FIDDLY (AS ARE INSTR/REGEXMATCH)
- There is not really an easy solution re. what to do with SubStr, e.g. to signify that one SubStr line *is* AHK v2 ready, and that one line isn't. Especially when it may not be possible to completely convert a script automatically, so there is temporary phase where conversion is incomplete.
- For me the most practical solution was this: wherever StartingPos was ambiguous (not a positive number), I changed the function from 'SubStr' to 'JEE_SubStr', and added/subtracted 1 if necessary. JEE_SubStr is a two-way compatible function, with the AHK v2 behaviour in both AHK v1 and AHK v2. E.g. StringRight can be converted to JEE_SubStr.
guest3456
Posts: 2396
Joined: 09 Oct 2013, 10:31

Re: AHK v1 to AHK v2 converter

26 Feb 2018, 09:30

jeeswg wrote:

Code: [Select all] [Download] GeSHi © Codebox Plus

;before
IfEqual, var, value, Cmd, arg1, arg2
;after
if (var = "value")
Cmd(arg1, arg2)
Because that effectively involves two separate line conversions.


see here for some caveats i found with same line if statements:

https://github.com/mmikeww/AHK-v2-scrip ... /issues/25

that converter pretty much already does "one line at a time". not sure why you don't want to collaborate and instead build your own, but your choice

User avatar
jeeswg
Posts: 4971
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK v1 to AHK v2 converter

26 Feb 2018, 10:03

- Hello guest3456, ah, so they can be chained. I guess a function can check for IfXXX, and then check again etc, as you pointed out.
- You're welcome to use ideas/code from my script for your converter. You probably don't need anything, but you might find it interesting to look at handling for any awkward commands.
- If you have any areas where you would recommend a different approach, or if there are any elements you like, it would be interesting to know. Or any general comments about your experiences writing a converter. The classic point being that things start out as a Rube Goldberg machine, and then get tidied up. Also, when problems recur often enough, they become a function, but then you have to be careful retrofitting old code to use the function.
- I try to make my scripts quite readable, so hopefully things are reasonably easy to follow. It can be quite entertaining reading someone else's attempts at solving the problems.
- I mention about 'conversion obstacles' here, i.e. impediments to both one-way conversion, and two-way compatible conversion.
Wish List 2.0 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=13&t=36789

- Re. collaboration. Conversion is a fundamental thing in programming, and it benefits anyone to become good at it, so, even if I could get someone else to do it for me, it's a useful skill to have. I wanted to understand the process from start to finish.
- For me, I wanted to do some things in a specific way, I would have spent more time debating than coding. At other times I've played second fiddle.
- For me, the best form of collaboration is the forum itself, people can help fill in the random gaps in my knowledge, and vice versa.
User avatar
derz00
Posts: 495
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: AHK v1 to AHK v2 converter

27 Mar 2018, 08:08

Hi, I just tried this out a bit. It was complicated to get the script and the 3 lib files together from 2 forum threads (and not the first post in the threads) to the right places. Would be relatively easy for you and much easier for everyone if you simply provided a zip file.. So it is attached here to give you the idea...

converter.zip
(36.63 KiB) Downloaded 23 times


The converter seems to fail to convert commands in line with a hotkey like this:

Code: [Select all] [Download] GeSHi © Codebox Plus

^Numpad0::Run, taskmgr ; Run Task Manager


Maybe you don't have any hotkeys like that, so I thought I'd let you know...
try it and see
...
User avatar
jeeswg
Posts: 4971
Joined: 19 Dec 2016, 01:58
Location: UK

Re: AHK v1 to AHK v2 converter

27 Mar 2018, 12:09

- Thanks derz00. Someone messaged me suggesting to make the files more easily available, and I had intended to do this anyway, and had hoped to do it last weekend. (It might seem simple but I wanted to: use correct modified dates, do some checks, possibly do a readme, and figure out sensible naming conventions for the zip and its files, and make sure all files were UTF-8.)
- You are right re. hotkey one-liners. I had said above: 'Any good code to identify a valid hotkey label would be great if someone can help me with it.' I mentioned it here a while back:
parsing AutoHotkey scripts - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=40471
- I need to be sure that I correctly identify where hotkey labels begin/end in order to do this, perhaps other people that have worked on parsers or source code could point me to some info on this. Otherwise I could try to identify at least a wide range of common hotkeys as a start.
- [EDIT:] I have some AHK v1 code which covers most of these hotkey one-liners:

Code: [Select all] [Download] GeSHi © Codebox Plus

	JEE_CvtSplitLine(vTemp, vWhitespace, vCode, vComments)
if InStr(vCode, "::")
&& !(vCode ~= "::.*::")
&& RegExMatch(vCode, "O)^([^:]*)::(.*)$", o)
vWhitespace .= o.1 "::", vCode := o.2

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 5 guests