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>
111 lines
2.7 KiB
ChaiScript
111 lines
2.7 KiB
ChaiScript
// Basic enum class definition (default underlying type: int)
|
|
enum class Color { Red, Green, Blue }
|
|
|
|
// Access via :: syntax
|
|
auto r = Color::Red
|
|
auto g = Color::Green
|
|
auto b = Color::Blue
|
|
|
|
// Equality and inequality
|
|
assert_true(Color::Red == Color::Red)
|
|
assert_false(Color::Red == Color::Green)
|
|
assert_true(Color::Red != Color::Green)
|
|
assert_false(Color::Red != Color::Red)
|
|
|
|
// Constructor from valid underlying value
|
|
auto c = Color::Color(1)
|
|
assert_true(c == Color::Green)
|
|
|
|
// Constructor from invalid value throws
|
|
try {
|
|
Color::Color(52)
|
|
assert_true(false)
|
|
} catch(e) {
|
|
// expected
|
|
}
|
|
|
|
// Strong typing: function with typed parameter
|
|
def takes_color(Color val) { val }
|
|
takes_color(Color::Red)
|
|
takes_color(Color::Green)
|
|
takes_color(Color::Color(2))
|
|
|
|
// Cannot pass int where Color is expected
|
|
try {
|
|
takes_color(52)
|
|
assert_true(false)
|
|
} catch(e) {
|
|
// expected: dispatch error
|
|
}
|
|
|
|
// to_underlying accessor
|
|
assert_equal(0, Color::Red.to_underlying())
|
|
assert_equal(1, Color::Green.to_underlying())
|
|
assert_equal(2, Color::Blue.to_underlying())
|
|
|
|
// Enum class with explicit values
|
|
enum class Priority { Low = 10, Medium = 20, High = 30 }
|
|
assert_equal(10, Priority::Low.to_underlying())
|
|
assert_equal(20, Priority::Medium.to_underlying())
|
|
assert_equal(30, Priority::High.to_underlying())
|
|
|
|
auto p = Priority::Priority(20)
|
|
assert_true(p == Priority::Medium)
|
|
|
|
// Mixed auto and explicit values
|
|
enum class Status { Pending, Active = 5, Done }
|
|
assert_equal(0, Status::Pending.to_underlying())
|
|
assert_equal(5, Status::Active.to_underlying())
|
|
assert_equal(6, Status::Done.to_underlying())
|
|
|
|
// Switch on enum values
|
|
var result = ""
|
|
switch(Color::Green) {
|
|
case (Color::Red) {
|
|
result = "red"
|
|
break
|
|
}
|
|
case (Color::Green) {
|
|
result = "green"
|
|
break
|
|
}
|
|
case (Color::Blue) {
|
|
result = "blue"
|
|
break
|
|
}
|
|
}
|
|
assert_equal("green", result)
|
|
|
|
// Switch on enum with explicit values
|
|
var prio_result = ""
|
|
switch(Priority::High) {
|
|
case (Priority::Low) {
|
|
prio_result = "low"
|
|
break
|
|
}
|
|
case (Priority::Medium) {
|
|
prio_result = "medium"
|
|
break
|
|
}
|
|
case (Priority::High) {
|
|
prio_result = "high"
|
|
break
|
|
}
|
|
}
|
|
assert_equal("high", prio_result)
|
|
|
|
// Enum class with explicit underlying type
|
|
enum class Flags : char { Read = 1, Write = 2, Execute = 4 }
|
|
assert_equal(1, Flags::Read.to_underlying())
|
|
assert_equal(2, Flags::Write.to_underlying())
|
|
assert_equal(4, Flags::Execute.to_underlying())
|
|
|
|
auto f = Flags::Flags(2)
|
|
assert_true(f == Flags::Write)
|
|
|
|
// enum struct syntax (equivalent to enum class, like C++)
|
|
enum struct Direction { North, East, South, West }
|
|
assert_equal(0, Direction::North.to_underlying())
|
|
assert_equal(3, Direction::West.to_underlying())
|
|
assert_true(Direction::East != Direction::South)
|