sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit 9290ec59d5b94cf01c880e8c5538eaca8d6b55ba
parent 276256e9c67680bb7e1c7e2c882d449f502e62e7
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:
Mbc.y | 8++++----
1 file changed, 4 insertions(+), 4 deletions(-)

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);}