Array types

This commit is contained in:
2026-07-05 02:37:48 +02:00
parent b6256283a6
commit 13664413c5
5 changed files with 34 additions and 4 deletions
+8 -3
View File
@@ -78,9 +78,7 @@ impl RustCodegen {
} }
pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String { pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String {
let mut ctx = Context { let mut ctx = Context { expr_super: None };
expr_super: None,
};
for tl in toplevels { for tl in toplevels {
tl.gen_rust(&mut ctx, self); tl.gen_rust(&mut ctx, self);
@@ -275,6 +273,13 @@ impl GetRust for TypeExpr {
return_type.get_rust(), return_type.get_rust(),
) )
} }
Self::Array(ty, count) => {
if let Some(count) = count {
format!("[{}; {count}]", ty.get_rust())
} else {
format!("[{}]", ty.get_rust())
}
}
} }
} }
} }
+1
View File
@@ -48,6 +48,7 @@ pub enum TypeExpr {
return_type: Box<TypeExpr>, return_type: Box<TypeExpr>,
params: Vec<TypeExpr>, params: Vec<TypeExpr>,
}, },
Array(Box<TypeExpr>, Option<isize>)
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+1
View File
@@ -24,6 +24,7 @@ pub enum ErrorCode {
InvalidStatement, InvalidStatement,
AstGenBug, AstGenBug,
Module, Module,
NumberParse,
} }
impl From<pest::error::Error<Rule>> for ParseError<'_> { impl From<pest::error::Error<Rule>> for ParseError<'_> {
+5 -1
View File
@@ -141,8 +141,12 @@ void_type = {
"void" "void"
} }
array_type = {
"[" ~ integer? ~ "]"
}
type_expr = { 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 = { lifetime = {
+19
View File
@@ -50,6 +50,25 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> 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)?, _ => AstError::bug_unimplemented(ref_pair)?,
} }
} }