Skip to content

Commit bc5864a

Browse files
authored
Merge pull request #4 from CppCXY/refactor
Refactor
2 parents c3eb70f + dd4a50c commit bc5864a

32 files changed

+3297
-2551
lines changed

crates/luars/src/compiler/expr.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ use emmylua_parser::{
2020
LuaNameExpr, LuaUnaryExpr, LuaVarExpr,
2121
};
2222

23+
//======================================================================================
24+
// Helper functions
25+
//======================================================================================
26+
27+
/// Check if an expression is a vararg (...) literal
28+
fn is_vararg_expr(expr: &LuaExpr) -> bool {
29+
if let LuaExpr::LiteralExpr(lit) = expr {
30+
matches!(lit.get_literal(), Some(LuaLiteralToken::Dots(_)))
31+
} else {
32+
false
33+
}
34+
}
35+
2336
//======================================================================================
2437
// NEW API: ExpDesc-based expression compilation (Lua 5.4 compatible)
2538
//======================================================================================
@@ -585,11 +598,14 @@ fn compile_binary_expr_desc(c: &mut Compiler, expr: &LuaBinaryExpr) -> Result<Ex
585598
let concat_base = c.freereg;
586599
alloc_register(c); // for left operand copy
587600
alloc_register(c); // for right operand
588-
601+
589602
emit_move(c, concat_base, left_reg);
590603
emit_move(c, concat_base + 1, right_reg);
591-
emit(c, Instruction::encode_abc(OpCode::Concat, concat_base, 1, 0));
592-
604+
emit(
605+
c,
606+
Instruction::encode_abc(OpCode::Concat, concat_base, 1, 0),
607+
);
608+
593609
result_reg = concat_base;
594610
// Reset freereg to result_reg + 1 (concat consumes right operand)
595611
c.freereg = result_reg + 1;
@@ -1676,10 +1692,10 @@ fn compile_binary_expr_to(
16761692
emit_move(c, concat_reg, left_reg);
16771693
emit_move(c, concat_reg + 1, right_reg);
16781694
emit(c, Instruction::encode_abc(OpCode::Concat, concat_reg, 1, 0));
1679-
1695+
16801696
// Reset freereg (concat consumes right operand)
16811697
c.freereg = concat_reg + 1;
1682-
1698+
16831699
if let Some(d) = dest {
16841700
if d != concat_reg {
16851701
emit_move(c, d, concat_reg);
@@ -2618,8 +2634,7 @@ fn compile_table_expr_to(
26182634
if field.is_value_field() {
26192635
// Check if it's a simple value (not ... or call as last element)
26202636
if let Some(value_expr) = field.get_value_expr() {
2621-
let is_dots = matches!(&value_expr, LuaExpr::LiteralExpr(lit)
2622-
if matches!(lit.get_literal(), Some(LuaLiteralToken::Dots(_))));
2637+
let is_dots = is_vararg_expr(&value_expr);
26232638
let is_call = matches!(&value_expr, LuaExpr::CallExpr(_));
26242639
let is_last = i == fields.len() - 1;
26252640

@@ -2684,8 +2699,7 @@ fn compile_table_expr_to(
26842699
if field.is_value_field() {
26852700
// Array element or special case (vararg/call at end)
26862701
if let Some(value_expr) = field.get_value_expr() {
2687-
let is_dots = matches!(&value_expr, LuaExpr::LiteralExpr(lit)
2688-
if matches!(lit.get_literal(), Some(LuaLiteralToken::Dots(_))));
2702+
let is_dots = is_vararg_expr(&value_expr);
26892703
let is_call = matches!(&value_expr, LuaExpr::CallExpr(_));
26902704

26912705
if is_last_field && is_dots {

crates/luars/src/compiler/helpers.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,13 @@ pub fn emit_set_global(c: &mut Compiler, name: &str, src_reg: u32) {
409409
// k=false means C is a register index (not constant)
410410
emit(
411411
c,
412-
Instruction::create_abck(OpCode::SetTabUp, env_index as u32, const_idx, src_reg, false),
412+
Instruction::create_abck(
413+
OpCode::SetTabUp,
414+
env_index as u32,
415+
const_idx,
416+
src_reg,
417+
false,
418+
),
413419
);
414420
}
415421

crates/luars/src/compiler/stmt.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ use emmylua_parser::{
1717
LuaRepeatStat, LuaReturnStat, LuaStat, LuaVarExpr, LuaWhileStat,
1818
};
1919

20+
/// Check if an expression is a vararg (...) literal
21+
fn is_vararg_expr(expr: &LuaExpr) -> bool {
22+
if let LuaExpr::LiteralExpr(lit) = expr {
23+
matches!(lit.get_literal(), Some(LuaLiteralToken::Dots(_)))
24+
} else {
25+
false
26+
}
27+
}
28+
2029
/// Check if a block contains only a single unconditional jump statement (break/return only)
2130
/// Note: goto is NOT optimized by luac, so we don't include it here
2231
#[allow(dead_code)]
@@ -641,11 +650,7 @@ fn compile_return_stat(c: &mut Compiler, stat: &LuaReturnStat) -> Result<(), Str
641650

642651
// Check if last expression is varargs (...) or function call - these can return multiple values
643652
let last_is_multret = if let Some(last_expr) = exprs.last() {
644-
matches!(last_expr, LuaExpr::CallExpr(_))
645-
|| matches!(last_expr, LuaExpr::LiteralExpr(lit) if matches!(
646-
lit.get_literal(),
647-
Some(emmylua_parser::LuaLiteralToken::Dots(_))
648-
))
653+
matches!(last_expr, LuaExpr::CallExpr(_)) || is_vararg_expr(last_expr)
649654
} else {
650655
false
651656
};

0 commit comments

Comments
 (0)