Skip to main content

luna_core/
version.rs

1//! Lua dialect selection.
2//!
3//! Grammar and semantics switches are expressed as capability predicates, not
4//! version comparisons at use sites, so further dialects can be added by
5//! extending this enum only.
6
7/// Lua dialect the VM emulates. Drives lexer, parser, and runtime feature
8/// gating. `Lua55` is the primary; the others are compat modes; `MacroLua`
9/// (v1.3 Phase ML) is an additive dialect built on top of the 5.4 base.
10///
11/// **Enum ordering note**: `MacroLua` is placed **between `Lua54` and
12/// `Lua55`** on purpose. The capability predicates below use `>=` / `<=`
13/// comparisons; placing `MacroLua` immediately after `Lua54` means
14/// MacroLua inherits every 5.4-and-earlier capability for free while
15/// staying below the 5.5-only gate (`global` keyword, named vararg).
16#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
17pub enum LuaVersion {
18    /// Lua 5.1 — no integer subtype, `arg` table, no `goto`.
19    Lua51,
20    /// Lua 5.2 — adds `goto` / `bit32` / `\xXX` escapes, retires `setfenv`.
21    Lua52,
22    /// Lua 5.3 — adds native 64-bit integers, bitwise ops, `string.pack`.
23    Lua53,
24    /// Lua 5.4 — adds `<const>` / `<close>` attributes and integer-for spec.
25    Lua54,
26    /// MacroLua (v1.3 Phase ML) — 5.4 base + compile-time macros
27    /// (`@name(args)`, `@quote{...}`, `@if`, `@gensym`). Opt-in via
28    /// `Vm::new(LuaVersion::MacroLua)`; PUC 5.1-5.5 lexer/parser
29    /// behavior is **not** affected. See `docs/compatibility.md`
30    /// "MacroLua extensions".
31    MacroLua,
32    /// Lua 5.5 — adds `global` declarations and named vararg parameters.
33    Lua55,
34}
35
36impl LuaVersion {
37    /// Integer subtype and integer literals (5.3+).
38    pub fn has_integers(self) -> bool {
39        self >= LuaVersion::Lua53
40    }
41
42    /// `goto` / `::label::` (5.2+); `goto` is a reserved word.
43    pub fn has_goto(self) -> bool {
44        self >= LuaVersion::Lua52
45    }
46
47    /// `& | ~ << >>` operators (5.3+).
48    pub fn has_bitwise_ops(self) -> bool {
49        self >= LuaVersion::Lua53
50    }
51
52    /// `//` floor division (5.3+).
53    pub fn has_idiv(self) -> bool {
54        self >= LuaVersion::Lua53
55    }
56
57    /// `<const>` / `<close>` attributes on local declarations (5.4+).
58    pub fn has_attribs(self) -> bool {
59        self >= LuaVersion::Lua54
60    }
61
62    /// Hexadecimal float literals `0x1p4` (5.2+).
63    pub fn has_hex_float(self) -> bool {
64        self >= LuaVersion::Lua52
65    }
66
67    /// String escapes `\z`, `\xXX` (5.2+) and `\u{XXX}` (5.3+).
68    pub fn has_extended_escapes(self) -> bool {
69        self >= LuaVersion::Lua52
70    }
71
72    /// `\u{XXX}` unicode escape specifically (5.3+).
73    pub fn has_unicode_escape(self) -> bool {
74        self >= LuaVersion::Lua53
75    }
76
77    /// Empty statement `;` (5.2+).
78    pub fn has_empty_statement(self) -> bool {
79        self >= LuaVersion::Lua52
80    }
81
82    /// `global` declarations; `global` is a reserved word (5.5+).
83    pub fn has_global_decl(self) -> bool {
84        self >= LuaVersion::Lua55
85    }
86
87    /// Named vararg parameter `function f(...name)` (5.5+).
88    pub fn has_named_vararg(self) -> bool {
89        self >= LuaVersion::Lua55
90    }
91
92    /// Leading collective attribute in declarations: `local <const> a, b` (5.5+).
93    pub fn has_collective_attrib(self) -> bool {
94        self >= LuaVersion::Lua55
95    }
96
97    /// In 5.1 `break` is a "last statement" like `return`; later versions allow
98    /// it anywhere in a block.
99    pub fn break_is_last_statement(self) -> bool {
100        self == LuaVersion::Lua51
101    }
102
103    /// In 5.1, `[[` inside a level-0 long string is an error
104    /// ("nesting of [[...]] is deprecated").
105    pub fn rejects_nested_long_string(self) -> bool {
106        self == LuaVersion::Lua51
107    }
108
109    /// MacroLua dialect — compile-time macros enabled in lexer/parser.
110    /// Base semantics = Lua 5.4 (audit-locked, v1.3 Phase ML).
111    pub fn is_macro_lua(self) -> bool {
112        self == LuaVersion::MacroLua
113    }
114}