From 96df4a3b3bd090d1c71f5d95bc00241540a5a095 Mon Sep 17 00:00:00 2001 From: Ming-Yang Lu Date: Tue, 17 Oct 2023 22:09:35 +0800 Subject: [PATCH] Add tuple support --- examples/memory_management/main.zng | 5 +++++ zngur-generator/src/rust.rs | 5 ++++- zngur-parser/src/lib.rs | 8 ++++++++ zngur-parser/src/tests.rs | 12 ++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/examples/memory_management/main.zng b/examples/memory_management/main.zng index 722f8dc..b6837cf 100644 --- a/examples/memory_management/main.zng +++ b/examples/memory_management/main.zng @@ -5,6 +5,11 @@ type () { wellknown_traits(Copy); } +type (i32, u32) { + #layout(size = 8, align = 4); + wellknown_traits(Copy); +} + type bool { #layout(size = 1, align = 1); wellknown_traits(Copy); diff --git a/zngur-generator/src/rust.rs b/zngur-generator/src/rust.rs index e397d41..df3cd5a 100644 --- a/zngur-generator/src/rust.rs +++ b/zngur-generator/src/rust.rs @@ -109,7 +109,10 @@ impl IntoCpp for RustType { if v.is_empty() { return CppType::from("rust::Unit"); } - todo!() + CppType { + path: CppPath::from("rust::Tuple"), + generic_args: v.into_iter().map(|x| x.into_cpp()).collect(), + } } RustType::Dyn(tr, marker_bounds) => { let tr_as_cpp_type = tr.into_cpp(); diff --git a/zngur-parser/src/lib.rs b/zngur-parser/src/lib.rs index e347a2b..a0e2a2c 100644 --- a/zngur-parser/src/lib.rs +++ b/zngur-parser/src/lib.rs @@ -725,6 +725,13 @@ fn rust_type<'a>( let unit = just(Token::ParenOpen) .then(just(Token::ParenClose)) .map(|_| ParsedRustType::Tuple(vec![])); + let tuple = parser + .clone() + .separated_by(just(Token::Comma)) + .allow_trailing() + .collect::>() + .delimited_by(just(Token::ParenOpen), just(Token::ParenClose)) + .map(|xs| ParsedRustType::Tuple(xs)); let slice = parser .clone() .map(|x| ParsedRustType::Slice(Box::new(x))) @@ -748,6 +755,7 @@ fn rust_type<'a>( scalar .or(boxed) .or(unit) + .or(tuple) .or(slice) .or(adt) .or(reference) diff --git a/zngur-parser/src/tests.rs b/zngur-parser/src/tests.rs index 624cf0a..db73b1f 100644 --- a/zngur-parser/src/tests.rs +++ b/zngur-parser/src/tests.rs @@ -35,6 +35,18 @@ type () { ); } +#[test] +fn parse_tuple() { + check_success( + r#" +type (i8, u8) { + #layout(size = 0, align = 1); +} + "#, + ); +} + + #[test] fn typo_in_wellknown_trait() { check_fail(