Converter AU3 to AHK / AHK to AU3

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SimBeSim
Posts: 2
Joined: 05 Mar 2017, 06:29
Location: Eindhoven
Contact:

Converter AU3 to AHK / AHK to AU3

16 Nov 2017, 09:31

Hallo developers,
Due to the feeling that I do miss a certain amount of functionality when programming mainly in AutoIt. I too examine with lots of interest all that Information that I come across with comparison of programming languages like on Wikipedia and especially
Rosetta AutoHotkey and Rosetta AutoIt
code site shines a lot of light into the digital darkness of binary machine terror.

So an idea to make one or another sort converter I got already long ago since the use of PHP. But from the moment I needed to automate a VoipCall application I entered into AutoIt/AutoHotKey world.
Though probably due to quite unusual for someone coming from PHP syntax to assign, use, declare variables with:

Code: Select all

s := "Hello, "
and the use of variable in a function by writing it like this:

Code: Select all

MsgBox % s
were causing too much confusion regarding understanding what's going on during execution.

But after drilling a hole in AutoItscript.com and used AutoIt for a year I kinda have a feeling that to get lost in au3 file is not gonna happen and functionality what for example in SciTE4AutoHotKey is applied I definitely like lot more then being severely limited by keyboard shortcuts and no additional Toolbar and/or Own Menu entries, context menu and so on in SciTE4AutoIt I am examining 2 directions: AHK and Lua .
Depending on availability of any help/feedback I would like to make a reasonable script converter between AHK and AU3 . In both directions preferably.

So if you have any tips or know about something comparable can you, please direct me onto the path to go and if it does not get much rejection I will later layout general key points what will have to be converted and maybe with some yours help build AHK|AU3 converter.

What do you think?
User avatar
Gio
Posts: 1247
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: Converter AU3 to AHK / AHK to AU3

16 Nov 2017, 10:51

Hello SimBeSim.

Welcome to the AutoHotkey community forums.

The issue you are presenting is unfortunately extremely hard to implement. Languages (Programming languages included) adopt varying ammounts of abstraction in each of their individual statements and thus translations are NOT a simple 1 to 1 substitution. That is why a translator usually reads (or listens) to a sentence in a language, than interprets the meaning and than writes a completely new sentence in that other language which aims to express the same meaning (many times using words that are not translatable 1 to 1).

For this reason, any high-level to high-level language translator would probably be even harder to implement than a regular language compiler, which is already hard enougth. The only feasible option of similar functionality that i can think of is when a specific programming language is actually developed with the specific intent to be translatable to other languages (Genexus is a comercial example that applies this). In these cases, language A is translatable to languages B and C, but B and C CANNOT be translated to A or to each other.

Either way, don't forget that two applications written in different languages CAN interact with each other and that AutoHotkey actually has a fork that aims to provide the AHK functionality to other languages via DLLs and other means (It's called AutoHotkey_H).

Feel free to ask anything regarding AutoHotkey_H here: https://autohotkey.com/boards/viewforum.php?f=65

Best wishes.
AnAttemptWasMade

Re: Converter AU3 to AHK / AHK to AU3

16 Nov 2017, 12:43

Nimda had an interesting idea, create a AutoHotkey library so you could run AU3 code in AutoHotkey :-)

Never finished though, discussion: https://autohotkey.com/board/topic/7933 ... key-l-v11/
Code: https://github.com/nimdahk/AU3toAHK
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Converter AU3 to AHK / AHK to AU3

16 Nov 2017, 13:33

If I was to do this, AHK functions are more similar to AutoIt functions, then AHK commands are, so I would recommend converting from AutoIt to AHK v2, or converting from AutoIt to AHK v1 but using this library (which has function versions of all the commands):
commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 37&t=29689

Also I would check for any AutoIt functions that don't have an equivalent in AutoHotkey, and try to recreate them.

I would then convert manually but look for various ways that I could automate some of the conversion, e.g. hotkeys that get the selected text, to some text manipulations and paste. I might even create some variations of AutoHotkey functions/commands, that use the same parameter order as AutoIt, to make things easier. E.g. pseudo-AutoIt code within AutoHotkey.

Also sometimes it's easier to rewrite than to convert, i.e., you've learnt more things about coding, or you know variables tricks of the destination language, in which case, having a lot of hotstrings could be helpful.

I can't give any better advice than that, because I don't really know AutoIt very well. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
SimBeSim
Posts: 2
Joined: 05 Mar 2017, 06:29
Location: Eindhoven
Contact:

Re: Converter AU3 to AHK / AHK to AU3

16 Nov 2017, 16:57

Gio wrote: The issue you are presenting is unfortunately extremely hard to implement. Languages (Programming languages included) adopt varying ammounts of abstraction in each of their individual statements and thus translations are NOT a simple 1 to 1 substitution. That is why a translator usually reads (or listens) to a sentence in a language, than interprets the meaning and than writes a completely new sentence in that other language which aims to express the same meaning (many times using words that are not translatable 1 to 1).

Best wishes.
Thanks Gio for reply, though I want to disagree with you regarding comparison of translation between human languages and conversion between computer languages. And especially in this subject in theory 100% equivalent conversion should be possible between Turing complete languages like C, C++, Python, PHP and so on. At the lowest level in CPU while being processed code has been anyway exploded into primitive ON and OFF electrical signals organized by arithmetic logic and control units, some memory and flow of currency at the in CPU set clock rate.

Anyway, this is also my first step on AHK and curiosity about roughness of this forum and severity of moderators watching out for any HOLY RULES disobedience.

So let me then illustrate my idea by following as far I can judge equivalent producing result AU3 and AHK scripts.

AutoHotKey version:

Code: Select all

MsgBox, 0, Hey Gio!, Do you see the difference?
AutoIt version:

Code: Select all

MsgBox(0,"Hey Gio!","Do you see the difference?")
and here is how it looks like:
AutoIt
AutoHotKey
And now comes conversion part from:
MsgBox, 0, Hey Gio!, Do you see the difference?
to
MsgBox(0,"Hey Gio!","Do you see the difference?")

Here especially when converting from AHK to AU3 I definitely can use some exact understanding what is allowed in this example
MsgBox function and what not and how parameters are treated by autohotkey.exe

For instance in AutoIt if you put it like:
MsgBox("Hey Gio!","Do you see the difference?")
you get an error - called with wrong number of args.
Also this results with same error:
MsgBox(0,"Hey Gio!")
Though doing it like this:
MsgBox(0,"","Do you see the difference?")
and like this also:
MsgBox(0,"","")
is perfectly possible.
If left or right brace is missing like:
MsgBox 0,"","")
MsgBox(0,"",""
produces syntax error.

Ok!
I of course know how to open somewhat buggy AHK manual and look up all the information on my own. Definitely possible and is the way I usually go. Still having noticed how active community of AutoIt is and seeing here also lots of movement I again want to give another try to join or start some sort of cooperation based on principles which I emotionally like much more AutoHotKey
Acknowledgements
with words more sounding like art "inspiration and time-saver" written by Chris

And then mind-fucking commercially smelling CopyRighted EULA of his great majesty holy J. Jon Jonathan Bennett, the Author of AutoIt Software and his Team AutoIt Kingdom
END-USER LICENSE AGREEMENT
Particularly sentence "You may not reverse engineer or disassemble the SOFTWARE PRODUCT" probably points into commercial direction.
But of course it's his piece of cake and too may Criss with any other former or current AHK developers see it for them self. I kinda looking around, maybe it turns out to stumble upon persons who just love to code and not cursed by compilation greedy lust for in 1 way or another some money.

Let's see what this is going to cause...
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Converter AU3 to AHK / AHK to AU3

24 Sep 2018, 06:06

Happen to see this. A direct AU3 to AHK converter that produces working code might be too lofty of a goal. However, I think it can be done as pseudo code, by using the help files, keywords, and index of both languages. You could possibly strip it down to keyword and just code examples. Possibly replacing various code examples with ones you prefer or from the forums, as oppose to just the help docs. This might also include making the code examples equivalent to each other, for more clarity.

Then create something like a AU3 to AHK reference script. Have the script scan for keywords, commands, and functions in the AutoIt script and spit out the equivalent AHK keywords, commands, functions, or pseudo code. You wouldn't necessarily get working converted script or code, but it would give you a strong idea and understanding of what an AU3 script was doing in AHK language. And would very likely be a strong starting point for your next step.

Autoit Code

While...WEnd

Code: Select all

Local $i = 0
While $i <= 10
    MsgBox($MB_SYSTEMMODAL, "", "Value of $i is: " & $i)
    $i = $i + 1
WEnd
AutoHotkey code

While-loop

Code: Select all

 a := 0
while (a <= 5)
{
a++
msgbox, not yet pass 5
if a > 5
break
}
However, with that typed, there was an AutoHotkey person that made a parser and other scripts for .AU3 to .AHK. Very interesting.

https://github.com/nimdahk/AU3toAHK

Code: Select all

#NoEnv
SetWorkingDir %A_ScriptDir%

If 0 > 0 ; incoming cmdline parameters
	fileName = %1%
else
	FileSelectFile, fileName

FileRead, file, %fileName%

File := "#Include AU3.ahk`r`n" . File

; Func .. EndFunc -> Func(){ .. }
file := RegExReplace(file, "mi`a)^(\h*)Func\s*(.*)$", "$1$2`r`n$1{")
StringReplace, file, file, EndFunc, }, All

; While .. Wend -> While{ .. }
file := RegExReplace(file, "mi`a)^(\h*)(While.*)$", "$1$2`r`n$1{")
StringReplace, file, file, Wend, }, All

; Switch .. Case .. EndSwitch -> Switch .. SwitchCase .. EndSwitch
file := preparseSelectSwitch(file)

;Switch .. EndSwitch -> some awful If else if crap
file := changeSwitch(file)

;Select .. EndSelect -> more awful If else if crap
;Nested Switch and Selects will cause undefined behavior when translating
file := changeSelect(file)

; Underscore continuation
file := RegExReplace(file, "mi`a)\h_(\s*;.*)[\r\n]*")

; Concatenation
StringReplace, file, file, %A_Space%&%A_Space%, %A_Space%.%A_Space%, All

; For .. = .. to .. [Step ..] .. Next -> for .. in range(.., .. [, ..]){ .. }
file := RegExReplace(file, "mi`a)^(\s*for\s+\$\w+\s*)=\s*([-\d\.]+)\s+to\s+([-\d\.]+\s+)(?:step\s+([-\d\.]+(?:\s*\/\s*[-\d\.]+)?))", "$1in range($2, $3, $4){")
file := RegExReplace(file, "mi`a)^(\s*for\s+\$\w+\s*)=\s*([-\d\.]+)\s+to\s+([-\d\.]+\s*?)(?!step)", "$1in range($2, $3){")
StringReplace, file, file, Next, }, All

; for a in b .. next -> for a in b { .. }
file := RegExReplace(file, "i)(?:^|[\r\n])\s*(for[^`r`n{]*[\r\n]*)", "$1{")
; the NEXT keyword has been handled above

; Do .. Until -> Loop .. Until
file := RegExReplace(file, "mi`a)^(\s*)Do", "$1Loop")

; ContinueLoop -> Continue
StringReplace, file, file, ContinueLoop, Continue, All

; ExitLoop -> Break
StringReplace, file, file, ExitLoop, Break, All

; Exit -> ExitApp
; use RegEx to reduce false positives
file := RegExReplace(file, "mi`a)^(\s*)Exit", "$1ExitApp")

; 'She said "Hi"' -> "She said ""Hi"""
file := changeQuotes(File)

MsgBox % file

FileDelete, %fileName%.ahk
FileAppend, %file%, %fileName%.ahk


changeQuotes(file){
    Loop Parse, file, `n, `r
    {
        if RegExMatch(A_LoopField, "^(\s*)#(?:comments-start|cs)(\s*)$", $)
        {
            out .= $1 . "/*" . $2 . "`r`n"
            comments_start := true
            continue
        }
        else if RegExMatch(A_LoopField, "^(\s*)#(?:comments-end|ce)(\s*)$", $){
            out .= $1 . "*/" . $2 . "`r`n"
            comments_start := false
            continue
        }
        if (comments_start){
            out .= A_LoopField "`r`n"
            continue
        }


        i := 0
        While (LoopField := SubStr(A_LoopField, ++i, 1)) != ""
        {
            ; ignore single-line comments
            if (LoopField = ";") 
                and (i = 1 or RegExMatch(SubStr(A_LoopField, i-1, 1), "\s")){
                    out .= SubStr(A_LoopField, i)
                    i := StrLen(A_LoopField)
                    out .= "`r`n"
                    continue 2
            }
            else if (LoopField = """") or (LoopField = "'") ; begin quote
            {
                out .= """"
                beginQuote := LoopField
                While (LoopField := SubStr(A_LoopField, ++i, 1)) != ""
                {
                    if (LoopField = beginQuote)
                    {
                        if SubStr(A_LoopField, i+1, 1) != beginQuote ; this ends the quote
                        {
                            out .= """"
                            continue 2
                        }
                        else
                        {
                            out .= beginQuote . (beginQuote = """" ? """" : "")
                            i++ ; There was a double-quote for escaping; eat the second one
                        }
                    }
                    else if (LoopField = """")
                    {
                        out .= """"""
                    }
                    else
                    {
                        out .= LoopField
                    }
                }
            }
             
            out .= LoopField
        }
        out .= "`r`n"
    }
    return out
}
changeSwitch(var){
	Switch := 0
	Loop Parse, var, `n, `r
	{
		If RegExMatch(A_LoopField, "mi`a)^(\s*)Switch(\s+.*)$", $)
			out .= $1 "Switch" . ++Switch . " := " $2
		else if RegExMatch(A_LoopField, "mi`a)^(\s*)SwitchCase(.*)$", $) and Switch > 0
		{
			case%Switch%++
			if ( Trim($2) = "else" )
			{
				out .=  $1 "else{`r`n"
				continue
			}
			out .= $1 (Case%Switch% > 1 ? "}else " : "" ) . "if "
			SwitchN := "Switch" Switch
			Loop Parse, $2, `,
			{
				if A_Index > 1
					out .= " or "
				if RegExMatch(A_LoopField, "i)(.*)\s*To\s*(.*)")
					out .= RegExReplace(A_LoopField, "i)(.*)\s*To\s*(.*)"
						, "($1 <= " SwitchN " and " SwitchN " <= $2)")
				else if RegExMatch(A_LoopField, "i)^(?!\sTo\s)(.*)$")
					out .= RegExReplace(A_LoopField, "i)^(?!\sTo\s)(.*)$"
						, "($1 = " SwitchN ")")
			}
			out .= "{"
		}
		else if RegExMatch(A_LoopField, "mi`a)^(\s*)EndSwitch(.*)", $)
		{
			out .= $1 "}" $2
			Switch--
		}
		else
			out .= A_LoopField
		out .= "`r`n"
	}
	StringTrimRight, out, out, 2
	return out
}
changeSelect(var){
	Select := 0
	Loop Parse, var, `n, `r
	{
		if Trim(A_LoopField) = "Select"
			Select++
		else if RegExMatch(A_LoopField, "i)(\s*)SelectCase\s*else(.*)", $) and Select > 0
		{
			Case%Select%++
			out .= $1 (Case%Select% > 1 ? "}" : "") . "else{" . $2
		}
		else if RegExMatch(A_LoopField, "i)(\s*)SelectCase\s*(.*)", $) and Select > 0
		{
			Case%Select%++
			out .= $1 . (Case%Select% > 1 ? "}else " : "") "if (" $2 "){"
		}
		else if RegExMatch(A_LoopField, "i)^(\s*)EndSelect(.*)$", $)
		{
			out .=$1 . "}" . $2
			Select--
		}
		else
			out .= A_LoopField
		out .= "`r`n"
	}
	StringTrimRight, out, out, 2
	return out
}
preparseSelectSwitch(var){
	stack := {push: func("ObjInsert"), pop: func("ObjRemove")}
	Loop Parse, var, `n, `r
	{
		if RegExMatch(A_LoopField,      "i)^\s*(Select|Switch)", $){
			stack.push($1)
			out .= A_LoopField
		}
		else if RegExMatch(A_LoopField, "i)^\s*End(Select|Switch)"){
			stack.pop()
			out .= A_LoopField
		}
		else if RegExMatch(A_LoopField, "i)^(\s*)case(.*)$", $){
			out .= $1 . stack[stack.MaxIndex()] . "case" . $2
		}
		else{
			out .= A_LoopField
		}
		out .= "`r`n"
	}
	return out
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid, Google [Bot] and 198 guests