[hackers] [sbase] bc: Implement return statements || Roberto E. Vargas Caballero
commit 9290ec59d5b94cf01c880e8c5538eaca8d6b55ba
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.net>
AuthorDate: Sun Nov 23 20:34:34 2025 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.net>
CommitDate: Sun Nov 23 20:34:34 2025 +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.
diff --git a/bc.y b/bc.y
index 41b3282..951f283 100644
--- a/bc.y
+++ b/bc.y
_AT_@ -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);}
Received on Sun Nov 23 2025 - 20:38:54 CET
This archive was generated by hypermail 2.3.0
: Sun Nov 23 2025 - 20:48:36 CET