diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index c12c7e2..6d9b007 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -78,9 +78,7 @@ impl RustCodegen { } pub fn generate(&mut self, toplevels: Vec) -> String { - let mut ctx = Context { - expr_super: None, - }; + let mut ctx = Context { expr_super: None }; for tl in toplevels { tl.gen_rust(&mut ctx, self); @@ -275,6 +273,13 @@ impl GetRust for TypeExpr { return_type.get_rust(), ) } + Self::Array(ty, count) => { + if let Some(count) = count { + format!("[{}; {count}]", ty.get_rust()) + } else { + format!("[{}]", ty.get_rust()) + } + } } } } diff --git a/crates/codegen/src/statement.rs b/crates/codegen/src/statement.rs index a133d0b..38d7a2a 100644 --- a/crates/codegen/src/statement.rs +++ b/crates/codegen/src/statement.rs @@ -215,7 +215,11 @@ impl GenRust for VarDecl { .as_ref() .map(|t| { let t = vec![t.get_rust(); self.tuple_names.len() + 1]; - format!(": ({})", t.join(", ")) + if self.tuple_names.is_empty() { + format!(": {}", t.join(", ")) + } else { + format!(": ({})", t.join(", ")) + } }) .unwrap_or_default(), ); diff --git a/crates/parser/src/ast/mod.rs b/crates/parser/src/ast/mod.rs index 44ab48d..4483749 100644 --- a/crates/parser/src/ast/mod.rs +++ b/crates/parser/src/ast/mod.rs @@ -48,6 +48,7 @@ pub enum TypeExpr { return_type: Box, params: Vec, }, + Array(Box, Option) } #[derive(Debug, Clone, Serialize)] diff --git a/crates/parser/src/error.rs b/crates/parser/src/error.rs index dd635f2..22e7150 100644 --- a/crates/parser/src/error.rs +++ b/crates/parser/src/error.rs @@ -24,6 +24,7 @@ pub enum ErrorCode { InvalidStatement, AstGenBug, Module, + NumberParse, } impl From> for ParseError<'_> { diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 954a9ad..8ed88cb 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -141,8 +141,12 @@ void_type = { "void" } +array_type = { + "[" ~ integer? ~ "]" +} + type_expr = { - (void_type | path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type | fn_type)* + (void_type | path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type | fn_type | array_type)* } lifetime = { diff --git a/crates/parser/src/parser/common/types.rs b/crates/parser/src/parser/common/types.rs index 6726f41..8d8cac1 100644 --- a/crates/parser/src/parser/common/types.rs +++ b/crates/parser/src/parser/common/types.rs @@ -50,6 +50,25 @@ impl<'a> TryFrom> for TypeExpr { }; } + Rule::array_type => { + ty = TypeExpr::Array( + Box::new(ty), + ref_inner + .next() + .map(|v| { + v.as_str().parse().map_err(|_| AstError { + span: v.as_span(), + error_code: crate::error::ErrorCode::NumberParse, + error_message: format!( + "Number exceeds isize limits: {}", + v.as_str() + ), + }) + }) + .transpose()?, + ) + } + _ => AstError::bug_unimplemented(ref_pair)?, } }