Jump to content

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

eval() - evaluate AutoHotkey expressions dynamically!


  • This topic is locked This topic is locked
17 replies to this topic
fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Some of you may remember that Lexikos' LowLevel.ahk used to support a nifty function called __expr(), but then...

I recently realised that __expr cannot easily be made to work with the pre-v1.0.48 beta, since all expressions must be pre-tokenized before evaluation. In other words, it would need to implement a full expression parser, which is simply something I will not do. (If Line::ExpressionToPostfix() were exposed to script it would be possible, but permanent memory would be allocated for each expression evaluated.)

... which is what I crazily did :D

Introducing...

expr() v1.0

This library allows you to dynamically execute AutoHotkey expressions on the fly, with NO temporary scripts at all! It works via parsing the expression manually using AHK source code translated from C++ to AHK (!), then executing it using a bit (understatement) of low-level magic. As such, it may or may not work with AutoHotkey versions newer than v1.1.08.

Usage is really simple:
MsgBox % eval("(1 + sqrt(5)) / 2")
eval("DllCall(""MessageBox"", ""ptr"", 0, ""str"", ""Hello, world!"", ""str"", A_ScriptName, ""uint"", 0)")
MsgBox % eval("eval(""2+2"")") ; you can even nest calls to eval()!
eval("variable := ""value""") ; variables are also supported...
eval("obj := { array: [2, 3, 5] }") ; even objects!
MsgBox % variable " | " obj.array[2]
Download - GitHub

Included with the package is calculator.ahk, which allows you to type commands. A fun one is this:
ComObjCreate("SAPI.SpVoice").Speak("Hello world!")
Limitations[*:1bxrzo80]You can only access global variables
[*:1bxrzo80]Double-derefs are not supported yet
[*:1bxrzo80]Clipboard is read-only
[*:1bxrzo80]ClipboardAll doesn't work
[*:1bxrzo80]obj.field(something) := val is not supported yet
[*:1bxrzo80]Compound object field assignment is not supported yet

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
Very interesting! Could you point out some potential uses? A calculator obviously comes to mind, but other methods might be more suited as they could support units for example.

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
fragman: well, I did not think of any uses besides making a calculator. I'll let users be creative :)

flyingDman
  • Spam Officer
  • 2186 posts
  • Last active: Nov 07 2015 08:15 AM
  • Joined: 27 Feb 2009
What are the advantages over Lazlo's "Monster" (http://www.autohotke...pic.php?t=17058)?

Marine Corps Gen. Joseph Dunford told senators at his Joint Chiefs of Staff confirmation hearing : “If you want to talk about a nation that could pose an existential threat to the United States, I'd have to point to Russia. And if you look at their behavior, it's nothing short of alarming.”


fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Laszlo's Monster does not support the full AutoHotkey expression syntax (including strings, objects and built-in functions/variables) and it sometimes did not evaluate expressions correctly when I was testing it a long time ago (sadly, I cannot remember which expressions they were).

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
How does it compare to ExprEval(), which presumably isn't as version-dependent? Since you haven't mentioned ternary or short-circuit evaluation, I presume your function supports those.

NOTE: Does not support AHK_L object references, ternary operators, or boolean short-circuiting.



fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007

How does it compare to ExprEval(), which presumably isn't as version-dependent? Since you haven't mentioned ternary or short-circuit evaluation, I presume your function supports those.

This library does support both ternary and short-circuit evaluation, as it's a pretty straightforward translation of AHK's expression parsing code. It also supports object syntax, variable operations (with some restrictions on built-in variables that are to be fixed) as well as function operations (including variadic calls). Also, it may be faster because it does not use RegEx (unlike Monster or ExprEval), it doesn't do ugly string operations (which the others do due to AHK Basic's lack of objects) and it lets AHK itself perform the evaluation (I haven't benchmarked it). Also, the source code is significantly more readable :)

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
This is great, fincs. I suppose you don't intend to make a Basic-compatible version, right?

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
MasterFocus: no, AutoHotkey Basic is ridiculously outdated and I don't plan on making scripts compatible with it anymore. Not to mention I make heavy use of objects and other new features, and in this case internal AHK structures.

IsNull
  • Moderators
  • 990 posts
  • Last active: May 15 2014 11:56 AM
  • Joined: 10 May 2007
fincs this is a great hack!

Injecting the tokens directly into the AHK Interpreter is really bad ass! :mrgreen:

Even this one works:
([10,20,30])[2]


adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012
Wow. Crazy! I'm a little afraid to use it. :)

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
One use I've found for eval() is that you can give it a JSON string and eval() will turn it into an AutoHotkey object.
cool.png

Edit: Still working with AutoHotkey v1.1.11.01 grin.png

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
It will not in the latest AutoHotkey preview because of the addition of SYM_MISSING.

rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

It will not in the latest AutoHotkey preview because of the addition of SYM_MISSING.


sad.png

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


Moebius
  • Members
  • 39 posts
  • Last active: May 11 2015 08:46 PM
  • Joined: 08 Mar 2009

Hi,

it does not work with:

eval("FormatTime, time, , yyyy-MM-dd HH:mm:ss tt")

throws an exeption: "error while executing expression - A ":" is missing its "?"

nor

eval("FormatTime, time")

no exeption, but time is empty

 

Is there a way to get it to work?

 

About usage: i would like to integrate it into my m00lmt script (shared here).

You can define abbreviations to trigger an eval expression.

for example you enter: //m

and the script expands it to: //m00n 22.07.2013