1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? Shortcut evaluation

Discussion in 'Support' started by nickles, Jan 10, 2012.

  1. nickles

    Joined:
    Jun 24, 2008
    Messages:
    220
    Likes Received:
    0
    Is there any way to achieve shortcut evaluation of logical expressions?

    E.g. iff not isfile abc .or. %@fileage[abc] gt 12345 then...

    runs into an error if file "abc" doesn't exist.
     
  2. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    I presume what you mean is to evaluate a logical expression left-to-right, and STOP when the result is known? AFAIK that would require a major redesign of the parser.
     
  3. nickles

    Joined:
    Jun 24, 2008
    Messages:
    220
    Likes Received:
    0
    yup

    Replaced @filesize[] with @fileage[] as the previous wouldn't bail out if the file in question doesn't exist.
     
  4. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    10,091
    Likes Received:
    85
    Not really feasible unless I remove support for the boolean operations, as the parser can't tell whether you might reverse the condition somewhere later in the evaluation.
     
  5. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Excluding the .XOR. operator, wouldn't reversing the result of a test already performed require that it be within parentheses? But I see the other major parsing issue: IIRC variable expansion is performed before anything else, thus in the OP's example the attempt to evaluate %@fileage[abc] would fail before the "isfile abc" test is performed.

    When I come across a compound test such as in the OP, where the result of one of the tests determines whether or not a second test can be validly performed, I use nested IF/IFF tests; and I may need to perform Boolean manipulations to get them in the proper order. Auxiliary variables may be need, too.

    unset x
    if isfile abc if not %@filesize[abc] GT 12345 set x=no
    iff not defined x then ...

    It is more complicated than in some other programming languages, but it's all that can be done...
     
  6. Stefano Piccardi

    Joined:
    May 31, 2008
    Messages:
    376
    Likes Received:
    2
    not exactly what the OP asked for, but here's another idea, using do/leave as a glorified goto
    Code:
    do 1
      if %a == %b .and. %c == %d leave
      if %e != 32 .or. %f == A leave
      iff %something% then
        ...
        leave
      endiff
      gosub WHEN_ALL_CONDITIONS_FAIL
    enddo
    
     

Share This Page