Skip to content

Commit

Permalink
* Fixed bugs in math operations with subtraction.
Browse files Browse the repository at this point in the history
* Fixed bug with negative signs before parenthesis.
* Fixed bug in assignments.
  • Loading branch information
Pulover committed Sep 26, 2020
1 parent d019b0a commit e1785f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 14 additions & 2 deletions Eval.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ Eval($x, _CustomVars := "", _Init := true)
While (RegExMatch($z[$i], "(""&_String\d+_&""\s+)([^\d\.,\s:\?])"))
$z[$i] := RegExReplace($z[$i], "(""&_String\d+_&""\s+)([^\d\.,\s:\?])", "$1. $2")

; Remove remaining parenthesis to allow math operations
While (RegExMatch($z[$i], "\(([^()]++|(?R))*\)"))
$z[$i] := RegExReplace($z[$i], "\(([^()]++|(?R))*\)", "$1")

; Evaluate right hand of assignments
AssignParse($z[$i], _InVar, _OnOper, _OutValue)
If ((!InStr(_OutValue, "&_String")) && (_InVar && _OnOper && _OutValue))
$z[$i] := _InVar . _OnOper . Eval(_OutValue, _CustomVars, false)[1]

; Evaluate parsed expression with ExprEval()
ExprInit()
, CompiledExpression := ExprCompile($z[$i])
Expand Down Expand Up @@ -280,8 +289,11 @@ Eval($x, _CustomVars := "", _Init := true)
}

; Check if there are still math operations inside the resulting string
If (RegExMatch(StrJoin($z), "^\s*\b\d+\b\s*[+\-/<>&^|``%!~*]+\s*\b\d+\b\s*$"))
return Eval(StrJoin($z), _CustomVars, false)
For _i, _v in $z
{
If (RegExMatch(_v, "^\s*\b\d+\b\s*[+\-/<>&^|``%!~*]+\s*\b\d+\b\s*$"))
$z[_i] := Eval($z[_i], _CustomVars, false)[1]
}

return $z
}
Expand Down
13 changes: 7 additions & 6 deletions Examples/EvalExamples.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
SetBatchLines, -1

; Math Expressions
Expression := "2**(4*-1+10)/Floor(-1.3) /*`nThis is a multiline comment`n*/,SomeText:=(2<<1)*-3/Sin(4),1+5*3 `;this is a single line comment`n . "" Appended line"""
Expression := "-(2.616700)-193,2**(4*-1+10)/Floor(-1.3) /*`nThis is a multiline comment`n*/,SomeText:=(2<<1)*-3/Sin(4),1+5*3 `;this is a single line comment`n . "" Appended line"""
Result := Eval(Expression)
r := StrJoin(Result, "`n")
MsgBox, 1, Math Expression, % "Expression: `n`n" Expression "`n`n`nResults:`n`n" r
Expand All @@ -17,9 +17,10 @@ MsgBox, 1, Operators, % "Expression: `n`n" Expression "`n`n`nResults:`n`n" r
IfMsgBox, Cancel, ExitApp

; Variable Assignment
Expression := "VarX := ""First,Second,Third,Fourth"""
Expression := "VarA := 123,VarB := VarA - 23,VarX := ""First,Second,Third,Fourth"""
Result := Eval(Expression)
MsgBox, 1, Variable Assignment, % "Expression: `n`n" Expression "`n`n`nResults:`n`n" VarX
r := StrJoin(Result, "`n")
MsgBox, 1, Variable Assignment, % "Expression: `n`n" Expression "`n`n`nResults:`n`n" r
IfMsgBox, Cancel, ExitApp

; String & Function
Expand Down Expand Up @@ -137,19 +138,19 @@ MsgBox, 1, Ternary Operator with objects, % "Expression: `n`n" Expression "`n`n`
IfMsgBox, Cancel, ExitApp

; COM Object Expressions
Expression := "ie := ComObjCreate(""InternetExplorer.Application""), ie.Visible := true, ie.Navigate(""www.autohotkey.com"")"
Expression := "ie := ComObjCreate(""InternetExplorer.Application""), ie.Visible := true, ie.Navigate(""www.autohotkey.com/search/"")"
MsgBox, 1, COM Object Expressions, % "Expression: `n`n" Expression "`n`n`nPress OK to execute"
IfMsgBox, Cancel, ExitApp
Result := Eval(Expression)

; Set COM Object Property
Expression := "ie.document.getElementsByName(""q"")[0].Value := ""comobjcreate"""
Expression := "ie.document.getElementsByName(""search"")[0].Value := ""comobjcreate"""
MsgBox, 1, Set COM Object Property, % "Expression: `n`n" Expression "`n`n`nPress OK to execute"
IfMsgBox, Cancel, ExitApp
Result := Eval(Expression)

; Get COM Object Property
Expression := "Query := ie.document.getElementsByName(""q"")[0].Value"
Expression := "Query := ie.document.getElementsByName(""search"")[0].Value"
Result := Eval(Expression)
MsgBox,, Get COM Object Property, % "Expression: `n`n" Expression "`n`n`nResults:`n`nQuery: " Query

0 comments on commit e1785f8

Please sign in to comment.