Skip to main content
Lua 5.3 adds bitwise operators and floor division to the language.

Feature Flag

To parse Lua 5.3 syntax, you must enable the lua53 feature flag in your Cargo.toml:
[dependencies]
full_moon = { version = "2.1", features = ["lua53"] }
Note: The lua53 feature automatically enables lua52, so you get goto/label support as well.

Version-Specific Operators

Bitwise Binary Operators

Lua 5.3 introduces several bitwise operators as part of BinOp:
pub enum BinOp {
    // ... other operators
    
    #[cfg(feature = "lua53")]
    Ampersand(TokenReference),        // Bitwise AND: &
    
    #[cfg(feature = "lua53")]
    Pipe(TokenReference),             // Bitwise OR: |
    
    #[cfg(feature = "lua53")]
    Tilde(TokenReference),            // Bitwise XOR: ~
    
    #[cfg(feature = "lua53")]
    DoubleLessThan(TokenReference),   // Left shift: <<
    
    #[cfg(feature = "lua53")]
    DoubleGreaterThan(TokenReference), // Right shift: >>
    
    #[cfg(feature = "lua53")]
    DoubleSlash(TokenReference),      // Floor division: //
}

Operator Precedence

OperatorPrecedenceDescription
//10Floor division (same as *, /, %)
<<, >>7Bitwise shifts
&6Bitwise AND
~5Bitwise XOR
``4Bitwise OR

Examples

-- Bitwise AND
local result = 5 & 3  -- 1

-- Bitwise OR
local result = 5 | 3  -- 7

-- Bitwise XOR
local result = 5 ~ 3  -- 6

-- Left shift
local result = 1 << 3  -- 8

-- Right shift
local result = 8 >> 2  -- 2

-- Floor division
local result = 7 // 2  -- 3

Bitwise NOT (Unary Operator)

Lua 5.3 adds the bitwise NOT operator:
pub enum UnOp {
    Minus(TokenReference),
    Not(TokenReference),
    Hash(TokenReference),
    
    #[cfg(feature = "lua53")]
    Tilde(TokenReference),  // Bitwise NOT: ~
}

Example

-- Bitwise NOT
local result = ~5  -- -6 (two's complement)

Usage

use full_moon::parse;
use full_moon::ast::{BinOp, UnOp};

let ast = parse(r#"
  local x = 5 & 3
  local y = 8 >> 1
  local z = ~x
  local w = 7 // 2
"#)?;

Inherited Features

Since lua53 enables lua52, you also get:
  • Goto statements and labels (see Lua 5.2)
  • All Lua 5.2 syntax features

Notes

  • All bitwise operators work on integers
  • The ~ operator serves as both unary bitwise NOT and binary bitwise XOR depending on context
  • Floor division // always rounds toward negative infinity, unlike truncating division /
  • Lua 5.3 also supports integer literals with the 0x prefix and fractional hexadecimal numbers

Build docs developers (and LLMs) love