AutoHotkey Community

It is currently May 27th, 2012, 2:18 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 35 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject:
PostPosted: February 5th, 2010, 8:02 am 
can you update the new RegEx version to support in-program variables?

i think that might be a solution to my problem:
http://www.autohotkey.com/forum/post-329565.html


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 5th, 2010, 8:12 am 
oh whoops, maybe it already supports vars? i was glancing quickly for the old syntax [var] and didnt see the brackets


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 5th, 2010, 8:02 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Yes, the RegEx version handles global variables, too. The only problem is if they collide with local variables: i, y, y0, y1, y2, y3; or the parameter name: x. To reduce the chance of such collisions, here is a version which uses the following forbidden names, instead: _, __, ___, _0, _1, _2 and _3:
Code:
; Arithmetic expressions evaluator, handling
; unary +,- (-2*3; +3)
; +,-,*,/,\(or % = mod); **(or @ = power)
; (..); constants (pi,e); Global variables (not starting with '_'); abs(),sqrt(),floor()

abs := 3, b := 2, c := 1
MsgBox % Eval("abs*b-c") ; 5
x := 10, y2 := 100
MsgBox % Eval("x+y2") ; 110
MsgBox % Eval("-floor(abs(sqrt(1))) * (+pi -((3%5))) +pi+ 2-1-1 + e-abs(sqrt(floor(2)))**2-e") ; 1

Eval(__) {                               ; expression preprocessing
   Static pi = 3.141592653589793, e = 2.718281828459045

   StringReplace __, __,`%, \, All       ; % -> \ for MOD
   __ := RegExReplace(__,"\s*")          ; remove whitespace
   __ := RegExReplace(__,"([a-zA-Z]\w*)([^\w\(]|$)","%$1%$2") ; var -> %var%
   Transform __, Deref, %__%             ; dereference all %var%

   StringReplace __, __, -, #, All       ; # = subtraction
   StringReplace __, __, (#, (0#, All    ; (-x -> (0-x
   If (Asc(__) = Asc("#"))
      __ = 0%__%                         ; leading -x -> 0-x
   StringReplace __, __, (+, (, All      ; (+x -> (x
   If (Asc(__) = Asc("+"))
      StringTrimLeft __, __, 1           ; leading +x -> x
   StringReplace __, __, **, @, All      ; ** -> @ for easier process

   Loop {                                ; find innermost (..)
      If !RegExMatch(__, "(.*)\(([^\(\)]*)\)(.*)", _)
         Break
      __ := _1 . Eval@(_2) . _3          ; replace "(x)" with value of x
   }
   Return Eval@(__)                      ; no more (..)
}

Eval@(__) {
   RegExMatch(__, "(.*)(\+|\#)(.*)", _)  ; execute rightmost +- operator
   IfEqual _2,+,  Return Eval@(_1) + Eval@(_3)
   IfEqual _2,#,  Return Eval@(_1) - Eval@(_3)
                                        ; execute rightmost */% operator
   RegExMatch(__, "(.*)(\*|\/|\\)(.*)", _)
   IfEqual _2,*,  Return Eval@(_1) * Eval@(_3)
   IfEqual _2,/,  Return Eval@(_1) / Eval@(_3)
   IfEqual _2,\,  Return Mod(Eval@(_1),Eval@(_3))
                                        ; execute rightmost power
   StringGetPos ___, __, @, R
   IfGreaterOrEqual ___,0, Return Eval@(SubStr(__,1,___)) ** Eval@(SubStr(__,2+___))
                                        ; execute rightmost function
   If !RegExMatch(__,".*(abs|floor|sqrt)(.*)", _)
      Return __                         ; no more function
   IfEqual _1,abs,  Return abs(  Eval@(_2))
   IfEqual _1,floor,Return floor(Eval@(_2))
   IfEqual _1,sqrt, Return sqrt( Eval@(_2))
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2012, 4:05 am 
This is just Great!!!!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 31st, 2012, 4:06 am 
Offline
User avatar

Joined: July 6th, 2005, 2:57 am
Posts: 67
This is just Great!!!!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 35 posts ]  Go to page Previous  1, 2, 3

All times are UTC [ DST ]


Who is online

Users browsing this forum: MSN [Bot] and 10 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group