Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

My syntax ideas for AutoHotkey v2


  • Please log in to reply
14 replies to this topic
fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Hi,
I've recently came out with some ideas for the syntax of AutoHotkey v2.
Here are all them (needs formatting and organization):
; In v2 all "commands" are functions.
; Call syntax (parentheses can be omitted in one-line function calls, just like VB):

Function(param1, param2, param3...) ; proper syntax
Function param1, param2, param3... ; for one liners
Function, param1, param2, param3... ; for people who like the old syntax, for one liners

; Examples (MsgBox returns the button pressed):

MsgBox("Valid")
MsgBox "Valid"
MsgBox, "Valid"
var := MsgBox "Invalid"
var := MsgBox, "Invalid"
var := MsgBox("Valid")

; The old literal syntax has been removed, all parameters are now expressions.
; However, there's a special literal assignment statement:

var @= Hello World! I'm literal text.

; := is assignment, = is comparison.

foo := "bar"
bar := foo
if(bar = foo)
    MsgBox "Yes"

; Continuation sections:

var := (|
Hello.
I'm a continuation section.
To open one, use (| and a newline.
It is ended by |) on newline.
|)

password := MyEncrypt(InputBox("Password entry", (|LTrim ; just like the old v1
    Please enter your password.
    If you forgot it type "**CANTREMEMBER**"
    without the quotes.
|), true), "coolguy25")

; v2 flow statements basic structure

<name>(<parameters>)<statement_or_opening_brace>

<name>(<parameters>)
<statement_or_opening_brace>

<name> <parameters>
<statement_or_opening_brace>

; v2 if

if(GetKeyState("LButton")) MsgBox "You pressed the left mouse button."
if GetKeyState("RButton", "P")
    MsgBox "You really pressed the right mouse button."
else{
    MsgBox "You didn't press the right mouse button."
}

; v2 while loop

while(GetKeyState("LButton")){
    pos := MouseGetPos()
    ToolTip "x: " pos[0] " y: " pos[1]
    Sleep 10
}

; v2 for loop
for i: 10 ; short count loop 1..10
    MsgBox i

for i := 10: i > 0: i -= 2
    MsgBox i

for i := 1 to 10 ; alternative BASIC-like syntax - also supports step
    MsgBox i

for(::){ ; empty blocks allowed, if the middle part is blank then it defaults to true.
   gameEngine ; call an user defined function
   sleep 16
}

; Note: to use the ? and : operators we must enclose them in parentheses.

for i := 0: i < (doMore ? 20 : 10): i ++
    MsgBox i

for(: (canBreak ? (isBreaking) : true): nextState()) void ; void is a special statement that does nothing

; v2 labels - this example should have been done with a for loop
    i := 1
<loop>
    if(i > 10) goto exitloop
    MsgBox i
    goto loop
<exitloop>

; A better example (GUI programming)

gosub InitSystem
gosub SetupGui
return

<InitSystem>
    ; ...
    return

<SetupGui>
    ; ...
    return

<GuiClose>
    ExitApp

; v2 arrays
array numbers[10]
for(i: 10)
    i := InputBox("", "Enter number " i)

; v2 foreach
array windows[] = WinList()
foreach(win: windows)
    MsgBox 0, "Window Listing", win

; v2 hotkeys

hotkey "#n": Run "Notepad" ; one-liners

hotkey "#c" ; multiple lines
    IfWinExist("Calculator")
        WinActivate
    else{
        Run "calc"
        WinWait "Calculator"
        WinActivate
    }
    return

; v2 hotstrings

hotstring "btw", "by the way,{enter}{tab}" ; Simple hotstring
hotstring "*b0": "<em>", "</em>{left 5}" ; Hotstring with options
hotstring "*": "]d", FormatTime("M/d/yyyy h:mm tt") ; Hotstring with expression and options
hotstring "*": "]?" ; hotstring with action
    v := StringLower(Input("V L5"))
    Send "{Backspace " StrLen(v) "}" ; Concatenation. The dot operator is optional.
    myHandler(v)
    return

; v2 try..catch block - replaces ErrorLevel - also const variables
try{
    const listpath := "E:\PortableProgramList.txt"
    list := FileRead(listpath) "`r`nAutoHotkey|.\AutoHotkey\AutoHotkey.exe"
    FileDelete listpath
    FileAppend list, listpath
}catch(e){
    MsgBox "Error while recreating file.`nReason: " e
    ExitApp 1
}    

; v2 user-defined functions

Run "calc"
WinWait "Calculator"
WinCenter "Calculator"

function WinCenter(title){
    pos := WinGetPos(title)
    WinMove title, "", (A_ScreenWidth/2) - (Width/2), (A_ScreenHeight/2) - (Height/2)
}

; v2 clipboard - just like the good ol' v1!
temp := ClipboardAll
Clipboard := "Hello World"
Send "^v{enter}"
Sleep 200
Send "^c"
try
    ClipWait 2
catch(e)
    MsgBox "Unable to copy text to clipboard.`nReason: " e
Clipboard := temp

Notes:[*:ilnjfiom]Yes, I abuse the colon sign too much :lol:
[*:ilnjfiom]I like keywords.
[*:ilnjfiom]I mainly program in C hence the strong influences.I'm open to criticism and questions :)

Petru
  • Members
  • 236 posts
  • Last active: Jan 19 2012 06:47 PM
  • Joined: 17 Dec 2007
I like it but I'd also like to have ending punctuation like semicolon and the ability to have any number of commands on one single line...

I love Pascal... hate C/C++...
In Pascal it's like ...
var a,b:integer;
begin
readln(a,b)
write(a+b);
end.

Feels natural. :X

Thus I'd like to have something like for ... to ... do, say:

for i:=1 to 10 do
{
MsgBox, Hi there. This is iteration #%i%.
}
;; and
for i:=10 downto 1 do
{
MsgBox, Hi there. This is a reversed cycle (#%i%).
}

or maybe

repeat
{
inputbox,result,Questioning question,How much is 2+2?
}
until result=4

I think that we should mantain the variable referal system... so I can still use
MsgBox, Hi there`, %user%!
and
MsgBox,% "Hi there, " . user . "!"

I like very much that AHK is kind of combined language and syntax between BASIC, C, C++ and others. Your ideas are good too...

My only real problem with AHK at the moment is that IT DOESN'T SUPPORT UNICODE!!!! I am desperate... I went at a contests with an nice program I made in AutoHotkey and the judges were unpleased to know it didn't support Unicode (which is needed for the Romanian characters to show properly on most computers...) It was so shameful...

EDIT:
And regarding arrays, I'd like to have them like v[1], v[2], ... v[n].

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
AutoHotkey v2 will probably support Unicode.
The current literal and %% syntax just sucks compared to other programming languages.
Also, the var = start to end approach for the for() loop could be another alternative so the
following code would be valid (each loop printing numbers 1..10).
for(i: 10) MsgBox i
for(i := 1: i <= 10: i ++) MsgBox i
for(i := 1 to 10) MsgBox i


infogulch
  • Moderators
  • 717 posts
  • Last active: Jul 31 2014 08:27 PM
  • Joined: 27 Mar 2008
I really like almost all of your ideas, fincs.

I strongly disagree with forcing ternaries being surrounded by parenthesis. Though this might contribute to their readability, sometimes parenthesis make ternaries less readable. And anyway, when an expression isn't ambiguous, this should be at the programmer's discretion.

I agree that it should include a "For" loop, but a colon shouldn't be the separator. That's something that might be discussed in a different thread. Perhaps a comma. That's common enough for parameter separators, even in our current loops. If you want a sub-expression, surround it in parenthesis like usual.

I might also add:

Force labels to have a reasonable set of limited characters. (like functions)

Currently our "literal" text (in command parameters and the = assignment operator) dereferences variables with the % symbol. We need this same functionality for expressions. Other languages implement this with double quotes, and do non-dereferencing text with single quotes. So my suggestion is:
Add single quoted literal undereferenced text in expressions that act like v1 double quotes.
Change double quoted text to dereference variables encased in %'s.

Petru
  • Members
  • 236 posts
  • Last active: Jan 19 2012 06:47 PM
  • Joined: 17 Dec 2007

AutoHotkey v2 will probably support Unicode.

But when will version 2 be released? In 2011?

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Ternaries only need to be enclosed in parentheses in for loops.
I chose the colon sign as a separator to allow sentences like this:
var := isTrue ? "yes" : "no" ; valid
for i := 1, j := 0: i <= 10: i ++, j += log(i)
    MsgBox i ", " j

Having %% in double-quoted text just defeats the purpose of expressions.
Also, it becomes useless when arrays enter the house (no more need to
have pseudo-arrays!).
But having %% in continuation expressions and &='ed variables would be nice.

ruespe
  • Members
  • 567 posts
  • Last active: Dec 01 2014 07:59 PM
  • Joined: 17 Jun 2008
I agree with a "For"-Loop. But the syntax should be like
For, var, start-val, end-val[, step]
For i, 1, 10
For i, 1, 10, 2
For i, 1, 2, 0.1
For i, 2, 1, -0.1
'Cause everybody has or has had probs with literals and vars, only one syntax should survive: literals always in " or ' (exept var @= literal), vars without the need of % (exept var1%var2% for arrays)

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
@ruespe: I've also thought about using BASIC-style syntax for for loops, but as I'm a C/C++ guy and I didn't
want to lose the power of C-style for loops I decided to have different syntaxes for for() that would satisfy everyone :D
for(i: 10) MsgBox i ; same as our current loop
for(i := 0: i < 10: i ++) MsgBox i ; C-style loops
for(i := 0 to 9) MsgBox i ; BASIC-style loops
for(i := 0 to 9 step 0.5) MsgBox i ; BASIC-style loops with step

And as I said before, there's no need for %% in expressions because
real arrays would have been introduced.

infogulch
  • Moderators
  • 717 posts
  • Last active: Jul 31 2014 08:27 PM
  • Joined: 27 Mar 2008

I chose the colon sign as a separator to allow sentences like this: ...

That would already be possible with commas, as we encounter this eventuality in expressions already:
for( (i := 1, j := 0), (i <= 10), (i++, j += log(i)) )
    MsgBox i ", " j)
; note that in current autohotkey v1 if for() is a user defined function the first line would be a valid call to it with three parameters
This way is simpler, conforms to current autohotkey expression syntax, and would be very simple to parse, since the for is simply three expression parameters that are evaluated at different times. (The first param once before, the second param every time before execution (breaks if false), the third every time at the end of execution.)

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
infogulch: you're right :D

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

AutoHotkey v2 will probably support Unicode.

Don't care much for AHK 2 but I think Unicode support is wishful thinking to put
it mildly (both Chris and Lexikos say it is very difficult/too much work etc)

Petru
  • Members
  • 236 posts
  • Last active: Jan 19 2012 06:47 PM
  • Joined: 17 Dec 2007
I'd also like to be able to edit strings just like arrays.
For example

s:="abc"
msgbox s[1] ;will show "a"
s[2]:="i"   ;s will be "aic"

for i:=1 to strlen(s)
{
msgbox s[i] ;will show "a", then "i", then "c"...
}


fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
That's a good idea that I miss in many languages, unfortunately :(.

Petru
  • Members
  • 236 posts
  • Last active: Jan 19 2012 06:47 PM
  • Joined: 17 Dec 2007
Pascal has it though...

Petru
  • Members
  • 236 posts
  • Last active: Jan 19 2012 06:47 PM
  • Joined: 17 Dec 2007
But these are dreams... God knows when AHK 2 will be released...