Implement length macro in luaify
This commit is contained in:
parent
c3fb3407c4
commit
c9026123e6
@ -796,6 +796,7 @@ fn generate_receiver(f: &mut Formatter, recv: &Receiver) -> Result<()> {
|
||||
fn generate_macro(f: &mut Formatter, mac: &Macro, cx: Context) -> Result<()> {
|
||||
match format!("{}", mac.path.require_ident()?).as_str() {
|
||||
"concat" => generate_macro_concat(f, mac, cx),
|
||||
"len" => generate_macro_len(f, mac, cx),
|
||||
"variadic" => generate_macro_variadic(f, mac, cx),
|
||||
"embed" => generate_macro_embed(f, mac, cx),
|
||||
"raw" => generate_macro_raw(f, mac, cx),
|
||||
@ -815,6 +816,13 @@ fn generate_macro_concat(f: &mut Formatter, mac: &Macro, cx: Context) -> Result<
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_macro_len(f: &mut Formatter, mac: &Macro, cx: Context) -> Result<()> {
|
||||
syn_assert!(cx.is_value(), mac, "len! must be in expression position");
|
||||
cx.is_ret().then(|| f.write("return"));
|
||||
f.write("#");
|
||||
generate_expr(f, &mac.parse_body()?, Context::expr(false))
|
||||
}
|
||||
|
||||
fn generate_macro_variadic(f: &mut Formatter, mac: &Macro, cx: Context) -> Result<()> {
|
||||
syn_assert!(
|
||||
cx.is_multi_expr(),
|
||||
|
@ -391,3 +391,14 @@ fn variadic() {
|
||||
r#"function(a,...)local a,b=a,b,c,...;return func(a,b,c,d,...);end"#
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn length() {
|
||||
assert_eq!(luaify!(len!(a)), r#"#a"#);
|
||||
assert_eq!(
|
||||
luaify!({
|
||||
let (a, b, c) = (len!(a), len!(b), len!(c));
|
||||
}),
|
||||
r#"local a,b,c=#a,#b,#c;"#
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user