mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-04-30 19:09:26 +08:00
* Fix #17: Add const local variable support to ChaiScript Adds `const var`, `const auto`, and `const` as variable declaration syntax that creates immutable local variables. A const_override flag on Boxed_Value enables script-level constness without changing the C++ type system integration. The parser, optimizer, and evaluator are extended with Const_Var_Decl and Const_Assign_Decl AST nodes that mirror their non-const counterparts but mark the value as const after initialization. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: remove const_override, set const flag directly on Type_Info Replace the m_const_override bool on Boxed_Value::Data with a Type_Info::make_const() method that sets the const bit in m_flags directly. This ensures constness is visible everywhere consistently, including places that check get_type_info().is_const() directly. Requested by @lefticus in PR #643 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: leftibot <leftibot@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
22 lines
633 B
ChaiScript
22 lines
633 B
ChaiScript
// Test const variable declarations
|
|
const var x = 5
|
|
assert_equal(5, x)
|
|
|
|
// Test that const variables cannot be reassigned
|
|
assert_throws("Error: \"Error, cannot assign to constant value.\"", fun() { const var y = 10; y = 20 })
|
|
|
|
// Test const with auto keyword
|
|
const auto z = "hello"
|
|
assert_equal("hello", z)
|
|
|
|
// Test that const auto variables cannot be reassigned
|
|
assert_throws("Error: \"Error, cannot assign to constant value.\"", fun() { const auto w = 3; w = 4 })
|
|
|
|
// Test is_var_const on const var
|
|
const var c = 42
|
|
assert_true(c.is_var_const())
|
|
|
|
// Test that non-const var is not const
|
|
var nc = 42
|
|
assert_false(nc.is_var_const())
|