From 0d1ceed05d2fdff07ec0c8f3e3f03501b460860d Mon Sep 17 00:00:00 2001 From: leftibot Date: Sat, 11 Apr 2026 17:13:50 -0600 Subject: [PATCH] Fix #473: Allow assignment expressions in return statements (#665) 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: leftibot Co-authored-by: Claude Opus 4.6 (1M context) --- .../chaiscript/language/chaiscript_parser.hpp | 2 +- unittests/return_assignment.chai | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 unittests/return_assignment.chai diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index edfc0e06..be2a7525 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -2282,7 +2282,7 @@ namespace chaiscript { const auto prev_stack_top = m_match_stack.size(); if (Keyword("return")) { - Operator(); + Equation(); build_match>(prev_stack_top); return true; } else { diff --git a/unittests/return_assignment.chai b/unittests/return_assignment.chai new file mode 100644 index 00000000..02a008d4 --- /dev/null +++ b/unittests/return_assignment.chai @@ -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())