This commit is contained in:
2025-06-22 15:11:37 +10:00
parent 5be3f2970c
commit 0667f79ff5
14 changed files with 508 additions and 319 deletions

View File

@@ -163,9 +163,9 @@ fn generate_expr_binary(f: &mut Formatter, bin: &ExprBinary, cx: Context) -> Res
| BinOp::Shl(_)
| BinOp::Shr(_)
| BinOp::Eq(_)
| BinOp::Ne(_)
| BinOp::Lt(_)
| BinOp::Le(_)
| BinOp::Ne(_)
| BinOp::Ge(_)
| BinOp::Gt(_)
| BinOp::And(_)
@@ -233,22 +233,22 @@ fn generate_expr_binary(f: &mut Formatter, bin: &ExprBinary, cx: Context) -> Res
BinOp::MulAssign(_) => assign_bin_op!("*"),
BinOp::Div(_) => bin_op!("/"),
BinOp::DivAssign(_) => assign_bin_op!("/"),
BinOp::Rem(_) => call_op!("math.fmod"),
BinOp::RemAssign(_) => assign_call_op!("math.fmod"),
BinOp::BitAnd(_) => call_op!("band"),
BinOp::BitAndAssign(_) => assign_call_op!("band"),
BinOp::BitOr(_) => call_op!("bor"),
BinOp::BitOrAssign(_) => assign_call_op!("bor"),
BinOp::BitXor(_) => call_op!("bxor"),
BinOp::BitXorAssign(_) => assign_call_op!("bxor"),
BinOp::Shl(_) => call_op!("lshift"),
BinOp::ShlAssign(_) => assign_call_op!("lshift"),
BinOp::Shr(_) => call_op!("arshift"),
BinOp::ShrAssign(_) => assign_call_op!("arshift"),
BinOp::Rem(_) => call_op!("__fmod"),
BinOp::RemAssign(_) => assign_call_op!("__fmod"),
BinOp::BitAnd(_) => call_op!("__band"),
BinOp::BitAndAssign(_) => assign_call_op!("__band"),
BinOp::BitOr(_) => call_op!("__bor"),
BinOp::BitOrAssign(_) => assign_call_op!("__bor"),
BinOp::BitXor(_) => call_op!("__bxor"),
BinOp::BitXorAssign(_) => assign_call_op!("__bxor"),
BinOp::Shl(_) => call_op!("__blshift"),
BinOp::ShlAssign(_) => assign_call_op!("__blshift"),
BinOp::Shr(_) => call_op!("__barshift"),
BinOp::ShrAssign(_) => assign_call_op!("__barshift"),
BinOp::Eq(_) => bin_op!("=="),
BinOp::Ne(_) => bin_op!("~="),
BinOp::Lt(_) => bin_op!("<"),
BinOp::Le(_) => bin_op!("<="),
BinOp::Ne(_) => bin_op!("~="),
BinOp::Ge(_) => bin_op!(">="),
BinOp::Gt(_) => bin_op!(">"),
BinOp::And(_) => bin_op!("and"),

View File

@@ -1,4 +1,4 @@
use crate::utils::{LuaType, syn_error, unwrap_expr_ident, unwrap_pat_ident, wrap_expr_block};
use crate::utils::{LuaType, expr_ident, pat_ident, syn_error, wrap_expr_block};
use quote::format_ident;
use std::mem;
use syn::{spanned::*, visit_mut::*, *};
@@ -73,7 +73,7 @@ impl Visitor {
match input {
Pat::Ident(_) => {}
Pat::Type(typed) => {
let ident = unwrap_pat_ident(&typed.pat)?;
let ident = pat_ident(&typed.pat)?;
let ty = mem::replace(&mut typed.ty, parse_quote!(_));
match (&*ty).try_into()? {
LuaType::Any => {}
@@ -112,7 +112,7 @@ impl Visitor {
Some((Ident::new("self", recv.self_token.span()), ty))
}
FnArg::Typed(typed) => {
let ident = unwrap_pat_ident(&typed.pat)?;
let ident = pat_ident(&typed.pat)?;
let ty = mem::replace(&mut typed.ty, parse_quote!(_));
Some((ident, ty))
}
@@ -149,9 +149,9 @@ impl Visitor {
let mut prelude: Option<Stmt> = None;
let ty: LuaType = (&*cast.ty).try_into()?;
let ty_str = format!("{ty}");
let (ident, msg) = match unwrap_expr_ident(&arg).ok() {
Some(ident) => (ident.clone(), format!("{ty} expected in '{ident}', got ")),
None => {
let (ident, msg) = match expr_ident(&arg) {
Ok(ident) => (ident.clone(), format!("{ty} expected in '{ident}', got ")),
Err(_) => {
let ident = Ident::new("_", arg.span());
prelude = Some(parse_quote! { let #ident = #arg; });
(ident, format!("{ty} expected, got "))

View File

@@ -25,14 +25,14 @@ pub fn wrap_expr_block(expr: &Expr) -> Block {
}
}
pub fn unwrap_expr_ident(expr: &Expr) -> Result<&Ident> {
pub fn expr_ident(expr: &Expr) -> Result<&Ident> {
match expr {
Expr::Path(path) => path.path.require_ident(),
_ => syn_error!(expr, "expected ident"),
}
}
pub fn unwrap_pat_ident(pat: &Pat) -> Result<Ident> {
pub fn pat_ident(pat: &Pat) -> Result<Ident> {
Ok(match pat {
Pat::Ident(ident) => match ident.subpat {
Some((_, ref subpat)) => syn_error!(subpat, "unexpected subpattern"),