mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-07-30 16:26:28 +08:00
Fix #473: Allow assignment expressions in return statements
The Return() parser function called Operator() to parse the return value, which only handles arithmetic/logical operators but not assignments. Changed it to call Equation(), which wraps Operator() and adds assignment parsing. This is consistent with how If, For, and function argument parsing already work. Enables `return foo = 5`, `return x += 1`, etc. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
7b95ff5126
commit
ffb8c82818
@ -2282,7 +2282,7 @@ namespace chaiscript {
|
||||
const auto prev_stack_top = m_match_stack.size();
|
||||
|
||||
if (Keyword("return")) {
|
||||
Operator();
|
||||
Equation();
|
||||
build_match<eval::Return_AST_Node<Tracer>>(prev_stack_top);
|
||||
return true;
|
||||
} else {
|
||||
|
||||
16
unittests/return_assignment.chai
Normal file
16
unittests/return_assignment.chai
Normal file
@ -0,0 +1,16 @@
|
||||
// Test that assignment expressions work inside return statements
|
||||
// Issue #473: `return foo = 5` should work
|
||||
|
||||
def return_assign() {
|
||||
var x = 0
|
||||
return x = 5
|
||||
}
|
||||
|
||||
assert_equal(5, return_assign())
|
||||
|
||||
def return_member_assign() {
|
||||
var o = Dynamic_Object()
|
||||
return o.value = 42
|
||||
}
|
||||
|
||||
assert_equal(42, return_member_assign())
|
||||
Loading…
x
Reference in New Issue
Block a user