bc: Implement return statements - sbase - suckless unix tools
 (HTM) git clone git://git.suckless.org/sbase
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 9290ec59d5b94cf01c880e8c5538eaca8d6b55ba
 (DIR) parent 276256e9c67680bb7e1c7e2c882d449f502e62e7
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
       Date:   Sun, 23 Nov 2025 20:34:34 +0100
       
       bc: Implement return statements
       
       When a return is found we have to go out of all the nested contexts
       and _ideally_ that number should be in nested, but we are including
       the function contexts here. Also, this makes clear that the
       implementation of break was wrong, because we have to find the nesting
       level of the previous loop to break it. Also, at this moment no
       semantic check is done to vaalidate that we are in a loop or even
       in a function, and it means that weird things can happen if we put
       a return out of a function scope.
       
       Diffstat:
         M bc.y                                |       8 ++++----
       
       1 file changed, 4 insertions(+), 4 deletions(-)
       ---
 (DIR) diff --git a/bc.y b/bc.y
       @@ -121,11 +121,11 @@ statlst :                       {$$ = code("");}
        
        stat    : exprstat
                | STRING                {$$ = code("[%s]P", $1);}
       -        | BREAK                 {$$ = code(" %dQ", nested);}
       +        | BREAK                 {$$ = code(" %dQ", nested);} /* FIXME */
                | QUIT                  {quit();}
       -        | RETURN
       -        | RETURN '(' expr ')'
       -        | RETURN '(' ')'
       +        | RETURN                {$$ = code(" %dQ", nested);}
       +        | RETURN '(' expr ')'   {$$ = code(" %s %dQ", $3, nested);}
       +        | RETURN '(' ')'        {$$ = code(" %dQ", nested);}
                | FOR fordef stat       {$$ = forcode($2, $3);}
                | IF cond stat          {$$ = ifcode($2, $3);}
                | WHILE cond stat       {$$ = whilecode($2, $3);}