Skip to content

Fix #19: Add enum support#679

Merged
lefticus merged 8 commits intoChaiScript:developfrom
leftibot:fix/issue-19-add-enum-support
Apr 15, 2026
Merged

Fix #19: Add enum support#679
lefticus merged 8 commits intoChaiScript:developfrom
leftibot:fix/issue-19-add-enum-support

Conversation

@leftibot
Copy link
Copy Markdown
Contributor

Automated fix by @leftibot.

What changed

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) [email protected]

Files

 include/chaiscript/language/chaiscript_common.hpp |  8 ++-
 include/chaiscript/language/chaiscript_eval.hpp   | 83 +++++++++++++++++++++++
 include/chaiscript/language/chaiscript_parser.hpp | 83 ++++++++++++++++++++++-
 unittests/enum.chai                               | 59 ++++++++++++++++
 4 files changed, 229 insertions(+), 4 deletions(-)

Closes #19

Triggered by @lefticus.

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) <[email protected]>
@lefticus
Copy link
Copy Markdown
Member

@leftibot try to simplify this PR, if possible.

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 ChaiScript#679 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

a1fa15c4 Address review: simplify enum implementation

@lefticus
Copy link
Copy Markdown
Member

@leftibot

  • rename "to_int" to "to_underlying".
  • test usage with switch support

Requested by @lefticus in PR ChaiScript#679 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

18d97948 Address review: rename to_int to to_underlying, add switch tests

@lefticus
Copy link
Copy Markdown
Member

@leftibot resolve merge conflicts and review this code for compatibility / interaction with the newly added namespace support

@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

7c4673ca Merge remote-tracking branch 'origin/develop' into fix/issue-19-add-enum-support
e61be765 Fix #601: Allow operator functions to accept any type with an add() method (#686)
d4c5bdb3 Fix #61: Comprehensive exception test suite and fix for silently swallowed exceptions (#681)

@lefticus
Copy link
Copy Markdown
Member

@leftibot your attempt to fix the merge conflicts did not do anything, notice that the diffs between your two commits is nothing.

Merge upstream/develop (including namespace support from ChaiScript#552, WASM
support from ChaiScript#678, and grammar diagrams from ChaiScript#628) and adapt enum
implementation for compatibility with the :: dot-access semantics.

Enum values are now stored as attributes on a container Dynamic_Object,
aligning with how namespaces store members. The from_int() factory
method replaces direct constructor syntax since a name cannot be both
a global object (for :: access) and a callable constructor.

Requested by @lefticus in PR ChaiScript#679 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

2c807d8c Address review: resolve merge conflicts with upstream namespace support
9ff56426 Fix #552: Feature-request: nested namespaces (#675)
092ec417 Fix #628: Grammar railroad diagram (#673)
0fd9cab6 Fix #678: Add WASM exception support to Emscripten build (#689)

@lefticus
Copy link
Copy Markdown
Member

@leftibot instead of from_int I think we want just a constructor for the Enumeration that takes the underlying type. This makes sure we're not tied into int specifically and instead can make the underlying type configurable, like C++11. Actually, for consistency, we should support only enum class (making it consistent with C++'s strongly typed enums) and allow enum class Stuff : char or whatever to specify the underlying type.

…ng 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 ChaiScript#679 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

c08447d9 Address review: enum class syntax, constructor, configurable underlying type

@lefticus
Copy link
Copy Markdown
Member

@leftibot extend support for enum struct like C++ has. This should be a very simple 1 line change.

Requested by @lefticus in PR ChaiScript#679 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

f9f7c139 Address review: support enum struct syntax alongside enum class

@lefticus
Copy link
Copy Markdown
Member

@leftibot update documentation: EBNF, cheatsheet. Document what types can be used for the underlying type (can string by used)

…ation

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 ChaiScript#679 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@leftibot
Copy link
Copy Markdown
Contributor Author

Addressed review feedback from @lefticus. New commits:

aee12d70 Address review: update EBNF grammar and cheatsheet with enum documentation

@lefticus lefticus merged commit 1df1b4a into ChaiScript:develop Apr 15, 2026
21 of 22 checks passed
leftibot added a commit to leftibot/ChaiScript that referenced this pull request Apr 15, 2026
Carefully resolved merge conflicts to preserve both the strong typedef
(using) feature from this branch and the enum support (ChaiScript#679), nested
namespaces (ChaiScript#552), grammar diagram (ChaiScript#628), and WASM exception (ChaiScript#678)
features from upstream develop. All 376 tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add enum support

2 participants