Eval - Evaluate Expressions in Strings Dynamically (Updated 07/08/2016)

Post your working scripts, libraries and tools
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Eval - Evaluate Expressions in Strings Dynamically (Updated 07/08/2016)

31 Jan 2016, 23:33

Array := Eval(String [, CustomVars])

This is my take on the Eval() function. The actual evaluation is done by Uberi's ExprEval(), but I've added parsers to support objects and ternary. There's also an optional parameter to set Custom Variables, mostly usable to resolve built-in variables like A_Index, A_LoopField, etc. Usage is explained in the header.

Code: [Select all] [Expand] [Download] (Eval.ahk)GeSHi © Codebox Plus


:arrow: Sources and examples on GitHub

Know limitation: Some short variable names used in the ExprEval() should be avoided as they will not be correctly evaluated. The names are a, e, i, o, r, s, t, ac, eo, lf, lp, tt and numbered variations of them like a1, a2... tt1, tt2....
Last edited by Pulover on 07 Aug 2016, 11:52, edited 22 times in total.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
oldbrother
Posts: 134
Joined: 23 Oct 2013, 05:08

Re: Eval - Evaluate Expressions in Strings

01 Feb 2016, 12:59

Is is possible to make "Eval" to interact with the main program?

Like this:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus




Edited!

Sorry, after I added StrJoin(), it worked. Thanks a lot!

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by oldbrother on 01 Feb 2016, 13:11, edited 1 time in total.
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings

01 Feb 2016, 13:07

Hello oldbrother,

The output is an array, because you can evaluate multiple expressions (e.g. "Var1 * Var2, Var1 / Var2") so you have to select one to show.
To make your test work you can either do this:

Code: [Select all] [Download] GeSHi © Codebox Plus

Result := Eval(Expression)[1]

...or this:
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
oldbrother
Posts: 134
Joined: 23 Oct 2013, 05:08

Re: Eval - Evaluate Expressions in Strings

01 Feb 2016, 13:12

Thank you very much!
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings

01 Feb 2016, 22:02

Update:
  • Fixed problem solving more than one object reference in the same expression.
  • Fixed problem concatenating object values and other values.
Last edited by Pulover on 16 Mar 2016, 20:04, edited 1 time in total.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
oldbrother
Posts: 134
Joined: 23 Oct 2013, 05:08

Re: Eval - Evaluate Expressions in Strings

02 Feb 2016, 12:06

Hi Pulover,

Is it possible to get the result form multiple line equations?

For example:

D :=1.5
L :=2
Pi :=3.14

Str = ( S := Pi * (D/2)**2
V := S * L
Weight := V * 0.3
)

And then use Eval to get the result of "Weight".

Thanks!
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings

02 Feb 2016, 16:50

Hi oldbrother,

You can use Join, or StrReplace() to replace line breaks with commas. Only trouble is the limitation I mentioned above about variable names used in ExprEval(), I had to rename the ones in your example to make it work.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


I tried to rename variables in Uberi's functions but it stopped working. I will try to change at least a few of them.

Edit: I used a function to test all possible alphabetical variable names from a to zzz and found that the only names that should be avoided are a, e, i, o, r, s, t, ac, eo, lf, lp, tt and numbered variations like a1, a2... tt1, tt2.... Replacing even one of them is difficult because of combinations and variations, so I won't touch that.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Gio
Posts: 131
Joined: 30 Sep 2013, 10:54

Re: Eval - Evaluate Expressions in Strings

03 Feb 2016, 08:39

The function works nicely, thank you for it :thumbup:

I have some questions:

1 - Uberi stated that ExprEval was 28x slower than actual expression evaluation in one of his examples. Are there any big practical advantages of using this method vs the ordinary expression method though? Can you please provide examples?

2 - Is this part of a bigger project? (a compiler os something like that)?

Best wishes.
"What is a suitable automation? Whatever saves your day for the greater matters."
Barcoder - Create QR Codes and other Barcodes using only Autohotkey !!
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings

03 Feb 2016, 09:19

Hi Gio,

I haven't made performance tests, but it works fast enough for my needs :) .
The one practical use for this function is provide a method to evaluate expressions dynamically, i.e. you can get results from copied text, user input, read files, etc. Without eval you cannot build something like a calculator, or the expression tester I posted above.

I made this function to use with the next version of my Macro Creator. The current version uses Laszlo's Eval, but it's limited to math expressions. When I first tried Uberi's, some years ago, for some reason I couldn't get it to work with my project (I probably did something wrong), but in the last weeks I was looking for a way to support 'real' expressions and gave it another try and it worked well, allowing me to support multiple statements for example (a || b, a && b...). I knew how to evaluate object calls in strings, so I tried to combine what I did in my ComInterface function for PMC to give it support for object creation and calls, as well as functions that return objects, such as StrSplit(). The last thing I did was find a way to support ternary, one of the requests from Uberi's original post. I haven't tested short-circuiting, though.

Regards.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Gio
Posts: 131
Joined: 30 Sep 2013, 10:54

Re: Eval - Evaluate Expressions in Strings

03 Feb 2016, 09:29

Ok, thanks for the explanation, those examples did sparkled some more ideas :thumbup:
"What is a suitable automation? Whatever saves your day for the greater matters."
Barcoder - Create QR Codes and other Barcodes using only Autohotkey !!
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings

03 Feb 2016, 14:39

Update:
  • Added support for Object references in [key, key] format.


I was forgetting that it's possible to assign and access multi-dimensional arrays that way... I updated the function to make it consistent with this feature.

Example:

Code: [Select all] [Download] GeSHi © Codebox Plus

Expression = 
(Join,
Obj := []
Obj[1, "Pi"] := 3.141593
)

Eval(Expression)

MsgBox % Eval("Obj[1, ""Pi""]")[1]
Last edited by Pulover on 16 Mar 2016, 20:03, edited 1 time in total.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings

06 Feb 2016, 22:03

Update:
  • Updated to resolve objects inside CustomVars values.
Last edited by Pulover on 16 Mar 2016, 20:03, edited 1 time in total.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings

10 Feb 2016, 09:54

Update:
  • Fixed bug in ExprEval() when concatenating variables or numbers with a number.

I had fixed a few bugs in Uberi's function already, and I have found now that it cannot process expressions like Variable 1 or Variable 1.1. Took me some time to figure out the code, but I managed to solve the problem.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings

16 Mar 2016, 20:12

Update:
  • Added support for Unicode strings.
  • Added support for dynamic references of objects and functions.
  • Fixed various bugs with literal strings.
I found that ExprEval() cannot parse Unicode strings, so I added a workaround.
The function should be able to evaluate dynamic references now, such as fn := "StrLen", %fn%("some string") and similar, as well as object calls with them.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings

21 Mar 2016, 17:55

Update: v1.1.1
  • Fixed creating linear arrays not working with string or array elements.
My mistake this time... forgot to restore strings and other elements before creating linear arrays.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings Dynamically (Updated 03/21/2016)

21 Mar 2016, 20:19

Update: v1.1.2
  • Fixed bug when creating associative arrays with commas in values (such as arrays or function parameters).
Just found another problem (I do hope they all will be fixed some day...), this time with Associative Arrays. Something like person := {name: "John", surname: "Smith", age: 26, stats: [10,9,8.5,7,9.5,10]} would not work because of the commas in stats. Fixed now.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings Dynamically (Updated 03/21/2016)

24 Mar 2016, 20:21

Update: v1.1.3
  • Added support for missing parameters in function calls.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Re: Eval - Evaluate Expressions in Strings Dynamically (Updated 03/21/2016)

01 Apr 2016, 11:30

Update: v1.1.4
  • Fixed bug in ExprEval() with hex numbers.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Update v1.1.5

14 Apr 2016, 10:06

Update: v1.1.5
  • Fixed problem with setting false value to COM object parameters.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)
User avatar
Pulover
Posts: 365
Joined: 29 Sep 2013, 19:51
Location: Brazil
Contact:

Update v1.1.6

27 Apr 2016, 11:26

Update: v1.1.6
  • Fixed identical function or object calls in being executed only once.
Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Yahoo [Bot] and 18 guests