mmikeww / AHK-v2-script-converter

AHK v1 -> v2 script converter
https://autohotkey.com/boards/viewtopic.php?f=6&t=25100
The Unlicense
578 stars 43 forks source link

Multi-statement returns #297

Closed Banaanae closed 1 week ago

Banaanae commented 1 month ago

V1:

MyFunc() {
    global
    Return a := 1, b := a + 1
}
MyFunc()
MsgBox % a " " b

V2 (Converted):

MyFunc() {
    global
    Return a := 1, b := a + 1
}
MyFunc()
MsgBox(a " " b)

V2 (Expected):

MyFunc() {
    global
    a := 1
    Return  b := a + 1
}
MyFunc()
MsgBox(a " " b)

Return now only accepts 1 param

Expected needs more work, because above logic does not work with below

MyFunc() {
    global b
    Return a := 1, b := a + 1
}
MyFunc()
MsgBox % MyFunc() " " b ; 1 2
MyFunc() {
    global b
    a := 1
    Return b := a + 1
}
MyFunc()
MsgBox(MyFunc() " " b) ; 2 2
Banaanae commented 1 month ago

~~Turns out putting in brackets works Submitting fix now~~ Not perfect fix :/ ill see if theres alternatives

Banaanae commented 1 month ago

The problem with this is v1 returns first statement while v2 returns last

Return (a := 1, b := a + 1)
;         ^            ^
;         v1           v2

We could either copy first statement to the back

Return (a := 1, b := a + 1, a := 1) ; Or even just 'a'

; For example
Return SomeFuncValue(), ErrorLevel := 0
; ->
Return (V1toV2_Temp := SomeFuncValue(), ErrorLevel := 0, V1toV2_Temp)

Or give warning

Return (a := 1, b := a + 1) ; V1toV2: Now returns last statement instead of first
fade2gray commented 1 month ago

I came across this in my attempt to convert VisualDiff to ah2, before giving up, and added this to ConvertFuncs.ahk ...

if InStr(Line, 'return')
{
    checkReturnParams(&Line)
}

checkReturnParams(&Line)
{
    cc := po := pc := ip := 0
    cpa := []

    for i, char in StrSplit(Line) {
        if char = ','
            cc++, cpa.Push(i)
        else if char = '('
            po := i
        else if char = ')'
            pc := i
    }

    for _, index in cpa {
        if po && pc && (index > po && index < pc)
            ip++
    }

    if ip != cc {
        Line := RegExReplace(Line, 'i)^(\s*return\s*)(.*)', '$1($2)')
        Line .= ' `; V1toV2: Multiple params enclosed in parantheses'
    }
}

¯\(ツ)