mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-04-30 19:09:26 +08:00
* Fix #19: Add strongly-typed enum support to ChaiScript Adds the ability to define enums inside ChaiScript with syntax: enum Color { Red, Green, Blue } enum Priority { Low = 10, Medium = 20, High = 30 } Enum values are strongly typed Dynamic_Objects accessed via :: syntax (e.g. Color::Red). A validating constructor from int is registered that rejects values outside the defined range. Functions declared with an enum parameter type (e.g. def fun(Color val)) correctly reject plain integers, enforcing type safety at the dispatch level. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: simplify enum implementation Remove Enum_Access AST node type — reuse Id_AST_Node for enum value lookups by combining "EnumName::ValueName" at parse time. Replace std::set with std::vector for valid value tracking (enums are small). Net removal of ~18 lines. Requested by @lefticus in PR #679 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: rename to_int to to_underlying, add switch tests Requested by @lefticus in PR #679 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: enum class syntax, constructor, configurable underlying type - Change syntax from `enum` to `enum class` (only strongly-typed enums) - Support optional underlying type: `enum class Flags : char { ... }` (defaults to `int` when omitted) - Replace `from_int` with a constructor named after the enum type, accessed as `Color::Color(1)` — the underlying type is no longer hardcoded to int - Use Boxed_Number for type-generic value storage and comparison - to_underlying now returns the actual underlying type Note: `Color(1)` syntax is not possible because ChaiScript's global objects shadow functions with the same name; `Color::Color(1)` is the C++-consistent alternative. Requested by @lefticus in PR #679 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: support enum struct syntax alongside enum class Requested by @lefticus in PR #679 review. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Address review: update EBNF grammar and cheatsheet with enum documentation Add enum production rules to the EBNF grammar. Add comprehensive enum section to the cheatsheet covering syntax, explicit values, underlying type specification, construction, to_underlying, comparison, type-safe dispatch, and switch usage. Document that the underlying type must be a numeric type (string cannot be used) and list all available types. Requested by @lefticus in PR #679 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> |
||
|---|---|---|
| .. | ||
| dispatchkit | ||
| language | ||
| utility | ||
| chaiscript_basic.hpp | ||
| chaiscript_defines.hpp | ||
| chaiscript_stdlib.hpp | ||
| chaiscript_threading.hpp | ||
| chaiscript.hpp | ||