I do some tests and conclusion - AHK have main problem with %Expr%.
Test 1: global variables and functions
Code: Select all
deref(__exp) {
return RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp
}
a:= 5
b:= 5
c:= 5
while A_Index <= 3 {
msgBox(deref("a= %a% | b= %b% | c= %c%"))
a+=1, b+=2, c+=3
}
As we expected a, b, c not visible inside function deref.
Test 2: global variables and short functions
Code: Select all
deref(__exp) => RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp
a:= 5
b:= 5
c:= 5
while A_Index <= 3 {
msgBox(deref("a= %a% | b= %b% | c= %c%"))
a+=1, b+=2, c+=3
}
In this case a, b, c visible inside short function deref.
Short function deref(__exp) work like MACROS when in global scope.
Test 3:We expected that short function will see variables in the same scope as in Test 2 (but)
Code: Select all
fun()
fun() {
deref(__exp) => RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp
a:= 1
b:= 1
c:= 1
while A_Index <= 3 {
msgBox(deref("a= %a% | b= %b% | c= %c%"))
a+=1, b+=2, c+=3
} }
Test 4:Nested function without "some action"
Code: Select all
fun()
fun() {
deref(__exp) {
return RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp
}
a:= 1
b:= 1
c:= 1
while A_Index <= 3 {
msgBox(deref("a= %a% | b= %b% | c= %c%"))
a+=1, b+=2, c+=3
} }
Test 5:Nested function with "some action"
Code: Select all
fun()
fun() {
deref(__exp) {
_:= a ;a action
_:= b ;b action
_:= c ;c action
return RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp
}
a:= 1
b:= 1
c:= 1
while A_Index <= 3 {
msgBox(deref("a= %a% | b= %b% | c= %c%"))
a+=1, b+=2, c+=3
} }
Any variable that must be seen - must have "some action".
Test 6:BASIC programming style.
Code: Select all
fun()
fun() {
a:= 1
b:= 1
c:= 1
while A_Index <= 3 {
__exp:= "a= %a% | b= %b% | c= %c%"
gosub derefLABEL
msgBox(__exp)
a+=1, b+=2, c+=3
}
return
derefLABEL:
if RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var) {
__exp:= RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")
goto derefLABEL
}
return
}
As we not expected there only 2 working sets: Test 2 and Test 6.
Conclusions:
1. AHK have 3 type of functions.
2. Short Functions and BASIC style worked in global scope like MACROSes.
3. Nested functions not work as we expected.
4. Probably it is main [BUG] how %Expr% work.