AHK-v2-script-converter icon indicating copy to clipboard operation
AHK-v2-script-converter copied to clipboard

Trouble identifying end-of-label-block during label-to-function conversion

Open andymbody opened this issue 7 months ago • 15 comments

V1:

gosub Label1

MsgBox, % "global msg"
Return

Label1:
var := 5
if var < 5
    return   ; this return can fool any attempt to detect end of block
MsgBox, label/func msg
Return    ; final return should be used to detect end of label

MsgBox Not part of Label1

V2 (Converted):

Label1()

MsgBox("global msg")
Return

Label1()
{ ; V1toV2: Added bracket
global ; V1toV2: Made function global
var := 5
if (var < 5)
    return   ; this return can fool any attempt to detect end of block
MsgBox("label/func msg")
Return    ; final return should be used to detect end of label

MsgBox("Not part of Label1")
} ; Added bracket in the end

V2 (Expected):

Label1()

MsgBox("global msg")
Return

Label1()
{ ; V1toV2: Added bracket
global ; V1toV2: Made function global
var := 5
if (var < 5)
    return   ; this return can fool any attempt to detect end of block
MsgBox("label/func msg")
Return    ; final return should be used to detect end of label
} ; Added closing brace to func

MsgBox("Not part of Label1")

I ran into this while working on label name conversions (current project). I played with it and see that the labelToFunc conversion relies on detecting the beginning of hotkeys/hotstrings/functions to know where the previous block ends. This is very unreliable and should be fixed. I plan to tackle this issue next (after the label/func names).

Looks like the fix will require dipping into the masking bucket. I will need to add masking support for other blocks such as If/While/Loop/Switch, etc... Anything that can redirect the target label/func block using return/exitapp/ etc. Masking the sub-blocks temporarily should hide those conditional redirect commands and allow the the lableToFunc to reliably identify the final return/exitapp/exit within the label/func block. I have already have a very long If block needle and it works well for standard If/If-Else/Else, but will need to make sure all legacy IFs have been handle first. The needle handles most of the legacy IFs as well but I recently ran into a couple that were not detected. Might need to write a callout function that compliments the needle, or a dedicated function to make things easier. We will see how it goes.

andymbody avatar Jul 07 '24 20:07 andymbody