Version 1.1 (Tuesday, March 29, 2016)
Description:
Need to do math on a piece of text like "5+5-74*pi"? AHK doesn't allow that! But now you can.
All the other eval things on the forums are massive. AHK now has COM. COM can do IE/JS stuff. JS supports eval(). Tap into JS with AHK and you got a nice simple powerful short eval function.
EXAMPLES:
Code: Select all
; example 1
var:=eval("pi*e*4")
msgbox %var%
; example 2
msgbox, % eval("5+5")
; example 3
if (eval("5+3")=132)
msgbox, your math is wrong
else
msgbox, 5+3 does not equal 132!
; example 4
var=10
msgbox % eval("%var%+10+" var)
Code: Select all
; Name: eval
; Version 1.1 (Tuesday, March 29, 2016)
; Created: Monday, March 28, 2016
; Author: tidbit
; Credit: TLM
; Description: Uses Javascript/COM to evaluate stringed math expressions.
; Supported constants and functions: http://www.w3schools.com/jsref/jsref_obj_math.asp
; Example: msgbox, % eval("5*pi+(7*sqrt(27))/2")
eval(exp)
{
transform, exp, deref, %exp%
; make everything lowercase, set constants to uppercase
exp:=format("{:l}", exp)
exp:=regExReplace(exp, "i)(E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)", "$U1")
exp:=regExReplace(exp, "i)(abs|acos|asin|atan|atan2|ceil|cos|exp|floor"
. "|log|max|min|pow|random|round|sin|sqrt|tan"
. "|E|LN2|LN10|LOG2E|LOG10E|PI|SQRT1_2|SQRT2)", "Math.$1")
obj:=ComObjCreate("HTMLfile")
obj.write("<body><script>document.body.innerText=eval('" exp "');</script>")
return inStr(cabbage:=obj.body.innerText, "body") ? "ERROR" : cabbage
}
Spoiler