Array types
This commit is contained in:
@@ -78,9 +78,7 @@ impl RustCodegen {
|
||||
}
|
||||
|
||||
pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> 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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ pub enum TypeExpr {
|
||||
return_type: Box<TypeExpr>,
|
||||
params: Vec<TypeExpr>,
|
||||
},
|
||||
Array(Box<TypeExpr>, Option<isize>)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
||||
@@ -24,6 +24,7 @@ pub enum ErrorCode {
|
||||
InvalidStatement,
|
||||
AstGenBug,
|
||||
Module,
|
||||
NumberParse,
|
||||
}
|
||||
|
||||
impl From<pest::error::Error<Rule>> for ParseError<'_> {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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)?,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user