Alpha Video Training
Results 1 to 6 of 6

Thread: Expression Evaluation

  1. #1
    "Certified" Alphaholic G Gabriel's Avatar
    Real Name
    G. Gabriel
    Join Date
    Oct 2004
    Posts
    7,204

    Default Expression Evaluation

    Say in a script you have something like:

    Code:
    select
    case A=1 .and. B=2
    do this
    
    case (A=2.and.B=3).or.C=1
    do this
    end select
    At what point does the expression fails and alpha move on to the next selection?

    In the first case:
    Does it fail immediately at A if A <>1? or does evaluate the whole expression?

    In the second case: same quesion.

  2. #2
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Expression Evaluation

    To see how code evaluates and when use code like the following;
    Code:
    a=3
    b=6
    c=2
    
    select
    case A=afunc(1,"msg A1") .and. B=afunc(2,"msg B1")
        ui_msg_box("","1")
    case (A=afunc(2,"msg A2").and.B=afunc(3,"msg B2")).or.C=afunc(1,"msg C2")
        ui_msg_box("","2")
    end select
    
    end
    '============================
    function afunc as a(val as A,msg="" as c)
    ui_msg_box("",msg)
    afunc=val
    end function
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  3. #3
    "Certified" Alphaholic G Gabriel's Avatar
    Real Name
    G. Gabriel
    Join Date
    Oct 2004
    Posts
    7,204

    Default Re: Expression Evaluation

    Ira:
    I have a sense of dissmay!

    Running your sample test shows few things that I didn't expect nor hoped to see:

    a-Alpha is evaluating the expression from right to left. That's not a big deal.

    b-I hope I am wrong on this, but from the looks of it, alpha is not following the precedence rules. That is the bad part.

    c-Before failing the expression, alpha evaluates the entire expression. That would be the worst part.

  4. #4
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Expression Evaluation

    Quote Originally Posted by G Gabriel View Post
    a-Alpha is evaluating the expression from right to left.
    Very typical of most languages

    Quote Originally Posted by G Gabriel View Post
    b-I hope I am wrong on this, but from the looks of it, alpha is not following the precedence rules.
    It is following the precedence rules. Each level of precedence is evaluated before moving to the next level. Within a precedence level, it can be any order. You can prove it by placing parenthesis around sections and see that the insides of parenthesis are evaluated prior to being needed elsewhere in the expression.

    Quote Originally Posted by G Gabriel View Post
    c-Before failing the expression, alpha evaluates the entire expression. That would be the worst part.
    See Lazy Evaluation and related links.

    If using an IF(logical_expr, true_expr, false_expr), you will find the false expression is not evaluated if true. This allows for expressions to test for divide by 0 by not executing the 2nd part within the function. But this has nothing to do with expression evaluation, as it is within the function's code as to the order of evaluation of it's parameters (although the false expression is treated special, because normally, that value would be calculated at the call of the function since most expressions are call by value. In this case, it is more like the middle parameter of *for_each(), where you are not really evaluating the parameter, but passing it as a string (even though there are no quotes around it.
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  5. #5
    "Certified" Alphaholic G Gabriel's Avatar
    Real Name
    G. Gabriel
    Join Date
    Oct 2004
    Posts
    7,204

    Default Re: Expression Evaluation

    I am not really sure if it is following the rules. It is evaluating .or. before .and. even after enclosing .and. in parenthisis.
    See attached image showing first msg:

    Attachment 16807
    While I agree with you that evaluating right to left is most common in most programming languange, alpha no exception, yet I thought in case of select it might work left to right as it's counterpart case(). Again, it's not a big issue.

    I had hoped that once alpha comes across a false statment that it will abandon the rest of the expression and move on to the next one. For example, in an expression like:

    case A=1.and. B=2

    since alpha will evalute this right to left and if b does not equal 2, and since the expression states .and. then regardless of what A is the expression will render a false value. I had hoped that once alpha comes across the first false statment that it will abandon the rest of the expression and move on to the next line. But here, alpha will continue to evalue the rest of the expression, that is it will test to see if A equals 1, which is a useless test.
    Last edited by G Gabriel; 04-04-2008 at 09:48 AM.

  6. #6
    "Certified" Alphaholic G Gabriel's Avatar
    Real Name
    G. Gabriel
    Join Date
    Oct 2004
    Posts
    7,204

    Default Re: Expression Evaluation

    Taking a second look at this, maybe not:
    The .or. statment is part of th last statment and thus it was evaluated first.
    Parenthises always a help.

    I have no bone to pick with any of that except, I wish alpha did not evaluate the whole expression. The expression in this example is very simple, but if it were a more complex one and if it is to be evaluated over so many iterations, then it becomes a problem.

Similar Threads

  1. Evaluation Order
    By russ Boehle in forum Alpha Five Version 8
    Replies: 2
    Last Post: 03-11-2007, 08:59 PM
  2. Evaluation within a Filter
    By KH Koh in forum Alpha Five Version 5
    Replies: 34
    Last Post: 02-05-2007, 09:22 AM
  3. Evaluation
    By Frank Russ in forum Alpha Five Version 5
    Replies: 1
    Last Post: 12-26-2003, 11:34 AM
  4. Evaluation error
    By John Blum in forum Alpha Four Versions 7 and 8
    Replies: 1
    Last Post: 12-01-2003, 04:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •