Remove type checking transformation in luaify
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
use crate::utils::{LuaType, expr_ident, pat_ident, syn_error, wrap_expr_block};
|
||||
use quote::format_ident;
|
||||
use std::mem;
|
||||
use crate::utils::syn_error;
|
||||
use syn::{spanned::*, visit_mut::*, *};
|
||||
|
||||
pub fn transform(expr: &mut Expr) -> Result<()> {
|
||||
@@ -27,27 +25,6 @@ impl Visitor {
|
||||
}
|
||||
|
||||
impl VisitMut for Visitor {
|
||||
fn visit_expr_closure_mut(&mut self, clo: &mut ExprClosure) {
|
||||
match self.transform_expr_closure(clo) {
|
||||
res @ Err(_) => self.result = res,
|
||||
_ => visit_expr_closure_mut(self, clo),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_item_fn_mut(&mut self, func: &mut ItemFn) {
|
||||
match self.transform_function(func) {
|
||||
res @ Err(_) => self.result = res,
|
||||
_ => visit_item_fn_mut(self, func),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_expr_mut(&mut self, expr: &mut Expr) {
|
||||
match self.transform_expr(expr) {
|
||||
res @ Err(_) => self.result = res,
|
||||
_ => visit_expr_mut(self, expr),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_expr_unary_mut(&mut self, un: &mut ExprUnary) {
|
||||
match self.transform_unary(un) {
|
||||
res @ Err(_) => self.result = res,
|
||||
@@ -64,147 +41,9 @@ impl VisitMut for Visitor {
|
||||
}
|
||||
|
||||
impl Visitor {
|
||||
fn transform_expr_closure(&mut self, clo: &mut ExprClosure) -> Result<()> {
|
||||
//
|
||||
// transforms a closure expression with input type annotations by removing the annotations
|
||||
// and inserting `as` casts at the start.
|
||||
//
|
||||
// before:
|
||||
// |a: string, b: number| { ... }
|
||||
// after:
|
||||
// |a, b| { a as string; b as number; ... }
|
||||
//
|
||||
let mut checks: Vec<Stmt> = vec![];
|
||||
for input in clo.inputs.iter_mut() {
|
||||
match input {
|
||||
Pat::Ident(_) => {}
|
||||
Pat::Type(typed) => {
|
||||
let ident = pat_ident(&typed.pat)?;
|
||||
let ty = mem::replace(&mut typed.ty, parse_quote!(_));
|
||||
match (&*ty).try_into()? {
|
||||
LuaType::Any => {}
|
||||
_ => checks.push(parse_quote! { #ident as #ty; }),
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if !checks.is_empty() {
|
||||
let mut body = wrap_expr_block(&clo.body);
|
||||
body.stmts.splice(..0, checks);
|
||||
clo.body = Box::new(parse_quote! { #body });
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn transform_function(&mut self, func: &mut ItemFn) -> Result<()> {
|
||||
//
|
||||
// transforms a function item with input type annotations by removing the annotations
|
||||
// and inserting `as` casts at the start.
|
||||
//
|
||||
// before:
|
||||
// fn my_func(self: table, a: string) { ... }
|
||||
// after:
|
||||
// fn my_func(self: _, a: _) { self as table; a as string; ... }
|
||||
//
|
||||
let mut checks: Vec<Stmt> = vec![];
|
||||
for input in func.sig.inputs.iter_mut() {
|
||||
if let Some((ident, ty)) = match input {
|
||||
FnArg::Receiver(recv) if recv.colon_token.is_some() => {
|
||||
let ty = mem::replace(&mut recv.ty, parse_quote!(_));
|
||||
recv.colon_token = None;
|
||||
Some((Ident::new("self", recv.self_token.span()), ty))
|
||||
}
|
||||
FnArg::Typed(typed) => {
|
||||
let ident = pat_ident(&typed.pat)?;
|
||||
let ty = mem::replace(&mut typed.ty, parse_quote!(_));
|
||||
Some((ident, ty))
|
||||
}
|
||||
_ => None,
|
||||
} {
|
||||
match (&*ty).try_into()? {
|
||||
LuaType::Any => {}
|
||||
_ => checks.push(parse_quote! { #ident as #ty; }),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
func.block.stmts.splice(..0, checks);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn transform_expr(&mut self, expr: &mut Expr) -> Result<()> {
|
||||
self.transform_expr_cast(expr)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn transform_expr_cast(&mut self, expr: &mut Expr) -> Result<()> {
|
||||
//
|
||||
// transforms an `as` cast expression into a block expression containing a runtime
|
||||
// lua type check.
|
||||
//
|
||||
// before:
|
||||
// var as string
|
||||
// after:
|
||||
// { if type(var) != "string" { error(...) } }
|
||||
//
|
||||
if let Expr::Cast(cast) = expr {
|
||||
let arg = (*cast.expr).clone();
|
||||
let mut prelude: Option<Stmt> = None;
|
||||
let ty: LuaType = (&*cast.ty).try_into()?;
|
||||
let ty_str = format!("{ty}");
|
||||
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 "))
|
||||
}
|
||||
};
|
||||
|
||||
let tmp = format_ident!("__{ident}");
|
||||
let span = cast.span();
|
||||
*expr = match ty {
|
||||
LuaType::Any => parse_quote_spanned!(span => {}),
|
||||
LuaType::Nil => parse_quote_spanned!(span => {
|
||||
#prelude
|
||||
assert(#ident == (), concat!(#msg, r#type(#ident)));
|
||||
}),
|
||||
LuaType::Number => parse_quote_spanned!(span => {
|
||||
#prelude
|
||||
let #tmp = #ident;
|
||||
#ident = tonumber(#ident);
|
||||
assert(#ident != (), concat!(#msg, r#type(#tmp)));
|
||||
}),
|
||||
LuaType::Integer => parse_quote_spanned!(span => {
|
||||
#prelude
|
||||
let #tmp = #ident;
|
||||
#ident = tonumber(#ident);
|
||||
assert(#ident != () && math::floor(#ident) == #ident, concat!(#msg, r#type(#tmp)));
|
||||
}),
|
||||
LuaType::String => parse_quote_spanned!(span => {
|
||||
#prelude
|
||||
if r#type(#ident) == "number" {
|
||||
#ident = tostring(#ident);
|
||||
} else {
|
||||
assert(r#type(#ident) == "string", concat!(#msg, r#type(#ident)));
|
||||
}
|
||||
}),
|
||||
_ => parse_quote_spanned!(span => {
|
||||
#prelude
|
||||
assert(r#type(#ident) == #ty_str, concat!(#msg, r#type(#ident)));
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn transform_unary(&mut self, un: &mut ExprUnary) -> Result<()> {
|
||||
//
|
||||
// separates a nested negation unary operator with parentheses, because double hyphen
|
||||
// separates a nested negation unary operator with parentheses, because the double hyphen
|
||||
// `--` indicates a comment in lua.
|
||||
//
|
||||
// before:
|
||||
@@ -216,7 +55,7 @@ impl Visitor {
|
||||
&& let Expr::Unary(ref inner) = *un.expr
|
||||
&& let UnOp::Neg(_) = inner.op
|
||||
{
|
||||
un.expr = Box::new(parse_quote!((#inner)));
|
||||
un.expr = Box::new(parse_quote_spanned!(inner.span() => (#inner)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user