Expand description
Lua parser — translates the token stream produced by the lexer into
bytecode prototypes (LuaProto).
§C source
reference/lua-5.4.7/src/lparser.c (1968 lines, 95 functions)
§Design notes (Phase A)
BlockCntandLhsAssignform intrusive linked lists in C via raw pointers to stack-allocated nodes. In Rust they becomeOption<Box<...>>chains;enter_blockpushes,leave_blockpops.FuncState.prevsimilarly usesOption<Box<FuncState>>.FuncState.fisBox<LuaProto>during compilation (owned, mutably accessible). types.tsv maps it toGcRef<LuaProto>but interior- mutability viaRc<RefCell<...>>would be too noisy; Phase B can switch. PORT NOTE: FuncState.f is Box, not GcRef . LexStateis logically defined inlua-lex; a minimal stub is declared here for Phase A. Phase B will replace withlua_lex::LexStateonce inter-crate deps are wired.- Cross-crate calls to
lua_code::luaK_*andlua_lex::luaX_*are written as qualified paths and will resolve in Phase B. LuaStateis fromlua-vm; referenced here as an unresolved import.
Structs§
- Block
Cnt - In C: stack-allocated, chained via raw
*previouspointer. In Rust: heap-allocated in anOption<Box<BlockCnt>>chain on FuncState. - Cons
Control - PORT NOTE: C stores
expdesc *tas a pointer to the caller’s expdesc. Rust stores a copy of the table descriptor; callers must sync back if they mutate it. Phase B may restructure. - DynData
- C stored C-style dynamic arrays (arr/n/size); Rust uses Vec.
- Expr
Desc - Field
t/fare patch-lists for short-circuit boolean evaluation. - Expr
Payload - PORT NOTE: C uses a union; all arms share memory. Rust keeps all fields in one struct for Phase A simplicity. Phase B may refactor to a proper enum.
- Func
State - In C: stack-allocated in
body(), chained via raw*prevpointer. In Rust: heap-allocated viaOption<Box<FuncState>>in LexState. - Label
Desc - LexState
- PORT NOTE: This is a Phase A stub. In Phase B,
LexStatelives inlua-lexandlua-parseimports it.FuncStatewill move here or be passed separately. Thefsfield creates a circular-crate dependency that Phase B must resolve (likely: both live in one crate). - LexToken
- LhsAssign
- In C: stack-allocated, chained via raw
*prev. In Rust:Option<Box<...>>. - LuaState
- Per-thread Lua execution state.
- Token
Value - Semantic info attached to a token.
- VarDesc
- PORT NOTE: C uses a union (vd fields + k for const value). Rust keeps all
fields in a struct. The
const_valfield is only meaningful whenkind == VarKind::CompileTimeConst.
Enums§
- BinOpr
- Expr
Kind - Variants correspond exactly to the C enum in lparser.h.
- OpCode
- All opcodes for the Lua 5.4 virtual machine.
- UnOpr
- VarKind
Constants§
- TK_AND
- TK_
BREAK - TK_
CONCAT - TK_
DBCOLON - TK_DO
- TK_DOTS
- TK_ELSE
- TK_
ELSEIF - TK_END
- TK_EOS
- TK_EQ
- TK_
FALSE - TK_FLT
- TK_FOR
- TK_
FUNCTION - TK_GE
- TK_GOTO
- TK_IDIV
- TK_IF
- TK_IN
- TK_INT
- TK_LE
- TK_
LOCAL - TK_NAME
- TK_NE
- TK_NIL
- TK_NOT
- TK_OR
- TK_
REPEAT - TK_
RETURN - TK_SHL
- TK_SHR
- TK_
STRING - TK_THEN
- TK_TRUE
- TK_
UNTIL - TK_
WHILE