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

DWIM parser?

Discussion in 'Support' started by Stefano Piccardi, Feb 2, 2012.

  1. Stefano Piccardi

    Joined:
    May 31, 2008
    Messages:
    376
    Likes Received:
    2
    Running the batch file at the bottom, I expected to see output
    Code:
     
    (ABC)
    () ()
    
    Instead I got
    Code:
     
    (ABC)
    (Xcon) ()
    
    WAD, DWIM or something else? I wasn't able to ascertain if the help file does say that %[name] and %name% are equivalent forms, I thought they are under all circumstances.
    Code:
    @echo off
    setlocal
    for %i in (X) (gosub LBL "%i")
    endlocal
    quit
    :LBL
    set icon=ABC
    echo (%icon%)
    unset icon
    echo (%icon%) (%[icon])
    return
    
    TCC LE 13.03.38 Windows 7 [Version 6.1.7601]
     
  2. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    The whole GOSUB is within the scope of the FOR, thus "%icon" is interpreted as in a FOR - %i becomes X (its only value set by FOR), %icon becomes Xcon... Goes back to the days of COMMAND.COM...
     
  3. Stefano Piccardi

    Joined:
    May 31, 2008
    Messages:
    376
    Likes Received:
    2
    So that's one more reason for getting in the habit of using %[name] instead of %name or %name%. %[name] seems to give consistent results under all circumstances.
     
  4. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
    Or it's one more reason not to use single-character FOR variables. TCC (unlike CMD) supports understandable variable names in FOR, not just %a - %z.
     
  5. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    559
    Likes Received:
    7
    That's a hard to habit to break. I bet if I had the TCC source code in front of me, in it I'd probably find something similar to the following...

    Code:
    for (int i=0;i<10;i++) {
      // some code here
    }
    
     
  6. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,307
    Likes Received:
    39
    No doubt, but in C++ there's no difference between one-letter and multicharacter variable names.
     
  7. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Nor in any other compiled language, AFAIK. Even among interpreted languages this one stands alone - I know of no other case but COMMAND.COM and its supersets, and even those only when declared in a FOR command, that single out single-character variable names for unique treatment. For TCC this behavior is thoroughly documented in the description of the FOR command, in the "Other Notes" section.
     

Share This Page